diff --git a/.gitignore b/.gitignore index 1bb99d7..f707949 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.pyc .*.swp conf/servers.json +*.xlsx diff --git a/README.md b/README.md index 78b9f89..4bb6fb7 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ thanks to the built-in web server, it is displayed in a single HTML page. #### Python libaries - `yum install python-requests python-xmltodict python-simplejson` +- `pip install xlsxwriter` ## Requisites diff --git a/bin/monit-dashboard.py b/bin/monit-dashboard.py index f34b3c9..9cb90b1 100755 --- a/bin/monit-dashboard.py +++ b/bin/monit-dashboard.py @@ -9,9 +9,11 @@ import sys import datetime from collections import OrderedDict from operator import itemgetter +import utils urls = ('/', 'index', - '/help', 'help' + '/help', 'help', + '/download', 'download' ) app = web.application(urls, globals()) @@ -64,35 +66,44 @@ def getMonit(): checks[name] = status[name] sorted_checks = OrderedDict() - sorted_checks = OrderedDict(sorted(checks.iteritems(), key=itemgetter(1), reverse=True)) + sorted_checks = OrderedDict(sorted(checks.iteritems(), + key=itemgetter(1), reverse=True)) count = calculate_count(sorted_checks) - server = dict(name=site, url=s['url'], result=sorted_checks, s_rate=count) + server = dict(name=site, url=s['url'], + result=sorted_checks, s_rate=count) output.append(server) - print(datetime.datetime.now()) return(output) # Classes class monitDashboard(web.application): - def run(self, port=8080, *middleware): func = self.wsgifunc(*middleware) return web.httpserver.runsimple(func, ('0.0.0.0', port)) class index(object): - def GET(self): - return render.index(output=getMonit(), now=datetime.datetime.now()) - + return render.index(output=getMonit(), + now=datetime.datetime.now()) class help(object): - def GET(self): return render.help() +class download(object): + def GET(self): + filename = 'health_report.xlsx' + output = getMonit() + utils.generate_report_excel(output, filename) + web.header('Content-Disposition', + 'attachment; filename="health_report.xlsx"') + web.header('Content-type','application/octet-stream') + web.header('Cache-Control','no-cache') + return open(filename, 'rb').read() + # Main if __name__ == "__main__": app = monitDashboard(urls, globals()) diff --git a/bin/utils.py b/bin/utils.py new file mode 100644 index 0000000..cd58daa --- /dev/null +++ b/bin/utils.py @@ -0,0 +1,26 @@ +import xlsxwriter +import os + +def generate_report_excel(output, filename): + if os.path.exists(filename): + os.remove(filename) + workbook = xlsxwriter.Workbook(filename) + bold = workbook.add_format({'bold': 1}) + for server in range(len(output)): + worksheet = workbook.add_worksheet(output[server]['name']) + worksheet.set_column('A:A', 40) + worksheet.set_column('B:B', 15) + worksheet.write('A1', 'Components', bold) + worksheet.write('B1', 'Status', bold) + row = 1 + col = 0 + for key, value in output[server]['result'].items(): + worksheet.write_string(row, col, key) + if value == 0: + status = 'OK' + else: + status = 'Error' + worksheet.write_string(row, col + 1, key) + worksheet.write_string(row, col + 1, status) + row += 1 + workbook.close() diff --git a/static/monit-dashboard.css b/static/monit-dashboard.css index 839a91c..8e1b27e 100644 --- a/static/monit-dashboard.css +++ b/static/monit-dashboard.css @@ -33,7 +33,7 @@ a { } button.accordion { - color: #444; + color: #FFF; cursor: pointer; padding: 18px; width: 100%; @@ -41,7 +41,28 @@ button.accordion { border: none; outline: none; transition: 0.4s; - margin: 0.6rem; + margin: 0.6rem 0; +} + +button.download-button { + color: white; + background-color: #00a8ff; + cursor: pointer; + padding: 18px; + text-align: center; + border: none; + outline: none; + transition: 0.4s; + float: right; + margin: auto 0; +} + +div.download-header { + height: 7%; + background-color: #273c75; + display: flex; + justify-content: flex-end; + padding-right: 20px; } div.panel { @@ -73,7 +94,7 @@ div.panel.show { } .canvas-container { - height: 40%; + height: 35%; margin: auto; width: 24%; } diff --git a/static/monit-dashboard.js b/static/monit-dashboard.js index a77eb76..ef0c7d3 100644 --- a/static/monit-dashboard.js +++ b/static/monit-dashboard.js @@ -19,7 +19,7 @@ function draw(rate) { var red = (percentage[1]*2)/100; var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); - var colors = ['#40ff00', '#ff0000']; + var colors = ['#00a90e','#ff9a02']; var angles = [Math.PI * green, Math.PI * red]; var offset = 0; var beginAngle = 0; @@ -41,7 +41,7 @@ function draw(rate) { ctx.moveTo(200 + offsetX, 200 + offsetY); ctx.arc(200 + offsetX, 200 + offsetY, 120, beginAngle, endAngle); ctx.lineTo(200 + offsetX, 200 + offsetY); - ctx.stroke(); + // ctx.stroke(); ctx.fill(); ctx.rect(canvas.width - 129, i * 20 + 10, 10, 10); diff --git a/templates/index.html b/templates/index.html index 03f4885..ea8169f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -4,7 +4,10 @@ $def with (output, now) $ errors = 0 $ color = "green"
-
+
+ +
+
$for server in range(len(output)): $code: errors = 0