-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (49 loc) · 1.84 KB
/
main.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
import paho.mqtt.client as mqtt
import signal
import time
from datetime import datetime
from snmp import gather
try:
from config import *
except ModuleNotFoundError:
print('Copy over config.py.example to config.py and restart the script')
exit(0)
def trigger_update(publish):
apStats, assocAp, assocSsid = gather(SNMP_HOST, SNMP_PORT, SNMP_COMMUNITY)
for mac, ap in apStats.items():
for label, value in ap.items():
if label not in TOPIC_MAP:
continue
publish(PREFIX + TOPIC_MAP[label] % ap['aiAPName'], value)
if 'ap_mem_free' in TOPIC_MAP:
publish(
PREFIX + TOPIC_MAP['ap_mem_free'] % ap['aiAPName'],
round(ap['aiAPMemoryFree'] / ap['aiAPTotalMemory'] * 100, 1)
)
if 'ap_clients' in TOPIC_MAP:
for mac, count in assocAp.items():
publish(PREFIX + TOPIC_MAP['ap_clients'] % apStats[mac]['aiAPName'], count)
if 'ssid_connected' in TOPIC_MAP:
for ssid, count in assocSsid.items():
publish(PREFIX + TOPIC_MAP['ssid_connected'] % str(ssid), count)
if 'ssid_connected_total' in TOPIC_MAP:
publish(PREFIX + TOPIC_MAP['ssid_connected_total'], sum(assocSsid.values()))
if __name__ == '__main__':
client = mqtt.Client()
client.connect(MQTT_HOST, MQTT_PORT, 60)
client.loop_start()
done = False
def publish(topic, message):
client.publish(topic, str(message))
def do_exit(signal, frame):
global done
client.loop_stop()
done = True
signal.signal(signal.SIGINT, do_exit)
while not done:
now = datetime.now().timestamp()
trigger_update(publish)
sleeptime = now + UPDATE_INTERVAL - datetime.now().timestamp()
while sleeptime > 0 and not done:
sleeptime -= 1
time.sleep(1)