send ack with all packets

display host that support ack in upper case
This commit is contained in:
andreas
2012-03-29 00:04:34 +00:00
parent dd03128ed3
commit 4c3470c44d
2 changed files with 54 additions and 29 deletions
+23 -19
View File
@@ -1,5 +1,5 @@
#!/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)
#
import time, os, string, sys, socket, atexit, select, SocketServer, getopt, signal, cPickle
@@ -65,15 +65,21 @@ class Host:
self.lastbeat=time.time()
self.upcount=0
self.state=Host.up
self.uppercent="n/a"
self.state="up"
self.statetime=self.lastbeat
self.interval=0
self.doesack=False
num+=1
def getstate(self):
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
def newstate(self, state, when=0):
self.state=state
@@ -194,7 +200,6 @@ def displaytime():
if hosts[h].state == Host.overdue:
attr=curses.A_BOLD
win.addstr(hosts[h].num+1, 25, "%8s" % d, attr)
win.addstr(hosts[h].num+1, 53, "%3s" % hosts[h].uppercent )
win.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
if not hosts.has_key(name):
addhost(name, addr)
host=hosts[name]
print "xx", acks
host.doesack=acks
if host.addr != addr:
log("%s changed address to %s" % (host.name, addr))
if htab.has_key(host.addr):
@@ -291,6 +298,7 @@ def fromaddr(name, addr, boot, interval):
def readsock():
global htab, win
data, addr = sock.recvfrom(1024)
sock.sendto("ACK", addr)
pairs=string.split(data,';')
boot=0
shutdown=0
@@ -299,6 +307,7 @@ def readsock():
msg=None
interval=0
deltaT=0.0
acks=False
for pair in pairs:
l=string.split(pair,"=")
key=l[0]
@@ -323,11 +332,14 @@ def readsock():
deltaT=now-float(val)
except:
pass
elif key == 'acks':
acks=True
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:
log("%s msg: %s" % (name, msg),service=service)
fromaddr(name, addr[0], boot, interval)
log("%s msg: %s" % (name, msg), service=service)
fromaddr(name, addr[0], boot, interval, acks)
if shutdown:
log("%s shutdown" % name)
try:
@@ -341,15 +353,6 @@ def readsock():
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.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].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("<h4>Log of Events</h4>")
for m in msgs[len(msgs)-30:]:
@@ -571,6 +574,9 @@ atexit.register(on_exit)
ilist=[]
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))
ilist.append(sock)
@@ -633,8 +639,6 @@ while up:
serv.handle_request()
if now >= next:
next=now+1
if int(now) % interval == startsec:
updatestats()
checkoverdue()
if visual:
stdscr.move(1 , 0)