2 # Copyright (c) 2004 The Trustees of Princeton University (Trustees).
4 # Faiyaz Ahmed <faiyaza@cs.princeton.edu>
8 # Get CoMon data, unsorted, in CSV, and create a huge hash.
17 from threading import *
18 #httplib.HTTPConnection.debuglevel = 1
20 logger = logging.getLogger("monitor")
22 # Time between comon refresh
26 COMONURL = "http://summer.cs.princeton.edu/status/tabulator.cgi?table=table_nodeview"
31 cdb is the comon database (dictionary)
32 all buckets is a queue of all problem nodes. This gets sent to rt to find
33 tickets open for host.
35 def __init__(self, cdb, allbuckets):
37 self.updated = time.time()
38 self.allbuckets = allbuckets
39 self.comonbkts = {"down" : "resptime%20==%200%20&&%20keyok==null",
40 "ssh": "sshstatus%20%3E%202h",
41 "clock_drift": "drift%20%3E%201m",
42 "dns": "dns1udp%20%3E%2080%20&&%20dns2udp%20%3E%2080",
43 "filerw": "filerw%3E0",
47 def __tohash(self,rawdata):
48 # First line Comon returns is list of keys with respect to index
49 keys = rawdata.readline().rstrip().split(", ")
53 for line in rawdata.readlines():
54 host = line.rstrip().split(", ")
56 for i in range(1,len(keys)):
59 logger.debug("Retrieved %s hosts" % len(hash.keys()))
60 except Exception, err:
61 logger.debug("No hosts retrieved")
65 # Update individual buckekts. Hostnames only.
67 for (bkt,url) in self.comonbkts.items():
68 logger.debug("COMON: Updating bucket %s" % bkt)
69 tmp = self.coget(COMONURL + "&format=formatcsv&select='" + url + "'").keys()
70 setattr(self, bkt, tmp)
72 # Update ALL node information
75 self.updated = time.time()
76 # Make a Hash, put in self.
77 self.codata.update(self.coget(COMONURL + "&format=formatcsv"))
82 coserv = urllib2.Request(url)
83 coserv.add_header('User-Agent',
84 'PL_Monitor +http://monitor.planet-lab.org/')
85 opener = urllib2.build_opener()
86 # Initial web get from summer.cs in CSV
87 rawdata = opener.open(coserv)
88 except urllib2.URLError, (err):
89 print "Attempting %s" %COMONURL
90 print "URL error (%s)" % (err)
92 return self.__tohash(rawdata)
94 # Push nodes that are bad (in *a* bucket) into q(allbuckets)
96 for bucket in self.comonbkts.keys():
97 for host in getattr(self,bucket):
98 self.allbuckets.put(host)
111 logger.setLevel(logging.DEBUG)
112 ch = logging.StreamHandler()
113 ch.setLevel(logging.DEBUG)
114 formatter = logging.Formatter('%(message)s')
115 ch.setFormatter(formatter)
116 logger.addHandler(ch)
130 for host in cdb.keys():
131 if cdb[host]['keyok'] == "0":
132 print("%s \t Bootstate %s nodetype %s kernver %s keyok %s" %(host, cdb[host]['bootstate'], cdb[host]['nodetype'], cdb[host]['kernver'], cdb[host]['keyok']))
136 #print a.coget(COMONURL + "&format=formatcsv&select='" + a.comonbkts['filerw'])
139 if __name__ == '__main__':
143 except KeyboardInterrupt:
144 print "Killed. Exitting."
145 logger.info('Monitor Killed')