add IPv6 support
This commit is contained in:
@@ -6,6 +6,9 @@ PORT=50003
|
|||||||
INTERVAL=10
|
INTERVAL=10
|
||||||
False=0
|
False=0
|
||||||
True=1
|
True=1
|
||||||
|
|
||||||
|
sock=None
|
||||||
|
|
||||||
class NullDevice:
|
class NullDevice:
|
||||||
def write(self, s):
|
def write(self, s):
|
||||||
pass
|
pass
|
||||||
@@ -17,6 +20,35 @@ def handler(signum, frame):
|
|||||||
return
|
return
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
|
def getsock(host):
|
||||||
|
try:
|
||||||
|
r=socket.getaddrinfo(host, 50001, 0, 0, socket.SOL_UDP)
|
||||||
|
except socket.gaierror:
|
||||||
|
return None
|
||||||
|
if r[0][0] == 28:
|
||||||
|
af_type=socket.AF_INET6
|
||||||
|
elif r[0][0] == 2:
|
||||||
|
af_type=socket.AF_INET
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
if verbose:
|
||||||
|
print "socktype: %s" % af_type
|
||||||
|
sock=socket.socket(af_type, socket.SOCK_DGRAM)
|
||||||
|
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, \
|
||||||
|
sock.getsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR) | 1)
|
||||||
|
if verbose: print "get socket %s" % sock
|
||||||
|
|
||||||
|
return sock
|
||||||
|
|
||||||
|
def socksend(msg, tohost):
|
||||||
|
global sock
|
||||||
|
|
||||||
|
if sock == None:
|
||||||
|
sock=getsock(tohost[0])
|
||||||
|
sock.sendto(msg, tohost)
|
||||||
|
|
||||||
|
|
||||||
msgonly=False
|
msgonly=False
|
||||||
helpflag=False
|
helpflag=False
|
||||||
verbose=False
|
verbose=False
|
||||||
@@ -115,10 +147,6 @@ if len(hb_hosts) == 0:
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
sock=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
||||||
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, \
|
|
||||||
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)
|
||||||
@@ -139,7 +167,7 @@ if len(msgboot) > 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))
|
socksend(msg, (hb_host, hb_port))
|
||||||
except:
|
except:
|
||||||
fail=1
|
fail=1
|
||||||
if fail:
|
if fail:
|
||||||
@@ -199,7 +227,7 @@ while up:
|
|||||||
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))
|
socksend(msg, (hb_host, hb_port))
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -207,6 +235,6 @@ 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 "hbc: 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))
|
socksend(msg, (hb_host, hb_port))
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
sock.close()
|
sock.close()
|
||||||
@@ -340,6 +340,7 @@ def display():
|
|||||||
|
|
||||||
|
|
||||||
def log(m, service="heartbeat"):
|
def log(m, service="heartbeat"):
|
||||||
|
if DEBUG: print "Log: %s" % m
|
||||||
msg = time.strftime("%b %d %H:%M:%S", time.localtime(time.time()))+": "+m+"\n"
|
msg = time.strftime("%b %d %H:%M:%S", time.localtime(time.time()))+": "+m+"\n"
|
||||||
msgs.append(msg)
|
msgs.append(msg)
|
||||||
if logfmt == "msg":
|
if logfmt == "msg":
|
||||||
@@ -396,7 +397,7 @@ def fromaddr(name, addr, boot, interval, acks):
|
|||||||
|
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
def readsock():
|
def readsock(sock):
|
||||||
global htab, win
|
global htab, win
|
||||||
data, addr = sock.recvfrom(1024)
|
data, addr = sock.recvfrom(1024)
|
||||||
pairs = string.split(data, ';')
|
pairs = string.split(data, ';')
|
||||||
@@ -475,7 +476,9 @@ def readsock():
|
|||||||
del hosts[name].cmds[0]
|
del hosts[name].cmds[0]
|
||||||
log("%s command initiated" % name)
|
log("%s command initiated" % name)
|
||||||
try:
|
try:
|
||||||
sock.sendto(rmsg, addr)
|
ss=sock.sendto(rmsg, addr)
|
||||||
|
if DEBUG:
|
||||||
|
log("msg from %s,%s, sent %s bytes back" % (addr[0], addr[1], ss))
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -791,11 +794,16 @@ atexit.register(on_exit)
|
|||||||
ilist = []
|
ilist = []
|
||||||
|
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
|
||||||
sock.bind(("", hb_port))
|
sock.bind(("", hb_port))
|
||||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
ilist.append(sock)
|
ilist.append(sock)
|
||||||
|
|
||||||
|
sock6 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
|
||||||
|
sock6.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
sock6.bind(("", hb_port))
|
||||||
|
ilist.append(sock6)
|
||||||
|
|
||||||
|
|
||||||
serv = HtmlServer((hbd_host, hbd_port), HtmlHandler)
|
serv = HtmlServer((hbd_host, hbd_port), HtmlHandler)
|
||||||
ilist.append(serv.fileno())
|
ilist.append(serv.fileno())
|
||||||
|
|
||||||
@@ -861,10 +869,12 @@ while running:
|
|||||||
except:
|
except:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
for fh in sr[0]:
|
for fh in sr[0]:
|
||||||
if fh == sock:
|
if fh in [sock, sock6]:
|
||||||
readsock()
|
readsock(fh)
|
||||||
if fh == serv.fileno():
|
elif fh == serv.fileno():
|
||||||
serv.handle_request()
|
serv.handle_request()
|
||||||
|
else:
|
||||||
|
print("what happend just now")
|
||||||
if now >= next:
|
if now >= next:
|
||||||
next = now+1
|
next = now+1
|
||||||
checkoverdue()
|
checkoverdue()
|
||||||
|
|||||||
Reference in New Issue
Block a user