-
Notifications
You must be signed in to change notification settings - Fork 7
/
tdm.py
136 lines (102 loc) · 3.09 KB
/
tdm.py
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
#!/usr/bin/env python
# coding: utf-8
import sys
sys.path.append("../eventsocket")
import eventsocket
from twisted.python import log
from twisted.internet import defer, reactor, protocol
from config import config
class FreeSwitchESLProtocol(eventsocket.EventProtocol):
def __init__(self):
eventsocket.EventProtocol.__init__(self)
@defer.inlineCallbacks
def authRequest(self, ev):
# Try to authenticate in the eventsocket (Inbound)
# Please refer to http://wiki.freeswitch.org/wiki/Mod_event_socket#auth
# for more information.
try:
yield self.auth(config.freeSwitch.password)
except eventsocket.AuthError, e:
self.factory.continueTrying = False
self.factory.ready.errback(e)
#check for G729
g729_available = yield self.api('g729_available')
log.msg("G729: " + g729_available.data.rawresponse)
self.g729 = "true" in g729_available
self.factory.ready.callback(self)
@defer.inlineCallbacks
def g729_metrics(self):
if (self.g729):
g729_count = yield self.api('g729_count')
g729_count = int(g729_count)
statsd.gauge('freeswitch.g729.total', g729_count)
g729_counts = yield self.api('g729_used')
g729_enc, g729_dec = [int(e) for e in g729_counts.split(":")]
statsd.gauge('freeswitch.g729.used.encoder', g729_enc)
statsd.gauge('freeswitch.g729.used.decoder', g729_dec)
if (g729_enc > g729_dec):
statsd.gauge('freeswitch.g729.utilization', g729_enc / g729_count)
else:
statsd.gauge('freeswitch.g729.utilization', g729_dec / g729_count)
def tdmMetrics(self):
data = yield self.api('status')
print data.rawresponse
"""
+OK
span: 1 (wp1)
type: Sangoma (ISDN)
physical_status: ok
signaling_status: UP
chan_count: 24
dialplan: XML
context: belltdm
dial_regex:
fail_dial_regex:
hold_music:
analog_options: none
span_id: 1
chan_id: 1
physical_span_id: 1
physical_chan_id: 1
physical_status: ok
physical_status_red: 0
physical_status_yellow: 0
physical_status_rai: 0
physical_status_blue: 0
physical_status_ais: 0
physical_status_general: 0
signaling_status: UP
type: B
state: DOWN THIS IS WAHT YOU CARE ABOUT
last_state: HANGUP_COMPLETE
txgain: 0.00
rxgain: 0.00
cid_date:
cid_name:
cid_num:
ani:
aniII:
dnis:
rdnis:
cause: NONE
session: (none)
"""
class FreeSwitchESLFactory(protocol.ReconnectingClientFactory):
maxDelay = 15
protocol = FreeSwitchESLProtocol
def __init__(self):
self.ready = defer.Deferred()
@defer.inlineCallbacks
def main():
factory = FreeSwitchESLFactory()
reactor.connectTCP(config.freeSwitch.host, config.freeSwitch.port, factory)
# Wait for the connection to be established
try:
client = yield factory.ready
except Exception, e:
log.err("cannot connect: %s" % e)
defer.returnValue(None)
if __name__ == "__main__":
log.startLogging(sys.stdout)
main()
reactor.run()