13 from nodequery import verify,query_to_dict,node_select
14 from nodecommon import *
17 api = plc.getAuthAPI()
18 from unified_model import *
19 from const import MINUP
22 externalState = {'round': round, 'nodes': {}}
27 externalState = database.if_cached_else(1, config.dbname, lambda : externalState)
29 # update global round number to force refreshes across all nodes
30 externalState['round'] += 1
32 l_nodes = syncplcdb.create_plcdb()
33 l_plcnodes = database.dbLoad("l_plcnodes")
35 l_nodes = get_nodeset(config)
38 # l_nodes = [config.node]
40 # l_nodes = [node['hostname'] for node in l_plcnodes]
42 checkAndRecordState(l_nodes, l_plcnodes)
44 def checkAndRecordState(l_nodes, l_plcnodes):
47 global_round = externalState['round']
49 for nodename in l_nodes:
50 if nodename not in externalState['nodes']:
51 externalState['nodes'][nodename] = {'round': 0, 'values': []}
53 node_round = externalState['nodes'][nodename]['round']
54 if node_round < global_round:
56 values = collectStatusAndState(nodename, l_plcnodes)
57 global_round = externalState['round']
58 externalState['nodes'][nodename]['values'] = values
59 externalState['nodes'][nodename]['round'] = global_round
61 pf = PersistFlags(nodename, 1, db='node_persistflags')
62 print "%d %35s %s since %s" % (count, nodename, pf.status, pf.last_changed)
67 database.dbDump(config.dbname, externalState)
69 database.dbDump(config.dbname, externalState)
71 fb = database.dbLoad('findbad')
73 def getnodesup(nodelist):
76 if node['hostname'] in fb['nodes'].keys():
78 if fb['nodes'][node['hostname']]['values']['state'] == "BOOT":
85 indexes = path.split("/")
89 values = values[index]
94 def collectStatusAndState(nodename, l_plcnodes):
98 for node in l_plcnodes:
99 if node['hostname'] == nodename:
105 pf = PersistFlags(nodename, 1, db='node_persistflags')
107 if not pf.checkattr('last_changed'):
108 pf.last_changed = time.time()
110 pf.last_checked = time.time()
112 if not pf.checkattr('status'):
113 pf.status = "unknown"
115 state_path = "nodes/" + nodename + "/values/state"
116 bootstate_path = "nodes/" + nodename + "/values/plcnode/boot_state"
118 if get(fb, state_path) == "BOOT":
119 if pf.status != "good": pf.last_changed = time.time()
121 elif get(fb, state_path) == "DEBUG":
122 bs = get(fb, bootstate_path)
123 if pf.status != bs: pf.last_changed = time.time()
126 if pf.status != "down": pf.last_changed = time.time()
130 print "%d %35s %s since(%s)" % (count, nodename, pf.status, diff_time(pf.last_changed))
131 # updated by other modules
139 if __name__ == '__main__':
140 import parser as parsermodule
141 parser = parsermodule.getParser(['nodesets'])
142 parser.set_defaults(filename=None, node=None, nodeselect=False, nodegroup=None,
143 increment=False, dbname="nodebad", cachenodes=False)
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 parser = parsermodule.getParser(['defaults'], parser)
150 config = parsermodule.parse_args(parser)
154 except Exception, err:
156 print traceback.print_exc()
157 from nodecommon import email_exception
159 print "Exception: %s" % err
160 print "Saving data... exitting."
161 database.dbDump(config.dbname, externalState)