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