replace some print statements to stderr
Stephen Soltesz [Thu, 21 Jan 2010 19:47:29 +0000 (19:47 +0000)]
add HistorySiteRecord to checksync

commands/checksync.py
commands/comonquery.py
commands/nodequery.py

index 2a17687..d92d60f 100755 (executable)
@@ -3,29 +3,48 @@
 from monitor.database.info.model import *
 import sys
 
-fbquery = HistoryNodeRecord.query.all()
-hostnames = [ n.hostname for n in fbquery ]
 
 if True:
-       for hn in hostnames:
-               fbrec = FindbadNodeRecord.query.filter_by(hostname=hn).order_by(FindbadNodeRecord.version.desc()).first()
-               if len(fbrec.versions) >= 2:
-                       if fbrec.version != fbrec.versions[-2].version + 1:
-                               print fbrec.hostname, fbrec.version, fbrec.versions[-2].version
-                               fbrec.version = fbrec.versions[-2].version + 1
-                               fbrec.flush()
-
-       session.flush()
-
-fbquery = HistoryPCURecord.query.all()
-pcus = [ n.plc_pcuid for n in fbquery ]
-
-for pcuid in pcus:
-       fbrec = FindbadPCURecord.query.filter_by(plc_pcuid=pcuid).order_by(FindbadPCURecord.version.desc()).first()
-       if len(fbrec.versions) >= 2:
-               if fbrec.version != fbrec.versions[-2].version + 1:
-                       print fbrec.plc_pcuid, fbrec.version, fbrec.versions[-2].version
-                       fbrec.version = fbrec.versions[-2].version + 1
-                       fbrec.flush()
+    fbquery = HistorySiteRecord.query.all()
+    loginbases = [ h.loginbase for h in fbquery ]
+
+    for lb in loginbases:
+        h = HistorySiteRecord.query.filter_by(loginbase=lb).first()
+        if len(h.versions) >= 2:
+            if h.version != h.versions[-2].version + 1:
+                print h.loginbase, h.version, h.versions[-2].version
+                h.version = h.versions[-2].version + 1
+                h.flush()
+
+    session.flush()
+
+
+
+if True:
+    fbquery = HistoryNodeRecord.query.all()
+    hostnames = [ n.hostname for n in fbquery ]
+
+    for hn in hostnames:
+        fbrec = FindbadNodeRecord.query.filter_by(hostname=hn).order_by(FindbadNodeRecord.version.desc()).first()
+        if len(fbrec.versions) >= 2:
+            if fbrec.version != fbrec.versions[-2].version + 1:
+                print fbrec.hostname, fbrec.version, fbrec.versions[-2].version
+                fbrec.version = fbrec.versions[-2].version + 1
+                fbrec.flush()
+
+    session.flush()
+
+
+if True:
+    fbquery = HistoryPCURecord.query.all()
+    pcus = [ n.plc_pcuid for n in fbquery ]
+
+    for pcuid in pcus:
+        fbrec = FindbadPCURecord.query.filter_by(plc_pcuid=pcuid).order_by(FindbadPCURecord.version.desc()).first()
+        if len(fbrec.versions) >= 2:
+            if fbrec.version != fbrec.versions[-2].version + 1:
+                print fbrec.plc_pcuid, fbrec.version, fbrec.versions[-2].version
+                fbrec.version = fbrec.versions[-2].version + 1
+                fbrec.flush()
 
 session.flush()
index db0bafe..b14f9b5 100755 (executable)
@@ -34,7 +34,6 @@ def daysdown_print_nodeinfo(co_nodeinfo, hostname):
 
 def co_print_nodeinfo(co_nodeinfo, hostname, fields=None):
        
-       # co_nodeinfo['bootstate'] : unknown pattern
        co_nodeinfo['name'] = hostname
 
        if 'uptime' in co_nodeinfo and co_nodeinfo['uptime'] != "null":
@@ -49,7 +48,7 @@ def co_print_nodeinfo(co_nodeinfo, hostname, fields=None):
        else:
                format = ""
                for f in fields:
-                       format += "%%(%s)s " % f
+                       format += "%%(%s)s," % f
                print format % co_nodeinfo
 
 def main():
@@ -63,6 +62,7 @@ def main():
                                dns=False,
                                listkeys=False,
                                pcuselect=None, 
+                               cache=False,
                                nodelist=None, 
                                daysdown=None, 
                                fields=default_fields)
@@ -79,6 +79,8 @@ def main():
                                                help="A list of nodes to bring out of debug mode.")
        parser.add_option("", "--listkeys", dest="listkeys", action="store_true",
                                                help="A list of nodes to bring out of debug mode.")
+       parser.add_option("", "--cache", dest="cache", action="store_true",
+                                               help="Specify whether the command should perform a live query or save/use a cached values.")
 
        parser.add_option("", "--dns", dest="dns", action="store_true",
                                                help="A convenience query for dns values")
@@ -86,11 +88,6 @@ def main():
        parser = parsermodule.getParser(['defaults'], parser)
        config = parsermodule.parse_args(parser)
        
-       #if config.fromtime:
-       #       fb = None
-       #else:
-       #       fb = None
-
        # lastcotop measures whether cotop is actually running.  this is a better
        # metric than sshstatus, or other values from CoMon
 
@@ -112,7 +109,14 @@ def main():
                cotop_url = COMON_COTOPURL + "&limit=1"
 
        cotop = comon.Comon()
-       cohash = cotop.coget(cotop_url)
+       if database.cachedRecently(cotop_url):
+               print >>sys.stderr, "loading cached query :%s" % cotop_url
+               cohash = database.dbLoad(cotop_url)
+       else:
+               cohash = cotop.coget(cotop_url)
+               if config.cache:
+                       print >>sys.stderr, "saving query :%s" % cotop_url
+                       database.dbDump(cotop_url, cohash)
 
        if config.nodelist:
                nodelist = file.getListFromFile(config.nodelist)
@@ -120,12 +124,11 @@ def main():
                # NOTE: list of nodes should come from comon query.   
                nodelist = cohash.keys()
 
-       print "%(name)-40s %(sshstatus)5.5s %(resptime)6.6s %(lastcotop)6.6s %(uptime)s" % {
-                                       'name' : 'hostname', 
-                                       'sshstatus' : 'sshstatus', 
-                                       'resptime' : 'resptime', 
-                                       'lastcotop' : 'lastcotop', 
-                                       'uptime' : 'uptime'}
+       fields = config.fields.split(",")
+       f_info = {}
+       for f in fields: f_info[f]=f 
+       co_print_nodeinfo(f_info, "hostname", fields)
+
        for node in nodelist:
                config.node = node
 
@@ -134,7 +137,7 @@ def main():
                co_nodeinfo = cohash[node]
 
                if config.listkeys:
-                       print "Primary keys available in the comon object:"
+                       print >>sys.stderr, "Primary keys available in the comon object:"
                        for key in co_nodeinfo.keys():
                                print "\t",key
                        sys.exit(0)
index 61513f4..455728b 100755 (executable)
@@ -133,7 +133,7 @@ def main():
        #       for pcu in pculist:
        #               print pcu
 
-       print "len: %s" % len(nodelist)
+       print >>sys.stderr, "len: %s" % len(nodelist)
        for node in nodelist:
                config.node = node