kumistatus/servertools/__init__.py

84 lines
2.3 KiB
Python
Raw Normal View History

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)