Rediretti stdout e stderr dove non rompono

Leonardo Robol [2010-04-17 09:06]
Rediretti stdout e stderr dove non rompono
Filename
phcstats.py
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)
ViewGit