Add timeout to dashboard, show offline status if node times out for web api

This commit is contained in:
KemoNine 2020-08-22 17:49:57 -04:00
parent 9e2f369ad4
commit de8b52b899

View file

@ -50,31 +50,36 @@ def getMonit():
cf = json.loads(f.read()) cf = json.loads(f.read())
for site in cf: for site in cf:
s = cf[site] try:
r = requests.get(s['url'] + xmlQuery, s = cf[site]
auth=(s['user'], s['passwd']), r = requests.get(s['url'] + xmlQuery,
timeout=5) auth=(s['user'], s['passwd']),
timeout=5)
allstat = json.loads(json.dumps(xmltodict.parse(r.text)['monit'])) allstat = json.loads(json.dumps(xmltodict.parse(r.text)['monit']))
services = allstat['service'] services = allstat['service']
status = {} status = {}
server = {} server = {}
checks = OrderedDict() checks = OrderedDict()
for service in services: for service in services:
name = service['name'] name = service['name']
status[name] = int(service['status']) status[name] = int(service['status'])
checks[name] = status[name] checks[name] = status[name]
sorted_checks = OrderedDict() sorted_checks = OrderedDict()
sorted_checks = OrderedDict(sorted(iter(checks.items()), sorted_checks = OrderedDict(sorted(iter(checks.items()),
key=itemgetter(1), reverse=True)) key=itemgetter(1), reverse=True))
count = calculate_count(sorted_checks) count = calculate_count(sorted_checks)
server = dict(name=site, url=s['url'], server = dict(name=site, url=s['url'],
result=sorted_checks, s_rate=count) result=sorted_checks, s_rate=count)
output.append(server) output.append(server)
except:
server = dict(name=site, url=s['url'],
result=OrderedDict({'online': '1'}), s_rate={'green': 0, 'red': 1})
output.append(server)
print((datetime.datetime.now())) print((datetime.datetime.now()))
return(output) return(output)