7 from datetime import datetime,timedelta
12 from pcucontrol import reboot
14 from pcucontrol.util import command
15 from monitor import config
17 from monitor.database.info.model import *
19 from monitor.sources import comon
20 from monitor.wrapper import plc, plccache
23 from monitor.common import nmap_port_status
25 COMON_COTOPURL= "http://summer.cs.princeton.edu/status/tabulator.cgi?" + \
26 "table=table_nodeview&" + \
27 "dumpcols='name,resptime,sshstatus,uptime,lastcotop,cpuspeed,memsize,disksize'&" + \
30 api = plc.getAuthAPI()
31 plc_lock = threading.Lock()
40 #print "GetPCU from PLC %s" % pcuname
41 l_pcu = plc.GetPCUs({'pcu_id' : pcuname})
47 #print "GetPCU from file %s" % pcuname
48 l_pcus = plccache.l_pcus
50 if i['pcu_id'] == pcuname:
59 def get_nodes(node_ids):
63 l_node = plc.getNodes(node_ids, ['hostname', 'last_contact', 'node_id', 'ports'])
66 plc_nodes = plccache.l_plcnodes
68 if n['node_id'] in node_ids:
80 def get_plc_pcu_values(pcuname):
82 Try to contact PLC to get the PCU info.
83 If that fails, try a backup copy from the last run.
84 If that fails, return None
88 l_pcu = get_pcu(pcuname)
91 site_id = l_pcu['site_id']
92 node_ids = l_pcu['node_ids']
93 l_node = get_nodes(node_ids)
95 if l_node is not None:
97 values[node['hostname']] = node['ports'][0]
99 values['nodenames'] = [node['hostname'] for node in l_node]
101 # NOTE: this is for a dry run later. It doesn't matter which node.
102 values['node_id'] = l_node[0]['node_id']
110 class ScanInterface(object):
113 primarykey = 'hostname'
115 def __init__(self, round):
119 def __getattr__(self, name):
120 if 'collect' in name or 'record' in name:
121 method = getattr(self, name, None)
123 raise Exception("No such method %s" % name)
126 raise Exception("No such method %s" % name)
128 def collect(self, nodename, data):
131 def record(self, request, (nodename, values) ):
137 fbnodesync = self.syncclass.findby_or_create(
138 if_new_set={'round' : self.round},
139 **{ self.primarykey : nodename})
140 # NOTE: This code will either add a new record for the new self.round,
141 # OR it will find the previous value, and update it with new information.
142 # The data that is 'lost' is not that important, b/c older
143 # history still exists.
144 fbrec = self.recordclass.findby_or_create(
145 **{'round':self.round, self.primarykey:nodename})
147 fbrec.set( **values )
150 fbnodesync.round = self.round
153 print "%d %s %s" % (self.count, nodename, values)
158 print traceback.print_exc()
161 class ScanNodeInternal(ScanInterface):
162 recordclass = FindbadNodeRecord
163 syncclass = FindbadNodeRecordSync
164 primarykey = 'hostname'
166 def collectNMAP(self, nodename, cohash):
167 #### RUN NMAP ###############################
170 print "nmap -oG - -P0 -p22,80,806 %s | grep Host:" % nodename
171 (oval,eval) = nmap.run_noexcept("nmap -oG - -P0 -p22,80,806 %s | grep Host:" % nodename)
172 # NOTE: an empty / error value for oval, will still work.
173 (values['port_status'], continue_probe) = nmap_port_status(oval)
175 values['date_checked'] = datetime.now()
177 return (nodename, values)
179 def collectInternal(self, nodename, cohash):
180 ### RUN PING ######################
182 (oval,errval) = ping.run_noexcept("ping -c 1 -q %s | grep rtt" % nodename)
189 values['ping_status'] = False
191 values['ping_status'] = True
194 for port in [22, 806]:
195 ssh = command.SSH('root', nodename, port)
197 (oval, errval) = ssh.run_noexcept2(""" <<\EOF
199 echo ' "kernel_version":"'`uname -a`'",'
200 echo ' "bmlog":"'`ls /tmp/bm.log`'",'
201 echo ' "bootcd_version":"'`cat /mnt/cdrom/bootme/ID`'",'
202 echo ' "nm_status":"'`ps ax | grep nm.py | grep -v grep`'",'
203 echo ' "fs_status":"'`touch /var/log/monitor 2>&1`'",'
204 echo ' "dns_status":"'`host boot.planet-lab.org 2>&1`'",'
205 echo ' "princeton_comon_dir":"'`ls -d /vservers/princeton_comon`'",'
207 ID=`grep princeton_comon /etc/passwd | awk -F : '{if ( $3 > 500 ) { print $3}}'`
208 echo ' "princeton_comon_running":"'`ls -d /proc/virtual/$ID`'",'
209 echo ' "princeton_comon_procs":"'`vps ax | grep $ID | grep -v grep | wc -l`'",'
213 values['ssh_error'] = errval
215 #print "OVAL: %s" % oval
216 values.update(eval(oval))
217 values['ssh_portused'] = port
220 values.update({'kernel_version': "", 'bmlog' : "", 'bootcd_version' : '',
224 'princeton_comon_dir' : "",
225 'princeton_comon_running' : "",
226 'princeton_comon_procs' : "", 'ssh_portused' : None})
228 print traceback.print_exc()
231 ### RUN SSH ######################
232 b_getbootcd_id = True
234 oval = values['kernel_version']
235 if "2.6.17" in oval or "2.6.2" in oval:
236 values['ssh_status'] = True
237 values['observed_category'] = 'PROD'
238 if "bm.log" in values['bmlog']:
239 values['observed_status'] = 'DEBUG'
241 values['observed_status'] = 'BOOT'
242 elif "2.6.12" in oval or "2.6.10" in oval:
243 values['ssh_status'] = True
244 values['observed_category'] = 'OLDPROD'
245 if "bm.log" in values['bmlog']:
246 values['observed_status'] = 'DEBUG'
248 values['observed_status'] = 'BOOT'
250 # NOTE: on 2.6.8 kernels, with 4.2 bootstrapfs, the chroot
251 # command fails. I have no idea why.
252 elif "2.4" in oval or "2.6.8" in oval:
253 b_getbootcd_id = False
254 values['ssh_status'] = True
255 values['observed_category'] = 'OLDBOOTCD'
256 values['observed_status'] = 'DEBUG'
258 values['ssh_status'] = True
259 values['observed_category'] = 'UNKNOWN'
260 if "bm.log" in values['bmlog']:
261 values['observed_status'] = 'DEBUG'
263 values['observed_status'] = 'BOOT'
266 b_getbootcd_id = False
267 values['ssh_status'] = False
268 values['observed_category'] = 'ERROR'
269 values['observed_status'] = 'DOWN'
271 values['ssh_error'] = val
272 values['kernel_version'] = ""
275 # try to get BootCD for all nodes that are not 2.4 nor inaccessible
276 oval = values['bootcd_version']
278 values['bootcd_version'] = oval
279 if "v2" in oval and \
280 ( nodename is not "planetlab1.cs.unc.edu" and \
281 nodename is not "planetlab2.cs.unc.edu" ):
282 values['observed_category'] = 'OLDBOOTCD'
284 values['bootcd_version'] = ""
286 values['bootcd_version'] = ""
288 oval = values['nm_status']
290 values['nm_status'] = "Y"
292 values['nm_status'] = "N"
294 continue_slice_check = True
295 oval = values['princeton_comon_dir']
296 if "princeton_comon_dir" in oval:
297 values['princeton_comon_dir'] = True
299 values['princeton_comon_dir'] = False
300 continue_slice_check = False
302 if continue_slice_check:
303 oval = values['princeton_comon_running']
304 if len(oval) > len('/proc/virtual/'):
305 values['princeton_comon_running'] = True
307 values['princeton_comon_running'] = False
308 continue_slice_check = False
310 values['princeton_comon_running'] = False
312 if continue_slice_check:
313 oval = values['princeton_comon_procs']
314 values['princeton_comon_procs'] = int(oval)
316 values['princeton_comon_procs'] = None
319 if nodename in cohash:
320 values['comon_stats'] = cohash[nodename]
322 values['comon_stats'] = {'resptime': '-1',
329 # include output value
330 ### GET PLC NODE ######################
334 d_node = plc.getNodes({'hostname': nodename}, ['pcu_ids', 'site_id',
335 'date_created', 'last_updated',
336 'last_contact', 'boot_state', 'nodegroup_ids'])[0]
338 traceback.print_exc()
340 values['plc_node_stats'] = d_node
342 ##### NMAP ###################
343 (n, v) = self.collectNMAP(nodename, None)
346 ### GET PLC PCU ######################
350 pcu = d_node['pcu_ids']
354 site_id = d_node['site_id']
356 values['plc_pcuid'] = d_pcu
358 ### GET PLC SITE ######################
361 values['loginbase'] = ""
363 d_site = plc.getSites({'site_id': site_id},
364 ['max_slices', 'slice_ids', 'node_ids', 'login_base'])[0]
365 values['loginbase'] = d_site['login_base']
367 traceback.print_exc()
370 values['plc_site_stats'] = d_site
371 values['date_checked'] = datetime.now()
373 print traceback.print_exc()
375 return (nodename, values)
377 def internalprobe(hostname):
378 fbsync = FindbadNodeRecordSync.findby_or_create(hostname="global",
379 if_new_set={'round' : 1})
380 scannode = ScanNodeInternal(fbsync.round)
382 (nodename, values) = scannode.collectInternal(hostname, {})
383 scannode.record(None, (nodename, values))
387 print traceback.print_exc()
390 def externalprobe(hostname):
391 fbsync = FindbadNodeRecordSync.findby_or_create(hostname="global",
392 if_new_set={'round' : 1})
393 scannode = ScanNodeInternal(fbsync.round)
395 (nodename, values) = scannode.collectNMAP(hostname, {})
396 scannode.record(None, (nodename, values))
400 print traceback.print_exc()
403 class ScanPCU(ScanInterface):
404 recordclass = FindbadPCURecord
405 syncclass = FindbadPCURecordSync
406 primarykey = 'plc_pcuid'
408 def collectInternal(self, pcuname, cohash):
410 continue_probe = True
412 values = {'reboot_trial_status' : 'novalue'}
413 ### GET PCU ######################
417 v = get_plc_pcu_values(pcuname)
418 if v['hostname'] is not None: v['hostname'] = v['hostname'].strip()
419 if v['ip'] is not None: v['ip'] = v['ip'].strip()
422 values['plc_pcu_stats'] = v
424 continue_probe = False
427 traceback.print_exc()
428 continue_probe = False
430 if b_except or not continue_probe: return (None, None, None)
432 #### RUN NMAP ###############################
435 print "nmap -oG - -P0 -p22,23,80,443,5869,9100,16992 %s | grep Host:" % reboot.pcu_name(values['plc_pcu_stats'])
436 (oval,eval) = nmap.run_noexcept("nmap -oG - -P0 -p22,23,80,443,5869,9100,16992 %s | grep Host:" % reboot.pcu_name(values['plc_pcu_stats']))
437 # NOTE: an empty / error value for oval, will still work.
438 (values['port_status'], continue_probe) = nmap_port_status(oval)
440 values['port_status'] = None
442 #### COMPLETE ENTRY #######################
444 values['entry_complete'] = []
445 #if values['protocol'] is None or values['protocol'] is "":
446 # values['entry_complete'] += ["protocol"]
447 if values['plc_pcu_stats']['model'] is None or values['plc_pcu_stats']['model'] is "":
448 values['entry_complete'] += ["model"]
449 # Cannot continue due to this condition
450 continue_probe = False
452 if values['plc_pcu_stats']['password'] is None or values['plc_pcu_stats']['password'] is "":
453 values['entry_complete'] += ["password"]
454 # Cannot continue due to this condition
455 continue_probe = False
457 if len(values['entry_complete']) > 0:
458 continue_probe = False
460 if values['plc_pcu_stats']['hostname'] is None or values['plc_pcu_stats']['hostname'] is "":
461 values['entry_complete'] += ["hostname"]
462 if values['plc_pcu_stats']['ip'] is None or values['plc_pcu_stats']['ip'] is "":
463 values['entry_complete'] += ["ip"]
465 # If there are no nodes associated with this PCU, then we cannot continue.
466 if len(values['plc_pcu_stats']['node_ids']) == 0:
467 continue_probe = False
468 values['entry_complete'] += ['nodeids']
471 #### DNS and IP MATCH #######################
472 if values['plc_pcu_stats']['hostname'] is not None and values['plc_pcu_stats']['hostname'] is not "" and \
473 values['plc_pcu_stats']['ip'] is not None and values['plc_pcu_stats']['ip'] is not "":
475 ipaddr = socket.gethostbyname(values['plc_pcu_stats']['hostname'])
476 if ipaddr == values['plc_pcu_stats']['ip']:
477 values['dns_status'] = "DNS-OK"
479 values['dns_status'] = "DNS-MISMATCH"
480 continue_probe = False
482 except Exception, err:
483 values['dns_status'] = "DNS-NOENTRY"
484 values['plc_pcu_stats']['hostname'] = values['plc_pcu_stats']['ip']
486 if values['plc_pcu_stats']['ip'] is not None and values['plc_pcu_stats']['ip'] is not "":
487 values['dns_status'] = "NOHOSTNAME"
488 values['plc_pcu_stats']['hostname'] = values['plc_pcu_stats']['ip']
490 values['dns_status'] = "NO-DNS-OR-IP"
491 values['plc_pcu_stats']['hostname'] = "No_entry_in_DB"
492 continue_probe = False
495 ###### DRY RUN ############################
496 if continue_probe and 'node_ids' in values['plc_pcu_stats'] and \
497 len(values['plc_pcu_stats']['node_ids']) > 0:
498 rb_ret = reboot.reboot_test_new(values['plc_pcu_stats']['nodenames'][0],
501 rb_ret = "Not_Run" # No nodes to test"
503 values['reboot_trial_status'] = rb_ret
506 print "____________________________________"
509 print "____________________________________"
510 errors['traceback'] = traceback.format_exc()
511 print errors['traceback']
512 values['reboot_trial_status'] = str(errors['traceback'])
515 values['entry_complete']=" ".join(values['entry_complete'])
517 values['date_checked'] = datetime.now()
518 return (pcuname, values)