implement /d
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
# $Id: hbd,v 1.38 2013/07/14 02:25:05 andreas Exp $
|
||||
# Wait for heartbeat messages and act on them (or their absence)
|
||||
#
|
||||
VER = 1.36
|
||||
VER = 1.38
|
||||
|
||||
import time, os, string, sys, socket, atexit, select, SocketServer, getopt, signal, cPickle, smtplib, traceback, urllib
|
||||
|
||||
@@ -239,7 +239,7 @@ def initwin():
|
||||
msgw.setscrreg(0, msgwHeight-1)
|
||||
msgw.scrollok(1)
|
||||
|
||||
stdscr.addstr(0, 0, "hbd Version 1.0", curses.A_BOLD)
|
||||
stdscr.addstr(0, 0, "hbd Version %s" % VER, curses.A_BOLD)
|
||||
stdscr.refresh()
|
||||
msgwB.refresh()
|
||||
|
||||
@@ -456,10 +456,11 @@ def readsock():
|
||||
pass
|
||||
|
||||
rmsg="ACK"
|
||||
if len(hosts[name].cmds) then
|
||||
if len(hosts[name].cmds):
|
||||
rmsg=hosts[name].cmds[0]
|
||||
email("%s cmd exec" % name, "command '%s' initiated" % hosts[name].cmds[0])
|
||||
del hosts[name].cmds[0]
|
||||
log("%s command initiated" % name)
|
||||
try:
|
||||
sock.sendto(rmsg, addr)
|
||||
except:
|
||||
@@ -489,11 +490,49 @@ def exitcurses():
|
||||
#
|
||||
#
|
||||
class HtmlHandler(SocketServer.BaseRequestHandler):
|
||||
allow_reuse_address = True
|
||||
|
||||
|
||||
def buildhead(self, title="Heartbeat", refresh=None):
|
||||
res=[]
|
||||
res.append('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">')
|
||||
res.append("<html>")
|
||||
res.append("<head>")
|
||||
res.append('<title>%s</title>' % (title))
|
||||
if refresh:
|
||||
res.append("<meta http-equiv = Refresh content = %d>\n" % refresh)
|
||||
res.append("</head>")
|
||||
res.append('<body BGCOLOR = "#FFFFFF" LINK = "#008000" VLINK = "#008000" BACKGROUND = "/~andreas/images/tile.marble.gif">')
|
||||
return res
|
||||
|
||||
def buildpage(self):
|
||||
res=self.buildhead(refresh=60)
|
||||
res.append("<H2>Heartbeat status</h2><h4> %s (%s)</H4>" % (time.strftime("%H:%M:%S", time.localtime(now)), os.environ.get('TZ', 'CET-1CDT')))
|
||||
res.append("<table>")
|
||||
res.append("<tr><th>Host</th><th>State</th><th>IP Addr</th><th>Last change</th></tr>\n")
|
||||
hosts_sorted = hosts.keys()
|
||||
hosts_sorted.sort()
|
||||
for h in hosts_sorted:
|
||||
res.append("<tr><td>%-24s</td><td>%-7s</td><td>%-16s</td><td>%-17s</td></tr>\n" % (h, hosts[h].dispstate(), hosts[h].addr, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(hosts[h].statetime))))
|
||||
res.append("</table>")
|
||||
res.append("<h4>Log of Events</h4>")
|
||||
for m in msgs[len(msgs)-30:]:
|
||||
res.append("%s<BR>" % m)
|
||||
res.append("</body></html>")
|
||||
return res
|
||||
|
||||
|
||||
def handle(self):
|
||||
f = self.request.makefile()
|
||||
headers=[]
|
||||
headers.append("Date: %s" % time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)))
|
||||
headers.append("Server: hbd")
|
||||
headers.append("Last-Modified: %s" % time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)))
|
||||
headers.append("Accept-Ranges: bytes")
|
||||
headers.append("Connection: close")
|
||||
headers.append("Content-Type: text/html; charset = ISO-8859-1")
|
||||
|
||||
uri = '/unknown'
|
||||
f = self.request.makefile()
|
||||
while 1:
|
||||
line = string.strip(f.readline())
|
||||
if len(line) == 0:
|
||||
@@ -506,14 +545,14 @@ class HtmlHandler(SocketServer.BaseRequestHandler):
|
||||
if len(upar) == 1:
|
||||
uarg=[]
|
||||
else:
|
||||
uargs=string.split(upar[1],"&")
|
||||
uarg=string.split(upar[1],"&")
|
||||
|
||||
code = 404
|
||||
cause = "Not Found"
|
||||
code = 200
|
||||
cause = "OK"
|
||||
if uri == "/":
|
||||
code = 200
|
||||
cause = "OK"
|
||||
elif upar[0] == "/c": # /c?h=melschserver&c=sudo%20ls
|
||||
res=self.buildpage()
|
||||
|
||||
elif upar[0] == "/c": # command on host /c?h=melschserver&c=sudo%20ls
|
||||
uname=""
|
||||
ucmd=""
|
||||
if uarg[0][:2] == "h=":
|
||||
@@ -522,50 +561,36 @@ class HtmlHandler(SocketServer.BaseRequestHandler):
|
||||
ucmd=uarg[1][2:]
|
||||
if ucmd != "" and uname != "" and hosts.has_key(uname):
|
||||
hosts[uname].cmds.append(urllib.unquote(ucmd))
|
||||
code = 200
|
||||
cause = "OK"
|
||||
res=self.buildhead()
|
||||
res.append("2Done")
|
||||
|
||||
elif upar[0] == "/d": # drop host /d?h=melschserver
|
||||
if uarg[0][:2] == "h=":
|
||||
uname=uarg[0][2:]
|
||||
if uname != "" and hosts.has_key(uname):
|
||||
del hosts[uname]
|
||||
log("%s dropped" % uname)
|
||||
res=self.buildhead()
|
||||
res.append("Done")
|
||||
|
||||
|
||||
self.request.send("HTTP/1.0 %s %s\r\n" % (code, cause))
|
||||
self.request.send("Date: %s\r\n" % time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)))
|
||||
self.request.send("Server: hbd\r\n")
|
||||
self.request.send("Last-Modified: %s\r\n" % time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)))
|
||||
self.request.send("Accept-Ranges: bytes\r\n")
|
||||
self.request.send("Connection: close\r\n")
|
||||
self.request.send("Content-Type: text/html; charset = ISO-8859-1\r\n\r\n")
|
||||
res = []
|
||||
if code != 200:
|
||||
else:
|
||||
code = 404
|
||||
cause = "Not Found"
|
||||
res=[]
|
||||
res.append('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">')
|
||||
res.append('<html><head>')
|
||||
res.append('<meta http-equiv = Refresh content = 60>')
|
||||
res.append('<title>%s %s</title>' % (code, cause))
|
||||
res.append('</head><body>')
|
||||
res.append('<h1>%s</h1>' % (cause))
|
||||
res.append('<p>The requested URL %s was not found on this server.</p>' % uri)
|
||||
res.append('<hr>')
|
||||
res.append('<address>hbd (Unix) Server at %s Port %d</address>' % (hbd_host, hbd_port))
|
||||
res.append('<address>hbd (Unix) Server at %s Port %s</address>' % (hbd_host, hbd_port))
|
||||
res.append('</body></html>')
|
||||
|
||||
else:
|
||||
res.append('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">')
|
||||
res.append("<html>")
|
||||
res.append("<head>")
|
||||
res.append("<meta http-equiv = Refresh content = %d>\n" % 60)
|
||||
res.append("</head>")
|
||||
res.append('<body BGCOLOR = "#FFFFFF" LINK = "#008000" VLINK = "#008000" BACKGROUND = "/~andreas/images/tile.marble.gif">')
|
||||
res.append("<H2>Heartbeat status</h2><h4> %s (%s)</H4>" % (time.strftime("%H:%M:%S", time.localtime(now)), os.environ.get('TZ', 'CET-1CDT')))
|
||||
res.append("<table>")
|
||||
res.append("<tr><th>Host</th><th>State</th><th>IP Addr</th><th>Last change</th></tr>\n")
|
||||
hosts_sorted = hosts.keys()
|
||||
hosts_sorted.sort()
|
||||
for h in hosts_sorted:
|
||||
res.append("<tr><td>%-24s</td><td>%-7s</td><td>%-16s</td><td>%-17s</td></tr>\n" % (h, hosts[h].dispstate(), hosts[h].addr, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(hosts[h].statetime))))
|
||||
res.append("</table>")
|
||||
res.append("<h4>Log of Events</h4>")
|
||||
for m in msgs[len(msgs)-30:]:
|
||||
res.append("%s<BR>" % m)
|
||||
res.append("</body></html>")
|
||||
self.request.send("HTTP/1.0 %s %s\r\n" % (code, cause))
|
||||
for h in headers:
|
||||
self.request.send("%s\r\n" % h)
|
||||
self.request.send("\r\n")
|
||||
|
||||
try:
|
||||
self.request.send(string.join(res, "\n"))
|
||||
@@ -576,7 +601,7 @@ class HtmlHandler(SocketServer.BaseRequestHandler):
|
||||
def saveandrestart():
|
||||
sock.close()
|
||||
serv.shutdown()
|
||||
serv.socket.close()
|
||||
serv.server_close()
|
||||
log("restarting")
|
||||
os.execv(sys.argv[0], [sys.argv[0]]+cmdargs)
|
||||
|
||||
@@ -739,9 +764,9 @@ atexit.register(on_exit)
|
||||
ilist = []
|
||||
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
|
||||
sock.bind(("", hb_port))
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
ilist.append(sock)
|
||||
|
||||
serv = SocketServer.TCPServer((hbd_host, hbd_port), HtmlHandler)
|
||||
|
||||
Reference in New Issue
Block a user