modified *list templates with abreviated information
[monitor.git] / pcubad.py
1 #!/usr/bin/python
2
3 import os
4 import sys
5 import string
6 import time
7 from datetime import datetime,timedelta
8
9 from monitor import database
10 from pcucontrol  import reboot
11 from monitor import parser as parsermodule
12 from monitor import config
13 from monitor.database.info.model import HistoryPCURecord, FindbadPCURecord
14 from monitor.database.dborm import mon_session as session
15 from monitor.wrapper import plc,plccache
16 from monitor.const import MINUP
17
18 from nodecommon import *
19 from nodequery import verify,query_to_dict,node_select
20 from monitor.model import *
21
22 api = plc.getAuthAPI()
23
24 def main(config):
25
26         l_plcpcus = plccache.l_pcus 
27
28         l_pcus = None
29         if config.pcu:
30                 for pcu in l_plcpcus:
31                         if ( pcu['hostname'] is not None and config.pcu in pcu['hostname'] ) or \
32                            ( pcu['ip'] is not None and config.pcu in pcu['ip'] ):
33                                 l_pcus = [pcu['pcu_id']]
34                 if not l_pcus:
35                         print "ERROR: could not find pcu %s" % config.pcu
36                         sys.exit(1)
37         else:
38                 l_pcus = [pcu['pcu_id'] for pcu in l_plcpcus]
39         
40         checkAndRecordState(l_pcus, l_plcpcus)
41
42 hn2lb = plccache.plcdb_hn2lb
43
44 def checkAndRecordState(l_pcus, l_plcpcus):
45         count = 0
46         for pcuname in l_pcus:
47
48                 d_pcu = None
49                 for pcu in l_plcpcus:
50                         if pcu['pcu_id'] == pcuname:
51                                 d_pcu = pcu
52                                 break
53                 if not d_pcu:
54                         continue
55
56                 pf = HistoryPCURecord.findby_or_create(plc_pcuid=d_pcu['pcu_id'])
57                 pf.last_checked = datetime.now()
58
59                 try:
60                         # Find the most recent record
61                         pcurec = FindbadPCURecord.query.filter(FindbadPCURecord.plc_pcuid==pcuname).order_by(FindbadPCURecord.date_checked.desc()).first()
62                         print "NODEREC: ", pcurec.date_checked
63                 except:
64                         print "COULD NOT FIND FB record for %s" % reboot.pcu_name(pcu)
65                         import traceback
66                         print traceback.print_exc()
67                         # don't have the info to create a new entry right now, so continue.
68                         continue 
69
70                 pcu_state      = pcurec.reboot_trial_status
71                 current_state = pcu_state
72
73                 if current_state == 0 or current_state == "0":
74                         if pf.status != "good": 
75                                 pf.last_changed = datetime.now() 
76                                 pf.status = "good"
77                 elif current_state == 'NetDown':
78                         if pf.status != "netdown": 
79                                 pf.last_changed = datetime.now()
80                                 pf.status = "netdown"
81                 elif current_state == 'Not_Run':
82                         if pf.status != "badconfig": 
83                                 pf.last_changed = datetime.now()
84                                 pf.status = "badconfig"
85                 else:
86                         if pf.status != "error": 
87                                 pf.last_changed = datetime.now()
88                                 pf.status = "error"
89
90                 count += 1
91                 print "%d %35s %s since(%s)" % (count, reboot.pcu_name(d_pcu), pf.status, diff_time(time.mktime(pf.last_changed.timetuple())))
92
93         # NOTE: this commits all pending operations to the DB.  Do not remove, or
94         # replace with another operations that also commits all pending ops, such
95         # as session.commit() or flush() or something
96         print HistoryPCURecord.query.count()
97         session.flush()
98
99         return True
100
101 if __name__ == '__main__':
102         parser = parsermodule.getParser()
103         parser.set_defaults(filename=None, pcu=None, pcuselect=False, pcugroup=None, cachepcus=False)
104         parser.add_option("", "--pcu", dest="pcu", metavar="hostname", 
105                                                 help="Provide a single pcu to operate on")
106         parser.add_option("", "--pculist", dest="pculist", metavar="file.list", 
107                                                 help="Provide a list of files to operate on")
108
109         config = parsermodule.parse_args(parser)
110
111         try:
112                 main(config)
113         except Exception, err:
114                 import traceback
115                 print traceback.print_exc()
116                 print "Exception: %s" % err
117                 sys.exit(0)