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