13 from nodequery import verify,query_to_dict,node_select
16 api = plc.getAuthAPI()
17 from unified_model import *
18 from monitor_policy import MINUP
21 externalState = {'round': round, 'nodes': {}}
26 externalState = database.if_cached_else(1, config.dbname, lambda : externalState)
28 # update global round number to force refreshes across all nodes
29 externalState['round'] += 1
31 l_nodes = syncplcdb.create_plcdb()
32 l_plcnodes = database.dbLoad("l_plcnodes")
35 l_nodes = [config.node]
37 l_nodes = [node['hostname'] for node in l_plcnodes]
39 checkAndRecordState(l_nodes, l_plcnodes)
41 def checkAndRecordState(l_nodes, l_plcnodes):
44 global_round = externalState['round']
46 for nodename in l_nodes:
47 if nodename not in externalState['nodes']:
48 externalState['nodes'][nodename] = {'round': 0, 'values': []}
50 node_round = externalState['nodes'][nodename]['round']
51 if node_round < global_round:
53 values = collectStatusAndState(nodename, l_plcnodes)
54 global_round = externalState['round']
55 externalState['nodes'][nodename]['values'] = values
56 externalState['nodes'][nodename]['round'] = global_round
61 database.dbDump(config.dbname, externalState)
63 database.dbDump(config.dbname, externalState)
65 fb = database.dbLoad('findbad')
66 hn2lb = database.dbLoad("plcdb_hn2lb")
68 def getnodesup(nodelist):
71 if node['hostname'] in fb['nodes'].keys():
73 if fb['nodes'][node['hostname']]['values']['state'] == "BOOT":
80 indexes = path.split("/")
84 values = values[index]
89 def collectStatusAndState(nodename, l_plcnodes):
93 for node in l_plcnodes:
94 if node['hostname'] == nodename:
100 pf = PersistFlags(nodename, 1, db='node_persistflags')
102 if not pf.checkattr('last_changed'):
103 pf.last_changed = time.time()
105 pf.last_checked = time.time()
107 if not pf.checkattr('status'):
108 pf.status = "unknown"
110 state_path = "nodes/" + nodename + "/values/state"
111 bootstate_path = "nodes/" + nodename + "/values/plcnode/boot_state"
113 if get(fb, state_path) == "BOOT":
114 if pf.status != "good": pf.last_changed = time.time()
116 elif get(fb, state_path) == "DEBUG":
117 bs = get(fb, bootstate_path)
118 if pf.status != bs: pf.last_changed = time.time()
121 if pf.status != "down": pf.last_changed = time.time()
125 print "%d %35s %s since(%s)" % (count, nodename, pf.status, diff_time(pf.last_changed))
126 # updated by other modules
134 if __name__ == '__main__':
135 from config import config
136 from optparse import OptionParser
137 parser = OptionParser()
138 parser.set_defaults(filename=None, node=None, nodeselect=False, nodegroup=None,
139 increment=False, dbname="nodebad", cachenodes=False)
140 parser.add_option("", "--node", dest="node", metavar="hostname",
141 help="Provide a single node to operate on")
142 parser.add_option("", "--nodelist", dest="nodelist", metavar="file.list",
143 help="Provide a list of files to operate on")
145 parser.add_option("", "--dbname", dest="dbname", metavar="FILE",
146 help="Specify the name of the database to which the information is saved")
147 parser.add_option("-i", "--increment", action="store_true", dest="increment",
148 help="Increment round number to force refresh or retry")
149 config = config(parser)
154 except Exception, err:
156 print traceback.print_exc()
157 print "Exception: %s" % err
158 print "Saving data... exitting."
159 database.dbDump(config.dbname, externalState)