changes for 3.0
[monitor.git] / nodehistory.py
1 #!/usr/bin/python
2
3 import plc
4 api = plc.getAuthAPI()
5
6 import database
7 import reboot
8 import time
9 from datetime import datetime, timedelta
10 import calendar
11
12 import sys
13 import time
14 from model import *
15 from nodecommon import *
16
17 def get_filefromglob(d, str):
18         import os
19         import glob
20         # TODO: This is aweful.
21         path = "archive-pdb"
22         archive = database.SPickle(path)
23         glob_str = "%s*.%s.pkl" % (d.strftime("%Y-%m-%d"), str)
24         os.chdir(path)
25         #print glob_str
26         #file = glob.glob(glob_str)[0]
27         files = glob.glob(glob_str)
28         #print "loading %s" % file
29         os.chdir("..")
30         files_chng = [ file[:-4] for file in files ]
31         return files_chng
32         #fb = archive.load(file[:-4])
33
34
35 def fb_print_nodeinfo(fbnode, verbose, date=None):
36         if verbose: print "              state |  ssh  |  pcu  | bootcd | category | kernel"
37         if 'checked' in fbnode:
38                 if date: print date,
39                 #print "%11.11s " % diff_time(fbnode['checked']),
40         else:
41                 if date: print date,
42                 else: print "Unknown",
43                 
44         if fbnode['bootcd']:
45                 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
46         else:
47                 fbnode['bootcd'] = "unknown"
48         fbnode['state'] = color_boot_state(get_current_state(fbnode))
49         fbnode['boot_state'] = getvalue(fbnode, 'plcnode/boot_state')
50         if len(fbnode['kernel'].split()) >= 3:
51                 fbnode['kernel'] = fbnode['kernel'].split()[2]
52         print "    %(state)5s | %(boot_state)s | %(ssh)5.5s | %(pcu)5.5s | %(bootcd)6.6s | %(category)8.8s | %(kernel)s" % fbnode
53
54 def pcu_print_info(pcuinfo, hostname):
55         print "   Checked: ",
56         if 'checked' in pcuinfo:
57                 print "%11.11s " % diff_time(pcuinfo['checked'])
58         else:
59                 print "Unknown"
60
61         print "\t            user   |          password | port | hostname "
62         print "\t %17s | %17s | %4s | %30s | %s" % \
63                 (pcuinfo['username'], pcuinfo['password'], 
64                  pcuinfo[hostname], reboot.pcu_name(pcuinfo), pcuinfo['model'])
65
66         if 'portstatus' in pcuinfo and pcuinfo['portstatus'] != {}:
67                 if pcuinfo['portstatus']['22'] == "open":
68                         print "\t ssh -o PasswordAuthentication=yes -o PubkeyAuthentication=no %s@%s" % (pcuinfo['username'], reboot.pcu_name(pcuinfo))
69                 if pcuinfo['portstatus']['23'] == "open":
70                         print "\t telnet %s" % (reboot.pcu_name(pcuinfo))
71                 if pcuinfo['portstatus']['80'] == "open" or \
72                         pcuinfo['portstatus']['443'] == "open":
73                         print "\t http://%s" % (reboot.pcu_name(pcuinfo))
74                 if pcuinfo['portstatus']['443'] == "open":
75                         print "\t racadm.py -r %s -u %s -p '%s'" % (pcuinfo['ip'], pcuinfo['username'], pcuinfo['password'])
76                         print "\t cmdhttps/locfg.pl -s %s -f iloxml/Reset_Server.xml -u %s -p '%s' | grep MESSAGE" % \
77                                 (reboot.pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
78
79 def main():
80         import parser as parsermodule
81
82         parser = parsermodule.getParser()
83         parser.set_defaults(node=None, fields='state', fromtime=None)
84         parser.add_option("", "--node", dest="node", metavar="nodename.edu", 
85                                                 help="A single node name to add to the nodegroup")
86         parser.add_option("", "--fields", dest="fields", metavar="key",
87                                                 help="Which record field to extract from all files.")
88         parser.add_option("", "--fromtime", dest="fromtime", metavar="YYYY-MM-DD",
89                                                 help="Specify a starting date from which to begin the query.")
90         config = parsermodule.parse_args(parser)
91
92         path = "archive-pdb"
93         archive = database.SPickle(path)
94
95         if config.fromtime:
96                 begin = config.fromtime
97         else:
98                 begin = "2007-11-06"
99
100         if config.node is None and len(config.args) > 0:
101                 config.node = config.args[0]
102         elif config.node is None:
103                 print "Add a hostname to arguments"
104                 print "exit."
105                 sys.exit(1)
106
107         d = datetime_fromstr(begin)
108         tdelta = timedelta(1)
109         verbose = 1
110
111         while True:
112                 
113                 try:
114                         for file in get_filefromglob(d, "production.findbad"):
115                                 #file = get_filefromglob(d, "production.findbad")
116                                 #file = "%s.production.findbad" % d.strftime("%Y-%m-%d")
117                                 fb = archive.load(file)
118                                 if config.node in fb['nodes']:
119                                         fb_nodeinfo  = fb['nodes'][config.node]['values']
120                                         fb_print_nodeinfo(fb_nodeinfo, verbose, d.strftime("%Y-%m-%d"))
121
122                                 del fb
123                                 verbose = 0
124
125                 except KeyboardInterrupt:
126                         sys.exit(1)
127                 except:
128                         print d.strftime("%Y-%m-%d"), "No record"
129
130                 d = d + tdelta
131                 if d > datetime.now(): break
132
133 if __name__ == "__main__":
134         main()