From 79d4a3b4c314a250d461473b5766b8d0217fcbf2 Mon Sep 17 00:00:00 2001 From: spike Date: Thu, 28 Jun 2018 23:43:54 +0200 Subject: [PATCH 1/2] Added custom Summary class to handle status line output Added verbosity flag --- bin/pynagsystemd.py | 49 +++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/bin/pynagsystemd.py b/bin/pynagsystemd.py index 608cc5f..e3b8b94 100755 --- a/bin/pynagsystemd.py +++ b/bin/pynagsystemd.py @@ -30,13 +30,13 @@ def probe(self): raise nagiosplugin.CheckError(err) if pres: - result = "" for line in io.StringIO(pres.decode('utf-8')): - result = "%s %s" % (result, line.split(' ')[0]) - - return [nagiosplugin.Metric('systemd', (False, result), context='systemd')] - - return [nagiosplugin.Metric('systemd', (True, None), context='systemd')] + split_line = line.split() + unit = split_line[0] + active = split_line[2] + yield nagiosplugin.Metric(unit, active, context='systemd') + else: + yield nagiosplugin.Metric('all', None, context='systemd') class ServiceStatus(nagiosplugin.Resource): @@ -60,45 +60,50 @@ def probe(self): if err: raise nagiosplugin.CheckError(err) if pres: - result = "" for line in io.StringIO(pres.decode('utf-8')): - result = "%s %s" % (result, line.split(' ')[0]) - result = result.strip() - if result == "active": - return [nagiosplugin.Metric('systemd', (True, None), context='systemd')] - else: - return [nagiosplugin.Metric('systemd', (False, self.service), context='systemd')] - - return [nagiosplugin.Metric('systemd', (False, "No Service given"), context='systemd')] + yield nagiosplugin.Metric(self.service, line.strip(), context='systemd') class SystemdContext(nagiosplugin.Context): + def __init__(self): super(SystemdContext, self).__init__('systemd') def evaluate(self, metric, resource): - value, output = metric.value - if value: - return self.result_cls(nagiosplugin.Ok, metric=metric) + hint = '%s: %s' % (metric.name, metric.value) if metric.value else metric.name + if metric.value and metric.value != 'active': + return self.result_cls(nagiosplugin.Critical, metric=metric, hint=hint) else: - return self.result_cls(nagiosplugin.Critical, metric=metric, hint='failed units: %s' % output) + return self.result_cls(nagiosplugin.Ok, metric=metric, hint=hint) + + +class SystemdSummary(nagiosplugin.Summary): + + def problem(self, results): + return ', '.join(['{0}'.format(result) for result in results.most_significant]) + + def verbose(self, results): + return ['{0}: {1}'.format(result.state, result) for result in results] def main(): parser = argparse.ArgumentParser() parser.add_argument("-s", "--service", type=str, dest="service", help="Name of the Service that is beeing tested") + parser.add_argument('-v', '--verbose', action='count', default=0, help='Increase output verbosity (use up to 3 times)') args = parser.parse_args() if args.service is None: check = nagiosplugin.Check( SystemdStatus(), - SystemdContext()) + SystemdContext(), + SystemdSummary()) else: check = nagiosplugin.Check( ServiceStatus(service=args.service), - SystemdContext()) - check.main() + SystemdContext(), + SystemdSummary()) + check.main(args.verbose) if __name__ == '__main__': From f4e38b49eae106030649dfc20aec93924a309a53 Mon Sep 17 00:00:00 2001 From: spike Date: Thu, 28 Jun 2018 23:52:07 +0200 Subject: [PATCH 2/2] Renamed variables to stdout and stderr --- bin/pynagsystemd.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/bin/pynagsystemd.py b/bin/pynagsystemd.py index e3b8b94..33fb64b 100755 --- a/bin/pynagsystemd.py +++ b/bin/pynagsystemd.py @@ -22,15 +22,15 @@ def probe(self): stderr=subprocess.PIPE, stdin=subprocess.PIPE, stdout=subprocess.PIPE) - pres, err = p.communicate() + stdout, stderr = p.communicate() except OSError as e: raise nagiosplugin.CheckError(e) - if err: - raise nagiosplugin.CheckError(err) + if stderr: + raise nagiosplugin.CheckError(stderr) - if pres: - for line in io.StringIO(pres.decode('utf-8')): + if stdout: + for line in io.StringIO(stdout.decode('utf-8')): split_line = line.split() unit = split_line[0] active = split_line[2] @@ -53,14 +53,14 @@ def probe(self): stderr=subprocess.PIPE, stdin=subprocess.PIPE, stdout=subprocess.PIPE) - pres, err = p.communicate() + stdout, stderr = p.communicate() except OSError as e: raise nagiosplugin.CheckError(e) - if err: - raise nagiosplugin.CheckError(err) - if pres: - for line in io.StringIO(pres.decode('utf-8')): + if stderr: + raise nagiosplugin.CheckError(stderr) + if stdout: + for line in io.StringIO(stdout.decode('utf-8')): yield nagiosplugin.Metric(self.service, line.strip(), context='systemd')