add basic template
This commit is contained in:
119
templates/check_.py
Executable file
119
templates/check_.py
Executable file
@@ -0,0 +1,119 @@
|
||||
#!/usr/bin/env python3
|
||||
""" Template python check"""
|
||||
"""dependencys:
|
||||
- pip3 install nagiosplugin
|
||||
- pip3 install argparse
|
||||
"""
|
||||
|
||||
__version__ = '0.0.1'
|
||||
__author__ = 'anima'
|
||||
|
||||
# imports
|
||||
import logging
|
||||
import argparse
|
||||
import nagiosplugin
|
||||
|
||||
# log settings
|
||||
logging.basicConfig(format='[%(asctime)s] %(levelname)s %(message)s', level=logging.INFO)
|
||||
|
||||
# TEMPLATE TODO: Rename Resource Class(es)
|
||||
class TEMPLATEResource(nagiosplugin.Resource):
|
||||
def __init__(self, session) -> None:
|
||||
self.session = session
|
||||
|
||||
def probe(self) -> list:
|
||||
"""template check
|
||||
|
||||
# TEMPLATE TODO: edit check description and docstring
|
||||
Returns:
|
||||
list[nagisplugin.Metric]: multiple metric elements (yield)
|
||||
or
|
||||
nagiosplugin.Metric: single metric element (return)
|
||||
"""
|
||||
return nagiosplugin.Metric(name='metric_name', value=int(result.value), context='snmp_scalar_context')
|
||||
|
||||
|
||||
|
||||
class TEMPLATEResult(nagiosplugin.Result):
|
||||
def __str__(self):
|
||||
return f'{self.metric.value}'
|
||||
|
||||
|
||||
class TEMPLATEContext(nagiosplugin.Context):
|
||||
def __init__(self, name):
|
||||
super().__init__(name, fmt_metric='{name} is', result_cls=SNMPSynologySystemResult)
|
||||
|
||||
def evaluate(self, metric, resource):
|
||||
if metric.value['status'] == '2':
|
||||
return self.result_cls(nagiosplugin.Critical, "critical", metric)
|
||||
elif metric.value['status'] == '1':
|
||||
return self.result_cls(nagiosplugin.Ok, "ok", metric)
|
||||
return self.result_cls(nagiosplugin.Unknown, "unknown", metric)
|
||||
|
||||
|
||||
class TEMPLATESummary(nagiosplugin.Summary):
|
||||
def verbose(self, results):
|
||||
return 'Some Summary'
|
||||
|
||||
def ok(self, results):
|
||||
return
|
||||
|
||||
def problem(self, results):
|
||||
return
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
"""evaluates given arguments
|
||||
|
||||
Returns:
|
||||
argsparse.Namespace: Namespace Object with all arguments insert (use: args.long_name_of_argument)
|
||||
"""
|
||||
argp = argparse.ArgumentParser(description=__doc__)
|
||||
# Default args
|
||||
argp.add_argument('-v', '--verbose', action='count', default=0,
|
||||
help='increase output verbosity (use up to 3 times)')
|
||||
argp.add_argument('-H', '--hostname', default='localhost',
|
||||
help='IP address or hostname of device to query')
|
||||
argp.add_argument('-t', '--timeout', default=30,
|
||||
help='abort execution after TIMEOUT seconds')
|
||||
# TEMPLATE TODO: edit choices
|
||||
argp.add_argument('-m', '--check_mode',
|
||||
choices=[
|
||||
'template1',
|
||||
'template2',
|
||||
],
|
||||
help='check mode to run')
|
||||
|
||||
# Nagios args / see https://nagios-plugins.org/doc/guidelines.html#THRESHOLDFORMAT
|
||||
argp.add_argument('-w', '--warning', default=':80',
|
||||
help='warning threshold')
|
||||
argp.add_argument('-c', '--critical', default=':90',
|
||||
help='critical threshold')
|
||||
|
||||
|
||||
args = argp.parse_args()
|
||||
return args
|
||||
|
||||
# @nagiosplugin.guarded(verbose=0)
|
||||
def main():
|
||||
args = parse_args()
|
||||
if args.verbose >= 3:
|
||||
logging.getLogger().setLevel(logging.DEBUG)
|
||||
|
||||
# TEMPLATE TODO: Rename Resource and Context and edit cases (see check_mode choices)
|
||||
# dice which check will be run bases on check_mode
|
||||
match args.check_mode:
|
||||
case 'template1':
|
||||
check = nagiosplugin.Check(Resource(session=session),
|
||||
nagiosplugin.Context(name='context'),
|
||||
nagiosplugin.ScalarContext(name='scalar_context', warning=args.warning, critical=args.critical),
|
||||
nagiosplugin.Summary())
|
||||
# TEMPLATE TODO: rename check
|
||||
check.name = "Template"
|
||||
case _:
|
||||
raise nagiosplugin.CheckError(f'Unknown check mode: {args.check_mode}')
|
||||
|
||||
check.main(args.verbose, args.timeout)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user