8 from nodecommon import *
9 #from policy import Diagnose
10 from unified_model import Record
13 from reboot import pcu_name
24 class NoKeyException(Exception): pass
26 def daysdown_print_nodeinfo(fbnode, hostname):
27 fbnode['hostname'] = hostname
28 fbnode['daysdown'] = Record.getStrDaysDown(fbnode)
29 fbnode['intdaysdown'] = Record.getDaysDown(fbnode)
31 print "%(intdaysdown)5s %(hostname)-44s | %(state)10.10s | %(daysdown)s" % fbnode
33 def fb_print_nodeinfo(fbnode, hostname, fields=None):
34 fbnode['hostname'] = hostname
35 fbnode['checked'] = diff_time(fbnode['checked'])
37 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
39 fbnode['bootcd'] = "unknown"
40 fbnode['pcu'] = color_pcu_state(fbnode)
43 if 'ERROR' in fbnode['category']:
46 fbnode['kernel'] = fbnode['kernel'].split()[2]
47 fbnode['boot_state'] = fbnode['plcnode']['boot_state']
50 if len(fbnode['nodegroups']) > 0:
51 fbnode['category'] = fbnode['nodegroups'][0]
53 #print "ERROR!!!!!!!!!!!!!!!!!!!!!"
56 print "%(hostname)-45s | %(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
60 format += "%%(%s)s " % f
64 indexes = path.split("/")
68 values = values[index]
70 raise NoKeyException(index)
73 def verifyType(constraints, data):
75 constraints is a list of key, value pairs.
76 # [ {... : ...}==AND , ... , ... , ] == OR
79 for con in constraints:
80 #print "con: %s" % con
81 if len(con.keys()) == 0:
86 for key in con.keys():
87 #print "looking at key: %s" % key
95 if o.name() == "Match":
96 if get(data,key) is not None:
97 value_re = re.compile(o.value)
98 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
101 elif o.name() == "ListMatch":
102 if get(data,key) is not None:
104 for listitem in get(data,key):
105 value_re = re.compile(o.value)
106 if value_re.search(listitem) is not None:
109 con_and_true = con_and_true & match
112 elif o.name() == "Is":
113 con_and_true = con_and_true & (get(data,key) == o.value)
114 elif o.name() == "FilledIn":
115 con_and_true = con_and_true & (len(get(data,key)) > 0)
116 elif o.name() == "PortOpen":
117 if get(data,key) is not None:
119 con_and_true = con_and_true & (v[str(o.value)] == "open")
123 value_re = re.compile(o.value)
124 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
126 except NoKeyException, key:
127 print "missing key %s" % key,
129 #print "missing key %s" % key
130 #con_and_true = False
132 con_or_true = con_or_true | con_and_true
136 def verify(constraints, data):
138 constraints is a list of key, value pairs.
139 # [ {... : ...}==AND , ... , ... , ] == OR
142 for con in constraints:
143 #print "con: %s" % con
144 if len(con.keys()) == 0:
149 for key in con.keys():
150 #print "looking at key: %s" % key
152 value_re = re.compile(con[key])
153 if type([]) == type(data[key]):
154 local_or_true = False
155 for val in data[key]:
156 local_or_true = local_or_true | (value_re.search(val) is not None)
157 con_and_true = con_and_true & local_or_true
159 con_and_true = con_and_true & (value_re.search(data[key]) is not None)
160 elif key not in data:
161 print "missing key %s" % key,
163 #print "missing key %s" % key
164 #con_and_true = False
166 con_or_true = con_or_true | con_and_true
170 def query_to_dict(query):
174 or_queries = query.split('||')
175 for or_query in or_queries:
176 and_queries = or_query.split('&&')
180 for and_query in and_queries:
181 (key, value) = and_query.split('=')
189 if 'plcnode' in fbdata:
190 if 'pcu_ids' in fbdata['plcnode']:
191 if len(fbdata['plcnode']['pcu_ids']) > 0:
195 def pcu_select(str_query, nodelist=None):
200 if str_query is None: return (nodenames, pcunames)
203 fb = database.dbLoad("findbad")
205 fbpcu = database.dbLoad("findbadpcus")
208 dict_query = query_to_dict(str_query)
211 for node in fb['nodes'].keys():
212 if nodelist is not None:
213 if node not in nodelist: continue
215 fb_nodeinfo = fb['nodes'][node]['values']
216 if pcu_in(fb_nodeinfo):
217 pcuinfo = fbpcu['nodes']['id_%s' % fb_nodeinfo['plcnode']['pcu_ids'][0]]['values']
218 if verify(dict_query, pcuinfo):
219 nodenames.append(node)
220 str = "cmdhttps/locfg.pl -s %s -f iloxml/License.xml -u %s -p '%s' | grep MESSAGE" % \
221 (pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
222 #pcunames.append(str)
223 pcunames.append(pcuinfo['pcu_id'])
224 return (nodenames, pcunames)
226 def node_select(str_query, nodelist=None, fbdb=None):
230 if str_query is None: return hostnames
233 dict_query = query_to_dict(str_query)
239 for node in fb['nodes'].keys():
240 if nodelist is not None:
241 if node not in nodelist: continue
243 fb_nodeinfo = fb['nodes'][node]['values']
245 if fb_nodeinfo == []:
246 #print node, "has lost values"
249 fb_nodeinfo['pcu'] = color_pcu_state(fb_nodeinfo)
250 fb_nodeinfo['hostname'] = node
251 if 'plcnode' in fb_nodeinfo:
252 fb_nodeinfo.update(fb_nodeinfo['plcnode'])
254 if verify(dict_query, fb_nodeinfo):
255 #print node #fb_nodeinfo
256 hostnames.append(node)
258 #print "NO MATCH", node
268 import parser as parsermodule
269 parser = parsermodule.getParser()
271 parser.set_defaults(node=None, fromtime=None, select=None, list=None,
272 pcuselect=None, nodelist=None, daysdown=None, fields=None)
273 parser.add_option("", "--daysdown", dest="daysdown", action="store_true",
274 help="List the node state and days down...")
275 parser.add_option("", "--select", dest="select", metavar="key=value",
276 help="List all nodes with the given key=value pattern")
277 parser.add_option("", "--fields", dest="fields", metavar="key,list,...",
278 help="a list of keys to display for each entry.")
279 parser.add_option("", "--list", dest="list", action="store_true",
280 help="Write only the hostnames as output.")
281 parser.add_option("", "--pcuselect", dest="pcuselect", metavar="key=value",
282 help="List all nodes with the given key=value pattern")
283 parser.add_option("", "--nodelist", dest="nodelist", metavar="nodelist.txt",
284 help="A list of nodes to bring out of debug mode.")
285 parser.add_option("", "--fromtime", dest="fromtime", metavar="YYYY-MM-DD",
286 help="Specify a starting date from which to begin the query.")
288 parser = parsermodule.getParser(['defaults'], parser)
289 config = parsermodule.parse_args(parser)
293 archive = database.SPickle(path)
294 d = datetime_fromstr(config.fromtime)
295 glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
298 file = glob.glob(glob_str)[0]
299 #print "loading %s" % file
301 fb = archive.load(file[:-4])
303 fb = database.dbLoad("findbad")
305 fbpcu = database.dbLoad("findbadpcus")
309 nodelist = util.file.getListFromFile(config.nodelist)
311 nodelist = fb['nodes'].keys()
314 if config.select is not None and config.pcuselect is not None:
315 nodelist = node_select(config.select, nodelist, fb)
316 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
317 elif config.select is not None:
318 nodelist = node_select(config.select, nodelist, fb)
319 elif config.pcuselect is not None:
320 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
326 for node in nodelist:
329 if node not in fb['nodes']:
332 fb_nodeinfo = fb['nodes'][node]['values']
338 daysdown_print_nodeinfo(fb_nodeinfo, node)
342 fields = config.fields.split(",")
346 fb_print_nodeinfo(fb_nodeinfo, node, fields)
347 elif not config.select and 'state' in fb_nodeinfo:
348 fb_print_nodeinfo(fb_nodeinfo, node)
352 if __name__ == "__main__":