5 api = plc.PLC(auth.auth, auth.plc)
9 from nodecommon import *
10 from policy import Diagnose
13 from reboot import pcu_name
19 fb = database.dbLoad("findbad")
22 class NoKeyException(Exception): pass
24 def daysdown_print_nodeinfo(fbnode, hostname):
25 fbnode['hostname'] = hostname
26 fbnode['daysdown'] = Diagnose.getStrDaysDown(fbnode)
27 fbnode['intdaysdown'] = Diagnose.getDaysDown(fbnode)
29 print "%(intdaysdown)5s %(hostname)-44s | %(state)10.10s | %(daysdown)s" % fbnode
31 def fb_print_nodeinfo(fbnode, hostname, fields=None):
32 fbnode['hostname'] = hostname
33 fbnode['checked'] = diff_time(fbnode['checked'])
35 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
37 fbnode['bootcd'] = "unknown"
38 fbnode['pcu'] = color_pcu_state(fbnode)
41 if 'ERROR' in fbnode['category']:
44 fbnode['kernel'] = fbnode['kernel'].split()[2]
45 fbnode['boot_state'] = fbnode['plcnode']['boot_state']
47 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
51 format += "%%(%s)s " % f
55 indexes = path.split("/")
59 values = values[index]
61 raise NoKeyException(index)
64 def verifyType(constraints, data):
66 constraints is a list of key, value pairs.
67 # [ {... : ...}==AND , ... , ... , ] == OR
70 for con in constraints:
71 #print "con: %s" % con
72 if len(con.keys()) == 0:
77 for key in con.keys():
78 #print "looking at key: %s" % key
86 if o.name() == "Match":
87 if get(data,key) is not None:
88 value_re = re.compile(o.value)
89 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
92 elif o.name() == "ListMatch":
93 if get(data,key) is not None:
95 for listitem in get(data,key):
96 value_re = re.compile(o.value)
97 if value_re.search(listitem) is not None:
100 con_and_true = con_and_true & match
103 elif o.name() == "Is":
104 con_and_true = con_and_true & (get(data,key) == o.value)
105 elif o.name() == "FilledIn":
106 con_and_true = con_and_true & (len(get(data,key)) > 0)
107 elif o.name() == "PortOpen":
108 if get(data,key) is not None:
110 con_and_true = con_and_true & (v[str(o.value)] == "open")
114 value_re = re.compile(o.value)
115 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
117 except NoKeyException, key:
118 print "missing key %s" % key,
120 #print "missing key %s" % key
121 #con_and_true = False
123 con_or_true = con_or_true | con_and_true
127 def verify(constraints, data):
129 constraints is a list of key, value pairs.
130 # [ {... : ...}==AND , ... , ... , ] == OR
133 for con in constraints:
134 #print "con: %s" % con
135 if len(con.keys()) == 0:
140 for key in con.keys():
141 #print "looking at key: %s" % key
143 value_re = re.compile(con[key])
144 con_and_true = con_and_true & (value_re.search(data[key]) is not None)
145 elif key not in data:
146 print "missing key %s" % key,
148 #print "missing key %s" % key
149 #con_and_true = False
151 con_or_true = con_or_true | con_and_true
155 def query_to_dict(query):
159 or_queries = query.split('||')
160 for or_query in or_queries:
161 and_queries = or_query.split('&&')
165 for and_query in and_queries:
166 (key, value) = and_query.split('=')
174 if 'plcnode' in fbdata:
175 if 'pcu_ids' in fbdata['plcnode']:
176 if len(fbdata['plcnode']['pcu_ids']) > 0:
180 def pcu_select(str_query, nodelist=None):
183 if str_query is None: return (nodenames, pcunames)
186 dict_query = query_to_dict(str_query)
189 for node in fb['nodes'].keys():
190 if nodelist is not None:
191 if node not in nodelist: continue
193 fb_nodeinfo = fb['nodes'][node]['values']
194 if pcu_in(fb_nodeinfo):
195 pcuinfo = fbpcu['nodes']['id_%s' % fb_nodeinfo['plcnode']['pcu_ids'][0]]['values']
196 if verify(dict_query, pcuinfo):
197 nodenames.append(node)
198 str = "cmdhttps/locfg.pl -s %s -f iloxml/License.xml -u %s -p '%s' | grep MESSAGE" % \
199 (pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
201 return (nodenames, pcunames)
203 def node_select(str_query, nodelist=None, fbdb=None):
205 if str_query is None: return hostnames
208 dict_query = query_to_dict(str_query)
215 for node in fb['nodes'].keys():
216 if nodelist is not None:
217 if node not in nodelist: continue
219 fb_nodeinfo = fb['nodes'][node]['values']
221 if fb_nodeinfo == []:
222 #print node, "has lost values"
225 fb_nodeinfo['pcu'] = color_pcu_state(fb_nodeinfo)
226 fb_nodeinfo['hostname'] = node
227 if 'plcnode' in fb_nodeinfo:
228 fb_nodeinfo.update(fb_nodeinfo['plcnode'])
230 if verify(dict_query, fb_nodeinfo):
231 #print node #fb_nodeinfo
232 hostnames.append(node)
234 #print "NO MATCH", node
244 from config import config
245 from optparse import OptionParser
246 parser = OptionParser()
247 parser.set_defaults(node=None, fromtime=None, select=None, list=None, pcuselect=None, nodelist=None, daysdown=None, fields=None)
248 parser.add_option("", "--daysdown", dest="daysdown", action="store_true",
249 help="List the node state and days down...")
250 parser.add_option("", "--select", dest="select", metavar="key=value",
251 help="List all nodes with the given key=value pattern")
252 parser.add_option("", "--fields", dest="fields", metavar="key,list,...",
253 help="a list of keys to display for each entry.")
254 parser.add_option("", "--list", dest="list", action="store_true",
255 help="Write only the hostnames as output.")
256 parser.add_option("", "--pcuselect", dest="pcuselect", metavar="key=value",
257 help="List all nodes with the given key=value pattern")
258 parser.add_option("", "--nodelist", dest="nodelist", metavar="nodelist.txt",
259 help="A list of nodes to bring out of debug mode.")
260 parser.add_option("", "--fromtime", dest="fromtime", metavar="YYYY-MM-DD",
261 help="Specify a starting date from which to begin the query.")
262 config = config(parser)
267 archive = database.SPickle(path)
268 d = datetime_fromstr(config.fromtime)
269 glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
272 file = glob.glob(glob_str)[0]
273 #print "loading %s" % file
275 fb = archive.load(file[:-4])
277 fb = database.dbLoad("findbad")
279 fbpcu = database.dbLoad("findbadpcus")
282 nodelist = config.getListFromFile(config.nodelist)
284 nodelist = fb['nodes'].keys()
287 if config.select is not None and config.pcuselect is not None:
288 nodelist = node_select(config.select, nodelist)
289 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
290 elif config.select is not None:
291 nodelist = node_select(config.select, nodelist)
292 elif config.pcuselect is not None:
293 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
300 for node in nodelist:
303 if node not in fb['nodes']:
306 fb_nodeinfo = fb['nodes'][node]['values']
312 daysdown_print_nodeinfo(fb_nodeinfo, node)
316 fields = config.fields.split(",")
320 fb_print_nodeinfo(fb_nodeinfo, node, fields)
321 elif not config.select and 'state' in fb_nodeinfo:
322 fb_print_nodeinfo(fb_nodeinfo, node)
326 if __name__ == "__main__":