From cb17e8adae17d6b75216b3f47d350268a3a11027 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sun, 14 Apr 2019 20:30:04 +0200 Subject: [PATCH] Fix DF check --- README.md | 1 + check-cli.py | 49 +++++++++++++++++++++++++++++++++++++------------ check.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 92eae43..81b6956 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ our new monitoring system that does not suck. - history support -> possible grafana interface - easy to create and extend checks - Requirements: python3 + binaries for certain checks +- Minimal core logic - checks can check "anything" ### Planned features (game) diff --git a/check-cli.py b/check-cli.py index b92b55c..a12756e 100644 --- a/check-cli.py +++ b/check-cli.py @@ -2,18 +2,43 @@ import check +import argparse +import logging -checks = [] - -checks.append(check.DNSCheck("www.ungleich.ch")) -checks.append(check.DNSCheck("www.ungleich.ch", expected_result="2a0a:e5c0:0:2:400:b3ff:fe39:795c")) -checks.append(check.DNSCheck("www.ungleich.ch", rr_type="A", expected_result="185.203.112.17")) - -for num_server in range(1,6): - server="d{}.ungleich.ch".format(num_server) - - checks.append(check.DNSCheck("www.ungleich.ch", server=server)) +logging.basicConfig() +log = logging.getLogger(None) -for c in checks: - print("{} {}".format(c, c.check())) +def do_checks(): + checks = [] + + checks.append(check.DNSCheck("www.ungleich.ch")) + checks.append(check.DNSCheck("www.ungleich.ch", expected_result="2a0a:e5c0:0:2:400:b3ff:fe39:795c")) + checks.append(check.DNSCheck("www.ungleich.ch", rr_type="A", expected_result="185.203.112.17")) + + for num_server in range(1,6): + server="d{}.ungleich.ch".format(num_server) + + checks.append(check.DNSCheck("www.ungleich.ch", server=server)) + checks.append(check.DFCheck(server)) + + + for c in checks: + print("{} {}".format(c, c.check())) + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='checks') + parser.add_argument('--debug', help='Enable debug logging', action='store_true') + parser.add_argument('--verbose', help='Enable verbose logging', action='store_true') + + args = parser.parse_args() + + if args.debug: + log.setLevel(logging.DEBUG) + print("dbg") + elif args.verbose: + log.setLevel(logging.INFO) + else: + log.setLevel(logging.WARNING) + + do_checks() diff --git a/check.py b/check.py index 0fc01fa..a7f460c 100644 --- a/check.py +++ b/check.py @@ -1,4 +1,8 @@ import subprocess +import logging + +logging.basicConfig() +log = logging.getLogger("checks") class CheckException(Exception): pass @@ -82,3 +86,37 @@ class DNSCheck(BaseCheck): name = "{}@{}".format(name, self.server) return name + + +class DFCheck(BaseCheck): + def __init__(self, name, username="root", path="/"): + self.name = name + self.username = username + self.path = path + + self.command = self.create_command() + log.info("Command = {}".format(self.command)) + + def create_command(self): + base_command ='ssh {}@{}'.format(self.username, self.name).split() + base_command.append("df {}".format(self.path)) + + return base_command + + def check(self): + res = subprocess.run(self.command, + capture_output=True, + encoding="utf-8") + + if not res.returncode == 0: + return (False, "") + + return (True, res.stdout) + + def __repr__(self): + return "".format(self.__str__()) + + def __str__(self): + name = "{}:{}".format(self.name, self.path) + + return name