From b724530b6e9db257129abc88143f8313997685ed Mon Sep 17 00:00:00 2001 From: Leonardo Robol Date: Sat, 17 Apr 2010 11:06:42 +0200 Subject: [PATCH] Rediretti stdout e stderr dove non rompono --- phcstats.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/phcstats.py b/phcstats.py index 97c8fba..a6c386f 100755 --- a/phcstats.py +++ b/phcstats.py @@ -5,7 +5,7 @@ # e restituisce delle statistiche a proposito. import subprocess, time -from optparse import OptionsParser +from optparse import OptionParser # Qualche variabile globale database_directory = '/home/robol/client_stats/' # Il traling slash ci serve @@ -27,7 +27,8 @@ def IsAlive(client): """ Ritorna True se il client รจ acceso e risponde ai ping """ - alive = subprocess.call(["fping", client]) + alive = subprocess.call(["fping", client], stdout = subprocess.PIPE, + stderr = subprocess.PIPE) return (alive == 0) def LoadClientData(client): @@ -42,8 +43,10 @@ def LoadClientData(client): except IOError: return data for line in f: - (time, alive) = line.split(":") - data[time] = alive + if ":" in line: + (time, alive) = line.split(":") + alive = ("True" in alive) + data[time] = bool(alive) f.close() return data @@ -55,8 +58,7 @@ def DumpClientData(client, data): data = dict (filter(lambda i: (i[0] > time.time() - max_time), data.items())) f = open(database_directory + client, 'w') - for stat in data.items(): - f.write (":".join(map(str, stat))) + f.write ("\n".join( map(lambda i: ":".join(map(str, i)), data.items()))) f.close() def UpdateClientsData(client, alive): @@ -73,9 +75,12 @@ def PrintStats(client): Stampa le statistiche sul client prescelto """ data = LoadClientData(client) - uptime = len(filter(lambda s: (s==True), data))/ float(len(filter)) - print "Uptime: %3.3f (%s)" % (100 * uptime, client) - + up = len(filter(lambda i: i[1] == True, data.items())) + total = len(data) + uptime = 100.0 * up / total + print "Client: %s" % client + print "Uptime: %3.2f %% " % uptime + print "----------------------------------------------------" @@ -84,7 +89,7 @@ if __name__ == "__main__": parser = OptionParser() parser.add_option("-c", "--check", dest="check", help = "Check if clients are up and update database", - action = "store_true", default=True) + action = "store_true", default=False) parser.add_option("-s", "--stats", dest="stats", help = "Print stats about collected informations", action = "store_true", default = False) -- 2.1.4