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_avg_html
= "/var/www/html/web/sites/sysifos/hosts-ping/avg.html"
23 smpater_avg_html
= "/var/www/html/web/sites/sysifos/hosts-ping/backbone-avg.html"
24 smokeping_avg_json
= "/var/www/html/web/sites/sysifos/hosts-ping/smokeping.json"
25 smpater_avg_json
= "/var/www/html/web/sites/sysifos/hosts-ping/smpater.json"
26 smokeping_url
= "http://sisyfos.brevnov.czf/cgi-bin/smokeping.cgi?filter=%s&target=%s"
27 smpater_url
= "http://tartarus.brevnov.czf/cgi-bin/smokeping.cgi?filter=%s&target=%s"
29 <table class="decorated last">
30 <caption>hosts ping (%s)</caption><thead><tr>
31 <th style="text-align: right;">#</th>
33 <th style="text-align: right;">loss</th>
34 <th style="text-align: right;">avg</th>
35 <th style="text-align: right;">best</th>
36 <th style="text-align: right;">worst</th>
42 <p>Page generated by (G)2013 xChaos hosts-ping version 0.2-a</p>
45 def try_to_ping(host
):
51 for i
in range(0, attempts
):
53 delay
= ping
.Ping(host
, timeout
= timeout
).do() #timeout in ms
54 time
.sleep(interval
/1000)
59 if not best
or best
> delay
:
62 if not worst
or worst
< delay
:
68 except socket
.error
, e
:
71 return (sum/attempts
, best
, worst
, loss
)
74 def smokenam_style(hostname
, prefix
, babble_length
):
76 if not tld
in hostname
:
79 babble
= hostname
.split('.')
80 return '.'.join([prefix
,] + [a_tooth
for a_tooth
in reversed(babble
)][1:babble_length
] + ['-'.join(babble
),])
83 def append_host(html
, host
, base_url
, counter
, red_treshold
, green_treshold
):
84 style
= {'right': 'text-align: right;'}
85 columns
= ('loss','avg','best','worst')
87 for kolikaty
, column
in enumerate(columns
):
88 style
[column
] = style
['right']
91 host
[column
] = 0 #don't want it to be "None" type
93 if host
[column
] > red_treshold
[kolikaty
]:
94 style
[column
] += ' color: red;'
95 elif host
[column
] < green_treshold
[kolikaty
]:
96 style
[column
] += ' color: green;'
98 loss
= float(100*host
['loss'])/host
['attempts']
99 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")
100 % (('even', 'odd')[counter
% 2], style
['right'], counter
, base_url
% (host
['name'], host
['smokename']), host
['name'], style
['loss'], loss
))
102 if host
['avg'] and host
['best'] and host
['worst']:
103 html
.write( ('<td style="%s">%.1f</td><td style="%s">%.1f</td><td style="%s">%.1f</td></tr>' + "\n")
104 % (style
['avg'], host
['avg'], style
['best'], host
['best'], style
['worst'], host
['worst']))
106 html
.write(3*('<td style="%s">-</td>' % style
['loss']) + "\n")
109 def merge_json_avgs(filename
, smoke_array
):
112 avg_dict
= json
.load(open(filename
))
114 for host
in smoke_array
:
115 avg
= avg_dict
.get(host
['ip'])
117 if host
['avg'] and avg
['avg'] and avg
['attempts']+host
['attempts']-avg
['loss']-host
['loss'] > 0:
118 host
['avg'] = ((avg
['attempts']-avg
['loss'])*avg
['avg']+(host
['attempts']-host
['loss'])*host
['avg'])/(avg
['attempts']+host
['attempts']-avg
['loss']-host
['loss'])
120 host
['avg'] = avg
['avg']
122 if not host
['best'] or host
['best'] > avg
['best']:
123 host
['best'] = avg
['best']
125 if not host
['worst'] or host
['worst'] < avg
['worst']:
126 host
['worst'] = avg
['worst']
128 host
['attempts'] += avg
['attempts']
129 host
['loss'] += avg
['loss']
132 pass #start from scratch...
135 def save_json(filename
, smoke_array
):
138 for host
in smoke_array
:
139 smoke_dict
[host
['ip']] = host
141 json
.dump(smoke_dict
, open(filename
, 'w'))
149 for radek
in open(hosts
):
151 is_smokeping
= 'smokeping' in radek
and not 'hidden' in radek
152 is_smpater
= 'smpater' in radek
153 if is_smokeping
or is_smpater
:
154 slovo
= radek
.split("\t")
155 host
= { 'ip': slovo
[0], 'name': slovo
[1].split(' ')[0], 'attempts': attempts
}
156 (host
['avg'], host
['best'], host
['worst'], host
['loss']) = try_to_ping(host
['ip'])
159 host
['smokename'] = smokenam_style(host
['name'], smokeping_prefix
, smokeping_babble_length
)
160 smokeping
.append(host
)
162 host
['smokename'] = smokenam_style(host
['name'], smpater_prefix
, smpater_babble_length
)
167 red_treshold
= (0, 100, 50, 200)
168 green_treshold
= (0, 7, 5, 20)
169 html
= open(smokeping_html
, 'w')
170 html
.write("<h1>Aktuální odezva klientských zařízení</h1>");
171 html
.write(table_head
% time
.ctime());
173 for kolikaty
, host
in enumerate(sorted(smokeping
, key
= lambda host
: -host
['loss']*host
['attempts']*timeout
-host
['avg'])):
174 append_host(html
, host
, smokeping_url
, kolikaty
+1, red_treshold
, green_treshold
)
176 html
.write(table_end
)
181 red_treshold
= (0, 50, 20, 100)
182 green_treshold
= (0, 5, 2, 10)
183 html
= open(smpater_html
, 'w')
184 html
.write("<h1>Aktuální odezva páteřních routerů</h1>");
185 html
.write(table_head
% time
.ctime());
187 for kolikaty
, host
in enumerate(sorted(smpater
, key
= lambda host
: -host
['loss']*host
['attempts']*timeout
-host
['avg'])):
188 append_host(html
, host
, smpater_url
, kolikaty
+1, red_treshold
, green_treshold
)
190 html
.write(table_end
)
195 red_treshold
= (1000, 100, 20, 500)
196 green_treshold
= (0, 7, 5, 20)
197 merge_json_avgs(smokeping_avg_json
, smokeping
)
198 html
= open(smokeping_avg_html
, 'w')
199 html
.write("<h1>Průměrná odezva klientských zařízení</h1>");
200 html
.write(table_head
% time
.ctime());
202 for kolikaty
, host
in enumerate(sorted(smokeping
, key
= lambda host
: -host
['loss']*host
['attempts']*timeout
-host
['avg'])):
203 append_host(html
, host
, smokeping_url
, kolikaty
+1, red_treshold
, green_treshold
)
205 html
.write(table_end
)
207 save_json(smokeping_avg_json
, smokeping
)
211 red_treshold
= (100, 50, 10, 200)
212 green_treshold
= (0, 5, 2, 10)
213 merge_json_avgs(smpater_avg_json
, smpater
)
214 html
= open(smpater_avg_html
, 'w')
215 html
.write("<h1>Průměrná odezva páteřních routerů</h1>");
216 html
.write(table_head
% time
.ctime());
218 for kolikaty
, host
in enumerate(sorted(smpater
, key
= lambda host
: -host
['loss']*host
['attempts']*timeout
-host
['avg'])):
219 append_host(html
, host
, smpater_url
, kolikaty
+1, red_treshold
, green_treshold
)
221 html
.write(table_end
)
223 save_json(smpater_avg_json
, smpater
)
This page took 0.516255 seconds and 4 git commands to generate.