--- /dev/null
+#!/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 class="decorated last">
+<caption>hosts ping (%s)</caption><thead><tr>
+<th style="text-align: right;">#</th>
+<th>hostname</th>
+<th style="text-align: right;">received</th>
+<th style="text-align: right;">avg</th>
+<th style="text-align: right;">best</th>
+<th style="text-align: right;">worst</th>
+</tr></thead><tbody>
+"""
+table_end = """
+</tbody></table>
+<br />
+<p>Page generated by (G)2013 xChaos hosts-ping version 0.1-a</p>
+"""
+
+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( ('<tr class="%s"><td style="%s">%d</td><td><a href="%s" target="_blank" class="blue">%s</a></td><td style="%s">%d/%d</td>' + "\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( ('<td style="%s">%1.2f</td><td style="%s">%1.2f</td><td style="%s">%1.2f</td></tr>' + "\n")
+ % (style['avg'], host['avg'], style['best'], host['best'], style['worst'], host['worst']))
+ else:
+ html.write(3*('<td style="%s">-</td>' % 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("<h1>Smokeping - klientská zařízení</h1>");
+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("<h1>Smokeping - páteřní routery</h1>");
+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();