major rework
- support config file - support multiple hbd target hosts - daemonize (almost) properly
This commit is contained in:
@@ -1,48 +1,154 @@
|
||||
#!/usr/bin/env python
|
||||
# $Id: hbc,v 1.1 2005/07/14 19:28:59 andreas Exp $
|
||||
import sys, time, socket, os, signal
|
||||
# $Id: hbc,v 1.2 2005/07/15 14:25:06 andreas Exp $
|
||||
import sys, time, socket, os, signal, getopt, string
|
||||
|
||||
ADDR="204.29.161.33"
|
||||
#ADDR="10.99.1.4"
|
||||
PORT=50003
|
||||
INTERVAL=20
|
||||
|
||||
|
||||
def handler(signum, frame):
|
||||
global up
|
||||
if signum == 1:
|
||||
os.execv("/usr/bin/env", ["python", sys.argv[0],"relaunch"])
|
||||
if up == 0:
|
||||
return
|
||||
sys.exit(0)
|
||||
iam=socket.gethostname()
|
||||
sock=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
msg="interval=%s;name=%s" % (INTERVAL, iam)
|
||||
|
||||
if len(sys.argv) <= 1 or sys.argv[1] != "relaunch":
|
||||
msgboot="boot=1;interval=%s;name=%s" % (INTERVAL, iam)
|
||||
while 1:
|
||||
helpflag=0
|
||||
verbose=0
|
||||
daemon=0
|
||||
optlist=[]
|
||||
args=[]
|
||||
msgboot=[]
|
||||
home=os.environ['HOME']
|
||||
configfile="%s/.hbrc" % home
|
||||
|
||||
try:
|
||||
sock.sendto(msgboot, (ADDR, PORT))
|
||||
break
|
||||
optlist, args = getopt.getopt(sys.argv[1:], 'bc:dhm:v')
|
||||
except:
|
||||
time.sleep(10)
|
||||
helpflag=1
|
||||
|
||||
for o,a in optlist:
|
||||
if o == '-b':
|
||||
msgboot.append("boot=1")
|
||||
elif o == '-c':
|
||||
configfile=a
|
||||
elif o == '-d':
|
||||
daemon=1
|
||||
elif o == '-h':
|
||||
helpflag=1
|
||||
elif o == '-m':
|
||||
msgboot.append("service=%s" % "service")
|
||||
msgboot.append("msg=%s" % a)
|
||||
elif o == '-v':
|
||||
verbose+=1
|
||||
|
||||
|
||||
if helpflag:
|
||||
print "hbc HeartBeatClient"
|
||||
print "usage: hbc [-bdhv] [-c configfile] [-m msg][host1 [..]]"
|
||||
print
|
||||
print """ config file can contain
|
||||
hb_hosts=('host1', 'host2', ..._
|
||||
hb_port=50003
|
||||
interval=20
|
||||
"""
|
||||
|
||||
sys.exit(1)
|
||||
|
||||
#
|
||||
# set defaults
|
||||
|
||||
hb_port=PORT
|
||||
interval=INTERVAL
|
||||
hb_hosts=[]
|
||||
|
||||
try:
|
||||
f=open(configfile,"r")
|
||||
if verbose: print "notice: using config file %s" % configfile
|
||||
except:
|
||||
print "warning: running without conifig file: %s" % configfile
|
||||
f=None
|
||||
|
||||
if f:
|
||||
while 1:
|
||||
l=f.readline()
|
||||
if len(l) == 0:
|
||||
break
|
||||
r=l[:-1].split('=')
|
||||
if r[0] == 'hb_hosts':
|
||||
hb_hosts=eval(r[1])
|
||||
elif r[0] == 'interval':
|
||||
interval=eval(r[1])
|
||||
elif r[0] == 'hb_port':
|
||||
hb_port=eval(r[1])
|
||||
f.close()
|
||||
|
||||
if len(args) != 0:
|
||||
hb_hosts=args
|
||||
|
||||
if len(hb_hosts) == 0:
|
||||
print "no hb server specified"
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
sock=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
iam=socket.gethostname()
|
||||
|
||||
if verbose:
|
||||
print "notice: hb_hosts: %s" % str(hb_hosts)
|
||||
print "notice: hb_port: %s" % hb_port
|
||||
print "notice: interval: %s" % interval
|
||||
print "notice: iam: %s" % iam
|
||||
|
||||
if daemon:
|
||||
msgboot.append("interval=%s" % interval)
|
||||
|
||||
if len(msgboot) > 0:
|
||||
msgboot.append("name=%s" % iam)
|
||||
msg=string.join(msgboot,";")
|
||||
while 1:
|
||||
fail=0
|
||||
for hb_host in hb_hosts:
|
||||
try:
|
||||
if verbose: print "sock.send('%s', (%s, %s))" % (msg, hb_host, hb_port)
|
||||
sock.sendto(msg, (hb_host, hb_port))
|
||||
except:
|
||||
fail=1
|
||||
if fail:
|
||||
time.sleep(10)
|
||||
else:
|
||||
break
|
||||
|
||||
|
||||
if not daemon:
|
||||
sys.exit(0)
|
||||
|
||||
pid=os.fork()
|
||||
if pid > 0:
|
||||
if verbose:
|
||||
print "daemoinizing... pid=%d" % pid
|
||||
sys.exit(0)
|
||||
|
||||
msg="interval=%s;name=%s" % (interval, iam)
|
||||
up=1
|
||||
signal.signal(signal.SIGTERM, handler)
|
||||
signal.signal(signal.SIGHUP, handler)
|
||||
|
||||
while up:
|
||||
try:
|
||||
time.sleep(10)
|
||||
time.sleep(interval)
|
||||
except:
|
||||
break
|
||||
for hb_host in hb_hosts:
|
||||
try:
|
||||
sock.sendto(msg, (ADDR, PORT))
|
||||
if verbose: print "sock.send('%s', (%s, %s))" % (msg, hb_host, hb_port)
|
||||
sock.sendto(msg, (hb_host, hb_port))
|
||||
except:
|
||||
pass
|
||||
|
||||
up=0
|
||||
msg="shutdown=1;name=%s" % (iam)
|
||||
sock.sendto(msg, (ADDR, PORT))
|
||||
for hb_host in hb_hosts:
|
||||
if verbose: print "sock.send('%s', (%s, %s))" % (msg, hb_host, hb_port)
|
||||
sock.sendto(msg, (hb_host, hb_port))
|
||||
time.sleep(1)
|
||||
sock.close()
|
||||
|
||||
Reference in New Issue
Block a user