Skip to content

Commit

Permalink
Merge pull request #4 from Stunkymonkey/single-service-check
Browse files Browse the repository at this point in the history
Added Parsing option for testing a single Systemd-service
  • Loading branch information
kbytesys authored Sep 26, 2016
2 parents eb4f5da + 0ef5153 commit d4ffb8f
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions bin/pynagsystemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""
import io
import subprocess
import argparse

import nagiosplugin

Expand Down Expand Up @@ -39,6 +40,36 @@ def probe(self):
return [nagiosplugin.Metric('systemd', (True, None), context='systemd')]


class ServiceStatus(nagiosplugin.Resource):
name = 'SYSTEMD'

def probe(self):
# Execute systemctl is-active and get output
global service
try:
p = subprocess.Popen(['systemctl', 'is-active', service],
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
pres, err = 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, service), context='systemd')]

return [nagiosplugin.Metric('systemd', (False, "No Service given"), context='systemd')]


class SystemdContext(nagiosplugin.Context):
def __init__(self):
super(SystemdContext, self).__init__('systemd')
Expand All @@ -52,10 +83,22 @@ def evaluate(self, metric, resource):


def main():
check = nagiosplugin.Check(
SystemdStatus(),
SystemdContext())

global service

parser = argparse.ArgumentParser()
parser.add_argument("-s", "--service", type=str, dest="service", help="Name of the Service that is beeing tested")

args = parser.parse_args()
service = str(args.service)

if service == "None":
check = nagiosplugin.Check(
SystemdStatus(),
SystemdContext())
else:
check = nagiosplugin.Check(
ServiceStatus(),
SystemdContext())
check.main()


Expand Down

0 comments on commit d4ffb8f

Please sign in to comment.