add new scripts
[monitor.git] / statistics / aggregatehistory.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 monitor.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         #print "loading %s" % file
28         os.chdir("..")
29         return file[:-4]
30         #fb = archive.load(file[:-4])
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 agg = {}
76
77 def main():
78         import parser as parsermodule
79
80         parser = parsermodule.getParser()
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 = parsermodule.parse_args(parser)
89
90         path = "archive-pdb"
91         archive = database.SPickle(path)
92
93         if config.fromtime:
94                 begin = config.fromtime
95         else:
96                 begin = "2007-11-06"
97
98         d = datetime_fromstr(begin)
99         tdelta = timedelta(1)
100         verbose = 1
101
102         while True:
103                 try:
104                         file = get_filefromglob(d, "production.findbad")
105                         fb = archive.load(file)
106                         for node in fb['nodes']:
107                                 fb_nodeinfo  = fb['nodes'][node]['values']
108                                 state = fb_nodeinfo['state']
109                                 time = d.strftime("%Y-%m-%d")
110                                 if node not in agg:
111                                         agg[node] = []
112                                 if len(agg[node]) == 0:
113                                         agg[node].append((time, state))
114                                 else:
115                                         oldtime = agg[node][-1][0]
116                                         oldstate = agg[node][-1][1]
117                                         if oldstate != state:
118                                                 agg[node].append((time, state))
119                         del fb
120                         verbose = 0
121                 except KeyboardInterrupt:
122                         sys.exit(1)
123                 except:
124                         #import traceback; print traceback.print_exc()
125                         print d.strftime("%Y-%m-%d"), "No record"
126
127                 d = d + tdelta
128                 if d > datetime.now(): break
129         
130         database.dbDump("aggregatehistory", agg)
131
132 if __name__ == "__main__":
133         main()