Commit 5f26e3fa319ca5c71f849bcb0fa40196e6d54991

Authored by Imanol-Mikel Barba Sabariego
1 parent 81884b67

Adding check_d2gs

Showing 1 changed file with 65 additions and 0 deletions
check_d2gs/check_d2gs.py 0 → 100755
  1 +#!/bin/env python3
  2 +
  3 +import socket
  4 +import sys
  5 +from time import sleep
  6 +import argparse
  7 +
  8 +DEFAULT_D2GS_ADMIN_PORT = 8888
  9 +DEFAULT_D2GS_PASSWORD = "abcd123"
  10 +
  11 +def check_d2gs(host: str, port: int, pwd: str) -> int:
  12 + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  13 + server_address = (host, port)
  14 + sock.connect(server_address)
  15 + sock.setblocking(False)
  16 + ret = 0
  17 + output = ""
  18 +
  19 + try:
  20 + sock.sendall(pwd.encode('utf-8') + b"\n")
  21 + sleep(0.1)
  22 + sock.recv(1024)
  23 + sock.sendall("status".encode('utf-8') + b"\n")
  24 + sleep(0.1)
  25 + output = sock.recv(1024).decode()
  26 + sock.sendall("exit".encode('utf-8') + b"\n")
  27 + sleep(0.1)
  28 + sock.recv(1024)
  29 + except socket.error as e:
  30 + print ("Socket error: %s" %str(e))
  31 + ret = 3
  32 +
  33 + ret = 0
  34 + found = 0
  35 + for line in output.split("\n"):
  36 + if line.startswith("Connetion to"):
  37 + found += 1
  38 + if line.split(": ")[1].rstrip() != "connected":
  39 + ret = 2
  40 +
  41 + if ret == 2 or found != 2:
  42 + print(f"D2GS - CRITICAL: {output}")
  43 + else:
  44 + print("D2GS - OK")
  45 +
  46 + sock.close()
  47 + return ret
  48 +
  49 +
  50 +def main() -> int:
  51 + parser = argparse.ArgumentParser(description='D2GS checker')
  52 + parser.add_argument('--port', action="store", dest="port", type=int, required=False, default=DEFAULT_D2GS_ADMIN_PORT)
  53 + parser.add_argument('--host', action="store", dest="host", type=str, required=True)
  54 + parser.add_argument('--pass', action="store", dest="pwd", type=str, required=False, default=DEFAULT_D2GS_PASSWORD)
  55 + args = parser.parse_args()
  56 +
  57 + if args.host is None:
  58 + print("Missing host")
  59 + return 3
  60 +
  61 + return check_d2gs(args.host, args.port, args.pwd)
  62 +
  63 +
  64 +if __name__ == '__main__':
  65 + exit(main())
... ...