2 # Copyright (c) 2004 The Trustees of Princeton University (Trustees).
4 # Faiyaz Ahmed <faiyaza@cs.princeton.edu>
6 # $Id: comon.py,v 1.5 2007/05/16 01:53:46 faiyaza Exp $
8 # Get CoMon data, unsorted, in CSV, and create a huge hash.
18 from threading import *
19 #httplib.HTTPConnection.debuglevel = 1
21 logger = logging.getLogger("monitor")
23 # Time between comon refresh
27 COMONURL = "http://summer.cs.princeton.edu/status/tabulator.cgi?table=table_nodeview"
30 # null == <not in DB?>
46 cdb is the comon database (dictionary)
47 all buckets is a queue of all problem nodes. This gets sent to rt to find
48 tickets open for host.
50 def __init__(self, cdb, d_allplc_nodes, q_allbuckets):
52 self.d_allplc_nodes = d_allplc_nodes
53 self.updated = time.time()
54 self.q_allbuckets = q_allbuckets
55 #self.comon_buckets = {"down" : "resptime%20==%200%20&&%20keyok==null",
56 # "ssh": "sshstatus%20%3E%202h",
57 # "clock_drift": "drift%20%3E%201m",
58 # "dns": "dns1udp%20%3E%2080%20&&%20dns2udp%20%3E%2080",
59 # "filerw": "filerw%3E0",
61 self.comon_buckets = {
62 #"down" : "resptime==0 && keyok==null",
63 #"ssh": "sshstatus > 2h",
64 #"clock_drift": "drift > 1m",
65 #"dns": "dns1udp>80 && dns2udp>80",
66 #"filerw": "filerw > 0",
71 def __tohash(self,rawdata):
72 # First line Comon returns is list of keys with respect to index
73 keys = rawdata.readline().rstrip().split(", ")
78 for line in rawdata.readlines():
79 l_host = line.rstrip().split(", ") # split the line on ', '
81 if hostname in self.d_allplc_nodes: # then we'll track it
83 for i in range(1,len(keys)):
84 hash[hostname][keys[i]]=l_host[i]
88 print "Retrieved %s hosts" % len(hash.keys())
89 print "Ignoring %d hosts" % i_ignored
91 logger.debug("Retrieved %s hosts" % len(hash.keys()))
92 logger.debug("Ignoring %d hosts" % i_ignored)
93 except Exception, err:
94 logger.debug("No hosts retrieved")
98 # Update individual buckekts. Hostnames only.
99 def updatebuckets(self):
100 for (bucket,url) in self.comon_buckets.items():
101 logger.debug("COMON: Updating bucket %s" % bucket)
102 tmp = self.coget(COMONURL + "&format=formatcsv&select='" + url + "'").keys()
103 setattr(self, bucket, tmp)
105 # Update ALL node information
108 self.updated = time.time()
109 # Make a Hash, put in self.
110 self.codata.update(self.coget(COMONURL + "&format=formatcsv"))
114 print "Getting: %s" % url
116 coserv = urllib2.Request(url)
117 coserv.add_header('User-Agent',
118 'PL_Monitor +http://monitor.planet-lab.org/')
119 opener = urllib2.build_opener()
120 # Initial web get from summer.cs in CSV
121 rawdata = opener.open(coserv)
122 except urllib2.URLError, (err):
123 print "Attempting %s" %COMONURL
124 print "URL error (%s)" % (err)
126 return self.__tohash(rawdata)
128 # Push nodes that are bad (in *a* bucket) into q(q_allbuckets)
130 #buckets_per_node = []
131 #for bucket in self.comon.comon_buckets.keys():
132 # if (hostname in getattr(self.comon, bucket)):
133 # buckets_per_node.append(bucket)
135 #loginbase = self.plcdb_hn2lb[hostname] # plc.siteId(node)
137 #if not loginbase in self.sickdb:
138 # self.sickdb[loginbase] = [{hostname: buckets_per_node}]
140 # self.sickdb[loginbase].append({hostname: buckets_per_node})
143 print "calling Comon.push()"
144 for bucket in self.comon_buckets.keys():
145 #print "bucket: %s" % bucket
146 for host in getattr(self,bucket):
148 diag_node['nodename'] = host
149 diag_node['message'] = None
150 diag_node['bucket'] = [bucket]
151 diag_node['stage'] = ""
152 diag_node['args'] = None
153 diag_node['info'] = None
154 diag_node['time'] = time.time()
155 #print "host: %s" % host
156 self.q_allbuckets.put(diag_node)
162 # insert signal that this is the final host
163 self.q_allbuckets.put("None")
169 logger.setLevel(logging.DEBUG)
170 ch = logging.StreamHandler()
171 ch.setLevel(logging.DEBUG)
172 formatter = logging.Formatter('%(message)s')
173 ch.setFormatter(formatter)
174 logger.addHandler(ch)
180 #for i in a.comon_buckets: print "%s : %s" % ( i, a.comon_buckets[i])
184 #for i in a.down: print i
188 for host in cdb.keys():
189 #if cdb[host]['keyok'] == "0":
190 # null implies that it may not be in PL DB.
191 if cdb[host]['bootstate'] != "null" and \
192 cdb[host]['bootstate'] == "2" and \
193 cdb[host]['keyok'] == "0":
194 print("%-40s \t Bootstate %s nodetype %s kernver %s keyok %s" % (
195 host, cdb[host]['bootstate'], cdb[host]['nodetype'],
196 cdb[host]['kernver'], cdb[host]['keyok']))
197 #ssh = soltesz.SSH('root', host)
199 # val = ssh.run("uname -r")
200 # print "%s == %s" % (host, val),
204 # print("key mismatch at: %s" % host)
205 #print a.codata['michelangelo.ani.univie.ac.at']
209 #print a.coget(COMONURL + "&format=formatcsv&select='" + a.comon_buckets['filerw'])
212 if __name__ == '__main__':
216 except KeyboardInterrupt:
217 print "Killed. Exitting."
218 logger.info('Monitor Killed')