-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathip_status_trunks.php
138 lines (115 loc) · 4.18 KB
/
ip_status_trunks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* This file is part of SNEP.
*
* SNEP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SNEP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SNEP. If not, see <http://www.gnu.org/licenses/>.
*/
require_once "AMI.php";
$ami = new AMI ();
// Connect to database
$setup = parse_ini_file("setup.conf");
$res = "mysql:host=".$setup['db.host'].";dbname=".$setup['db.dbname'];
try {
$conn = new PDO($res,$setup["db.username"],$setup["db.password"]);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
// Get Trunks Sip from database - table: trunks
$like = 'SIP%';
$data = $conn->query("SELECT id,channel,callerid,host,username,type, disabled from trunks where channel LIKE '".$like."'");
// Popula troncos com itens faltantes do array
$troncos = array();
foreach ($data as $tronco) {
array_push($troncos, $tronco);
}
foreach ($troncos as $key => $val) {
$ami = new AMI ();
$troncos[$key]['status'] = "N.D.";
$troncos[$key]['latencia'] = "N.D.";
//Get information about peer
$sip_username = $val['type'] === "VIRTUAL" ? substr($val['channel'], 4): $val['username'];
$res_peer = $ami->get_sippeer($sip_username);
if ($res_peer) {
if (strripos($res_peer['status'], "ok") > 0 ) {
$troncos[$key]['latencia'] = $res_peer['status'];
} else {
$troncos[$key]['latencia'] = ucwords($res_peer['status']);
}
}
// Get information about registry
if ($val['type'] === "VIRTUAL") {
$troncos[$key]['host'] = $res_peer['tohost'] ;
}
$res_reg = $ami->get_SIPshowregistry($troncos[$key]['host'],$sip_username);
if ($res_reg) {
$troncos[$key]['status'] = $res_reg['state'];
}
}
foreach ($troncos as $key => $val) {
unset($troncos[$key]['channel']);
unset($troncos[$key]['username']);
if (trim($troncos[$key]['latencia']) === "") {
$troncos[$key]['latencia'] = "Unreachable";
}
if (trim($troncos[$key]['status']) === "") {
$troncos[$key]['status'] = "N.D.";
}
}
$troncos_sip = $troncos ;
/* -------------------------------------------------------------------------- */
// Get Trunks IAX from database - table: trunks
$like = 'IAX%';
$data = $conn->query("SELECT id,channel,callerid,host,username,type from trunks where channel LIKE '".$like."'");
// Popula troncos com itens faltantes do array
$troncos = array();
foreach ($data as $tronco) {
array_push($troncos, $tronco);
}
$peer_list = $ami->get_IAXpeerlist() ;
$reg_list = $ami->get_IAXregistry();
foreach ($troncos as $key => $val) {
$troncos[$key]['status'] = "N.D.";
$troncos[$key]['latencia'] = "N.D.";
// Get information about peer
$iax_username = $val['type'] === "VIRTUAL" ? substr($val['channel'], 5): $val['username'];
foreach ($peer_list as $pl_key => $pl_val) {
if ($pl_val['objectname'] === $iax_username) {
$troncos[$key]['latencia'] = $pl_val['status'];
}
}
//Get information about registry
if ($val['type'] === "VIRTUAL") {
$troncos[$key]['host'] = $res_peer['ipaddress'] ;
}
$res_reg = $ami->get_IAXregistry($troncos[$key]['host'],$iax_username);
if ($res_reg) {
$troncos[$key]['status'] = $res_reg['state'];
}
}
foreach ($troncos as $key => $val) {
unset($troncos[$key]['channel']);
unset($troncos[$key]['username']);
if ($troncos[$key]['latencia'] === "") {
$troncos[$key]['latencia'] = "Unreachable";
}
if (trim($troncos[$key]['status']) === "") {
$troncos[$key]['status'] = "N.D.";
}
}
$troncos_iax = $troncos ;
$troncos = array_merge($troncos_sip,$troncos_iax) ;
$out = array_values($troncos);
$ret = json_encode($out);
echo $ret ;