major changes to both hbc and hbd

This commit is contained in:
andreas
2005-07-19 20:31:05 +00:00
parent b818ffc4c8
commit 203f92d61f
2 changed files with 204 additions and 49 deletions
+51 -17
View File
@@ -1,9 +1,13 @@
#!/usr/bin/env python
# $Id: hbc,v 1.2 2005/07/15 14:25:06 andreas Exp $
# $Id: hbc,v 1.3 2005/07/19 20:31:05 andreas Exp $
import sys, time, socket, os, signal, getopt, string
PORT=50003
INTERVAL=20
INTERVAL=10
class NullDevice:
def write(self, s):
pass
def handler(signum, frame):
@@ -12,9 +16,10 @@ def handler(signum, frame):
return
sys.exit(0)
helpflag=0
verbose=0
daemon=0
msgonly=False
helpflag=False
verbose=False
daemon=False
optlist=[]
args=[]
msgboot=[]
@@ -24,7 +29,7 @@ configfile="%s/.hbrc" % home
try:
optlist, args = getopt.getopt(sys.argv[1:], 'bc:dhm:v')
except:
helpflag=1
helpflag=True
for o,a in optlist:
if o == '-b':
@@ -32,24 +37,37 @@ for o,a in optlist:
elif o == '-c':
configfile=a
elif o == '-d':
daemon=1
daemon=True
elif o == '-h':
helpflag=1
helpflag=True
elif o == '-m':
msgboot.append("service=%s" % "service")
a.replace(';',':')
msgboot.append("msg=%s" % a)
msgonly=True
elif o == '-v':
verbose+=1
verbose=True
if helpflag:
print "hbc HeartBeatClient"
print "usage: hbc [-bdhv] [-c configfile] [-m msg][host1 [..]]"
print
print " -b indicate machine boot"
print " -c configfile"
print " -d daemonize"
print " -h this help"
print " -m send a message"
print " -v verbose"
print
print """ config file can contain
hb_hosts=('host1', 'host2', ..._
hb_port=50003
interval=20
logfile=...
logfmt={|test|msg}
grace=SECONDS
reportstrict={True|False}
"""
sys.exit(1)
@@ -76,6 +94,8 @@ if f:
r=l[:-1].split('=')
if r[0] == 'hb_hosts':
hb_hosts=eval(r[1])
if verbose:
print "notice: cfg hb_hosts: %s" % hb_hosts
elif r[0] == 'interval':
interval=eval(r[1])
elif r[0] == 'hb_port':
@@ -99,7 +119,7 @@ if verbose:
print "notice: interval: %s" % interval
print "notice: iam: %s" % iam
if daemon:
if not msgonly:
msgboot.append("interval=%s" % interval)
if len(msgboot) > 0:
@@ -118,15 +138,27 @@ if len(msgboot) > 0:
else:
break
if msgonly:
sys.exit(0)
if not daemon:
sys.exit(0)
if daemon:
pid=os.fork()
if pid > 0:
if verbose:
print "daemoinizing... pid=%d" % pid
sys.exit(0)
os.close(0)
os.close(1)
os.close(2)
sys.stdin.close()
sys.stdout = NullDevice()
sys.stderr = NullDevice()
os.chdir("/")
os.setsid()
os.umask(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
@@ -152,3 +184,5 @@ for hb_host in hb_hosts:
sock.sendto(msg, (hb_host, hb_port))
time.sleep(1)
sock.close()