Skip to content

Commit

Permalink
Merge pull request #7 from spike77453/master
Browse files Browse the repository at this point in the history
Improve status line output
  • Loading branch information
kbytesys authored Jul 30, 2018
2 parents 98509c7 + f4e38b4 commit a984887
Showing 1 changed file with 37 additions and 32 deletions.
69 changes: 37 additions & 32 deletions bin/pynagsystemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ 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:
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')]
if stdout:
for line in io.StringIO(stdout.decode('utf-8')):
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):
Expand All @@ -53,52 +53,57 @@ 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:
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')]
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')


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__':
Expand Down

0 comments on commit a984887

Please sign in to comment.