11 COMON_COTOPURL= "http://summer.cs.princeton.edu/status/tabulator.cgi?" + \
12 "table=table_nodeview&" + \
13 "dumpcols='name,resptime,sshstatus,uptime,lastcotop,cpuspeed,memsize,disksize'&" + \
16 #"select='lastcotop!=0'"
19 plc_lock = threading.Lock()
21 externalState = {'round': round, 'nodes': {}}
30 from nodequery import verify,query_to_dict,node_select
33 api = plc.getAuthAPI()
35 def collectPingAndSSH(nodename, cohash):
36 ### RUN PING ######################
37 ping = moncommands.CMD()
38 (oval,errval) = ping.run_noexcept("ping -c 1 -q %s | grep rtt" % nodename)
44 values['ping'] = "NOPING"
46 values['ping'] = "PING"
49 for port in [22, 806]:
50 ssh = moncommands.SSH('root', nodename, port)
52 (oval, errval) = ssh.run_noexcept2(""" <<\EOF
54 echo ' "kernel":"'`uname -a`'",'
55 echo ' "bmlog":"'`ls /tmp/bm.log`'",'
56 echo ' "bootcd":"'`cat /mnt/cdrom/bootme/ID`'",'
57 echo ' "nm":"'`ps ax | grep nm.py | grep -v grep`'",'
58 echo ' "princeton_comon":"'`ls -d /vservers/princeton_comon`'",'
60 ID=`grep princeton_comon /etc/passwd | awk -F : '{if ( $3 > 500 ) { print $3}}'`
62 echo ' "princeton_comon_running":"'`ls -d /proc/virtual/$ID`'",'
63 echo ' "princeton_comon_procs":"'`vps ax | grep $ID | grep -v grep | wc -l`'",'
68 values.update(eval(oval))
69 values['sshport'] = port
72 values.update({'kernel': "", 'bmlog' : "", 'bootcd' : '', 'nm' :
73 '', 'princeton_comon' : '', 'princeton_comon_running' : '',
74 'princeton_comon_procs' : '', 'sshport' : None})
76 print traceback.print_exc()
79 ### RUN SSH ######################
81 #ssh = moncommands.SSH('root', nodename)
84 #(oval, errval) = ssh.run_noexcept('echo `uname -a ; ls /tmp/bm.log`')
86 oval = values['kernel']
87 if "2.6.17" in oval or "2.6.2" in oval:
89 values['category'] = 'ALPHA'
90 if "bm.log" in values['bmlog']:
91 values['state'] = 'DEBUG'
93 values['state'] = 'BOOT'
94 elif "2.6.12" in oval or "2.6.10" in oval:
96 values['category'] = 'PROD'
97 if "bm.log" in values['bmlog']:
98 values['state'] = 'DEBUG'
100 values['state'] = 'BOOT'
102 # NOTE: on 2.6.8 kernels, with 4.2 bootstrapfs, the chroot command fails. I have no idea why.
103 elif "2.4" in oval or "2.6.8" in oval:
104 b_getbootcd_id = False
105 values['ssh'] = 'SSH'
106 values['category'] = 'OLDBOOTCD'
107 values['state'] = 'DEBUG'
109 values['ssh'] = 'SSH'
110 values['category'] = 'UNKNOWN'
111 if "bm.log" in values['bmlog']:
112 values['state'] = 'DEBUG'
114 values['state'] = 'BOOT'
117 b_getbootcd_id = False
118 values['ssh'] = 'NOSSH'
119 values['category'] = 'ERROR'
120 values['state'] = 'DOWN'
122 values['kernel'] = val
124 #values['kernel'] = val
127 # try to get BootCD for all nodes that are not 2.4 nor inaccessible
128 #(oval, errval) = ssh.run_noexcept('cat /mnt/cdrom/bootme/ID')
129 oval = values['bootcd']
131 values['bootcd'] = oval
132 if "v2" in oval and \
133 ( nodename is not "planetlab1.cs.unc.edu" and \
134 nodename is not "planetlab2.cs.unc.edu" ):
135 values['category'] = 'OLDBOOTCD'
137 values['bootcd'] = ""
139 values['bootcd'] = ""
141 # TODO: get bm.log for debug nodes.
144 #(oval, errval) = ssh.run_noexcept('ps ax | grep nm.py | grep -v grep')
151 continue_slice_check = True
152 #(oval, errval) = ssh.run_noexcept('ls -d /vservers/princeton_comon')
153 oval = values['princeton_comon']
154 if "princeton_comon" in oval:
155 values['princeton_comon'] = "Y"
157 values['princeton_comon'] = "N"
158 continue_slice_check = False
160 if continue_slice_check:
161 #(oval, errval) = ssh.run_noexcept('ID=`grep princeton_comon /etc/passwd | awk -F : "{if ( \\\$3 > 500 ) { print \\\$3}}"`; ls -d /proc/virtual/$ID')
162 oval = values['princeton_comon_running']
163 if len(oval) > len('/proc/virtual/'):
164 values['princeton_comon_running'] = "Y"
166 values['princeton_comon_running'] = "N"
167 continue_slice_check = False
169 values['princeton_comon_running'] = "-"
171 if continue_slice_check:
172 #(oval, errval) = ssh.run_noexcept('ID=`grep princeton_comon /etc/passwd | awk -F : "{if ( \\\$3 > 500 ) { print \\\$3}}"`; vps ax | grep $ID | grep -v grep | wc -l')
173 oval = values['princeton_comon_procs']
174 values['princeton_comon_procs'] = oval
176 values['princeton_comon_procs'] = "-"
179 if nodename in cohash:
180 values['comonstats'] = cohash[nodename]
182 values['comonstats'] = {'resptime': '-1',
189 # include output value
190 ### GET PLC NODE ######################
195 d_node = plc.getNodes({'hostname': nodename}, ['pcu_ids', 'site_id', 'date_created', 'last_updated', 'last_contact', 'boot_state', 'nodegroup_ids'])
198 traceback.print_exc()
201 if b_except: return (None, None)
204 if d_node and len(d_node) > 0:
205 pcu = d_node[0]['pcu_ids']
207 values['pcu'] = "PCU"
209 values['pcu'] = "NOPCU"
210 site_id = d_node[0]['site_id']
211 last_contact = d_node[0]['last_contact']
212 nodegroups = [ i['name'] for i in api.GetNodeGroups(d_node[0]['nodegroup_ids']) ]
213 values['plcnode'] = {'status' : 'SUCCESS',
215 'boot_state' : d_node[0]['boot_state'],
217 'nodegroups' : nodegroups,
218 'last_contact': last_contact,
219 'date_created': d_node[0]['date_created'],
220 'last_updated': d_node[0]['last_updated']}
222 values['pcu'] = "UNKNOWN"
223 values['plcnode'] = {'status' : "GN_FAILED"}
226 ### GET PLC SITE ######################
231 d_site = plc.getSites({'site_id': site_id},
232 ['max_slices', 'slice_ids', 'node_ids', 'login_base'])
235 traceback.print_exc()
238 if b_except: return (None, None)
240 if d_site and len(d_site) > 0:
241 max_slices = d_site[0]['max_slices']
242 num_slices = len(d_site[0]['slice_ids'])
243 num_nodes = len(d_site[0]['node_ids'])
244 loginbase = d_site[0]['login_base']
245 values['plcsite'] = {'num_nodes' : num_nodes,
246 'max_slices' : max_slices,
247 'num_slices' : num_slices,
248 'login_base' : loginbase,
249 'status' : 'SUCCESS'}
251 values['plcsite'] = {'status' : "GS_FAILED"}
253 values['checked'] = time.time()
255 return (nodename, values)
257 def recordPingAndSSH(request, result):
260 (nodename, values) = result
262 if values is not None:
263 global_round = externalState['round']
264 externalState['nodes'][nodename]['values'] = values
265 externalState['nodes'][nodename]['round'] = global_round
268 print "%d %s %s" % (count, nodename, externalState['nodes'][nodename]['values'])
270 database.dbDump(config.dbname, externalState)
272 # this will be called when an exception occurs within a thread
273 def handle_exception(request, result):
274 print "Exception occured in request %s" % request.requestID
276 print "Result: %s" % i
279 def checkAndRecordState(l_nodes, cohash):
282 global_round = externalState['round']
284 tp = threadpool.ThreadPool(20)
286 # CREATE all the work requests
287 for nodename in l_nodes:
288 if nodename not in externalState['nodes']:
289 externalState['nodes'][nodename] = {'round': 0, 'values': []}
291 node_round = externalState['nodes'][nodename]['round']
292 if node_round < global_round:
293 # recreate node stats when refreshed
294 #print "%s" % nodename
295 req = threadpool.WorkRequest(collectPingAndSSH, [nodename, cohash], {},
296 None, recordPingAndSSH, handle_exception)
299 # We just skip it, since it's "up to date"
301 print "%d %s %s" % (count, nodename, externalState['nodes'][nodename]['values'])
304 # WAIT while all the work requests are processed.
310 # if more than two hours
311 if time.time() - begin > (60*60*1.5):
312 print "findbad.py has run out of time!!!!!!"
313 database.dbDump(config.dbname, externalState)
315 except KeyboardInterrupt:
318 except threadpool.NoResultsPending:
319 print "All results collected."
322 database.dbDump(config.dbname, externalState)
329 externalState = database.if_cached_else(1, config.dbname, lambda : externalState)
332 # update global round number to force refreshes across all nodes
333 externalState['round'] += 1
335 cotop = comon.Comon()
336 # lastcotop measures whether cotop is actually running. this is a better
337 # metric than sshstatus, or other values from CoMon
338 cotop_url = COMON_COTOPURL
340 # history information for all nodes
342 cohash = cotop.coget(cotop_url)
343 l_nodes = syncplcdb.create_plcdb()
345 f_nodes = config.getListFromFile(config.nodelist)
346 l_nodes = filter(lambda x: x['hostname'] in f_nodes, l_nodes)
348 f_nodes = [config.node]
349 l_nodes = filter(lambda x: x['hostname'] in f_nodes, l_nodes)
350 elif config.nodegroup:
351 ng = api.GetNodeGroups({'name' : config.nodegroup})
352 l_nodes = api.GetNodes(ng[0]['node_ids'])
354 site = api.GetSites(config.site)
355 l_nodes = api.GetNodes(site[0]['node_ids'], ['hostname'])
357 l_nodes = [node['hostname'] for node in l_nodes]
359 # perform this query after the above options, so that the filter above
361 if config.nodeselect:
362 l_nodes = node_select(config.nodeselect)
364 print "fetching %s hosts" % len(l_nodes)
366 checkAndRecordState(l_nodes, cohash)
371 if __name__ == '__main__':
372 import parser as parsermodule
374 parser = parsermodule.getParser(['nodesets'])
376 parser.set_defaults( increment=False, dbname="findbadnodes", cachenodes=False)
377 parser.add_option("", "--cachenodes", action="store_true",
378 help="Cache node lookup from PLC")
379 parser.add_option("", "--dbname", dest="dbname", metavar="FILE",
380 help="Specify the name of the database to which the information is saved")
381 parser.add_option("-i", "--increment", action="store_true", dest="increment",
382 help="Increment round number to force refresh or retry")
384 parser = parsermodule.getParser(['defaults'], parser)
386 cfg = parsermodule.parse_args(parser)
390 except Exception, err:
391 print traceback.print_exc()
392 print "Exception: %s" % err
393 print "Saving data... exitting."
394 database.dbDump(config.dbname, externalState)