1
|
|
|
# Copyright 2013 Mathias WOLFF |
2
|
|
|
# This file is part of pyfreebilling. |
3
|
|
|
# |
4
|
|
|
# pyfreebilling is free software: you can redistribute it and/or modify |
5
|
|
|
# it under the terms of the GNU General Public License as published by |
6
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
# (at your option) any later version. |
8
|
|
|
# |
9
|
|
|
# pyfreebilling is distributed in the hope that it will be useful, |
10
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
# GNU General Public License for more details. |
13
|
|
|
# |
14
|
|
|
# You should have received a copy of the GNU General Public License |
15
|
|
|
# along with pyfreebilling. If not, see <http://www.gnu.org/licenses/> |
16
|
|
|
|
17
|
|
|
import re |
18
|
|
|
import ESL |
19
|
|
|
|
20
|
|
|
# Default |
21
|
|
|
defaultESLport = 8021 |
22
|
|
|
defaultESLsecret = 'ClueCon' |
23
|
|
|
conn_timeout = 5 |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
class FSinfo: |
27
|
|
|
"""Class that establishes connection to FreeSWITCH ESL Interface |
28
|
|
|
to retrieve statistics on operation. |
29
|
|
|
|
30
|
|
|
""" |
31
|
|
|
|
32
|
|
|
def __init__(self, host='127.0.0.1', port=defaultESLport, secret="ClueCon", |
33
|
|
|
autoInit=True): |
34
|
|
|
"""Initialize connection to FreeSWITCH ESL Interface. |
35
|
|
|
|
36
|
|
|
@param host: FreeSWITCH Host |
37
|
|
|
@param port: FreeSWITCH ESL Port |
38
|
|
|
@param secret: FreeSWITCH ESL Secret |
39
|
|
|
@param autoInit: If True connect to FreeSWITCH ESL Interface on |
40
|
|
|
instantiation. |
41
|
|
|
|
42
|
|
|
>>> fs = FSinfo(host='127.0.0.1', port=defaultESLport, secret="ClueCon") |
43
|
|
|
>>> print fs.getChannelCount() |
44
|
|
|
0 |
45
|
|
|
>>> print fs.getReloadGateway() |
46
|
|
|
|
47
|
|
|
""" |
48
|
|
|
# Set Connection Parameters |
49
|
|
|
self._eslconn = None |
50
|
|
|
self._eslhost = host or '127.0.0.1' |
51
|
|
|
self._eslport = int(port or defaultESLport) |
52
|
|
|
self._eslpass = secret or defaultESLsecret |
53
|
|
|
|
54
|
|
|
ESL.eslSetLogLevel(0) |
55
|
|
|
if autoInit: |
56
|
|
|
self._connect() |
57
|
|
|
|
58
|
|
|
def __del__(self): |
59
|
|
|
"""Cleanup.""" |
60
|
|
|
if self._eslconn is not None: |
61
|
|
|
del self._eslconn |
62
|
|
|
|
63
|
|
|
def _connect(self): |
64
|
|
|
"""Connect to FreeSWITCH ESL Interface.""" |
65
|
|
|
try: |
66
|
|
|
self._eslconn = ESL.ESLconnection(self._eslhost, |
67
|
|
|
str(self._eslport), |
68
|
|
|
self._eslpass) |
69
|
|
|
except: |
70
|
|
|
pass |
71
|
|
|
if not self._eslconn.connected(): |
72
|
|
|
raise Exception( |
73
|
|
|
"Connection to FreeSWITCH ESL Interface on host %s and port %d failed." |
74
|
|
|
% (self._eslhost, self._eslport) |
75
|
|
|
) |
76
|
|
|
|
77
|
|
|
def _execCmd(self, cmd, args): |
78
|
|
|
"""Execute command and return result body as list of lines. |
79
|
|
|
|
80
|
|
|
@param cmd: Command string. |
81
|
|
|
@param args: Command arguments string. |
82
|
|
|
@return: Result dictionary. |
83
|
|
|
|
84
|
|
|
""" |
85
|
|
|
command = cmd + " " + args |
86
|
|
|
print command |
87
|
|
|
print command.encode('utf-8') |
88
|
|
|
output = self._eslconn.sendRecv(command.encode('utf-8')) |
89
|
|
|
body = output.getBody() |
90
|
|
|
if body: |
91
|
|
|
return body.splitlines() |
92
|
|
|
return None |
93
|
|
|
|
94
|
|
|
def _execShowCmd(self, showcmd): |
95
|
|
|
"""Execute 'show' command and return result dictionary. |
96
|
|
|
|
97
|
|
|
@param cmd: Command string. |
98
|
|
|
@return: Result dictionary. |
99
|
|
|
|
100
|
|
|
""" |
101
|
|
|
result = None |
102
|
|
|
lines = self._execCmd("show", showcmd) |
103
|
|
|
if lines and len(lines) >= 2 and lines[0] != '' and lines[0][0] != '-': |
104
|
|
|
result = {} |
105
|
|
|
result['keys'] = lines[0].split(',') |
106
|
|
|
items = [] |
107
|
|
|
for line in lines[1:]: |
108
|
|
|
if line == '': |
109
|
|
|
break |
110
|
|
|
items.append(line.split(',')) |
111
|
|
|
result['items'] = items |
112
|
|
|
return result |
113
|
|
|
|
114
|
|
|
def _execShowCountCmd(self, showcmd): |
115
|
|
|
"""Execute 'show' command and return result dictionary. |
116
|
|
|
|
117
|
|
|
@param cmd: Command string. |
118
|
|
|
@return: Result dictionary. |
119
|
|
|
|
120
|
|
|
""" |
121
|
|
|
result = None |
122
|
|
|
lines = self._execCmd("api show", showcmd + " count") |
123
|
|
|
for line in lines: |
124
|
|
|
mobj = re.match('\s*(\d+)\s+total', line) |
125
|
|
|
if mobj: |
126
|
|
|
return int(mobj.group(1)) |
127
|
|
|
return result |
128
|
|
|
|
129
|
|
|
def getChannelCount(self): |
130
|
|
|
"""Get number of active channels from FreeSWITCH. |
131
|
|
|
|
132
|
|
|
@return: Integer or None. |
133
|
|
|
|
134
|
|
|
""" |
135
|
|
|
return self._execShowCountCmd("channels") |
136
|
|
|
|
137
|
|
|
def getReloadACL(self): |
138
|
|
|
"""Reload ACL""" |
139
|
|
|
result = None |
140
|
|
|
return self._execCmd("bgapi", "reloadacl") |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
def getReloadGateway(self, profile_name): |
144
|
|
|
"""Reload sofia's gateway""" |
145
|
|
|
result = None |
146
|
|
|
return self._execCmd("bgapi", "sofia profile " + profile_name + " rescan reloadxml") |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
def getRestartSofia(self, profile_name): |
150
|
|
|
"""Restart sofia profile""" |
151
|
|
|
result = None |
152
|
|
|
return self._execCmd("bgapi", "sofia profile " + profile_name + " restart") |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
# fs = FSinfo(host='127.0.0.1', port=defaultESLport, secret="ClueCon") |
156
|
|
|
# print fs.getChannelCount() |
157
|
|
|
# print fs.getReloadGateway("external") |
158
|
|
|
|