change stats of connectivity loss

This commit is contained in:
2015-03-06 18:19:31 +01:00
parent b7ca714f86
commit 1adfacfed7
2 changed files with 120 additions and 23 deletions
+77 -1
View File
@@ -1,11 +1,20 @@
#!/usr/bin/env python
# $Id: hbc,v 1.9 2012/03/29 02:08:36 andreas Exp $
import sys, time, socket, os, signal, getopt, string, select
import sys
import time
import socket
import os
import signal
import getopt
import string
import select
import errno
PORT=50003
INTERVAL=10
False=0
True=1
DBG = False
sock=None
@@ -14,6 +23,60 @@ 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:
@@ -178,6 +241,9 @@ if len(msgboot) > 0:
if msgonly:
sys.exit(0)
#
#
if daemon:
pid=os.fork()
if pid > 0:
@@ -197,6 +263,15 @@ if daemon:
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)
up=1
signal.signal(signal.SIGTERM, handler)
signal.signal(signal.SIGHUP, handler)
@@ -238,3 +313,4 @@ for hb_host in hb_hosts:
socksend(msg, (hb_host, hb_port))
time.sleep(1)
sock.close()
lock.unlock()