Replace portsopen.py, siteup.py, sksup.py and sslexpiry.py by twitter.py

This commit is contained in:
Klaus-Uwe Mitterer 2017-06-23 21:52:50 +02:00
parent feac1be289
commit d82ec2a995
8 changed files with 74 additions and 23 deletions

View file

@ -1,8 +1,11 @@
import urllib.request, urllib.error
def getStatus(url):
if not "://" in url:
url = "http://" + url
req = urllib.request.Request(url, data=None, headers={'User-Agent': 'KumiStatus/0.8.15 (+https://kumig.it/kumitterer/kumistatus)'})
try:
return urllib.request.urlopen(url).getcode()
return urllib.request.urlopen(req).getcode()
except urllib.error.HTTPError as e:
return e.code
except urllib.error.URLError:

View file

@ -2,6 +2,8 @@
import time, setuptools, httptools, twitools
arg = "w"
def check(site, recipient, two = twitools.twObject()):
status = httptools.getStatus(site)
if not status == 200:
@ -10,7 +12,7 @@ def check(site, recipient, two = twitools.twObject()):
else:
two.tweet("@%s Site %s returns status code %s!" % (recipient, site, status))
if __name__ == "__main__":
def run():
sites = setuptools.getListSetting("HTTP", "sites")
for s in sites:

View file

@ -2,13 +2,15 @@
import time, setuptools, porttools, twitools
arg = "p"
def check(host, port, recipient, two = twitools.twObject()):
if not porttools.isPortOpen(host, port):
time.sleep(retry)
if not porttools.isPortOpen(host, port):
two.tweet("@%s Port %s is not open on host %s!" % (recipient, port, host))
if __name__ == "__main__":
def run():
hosts = setuptools.getListSetting("Ports", "hosts")
retry = int(setuptools.getSetting("Ports", "retry"))

View file

@ -3,15 +3,15 @@
import setuptools, servertools, socketserver, syslog, threading
if __name__ == "__main__":
socketserver.TCPServer.allow_reuse_address = True
servertools.TCPServer.allow_reuse_address = True
bind = setuptools.getSetting("Server", "bind") or "0.0.0.0"
port = int(setuptools.getSetting("Server", "port")) or 5747
try:
server = servertools.TCPServer((bind, port), servertools.TCPHandler)
except:
servertools.logger("Unable to bind on %s:%i. Make sure the port is not in use or use a different port." % (bind, port))
print("Unable to bind on %s:%i. Make sure the port is not in use or use a different port." % (bind, port))
servertools.shutdown(status=1)
servertools.logger("Kumi Status server running on %s:%i." % (bind, port))
print("Kumi Status server running on %s:%i." % (bind, port))
try:
thread = threading.Thread(target=server.serve_forever(), daemon=True)

View file

@ -1,5 +1,6 @@
import datetime
import encodings.idna
import httptools
import os
import porttools
import setuptools
@ -19,6 +20,7 @@ SILENT = 9 # Quiet mode
SSL = 0
PORT = 1
SKS = 2
HTTP = 3
logging = STDOUT
@ -47,7 +49,7 @@ def logger(message, prio=syslog.LOG_INFO, sink=logging):
def listIncluded(host, section):
if not setuptools.getListSetting("Server", "ignorelist"):
for i in setuptools.getListSetting("SSL" if section == SSL else "Ports" if section == PORT else "SKS", "hosts"):
for i in setuptools.getListSetting("HTTP" if section == HTTP else "SSL" if section == SSL else "Ports" if section == PORT else "SKS", "sites" if section == HTTP else "hosts"):
if encodings.idna.ToASCII(i[0].lower()).decode("UTF-8") == encodings.idna.ToASCII(host.lower()).decode("UTF-8"):
return True
return False
@ -120,7 +122,7 @@ class TCPHandler(socketserver.StreamRequestHandler):
host = encodings.idna.ToASCII(str(content[1])).decode("UTF-8")
if listIncluded(host, SKS):
if skstools.getStatus(host):
return "OK: SKS is running on %s and included in the pools." % content[1]
return "OK: SKS is running on %s and included in the pools." % content[0]
else:
return "ER: The SKS keyserver at %s is not included in the pools." % content[1]
else:
@ -132,6 +134,19 @@ class TCPHandler(socketserver.StreamRequestHandler):
elif command == "help":
return "UA: Not currently implemented."
elif command in ("http", "web"):
site = encodings.idna.ToASCII(str(content[1])).decode("UTF-8")
if listIncluded(site, HTTP):
status = httptools.getStatus(site)
if status == 200:
return "OK: Site %s seems to be working fine." % content[1]
elif status == None:
return "ER: Site %s looks down from here!" % content[1]
else:
return "ER: Site %s returns status code %s!" % (content[1], status)
else:
return "NM: %s is not being monitored!" % content[1]
else:
return "IM: Unknown command %s." % command
@ -141,6 +156,7 @@ class TCPHandler(socketserver.StreamRequestHandler):
return "IM: Invalid values passed to %s. Try HELP %s." % (command, command)
def handle(self):
try:
remote = self.client_address[0] + ":" + str(self.client_address[1])
logger("New connection from %s." % remote, syslog.LOG_INFO)
self.sendString(self.worker("hi"))
@ -150,10 +166,15 @@ class TCPHandler(socketserver.StreamRequestHandler):
logger("Connection from %s closed." % remote, syslog.LOG_DEBUG)
break
logger("%s said: %s" % (remote, message))
try:
response = self.worker(message)
except:
response = "EM: Something went terribly wrong. Sorry about that."
if response:
self.sendString(response)
logger("Sent to %s: %s" % (remote, response), syslog.LOG_DEBUG)
except Exception as e:
logger(e, syslog.LOG_CRIT)
class TCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
pass

View file

@ -2,11 +2,13 @@
import time, setuptools, skstools, twitools
arg = "k"
def check(server, recipient, two = twitools.twObject()):
if skstools.getStatus(server) == False:
two.tweet("@%s Something seems to be wrong with the %s keyserver!" % (recipient, site))
if __name__ == "__main__":
def run():
servers = setuptools.getListSetting("SKS", "servers")
for s in servers:
check(s[0], s[1])

View file

@ -2,6 +2,8 @@
import logging, datetime, setuptools, ssltools, twitools
arg = "s"
def getExpiry(host, port, notify, two = twitools.twObject()):
pbefore = int(setuptools.getSetting("SSL", "pbefore"))
pafter = int(setuptools.getSetting("SSL", "pafter"))
@ -18,7 +20,7 @@ def getExpiry(host, port, notify, two = twitools.twObject()):
logging.exception("Could not verify certificate.")
two.tweet("@%s Could not verify SSL certificate on %s:%i. Is the server down?" % (notify, host, port))
if __name__ == "__main__":
def run():
hosts = setuptools.getListSetting("SSL", "hosts")
for h in hosts:

19
twitter.py Executable file
View file

@ -0,0 +1,19 @@
#!/usr/bin/env python3
import argparse, importlib, setuptools
p = argparse.ArgumentParser(description="Kumi Status Twitter Helpers")
for mod in setuptools.getListSetting("KumiStatus", "modules"):
try:
name = mod + ".twitter"
temp = importlib.import_module(name)
p.add_argument("--" + mod, "-" + temp.arg, action="store_const", const=temp)
except Exception as e:
print(e)
args = p.parse_args()
for arg in vars(args):
if vars(args)[arg]:
vars(args)[arg].run()