-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasterisk_monitor.py
139 lines (121 loc) · 4.19 KB
/
asterisk_monitor.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
137
138
139
import panoramisk
import asyncio
import sys
import json
import asterisk_ami_config
conf = asterisk_ami_config.ami_config
manager = panoramisk.Manager(
loop=asyncio.get_event_loop(),
host=conf['host'],
username=conf['username'],
secret=conf['secret'],
port=5038,
ssl=False,
encoding='utf8')
def callback(manager, event):
print(manager, event)
def parse_args():
mode = sys.argv[1]
item = sys.argv[2]
action_args = sys.argv[3:]
act_args = {}
for action_arg in action_args:
action_arg_key = action_arg.split('=')[0]
action_arg_value = action_arg.split('=')[1]
act_args[action_arg_key] = action_arg_value
return mode, item, act_args
async def zabbix_discovery(discovery, args=None):
await manager.connect()
mode, item, args = parse_args()
if discovery == 'pjsip_trunks':
endpoints_discovery = []
action = {
'Action': 'PJSIPShowRegistrationsOutbound'
}
manager.register_event('OutboundRegistrationDetail', callback)
result = await manager.send_action(action, **args)
response, *events, end = result
for event in events:
if event.event == "OutboundRegistrationDetail":
outbound_endpoint = event.endpoint
endpoints_discovery.append({"{#ENDPOINT_NAME}": outbound_endpoint})
print(json.dumps({'data': endpoints_discovery}))
else:
print("Discovery name does not exist")
async def zabbix_items(item, **args):
await manager.connect()
mode, item, args = parse_args()
if item == 'pjsip_trunk_registration':
action = {
'Action': 'PJSIPShowRegistrationsOutbound'
}
result = await manager.send_action(action, **args)
response, *events, end = result
status = pjsip_trunk_registration(events, **args)
return status
elif item == 'pjsip_trunk_status':
action = {
'Action': 'PJSIPShowEndpoints'
}
result = await manager.send_action(action)
response, *events, end = result
status = pjsip_device_state(events, **args)
return status
elif item == 'pjsip_trunk_rtt':
action = {
'Action': 'PJSIPShowEndpoint'
}
result = await manager.send_action(action, **args)
response, *events, end = result
status = pjsip_endpoint_rtt(events)
return status
else:
print("Item name does not exist")
def pjsip_endpoint_rtt(events):
for event in events:
if event.event == "ContactStatusDetail":
print(event.roundtripusec)
def pjsip_device_state(endpoints, **args):
for endpoint in endpoints:
if endpoint.aor == args['Endpoint']:
if endpoint.devicestate == "Available":
print("100")
elif endpoint.devicestate == "Not in use":
print("200")
elif endpoint.devicestate == "In use":
print("300")
elif endpoint.devicestate == "Busy":
print("400")
elif endpoint.devicestate == "Invalid":
print("500")
elif endpoint.devicestate == "Unavailable":
print("600")
elif endpoint.devicestate == "Ringing":
print("700")
elif endpoint.devicestate == "Ring in use":
print("800")
elif endpoint.devicestate == "On hold":
print("900")
def pjsip_trunk_registration(trunks, **args):
for trunk in trunks:
if trunk.event == "OutboundRegistrationDetail":
if trunk.endpoint == args['Endpoint']:
if trunk.status == "Registered":
print("1")
else:
print("0")
def main():
manager.connect()
mode, item, args = parse_args()
try:
args
except IndexError:
args = None
loop = asyncio.get_event_loop()
if mode == '-d':
output = loop.run_until_complete(zabbix_discovery(item, **args))
elif mode == '-i':
output = loop.run_until_complete(zabbix_items(item, **args))
return output
manager.close()
main()