better message

This commit is contained in:
2013-09-03 17:17:52 -04:00
parent 93c407a9e3
commit cc00f6e82e
+212 -214
View File
@@ -1,214 +1,212 @@
#!/usr/bin/env python #!/usr/bin/env python
# $Id: hbc,v 1.9 2012/03/29 02:08:36 andreas Exp $ # $Id: hbc,v 1.9 2012/03/29 02:08:36 andreas Exp $
import sys, time, socket, os, signal, getopt, string, select import sys, time, socket, os, signal, getopt, string, select
PORT=50003 PORT=50003
INTERVAL=10 INTERVAL=10
False=0 False=0
True=1 True=1
class NullDevice: class NullDevice:
def write(self, s): def write(self, s):
pass pass
def handler(signum, frame): def handler(signum, frame):
global up global up
if up == 0: if up == 0:
return return
sys.exit(0) sys.exit(0)
msgonly=False msgonly=False
helpflag=False helpflag=False
verbose=False verbose=False
daemon=False daemon=False
optlist=[] optlist=[]
args=[] args=[]
msgboot=[] msgboot=[]
home=os.environ['HOME'] home=os.environ['HOME']
configfile="%s/.hbrc" % home configfile="%s/.hbrc" % home
try: try:
optlist, args = getopt.getopt(sys.argv[1:], 'bc:dhm:v') optlist, args = getopt.getopt(sys.argv[1:], 'bc:dhm:v')
except: except:
helpflag=True helpflag=True
for o,a in optlist: for o,a in optlist:
if o == '-b': if o == '-b':
msgboot.append("boot=1") msgboot.append("boot=1")
elif o == '-c': elif o == '-c':
configfile=a configfile=a
elif o == '-d': elif o == '-d':
daemon=True daemon=True
elif o == '-h': elif o == '-h':
helpflag=True helpflag=True
elif o == '-m': elif o == '-m':
msgboot.append("service=%s" % "service") msgboot.append("service=%s" % "service")
a.replace(';',':') a.replace(';',':')
msgboot.append("msg=%s" % a) msgboot.append("msg=%s" % a)
msgonly=True msgonly=True
elif o == '-v': elif o == '-v':
verbose=True verbose=True
if helpflag: if helpflag:
print "hbc HeartBeatClient" print "hbc HeartBeatClient"
print "usage: hbc [-bdhv] [-c configfile] [-m msg][host1 [..]]" print "usage: hbc [-bdhv] [-c configfile] [-m msg][host1 [..]]"
print print
print " -b indicate machine boot" print " -b indicate machine boot"
print " -c configfile" print " -c configfile"
print " -d daemonize" print " -d daemonize"
print " -h this help" print " -h this help"
print " -m send a message" print " -m send a message"
print " -v verbose" print " -v verbose"
print print
print """ config file can contain print """ config file can contain
hb_hosts=('host1', 'host2', ..._ hb_hosts=('host1', 'host2', ..._
hb_port=50003 hb_port=50003
interval=20 interval=20
logfile=... logfile=...
logfmt={|test|msg} logfmt={|test|msg}
grace=SECONDS grace=SECONDS
reportstrict={True|False} reportstrict={True|False}
""" """
sys.exit(1) sys.exit(1)
# #
# set defaults # set defaults
hb_port=PORT hb_port=PORT
interval=INTERVAL interval=INTERVAL
hb_hosts=[] hb_hosts=[]
iam=socket.gethostname() iam=socket.gethostname()
try: try:
f=open(configfile,"r") f=open(configfile,"r")
if verbose: print "notice: using config file %s" % configfile if verbose: print "notice: using config file %s" % configfile
except: except:
if verbose: print "warning: running without config file: %s" % configfile if verbose: print "warning: running without config file: %s" % configfile
f=None f=None
if f: if f:
while 1: while 1:
l=f.readline() l=f.readline()
if len(l) == 0: if len(l) == 0:
break break
r=l[:-1].split('=') r=l[:-1].split('=')
if r[0] == 'hb_hosts': if r[0] == 'hb_hosts':
hb_hosts=eval(r[1]) hb_hosts=eval(r[1])
if verbose: if verbose:
print "notice: cfg hb_hosts: %s" % hb_hosts print "notice: cfg hb_hosts: %s" % hb_hosts
elif r[0] == 'interval': elif r[0] == 'interval':
interval=eval(r[1]) interval=eval(r[1])
elif r[0] == 'hb_port': elif r[0] == 'hb_port':
hb_port=eval(r[1]) hb_port=eval(r[1])
elif r[0] == 'name': elif r[0] == 'name':
iam=eval(r[1]) iam=eval(r[1])
if verbose: print "name set to %s" % iam if verbose: print "name set to %s" % iam
f.close() f.close()
if len(args) != 0: if len(args) != 0:
hb_hosts=args hb_hosts=args
if len(hb_hosts) == 0: if len(hb_hosts) == 0:
print "no hb server specified" print "no hb server specified"
sys.exit(1) sys.exit(1)
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.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, \
sock.getsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR) | 1) 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)
print "notice: hb_port: %s" % hb_port print "notice: hb_port: %s" % hb_port
print "notice: interval: %s" % interval print "notice: interval: %s" % interval
print "notice: iam: %s" % iam print "notice: iam: %s" % iam
if not msgonly: if not msgonly:
msgboot.append("interval=%s" % interval) msgboot.append("interval=%s" % interval)
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())
msgboot.append("acks=0") msgboot.append("acks=0")
msg=";".join(msgboot) msg=";".join(msgboot)
while 1: while 1:
fail=0 fail=0
for hb_host in hb_hosts: for hb_host in hb_hosts:
try: try:
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:
fail=1 fail=1
if fail: if fail:
time.sleep(10) time.sleep(10)
else: else:
break break
if msgonly: if msgonly:
sys.exit(0) sys.exit(0)
if daemon: if daemon:
pid=os.fork() pid=os.fork()
if pid > 0: if pid > 0:
if verbose: if verbose:
print "daemoinizing... pid=%d" % pid print "daemoinizing... pid=%d" % pid
sys.exit(0) sys.exit(0)
os.close(0) os.close(0)
os.close(1) os.close(1)
os.close(2) os.close(2)
sys.stdin.close() sys.stdin.close()
sys.stdout = NullDevice() sys.stdout = NullDevice()
sys.stderr = NullDevice() sys.stderr = NullDevice()
os.chdir("/") os.chdir("/")
os.setsid() os.setsid()
os.umask(0) os.umask(0)
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 ackcount=0
lastT=time.time() lastT=time.time()
while up: while up:
sleep=(lastT+interval) - time.time() sleep=(lastT+interval) - time.time()
if verbose: print "sleep %s" % sleep if verbose: print "sleep %s" % sleep
if sleep > 0: if sleep > 0:
try: try:
r=select.select([sock.fileno()],[],[],sleep) r=select.select([sock.fileno()],[],[],sleep)
# time.sleep(interval) # time.sleep(interval)
except: except:
break break
if verbose: print r if verbose: print r
if sock.fileno() in r[0]: if sock.fileno() in r[0]:
data, addr = sock.recvfrom(1024) data, addr = sock.recvfrom(1024)
if data == "ACK": if data == "ACK":
ackcount+=1 ackcount+=1
else: else:
try: try:
os.system(data) os.system(data)
except: except:
pass pass
continue continue
lastT=time.time() lastT=time.time()
for hb_host in hb_hosts: for hb_host in hb_hosts:
try: try:
msg="interval=%s;name=%s;time=%s;acks=%s" % (interval, iam, time.time(), ackcount) 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;acks=%s" % (iam, ackcount) 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 "hbc: sock.send('%s', (%s, %s))" % (msg, hb_host, hb_port)
sock.sendto(msg, (hb_host, hb_port)) sock.sendto(msg, (hb_host, hb_port))
time.sleep(1) time.sleep(1)
sock.close() sock.close()