#!/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 = """
# | hostname | received | avg | best | worst |
---|
Page generated by (G)2013 xChaos hosts-ping version 0.1-a
""" 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( ('