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']
49 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
53 format += "%%(%s)s " % f
57 indexes = path.split("/")
61 values = values[index]
63 raise NoKeyException(index)
66 def verifyType(constraints, data):
68 constraints is a list of key, value pairs.
69 # [ {... : ...}==AND , ... , ... , ] == OR
72 for con in constraints:
73 #print "con: %s" % con
74 if len(con.keys()) == 0:
79 for key in con.keys():
80 #print "looking at key: %s" % key
88 if o.name() == "Match":
89 if get(data,key) is not None:
90 value_re = re.compile(o.value)
91 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
94 elif o.name() == "ListMatch":
95 if get(data,key) is not None:
97 for listitem in get(data,key):
98 value_re = re.compile(o.value)
99 if value_re.search(listitem) is not None:
102 con_and_true = con_and_true & match
105 elif o.name() == "Is":
106 con_and_true = con_and_true & (get(data,key) == o.value)
107 elif o.name() == "FilledIn":
108 con_and_true = con_and_true & (len(get(data,key)) > 0)
109 elif o.name() == "PortOpen":
110 if get(data,key) is not None:
112 con_and_true = con_and_true & (v[str(o.value)] == "open")
116 value_re = re.compile(o.value)
117 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
119 except NoKeyException, key:
120 print "missing key %s" % key,
122 #print "missing key %s" % key
123 #con_and_true = False
125 con_or_true = con_or_true | con_and_true
129 def verify(constraints, data):
131 constraints is a list of key, value pairs.
132 # [ {... : ...}==AND , ... , ... , ] == OR
135 for con in constraints:
136 #print "con: %s" % con
137 if len(con.keys()) == 0:
142 for key in con.keys():
143 #print "looking at key: %s" % key
145 value_re = re.compile(con[key])
146 con_and_true = con_and_true & (value_re.search(data[key]) is not None)
147 elif key not in data:
148 print "missing key %s" % key,
150 #print "missing key %s" % key
151 #con_and_true = False
153 con_or_true = con_or_true | con_and_true
157 def query_to_dict(query):
161 or_queries = query.split('||')
162 for or_query in or_queries:
163 and_queries = or_query.split('&&')
167 for and_query in and_queries:
168 (key, value) = and_query.split('=')
176 if 'plcnode' in fbdata:
177 if 'pcu_ids' in fbdata['plcnode']:
178 if len(fbdata['plcnode']['pcu_ids']) > 0:
182 def pcu_select(str_query, nodelist=None):
185 if str_query is None: return (nodenames, pcunames)
188 dict_query = query_to_dict(str_query)
191 for node in fb['nodes'].keys():
192 if nodelist is not None:
193 if node not in nodelist: continue
195 fb_nodeinfo = fb['nodes'][node]['values']
196 if pcu_in(fb_nodeinfo):
197 pcuinfo = fbpcu['nodes']['id_%s' % fb_nodeinfo['plcnode']['pcu_ids'][0]]['values']
198 if verify(dict_query, pcuinfo):
199 nodenames.append(node)
200 str = "cmdhttps/locfg.pl -s %s -f iloxml/License.xml -u %s -p '%s' | grep MESSAGE" % \
201 (pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
203 return (nodenames, pcunames)
205 def node_select(str_query, nodelist=None, fbdb=None):
209 if str_query is None: return hostnames
212 dict_query = query_to_dict(str_query)
218 for node in fb['nodes'].keys():
219 if nodelist is not None:
220 if node not in nodelist: continue
222 fb_nodeinfo = fb['nodes'][node]['values']
224 if fb_nodeinfo == []:
225 #print node, "has lost values"
228 fb_nodeinfo['pcu'] = color_pcu_state(fb_nodeinfo)
229 fb_nodeinfo['hostname'] = node
230 if 'plcnode' in fb_nodeinfo:
231 fb_nodeinfo.update(fb_nodeinfo['plcnode'])
233 if verify(dict_query, fb_nodeinfo):
234 #print node #fb_nodeinfo
235 hostnames.append(node)
237 #print "NO MATCH", node
247 import parser as parsermodule
248 parser = parsermodule.getParser()
250 parser.set_defaults(node=None, fromtime=None, select=None, list=None,
251 pcuselect=None, nodelist=None, daysdown=None, fields=None)
252 parser.add_option("", "--daysdown", dest="daysdown", action="store_true",
253 help="List the node state and days down...")
254 parser.add_option("", "--select", dest="select", metavar="key=value",
255 help="List all nodes with the given key=value pattern")
256 parser.add_option("", "--fields", dest="fields", metavar="key,list,...",
257 help="a list of keys to display for each entry.")
258 parser.add_option("", "--list", dest="list", action="store_true",
259 help="Write only the hostnames as output.")
260 parser.add_option("", "--pcuselect", dest="pcuselect", metavar="key=value",
261 help="List all nodes with the given key=value pattern")
262 parser.add_option("", "--nodelist", dest="nodelist", metavar="nodelist.txt",
263 help="A list of nodes to bring out of debug mode.")
264 parser.add_option("", "--fromtime", dest="fromtime", metavar="YYYY-MM-DD",
265 help="Specify a starting date from which to begin the query.")
267 parser = parsermodule.getParser(['defaults'], parser)
268 config = parsermodule.parse_args(parser)
272 archive = database.SPickle(path)
273 d = datetime_fromstr(config.fromtime)
274 glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
277 file = glob.glob(glob_str)[0]
278 #print "loading %s" % file
280 fb = archive.load(file[:-4])
282 fb = database.dbLoad("findbad")
284 fbpcu = database.dbLoad("findbadpcus")
288 nodelist = util.file.getListFromFile(config.nodelist)
290 nodelist = fb['nodes'].keys()
293 if config.select is not None and config.pcuselect is not None:
294 nodelist = node_select(config.select, nodelist, fb)
295 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
296 elif config.select is not None:
297 nodelist = node_select(config.select, nodelist, fb)
298 elif config.pcuselect is not None:
299 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
305 for node in nodelist:
308 if node not in fb['nodes']:
311 fb_nodeinfo = fb['nodes'][node]['values']
317 daysdown_print_nodeinfo(fb_nodeinfo, node)
321 fields = config.fields.split(",")
325 fb_print_nodeinfo(fb_nodeinfo, node, fields)
326 elif not config.select and 'state' in fb_nodeinfo:
327 fb_print_nodeinfo(fb_nodeinfo, node)
331 if __name__ == "__main__":