X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=nodehistory.py;h=e554e0a8fd88bbeaf4debeddd3491947135b219c;hb=6f2351e4b44590221425fa9b4bfa77c92db49b6a;hp=f40ecc7d50fc554ae2bdb0e16430a2c6582c768f;hpb=8b0bb120a071aa27951eb0bf9435aadd9e1df9ed;p=monitor.git diff --git a/nodehistory.py b/nodehistory.py index f40ecc7..e554e0a 100755 --- a/nodehistory.py +++ b/nodehistory.py @@ -1,68 +1,36 @@ #!/usr/bin/python import plc -import auth -api = plc.PLC(auth.auth, auth.plc) +api = plc.getAuthAPI() -import soltesz +import database import reboot import time from datetime import datetime, timedelta import calendar +import sys import time from model import * from nodecommon import * -from config import config -from optparse import OptionParser - -parser = OptionParser() -parser.set_defaults(node=None, fields='state', fromtime=None) -parser.add_option("", "--node", dest="node", metavar="nodename.edu", - help="A single node name to add to the nodegroup") -parser.add_option("", "--fields", dest="fields", metavar="key", - help="Which record field to extract from all files.") -parser.add_option("", "--fromtime", dest="fromtime", metavar="YYYY-MM-DD", - help="Specify a starting date from which to begin the query.") -config = config(parser) -config.parse_args() - -def datetime_fromstr(str): - if '-' in str: - tup = time.strptime(str, "%Y-%m-%d") - elif '/' in str: - tup = time.strptime(str, "%m/%d/%Y") - else: - tup = time.strptime(str, "%m/%d/%Y") - return datetime.fromtimestamp(calendar.timegm(tup)) - -def diff_time(timestamp): - now = time.time() - if timestamp == None: - return "unknown" - diff = now - timestamp - # return the number of seconds as a difference from current time. - t_str = "" - if diff < 60: # sec in min. - t = diff - t_str = "%s sec ago" % t - elif diff < 60*60: # sec in hour - t = diff // (60) - t_str = "%s min ago" % int(t) - elif diff < 60*60*24: # sec in day - t = diff // (60*60) - t_str = "%s hours ago" % int(t) - elif diff < 60*60*24*7: # sec in week - t = diff // (60*60*24) - t_str = "%s days ago" % int(t) - elif diff < 60*60*24*30: # approx sec in month - t = diff // (60*60*24*7) - t_str = "%s weeks ago" % int(t) - elif diff > 60*60*24*30: # approx sec in month - t = diff // (60*60*24*7*30) - t_str = "%s months ago" % int(t) - return t_str +def get_filefromglob(d, str): + import os + import glob + # TODO: This is aweful. + path = "archive-pdb" + archive = database.SPickle(path) + glob_str = "%s*.%s.pkl" % (d.strftime("%Y-%m-%d"), str) + os.chdir(path) + #print glob_str + #file = glob.glob(glob_str)[0] + files = glob.glob(glob_str) + #print "loading %s" % file + os.chdir("..") + files_chng = [ file[:-4] for file in files ] + return files_chng + #fb = archive.load(file[:-4]) + def fb_print_nodeinfo(fbnode, verbose, date=None): if verbose: print " state | ssh | pcu | bootcd | category | kernel" @@ -77,8 +45,10 @@ def fb_print_nodeinfo(fbnode, verbose, date=None): else: fbnode['bootcd'] = "unknown" fbnode['state'] = color_boot_state(get_current_state(fbnode)) - fbnode['kernel'] = fbnode['kernel'].split()[2] - print " %(state)5s | %(ssh)5.5s | %(pcu)5.5s | %(bootcd)6.6s | %(category)8.8s | %(kernel)s" % fbnode + fbnode['boot_state'] = getvalue(fbnode, 'plcnode/boot_state') + if len(fbnode['kernel'].split()) >= 3: + fbnode['kernel'] = fbnode['kernel'].split()[2] + print " %(state)5s | %(boot_state)s | %(ssh)5.5s | %(pcu)5.5s | %(bootcd)6.6s | %(category)8.8s | %(kernel)s" % fbnode def pcu_print_info(pcuinfo, hostname): print " Checked: ", @@ -105,32 +75,60 @@ def pcu_print_info(pcuinfo, hostname): print "\t cmdhttps/locfg.pl -s %s -f iloxml/Reset_Server.xml -u %s -p '%s' | grep MESSAGE" % \ (reboot.pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password']) -path = "archive-pdb" -archive = soltesz.SPickle(path) +def main(): + import parser as parsermodule -if config.fromtime: - begin = config.fromtime -else: - begin = "2007-11-06" + parser = parsermodule.getParser() + parser.set_defaults(node=None, fields='state', fromtime=None) + parser.add_option("", "--node", dest="node", metavar="nodename.edu", + help="A single node name to add to the nodegroup") + parser.add_option("", "--fields", dest="fields", metavar="key", + help="Which record field to extract from all files.") + parser.add_option("", "--fromtime", dest="fromtime", metavar="YYYY-MM-DD", + help="Specify a starting date from which to begin the query.") + config = parsermodule.parse_args(parser) -d = datetime_fromstr(begin) -tdelta = timedelta(1) -verbose = 1 + path = "archive-pdb" + archive = database.SPickle(path) -while True: - file = "%s.production.findbad" % d.strftime("%Y-%m-%d") - - try: - fb = archive.load(file) - if config.node in fb['nodes']: - fb_nodeinfo = fb['nodes'][config.node]['values'] - fb_print_nodeinfo(fb_nodeinfo, verbose, d.strftime("%Y-%m-%d")) + if config.fromtime: + begin = config.fromtime + else: + begin = "2007-11-06" - del fb - verbose = 0 - except: - print d.strftime("%Y-%m-%d"), "No record" + if config.node is None and len(config.args) > 0: + config.node = config.args[0] + elif config.node is None: + print "Add a hostname to arguments" + print "exit." + sys.exit(1) - d = d + tdelta - if d > datetime.now(): break + d = datetime_fromstr(begin) + tdelta = timedelta(1) + verbose = 1 + while True: + + try: + for file in get_filefromglob(d, "production.findbad"): + #file = get_filefromglob(d, "production.findbad") + #file = "%s.production.findbad" % d.strftime("%Y-%m-%d") + fb = archive.load(file) + if config.node in fb['nodes']: + fb_nodeinfo = fb['nodes'][config.node]['values'] + fb_print_nodeinfo(fb_nodeinfo, verbose, d.strftime("%Y-%m-%d")) + + del fb + verbose = 0 + + except KeyboardInterrupt: + sys.exit(1) + except: + #import traceback; print traceback.print_exc() + print d.strftime("%Y-%m-%d"), "No record" + + d = d + tdelta + if d > datetime.now(): break + +if __name__ == "__main__": + main()