diff --git a/bin/monit-dashboard.py b/bin/monit-dashboard.py index d502b1a..237bfcb 100755 --- a/bin/monit-dashboard.py +++ b/bin/monit-dashboard.py @@ -10,6 +10,7 @@ import datetime from collections import OrderedDict from operator import itemgetter import utils +import re urls = ('/', 'index', '/help', 'help', @@ -26,21 +27,41 @@ web.config.debug = False output = [] # Functions - - def calculate_count(data): count = {} - ls = data.values() z, nz = 0, 0 - for v in ls: - if v == 0: - z += 1 - else: - nz += 1 + for _,v in data.items(): + ls = v.values() + for v in ls: + if v == 0: + z += 1 + else: + nz += 1 count['green'] = z count['red'] = nz return count +def arrange_services(services): + checks = {} + for service in services: + name = service['name'] + name1, _, name2 = name.partition('-') + if name2 == '': + name2 = name1 + name1 = 'others' + name1 = re.sub("([a-z])([A-Z])","\g<1> \g<2>",name1) + 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 def getMonit(): output = [] @@ -57,22 +78,13 @@ def getMonit(): allstat = json.loads(json.dumps(xmltodict.parse(r.text)['monit'])) services = allstat['service'] - status = {} server = {} - checks = OrderedDict() - for service in services: - name = service['name'] - status[name] = int(service['status']) - checks[name] = status[name] - - sorted_checks = OrderedDict() - sorted_checks = OrderedDict(sorted(checks.iteritems(), - key=itemgetter(1), reverse=True)) - count = calculate_count(sorted_checks) + checks = arrange_services(services) + s_checks = sorted_checks(checks) + count = calculate_count(s_checks) server = dict(name=site, url=s['url'], - result=sorted_checks, s_rate=count) - + result=s_checks, s_rate=count) output.append(server) print(datetime.datetime.now()) return(output) diff --git a/static/monit-dashboard.css b/static/monit-dashboard.css index deba533..a547a80 100644 --- a/static/monit-dashboard.css +++ b/static/monit-dashboard.css @@ -10,6 +10,11 @@ th { padding: 10px; } +.th-custom { + padding: 10px; + font-weight: 300; +} + td { border-bottom: 1px solid #ccc; padding: 15px; @@ -68,11 +73,11 @@ div.download-header { div.panel { padding: 0 18px; background-color: white; - display: none; + display: block; } div.panel.show { - display: block; + display: none; } .green { @@ -117,3 +122,11 @@ div.panel.show { background-color: #F2F2F2; text-align: center; } + +tbody.toggle-id { + display: none; +} + +tbody.toggle-id.show { + display: contents; +} diff --git a/static/monit-dashboard.js b/static/monit-dashboard.js index 2e9b523..346ebbb 100644 --- a/static/monit-dashboard.js +++ b/static/monit-dashboard.js @@ -8,6 +8,16 @@ for (i = 0; i < acc.length; i++) { } } +var acc = document.getElementsByClassName("inner-rows"); +var i; + +for (i = 0; i < acc.length; i++) { + acc[i].onclick = function() { + this.classList.toggle("active"); + this.nextElementSibling.classList.toggle("show"); + } +} + function draw(rate) { var count = [rate['green'], rate['red']]; var percentage = [ @@ -41,13 +51,11 @@ 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.fill(); ctx.rect(canvas.width - 129, i * 20 + 10, 10, 10); ctx.fill(); ctx.font = "13px sans-serif"; - //ctx.font = "20px Georgia"; ctx.fillText(status[i] + " - " + count[i] + " (" + Number(percentage[i]).toFixed(1) + "%)", canvas.width - 109, i * 20 + 20); diff --git a/templates/index.html b/templates/index.html index 002a631..f6d0a85 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,5 +1,36 @@ $def with (output, now) + +$def row_error(check): + + $check + + + +$def row_ok(check): + + $check + + + +$def table_contents(color, text, services): + + + $text + + + + $for check in services.keys(): + $ isError = services.get(check) + $if isError != 0: + $:row_error(check) + $else: + $:row_ok(check) + + + $ errors = 0 $ color = "green" @@ -13,11 +44,12 @@ $def with (output, now) errors = 0 color = "green" - $for check in output[server]['result'].keys(): - $ isError = output[server]['result'].get(check) - $if isError != 0: - $code: - errors=errors+1 + $for _,v in output[server]['result'].items(): + $for check in v.keys(): + $ isError = v.get(check) + $if isError != 0: + $code: + errors=errors+1 $if errors > 0: $code: @@ -27,26 +59,26 @@ $def with (output, now)
- + + $for env, services in output[server]['result'].items(): + $ errors = 0 + $for check in services.keys(): + $ isError = services.get(check) $if isError != 0: - - - - - $else: - - - - - + $ errors=errors+1 + $if errors > 0: + $code: + color = "red" + txt = "{}: {} error(s)".format(env, errors) + $:table_contents(color, txt, services) + $else: + $code: + color = "green" + $:table_contents(color, env, services)
$check
$check
$else: @@ -57,26 +89,25 @@ $def with (output, now)
- + + $for env, services in output[server]['result'].items(): + $ errors = 0 + $for check in services.keys(): + $ isError = services.get(check) $if isError != 0: - - - - - $else: - - - - - + $ errors=errors+1 + $if errors > 0: + $code: + color = "red" + txt = "{}: {} error(s)".format(env, errors) + $:table_contents(color, txt, services) + $else: + $ color = "green" + $:table_contents(color, env, services)
$check
$check