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