update dns ina thread

This commit is contained in:
2016-01-12 21:37:27 +01:00
parent ce489af512
commit e12b524c0c
+24 -8
View File
@@ -21,6 +21,7 @@ import traceback
import urllib import urllib
import httplib import httplib
import threading import threading
import Queue
from subprocess import Popen, STDOUT, PIPE from subprocess import Popen, STDOUT, PIPE
@@ -200,7 +201,7 @@ def pushover(msg):
# nsupdate: set the DNS A record for a fqdn # nsupdate: set the DNS A record for a fqdn
# return: None if ok, else error text # return: None if ok, else error text
def nsupdate(hostname, newip): def nsupdate(hostname, newip):
u = {} D = {}
D['domain'] = 'dy.wapanafa.org' D['domain'] = 'dy.wapanafa.org'
D['fqdn'] = '%s.dy.wapanafa.org' % hostname D['fqdn'] = '%s.dy.wapanafa.org' % hostname
D['dnsttl'] = '5' D['dnsttl'] = '5'
@@ -318,6 +319,19 @@ def log(m, service="heartbeat"):
pickleit() pickleit()
def dnsupdatethread():
while True:
name, addr = dnsQ.get()
m = "%s changed address to %s" % (name, addr)
err = nsupdate(name, addr)
if err:
m += ", DNS failed: %s" % err
email("error: nsupdate failed", m)
else:
m += ", DNS updated."
dnsQ.task_done()
log(m)
# #
# #
def fromaddr(name, addr, boot, interval, acks): def fromaddr(name, addr, boot, interval, acks):
@@ -337,13 +351,9 @@ def fromaddr(name, addr, boot, interval, acks):
htab[addr] = name htab[addr] = name
m = "%s changed address to %s" % (host.name, addr) m = "%s changed address to %s" % (host.name, addr)
if name in dyndnshosts and not ":" in addr: # don't try and cat ptr to IPv6 addr if name in dyndnshosts and not ":" in addr: # don't try and cat ptr to IPv6 addr
err = nsupdate(name, addr) dnsQ.put((name, addr))
if err: else:
m += ", DNS failed: %s" % err log(m)
email("error: nsupdate failed", m)
else:
m += ", DNS updated."
log(m)
if name in watchhosts: if name in watchhosts:
email("address change", m) email("address change", m)
pushover(m) pushover(m)
@@ -837,6 +847,7 @@ ilist.append(sock6)
serv = HttpServer((hbd_host, hbd_port), HttpHandler) serv = HttpServer((hbd_host, hbd_port), HttpHandler)
servthread = threading.Thread(target=serv.serve_forever) servthread = threading.Thread(target=serv.serve_forever)
servthread.daemon = True
servthread.start() servthread.start()
#ilist.append(serv.fileno()) #ilist.append(serv.fileno())
@@ -859,6 +870,11 @@ if not forground:
os.setsid() os.setsid()
os.umask(0) os.umask(0)
dnsQ = Queue.Queue()
dnsT = threading.Thread(target=dnsupdatethread)
dnsT.daemon = True
dnsT.start()
running = True running = True
sig = 0 sig = 0
#signal.signal(signal.SIGTERM, handler) #signal.signal(signal.SIGTERM, handler)