Skip to content

Commit

Permalink
Release 1.1.1
Browse files Browse the repository at this point in the history
Added Icinga2 configuration, bumped RPM spec file, cleaned-up code
  • Loading branch information
stdevel committed May 31, 2018
1 parent 5f9e5c2 commit fbecf7d
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 89 deletions.
19 changes: 19 additions & 0 deletions check_omd.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
object CheckCommand "check_omd" {
import "plugin-check-command"
command = [ "sudo", "-u", "$omd_site$", PluginDir + "/check_omd.py" ]

arguments = {
"-x" = {
value = "$omd_exclude_service$"
description = "services that should be excluded (default: none)"
}
"-w" = {
set_if = "$omd_warning_service$"
description = "services that only should throw a warning if not running (useful for fragile stuff like npcd, default: none)"
}
"-d" = {
set_if = "$omd_debug$"
description = "enable debugging outputs (default: no)"
}
}
}
214 changes: 132 additions & 82 deletions check_omd.py
Original file line number Diff line number Diff line change
@@ -1,94 +1,144 @@
#!/usr/bin/python
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# check_omd.py - a script for checking a particular
# OMD site status
#
# 2016 By Christian Stankowic
# <info at stankowic hyphen development dot net>
# https://github.com/stdevel
#
"""
check_omd.py - a script for checking a particular
OMD site status
2018 By Christian Stankowic
<info at cstan dot io>
https://github.com/stdevel/check_omd
"""

from optparse import OptionParser
import subprocess
import io
import logging

__version__ = "1.1.1"
"""
str: Program version
"""
LOGGER = logging.getLogger('check_omd')
"""
logging: Logger instance
"""



def get_site_status():
"""
Retrieves a particular site's status
"""
#get username
proc = subprocess.Popen("whoami", stdout=subprocess.PIPE)
site = proc.stdout.read().rstrip()
LOGGER.debug("It seems like I'm OMD site '%s'", site)

def getSiteStatus():
#get username
proc = subprocess.Popen("whoami", stdout=subprocess.PIPE)
site = proc.stdout.read().rstrip()
if options.debug: print "DEBUG: It seems like I'm OMD site '{0}'".format(site)

#get OMD site status
cmd = ['omd', 'status', '-b']
if options.debug: print "DEBUG: running command '{0}'".format(cmd)
proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
res, err = proc.communicate()

if err:
if "no such site" in err:
print "UNKNOWN: unable to check site: '{0}' - did you miss running this plugin as OMD site user?".format(err.rstrip())
else:
print "UNKNOWN: unable to check site: '{0}'".format(err.rstrip())
exit(3)
if res:
#try to find out whether omd was executed as root
if res.count("OVERALL") > 1:
print "UNKOWN: unable to check site, it seems this plugin is executed as root (use OMD site context!)"
exit(3)

#check all services
fail_srvs=[]
warn_srvs=[]
if options.debug: print "DEBUG: Got result '{0}'".format(res)
for line in io.StringIO(res.decode('utf-8')):
service = line.rstrip().split(" ")[0]
status = line.rstrip().split(" ")[1]
if service not in options.exclude:
#check service
if status != "0":
if service in options.warning:
if options.debug: print "{0} service marked for warning has failed state ({1})".format(service, status)
warn_srvs.append(service)
else:
fail_srvs.append(service)
if options.debug: print "{0} service has failed state ({1})".format(service, status)
else:
if options.debug: print "Ignoring '{0}' as it's blacklisted...".format(service)
if len(fail_srvs) == 0 and len(warn_srvs) == 0:
print "OK: OMD site '{0}' services are running.".format(site)
exit(0)
elif len(fail_srvs) > 0:
print "CRITICAL: OMD site '{0}' has failed service(s): '{1}'".format(site, ' '.join(fail_srvs))
exit(2)
else:
print "WARNING: OMD site '{0}' has service(s) in warning state: '{1}'".format(site, ' '.join(warn_srvs))
exit(1)
#get OMD site status
cmd = ['omd', 'status', '-b']
LOGGER.debug("running command '%s'", cmd)
proc = subprocess.Popen(
cmd, stderr=subprocess.PIPE, stdin=subprocess.PIPE, stdout=subprocess.PIPE
)
res, err = proc.communicate()

if err:
if "no such site" in err:
print "UNKNOWN: unable to check site: '{0}' - did you miss " \
"running this plugin as OMD site user?".format(err.rstrip())
else:
print "UNKNOWN: unable to check site: '{0}'".format(err.rstrip())
exit(3)
if res:
#try to find out whether omd was executed as root
if res.count("OVERALL") > 1:
print "UNKOWN: unable to check site, it seems this plugin is " \
"executed as root (use OMD site context!)"
exit(3)

#check all services
fail_srvs = []
warn_srvs = []
LOGGER.debug("Got result '%s'", res)
for line in io.StringIO(res.decode('utf-8')):
service = line.rstrip().split(" ")[0]
status = line.rstrip().split(" ")[1]
if service not in OPTIONS.exclude:
#check service
if status != "0":
if service in OPTIONS.warning:
LOGGER.debug(
"%s service marked for warning has failed" \
" state (%s)", service, status
)
warn_srvs.append(service)
else:
fail_srvs.append(service)
LOGGER.debug(
"%s service has failed state " \
"(%s)", service, status
)
else:
LOGGER.debug(
"Ignoring '%s' as it's blacklisted.", service
)
if len(fail_srvs) == 0 and len(warn_srvs) == 0:
print "OK: OMD site '{0}' services are running.".format(site)
exit(0)
elif len(fail_srvs) > 0:
print "CRITICAL: OMD site '{0}' has failed service(s): " \
"'{1}'".format(site, ' '.join(fail_srvs))
exit(2)
else:
print "WARNING: OMD site '{0}' has service(s) in warning state: " \
"'{1}'".format(site, ' '.join(warn_srvs))
exit(1)



if __name__ == "__main__":
#define description, version and load parser
desc='''%prog is used to check a particular OMD site status. By default, the script only checks a site's overall status. It is also possible to exclude particular services and only check the remaining services (e.g. rrdcached, npcd, icinga, apache, crontab).
Checkout the GitHub page for updates: https://github.com/stdevel/check_omd'''
parser = OptionParser(description=desc,version="%prog version 1.1.0")

#-d / --debug
parser.add_option("-d", "--debug", dest="debug", default=False, action="store_true", help="enable debugging outputs")

#-e / --exclude
parser.add_option("-x", "--exclude", dest="exclude", default=["OVERALL"], action="append", metavar="SERVICE", help="defines one or more services that should be excluded")

#-w / --warning
parser.add_option("-w", "--warning", dest="warning", default=[""], action="append", metavar="SERVICE", help="defines one or more services that only should throw a warning if not running (useful for fragile stuff like npcd)")

#parse arguments
(options, args) = parser.parse_args()

#debug outputs
if options.debug: print "OPTIONS: {0}".format(options)

#check site status
getSiteStatus()
#define description, version and load parser
DESC = '''%prog is used to check a particular OMD site status. By default,
the script only checks a site's overall status. It is also possible to exclude
particular services and only check the remaining services (e.g. rrdcached,
npcd, icinga, apache, crontab).
Checkout the GitHub page for updates: https://github.com/stdevel/check_omd'''
PARSER = OptionParser(description=DESC, version=__version__)

#-d / --debug
PARSER.add_option(
"-d", "--debug", dest="debug", default=False, action="store_true",
help="enable debugging outputs (default: no)"
)

#-e / --exclude
PARSER.add_option(
"-x", "--exclude", dest="exclude", default=["OVERALL"],
action="append", metavar="SERVICE", help="defines one or more " \
"services that should be excluded (default: none)"
)

#-w / --warning
PARSER.add_option(
"-w", "--warning", dest="warning", default=[""], action="append",
metavar="SERVICE", help="defines one or more services that only " \
"should throw a warning if not running (useful for fragile stuff " \
"like npcd, default: none)"
)

#parse arguments
(OPTIONS, ARGS) = PARSER.parse_args()

#set logging level
logging.basicConfig()
if OPTIONS.debug:
LOGGER.setLevel(logging.DEBUG)
else:
LOGGER.setLevel(logging.ERROR)

LOGGER.debug("OPTIONS: %s", OPTIONS)

#check site status
get_site_status()
10 changes: 5 additions & 5 deletions icingaexchange.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
name: check_omd
description: "file:///README.md"
url: "https://github.com/stdevel/check_omd"
tags: OMD,Nagios,Icinga
tags: OMD,Nagios,Icinga,Icinga2
vendor: Linux
target: Operating System
type: Plugin
license: gplv3
releases:
-
name: 1.1.0
description: "1.1.0 Release"
name: 1.1.1
description: "Release 1.1.1"
files:
-
name: check_omd.py
url: "file:///check_omd.py"
description: "Initial release"
checksum: df76dfdeedb07b8c7df52ae621b57c19
description: "Release 1.1.1"
checksum: 17235ca90db9bd7d94988c847de67418
7 changes: 5 additions & 2 deletions nagios-plugins-check_omd.spec
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ rm -rf $RPM_BUILD_ROOT


%changelog
* Mon Jun 27 2016 Christian Stankowic <info@stankowic-development.net> - 1.1.1
- updated to upstream version 1.1.0
* Thu May 31 2018 Christian Stankowic <info@cstan.io> - 1.1.1
- rebased to upstream version 1.1.1

* Mon Jun 27 2016 Christian Stankowic <info@stankowic-development.net> - 1.1.0
- updated to upstream version 1.1.0
- added -w / --warning option for excluding a service from generating critical states
- added hint when plugin executed as root

Expand Down

0 comments on commit fbecf7d

Please sign in to comment.