Merge branch 'master' of git.wrede.ca:andreas/heartbeat
sequence error
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
#!/usr/bin/env python
|
||||
# $Id: hbc,v 1.9 2012/03/29 02:08:36 andreas Exp $
|
||||
|
||||
# requre python-filelock on Linux
|
||||
# require py27-lockfile on *bsd
|
||||
# or run sudo easy_install-2.7 lockfile
|
||||
import sys
|
||||
import time
|
||||
import socket
|
||||
@@ -9,12 +13,12 @@ import getopt
|
||||
import string
|
||||
import select
|
||||
import errno
|
||||
import traceback
|
||||
from lockfile import FileLock
|
||||
|
||||
PORT=50003
|
||||
INTERVAL=10
|
||||
False=0
|
||||
True=1
|
||||
DBG = False
|
||||
DBG = True
|
||||
|
||||
sock=None
|
||||
|
||||
@@ -23,60 +27,6 @@ class NullDevice:
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class Flock:
|
||||
def __init__(self, lock_file):
|
||||
self.lock_file = lock_file
|
||||
self.fd = None
|
||||
self.opid = None
|
||||
|
||||
def lock(self):
|
||||
while 1:
|
||||
self.fd = None
|
||||
try:
|
||||
self.fd = os.open(self.lock_file, os.O_CREAT | os.O_EXCL | os.O_RDWR)
|
||||
except OSError, e:
|
||||
if e.errno != errno.EEXIST:
|
||||
raise
|
||||
except:
|
||||
raise
|
||||
|
||||
if not self.fd:
|
||||
if self.oproc():
|
||||
if DBG: print "process is alive"
|
||||
os.remove(self.lock_file)
|
||||
continue
|
||||
else:
|
||||
if DBG: print "no pid process??"
|
||||
|
||||
if self.fd:
|
||||
f=os.fdopen(self.fd, 'w').write("%s" % os.getpid())
|
||||
return self.fd
|
||||
|
||||
|
||||
def unlock(self):
|
||||
os.remove(self.lock_file)
|
||||
self.fd=None
|
||||
|
||||
|
||||
def setopid(self):
|
||||
try:
|
||||
self.opid=open(self.lock_file).readline()
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def oproc(self):
|
||||
self.setopid()
|
||||
if not self.opid:
|
||||
return False
|
||||
try:
|
||||
os.kill(int(self.opid), 0)
|
||||
return True
|
||||
except:
|
||||
pass
|
||||
return False
|
||||
|
||||
def handler(signum, frame):
|
||||
global up
|
||||
if up == 0:
|
||||
@@ -110,7 +60,40 @@ def socksend(msg, tohost):
|
||||
if sock == None:
|
||||
sock=getsock(tohost[0])
|
||||
sock.sendto(msg, tohost)
|
||||
if verbose: print "msg %s sent" % msg
|
||||
|
||||
def process():
|
||||
ackcount=0
|
||||
lastT=time.time()
|
||||
while up:
|
||||
sleep=(lastT+interval) - time.time()
|
||||
if verbose: print "sleep %s" % sleep
|
||||
if sleep > 0:
|
||||
try:
|
||||
r=select.select([sock.fileno()],[],[],sleep)
|
||||
# time.sleep(interval)
|
||||
except:
|
||||
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:
|
||||
try:
|
||||
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)
|
||||
socksend(msg, (hb_host, hb_port))
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
msgonly=False
|
||||
helpflag=False
|
||||
@@ -238,6 +221,7 @@ if len(msgboot) > 0:
|
||||
else:
|
||||
break
|
||||
|
||||
if verbose: print "msgboot done msgonly=%s" % msgonly
|
||||
if msgonly:
|
||||
sys.exit(0)
|
||||
|
||||
@@ -252,59 +236,33 @@ if daemon:
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
os.close(0)
|
||||
os.close(1)
|
||||
os.close(2)
|
||||
sys.stdin.close()
|
||||
sys.stdout = NullDevice()
|
||||
sys.stderr = NullDevice()
|
||||
if not DBG:
|
||||
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)
|
||||
|
||||
|
||||
while True:
|
||||
lock=Flock('/tmp/hbc.pid')
|
||||
if lock.lock():
|
||||
break
|
||||
if not lock.oproc():
|
||||
sys.exit(1)
|
||||
os.kill(lock.opid,15)
|
||||
time.sleep(1)
|
||||
lock = FileLock('/tmp/hbc.pid')
|
||||
lock.acquire()
|
||||
|
||||
up=1
|
||||
signal.signal(signal.SIGTERM, handler)
|
||||
signal.signal(signal.SIGHUP, handler)
|
||||
ackcount=0
|
||||
lastT=time.time()
|
||||
while up:
|
||||
sleep=(lastT+interval) - time.time()
|
||||
if verbose: print "sleep %s" % sleep
|
||||
if sleep > 0:
|
||||
try:
|
||||
r=select.select([sock.fileno()],[],[],sleep)
|
||||
# time.sleep(interval)
|
||||
except:
|
||||
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:
|
||||
try:
|
||||
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)
|
||||
socksend(msg, (hb_host, hb_port))
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
|
||||
try:
|
||||
process()
|
||||
except:
|
||||
data='hbc died:\n'+traceback.format_exc()
|
||||
open("/tmp/hbc.log","a").write(data)
|
||||
sys.exit(1)
|
||||
|
||||
up=0
|
||||
msg="shutdown=1;name=%s;acks=%s" % (iam, ackcount)
|
||||
@@ -313,4 +271,4 @@ for hb_host in hb_hosts:
|
||||
socksend(msg, (hb_host, hb_port))
|
||||
time.sleep(1)
|
||||
sock.close()
|
||||
lock.unlock()
|
||||
lock.release()
|
||||
|
||||
Reference in New Issue
Block a user