[WIP] Initial version
commit
82b4ba4c71
@ -0,0 +1,67 @@
|
|||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
import argparse
|
||||||
|
from enums import Enum
|
||||||
|
|
||||||
|
"""
|
||||||
|
CheckLib: a simple wrapper around Icinga checks dealing with common
|
||||||
|
code like correct error codes and output formatting
|
||||||
|
|
||||||
|
The main class is `Check`, other classes are just helpers.
|
||||||
|
"""
|
||||||
|
|
||||||
|
class Result(Enum):
|
||||||
|
"""
|
||||||
|
Enumeration of possible check results.
|
||||||
|
|
||||||
|
The values are related return codes.
|
||||||
|
"""
|
||||||
|
OK = 0
|
||||||
|
WARNING = 1
|
||||||
|
CRITICAL = 2
|
||||||
|
UNKNOWN = 3
|
||||||
|
|
||||||
|
class Check:
|
||||||
|
"""
|
||||||
|
The class representing the check.
|
||||||
|
|
||||||
|
Expected usage is by invoking the `run` classmethod, which overtakes
|
||||||
|
the control, performs the check and returns the correct return
|
||||||
|
value.
|
||||||
|
|
||||||
|
The sequence is following:
|
||||||
|
1. run: Runs the check:
|
||||||
|
1. __init__: initialization
|
||||||
|
- Creation of default values in the instance
|
||||||
|
- Preparation of argument parser (TODO: Do we want this here?)
|
||||||
|
1. prepare_argparse: Optionally tweaks argparse's
|
||||||
|
ArgumentParser, e.g. adding more parameters
|
||||||
|
1. self.args = argparse.parse_args(sys.argv)
|
||||||
|
1. check(): the function to check state and populate
|
||||||
|
self.result, self.short_message and so on.
|
||||||
|
1. sys.exit with the relevant exit code according to
|
||||||
|
self.result
|
||||||
|
- If anything fails, e.g. by raising an exception,
|
||||||
|
the run method returns the UNKNOWN result and
|
||||||
|
uses the traceback as message.
|
||||||
|
|
||||||
|
Instead of using self.check, the default implementation invokes
|
||||||
|
measure method and compares its value to thresholds given either
|
||||||
|
by semi-standard arguments, or defaults defined in the class.
|
||||||
|
(If neither method is defined, the check fails. In other words,
|
||||||
|
you need to either implement measure, or override check.)
|
||||||
|
|
||||||
|
FIXME: It is currently not documented, which attributes of the instance
|
||||||
|
are used.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def run(cls, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
This method implements the checking start-to-end.
|
||||||
|
|
||||||
|
The arguments, if any, are passed to the __init__ of the class.
|
||||||
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
@ -0,0 +1,14 @@
|
|||||||
|
from checklib import Check, Result
|
||||||
|
|
||||||
|
class MyCheck(Check):
|
||||||
|
def check(self):
|
||||||
|
self.result.state = Result.UNKNOWN
|
||||||
|
self.result.short_message = 'I don\'t know :-)'
|
||||||
|
|
||||||
|
def add_arguments(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Collect argparse arguments at self.arguments
|
||||||
|
|
||||||
|
# If defining __init__, call super().__init__ first -- it populates
|
||||||
|
# self.result, self.argumentparser and so on.
|
Loading…
Reference in New Issue