f1a8fb6ac1a18786a8dfe8bc2313258fc0e4ff19
[svn/Prometheus-QoS/.git] / optional-tools / hosts-ping.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 import ping, socket
5 import os, time
6
7 # (G)2013 xChaos, Arachne Labs http://arachne.cz + SPOJE.NET http://spoje.net
8
9 hosts = "/etc/hosts"
10 timeout = 1500 #timeout in ms
11 interval = 200 #ping interval in ms
12 attempts = 10
13
14 tld = ".czf"
15 domain = ".brevnov.czf"
16 smokeping_prefix = "Klienti"
17 smpater_prefix = "Backbone"
18 smokeping_babble_length = 3
19 smpater_babble_length = 2
20 smokeping_html = "/var/www/html/web/sites/sysifos/hosts-ping/index.html"
21 smpater_html = "/var/www/html/web/sites/sysifos/hosts-ping/backbone.html"
22 smokeping_url = "http://sisyfos.brevnov.czf/cgi-bin/smokeping.cgi?filter=%s&target=%s"
23 smpater_url = "http://tartarus.brevnov.czf/cgi-bin/smokeping.cgi?filter=%s&target=%s"
24 table_head = """
25 <table class="decorated last">
26 <caption>hosts ping (%s)</caption><thead><tr>
27 <th style="text-align: right;">#</th>
28 <th>hostname</th>
29 <th style="text-align: right;">received</th>
30 <th style="text-align: right;">avg</th>
31 <th style="text-align: right;">best</th>
32 <th style="text-align: right;">worst</th>
33 </tr></thead><tbody>
34 """
35 table_end = """
36 </tbody></table>
37 <br />
38 <p>Page generated by (G)2013 xChaos hosts-ping version 0.1-a</p>
39 """
40
41 def try_to_ping(host):
42 sum = 0.0
43 best = None
44 worst = None
45 loss = 0
46
47 for i in range(0, attempts):
48 try:
49 delay = ping.Ping(host, timeout = timeout).do() #timeout in ms
50 time.sleep(interval/1000)
51
52 if delay:
53 sum += delay
54
55 if not best or best > delay:
56 best = delay
57
58 if not worst or worst < delay:
59 worst = delay
60
61 else:
62 loss += 1
63
64 except socket.error, e:
65 loss += 1
66
67 return (sum/attempts, best, worst, loss)
68
69
70 def smokenam_style(hostname, prefix, babble_length):
71
72 if not tld in hostname:
73 hostname += domain
74
75 babble = hostname.split('.')
76 return '.'.join([prefix,] + [a_tooth for a_tooth in reversed(babble)][1:babble_length] + ['-'.join(babble),])
77
78
79 def append_host(html, host, base_url, counter):
80 style = {'right': 'text-align: right;'}
81 columns = ('loss','avg','best','worst')
82 red_treshold = (0, 100, 50, 200)
83 green_treshold = (0, 2, 1, 10)
84
85 for kolikaty, column in enumerate(columns):
86 style[column] = style['right']
87
88 if not host[column]:
89 host[column] = 0 #don't want it to be "None" type
90
91 if host[column] > red_treshold[kolikaty]:
92 style[column] += ' color: red;'
93 elif host[column] < green_treshold[kolikaty]:
94 style[column] += ' color: green;'
95
96 received = attempts-host['loss']
97 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")
98 % (('even', 'odd')[counter % 2], style['right'], counter, base_url % (host['name'], host['smokename']), host['name'], style['loss'], received, attempts))
99
100 if host['avg'] and host['best'] and host['worst']:
101 html.write( ('<td style="%s">%1.2f</td><td style="%s">%1.2f</td><td style="%s">%1.2f</td></tr>' + "\n")
102 % (style['avg'], host['avg'], style['best'], host['best'], style['worst'], host['worst']))
103 else:
104 html.write(3*('<td style="%s">-</td>' % style['loss']) + "\n")
105
106 # main program
107
108 smokeping = []
109 smpater = []
110
111 for radek in open(hosts):
112 if radek[0] != '#':
113 is_smokeping = 'smokeping' in radek and not 'hidden' in radek
114 is_smpater = 'smpater' in radek
115 if is_smokeping or is_smpater:
116 slovo = radek.split("\t")
117 host = { 'ip': slovo[0], 'name': slovo[1].split(' ')[0] }
118 (host['avg'], host['best'], host['worst'], host['loss']) = try_to_ping(host['ip'])
119
120 if is_smokeping:
121 host['smokename'] = smokenam_style(host['name'], smokeping_prefix, smokeping_babble_length)
122 smokeping.append(host)
123 else:
124 host['smokename'] = smokenam_style(host['name'], smpater_prefix, smpater_babble_length)
125 smpater.append(host)
126
127 # smokeping
128
129 html = open(smokeping_html, 'w')
130 html.write("<h1>Smokeping - klientská zařízení</h1>");
131 html.write(table_head % time.ctime());
132
133 for kolikaty, host in enumerate(sorted(smokeping, key = lambda host: -host['loss']*attempts*timeout-host['avg'])):
134 append_host(html, host, smokeping_url, kolikaty+1)
135
136 html.write(table_end);
137 html.close();
138
139 # smpater
140
141 html = open(smpater_html, 'w')
142 html.write("<h1>Smokeping - páteřní routery</h1>");
143 html.write(table_head % time.ctime());
144
145 for kolikaty, host in enumerate(sorted(smpater, key = lambda host: -host['loss']*attempts*timeout-host['avg'])):
146 append_host(html, host, smpater_url, kolikaty+1)
147
148 html.write(table_end);
149 html.close();
This page took 0.349512 seconds and 4 git commands to generate.