4 from nodecommon import *
7 from monitor.util import file
16 class NoKeyException(Exception): pass
18 def fb_print_nodeinfo(fbnode, hostname, fields=None):
19 fbnode['hostname'] = hostname
20 fbnode['checked'] = diff_time(fbnode['checked'])
22 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
24 fbnode['bootcd'] = "unknown"
25 fbnode['pcu'] = color_pcu_state(fbnode)
28 if 'ERROR' in fbnode['category']:
31 fbnode['kernel'] = fbnode['kernel'].split()[2]
32 fbnode['boot_state'] = fbnode['plcnode']['boot_state']
35 if len(fbnode['nodegroups']) > 0:
36 fbnode['category'] = fbnode['nodegroups'][0]
38 #print "ERROR!!!!!!!!!!!!!!!!!!!!!"
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
45 format += "%%(%s)s " % f
49 indexes = path.split("/")
53 values = values[index]
55 raise NoKeyException(index)
58 def verifyType(constraints, data):
60 constraints is a list of key, value pairs.
61 # [ {... : ...}==AND , ... , ... , ] == OR
64 for con in constraints:
65 #print "con: %s" % con
66 if len(con.keys()) == 0:
71 for key in con.keys():
72 #print "looking at key: %s" % 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)
86 elif o.name() == "ListMatch":
87 if get(data,key) is not None:
89 for listitem in get(data,key):
90 value_re = re.compile(o.value)
91 if value_re.search(listitem) is not None:
94 con_and_true = con_and_true & match
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:
104 con_and_true = con_and_true & (v[str(o.value)] == "open")
108 value_re = re.compile(o.value)
109 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
111 except NoKeyException, key:
112 print "missing key %s" % key,
114 #print "missing key %s" % key
115 #con_and_true = False
117 con_or_true = con_or_true | con_and_true
121 def verify(constraints, data):
123 constraints is a list of key, value pairs.
124 # [ {... : ...}==AND , ... , ... , ] == OR
127 for con in constraints:
128 #print "con: %s" % con
129 if len(con.keys()) == 0:
134 for key in con.keys():
135 #print "looking at key: %s" % key
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
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,
148 #print "missing key %s" % key
149 #con_and_true = False
151 con_or_true = con_or_true | con_and_true
155 def query_to_dict(query):
159 or_queries = query.split('||')
160 for or_query in or_queries:
161 and_queries = or_query.split('&&')
165 for and_query in and_queries:
166 (key, value) = and_query.split('=')
174 if 'plcnode' in fbdata:
175 if 'pcu_ids' in fbdata['plcnode']:
176 if len(fbdata['plcnode']['pcu_ids']) > 0:
180 def node_select(str_query, nodelist=None, fbdb=None):
184 if str_query is None: return hostnames
187 dict_query = query_to_dict(str_query)
193 for node in fb['nodes'].keys():
194 if nodelist is not None:
195 if node not in nodelist: continue
197 fb_nodeinfo = fb['nodes'][node]['values']
199 if fb_nodeinfo == []:
200 #print node, "has lost values"
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'])
208 if verify(dict_query, fb_nodeinfo):
209 #print node #fb_nodeinfo
210 hostnames.append(node)
212 #print "NO MATCH", node