8 from nodecommon import *
9 #from policy import Diagnose
10 from unified_model import Record
13 from reboot import pcu_name
20 fb = database.dbLoad("findbad")
23 class NoKeyException(Exception): pass
25 def daysdown_print_nodeinfo(fbnode, hostname):
26 fbnode['hostname'] = hostname
27 fbnode['daysdown'] = Record.getStrDaysDown(fbnode)
28 fbnode['intdaysdown'] = Record.getDaysDown(fbnode)
30 print "%(intdaysdown)5s %(hostname)-44s | %(state)10.10s | %(daysdown)s" % fbnode
32 def fb_print_nodeinfo(fbnode, hostname, fields=None):
33 fbnode['hostname'] = hostname
34 fbnode['checked'] = diff_time(fbnode['checked'])
36 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
38 fbnode['bootcd'] = "unknown"
39 fbnode['pcu'] = color_pcu_state(fbnode)
42 if 'ERROR' in fbnode['category']:
45 fbnode['kernel'] = fbnode['kernel'].split()[2]
46 fbnode['boot_state'] = fbnode['plcnode']['boot_state']
48 print "%(hostname)-39s | %(checked)11.11s | %(boot_state)5.5s| %(state)8.8s | %(ssh)5.5s | %(pcu)6.6s | %(bootcd)6.6s | %(category)8.8s | %(kernel)s" % fbnode
52 format += "%%(%s)s " % f
56 indexes = path.split("/")
60 values = values[index]
62 raise NoKeyException(index)
65 def verifyType(constraints, data):
67 constraints is a list of key, value pairs.
68 # [ {... : ...}==AND , ... , ... , ] == OR
71 for con in constraints:
72 #print "con: %s" % con
73 if len(con.keys()) == 0:
78 for key in con.keys():
79 #print "looking at key: %s" % key
87 if o.name() == "Match":
88 if get(data,key) is not None:
89 value_re = re.compile(o.value)
90 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
93 elif o.name() == "ListMatch":
94 if get(data,key) is not None:
96 for listitem in get(data,key):
97 value_re = re.compile(o.value)
98 if value_re.search(listitem) is not None:
101 con_and_true = con_and_true & match
104 elif o.name() == "Is":
105 con_and_true = con_and_true & (get(data,key) == o.value)
106 elif o.name() == "FilledIn":
107 con_and_true = con_and_true & (len(get(data,key)) > 0)
108 elif o.name() == "PortOpen":
109 if get(data,key) is not None:
111 con_and_true = con_and_true & (v[str(o.value)] == "open")
115 value_re = re.compile(o.value)
116 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
118 except NoKeyException, key:
119 print "missing key %s" % key,
121 #print "missing key %s" % key
122 #con_and_true = False
124 con_or_true = con_or_true | con_and_true
128 def verify(constraints, data):
130 constraints is a list of key, value pairs.
131 # [ {... : ...}==AND , ... , ... , ] == OR
134 for con in constraints:
135 #print "con: %s" % con
136 if len(con.keys()) == 0:
141 for key in con.keys():
142 #print "looking at key: %s" % key
144 value_re = re.compile(con[key])
145 con_and_true = con_and_true & (value_re.search(data[key]) is not None)
146 elif key not in data:
147 print "missing key %s" % key,
149 #print "missing key %s" % key
150 #con_and_true = False
152 con_or_true = con_or_true | con_and_true
156 def query_to_dict(query):
160 or_queries = query.split('||')
161 for or_query in or_queries:
162 and_queries = or_query.split('&&')
166 for and_query in and_queries:
167 (key, value) = and_query.split('=')
175 if 'plcnode' in fbdata:
176 if 'pcu_ids' in fbdata['plcnode']:
177 if len(fbdata['plcnode']['pcu_ids']) > 0:
181 def pcu_select(str_query, nodelist=None):
184 if str_query is None: return (nodenames, pcunames)
187 dict_query = query_to_dict(str_query)
190 for node in fb['nodes'].keys():
191 if nodelist is not None:
192 if node not in nodelist: continue
194 fb_nodeinfo = fb['nodes'][node]['values']
195 if pcu_in(fb_nodeinfo):
196 pcuinfo = fbpcu['nodes']['id_%s' % fb_nodeinfo['plcnode']['pcu_ids'][0]]['values']
197 if verify(dict_query, pcuinfo):
198 nodenames.append(node)
199 str = "cmdhttps/locfg.pl -s %s -f iloxml/License.xml -u %s -p '%s' | grep MESSAGE" % \
200 (pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
202 return (nodenames, pcunames)
204 def node_select(str_query, nodelist=None, fbdb=None):
206 if str_query is None: return hostnames
209 dict_query = query_to_dict(str_query)
216 for node in fb['nodes'].keys():
217 if nodelist is not None:
218 if node not in nodelist: continue
220 fb_nodeinfo = fb['nodes'][node]['values']
222 if fb_nodeinfo == []:
223 #print node, "has lost values"
226 fb_nodeinfo['pcu'] = color_pcu_state(fb_nodeinfo)
227 fb_nodeinfo['hostname'] = node
228 if 'plcnode' in fb_nodeinfo:
229 fb_nodeinfo.update(fb_nodeinfo['plcnode'])
231 if verify(dict_query, fb_nodeinfo):
232 #print node #fb_nodeinfo
233 hostnames.append(node)
235 #print "NO MATCH", node
245 import parser as parsermodule
246 parser = parsermodule.getParser()
248 parser.set_defaults(node=None, fromtime=None, select=None, list=None,
249 pcuselect=None, nodelist=None, daysdown=None, fields=None)
250 parser.add_option("", "--daysdown", dest="daysdown", action="store_true",
251 help="List the node state and days down...")
252 parser.add_option("", "--select", dest="select", metavar="key=value",
253 help="List all nodes with the given key=value pattern")
254 parser.add_option("", "--fields", dest="fields", metavar="key,list,...",
255 help="a list of keys to display for each entry.")
256 parser.add_option("", "--list", dest="list", action="store_true",
257 help="Write only the hostnames as output.")
258 parser.add_option("", "--pcuselect", dest="pcuselect", metavar="key=value",
259 help="List all nodes with the given key=value pattern")
260 parser.add_option("", "--nodelist", dest="nodelist", metavar="nodelist.txt",
261 help="A list of nodes to bring out of debug mode.")
262 parser.add_option("", "--fromtime", dest="fromtime", metavar="YYYY-MM-DD",
263 help="Specify a starting date from which to begin the query.")
265 parser = parsermodule.getParser(['defaults'], parser)
266 config = parsermodule.parse_args(parser)
270 archive = database.SPickle(path)
271 d = datetime_fromstr(config.fromtime)
272 glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
275 file = glob.glob(glob_str)[0]
276 #print "loading %s" % file
278 fb = archive.load(file[:-4])
280 fb = database.dbLoad("findbad")
282 fbpcu = database.dbLoad("findbadpcus")
285 nodelist = util.file.getListFromFile(config.nodelist)
287 nodelist = fb['nodes'].keys()
290 if config.select is not None and config.pcuselect is not None:
291 nodelist = node_select(config.select, nodelist)
292 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
293 elif config.select is not None:
294 nodelist = node_select(config.select, nodelist)
295 elif config.pcuselect is not None:
296 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
302 for node in nodelist:
305 if node not in fb['nodes']:
308 fb_nodeinfo = fb['nodes'][node]['values']
314 daysdown_print_nodeinfo(fb_nodeinfo, node)
318 fields = config.fields.split(",")
322 fb_print_nodeinfo(fb_nodeinfo, node, fields)
323 elif not config.select and 'state' in fb_nodeinfo:
324 fb_print_nodeinfo(fb_nodeinfo, node)
328 if __name__ == "__main__":