adding files
[monitor.git] / nodehistory.py
1 #!/usr/bin/python
2
3 import plc
4 import auth
5 api = plc.PLC(auth.auth, auth.plc)
6
7 import soltesz
8 import reboot
9 import time
10 from datetime import datetime, timedelta
11 import calendar
12
13 import time
14 from model import *
15 from nodecommon import *
16
17 from config import config
18 from optparse import OptionParser
19
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)
29 config.parse_args()
30
31 def datetime_fromstr(str):
32     if '-' in str:
33         tup = time.strptime(str, "%Y-%m-%d")
34     elif '/' in str:
35         tup = time.strptime(str, "%m/%d/%Y")
36     else:
37         tup = time.strptime(str, "%m/%d/%Y")
38     return datetime.fromtimestamp(calendar.timegm(tup))
39
40 def diff_time(timestamp):
41         now = time.time()
42         if timestamp == None:
43                 return "unknown"
44         diff = now - timestamp
45         # return the number of seconds as a difference from current time.
46         t_str = ""
47         if diff < 60: # sec in min.
48                 t = diff
49                 t_str = "%s sec ago" % t
50         elif diff < 60*60: # sec in hour
51                 t = diff // (60)
52                 t_str = "%s min ago" % int(t)
53         elif diff < 60*60*24: # sec in day
54                 t = diff // (60*60)
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)
65         return t_str
66
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']),
71         else:
72                 if date: print date,
73                 else: print "Unknown",
74                 
75         if fbnode['bootcd']:
76                 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
77         else:
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
82
83 def pcu_print_info(pcuinfo, hostname):
84         print "   Checked: ",
85         if 'checked' in pcuinfo:
86                 print "%11.11s " % diff_time(pcuinfo['checked'])
87         else:
88                 print "Unknown"
89
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'])
94
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'])
107
108 path = "archive-pdb"
109 archive = soltesz.SPickle(path)
110
111 if config.fromtime:
112         begin = config.fromtime
113 else:
114         begin = "2007-11-06"
115
116 d = datetime_fromstr(begin)
117 tdelta = timedelta(1)
118 verbose = 1
119
120 while True:
121         file = "%s.production.findbad" % d.strftime("%Y-%m-%d")
122         
123         try:
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"))
128
129                 del fb
130                 verbose = 0
131         except:
132                 print d.strftime("%Y-%m-%d"), "No record"
133
134         d = d + tdelta
135         if d > datetime.now(): break
136