2017-01-07 11:46:50 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import web
|
2017-01-07 12:53:08 +00:00
|
|
|
import requests
|
|
|
|
import xmltodict
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import sys
|
2017-01-07 11:46:50 +00:00
|
|
|
import datetime
|
2019-08-14 11:01:29 +00:00
|
|
|
from collections import OrderedDict
|
|
|
|
from operator import itemgetter
|
2019-11-24 13:23:07 +00:00
|
|
|
import utils
|
2017-01-07 11:46:50 +00:00
|
|
|
|
|
|
|
urls = ('/', 'index',
|
2019-11-24 13:23:07 +00:00
|
|
|
'/help', 'help',
|
|
|
|
'/download', 'download'
|
2017-01-07 12:53:08 +00:00
|
|
|
)
|
2017-01-07 11:46:50 +00:00
|
|
|
|
|
|
|
app = web.application(urls, globals())
|
|
|
|
render = web.template.render('templates/', base="layout")
|
|
|
|
|
2017-01-07 12:53:08 +00:00
|
|
|
# Uncomment to turn debug off
|
2017-01-07 11:46:50 +00:00
|
|
|
web.config.debug = False
|
|
|
|
|
2017-01-07 12:53:08 +00:00
|
|
|
# Variables
|
2017-01-07 11:46:50 +00:00
|
|
|
output = []
|
|
|
|
|
2017-01-07 12:53:08 +00:00
|
|
|
# Functions
|
2019-10-25 15:48:24 +00:00
|
|
|
def calculate_count(data):
|
|
|
|
count = {}
|
2020-01-26 23:21:03 +00:00
|
|
|
z, nz = 0, 0
|
2020-02-25 14:05:31 +00:00
|
|
|
for _,v in data.items():
|
|
|
|
ls = v.values()
|
|
|
|
for v in ls:
|
|
|
|
if v == 0:
|
|
|
|
z += 1
|
|
|
|
else:
|
|
|
|
nz += 1
|
2019-10-25 15:48:24 +00:00
|
|
|
count['green'] = z
|
|
|
|
count['red'] = nz
|
|
|
|
return count
|
2019-08-16 17:07:50 +00:00
|
|
|
|
2020-02-25 14:05:31 +00:00
|
|
|
def arrange_services(services):
|
|
|
|
checks = {}
|
|
|
|
for service in services:
|
|
|
|
name = service['name']
|
|
|
|
name1, _, name2 = name.partition('-')
|
|
|
|
if name2 == '':
|
|
|
|
name2 = name1
|
2020-03-08 14:02:53 +00:00
|
|
|
name1 = 'others'
|
2020-02-25 14:05:31 +00:00
|
|
|
if name1 not in list(checks.keys()):
|
|
|
|
checks[name1] = OrderedDict()
|
|
|
|
checks[name1][name2] = int(service['status'])
|
|
|
|
return checks
|
|
|
|
|
|
|
|
def sorted_checks(checks):
|
|
|
|
s_checks = {}
|
|
|
|
for k,v in checks.iteritems():
|
|
|
|
s_checks[k] = OrderedDict(
|
|
|
|
sorted(v.iteritems(),key=itemgetter(1), reverse=True)
|
|
|
|
)
|
|
|
|
return s_checks
|
2019-08-16 17:07:50 +00:00
|
|
|
|
2017-01-07 11:46:50 +00:00
|
|
|
def getMonit():
|
|
|
|
output = []
|
|
|
|
xmlQuery = "/_status?format=xml"
|
|
|
|
|
|
|
|
with open('{0}/conf/servers.json'.format(os.path.expanduser('.'))) as f:
|
|
|
|
cf = json.loads(f.read())
|
|
|
|
|
|
|
|
for site in cf:
|
|
|
|
s = cf[site]
|
2017-01-07 12:53:08 +00:00
|
|
|
r = requests.get(s['url'] + xmlQuery,
|
|
|
|
auth=(s['user'], s['passwd']))
|
2017-01-07 11:46:50 +00:00
|
|
|
|
|
|
|
allstat = json.loads(json.dumps(xmltodict.parse(r.text)['monit']))
|
|
|
|
|
|
|
|
services = allstat['service']
|
2019-08-14 11:10:54 +00:00
|
|
|
server = {}
|
2017-01-07 11:46:50 +00:00
|
|
|
|
2020-02-25 14:05:31 +00:00
|
|
|
checks = arrange_services(services)
|
|
|
|
s_checks = sorted_checks(checks)
|
|
|
|
count = calculate_count(s_checks)
|
2019-11-24 13:25:21 +00:00
|
|
|
server = dict(name=site, url=s['url'],
|
2020-02-25 14:05:31 +00:00
|
|
|
result=s_checks, s_rate=count)
|
2017-01-07 11:46:50 +00:00
|
|
|
output.append(server)
|
|
|
|
print(datetime.datetime.now())
|
|
|
|
return(output)
|
|
|
|
|
2017-01-07 12:53:08 +00:00
|
|
|
# Classes
|
|
|
|
|
2020-01-26 23:21:03 +00:00
|
|
|
|
2017-01-07 11:46:50 +00:00
|
|
|
class monitDashboard(web.application):
|
|
|
|
def run(self, port=8080, *middleware):
|
|
|
|
func = self.wsgifunc(*middleware)
|
|
|
|
return web.httpserver.runsimple(func, ('0.0.0.0', port))
|
|
|
|
|
2017-01-07 12:53:08 +00:00
|
|
|
|
2017-01-07 11:46:50 +00:00
|
|
|
class index(object):
|
|
|
|
def GET(self):
|
2019-11-25 14:49:07 +00:00
|
|
|
return render.index(output=getMonit(),
|
2020-01-26 23:21:03 +00:00
|
|
|
now=datetime.datetime.now())
|
|
|
|
|
2017-01-07 11:46:50 +00:00
|
|
|
|
|
|
|
class help(object):
|
|
|
|
def GET(self):
|
|
|
|
return render.help()
|
|
|
|
|
2020-01-26 23:21:03 +00:00
|
|
|
|
2019-11-24 13:25:21 +00:00
|
|
|
class download(object):
|
|
|
|
def GET(self):
|
|
|
|
filename = 'health_report.xlsx'
|
|
|
|
output = getMonit()
|
|
|
|
utils.generate_report_excel(output, filename)
|
|
|
|
web.header('Content-Disposition',
|
2020-01-26 23:21:03 +00:00
|
|
|
'attachment; filename="health_report.xlsx"')
|
|
|
|
web.header('Content-type', 'application/octet-stream')
|
|
|
|
web.header('Cache-Control', 'no-cache')
|
2019-11-24 13:25:21 +00:00
|
|
|
return open(filename, 'rb').read()
|
|
|
|
|
2020-01-26 23:21:03 +00:00
|
|
|
|
2017-01-07 12:53:08 +00:00
|
|
|
# Main
|
2017-01-07 11:46:50 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
app = monitDashboard(urls, globals())
|
|
|
|
app.run(port=8080)
|