forked from MattMills/radiocapture-rf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatchdog.py
110 lines (91 loc) · 3.24 KB
/
watchdog.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
#!/usr/bin/env python
# Copyright 2019,2020 Radiocapture LLC - Radiocapture.com
import zmq
import json
import zlib
import time
class watchdog:
def __init__(self, host='127.0.0.1', port=50002):
context = zmq.Context()
self.socket = context.socket(zmq.REQ)
self.socket.setsockopt(zmq.LINGER, 0)
self.socket.connect("tcp://%s:%s" % (host, port))
#threading.Thread.__init__ (self, **kwds)
#self.setDaemon(1)
#self.start()
def handler(self, msg):
if msg['action'] == 'ALL_GET_STATUS':
resp['data'] = {}
for system in self.tb.systems:
try:
resp['data'][system] = self.tb.systems[system]['block']['quality'][-1]
except:
resp['data'][system] = -1
elif msg['action'] == 'ALL_GET_STATUS_AVG':
resp['data'] = {}
for system in self.tb.systems:
try:
l = self.tb.systems[system]['block']['quality']
resp['data'][system] = reduce(lambda x, y: x + y, l) / len(l)
except:
resp['data'][system] = -1
elif msg['action'] == 'ALL_GET_UPTIME':
resp['data'] = {}
for system in self.tb.systems:
try:
start = self.tb.systems[system]['start_time']
resp['data'][system] = time.time()-start
except:
resp['data'][system] = -1
else:
resp['fail'] = True
resp['data'] = 'UNKNOWN ACTION'
return resp
def send_message(self, msg):
resp = None
try:
self.socket.send(zlib.compress(json.dumps(msg)), zmq.NOBLOCK)
except:
return {'data': 'Timeout'}
loop_continue = True
loop_start = time.time()
while loop_continue:
try:
resp = json.loads(zlib.decompress(self.socket.recv(zmq.NOBLOCK)))
except Exception as e:
if e.errno == zmq.EAGAIN and time.time() - loop_start < 1:
pass #continue waiting for data
else:
loop_continue = False
if resp == None:
resp = {'data':'Timeout'}
return resp
def all_get_status(self):
return self.send_message({'action': 'ALL_GET_STATUS'})['data']
def all_get_status_avg(self):
return self.send_message({'action': 'ALL_GET_STATUS_AVG'})['data']
def all_get_uptime(self):
return self.send_message({'action': 'ALL_GET_UPTIME'})['data']
def restart_receiver(self, system):
return self.send_message({'action': 'RESTART_RECEIVER', 'system': system})['data']
if __name__ == '__main__':
w = watchdog()
while 1:
all_status = w.all_get_status()
all_status_avg = w.all_get_status_avg()
all_uptime = w.all_get_uptime()
if all_status == 'Timeout' or all_status_avg == 'Timeout' or all_uptime == 'Timeout':
print "Host connect timeout"
break
else:
print all_status
print all_status_avg
print all_uptime
for x in all_uptime:
if all_uptime[x] > 86400:
print 'Restart attempt on %s' % (x)
print w.restart_receiver(x)
#if all_uptime[x] > 300 and all_status_avg[x] < 0.8:
# print 'Restart attempt on %s' % (x)
# print w.restart_receiver(x)
time.sleep(10)