5 api = plc.PLC(auth.auth, auth.plc)
10 from datetime import datetime, timedelta
15 from nodecommon import *
17 from config import config
18 from optparse import OptionParser
20 parser = OptionParser()
21 parser.set_defaults(node=None, fields='state', fromtime=None)
22 parser.add_option("", "--node", dest="node", metavar="nodename.edu",
23 help="A single node name to add to the nodegroup")
24 parser.add_option("", "--fields", dest="fields", metavar="key",
25 help="Which record field to extract from all files.")
26 parser.add_option("", "--fromtime", dest="fromtime", metavar="YYYY-MM-DD",
27 help="Specify a starting date from which to begin the query.")
28 config = config(parser)
31 def datetime_fromstr(str):
33 tup = time.strptime(str, "%Y-%m-%d")
35 tup = time.strptime(str, "%m/%d/%Y")
37 tup = time.strptime(str, "%m/%d/%Y")
38 return datetime.fromtimestamp(calendar.timegm(tup))
40 def diff_time(timestamp):
44 diff = now - timestamp
45 # return the number of seconds as a difference from current time.
47 if diff < 60: # sec in min.
49 t_str = "%s sec ago" % t
50 elif diff < 60*60: # sec in hour
52 t_str = "%s min ago" % int(t)
53 elif diff < 60*60*24: # sec in day
55 t_str = "%s hours ago" % int(t)
56 elif diff < 60*60*24*7: # sec in week
57 t = diff // (60*60*24)
58 t_str = "%s days ago" % int(t)
59 elif diff < 60*60*24*30: # approx sec in month
60 t = diff // (60*60*24*7)
61 t_str = "%s weeks ago" % int(t)
62 elif diff > 60*60*24*30: # approx sec in month
63 t = diff // (60*60*24*7*30)
64 t_str = "%s months ago" % int(t)
67 def fb_print_nodeinfo(fbnode, verbose, date=None):
68 if verbose: print " state | ssh | pcu | bootcd | category | kernel"
69 if 'checked' in fbnode:
70 print "%11.11s " % diff_time(fbnode['checked']),
73 else: print "Unknown",
76 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
78 fbnode['bootcd'] = "unknown"
79 fbnode['state'] = color_boot_state(get_current_state(fbnode))
80 fbnode['kernel'] = fbnode['kernel'].split()[2]
81 print " %(state)5s | %(ssh)5.5s | %(pcu)5.5s | %(bootcd)6.6s | %(category)8.8s | %(kernel)s" % fbnode
83 def pcu_print_info(pcuinfo, hostname):
85 if 'checked' in pcuinfo:
86 print "%11.11s " % diff_time(pcuinfo['checked'])
90 print "\t user | password | port | hostname "
91 print "\t %17s | %17s | %4s | %30s | %s" % \
92 (pcuinfo['username'], pcuinfo['password'],
93 pcuinfo[hostname], reboot.pcu_name(pcuinfo), pcuinfo['model'])
95 if 'portstatus' in pcuinfo and pcuinfo['portstatus'] != {}:
96 if pcuinfo['portstatus']['22'] == "open":
97 print "\t ssh -o PasswordAuthentication=yes -o PubkeyAuthentication=no %s@%s" % (pcuinfo['username'], reboot.pcu_name(pcuinfo))
98 if pcuinfo['portstatus']['23'] == "open":
99 print "\t telnet %s" % (reboot.pcu_name(pcuinfo))
100 if pcuinfo['portstatus']['80'] == "open" or \
101 pcuinfo['portstatus']['443'] == "open":
102 print "\t http://%s" % (reboot.pcu_name(pcuinfo))
103 if pcuinfo['portstatus']['443'] == "open":
104 print "\t racadm.py -r %s -u %s -p '%s'" % (pcuinfo['ip'], pcuinfo['username'], pcuinfo['password'])
105 print "\t cmdhttps/locfg.pl -s %s -f iloxml/Reset_Server.xml -u %s -p '%s' | grep MESSAGE" % \
106 (reboot.pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
109 archive = soltesz.SPickle(path)
112 begin = config.fromtime
116 d = datetime_fromstr(begin)
117 tdelta = timedelta(1)
121 file = "%s.production.findbad" % d.strftime("%Y-%m-%d")
124 fb = archive.load(file)
125 if config.node in fb['nodes']:
126 fb_nodeinfo = fb['nodes'][config.node]['values']
127 fb_print_nodeinfo(fb_nodeinfo, verbose, d.strftime("%Y-%m-%d"))
132 print d.strftime("%Y-%m-%d"), "No record"
135 if d > datetime.now(): break