Commit 2e731e2b authored by Klaus-Uwe Mitterer's avatar Klaus-Uwe Mitterer

Restructured server -> Moved most functions to servertools

Started implementation of message handler
parent 77fb50b4
#!/usr/bin/env python3
import os, setuptools, socketserver, sys, syslog, threading
SYSLOG = 0
STDOUT = 1
STDDEB = 2 # STDOUT + Debug
exec("logging = %s or STDOUT" % setuptools.getSetting("Log", "sink"))
def logger(message, prio=syslog.LOG_INFO, sink=logging):
if sink in (STDOUT, STDDEB):
if prio not in (syslog.LOG_NOTICE, syslog.LOG_INFO, syslog.LOG_DEBUG):
print(message)
sys.stderr.write(message)
elif prio != syslog.LOG_DEBUG or sink == STDDEB:
print(message)
elif sink == SYSLOG:
syslog.openlog("KumiStatusServer", syslog.LOG_PID)
syslog.syslog(prio, message)
else:
try:
sys.stderr.write("Unknown logging level %s, assuming STDOUT from now on." % str(sink))
except:
pass
logging = STDOUT
logger(message, prio, logging)
def worker(message):
command = message.split()
if command[0].lower() in ("heartbeat", "hb", "ping"):
return "HB: Still here? Wow."
class TCPHandler(socketserver.StreamRequestHandler):
def readString(self):
return self.rfile.readline().strip()
def sendString(self, string):
self.request.sendall((string + "\n").encode('utf8'))
def handle(self):
remote = self.client_address[0] + ":" + str(self.client_address[1])
logger("New connection from %s." % remote, syslog.LOG_INFO)
self.sendString("HI: Kumi Status v0.8.15")
while True:
message = self.readString().decode('utf8')
if not message:
logger("Connection from %s closed." % remote, syslog.LOG_DEBUG)
break
logger("%s said: %s" % (remote, message))
response = worker(message)
if response:
self.sendString(response)
logger("Sent to %s: %s" % (remote, response), syslog.LOG_DEBUG)
class TCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
pass
def shutdown(reboot = False, status = 0):
if reboot:
args = sys.argv[:]
args.insert(0, sys.executable)
try:
os.execv(sys.executable, args)
except:
logger("Restart failed. Shutting down.")
exit(2)
else:
exit(status)
import os, setuptools, servertools, socketserver, sys, syslog, threading
if __name__ == "__main__":
socketserver.TCPServer.allow_reuse_address = True
try:
server = TCPServer((setuptools.getSetting("Server", "bind"), int(setuptools.getSetting("Server", "port"))), TCPHandler)
server = servertools.TCPServer((setuptools.getSetting("Server", "bind"), int(setuptools.getSetting("Server", "port"))), servertools.TCPHandler)
except:
logger("Unable to bind on %s:%s. Make sure the port is not in use or use a different port.")
shutdown(status=1)
logger("Kumi Status server running on %s:%s." % (setuptools.getSetting("Server", "bind"), setuptools.getSetting("Server", "port")))
servertools.logger("Unable to bind on %s:%s. Make sure the port is not in use or use a different port." % (setuptools.getSetting("Server", "bind"), setuptools.getSetting("Server", "port")))
servertools.shutdown(status=1)
servertools.logger("Kumi Status server running on %s:%s." % (setuptools.getSetting("Server", "bind"), setuptools.getSetting("Server", "port")))
try:
thread = threading.Thread(target=server.serve_forever(), daemon=True)
except KeyboardInterrupt:
logger("Caught KeyboardInterrupt. Shutting down.")
shutdown()
servertools.logger("Caught KeyboardInterrupt. Shutting down.")
servertools.shutdown()
except EOFError:
logger("Caught EOF. Shutting down.")
shutdown()
servertools.logger("Caught EOF. Shutting down.")
servertools.shutdown()
except BaseException as e:
logger(str(e), syslog.LOG_CRIT)
logger("Caught some bad exception. Trying to reload...", syslog.LOG_CRIT)
shutdown(reboot=True)
servertools.logger(str(e), syslog.LOG_CRIT)
servertools.logger("Caught some bad exception. Trying to reload...", syslog.LOG_CRIT)
servertools.shutdown(reboot=True, status=2)
#!/usr/bin/env python3
import os, setuptools, socketserver, sys, syslog, threading
SYSLOG = 0
STDOUT = 1
STDDEB = 2 # STDOUT + Debug
SILENT = 9 # Quiet mode
exec("logging = %s or STDOUT" % setuptools.getSetting("Log", "sink"))
def logger(message, prio=syslog.LOG_INFO, sink=logging):
if sink in (STDOUT, STDDEB):
if prio not in (syslog.LOG_NOTICE, syslog.LOG_INFO, syslog.LOG_DEBUG):
print(message)
sys.stderr.write(message)
elif prio != syslog.LOG_DEBUG or sink == STDDEB:
print(message)
elif sink == SYSLOG:
syslog.openlog("KumiStatusServer", syslog.LOG_PID)
syslog.syslog(prio, message)
elif sink != SILENT:
try:
sys.stderr.write("Unknown logging level %s, assuming STDOUT from now on." % str(sink))
except:
pass
logging = STDOUT
logger(message, prio, logging)
class TCPHandler(socketserver.StreamRequestHandler):
def readString(self):
return self.rfile.readline().strip()
def sendString(self, string):
self.request.sendall((string + "\n").encode('utf8'))
def requestHandler(self, request):
pass
def worker(self, message):
content = message.split()
command = content[0].lower()
if command in ("hi"):
return "HI: Kumi Status v0.8.15 (KSP)"
elif command in ("heartbeat", "hb", "ping"):
return "OK: Still here? Wow."
elif command in ("stat", "status"):
return False
elif command in ("ssl", "tls"):
return False
elif command in ("req", "request"):
return "NI: Requesting monitoring is not yet implemented."
elif command == "help":
return False
else:
return "UC: Unknown command %s." % command
def handle(self):
remote = self.client_address[0] + ":" + str(self.client_address[1])
logger("New connection from %s." % remote, syslog.LOG_INFO)
self.sendString(self.worker("hi"))
while True:
message = self.readString().decode('utf8')
if not message:
logger("Connection from %s closed." % remote, syslog.LOG_DEBUG)
break
logger("%s said: %s" % (remote, message))
response = self.worker(message)
if response:
self.sendString(response)
logger("Sent to %s: %s" % (remote, response), syslog.LOG_DEBUG)
class TCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
pass
def shutdown(reboot = False, status = 0):
if reboot:
args = sys.argv[:]
args.insert(0, sys.executable)
try:
os.execv(sys.executable, args)
except:
logger("Restart failed. Shutting down.")
exit(status)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment