file operations for reading and writing lists of nodes/sites/pcus, etc.
[monitor.git] / policy.py
index 20c12b4..027da35 100644 (file)
--- a/policy.py
+++ b/policy.py
@@ -19,12 +19,10 @@ import plc
 import sys
 import os
 import reboot
-import soltesz
+import database
 import string
 from www.printbadnodes import cmpCategoryVal
-from config import config
-#print "policy"
-config = config()
+import config
 
 DAT="./monitor.dat"
 
@@ -102,13 +100,13 @@ class Merge(Thread):
                self.toRT = toRT
                self.merge_list = l_merge
                # the hostname to loginbase mapping
-               self.plcdb_hn2lb = soltesz.dbLoad("plcdb_hn2lb")
+               self.plcdb_hn2lb = database.dbLoad("plcdb_hn2lb")
 
                # Previous actions taken on nodes.
-               self.act_all = soltesz.if_cached_else(1, "act_all", lambda : {})
-               self.findbad = soltesz.if_cached_else(1, "findbad", lambda : {})
+               self.act_all = database.if_cached_else(1, "act_all", lambda : {})
+               self.findbad = database.if_cached_else(1, "findbad", lambda : {})
 
-               self.cache_all = soltesz.if_cached_else(1, "act_all", lambda : {})
+               self.cache_all = database.if_cached_else(1, "act_all", lambda : {})
                self.sickdb = {}
                self.mergedb = {}
                Thread.__init__(self)
@@ -286,8 +284,8 @@ class Merge(Thread):
 class Diagnose(Thread):
        def __init__(self, fromRT):
                self.fromRT = fromRT
-               self.plcdb_hn2lb = soltesz.dbLoad("plcdb_hn2lb")
-               self.findbad = soltesz.if_cached_else(1, "findbad", lambda : {})
+               self.plcdb_hn2lb = database.dbLoad("plcdb_hn2lb")
+               self.findbad = database.if_cached_else(1, "findbad", lambda : {})
 
                self.diagnose_in = {}
                self.diagnose_out = {}
@@ -316,7 +314,7 @@ class Diagnose(Thread):
 
                if config.policysavedb:
                        print "Saving Databases... diagnose_out"
-                       soltesz.dbDump("diagnose_out", self.diagnose_out)
+                       database.dbDump("diagnose_out", self.diagnose_out)
 
        def accumSickSites(self):
                """
@@ -373,34 +371,59 @@ class Diagnose(Thread):
                
        def getDaysDown(cls, diag_record):
                daysdown = -1
-               if diag_record['comonstats']['uptime'] != "null":
-                       #print "uptime %s" % (int(float(diag_record['comonstats']['uptime'])) // (60*60*24))
+               last_contact = diag_record['plcnode']['last_contact']
+               date_created = diag_record['plcnode']['date_created']
+
+               if diag_record['comonstats']['uptime'] != "null" and diag_record['comonstats']['uptime'] != "-1":
                        daysdown = - int(float(diag_record['comonstats']['uptime'])) // (60*60*24)
-               elif diag_record['comonstats']['sshstatus'] != "null":
-                       daysdown = int(diag_record['comonstats']['sshstatus']) // (60*60*24)
-               elif diag_record['comonstats']['lastcotop'] != "null":
-                       daysdown = int(diag_record['comonstats']['lastcotop']) // (60*60*24)
+               elif last_contact is None:
+                       if date_created is not None:
+                               now = time.time()
+                               diff = now - date_created
+                               daysdown = diff // (60*60*24)
+                       else:
+                               daysdown = -1
                else:
                        now = time.time()
-                       last_contact = diag_record['plcnode']['last_contact']
-                       if last_contact == None:
-                               # the node has never been up, so give it a break
-                               daysdown = -1
-                       else:
-                               diff = now - last_contact
-                               daysdown = diff // (60*60*24)
+                       diff = now - last_contact
+                       daysdown = diff // (60*60*24)
                return daysdown
        getDaysDown = classmethod(getDaysDown)
 
        def getStrDaysDown(cls, diag_record):
-               daysdown = cls.getDaysDown(diag_record)
-               if daysdown > 0:
-                       return "%d days down"%daysdown
-               elif daysdown == -1:
-                       return "Unknown number of days"
+               daysdown = "unknown"
+               last_contact = diag_record['plcnode']['last_contact']
+               date_created = diag_record['plcnode']['date_created']
+
+               if      diag_record['comonstats']['uptime'] != "null" and \
+                       diag_record['comonstats']['uptime'] != "-1":
+                       daysdown = int(float(diag_record['comonstats']['uptime'])) // (60*60*24)
+                       daysdown = "%d days up" % daysdown
+
+               elif last_contact is None:
+                       if date_created is not None:
+                               now = time.time()
+                               diff = now - date_created
+                               daysdown = diff // (60*60*24)
+                               daysdown = "Never contacted PLC, created %s days ago" % daysdown
+                       else:
+                               daysdown = "Never contacted PLC"
                else:
-                       return "%d days up"% -daysdown
+                       now = time.time()
+                       diff = now - last_contact
+                       daysdown = diff // (60*60*24)
+                       daysdown = "%s days down" % daysdown
+               return daysdown
        getStrDaysDown = classmethod(getStrDaysDown)
+       #def getStrDaysDown(cls, diag_record):
+       #       daysdown = cls.getDaysDown(diag_record)
+       #       if daysdown > -1:
+       #               return "%d days down"%daysdown
+       #       elif daysdown == -1:
+       #               return "Has never contacted PLC"
+       #       else:
+       #               return "%d days up"% -daysdown
+       #getStrDaysDown = classmethod(getStrDaysDown)
 
        def __getCDVersion(self, diag_record, nodename):
                cdversion = ""
@@ -925,12 +948,12 @@ class Action(Thread):
                self.l_action = l_action
 
                # the hostname to loginbase mapping
-               self.plcdb_hn2lb = soltesz.dbLoad("plcdb_hn2lb")
+               self.plcdb_hn2lb = database.dbLoad("plcdb_hn2lb")
 
                # Actions to take.
-               self.diagnose_db = soltesz.if_cached_else(1, "diagnose_out", lambda : {})
+               self.diagnose_db = database.if_cached_else(1, "diagnose_out", lambda : {})
                # Actions taken.
-               self.act_all   = soltesz.if_cached_else(1, "act_all", lambda : {})
+               self.act_all   = database.if_cached_else(1, "act_all", lambda : {})
 
                # A dict of actions to specific functions. PICKLE doesnt' like lambdas.
                self.actions = {}
@@ -970,7 +993,7 @@ class Action(Thread):
                        print err
                        if config.policysavedb:
                                print "Saving Databases... act_all"
-                               soltesz.dbDump("act_all", self.act_all)
+                               database.dbDump("act_all", self.act_all)
                        sys.exit(1)
 
                print_stats("sites_observed", stats)
@@ -982,10 +1005,10 @@ class Action(Thread):
 
                if config.policysavedb:
                        print "Saving Databases... act_all"
-                       #soltesz.dbDump("policy.eventlog", self.eventlog)
+                       #database.dbDump("policy.eventlog", self.eventlog)
                        # TODO: remove 'diagnose_out', 
                        #       or at least the entries that were acted on.
-                       soltesz.dbDump("act_all", self.act_all)
+                       database.dbDump("act_all", self.act_all)
 
        def accumSites(self):
                """
@@ -1205,10 +1228,10 @@ class Action(Thread):
                
                if config.policysavedb:
                        print "Saving Databases... act_all, diagnose_out"
-                       soltesz.dbDump("act_all", self.act_all)
+                       database.dbDump("act_all", self.act_all)
                        # remove site record from diagnose_out, it's in act_all as done.
                        del self.diagnose_db[loginbase]
-                       soltesz.dbDump("diagnose_out", self.diagnose_db)
+                       database.dbDump("diagnose_out", self.diagnose_db)
 
                print "sleeping for 1 sec"
                time.sleep(1)