From 234c6c46dbcfb9cc1dc153020fac61ca4fcead0c Mon Sep 17 00:00:00 2001 From: Julien Riou Date: Thu, 21 Sep 2023 19:58:26 +0200 Subject: [PATCH] feat: Add check_ping Signed-off-by: Julien Riou --- README.md | 15 +++++++++ VERSION | 2 +- check_ping | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ check_voip | 2 +- lib/__init__.py | 24 ++++++++++++++ 5 files changed, 127 insertions(+), 2 deletions(-) create mode 100755 check_ping diff --git a/README.md b/README.md index 05e86a4..46a0529 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,21 @@ pip install -r requirements.txt Create OVHcloud API tokens following [this guide](https://github.com/ovh/python-ovh). +# check_ping + +Ensures basic configuration has succeeded. This command can be defined as +dependency of all other ones. + +``` +./check_ping --help +``` + +Example of configuration: + +``` +command[check_ping]=/opt/check_ovhcloud/check_ping +``` + # check_voip For each VoIP lines associated to the account, detect the last registration time: diff --git a/VERSION b/VERSION index 3eefcb9..9084fa2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.0.0 +1.1.0 diff --git a/check_ping b/check_ping new file mode 100755 index 0000000..a5d0ebc --- /dev/null +++ b/check_ping @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 + +import argparse +import logging +import sys +import ovh + +from lib import setup_logging, show_version, BooleanContext + +from nagiosplugin import ( + Check, + Metric, + Resource, + Summary, +) +from nagiosplugin.state import Unknown + +logger = logging.getLogger(__name__) + + +def parse_arguments(): + parser = argparse.ArgumentParser() + parser.add_argument( + "-v", + "--verbose", + dest="loglevel", + action="store_const", + const=logging.INFO, + help="Print more output", + ) + parser.add_argument( + "-d", + "--debug", + dest="loglevel", + action="store_const", + const=logging.DEBUG, + default=logging.WARNING, + help="Print even more output", + ) + + parser.add_argument( + "--version", + dest="show_version", + action="store_true", + help="Print version and exit", + ) + + args = parser.parse_args() + return args + + +class Ping(Resource): + def __init__(self, client): + self.client = client + + def probe(self): + try: + me = self.client.get(f"/me") + return [Metric(me["firstname"], True, context="ping")] + except Exception as err: + return [Metric(str(err), False, context="ping")] + + +def main(): + args = parse_arguments() + setup_logging(args) + + if args.show_version: + show_version() + return + + try: + check = Check( + Ping(client=ovh.Client()), + BooleanContext("ping", expected=True, critical=True), + Summary(), + ) + check.main() + except Exception as err: + print(f"Failed to execute check: {str(err)}") + logger.debug(err, exc_info=True) + sys.exit(Unknown.code) + + +if __name__ == "__main__": + main() diff --git a/check_voip b/check_voip index 4c346b6..56d89f5 100755 --- a/check_voip +++ b/check_voip @@ -15,7 +15,7 @@ from nagiosplugin import ( ScalarContext, Summary, ) -from nagiosplugin.state import Critical, Ok, Unknown, Warn +from nagiosplugin.state import Unknown logger = logging.getLogger(__name__) diff --git a/lib/__init__.py b/lib/__init__.py index 384feb9..f5c8f15 100644 --- a/lib/__init__.py +++ b/lib/__init__.py @@ -1,6 +1,30 @@ import logging import os +from nagiosplugin import Context +from nagiosplugin.state import Critical, Ok, Unknown, Warn + + +class BooleanContext(Context): + def __init__(self, name, expected=True, warning=False, critical=False): + super().__init__(name) + self.expected = expected + self.warning = warning + self.critical = critical + + def evaluate(self, metric, resource): + if not metric.value is self.expected: + result_type = Ok + if self.critical: + result_type = Critical + elif self.warning: + result_type = Warn + return self.result_cls( + result_type, f"{metric.name} is not {self.expected}", metric + ) + else: + return self.result_cls(Ok, None, metric) + def show_version(): with open(