From bf2122f6cc02857942996b6ca73662f338341afc Mon Sep 17 00:00:00 2001 From: xchaos Date: Wed, 8 May 2013 21:17:00 +0000 Subject: [PATCH] hosts-ping: moving averages added git-svn-id: https://dev.arachne.cz/repos/prometheus/trunk@224 251d49ef-1d17-4917-a970-b30cf55b089b --- optional-tools/hosts-ping.py | 110 +++++++++++++++++++++++++++++------ 1 file changed, 91 insertions(+), 19 deletions(-) diff --git a/optional-tools/hosts-ping.py b/optional-tools/hosts-ping.py index f1a8fb6..740a74b 100755 --- a/optional-tools/hosts-ping.py +++ b/optional-tools/hosts-ping.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- import ping, socket -import os, time +import os, time, json # (G)2013 xChaos, Arachne Labs http://arachne.cz + SPOJE.NET http://spoje.net @@ -19,6 +19,10 @@ smokeping_babble_length = 3 smpater_babble_length = 2 smokeping_html = "/var/www/html/web/sites/sysifos/hosts-ping/index.html" smpater_html = "/var/www/html/web/sites/sysifos/hosts-ping/backbone.html" +smokeping_avg_html = "/var/www/html/web/sites/sysifos/hosts-ping/avg.html" +smpater_avg_html = "/var/www/html/web/sites/sysifos/hosts-ping/backbone-avg.html" +smokeping_avg_json = "/var/www/html/web/sites/sysifos/hosts-ping/smokeping.json" +smpater_avg_json = "/var/www/html/web/sites/sysifos/hosts-ping/smpater.json" smokeping_url = "http://sisyfos.brevnov.czf/cgi-bin/smokeping.cgi?filter=%s&target=%s" smpater_url = "http://tartarus.brevnov.czf/cgi-bin/smokeping.cgi?filter=%s&target=%s" table_head = """ @@ -26,7 +30,7 @@ table_head = """ hosts ping (%s) # hostname -received +loss avg best worst @@ -35,7 +39,7 @@ table_head = """ table_end = """
-

Page generated by (G)2013 xChaos hosts-ping version 0.1-a

+

Page generated by (G)2013 xChaos hosts-ping version 0.2-a

""" def try_to_ping(host): @@ -76,11 +80,9 @@ def smokenam_style(hostname, prefix, babble_length): return '.'.join([prefix,] + [a_tooth for a_tooth in reversed(babble)][1:babble_length] + ['-'.join(babble),]) -def append_host(html, host, base_url, counter): +def append_host(html, host, base_url, counter, red_treshold, green_treshold): style = {'right': 'text-align: right;'} columns = ('loss','avg','best','worst') - red_treshold = (0, 100, 50, 200) - green_treshold = (0, 2, 1, 10) for kolikaty, column in enumerate(columns): style[column] = style['right'] @@ -93,16 +95,50 @@ def append_host(html, host, base_url, counter): elif host[column] < green_treshold[kolikaty]: style[column] += ' color: green;' - received = attempts-host['loss'] - html.write( ('%d%s%d/%d' + "\n") - % (('even', 'odd')[counter % 2], style['right'], counter, base_url % (host['name'], host['smokename']), host['name'], style['loss'], received, attempts)) + loss = 100*host['loss']/host['attempts'] + html.write( ('%d%s%.1f%%' + "\n") + % (('even', 'odd')[counter % 2], style['right'], counter, base_url % (host['name'], host['smokename']), host['name'], style['loss'], loss)) if host['avg'] and host['best'] and host['worst']: - html.write( ('%1.2f%1.2f%1.2f' + "\n") + html.write( ('%.1f%.1f%.1f' + "\n") % (style['avg'], host['avg'], style['best'], host['best'], style['worst'], host['worst'])) else: html.write(3*('-' % style['loss']) + "\n") + +def merge_json_avgs(filename, smoke_array): + + try: + avg_dict = json.load(open(filename)) + + for host in smoke_array: + avg = avg_dict.get(host['ip']) + if avg: + if host['avg'] and avg['avg'] and avg['attempts']+host['attempts']-avg['loss']-host['loss'] > 0: + host['avg'] = ((avg['attempts']-avg['loss'])*avg['avg']+(host['attempts']-host['loss'])*host['avg'])/(avg['attempts']+host['attempts']-avg['loss']-host['loss']) + + if not host['best'] or host['best'] > avg['best']: + host['best'] = avg['best'] + + if not host['worst'] or host['worst'] < avg['worst']: + host['worst'] = avg['worst'] + + host['attempts'] += avg['attempts'] + host['loss'] += avg['loss'] + + except: + pass #start from scratch... + + +def save_json(filename, smoke_array): + smoke_dict = {} + + for host in smoke_array: + smoke_dict[host['ip']] = host + + json.dump(smoke_dict, open(filename, 'w')) + + # main program smokeping = [] @@ -114,7 +150,7 @@ for radek in open(hosts): is_smpater = 'smpater' in radek if is_smokeping or is_smpater: slovo = radek.split("\t") - host = { 'ip': slovo[0], 'name': slovo[1].split(' ')[0] } + host = { 'ip': slovo[0], 'name': slovo[1].split(' ')[0], 'attempts': attempts } (host['avg'], host['best'], host['worst'], host['loss']) = try_to_ping(host['ip']) if is_smokeping: @@ -126,24 +162,60 @@ for radek in open(hosts): # smokeping +red_treshold = (0, 100, 50, 200) +green_treshold = (0, 7, 5, 20) html = open(smokeping_html, 'w') -html.write("

Smokeping - klientská zařízení

"); +html.write("

Aktuální odezva klientských zařízení

"); html.write(table_head % time.ctime()); for kolikaty, host in enumerate(sorted(smokeping, key = lambda host: -host['loss']*attempts*timeout-host['avg'])): - append_host(html, host, smokeping_url, kolikaty+1) + append_host(html, host, smokeping_url, kolikaty+1, red_treshold, green_treshold) -html.write(table_end); -html.close(); +html.write(table_end) +html.close() # smpater +red_treshold = (0, 50, 20, 100) +green_treshold = (0, 5, 2, 10) html = open(smpater_html, 'w') -html.write("

Smokeping - páteřní routery

"); +html.write("

Aktuální odezva páteřních routerů

"); +html.write(table_head % time.ctime()); + +for kolikaty, host in enumerate(sorted(smpater, key = lambda host: -host['loss']*attempts*timeout-host['avg'])): + append_host(html, host, smpater_url, kolikaty+1, red_treshold, green_treshold) + +html.write(table_end) +html.close() + +# smokeping average + +red_treshold = (1000, 100, 20, 500) +green_treshold = (0, 7, 5, 20) +merge_json_avgs(smokeping_avg_json, smokeping) +html = open(smokeping_avg_html, 'w') +html.write("

Průměrná odezva klientských zařízení

"); +html.write(table_head % time.ctime()); + +for kolikaty, host in enumerate(sorted(smokeping, key = lambda host: -host['loss']*attempts*timeout-host['avg'])): + append_host(html, host, smokeping_url, kolikaty+1, red_treshold, green_treshold) + +html.write(table_end) +html.close() +save_json(smokeping_avg_json, smokeping) + +# smpater average + +red_treshold = (100, 50, 10, 200) +green_treshold = (0, 5, 2, 10) +merge_json_avgs(smpater_avg_json, smpater) +html = open(smpater_avg_html, 'w') +html.write("

Průměrná odezva páteřních routerů

"); html.write(table_head % time.ctime()); for kolikaty, host in enumerate(sorted(smpater, key = lambda host: -host['loss']*attempts*timeout-host['avg'])): - append_host(html, host, smpater_url, kolikaty+1) + append_host(html, host, smpater_url, kolikaty+1, red_treshold, green_treshold) -html.write(table_end); -html.close(); +html.write(table_end) +html.close() +save_json(smpater_avg_json, smpater) \ No newline at end of file -- 2.30.2