Skip to content

Commit

Permalink
Merge pull request #6 from eLvErDe/master
Browse files Browse the repository at this point in the history
Remove global service variable
  • Loading branch information
kbytesys authored Jul 14, 2017
2 parents ba9fe68 + daeb929 commit ba2efbe
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions bin/pynagsystemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
You can redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation, either version 2
of the License.
Copyright Andrea Briganti a.k.a 'Kbyte'
"""
import io
Expand Down Expand Up @@ -43,11 +42,14 @@ def probe(self):
class ServiceStatus(nagiosplugin.Resource):
name = 'SYSTEMD'

def __init__(self, *args, **kwargs):
self.service = kwargs.pop('service')
super(nagiosplugin.Resource, self).__init__(*args, **kwargs)

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

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

Expand All @@ -83,21 +85,18 @@ def evaluate(self, metric, resource):


def main():
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":
if args.service is None:
check = nagiosplugin.Check(
SystemdStatus(),
SystemdContext())
else:
check = nagiosplugin.Check(
ServiceStatus(),
ServiceStatus(service=args.service),
SystemdContext())
check.main()

Expand Down

0 comments on commit ba2efbe

Please sign in to comment.