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 = soltesz.dbLoad("findbad")
22 def daysdown_print_nodeinfo(fbnode, hostname):
23 fbnode['hostname'] = hostname
24 fbnode['daysdown'] = Diagnose.getStrDaysDown(fbnode)
25 fbnode['intdaysdown'] = Diagnose.getDaysDown(fbnode)
27 print "%(intdaysdown)5s %(hostname)-44s | %(state)10.10s | %(daysdown)s" % fbnode
29 def fb_print_nodeinfo(fbnode, hostname, fields=None):
30 fbnode['hostname'] = hostname
31 fbnode['checked'] = diff_time(fbnode['checked'])
33 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
35 fbnode['bootcd'] = "unknown"
36 fbnode['pcu'] = color_pcu_state(fbnode)
39 if 'ERROR' in fbnode['category']:
42 fbnode['kernel'] = fbnode['kernel'].split()[2]
43 fbnode['boot_state'] = fbnode['plcnode']['boot_state']
45 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
49 format += "%%(%s)s " % f
52 def verify(constraints, data):
54 constraints is a list of key, value pairs.
55 # [ {... : ...}==AND , ... , ... , ] == OR
58 for con in constraints:
59 #print "con: %s" % con
60 if len(con.keys()) == 0:
65 for key in con.keys():
66 #print "looking at key: %s" % key
68 value_re = re.compile(con[key])
69 con_and_true = con_and_true & (value_re.search(data[key]) is not None)
71 print "missing key %s" % key,
73 #print "missing key %s" % key
76 con_or_true = con_or_true | con_and_true
80 def query_to_dict(query):
84 or_queries = query.split('||')
85 for or_query in or_queries:
86 and_queries = or_query.split('&&')
90 for and_query in and_queries:
91 (key, value) = and_query.split('=')
99 if 'plcnode' in fbdata:
100 if 'pcu_ids' in fbdata['plcnode']:
101 if len(fbdata['plcnode']['pcu_ids']) > 0:
105 def pcu_select(str_query, nodelist=None):
108 if str_query is None: return (nodenames, pcunames)
111 dict_query = query_to_dict(str_query)
114 for node in fb['nodes'].keys():
115 if nodelist is not None:
116 if node not in nodelist: continue
118 fb_nodeinfo = fb['nodes'][node]['values']
119 if _pcu_in(fb_nodeinfo):
120 pcuinfo = fbpcu['nodes']['id_%s' % fb_nodeinfo['plcnode']['pcu_ids'][0]]['values']
121 if verify(dict_query, pcuinfo):
122 nodenames.append(node)
123 str = "cmdhttps/locfg.pl -s %s -f iloxml/License.xml -u %s -p '%s' | grep MESSAGE" % \
124 (pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
126 return (nodenames, pcunames)
128 def node_select(str_query, nodelist=None):
130 if str_query is None: return hostnames
133 dict_query = query_to_dict(str_query)
137 for node in fb['nodes'].keys():
138 if nodelist is not None:
139 if node not in nodelist: continue
141 fb_nodeinfo = fb['nodes'][node]['values']
143 if fb_nodeinfo == []:
144 #print node, "has lost values"
147 fb_nodeinfo['pcu'] = color_pcu_state(fb_nodeinfo)
148 fb_nodeinfo['hostname'] = node
149 if 'plcnode' in fb_nodeinfo:
150 fb_nodeinfo.update(fb_nodeinfo['plcnode'])
152 if verify(dict_query, fb_nodeinfo):
153 #print node #fb_nodeinfo
154 hostnames.append(node)
156 #print "NO MATCH", node
166 from config import config
167 from optparse import OptionParser
168 parser = OptionParser()
169 parser.set_defaults(node=None, fromtime=None, select=None, list=None, pcuselect=None, nodelist=None, daysdown=None, fields=None)
170 parser.add_option("", "--daysdown", dest="daysdown", action="store_true",
171 help="List the node state and days down...")
172 parser.add_option("", "--select", dest="select", metavar="key=value",
173 help="List all nodes with the given key=value pattern")
174 parser.add_option("", "--fields", dest="fields", metavar="key,list,...",
175 help="a list of keys to display for each entry.")
176 parser.add_option("", "--list", dest="list", action="store_true",
177 help="Write only the hostnames as output.")
178 parser.add_option("", "--pcuselect", dest="pcuselect", metavar="key=value",
179 help="List all nodes with the given key=value pattern")
180 parser.add_option("", "--nodelist", dest="nodelist", metavar="nodelist.txt",
181 help="A list of nodes to bring out of debug mode.")
182 parser.add_option("", "--fromtime", dest="fromtime", metavar="YYYY-MM-DD",
183 help="Specify a starting date from which to begin the query.")
184 config = config(parser)
189 archive = soltesz.SPickle(path)
190 d = datetime_fromstr(config.fromtime)
191 glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
194 file = glob.glob(glob_str)[0]
195 #print "loading %s" % file
197 fb = archive.load(file[:-4])
199 fb = soltesz.dbLoad("findbad")
201 fbpcu = soltesz.dbLoad("findbadpcus")
204 nodelist = config.getListFromFile(config.nodelist)
206 nodelist = fb['nodes'].keys()
209 if config.select is not None and config.pcuselect is not None:
210 nodelist = node_select(config.select, nodelist)
211 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
212 elif config.select is not None:
213 nodelist = node_select(config.select, nodelist)
214 elif config.pcuselect is not None:
215 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
222 for node in nodelist:
225 if node not in fb['nodes']:
228 fb_nodeinfo = fb['nodes'][node]['values']
234 daysdown_print_nodeinfo(fb_nodeinfo, node)
238 fields = config.fields.split(",")
242 fb_print_nodeinfo(fb_nodeinfo, node, fields)
243 elif not config.select and 'state' in fb_nodeinfo:
244 fb_print_nodeinfo(fb_nodeinfo, node)
248 if __name__ == "__main__":