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"
32 def __init__(self, cdb, allbuckets):
34 self.updated = time.time()
35 self.allbuckets = allbuckets
36 self.comonbkts = {"ssh": "sshstatus%20%3E%202h",
37 "clock_drift": "drift%20%3E%201m",
38 "dns": "dns1udp%20%3E%2080%20&&%20dns2udp%20%3E%2080",
39 "disk": "resptime%20%3E%200%20&&%20gbfree%20%3C%205",
40 "filerw": "filerw%3E0"}
43 def __tohash(self,rawdata):
44 # First line Comon returns is list of keys with respect to index
45 keys = rawdata.readline().rstrip().split(", ")
49 for line in rawdata.readlines():
50 host = line.rstrip().split(", ")
52 for i in range(1,len(keys)):
55 logger.debug("Retrieved %s hosts" % len(hash.keys()))
56 except Exception, err:
57 logger.debug("No hosts retrieved")
61 # Update individual buckekts. Hostnames only.
63 for (bkt,url) in self.comonbkts.items():
64 tmp = self.coget(COMONURL + "&format=formatcsv&select='" + url + "'").keys()
65 setattr(self, bkt, tmp)
67 # Update ALL node information
70 self.updated = time.time()
71 # Make a Hash, put in self.
72 self.codata = self.coget(COMONURL + "&format=formatcsv")
77 logger.debug("Trying - " + url)
78 coserv = urllib2.Request(url)
79 coserv.add_header('User-Agent',
80 'PL_Monitor +http://monitor.planet-lab.org/')
81 opener = urllib2.build_opener()
82 # Initial web get from summer.cs in CSV
83 rawdata = opener.open(coserv)
84 except urllib2.URLError, (err):
85 print "Attempting %s" %COMONURL
86 print "URL error (%s)" % (err)
88 return self.__tohash(rawdata)
90 # Push nodes that are bad (in *a* bucket) into q(allbuckets)
92 for bucket in self.comonbkts.keys():
93 for host in getattr(self,bucket):
94 self.allbuckets.put(host)
107 logger.setLevel(logging.DEBUG)
108 ch = logging.StreamHandler()
109 ch.setLevel(logging.DEBUG)
110 formatter = logging.Formatter('%(message)s')
111 ch.setFormatter(formatter)
112 logger.addHandler(ch)
124 print a.coget(COMONURL + "&format=formatcsv&select='" + a.comonbkts['filerw'])
127 if __name__ == '__main__':
131 except KeyboardInterrupt:
132 print "Killed. Exitting."
133 logger.info('Monitor Killed')