move clean_policy.py into monitor package
[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 monitor.pcu import reboot
11 from monitor import parser as parsermodule
12 from monitor import config
13 from monitor.database import HistoryPCURecord, FindbadPCURecord
14 from monitor.wrapper import plc,plccache
15 from monitor.const import MINUP
16
17 from nodecommon import *
18 from nodequery import verify,query_to_dict,node_select
19 from monitor.model import *
20
21 api = plc.getAuthAPI()
22
23 def main(config):
24
25         #l_plcpcus = database.if_cached_else_refresh(1, 1, "pculist", lambda : plc.GetPCUs())
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
98         return True
99
100 if __name__ == '__main__':
101         parser = parsermodule.getParser()
102         parser.set_defaults(filename=None, pcu=None, pcuselect=False, pcugroup=None, cachepcus=False)
103         parser.add_option("", "--pcu", dest="pcu", metavar="hostname", 
104                                                 help="Provide a single pcu to operate on")
105         parser.add_option("", "--pculist", dest="pculist", metavar="file.list", 
106                                                 help="Provide a list of files to operate on")
107
108         config = parsermodule.parse_args(parser)
109
110         try:
111                 main(config)
112         except Exception, err:
113                 import traceback
114                 print traceback.print_exc()
115                 print "Exception: %s" % err
116                 sys.exit(0)