b0303440e7f7b1a0c6e7723d3f21d38974f5ead0
[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 util.file
15
16 import time
17 import re
18
19 #fb = {}
20 fb = database.dbLoad("findbad")
21 fbpcu = {}
22
23 class NoKeyException(Exception): pass
24
25 def daysdown_print_nodeinfo(fbnode, hostname):
26         fbnode['hostname'] = hostname
27         fbnode['daysdown'] = Record.getStrDaysDown(fbnode)
28         fbnode['intdaysdown'] = Record.getDaysDown(fbnode)
29
30         print "%(intdaysdown)5s %(hostname)-44s | %(state)10.10s | %(daysdown)s" % fbnode
31
32 def fb_print_nodeinfo(fbnode, hostname, fields=None):
33         fbnode['hostname'] = hostname
34         fbnode['checked'] = diff_time(fbnode['checked'])
35         if fbnode['bootcd']:
36                 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
37         else:
38                 fbnode['bootcd'] = "unknown"
39         fbnode['pcu'] = color_pcu_state(fbnode)
40
41         if not fields:
42                 if 'ERROR' in fbnode['category']:
43                         fbnode['kernel'] = ""
44                 else:
45                         fbnode['kernel'] = fbnode['kernel'].split()[2]
46                 fbnode['boot_state'] = fbnode['plcnode']['boot_state']
47
48                 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         else:
50                 format = ""
51                 for f in fields:
52                         format += "%%(%s)s " % f
53                 print format % fbnode
54
55 def get(fb, path):
56     indexes = path.split("/")
57     values = fb
58     for index in indexes:
59         if index in values:
60             values = values[index]
61         else:
62             raise NoKeyException(index)
63     return values
64
65 def verifyType(constraints, data):
66         """
67                 constraints is a list of key, value pairs.
68                 # [ {... : ...}==AND , ... , ... , ] == OR
69         """
70         con_or_true = False
71         for con in constraints:
72                 #print "con: %s" % con
73                 if len(con.keys()) == 0:
74                         con_and_true = False
75                 else:
76                         con_and_true = True
77
78                 for key in con.keys():
79                         #print "looking at key: %s" % key
80                         if data is None:
81                                 con_and_true = False
82                                 break
83
84                         try:
85                                 get(data,key)
86                                 o = con[key]
87                                 if o.name() == "Match":
88                                         if get(data,key) is not None:
89                                                 value_re = re.compile(o.value)
90                                                 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
91                                         else:
92                                                 con_and_true = False
93                                 elif o.name() == "ListMatch":
94                                         if get(data,key) is not None:
95                                                 match = False
96                                                 for listitem in get(data,key):
97                                                         value_re = re.compile(o.value)
98                                                         if value_re.search(listitem) is not None:
99                                                                 match = True
100                                                                 break
101                                                 con_and_true = con_and_true & match
102                                         else:
103                                                 con_and_true = False
104                                 elif o.name() == "Is":
105                                         con_and_true = con_and_true & (get(data,key) == o.value)
106                                 elif o.name() == "FilledIn":
107                                         con_and_true = con_and_true & (len(get(data,key)) > 0)
108                                 elif o.name() == "PortOpen":
109                                         if get(data,key) is not None:
110                                                 v = get(data,key)
111                                                 con_and_true = con_and_true & (v[str(o.value)] == "open")
112                                         else:
113                                                 con_and_true = False
114                                 else:
115                                         value_re = re.compile(o.value)
116                                         con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
117
118                         except NoKeyException, key:
119                                 print "missing key %s" % key,
120                                 pass
121                                 #print "missing key %s" % key
122                                 #con_and_true = False
123
124                 con_or_true = con_or_true | con_and_true
125
126         return con_or_true
127
128 def verify(constraints, data):
129         """
130                 constraints is a list of key, value pairs.
131                 # [ {... : ...}==AND , ... , ... , ] == OR
132         """
133         con_or_true = False
134         for con in constraints:
135                 #print "con: %s" % con
136                 if len(con.keys()) == 0:
137                         con_and_true = False
138                 else:
139                         con_and_true = True
140
141                 for key in con.keys():
142                         #print "looking at key: %s" % key
143                         if key in data: 
144                                 value_re = re.compile(con[key])
145                                 con_and_true = con_and_true & (value_re.search(data[key]) is not None)
146                         elif key not in data:
147                                 print "missing key %s" % key,
148                                 pass
149                                 #print "missing key %s" % key
150                                 #con_and_true = False
151
152                 con_or_true = con_or_true | con_and_true
153
154         return con_or_true
155
156 def query_to_dict(query):
157         
158         ad = []
159
160         or_queries = query.split('||')
161         for or_query in or_queries:
162                 and_queries = or_query.split('&&')
163
164                 d = {}
165
166                 for and_query in and_queries:
167                         (key, value) = and_query.split('=')
168                         d[key] = value
169
170                 ad.append(d)
171         
172         return ad
173
174 def pcu_in(fbdata):
175         if 'plcnode' in fbdata:
176                 if 'pcu_ids' in fbdata['plcnode']:
177                         if len(fbdata['plcnode']['pcu_ids']) > 0:
178                                 return True
179         return False
180
181 def pcu_select(str_query, nodelist=None):
182         pcunames = []
183         nodenames = []
184         if str_query is None: return (nodenames, pcunames)
185
186         #print str_query
187         dict_query = query_to_dict(str_query)
188         #print dict_query
189
190         for node in fb['nodes'].keys():
191                 if nodelist is not None: 
192                         if node not in nodelist: continue
193         
194                 fb_nodeinfo  = fb['nodes'][node]['values']
195                 if pcu_in(fb_nodeinfo):
196                         pcuinfo = fbpcu['nodes']['id_%s' % fb_nodeinfo['plcnode']['pcu_ids'][0]]['values']
197                         if verify(dict_query, pcuinfo):
198                                 nodenames.append(node)
199                                 str = "cmdhttps/locfg.pl -s %s -f iloxml/License.xml -u %s -p '%s' | grep MESSAGE" % \
200                                                         (pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
201                                 pcunames.append(str)
202         return (nodenames, pcunames)
203
204 def node_select(str_query, nodelist=None, fbdb=None):
205         hostnames = []
206         if str_query is None: return hostnames
207
208         #print str_query
209         dict_query = query_to_dict(str_query)
210         #print dict_query
211         global fb
212
213         if fbdb is not None:
214                 fb = fbdb
215
216         for node in fb['nodes'].keys():
217                 if nodelist is not None: 
218                         if node not in nodelist: continue
219         
220                 fb_nodeinfo  = fb['nodes'][node]['values']
221
222                 if fb_nodeinfo == []:
223                         #print node, "has lost values"
224                         continue
225                         #sys.exit(1)
226                 fb_nodeinfo['pcu'] = color_pcu_state(fb_nodeinfo)
227                 fb_nodeinfo['hostname'] = node
228                 if 'plcnode' in fb_nodeinfo:
229                         fb_nodeinfo.update(fb_nodeinfo['plcnode'])
230
231                 if verify(dict_query, fb_nodeinfo):
232                         #print node #fb_nodeinfo
233                         hostnames.append(node)
234                 else:
235                         #print "NO MATCH", node
236                         pass
237         
238         return hostnames
239
240
241 def main():
242         global fb
243         global fbpcu
244
245         import parser as parsermodule
246         parser = parsermodule.getParser()
247
248         parser.set_defaults(node=None, fromtime=None, select=None, list=None, 
249                                                 pcuselect=None, nodelist=None, daysdown=None, fields=None)
250         parser.add_option("", "--daysdown", dest="daysdown", action="store_true",
251                                                 help="List the node state and days down...")
252         parser.add_option("", "--select", dest="select", metavar="key=value", 
253                                                 help="List all nodes with the given key=value pattern")
254         parser.add_option("", "--fields", dest="fields", metavar="key,list,...", 
255                                                 help="a list of keys to display for each entry.")
256         parser.add_option("", "--list", dest="list", action="store_true", 
257                                                 help="Write only the hostnames as output.")
258         parser.add_option("", "--pcuselect", dest="pcuselect", metavar="key=value", 
259                                                 help="List all nodes with the given key=value pattern")
260         parser.add_option("", "--nodelist", dest="nodelist", metavar="nodelist.txt", 
261                                                 help="A list of nodes to bring out of debug mode.")
262         parser.add_option("", "--fromtime", dest="fromtime", metavar="YYYY-MM-DD",
263                                         help="Specify a starting date from which to begin the query.")
264
265         parser = parsermodule.getParser(['defaults'], parser)
266         config = parsermodule.parse_args(parser)
267         
268         if config.fromtime:
269                 path = "archive-pdb"
270                 archive = database.SPickle(path)
271                 d = datetime_fromstr(config.fromtime)
272                 glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
273                 os.chdir(path)
274                 #print glob_str
275                 file = glob.glob(glob_str)[0]
276                 #print "loading %s" % file
277                 os.chdir("..")
278                 fb = archive.load(file[:-4])
279         else:
280                 fb = database.dbLoad("findbad")
281
282         fbpcu = database.dbLoad("findbadpcus")
283
284         if config.nodelist:
285                 nodelist = util.file.getListFromFile(config.nodelist)
286         else:
287                 nodelist = fb['nodes'].keys()
288
289         pculist = None
290         if config.select is not None and config.pcuselect is not None:
291                 nodelist = node_select(config.select, nodelist)
292                 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
293         elif config.select is not None:
294                 nodelist = node_select(config.select, nodelist)
295         elif config.pcuselect is not None:
296                 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
297
298         if pculist:
299                 for pcu in pculist:
300                         print pcu
301
302         for node in nodelist:
303                 config.node = node
304
305                 if node not in fb['nodes']:
306                         continue
307
308                 fb_nodeinfo  = fb['nodes'][node]['values']
309
310                 if config.list:
311                         print node
312                 else:
313                         if config.daysdown:
314                                 daysdown_print_nodeinfo(fb_nodeinfo, node)
315                         else:
316                                 if config.select:
317                                         if config.fields:
318                                                 fields = config.fields.split(",")
319                                         else:
320                                                 fields = None
321
322                                         fb_print_nodeinfo(fb_nodeinfo, node, fields)
323                                 elif not config.select and 'state' in fb_nodeinfo:
324                                         fb_print_nodeinfo(fb_nodeinfo, node)
325                                 else:
326                                         pass
327                 
328 if __name__ == "__main__":
329         main()