clearer names for actions, and infer actions better
[monitor.git] / statistics / nodequeryold.py
1 #!/usr/bin/python
2
3 import sys
4 from nodecommon import *
5 import glob
6 import os
7 from monitor.util import file
8
9 import time
10 import re
11
12 #fb = {}
13 fb = None
14 fbpcu = None
15
16 class NoKeyException(Exception): pass
17
18 def fb_print_nodeinfo(fbnode, hostname, fields=None):
19         fbnode['hostname'] = hostname
20         fbnode['checked'] = diff_time(fbnode['checked'])
21         if fbnode['bootcd']:
22                 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
23         else:
24                 fbnode['bootcd'] = "unknown"
25         fbnode['pcu'] = color_pcu_state(fbnode)
26
27         if not fields:
28                 if 'ERROR' in fbnode['category']:
29                         fbnode['kernel'] = ""
30                 else:
31                         fbnode['kernel'] = fbnode['kernel'].split()[2]
32                 fbnode['boot_state'] = fbnode['plcnode']['boot_state']
33
34                 try:
35                         if len(fbnode['nodegroups']) > 0:
36                                 fbnode['category'] = fbnode['nodegroups'][0]
37                 except:
38                         #print "ERROR!!!!!!!!!!!!!!!!!!!!!"
39                         pass
40
41                 print "%(hostname)-45s | %(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
42         else:
43                 format = ""
44                 for f in fields:
45                         format += "%%(%s)s " % f
46                 print format % fbnode
47
48 def get(fb, path):
49     indexes = path.split("/")
50     values = fb
51     for index in indexes:
52         if index in values:
53             values = values[index]
54         else:
55             raise NoKeyException(index)
56     return values
57
58 def verifyType(constraints, data):
59         """
60                 constraints is a list of key, value pairs.
61                 # [ {... : ...}==AND , ... , ... , ] == OR
62         """
63         con_or_true = False
64         for con in constraints:
65                 #print "con: %s" % con
66                 if len(con.keys()) == 0:
67                         con_and_true = False
68                 else:
69                         con_and_true = True
70
71                 for key in con.keys():
72                         #print "looking at key: %s" % key
73                         if data is None:
74                                 con_and_true = False
75                                 break
76
77                         try:
78                                 get(data,key)
79                                 o = con[key]
80                                 if o.name() == "Match":
81                                         if get(data,key) is not None:
82                                                 value_re = re.compile(o.value)
83                                                 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
84                                         else:
85                                                 con_and_true = False
86                                 elif o.name() == "ListMatch":
87                                         if get(data,key) is not None:
88                                                 match = False
89                                                 for listitem in get(data,key):
90                                                         value_re = re.compile(o.value)
91                                                         if value_re.search(listitem) is not None:
92                                                                 match = True
93                                                                 break
94                                                 con_and_true = con_and_true & match
95                                         else:
96                                                 con_and_true = False
97                                 elif o.name() == "Is":
98                                         con_and_true = con_and_true & (get(data,key) == o.value)
99                                 elif o.name() == "FilledIn":
100                                         con_and_true = con_and_true & (len(get(data,key)) > 0)
101                                 elif o.name() == "PortOpen":
102                                         if get(data,key) is not None:
103                                                 v = get(data,key)
104                                                 con_and_true = con_and_true & (v[str(o.value)] == "open")
105                                         else:
106                                                 con_and_true = False
107                                 else:
108                                         value_re = re.compile(o.value)
109                                         con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
110
111                         except NoKeyException, key:
112                                 print "missing key %s" % key,
113                                 pass
114                                 #print "missing key %s" % key
115                                 #con_and_true = False
116
117                 con_or_true = con_or_true | con_and_true
118
119         return con_or_true
120
121 def verify(constraints, data):
122         """
123                 constraints is a list of key, value pairs.
124                 # [ {... : ...}==AND , ... , ... , ] == OR
125         """
126         con_or_true = False
127         for con in constraints:
128                 #print "con: %s" % con
129                 if len(con.keys()) == 0:
130                         con_and_true = False
131                 else:
132                         con_and_true = True
133
134                 for key in con.keys():
135                         #print "looking at key: %s" % key
136                         if key in data: 
137                                 value_re = re.compile(con[key])
138                                 if type([]) == type(data[key]):
139                                         local_or_true = False
140                                         for val in data[key]:
141                                                 local_or_true = local_or_true | (value_re.search(val) is not None)
142                                         con_and_true = con_and_true & local_or_true
143                                 else:
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 node_select(str_query, nodelist=None, fbdb=None):
181         global fb
182
183         hostnames = []
184         if str_query is None: return hostnames
185
186         #print str_query
187         dict_query = query_to_dict(str_query)
188         #print dict_query
189
190         if fbdb is not None:
191                 fb = fbdb
192
193         for node in fb['nodes'].keys():
194                 if nodelist is not None: 
195                         if node not in nodelist: continue
196         
197                 fb_nodeinfo  = fb['nodes'][node]['values']
198
199                 if fb_nodeinfo == []:
200                         #print node, "has lost values"
201                         continue
202                         #sys.exit(1)
203                 #fb_nodeinfo['pcu'] = color_pcu_state(fb_nodeinfo)
204                 fb_nodeinfo['hostname'] = node
205                 if 'plcnode' in fb_nodeinfo:
206                         fb_nodeinfo.update(fb_nodeinfo['plcnode'])
207
208                 if verify(dict_query, fb_nodeinfo):
209                         #print node #fb_nodeinfo
210                         hostnames.append(node)
211                 else:
212                         #print "NO MATCH", node
213                         pass
214         
215         return hostnames
216