root/modules/branches/2.3/dashboard/class.astinfo.php

Revision 4718, 7.1 kB (checked in by gregmac, 5 years ago)

Fix issue with recounting total registrations

Line 
1 <?php
2
3 class astinfo {
4   var $astman;
5  
6   function astinfo(&$astman) {
7     $this->astman =& $astman;
8   }
9  
10   function get_channel_totals() {
11     if (!$this->astman) {
12       return array(
13         'external_calls'=>0,
14         'internal_calls'=>0,
15         'total_calls'=>0,
16         'total_channels'=>0,
17       );
18     }
19     $response = $this->astman->send_request('Command',array('Command'=>"show channels"));
20     $astout = explode("\n",$response['data']);
21    
22     $external_calls = 0;
23     $internal_calls = 0;
24     $total_calls = 0;
25     $total_channels = 0;
26    
27     foreach ($astout as $line) {
28       if (preg_match('/s@macro-dialout/', $line)) {
29         $external_calls++;
30       } else if (preg_match('/s@macro-dial:/', $line)) {
31         $internal_calls++;
32       } else if (preg_match('/^(\d+) active channel/i', $line, $matches)) {
33         $total_channels = $matches[1];
34       } else if (preg_match('/^(\d+) active call/i', $line, $matches)) {
35         $total_calls = $matches[1];
36       }
37     }
38     return array(
39       'external_calls'=>$external_calls,
40       'internal_calls'=>$internal_calls,
41       'total_calls'=>$total_calls,
42       'total_channels'=>$total_channels,
43     );
44   }
45  
46   function get_connections($trunks = false) {
47     if (!$trunks) {
48       $trunks = array();
49     }
50    
51     $return = array(
52       'sip_users_online' => 0,
53       'sip_users_offline' => 0,
54       'sip_users_total' => 0,
55       'sip_trunks_online' => 0,
56       'sip_trunks_offline' => 0,
57       'sip_trunks_total' => 0,
58       'sip_registrations_online' => 0,
59       'sip_registrations_offline' => 0,
60       'sip_registrations_total' => 0,
61
62      
63       'iax2_users_online' => 0,
64       'iax2_users_offline' => 0,
65       'iax2_users_total' => 0,
66       'iax2_trunks_online' => 0,
67       'iax2_trunks_offline' => 0,
68       'iax2_trunks_total' => 0,
69       'iax2_registrations_online' => 0,
70       'iax2_registrations_offline' => 0,
71       'iax2_registrations_total' => 0,
72
73       //totals
74       'users_online' => 0,
75       'users_offline' => 0,
76       'users_total' => 0,
77       'trunks_online' => 0,
78       'trunks_offline' => 0,
79       'trunks_total' => 0,
80       'registrations_online' => 0,
81       'registrations_offline' => 0,
82       'registrations_total' => 0,
83     );
84
85     if (!$this->astman) {
86       return $return;
87     }
88
89     $response = $this->astman->send_request('Command',array('Command'=>"sip show peers"));
90     $astout = explode("\n",$response['data']); 
91     foreach ($astout as $line) {
92       if (preg_match('/^(([a-z0-9\-_]+)(\/([a-z0-9\-_]+))?)\s+(\([a-z]+\)|\d{1,3}(\.\d{1,3}){3})/i', $line, $matches)) {
93         //matches: [2] = name, [4] = username, [5] = host, [6] = part of ip (if IP)
94
95         // have an IP address listed, so its online
96         $online = !empty($matches[6]);
97
98         if (isset($trunks[$matches[2]])) {
99           // this is a trunk
100           //TODO match trunk tech as well?
101           $return['sip_trunks_'.($online?'online':'offline')]++;
102         } else {
103           $return['sip_users_'.($online?'online':'offline')]++;
104         }
105       }
106     }
107    
108    
109     $response = $this->astman->send_request('Command',array('Command'=>"sip show registry"));
110     $astout = explode("\n",$response['data']);
111     $pos = false;
112     foreach ($astout as $line) {
113       if (trim($line) != '') {
114         if ($pos===false) {
115           // find the position of "State" in the first line
116           $pos = strpos($line,"State");
117         } else {
118           // subsequent lines, check if it syas "Registered" at that position
119           if (substr($line,$pos,10) == "Registered") {
120             $return['sip_registrations_online']++;
121           } else {
122             $return['sip_registrations_offline']++;
123           }
124         }
125       }
126     }
127
128    
129     $response = $this->astman->send_request('Command',array('Command'=>"iax2 show peers"));
130     $astout = explode("\n",$response['data']);
131     foreach ($astout as $line) {
132       if (preg_match('/^(([a-z0-9\-_]+)(\/([a-z0-9\-_]+))?)\s+(\([a-z]+\)|\d{1,3}(\.\d{1,3}){3})/i', $line, $matches)) {
133         //matches: [2] = name, [4] = username, [5] = host, [6] = part of ip (if IP)
134
135         // have an IP address listed, so its online
136         $online = !empty($matches[6]);
137
138         if (isset($trunks[$matches[2]])) {
139           // this is a trunk
140           //TODO match trunk tech as well?
141           $return['iax2_trunks_'.($online?'online':'offline')]++;
142         } else {
143           $return['iax2_users_'.($online?'online':'offline')]++;
144         }
145       }
146     }
147    
148    
149     $response = $this->astman->send_request('Command',array('Command'=>"iax2 show registry"));
150     $astout = explode("\n",$response['data']);
151     $pos = false;
152     foreach ($astout as $line) {
153       if (trim($line) != '') {
154         if ($pos===false) {
155           // find the position of "State" in the first line
156           $pos = strpos($line,"State");
157         } else {
158           // subsequent lines, check if it syas "Registered" at that position
159           if (substr($line,$pos,10) == "Registered") {
160             $return['sip_registrations_online']++;
161           } else {
162             $return['sip_registrations_offline']++;
163           }
164         }
165       }
166     }
167
168    
169     $return['sip_users_total'] = $return['sip_users_online'] + $return['sip_users_offline'];
170     $return['sip_trunks_total'] = $return['sip_trunks_online'] + $return['sip_trunks_offline'];
171     $return['sip_registrations_total'] = $return['sip_registrations_online'] + $return['sip_registrations_offline'];
172
173     $return['iax2_users_total'] = $return['iax2_users_online'] + $return['iax2_users_offline'];
174     $return['iax2_trunks_total'] = $return['iax2_trunks_online'] + $return['iax2_trunks_offline'];
175     $return['iax2_registrations_total'] = $return['iax2_registrations_online'] + $return['iax2_registrations_offline'];
176
177     $return['users_online'] = $return['sip_users_online'] + $return['iax2_users_online'];
178     $return['users_offline'] = $return['sip_users_offline'] + $return['iax2_users_offline'];
179     $return['users_total'] = $return['users_online'] + $return['users_offline'];
180    
181     $return['trunks_online'] = $return['sip_trunks_online'] + $return['iax2_trunks_online'];
182     $return['trunks_offline'] = $return['sip_trunks_offline'] + $return['iax2_trunks_offline'];
183     $return['trunks_total'] = $return['trunks_online'] + $return['trunks_offline'];
184
185     $return['registrations_online'] = $return['sip_registrations_online'] + $return['iax2_registrations_online'];
186     $return['registrations_offline'] = $return['sip_registrations_offline'] + $return['iax2_registrations_offline'];
187     $return['registrations_total'] = $return['registrations_online'] + $return['registrations_offline'];
188
189     return $return;
190   }
191  
192   function get_uptime() {
193     /*
194     System uptime: 1 week, 4 days, 22 hours, 29 minutes, 21 seconds
195     Last reload: 1 week, 1 day, 6 hours, 14 minutes, 49 seconds
196     */
197     $output = array(
198       'system' => '',
199       'reload' => '',
200     );
201
202     if (!$this->astman) {
203       return $output;
204     }
205
206     $response = $this->astman->send_request('Command',array('Command'=>"show uptime"));
207     $astout = explode("\n",$response['data']);
208      
209     foreach ($astout as $line) {
210       if (preg_match('/^System uptime: (.*)$/i',$line,$matches)) {
211         $output["system"] = preg_replace('/,\s+(\d+ seconds?)?\s*$/', '', $matches[1]);       
212       } else if (preg_match('/^Last reload: (.*)$/i',$line,$matches)) {
213         $output["reload"] = preg_replace('/,\s+(\d+ seconds?)?\s*$/', '', $matches[1]);
214       }
215     }
216    
217     return $output;
218   }
219  
220   function check_asterisk() {
221     if (!$this->astman) {
222       return false;
223     }
224     $response = $this->astman->send_request('Command',array('Command'=>"show version"));
225     $astout = explode("\n",$response['data']);
226    
227     if (!preg_match('/^Asterisk /i', $astout[1])) {
228       return false;
229     } else {
230       return $astout[1];
231     }
232   }
233 }
234
235 ?>
Note: See TracBrowser for help on using the browser.