f1a8fb6ac1a18786a8dfe8bc2313258fc0e4ff19
2 # -*- coding: utf-8 -*-
7 # (G)2013 xChaos, Arachne Labs http://arachne.cz + SPOJE.NET http://spoje.net
10 timeout
= 1500 #timeout in ms
11 interval
= 200 #ping interval in ms
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"
25 <table class="decorated last">
26 <caption>hosts ping (%s)</caption><thead><tr>
27 <th style="text-align: right;">#</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>
38 <p>Page generated by (G)2013 xChaos hosts-ping version 0.1-a</p>
41 def try_to_ping(host
):
47 for i
in range(0, attempts
):
49 delay
= ping
.Ping(host
, timeout
= timeout
).do() #timeout in ms
50 time
.sleep(interval
/1000)
55 if not best
or best
> delay
:
58 if not worst
or worst
< delay
:
64 except socket
.error
, e
:
67 return (sum/attempts
, best
, worst
, loss
)
70 def smokenam_style(hostname
, prefix
, babble_length
):
72 if not tld
in hostname
:
75 babble
= hostname
.split('.')
76 return '.'.join([prefix
,] + [a_tooth
for a_tooth
in reversed(babble
)][1:babble_length
] + ['-'.join(babble
),])
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)
85 for kolikaty
, column
in enumerate(columns
):
86 style
[column
] = style
['right']
89 host
[column
] = 0 #don't want it to be "None" type
91 if host
[column
] > red_treshold
[kolikaty
]:
92 style
[column
] += ' color: red;'
93 elif host
[column
] < green_treshold
[kolikaty
]:
94 style
[column
] += ' color: green;'
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
))
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']))
104 html
.write(3*('<td style="%s">-</td>' % style
['loss']) + "\n")
111 for radek
in open(hosts
):
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'])
121 host
['smokename'] = smokenam_style(host
['name'], smokeping_prefix
, smokeping_babble_length
)
122 smokeping
.append(host
)
124 host
['smokename'] = smokenam_style(host
['name'], smpater_prefix
, smpater_babble_length
)
129 html
= open(smokeping_html
, 'w')
130 html
.write("<h1>Smokeping - klientská zařízení</h1>");
131 html
.write(table_head
% time
.ctime());
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)
136 html
.write(table_end
);
141 html
= open(smpater_html
, 'w')
142 html
.write("<h1>Smokeping - páteřní routery</h1>");
143 html
.write(table_head
% time
.ctime());
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)
148 html
.write(table_end
);
This page took 0.362722 seconds and 4 git commands to generate.