hosts-ping: moving averages added
authorxchaos <xchaos@251d49ef-1d17-4917-a970-b30cf55b089b>
Wed, 8 May 2013 21:17:00 +0000 (21:17 +0000)
committerxchaos <xchaos@251d49ef-1d17-4917-a970-b30cf55b089b>
Wed, 8 May 2013 21:17:00 +0000 (21:17 +0000)
git-svn-id: https://dev.arachne.cz/repos/prometheus/trunk@224 251d49ef-1d17-4917-a970-b30cf55b089b

optional-tools/hosts-ping.py

index f1a8fb6..740a74b 100755 (executable)
@@ -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&amp;target=%s"
 smpater_url = "http://tartarus.brevnov.czf/cgi-bin/smokeping.cgi?filter=%s&amp;target=%s"
 table_head = """
@@ -26,7 +30,7 @@ table_head = """
 <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;">loss</th>
 <th style="text-align: right;">avg</th>
 <th style="text-align: right;">best</th>
 <th style="text-align: right;">worst</th>
@@ -35,7 +39,7 @@ table_head = """
 table_end = """
 </tbody></table>
 <br />
-<p>Page generated by (G)2013 xChaos hosts-ping version 0.1-a</p>
+<p>Page generated by (G)2013 xChaos hosts-ping version 0.2-a</p>
 """
 
 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(   ('<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))
+  loss = 100*host['loss']/host['attempts']
+  html.write(   ('<tr class="%s"><td style="%s">%d</td><td><a href="%s" target="_blank" class="blue">%s</a></td><td style="%s">%.1f%%</td>' + "\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(   ('<td style="%s">%1.2f</td><td style="%s">%1.2f</td><td style="%s">%1.2f</td></tr>' + "\n")
+    html.write(   ('<td style="%s">%.1f</td><td style="%s">%.1f</td><td style="%s">%.1f</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")
 
+
+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("<h1>Smokeping - klientská zařízení</h1>");
+html.write("<h1>Aktuální odezva klientských 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)
+  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("<h1>Smokeping - páteřní routery</h1>");
+html.write("<h1>Aktuální odezva páteřních routerů</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, 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("<h1>Průměrná odezva klientských 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, 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("<h1>Průměrná odezva páteřních routerů</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)
+  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
This page took 0.201894 seconds and 4 git commands to generate.