-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvagus_webserver.py
executable file
·292 lines (256 loc) · 9.26 KB
/
vagus_webserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python
import BaseHTTPServer
from BaseHTTPServer import HTTPServer
from SocketServer import ThreadingMixIn
import argparse
import logging
import logging.config
import socket
import urlparse
import cgi
import time
def do_command(cmd):
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.connect(("localhost",8720))
s.send(cmd+"\n")
result = ""
while True:
r = s.recv(1000000)
if len(r)==0: #socket closed, but we don't expect that
return None
result += r
if result=="\n" or (len(result)>=2 and result[-2:]=="\n\n"):
break
return result
except socket.error, ex:
logger.debug("Got exception when trying to talk to vagus: %s",ex)
pass
finally:
s.close()
return None
def get_cluster_list():
r = do_command("getclusters")
if not r or len(r)==0:
return None
if r=="\n":
return []
return r.split('\n')[0:-2]
def get_instance_list(cluster_id):
r = do_command("poll %s"%cluster_id)
if not r or len(r)==0:
return None
if r=="\n":
return []
#do we want to split/parse into instance-id + extra_information? Nah...
return r.split('\n')[0:-2]
def get_instance_listx(cluster_id):
r = do_command("pollx %s"%cluster_id)
if not r or len(r)==0:
return None
if r=="\n":
return []
#do we want to split/parse into instance-id + extra_information? Nah...
l = []
for line in r.split('\n')[0:-2]:
instance_id = line.split(':')[0]
vagus_id = line.split(':')[1]
lifetime = float(line.split(':')[2])
if len(line.split(':'))>3:
extra_info = line.partition(':')[2].partition(':')[2].partition(':')[2]
else:
extra_info = ""
l.append( (vagus_id,instance_id,lifetime,extra_info) )
return l
def get_vagus_list():
r = do_command("getvaguslist")
if not r or len(r)==0:
return None
if r=="\n":
return []
#parse into id+lastseen+timeout
l = []
for line in r.split("\n"):
if len(line.split(":"))>=4:
(vagus_id,last_seen,end_of_life) = line.split(":")[0:3]
address = line.partition(':')[2].partition(':')[2].partition(':')[2]
last_seen = int(last_seen)
end_of_life = int(end_of_life)
l.append((vagus_id,last_seen,end_of_life,address))
return l
class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
def log_message(self,format,*args):
logger.info("%s" % (format%args))
def do_GET(self):
parsed_url = urlparse.urlparse(self.path)
parameters = cgi.parse_qs(parsed_url.query)
#print "parameters=",parameters
if parsed_url.path.find("..")!=-1:
self.send_response(404)
self.end_headers()
return
if parsed_url.path=="/":
return self.serve_root()
# /cluster/$cluster_id$
s = parsed_url.path.split("/")
if len(s)==3 and s[1]=="cluster":
return self.serve_cluster(s[2])
if len(s)==3 and s[1]=="cluster_details":
return self.serve_cluster_details(s[2])
self.send_response(404)
self.end_headers()
def serve_vagus_talk_error(self,msg):
self.send_response(500)
self.send_header("Content-type", "text/html; charset=utf-8")
self.end_headers()
print >>self.wfile, '<html>'
print >>self.wfile, '<body>'
print >>self.wfile, '<p>%s</p>'%msg
print >>self.wfile, '</body>'
print >>self.wfile, '</html>'
def serve_root(self):
cluster_list = get_cluster_list()
if cluster_list==None:
return self.serve_vagus_talk_error("Could not get cluster list from vagus")
vagus_list = get_vagus_list()
if vagus_list==None:
return self.serve_vagus_talk_error("Could not get vagus list from vagus")
#sort vagus instance list by name
vagus_list.sort()
self.send_response(200)
self.send_header("Content-type", "text/html; charset=utf-8")
self.send_header("cache-control","max-age=0")
self.end_headers()
print >>self.wfile, '<html>'
print >>self.wfile, '<head>'
print >>self.wfile, '<title>Vagus: Overview</title>'
print >>self.wfile, '<meta name="viewport" content="width=device-width, initial-scale=1.0"/>'
#print >>self.wfile, '<link rel="stylesheet" type="text/css" href="default.css" title="Default"/>'
print >>self.wfile, '</head>'
print >>self.wfile, '<body>'
print >>self.wfile, '<h1>Known clusters (%d)</h1>'%(len(cluster_list))
print >>self.wfile, '<ul>'
for cluster in cluster_list:
print >>self.wfile, '<li><a href="/cluster/%s">%s</a> (<a href="/cluster_details/%s">details</a>)'%(cluster,cluster,cluster)
print >>self.wfile, '</ul>'
print >>self.wfile, '<h1>Known Vagus processes (%d)</h1>'%(len(vagus_list))
print >>self.wfile, '<table>'
print >>self.wfile, ' <tr><th>Identity</th><th>Last seen</th><th>end-of-life</th><th>Most recent address</th></tr>'
for vagus in vagus_list:
print >>self.wfile, ' <tr>'
print >>self.wfile, ' <td>%s</td>'%vagus[0]
last_seen_str = time.strftime("%Y-%m-%dT%H:%M:%SZ",time.gmtime(vagus[1]/1000))
print >>self.wfile, ' <td>%s</td>'%last_seen_str
end_of_life_str = time.strftime("%Y-%m-%dT%H:%M:%SZ",time.gmtime(vagus[2]/1000))
print >>self.wfile, ' <td>%s</td>'%end_of_life_str
print >>self.wfile, ' <td>%s</td>'%vagus[3]
print >>self.wfile, ' </tr>'
print >>self.wfile, '</table>'
print >>self.wfile, '</body>'
print >>self.wfile, '</html>'
def serve_cluster(self,cluster_id):
instance_list = get_instance_list(cluster_id)
if instance_list==None:
return self.serve_vagus_talk_error("Could not get instance list from vagus")
#we want the instances to be sorted but we have to check if they should be numeriacally or alphamerically sorted
all_numeric = True
for i in instance_list:
if not i.split(':')[0].isdigit():
all_numeric = False
break
if all_numeric:
def cmp_numeric(a,b):
return cmp(int(a.split(':')[0]),int(b.split(':')[0]))
instance_list.sort(cmp=cmp_numeric)
else:
instance_list.sort()
self.send_response(200)
self.send_header("Content-type", "text/html; charset=utf-8")
self.send_header("cache-control","max-age=0")
self.end_headers()
print >>self.wfile, '<html>'
print >>self.wfile, '<head>'
print >>self.wfile, '<title>Vagus: instances in %s</title>'%(cluster_id)
print >>self.wfile, '<meta name="viewport" content="width=device-width, initial-scale=1.0"/>'
#print >>self.wfile, '<link rel="stylesheet" type="text/css" href="default.css" title="Default"/>'
print >>self.wfile, '</head>'
print >>self.wfile, '<body>'
print >>self.wfile, '<h1>Alive instances (%d)</h1>'%(len(instance_list))
print >>self.wfile, '<ul>'
for instance in instance_list:
print >>self.wfile, '<li>%s</li>'%(instance)
print >>self.wfile, '</ul>'
print >>self.wfile, '</body>'
print >>self.wfile, '</html>'
def serve_cluster_details(self,cluster_id):
instance_list = get_instance_listx(cluster_id)
if instance_list==None:
return self.serve_vagus_talk_error("Could not get instance list from vagus")
#we want the instances to be sorted but we have to check if they should be numeriacally or alphamerically sorted
all_numeric = True
for i in instance_list:
if not i[1][0].isdigit():
all_numeric = False
break
if all_numeric:
def cmp_numeric(a,b):
x = cmp(a[0],b[0])
if x!=0:
return x
else:
return cmp(int(a[1]),int(b[1]))
instance_list.sort(cmp=cmp_numeric)
else:
def cmp_alfameric(a,b):
x = cmp(a[0],b[0])
if x!=0:
return x
else:
return cmp(a[1],b[1])
instance_list.sort(cmp=cmp_alfameric)
#count the number of instances in each vagus-instance
instance_count = {}
for instance in instance_list:
if instance[0] not in instance_count:
instance_count[instance[0]] = 0
instance_count[instance[0]] += 1
self.send_response(200)
self.send_header("Content-type", "text/html; charset=utf-8")
self.send_header("cache-control","max-age=0")
self.end_headers()
print >>self.wfile, '<html>'
print >>self.wfile, '<head>'
print >>self.wfile, '<title>Vagus: instances in %s</title>'%(cluster_id)
print >>self.wfile, '<meta name="viewport" content="width=device-width, initial-scale=1.0"/>'
#print >>self.wfile, '<link rel="stylesheet" type="text/css" href="default.css" title="Default"/>'
print >>self.wfile, '</head>'
print >>self.wfile, '<body>'
print >>self.wfile, '<h1>Alive instances (%d)</h1>'%(len(instance_list))
print >>self.wfile, '<ul>'
previous_vagus_instance_id = None
for instance in instance_list:
if instance[0]!=previous_vagus_instance_id:
if previous_vagus_instance_id!=None:
print >>self.wfile, ' </ul></li>'
print >>self.wfile, '<li>%s (%d instances)'%(instance[0], instance_count[instance[0]])
print >>self.wfile, ' <ul>'
previous_vagus_instance_id = instance[0]
print >>self.wfile, ' <li>%s:%s:%s:%s</li>'%(instance[0],instance[1],instance[2],instance[3])
if previous_vagus_instance_id!=None:
print >>self.wfile, ' </ul></li>'
print >>self.wfile, '</ul>'
print >>self.wfile, '</body>'
print >>self.wfile, '</html>'
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
pass
parser = argparse.ArgumentParser(description="Vagus web interface")
parser.add_argument("--loggingconf",type=str,default="logging.dev.conf")
parser.add_argument("--port",type=int,default=8724)
args=parser.parse_args()
logging.config.fileConfig(args.loggingconf)
logger = logging.getLogger(__name__)
logger.info("vagus_webserver starting")
httpd = ThreadedHTTPServer(("", args.port), Handler)
logger.info("vagus_webserver ready")
httpd.serve_forever()