move to python 3
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python3
|
||||||
# $Id: hbd,v 1.38 2013/07/14 02:25:05 andreas Exp $
|
# $Id: hbd,v 1.38 2013/07/14 02:25:05 andreas Exp $
|
||||||
# Wait for heartbeat messages and act on them (or their absence)
|
# Wait for heartbeat messages and act on them (or their absence)
|
||||||
#
|
#
|
||||||
@@ -11,19 +11,19 @@ import sys
|
|||||||
import socket
|
import socket
|
||||||
import atexit
|
import atexit
|
||||||
import select
|
import select
|
||||||
import SocketServer
|
import socketserver
|
||||||
import BaseHTTPServer
|
import http.server
|
||||||
import getopt
|
import getopt
|
||||||
import signal
|
import signal
|
||||||
import cPickle
|
import pickle
|
||||||
import smtplib
|
import smtplib
|
||||||
import traceback
|
import traceback
|
||||||
import urllib
|
import urllib.request, urllib.parse, urllib.error
|
||||||
import urlparse
|
import urllib.parse
|
||||||
import httplib
|
import http.client
|
||||||
import threading
|
import threading
|
||||||
import Queue
|
import queue
|
||||||
import md5
|
from hashlib import md5
|
||||||
import json
|
import json
|
||||||
import zlib
|
import zlib
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ from hbdclass import *
|
|||||||
SEND_EMAIL=False
|
SEND_EMAIL=False
|
||||||
SEND_PUSHOVER=True
|
SEND_PUSHOVER=True
|
||||||
|
|
||||||
DEBUG = 0
|
DEBUG = 3
|
||||||
|
|
||||||
MAXRECV = 32767
|
MAXRECV = 32767
|
||||||
LOGFILE = "/home/andreas/public_html/messages/andreas"
|
LOGFILE = "/home/andreas/public_html/messages/andreas"
|
||||||
@@ -105,7 +105,7 @@ def handler(signum, frame):
|
|||||||
|
|
||||||
|
|
||||||
def shortname(name):
|
def shortname(name):
|
||||||
r = string.split(name, '.')
|
r = name.split('.')
|
||||||
return r[0]
|
return r[0]
|
||||||
|
|
||||||
|
|
||||||
@@ -132,22 +132,22 @@ def dicttos(ID, d, compress=False):
|
|||||||
s.append("%s=%s" % (k, d[k]))
|
s.append("%s=%s" % (k, d[k]))
|
||||||
pk = ";".join(s)
|
pk = ";".join(s)
|
||||||
if compress:
|
if compress:
|
||||||
zpk = zlib.compress(pk, 6)
|
zpk = zlib.compress(pk.encode(), 6)
|
||||||
ID = "!"+ID
|
ID = "!" + ID + ":"
|
||||||
|
opk = ID.encode() + zpk
|
||||||
else:
|
else:
|
||||||
zpk = pk
|
zpk = pk
|
||||||
return ID + ":" + zpk
|
opk = ID + ":" + zpk
|
||||||
|
return opk
|
||||||
|
|
||||||
|
|
||||||
def stodict(msg):
|
def stodict(msg):
|
||||||
d = {}
|
d = {}
|
||||||
r0 = msg.split(':',1)
|
if len(msg) > 0 and chr(msg[0]) == "!":
|
||||||
if len(r0) == 1:
|
pk = zlib.decompress(msg[5:]).decode()
|
||||||
return None
|
d['ID'] = msg[1:4].decode()
|
||||||
if r0[0][0] == '!': # compressed
|
|
||||||
pk = zlib.decompress(msg[len(r0[0])+1:])
|
|
||||||
d['ID'] = r0[0][1:]
|
|
||||||
else:
|
else:
|
||||||
|
r0 = msg.split(':',1)
|
||||||
pk = r0[1]
|
pk = r0[1]
|
||||||
d['ID'] = r0[0]
|
d['ID'] = r0[0]
|
||||||
r = pk.split(';')
|
r = pk.split(';')
|
||||||
@@ -181,11 +181,11 @@ def email(s, msg):
|
|||||||
server = smtplib.SMTP(SMTPSERVER)
|
server = smtplib.SMTP(SMTPSERVER)
|
||||||
if DEBUG > 0: server.set_debuglevel(1)
|
if DEBUG > 0: server.set_debuglevel(1)
|
||||||
server.sendmail(fromemail, toaddrs, body)
|
server.sendmail(fromemail, toaddrs, body)
|
||||||
except smtplib.SMTPRecipientsRefused, errs:
|
except smtplib.SMTPRecipientsRefused as errs:
|
||||||
log(None, "cannot send email: %s\n" % (errs))
|
log(None, "cannot send email: %s\n" % (errs))
|
||||||
ret = "Fail"
|
ret = "Fail"
|
||||||
except:
|
except:
|
||||||
print("smtp error: "+traceback.format_exc())
|
print(("smtp error: "+traceback.format_exc()))
|
||||||
saveandrestart()
|
saveandrestart()
|
||||||
try:
|
try:
|
||||||
server.quit()
|
server.quit()
|
||||||
@@ -197,10 +197,10 @@ def email(s, msg):
|
|||||||
def pushover(msg):
|
def pushover(msg):
|
||||||
if not SEND_PUSHOVER:
|
if not SEND_PUSHOVER:
|
||||||
return
|
return
|
||||||
conn = httplib.HTTPSConnection("api.pushover.net:443")
|
conn = http.client.HTTPSConnection("api.pushover.net:443")
|
||||||
try:
|
try:
|
||||||
conn.request("POST", "/1/messages.json",
|
conn.request("POST", "/1/messages.json",
|
||||||
urllib.urlencode({
|
urllib.parse.urlencode({
|
||||||
"token": "ac7NLX2rPjXFareeDgLpXNoDf4iFmf",
|
"token": "ac7NLX2rPjXFareeDgLpXNoDf4iFmf",
|
||||||
"user": "uDhH33UjQQDYtNzJb1ThRiWb9ingGK",
|
"user": "uDhH33UjQQDYtNzJb1ThRiWb9ingGK",
|
||||||
"message": msg, }), { "Content-type": "application/x-www-form-urlencoded" })
|
"message": msg, }), { "Content-type": "application/x-www-form-urlencoded" })
|
||||||
@@ -242,7 +242,7 @@ answer
|
|||||||
if DEBUG > 0: log(None, "DBG: cmd %s" % cmd)
|
if DEBUG > 0: log(None, "DBG: cmd %s" % cmd)
|
||||||
try:
|
try:
|
||||||
p = Popen(cmd, shell=False, bufsize=1, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
|
p = Popen(cmd, shell=False, bufsize=1, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
|
||||||
except OSError, e:
|
except OSError as e:
|
||||||
return "nsupdate: execution failed: %s" % e
|
return "nsupdate: execution failed: %s" % e
|
||||||
except:
|
except:
|
||||||
return "nsupdate: some error occured"
|
return "nsupdate: some error occured"
|
||||||
@@ -256,9 +256,9 @@ answer
|
|||||||
#
|
#
|
||||||
def dur(sec):
|
def dur(sec):
|
||||||
sec = int(sec)
|
sec = int(sec)
|
||||||
h = sec / 3600
|
h = int(sec / 3600)
|
||||||
m = (sec - h * 3600) / 60
|
m = int((sec - h * 3600) / 60)
|
||||||
s = (sec - h * 3600) % 60
|
s = int((sec - h * 3600) % 60)
|
||||||
if h > 0:
|
if h > 0:
|
||||||
return "%d:%02d:%02d" % (h, m, s)
|
return "%d:%02d:%02d" % (h, m, s)
|
||||||
if m > 0:
|
if m > 0:
|
||||||
@@ -267,7 +267,7 @@ def dur(sec):
|
|||||||
|
|
||||||
|
|
||||||
def fixsort():
|
def fixsort():
|
||||||
s = Host.hosts.keys()
|
s = list(Host.hosts.keys())
|
||||||
s.sort()
|
s.sort()
|
||||||
x = 0
|
x = 0
|
||||||
for n in s:
|
for n in s:
|
||||||
@@ -281,7 +281,7 @@ def on_exit():
|
|||||||
logf.close()
|
logf.close()
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
print "exit"
|
print("exit")
|
||||||
|
|
||||||
|
|
||||||
def initlog(logfile):
|
def initlog(logfile):
|
||||||
@@ -296,7 +296,7 @@ def initlog(logfile):
|
|||||||
#
|
#
|
||||||
def checkoverdue():
|
def checkoverdue():
|
||||||
now = time.time()
|
now = time.time()
|
||||||
for h in Host.hosts.keys():
|
for h in list(Host.hosts.keys()):
|
||||||
pmsg = []
|
pmsg = []
|
||||||
for c in Host.hosts[h].connections:
|
for c in Host.hosts[h].connections:
|
||||||
conn = Host.hosts[h].connections[c]
|
conn = Host.hosts[h].connections[c]
|
||||||
@@ -316,7 +316,7 @@ def checkoverdue():
|
|||||||
|
|
||||||
|
|
||||||
def log(host, m, service=None):
|
def log(host, m, service=None):
|
||||||
if DEBUG > 0: print "Log: %s %s" % (host, m)
|
if DEBUG > 0: print(("Log: %s %s" % (host, m)))
|
||||||
now = time.time()
|
now = time.time()
|
||||||
ts = time.strftime("%b %d %H:%M:%S", time.localtime(now))
|
ts = time.strftime("%b %d %H:%M:%S", time.localtime(now))
|
||||||
if service:
|
if service:
|
||||||
@@ -359,27 +359,28 @@ def dnsupdatethread():
|
|||||||
def readsock(sock):
|
def readsock(sock):
|
||||||
global now
|
global now
|
||||||
if DEBUG > 3: sys.stderr.write("readsock recfrom start")
|
if DEBUG > 3: sys.stderr.write("readsock recfrom start")
|
||||||
data, addrp = sock.recvfrom(MAXRECV)
|
|
||||||
now = time.time()
|
now = time.time()
|
||||||
if DEBUG > 2: sys.stderr.write("readsock = %s, %s\n" % (data,addrp))
|
data, addrp = sock.recvfrom(MAXRECV)
|
||||||
|
if DEBUG > 3: sys.stderr.write("readsock = %s, %s\n" % (data,addrp))
|
||||||
try:
|
try:
|
||||||
msg = stodict(data)
|
msg = stodict(data)
|
||||||
except:
|
except:
|
||||||
return
|
return
|
||||||
|
if DEBUG > 3: sys.stderr.write("msg is %s" % str(msg))
|
||||||
if not msg: # Old hbc client
|
if not msg: # Old hbc client
|
||||||
if verbose: print "old hbc:", data
|
if verbose: print(("old hbc:", data))
|
||||||
oldclient = True
|
oldclient = True
|
||||||
msg = oldmtodict(data)
|
msg = oldmtodict(data)
|
||||||
else:
|
else:
|
||||||
oldclient = False
|
oldclient = False
|
||||||
if DEBUG > 2: print "readsock = %s, %s" % (msg,addrp)
|
if DEBUG > 2: print(("readsock = %s, %s" % (msg,addrp)))
|
||||||
|
|
||||||
addr = addrp[0:2]
|
addr = addrp[0:2]
|
||||||
name = shortname(msg.get('name', "unknown"))
|
name = shortname(msg.get('name', "unknown"))
|
||||||
if not name in Host.hosts: # was: hosts.has_key(name):
|
if not name in Host.hosts: # was: hosts.has_key(name):
|
||||||
host = Host(name)
|
host = Host(name)
|
||||||
host.dyn = h in dyndnshosts
|
host.dyn = name in dyndnshosts
|
||||||
if verbose: print "XX: New host, num now %s" % (len(Host.hosts))
|
if verbose: print(("XX: New host, num now %s" % (len(Host.hosts))))
|
||||||
newh=True
|
newh=True
|
||||||
else:
|
else:
|
||||||
host = Host.hosts[name]
|
host = Host.hosts[name]
|
||||||
@@ -452,7 +453,7 @@ def readsock(sock):
|
|||||||
ss=sock.sendto(opkt, addr)
|
ss=sock.sendto(opkt, addr)
|
||||||
except:
|
except:
|
||||||
pass # XXX return pkg failes
|
pass # XXX return pkg failes
|
||||||
if DEBUG > 2: print "sendto1: %s (%s) %s %s" % (addr, len(opkt), op, str(rmsg)[:50])
|
if DEBUG > 2: print(("sendto1: %s (%s) %s %s" % (addr, len(opkt), op, str(rmsg)[:50])))
|
||||||
|
|
||||||
# send any commands we have queued
|
# send any commands we have queued
|
||||||
while len(host.cmds):
|
while len(host.cmds):
|
||||||
@@ -477,11 +478,11 @@ def readsock(sock):
|
|||||||
try:
|
try:
|
||||||
ss=sock.sendto(opkt, addr)
|
ss=sock.sendto(opkt, addr)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print "opkt len is %s" % len(opkt)
|
print(("opkt len is %s" % len(opkt)))
|
||||||
print "cannot send: %s" % e
|
print(("cannot send: %s" % e))
|
||||||
|
|
||||||
if verbose: print "sendto2: %s (%s) %s %s" % (addr, len(opkt), op, str(rmsg)[:50])
|
if verbose: print(("sendto2: %s (%s) %s %s" % (addr, len(opkt), op, str(rmsg)[:50])))
|
||||||
if DEBUG > 2: print "msg from %s,%s, sent %s bytes back" % (addr[0], addr[1], ss)
|
if DEBUG > 2: print(("msg from %s,%s, sent %s bytes back" % (addr[0], addr[1], ss)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -505,13 +506,13 @@ def updatecode(ucode, uname):
|
|||||||
#
|
#
|
||||||
# Web Server
|
# Web Server
|
||||||
#
|
#
|
||||||
class HttpServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
|
class HttpServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
|
||||||
allow_reuse_address = True
|
allow_reuse_address = True
|
||||||
def threaded():
|
def threaded():
|
||||||
pass
|
pass
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
class HttpHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
class HttpHandler(http.server.BaseHTTPRequestHandler):
|
||||||
|
|
||||||
server_version = "HeartbeatHTTP/%s" % VER
|
server_version = "HeartbeatHTTP/%s" % VER
|
||||||
|
|
||||||
@@ -520,8 +521,9 @@ class HttpHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||||||
|
|
||||||
|
|
||||||
def handle(self):
|
def handle(self):
|
||||||
|
return http.server.BaseHTTPRequestHandler.handle(self)
|
||||||
try:
|
try:
|
||||||
return BaseHTTPServer.BaseHTTPRequestHandler.handle(self)
|
return http.server.BaseHTTPRequestHandler.handle(self)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.log_error("Request went away: %r", e)
|
self.log_error("Request went away: %r", e)
|
||||||
self.close_connection = 1
|
self.close_connection = 1
|
||||||
@@ -587,11 +589,11 @@ class HttpHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||||||
global sig
|
global sig
|
||||||
code = 200
|
code = 200
|
||||||
xsig = 0
|
xsig = 0
|
||||||
rqAcceptEncoding = self.headers.getheader('Accept-encoding',{})
|
rqAcceptEncoding = self.headers.get('Accept-encoding',{})
|
||||||
headerdict = {"Content-Type": "text/html; charset = ISO-8859-1" }
|
headerdict = {"Content-Type": "text/html; charset = ISO-8859-1" }
|
||||||
if DEBUG > 2: sys.stderr.write("handle\n")
|
if DEBUG > 2: sys.stderr.write("handle\n")
|
||||||
qr = urlparse.urlparse(self.path)
|
qr = urllib.parse.urlparse(self.path)
|
||||||
qa = urlparse.parse_qs(qr.query)
|
qa = urllib.parse.parse_qs(qr.query)
|
||||||
|
|
||||||
if DEBUG > 2: sys.stderr.write("handle = %s\n" % (qr.geturl()))
|
if DEBUG > 2: sys.stderr.write("handle = %s\n" % (qr.geturl()))
|
||||||
if qr.path == "/":
|
if qr.path == "/":
|
||||||
@@ -602,10 +604,10 @@ class HttpHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||||||
ucmd=qa.get('c', [None])[0]
|
ucmd=qa.get('c', [None])[0]
|
||||||
if not ucmd or not uname:
|
if not ucmd or not uname:
|
||||||
code, res=self.builderror(400, 'Argument error', "need h= and c= arguments")
|
code, res=self.builderror(400, 'Argument error', "need h= and c= arguments")
|
||||||
elif not Host.hosts.has_key(uname):
|
elif uname not in Host.hosts:
|
||||||
code, res=self.builderror(400, 'Data error', "h=%s not found" % uname)
|
code, res=self.builderror(400, 'Data error', "h=%s not found" % uname)
|
||||||
else:
|
else:
|
||||||
Host.hosts[uname].cmds.append(('CMD', {'cmd': urllib.unquote(ucmd)}))
|
Host.hosts[uname].cmds.append(('CMD', {'cmd': urllib.parse.unquote(ucmd)}))
|
||||||
res=self.buildhead()
|
res=self.buildhead()
|
||||||
res.append("cmd %s queued for host %s" % (uname, ucmd))
|
res.append("cmd %s queued for host %s" % (uname, ucmd))
|
||||||
|
|
||||||
@@ -634,11 +636,11 @@ class HttpHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||||||
log(uname, ll)
|
log(uname, ll)
|
||||||
|
|
||||||
elif qr.path == "/u": # update
|
elif qr.path == "/u": # update
|
||||||
uname=urllib.unquote(qa.get('h',[None])[0])
|
uname=urllib.parse.unquote(qa.get('h',[None])[0])
|
||||||
ucode=qa.get('c', [None])[0]
|
ucode=qa.get('c', [None])[0]
|
||||||
if not ucode or not uname:
|
if not ucode or not uname:
|
||||||
code, res=self.builderror(400, 'Argument error', "need h= and c= arguments")
|
code, res=self.builderror(400, 'Argument error', "need h= and c= arguments")
|
||||||
elif uname != 'All' and not Host.hosts.has_key(uname):
|
elif uname != 'All' and uname not in Host.hosts:
|
||||||
code, res=self.builderror(400, 'Data error', "h=%s not found" % uname)
|
code, res=self.builderror(400, 'Data error', "h=%s not found" % uname)
|
||||||
else:
|
else:
|
||||||
res=self.buildhead()
|
res=self.buildhead()
|
||||||
@@ -678,9 +680,9 @@ class HttpHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||||||
|
|
||||||
if 'deflate' in rqAcceptEncoding:
|
if 'deflate' in rqAcceptEncoding:
|
||||||
headerdict['Content-Encoding'] = "deflate"
|
headerdict['Content-Encoding'] = "deflate"
|
||||||
towrite = zlib.compress(string.join(res, "\n"), 6)
|
towrite = zlib.compress("\n".join(res).encode(), 6)
|
||||||
else:
|
else:
|
||||||
towrite = string.join(res, "\n")
|
towrite = "\n".join(res)
|
||||||
headerdict['Content-Length'] = len(towrite)
|
headerdict['Content-Length'] = len(towrite)
|
||||||
headerdict['Cache-Control'] = 'private, must-revalidate, max-age=0'
|
headerdict['Cache-Control'] = 'private, must-revalidate, max-age=0'
|
||||||
headerdict['Expires'] = 'Thu, 01 Jan 1970 00:00:00 GMT'
|
headerdict['Expires'] = 'Thu, 01 Jan 1970 00:00:00 GMT'
|
||||||
@@ -731,9 +733,9 @@ def closeup():
|
|||||||
|
|
||||||
|
|
||||||
def restart():
|
def restart():
|
||||||
if verbose: print "execv %s %s" % (sys.argv[0], [sys.argv[0]]+cmdargs)
|
if verbose: print(("execv %s %s" % (sys.argv[0], [sys.argv[0]]+cmdargs)))
|
||||||
os.execv(sys.argv[0], [sys.argv[0]]+cmdargs)
|
os.execv(sys.argv[0], [sys.argv[0]]+cmdargs)
|
||||||
print "should not be here"
|
print("should not be here")
|
||||||
|
|
||||||
def saveandrestart():
|
def saveandrestart():
|
||||||
closeup()
|
closeup()
|
||||||
@@ -741,8 +743,8 @@ def saveandrestart():
|
|||||||
|
|
||||||
|
|
||||||
def pickleit():
|
def pickleit():
|
||||||
pickf = open(pickfile, 'w')
|
pickf = open(pickfile, 'wb')
|
||||||
pick = cPickle.Pickler(pickf)
|
pick = pickle.Pickler(pickf)
|
||||||
pick.dump(Host.hosts)
|
pick.dump(Host.hosts)
|
||||||
pick.dump(msgs)
|
pick.dump(msgs)
|
||||||
pick.dump(lastfm)
|
pick.dump(lastfm)
|
||||||
@@ -783,17 +785,17 @@ for o, a in optlist:
|
|||||||
cmdargs += [o]
|
cmdargs += [o]
|
||||||
|
|
||||||
if helpflag:
|
if helpflag:
|
||||||
print "hbc HeartBeatDaemon"
|
print("hbc HeartBeatDaemon")
|
||||||
print "usage: hbd [-dfhvx] [-c configfile]"
|
print("usage: hbd [-dfhvx] [-c configfile]")
|
||||||
print
|
print()
|
||||||
print " -c configfile"
|
print(" -c configfile")
|
||||||
print " -d display"
|
print(" -d display")
|
||||||
print " -f run in foreground"
|
print(" -f run in foreground")
|
||||||
print " -h this help"
|
print(" -h this help")
|
||||||
print " -v verbose"
|
print(" -v verbose")
|
||||||
print " -x increase debug lvl"
|
print(" -x increase debug lvl")
|
||||||
print
|
print()
|
||||||
print """ config file can contain
|
print(""" config file can contain
|
||||||
logfile = /var/log/heartbeat.log
|
logfile = /var/log/heartbeat.log
|
||||||
logfmt = [text|msg]
|
logfmt = [text|msg]
|
||||||
hb_port = 50003
|
hb_port = 50003
|
||||||
@@ -801,7 +803,7 @@ interval = 20
|
|||||||
hbd_port = 50004
|
hbd_port = 50004
|
||||||
hbd_host = www.domain.com
|
hbd_host = www.domain.com
|
||||||
grace = 2
|
grace = 2
|
||||||
"""
|
""")
|
||||||
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
@@ -823,9 +825,9 @@ drophosts = []
|
|||||||
try:
|
try:
|
||||||
f = open(configfile, "r")
|
f = open(configfile, "r")
|
||||||
if verbose:
|
if verbose:
|
||||||
print "notice: using config file %s" % configfile
|
print(("notice: using config file %s" % configfile))
|
||||||
except:
|
except:
|
||||||
print "warning: running without config file: %s" % configfile
|
print(("warning: running without config file: %s" % configfile))
|
||||||
f = None
|
f = None
|
||||||
|
|
||||||
if f:
|
if f:
|
||||||
@@ -837,13 +839,13 @@ if f:
|
|||||||
if len(l) == 0 or l[0] == "#":
|
if len(l) == 0 or l[0] == "#":
|
||||||
continue
|
continue
|
||||||
if verbose:
|
if verbose:
|
||||||
print " %s" % l
|
print((" %s" % l))
|
||||||
r = l.split('=')
|
r = l.split('=')
|
||||||
o = r[0].strip()
|
o = r[0].strip()
|
||||||
try:
|
try:
|
||||||
a = eval(r[1].strip())
|
a = eval(r[1].strip())
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print "error: %s" % str(r)
|
print(("error: %s" % str(r)))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
if o == 'interval':
|
if o == 'interval':
|
||||||
interval = a
|
interval = a
|
||||||
@@ -870,18 +872,18 @@ if f:
|
|||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
if len(args) != 0:
|
if len(args) != 0:
|
||||||
print "error: args"
|
print("error: args")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
print "notice: logging to %s" % logfile
|
print(("notice: logging to %s" % logfile))
|
||||||
logf = initlog(logfile)
|
logf = initlog(logfile)
|
||||||
|
|
||||||
if 1 and os.path.exists(pickfile):
|
if 1 and os.path.exists(pickfile):
|
||||||
if verbose: print "opening pickls %s" % pickfile
|
if verbose: print(("opening pickls %s" % pickfile))
|
||||||
pickf = open(pickfile, 'r')
|
pickf = open(pickfile, 'rb')
|
||||||
pick = cPickle.Unpickler(pickf)
|
pick = pickle.Unpickler(pickf)
|
||||||
try:
|
try:
|
||||||
Host.hosts = pick.load()
|
Host.hosts = pick.load()
|
||||||
msgs = pick.load()
|
msgs = pick.load()
|
||||||
@@ -891,18 +893,18 @@ if 1 and os.path.exists(pickfile):
|
|||||||
lastfm = ["","",""]
|
lastfm = ["","",""]
|
||||||
pickf.close()
|
pickf.close()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print "load pickled failed: %s" % e
|
print(("load pickled failed: %s" % e))
|
||||||
os.unlink(pickfile)
|
os.unlink(pickfile)
|
||||||
Connection.htab = {}
|
Connection.htab = {}
|
||||||
for h in Host.hosts.keys():
|
for h in list(Host.hosts.keys()):
|
||||||
Host.hosts[h].dyn = h in dyndnshosts
|
Host.hosts[h].dyn = h in dyndnshosts
|
||||||
Host.hosts[h].fixup()
|
Host.hosts[h].fixup()
|
||||||
for h in drophosts:
|
for h in drophosts:
|
||||||
if h in Host.hosts:
|
if h in Host.hosts:
|
||||||
del Host.hosts[h]
|
del Host.hosts[h]
|
||||||
if verbose: print "%s pickled hosts loaded" % len(Host.hosts)
|
if verbose: print(("%s pickled hosts loaded" % len(Host.hosts)))
|
||||||
else:
|
else:
|
||||||
if verbose: print "no pickled data"
|
if verbose: print("no pickled data")
|
||||||
|
|
||||||
|
|
||||||
now = time.time()
|
now = time.time()
|
||||||
@@ -930,7 +932,7 @@ if not forground:
|
|||||||
pid = os.fork()
|
pid = os.fork()
|
||||||
if pid > 0:
|
if pid > 0:
|
||||||
if verbose:
|
if verbose:
|
||||||
print "daemoinizing... pid = %d" % pid
|
print(("daemoinizing... pid = %d" % pid))
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
verbose = False
|
verbose = False
|
||||||
@@ -951,14 +953,14 @@ if not forground:
|
|||||||
try:
|
try:
|
||||||
serv = HttpServer((hbd_host, hbd_port), HttpHandler)
|
serv = HttpServer((hbd_host, hbd_port), HttpHandler)
|
||||||
except:
|
except:
|
||||||
print "failed to start server on %s:%s" % (hbd_host, hbd_port)
|
print(("failed to start server on %s:%s" % (hbd_host, hbd_port)))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
servthread = threading.Thread(target=serv.serve_forever)
|
servthread = threading.Thread(target=serv.serve_forever)
|
||||||
servthread.daemon = True
|
servthread.daemon = True
|
||||||
servthread.start()
|
servthread.start()
|
||||||
|
|
||||||
Host.dnsQ = Queue.Queue()
|
Host.dnsQ = queue.Queue()
|
||||||
dnsT = threading.Thread(target=dnsupdatethread)
|
dnsT = threading.Thread(target=dnsupdatethread)
|
||||||
dnsT.daemon = True
|
dnsT.daemon = True
|
||||||
dnsT.start()
|
dnsT.start()
|
||||||
@@ -968,13 +970,13 @@ sig = 0
|
|||||||
signal.signal(signal.SIGTERM, handler)
|
signal.signal(signal.SIGTERM, handler)
|
||||||
signal.signal(signal.SIGHUP, handler)
|
signal.signal(signal.SIGHUP, handler)
|
||||||
|
|
||||||
next = int(now)+15 # 15 seconds time to settle after (re-)start
|
rnext = int(now)+15 # 15 seconds time to settle after (re-)start
|
||||||
sleep = 1
|
sleep = 1
|
||||||
firstcheck = int(now) + 15
|
firstcheck = int(now) + 15
|
||||||
|
|
||||||
while running:
|
while running:
|
||||||
sr = None
|
sr = None
|
||||||
if DEBUG > 2: sys.stderr.write("about to sleep = %s\n" % (sleep))
|
if DEBUG > 3: sys.stderr.write("about to sleep = %s\n" % (sleep))
|
||||||
try:
|
try:
|
||||||
sr = select.select(ilist, [], [], sleep)
|
sr = select.select(ilist, [], [], sleep)
|
||||||
now = time.time()
|
now = time.time()
|
||||||
@@ -983,7 +985,7 @@ while running:
|
|||||||
running = False
|
running = False
|
||||||
closeup()
|
closeup()
|
||||||
continue
|
continue
|
||||||
except select.error, value:
|
except select.error as value:
|
||||||
if value[0] != 4: # interrupted system call
|
if value[0] != 4: # interrupted system call
|
||||||
sys.stderr.write("select err %s %s" % (select.error, value))
|
sys.stderr.write("select err %s %s" % (select.error, value))
|
||||||
#raise os.error, value
|
#raise os.error, value
|
||||||
@@ -992,7 +994,7 @@ while running:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
if DEBUG > 2: sys.stderr.write("select exception %s\n" % (str(e)))
|
if DEBUG > 2: sys.stderr.write("select exception %s\n" % (str(e)))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
if DEBUG > 2: sys.stderr.write("woke from sleep = %s (%s)\n" % (str(sr), str(ilist)))
|
if DEBUG > 3: sys.stderr.write("woke from sleep = %s (%s)\n" % (str(sr), str(ilist)))
|
||||||
for fh in sr[0]:
|
for fh in sr[0]:
|
||||||
if fh in [sock, sock6]:
|
if fh in [sock, sock6]:
|
||||||
readsock(fh)
|
readsock(fh)
|
||||||
@@ -1000,26 +1002,26 @@ while running:
|
|||||||
# serv.handle_request()
|
# serv.handle_request()
|
||||||
else:
|
else:
|
||||||
sys.stderr.write("what happend just now?\n")
|
sys.stderr.write("what happend just now?\n")
|
||||||
if DEBUG > 2: sys.stderr.write("done handling, running is %s, sig is %s\n" % (running, sig))
|
if DEBUG > 3: sys.stderr.write("done handling, running is %s, sig is %s\n" % (running, sig))
|
||||||
|
|
||||||
# check hour/day/week
|
# check hour/day/week
|
||||||
for v in xrange(3):
|
for v in range(3):
|
||||||
fm=tsfm[v]
|
fm=tsfm[v]
|
||||||
ts=time.strftime(tsfm[v], time.localtime(now))
|
ts=time.strftime(tsfm[v], time.localtime(now))
|
||||||
if ts != lastfm[v]:
|
if ts != lastfm[v]:
|
||||||
lastfm[v]=ts
|
lastfm[v]=ts
|
||||||
for h in Host.hosts.keys():
|
for h in list(Host.hosts.keys()):
|
||||||
Host.hosts[h].hdwcounts[v] = [Host.hosts[h].doesack, Host.hosts[h].upcount]
|
Host.hosts[h].hdwcounts[v] = [Host.hosts[h].doesack, Host.hosts[h].upcount]
|
||||||
|
|
||||||
if now >= next and now >= firstcheck:
|
if now >= rnext and now >= firstcheck:
|
||||||
next = now+1
|
rnext = now+1
|
||||||
checkoverdue()
|
checkoverdue()
|
||||||
|
|
||||||
sleep = next-now
|
sleep = rnext-now
|
||||||
if sleep < 0:
|
if sleep < 0:
|
||||||
sys.stderr.write("sleep is negative! %s next = %s\n" % (sleep, next))
|
sys.stderr.write("sleep is negative! %s next = %s\n" % (sleep, rnext))
|
||||||
sleep = 0
|
sleep = 0
|
||||||
if DEBUG > 2: sys.stderr.write("sleep = %s next = %s\n" % (sleep, next))
|
if DEBUG > 3: sys.stderr.write("sleep = %s next = %s\n" % (sleep, rnext))
|
||||||
|
|
||||||
if sig != 0:
|
if sig != 0:
|
||||||
setrunning(False)
|
setrunning(False)
|
||||||
|
|||||||
Reference in New Issue
Block a user