feat: Add check_ping

Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
Julien Riou 2023-09-21 19:58:26 +02:00
parent 2ced5e5a40
commit 234c6c46db
No known key found for this signature in database
GPG key ID: A2EB1F2CA8E3F677
5 changed files with 127 additions and 2 deletions

View file

@ -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:

View file

@ -1 +1 @@
1.0.0
1.1.0

86
check_ping Executable file
View file

@ -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()

View file

@ -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__)

View file

@ -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(