9 from monitor import database
10 from nodequeryold import verify,query_to_dict,node_select
11 from monitor.common import *
13 from monitor.wrapper import plc
14 api = plc.getAuthAPI()
15 from monitor.model import *
18 externalState = {'round': round, 'nodes': {}}
23 externalState = database.if_cached_else(1, config.dbname, lambda : externalState)
25 # update global round number to force refreshes across all nodes
26 externalState['round'] += 1
28 #l_nodes = syncplcdb.create_plcdb()
29 l_plcnodes = database.dbLoad("l_plcnodes")
31 l_nodes = get_nodeset(config)
33 # l_nodes = [config.node]
35 # l_nodes = [node['hostname'] for node in l_plcnodes]
37 checkAndRecordState(l_nodes, l_plcnodes)
39 def checkAndRecordState(l_nodes, l_plcnodes):
42 global_round = externalState['round']
44 for nodename in l_nodes:
45 if nodename not in externalState['nodes']:
46 externalState['nodes'][nodename] = {'round': 0, 'values': []}
48 node_round = externalState['nodes'][nodename]['round']
49 if node_round < global_round:
51 values = collectStatusAndState(nodename, l_plcnodes)
52 global_round = externalState['round']
53 externalState['nodes'][nodename]['values'] = values
54 externalState['nodes'][nodename]['round'] = global_round
59 database.dbDump(config.dbname, externalState)
61 database.dbDump(config.dbname, externalState)
63 fb = database.dbLoad('findbad')
65 def getnodesup(nodelist):
68 if node['hostname'] in fb['nodes'].keys():
70 if fb['nodes'][node['hostname']]['values']['state'] == "BOOT":
77 indexes = path.split("/")
81 values = values[index]
86 def collectStatusAndState(nodename, l_plcnodes):
90 for node in l_plcnodes:
91 if node['hostname'] == nodename:
97 pf = PersistFlags(nodename, 1, db='node_persistflags')
99 if not pf.checkattr('last_changed'):
100 pf.last_changed = time.time()
102 pf.last_checked = time.time()
104 if not pf.checkattr('status'):
105 pf.status = "unknown"
107 state_path = "nodes/" + nodename + "/values/state"
108 bootstate_path = "nodes/" + nodename + "/values/plcnode/boot_state"
110 if get(fb, state_path) == "BOOT":
111 if pf.status != "good": pf.last_changed = time.time()
113 elif get(fb, state_path) == "DEBUG":
114 bs = get(fb, bootstate_path)
115 if pf.status != bs: pf.last_changed = time.time()
118 if pf.status != "down": pf.last_changed = time.time()
122 print "%d %35s %s since(%s)" % (count, nodename, pf.status, diff_time(pf.last_changed))
123 # updated by other modules
131 if __name__ == '__main__':
132 import parser as parsermodule
133 parser = parsermodule.getParser(['nodesets'])
134 parser.set_defaults(filename=None, node=None, nodeselect=False, nodegroup=None,
135 increment=False, dbname="nodebad", cachenodes=False)
137 parser.add_option("", "--dbname", dest="dbname", metavar="FILE",
138 help="Specify the name of the database to which the information is saved")
139 parser.add_option("-i", "--increment", action="store_true", dest="increment",
140 help="Increment round number to force refresh or retry")
141 parser = parsermodule.getParser(['defaults'], parser)
142 config = parsermodule.parse_args(parser)
146 except Exception, err:
148 print traceback.print_exc()
149 print "Exception: %s" % err
150 print "Saving data... exitting."
151 database.dbDump(config.dbname, externalState)