Blame view

check_d2gs/check_d2gs.py 1.61 KB
Imanol-Mikel Barba Sabariego authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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())