X-Git-Url: https://git.harvie.cz/?p=svn%2FPrometheus-QoS%2F.git;a=blobdiff_plain;f=optional-tools%2Fhosts-ping.py;fp=optional-tools%2Fhosts-ping.py;h=f1a8fb6ac1a18786a8dfe8bc2313258fc0e4ff19;hp=0000000000000000000000000000000000000000;hb=00902283d975cc4cf209b670839f2cd58d756942;hpb=8eb6a62744fc5aa425ebd3d931a6145ea2898926 diff --git a/optional-tools/hosts-ping.py b/optional-tools/hosts-ping.py new file mode 100755 index 0000000..f1a8fb6 --- /dev/null +++ b/optional-tools/hosts-ping.py @@ -0,0 +1,149 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import ping, socket +import os, time + +# (G)2013 xChaos, Arachne Labs http://arachne.cz + SPOJE.NET http://spoje.net + +hosts = "/etc/hosts" +timeout = 1500 #timeout in ms +interval = 200 #ping interval in ms +attempts = 10 + +tld = ".czf" +domain = ".brevnov.czf" +smokeping_prefix = "Klienti" +smpater_prefix = "Backbone" +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_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 = """ + + + + + + + + + +""" +table_end = """ +
hosts ping (%s)
#hostnamereceivedavgbestworst
+
+

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

+""" + +def try_to_ping(host): + sum = 0.0 + best = None + worst = None + loss = 0 + + for i in range(0, attempts): + try: + delay = ping.Ping(host, timeout = timeout).do() #timeout in ms + time.sleep(interval/1000) + + if delay: + sum += delay + + if not best or best > delay: + best = delay + + if not worst or worst < delay: + worst = delay + + else: + loss += 1 + + except socket.error, e: + loss += 1 + + return (sum/attempts, best, worst, loss) + + +def smokenam_style(hostname, prefix, babble_length): + + if not tld in hostname: + hostname += domain + + babble = hostname.split('.') + return '.'.join([prefix,] + [a_tooth for a_tooth in reversed(babble)][1:babble_length] + ['-'.join(babble),]) + + +def append_host(html, host, base_url, counter): + 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'] + + if not host[column]: + host[column] = 0 #don't want it to be "None" type + + if host[column] > red_treshold[kolikaty]: + style[column] += ' color: red;' + 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)) + + if host['avg'] and host['best'] and host['worst']: + html.write( ('%1.2f%1.2f%1.2f' + "\n") + % (style['avg'], host['avg'], style['best'], host['best'], style['worst'], host['worst'])) + else: + html.write(3*('-' % style['loss']) + "\n") + +# main program + +smokeping = [] +smpater = [] + +for radek in open(hosts): + if radek[0] != '#': + is_smokeping = 'smokeping' in radek and not 'hidden' in radek + 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['avg'], host['best'], host['worst'], host['loss']) = try_to_ping(host['ip']) + + if is_smokeping: + host['smokename'] = smokenam_style(host['name'], smokeping_prefix, smokeping_babble_length) + smokeping.append(host) + else: + host['smokename'] = smokenam_style(host['name'], smpater_prefix, smpater_babble_length) + smpater.append(host) + +# smokeping + +html = open(smokeping_html, 'w') +html.write("

Smokeping - klientská 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) + +html.write(table_end); +html.close(); + +# smpater + +html = open(smpater_html, 'w') +html.write("

Smokeping - páteřní routery

"); +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) + +html.write(table_end); +html.close();