8 from nodecommon import *
9 from policy import Diagnose
12 from reboot import pcu_name
18 fb = database.dbLoad("findbad")
21 class NoKeyException(Exception): pass
23 def daysdown_print_nodeinfo(fbnode, hostname):
24 fbnode['hostname'] = hostname
25 fbnode['daysdown'] = Diagnose.getStrDaysDown(fbnode)
26 fbnode['intdaysdown'] = Diagnose.getDaysDown(fbnode)
28 print "%(intdaysdown)5s %(hostname)-44s | %(state)10.10s | %(daysdown)s" % fbnode
30 def fb_print_nodeinfo(fbnode, hostname, fields=None):
31 fbnode['hostname'] = hostname
32 fbnode['checked'] = diff_time(fbnode['checked'])
34 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
36 fbnode['bootcd'] = "unknown"
37 fbnode['pcu'] = color_pcu_state(fbnode)
40 if 'ERROR' in fbnode['category']:
43 fbnode['kernel'] = fbnode['kernel'].split()[2]
44 fbnode['boot_state'] = fbnode['plcnode']['boot_state']
46 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
50 format += "%%(%s)s " % f
54 indexes = path.split("/")
58 values = values[index]
60 raise NoKeyException(index)
63 def verifyType(constraints, data):
65 constraints is a list of key, value pairs.
66 # [ {... : ...}==AND , ... , ... , ] == OR
69 for con in constraints:
70 #print "con: %s" % con
71 if len(con.keys()) == 0:
76 for key in con.keys():
77 #print "looking at key: %s" % key
85 if o.name() == "Match":
86 if get(data,key) is not None:
87 value_re = re.compile(o.value)
88 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
91 elif o.name() == "ListMatch":
92 if get(data,key) is not None:
94 for listitem in get(data,key):
95 value_re = re.compile(o.value)
96 if value_re.search(listitem) is not None:
99 con_and_true = con_and_true & match
102 elif o.name() == "Is":
103 con_and_true = con_and_true & (get(data,key) == o.value)
104 elif o.name() == "FilledIn":
105 con_and_true = con_and_true & (len(get(data,key)) > 0)
106 elif o.name() == "PortOpen":
107 if get(data,key) is not None:
109 con_and_true = con_and_true & (v[str(o.value)] == "open")
113 value_re = re.compile(o.value)
114 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
116 except NoKeyException, key:
117 print "missing key %s" % key,
119 #print "missing key %s" % key
120 #con_and_true = False
122 con_or_true = con_or_true | con_and_true
126 def verify(constraints, data):
128 constraints is a list of key, value pairs.
129 # [ {... : ...}==AND , ... , ... , ] == OR
132 for con in constraints:
133 #print "con: %s" % con
134 if len(con.keys()) == 0:
139 for key in con.keys():
140 #print "looking at key: %s" % key
142 value_re = re.compile(con[key])
143 con_and_true = con_and_true & (value_re.search(data[key]) is not None)
144 elif key not in data:
145 print "missing key %s" % key,
147 #print "missing key %s" % key
148 #con_and_true = False
150 con_or_true = con_or_true | con_and_true
154 def query_to_dict(query):
158 or_queries = query.split('||')
159 for or_query in or_queries:
160 and_queries = or_query.split('&&')
164 for and_query in and_queries:
165 (key, value) = and_query.split('=')
173 if 'plcnode' in fbdata:
174 if 'pcu_ids' in fbdata['plcnode']:
175 if len(fbdata['plcnode']['pcu_ids']) > 0:
179 def pcu_select(str_query, nodelist=None):
182 if str_query is None: return (nodenames, pcunames)
185 dict_query = query_to_dict(str_query)
188 for node in fb['nodes'].keys():
189 if nodelist is not None:
190 if node not in nodelist: continue
192 fb_nodeinfo = fb['nodes'][node]['values']
193 if pcu_in(fb_nodeinfo):
194 pcuinfo = fbpcu['nodes']['id_%s' % fb_nodeinfo['plcnode']['pcu_ids'][0]]['values']
195 if verify(dict_query, pcuinfo):
196 nodenames.append(node)
197 str = "cmdhttps/locfg.pl -s %s -f iloxml/License.xml -u %s -p '%s' | grep MESSAGE" % \
198 (pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
200 return (nodenames, pcunames)
202 def node_select(str_query, nodelist=None, fbdb=None):
204 if str_query is None: return hostnames
207 dict_query = query_to_dict(str_query)
214 for node in fb['nodes'].keys():
215 if nodelist is not None:
216 if node not in nodelist: continue
218 fb_nodeinfo = fb['nodes'][node]['values']
220 if fb_nodeinfo == []:
221 #print node, "has lost values"
224 fb_nodeinfo['pcu'] = color_pcu_state(fb_nodeinfo)
225 fb_nodeinfo['hostname'] = node
226 if 'plcnode' in fb_nodeinfo:
227 fb_nodeinfo.update(fb_nodeinfo['plcnode'])
229 if verify(dict_query, fb_nodeinfo):
230 #print node #fb_nodeinfo
231 hostnames.append(node)
233 #print "NO MATCH", node
243 from config import config
244 from optparse import OptionParser
245 parser = OptionParser()
246 parser.set_defaults(node=None, fromtime=None, select=None, list=None, pcuselect=None, nodelist=None, daysdown=None, fields=None)
247 parser.add_option("", "--daysdown", dest="daysdown", action="store_true",
248 help="List the node state and days down...")
249 parser.add_option("", "--select", dest="select", metavar="key=value",
250 help="List all nodes with the given key=value pattern")
251 parser.add_option("", "--fields", dest="fields", metavar="key,list,...",
252 help="a list of keys to display for each entry.")
253 parser.add_option("", "--list", dest="list", action="store_true",
254 help="Write only the hostnames as output.")
255 parser.add_option("", "--pcuselect", dest="pcuselect", metavar="key=value",
256 help="List all nodes with the given key=value pattern")
257 parser.add_option("", "--nodelist", dest="nodelist", metavar="nodelist.txt",
258 help="A list of nodes to bring out of debug mode.")
259 parser.add_option("", "--fromtime", dest="fromtime", metavar="YYYY-MM-DD",
260 help="Specify a starting date from which to begin the query.")
261 config = config(parser)
266 archive = database.SPickle(path)
267 d = datetime_fromstr(config.fromtime)
268 glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
271 file = glob.glob(glob_str)[0]
272 #print "loading %s" % file
274 fb = archive.load(file[:-4])
276 fb = database.dbLoad("findbad")
278 fbpcu = database.dbLoad("findbadpcus")
281 nodelist = config.getListFromFile(config.nodelist)
283 nodelist = fb['nodes'].keys()
286 if config.select is not None and config.pcuselect is not None:
287 nodelist = node_select(config.select, nodelist)
288 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
289 elif config.select is not None:
290 nodelist = node_select(config.select, nodelist)
291 elif config.pcuselect is not None:
292 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
299 for node in nodelist:
302 if node not in fb['nodes']:
305 fb_nodeinfo = fb['nodes'][node]['values']
311 daysdown_print_nodeinfo(fb_nodeinfo, node)
315 fields = config.fields.split(",")
319 fb_print_nodeinfo(fb_nodeinfo, node, fields)
320 elif not config.select and 'state' in fb_nodeinfo:
321 fb_print_nodeinfo(fb_nodeinfo, node)
325 if __name__ == "__main__":