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