www/printbadnodes.py
[monitor.git] / nodequery.py
1 #!/usr/bin/python
2
3 import plc
4 api = plc.getAuthAPI()
5
6 import sys
7 import database
8 from nodecommon import *
9 #from policy import Diagnose
10 from unified_model import Record
11 import glob
12 import os
13 from reboot import pcu_name
14 import reboot
15 import util.file
16
17 import time
18 import re
19
20 #fb = {}
21 fb = {}
22 fbpcu = {}
23
24 class NoKeyException(Exception): pass
25
26 def daysdown_print_nodeinfo(fbnode, hostname):
27         fbnode['hostname'] = hostname
28         fbnode['daysdown'] = Record.getStrDaysDown(fbnode)
29         fbnode['intdaysdown'] = Record.getDaysDown(fbnode)
30
31         print "%(intdaysdown)5s %(hostname)-44s | %(state)10.10s | %(daysdown)s" % fbnode
32
33 def fb_print_nodeinfo(fbnode, hostname, fields=None):
34         fbnode['hostname'] = hostname
35         fbnode['checked'] = diff_time(fbnode['checked'])
36         if fbnode['bootcd']:
37                 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
38         else:
39                 fbnode['bootcd'] = "unknown"
40         fbnode['pcu'] = color_pcu_state(fbnode)
41
42         if not fields:
43                 if 'ERROR' in fbnode['category']:
44                         fbnode['kernel'] = ""
45                 else:
46                         fbnode['kernel'] = fbnode['kernel'].split()[2]
47                 fbnode['boot_state'] = fbnode['plcnode']['boot_state']
48
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
50         else:
51                 format = ""
52                 for f in fields:
53                         format += "%%(%s)s " % f
54                 print format % fbnode
55
56 def get(fb, path):
57     indexes = path.split("/")
58     values = fb
59     for index in indexes:
60         if index in values:
61             values = values[index]
62         else:
63             raise NoKeyException(index)
64     return values
65
66 def verifyType(constraints, data):
67         """
68                 constraints is a list of key, value pairs.
69                 # [ {... : ...}==AND , ... , ... , ] == OR
70         """
71         con_or_true = False
72         for con in constraints:
73                 #print "con: %s" % con
74                 if len(con.keys()) == 0:
75                         con_and_true = False
76                 else:
77                         con_and_true = True
78
79                 for key in con.keys():
80                         #print "looking at key: %s" % key
81                         if data is None:
82                                 con_and_true = False
83                                 break
84
85                         try:
86                                 get(data,key)
87                                 o = con[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)
92                                         else:
93                                                 con_and_true = False
94                                 elif o.name() == "ListMatch":
95                                         if get(data,key) is not None:
96                                                 match = False
97                                                 for listitem in get(data,key):
98                                                         value_re = re.compile(o.value)
99                                                         if value_re.search(listitem) is not None:
100                                                                 match = True
101                                                                 break
102                                                 con_and_true = con_and_true & match
103                                         else:
104                                                 con_and_true = False
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:
111                                                 v = get(data,key)
112                                                 con_and_true = con_and_true & (v[str(o.value)] == "open")
113                                         else:
114                                                 con_and_true = False
115                                 else:
116                                         value_re = re.compile(o.value)
117                                         con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
118
119                         except NoKeyException, key:
120                                 print "missing key %s" % key,
121                                 pass
122                                 #print "missing key %s" % key
123                                 #con_and_true = False
124
125                 con_or_true = con_or_true | con_and_true
126
127         return con_or_true
128
129 def verify(constraints, data):
130         """
131                 constraints is a list of key, value pairs.
132                 # [ {... : ...}==AND , ... , ... , ] == OR
133         """
134         con_or_true = False
135         for con in constraints:
136                 #print "con: %s" % con
137                 if len(con.keys()) == 0:
138                         con_and_true = False
139                 else:
140                         con_and_true = True
141
142                 for key in con.keys():
143                         #print "looking at key: %s" % key
144                         if key in data: 
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,
149                                 pass
150                                 #print "missing key %s" % key
151                                 #con_and_true = False
152
153                 con_or_true = con_or_true | con_and_true
154
155         return con_or_true
156
157 def query_to_dict(query):
158         
159         ad = []
160
161         or_queries = query.split('||')
162         for or_query in or_queries:
163                 and_queries = or_query.split('&&')
164
165                 d = {}
166
167                 for and_query in and_queries:
168                         (key, value) = and_query.split('=')
169                         d[key] = value
170
171                 ad.append(d)
172         
173         return ad
174
175 def pcu_in(fbdata):
176         if 'plcnode' in fbdata:
177                 if 'pcu_ids' in fbdata['plcnode']:
178                         if len(fbdata['plcnode']['pcu_ids']) > 0:
179                                 return True
180         return False
181
182 def pcu_select(str_query, nodelist=None):
183         pcunames = []
184         nodenames = []
185         if str_query is None: return (nodenames, pcunames)
186
187         #print str_query
188         dict_query = query_to_dict(str_query)
189         #print dict_query
190
191         for node in fb['nodes'].keys():
192                 if nodelist is not None: 
193                         if node not in nodelist: continue
194         
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'])
202                                 pcunames.append(str)
203         return (nodenames, pcunames)
204
205 def node_select(str_query, nodelist=None, fbdb=None):
206         global fb
207
208         hostnames = []
209         if str_query is None: return hostnames
210
211         #print str_query
212         dict_query = query_to_dict(str_query)
213         #print dict_query
214
215         if fbdb is not None:
216                 fb = fbdb
217
218         for node in fb['nodes'].keys():
219                 if nodelist is not None: 
220                         if node not in nodelist: continue
221         
222                 fb_nodeinfo  = fb['nodes'][node]['values']
223
224                 if fb_nodeinfo == []:
225                         #print node, "has lost values"
226                         continue
227                         #sys.exit(1)
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'])
232
233                 if verify(dict_query, fb_nodeinfo):
234                         #print node #fb_nodeinfo
235                         hostnames.append(node)
236                 else:
237                         #print "NO MATCH", node
238                         pass
239         
240         return hostnames
241
242
243 def main():
244         global fb
245         global fbpcu
246
247         import parser as parsermodule
248         parser = parsermodule.getParser()
249
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.")
266
267         parser = parsermodule.getParser(['defaults'], parser)
268         config = parsermodule.parse_args(parser)
269         
270         if config.fromtime:
271                 path = "archive-pdb"
272                 archive = database.SPickle(path)
273                 d = datetime_fromstr(config.fromtime)
274                 glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
275                 os.chdir(path)
276                 #print glob_str
277                 file = glob.glob(glob_str)[0]
278                 #print "loading %s" % file
279                 os.chdir("..")
280                 fb = archive.load(file[:-4])
281         else:
282                 fb = database.dbLoad("findbad")
283
284         fbpcu = database.dbLoad("findbadpcus")
285         reboot.fb = fbpcu
286
287         if config.nodelist:
288                 nodelist = util.file.getListFromFile(config.nodelist)
289         else:
290                 nodelist = fb['nodes'].keys()
291
292         pculist = None
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)
300
301         if pculist:
302                 for pcu in pculist:
303                         print pcu
304
305         for node in nodelist:
306                 config.node = node
307
308                 if node not in fb['nodes']:
309                         continue
310
311                 fb_nodeinfo  = fb['nodes'][node]['values']
312
313                 if config.list:
314                         print node
315                 else:
316                         if config.daysdown:
317                                 daysdown_print_nodeinfo(fb_nodeinfo, node)
318                         else:
319                                 if config.select:
320                                         if config.fields:
321                                                 fields = config.fields.split(",")
322                                         else:
323                                                 fields = None
324
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)
328                                 else:
329                                         pass
330                 
331 if __name__ == "__main__":
332         main()