diff --git a/check_d2gs/check_d2gs.py b/check_d2gs/check_d2gs.py new file mode 100755 index 0000000..e9563c0 --- /dev/null +++ b/check_d2gs/check_d2gs.py @@ -0,0 +1,65 @@ +#!/bin/env python3 + +import socket +import sys +from time import sleep +import argparse + +DEFAULT_D2GS_ADMIN_PORT = 8888 +DEFAULT_D2GS_PASSWORD = "abcd123" + +def check_d2gs(host: str, port: int, pwd: str) -> int: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + server_address = (host, port) + sock.connect(server_address) + sock.setblocking(False) + ret = 0 + output = "" + + try: + sock.sendall(pwd.encode('utf-8') + b"\n") + sleep(0.1) + sock.recv(1024) + sock.sendall("status".encode('utf-8') + b"\n") + sleep(0.1) + output = sock.recv(1024).decode() + sock.sendall("exit".encode('utf-8') + b"\n") + sleep(0.1) + sock.recv(1024) + except socket.error as e: + print ("Socket error: %s" %str(e)) + ret = 3 + + ret = 0 + found = 0 + for line in output.split("\n"): + if line.startswith("Connetion to"): + found += 1 + if line.split(": ")[1].rstrip() != "connected": + ret = 2 + + if ret == 2 or found != 2: + print(f"D2GS - CRITICAL: {output}") + else: + print("D2GS - OK") + + sock.close() + return ret + + +def main() -> int: + parser = argparse.ArgumentParser(description='D2GS checker') + parser.add_argument('--port', action="store", dest="port", type=int, required=False, default=DEFAULT_D2GS_ADMIN_PORT) + parser.add_argument('--host', action="store", dest="host", type=str, required=True) + parser.add_argument('--pass', action="store", dest="pwd", type=str, required=False, default=DEFAULT_D2GS_PASSWORD) + args = parser.parse_args() + + if args.host is None: + print("Missing host") + return 3 + + return check_d2gs(args.host, args.port, args.pwd) + + +if __name__ == '__main__': + exit(main())