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

Revision 5566, 7.7 kB (checked in by p_lindheimer, 4 years ago)

fix bug in core_trunks_list(true) that could not handle same name of different technologies, requires dashboard fix which also uses this routine. Add partial support for DUNDi trunks in FreePBX (required manual configuration of dundi.conf, but full support to use in routing/trunks once configured

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