From: Michael Howe Date: Sat, 9 Dec 2017 20:41:04 +0000 (+0000) Subject: Make check_monit more functional X-Git-Tag: 0.23~16 X-Git-Url: https://git.michaelhowe.org/gitweb/?a=commitdiff_plain;h=74c29927f7d8415d8353a4a81ffc2ebf51ccf921;p=packages%2Fn%2Fnagios-plugins-local.git Make check_monit more functional * handle (one) urllib error * list which services are unmonitored, rather than just the first --- diff --git a/debian/changelog b/debian/changelog index 234dcce..bda9c8b 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,12 @@ +nagios-plugins-local (0.17~test.0) UNRELEASED; urgency=medium + + * check_monit: + - improve various aspects: + + list all unmonitored services, not just the first + + exit with unknown state if can't connect to monit's web interface + + -- Michael Howe Sat, 09 Dec 2017 20:29:00 +0000 + nagios-plugins-local (0.16) unstable; urgency=medium * check_configtool: diff --git a/plugins/check_monit b/plugins/check_monit index 362d2fd..4564d13 100755 --- a/plugins/check_monit +++ b/plugins/check_monit @@ -18,21 +18,35 @@ p.add_option('-p', '--passwd', action='store', type='string', dest='passwd', def options, arguments = p.parse_args() - -request = urllib2.Request("http://%s:%s/_status?format=xml" % (options.host, options.port)) +check_url = "http://%s:%s/_status?format=xml" % (options.host, options.port) +request = urllib2.Request(check_url) base64string = base64.encodestring('%s:%s' % (options.user, options.passwd)).replace('\n', '') request.add_header("Authorization", "Basic %s" % base64string) -result = urllib2.urlopen(request) +try: + result = urllib2.urlopen(request) +except urllib2.HTTPError: + print "Cannot connect to %s as user '%s'" % (check_url, options.user) + sys.exit(3) dom = parseString("".join(result.readlines())) +monitored = [] +unmonitored = [] +exitcode = 0 + for service in dom.getElementsByTagName('service'): name = service.getElementsByTagName('name')[0].firstChild.data monitor = int(service.getElementsByTagName('monitor')[0].firstChild.data) if monitor == 0: - print "%s not monitored" % name - sys.exit(2) - - -print "Everything being monitored" -sys.exit(0) + unmonitored.append(name) + else: + monitored.append(name) + +if len(unmonitored): + print "monit: unmonitored services: %s" % (', '.join(unmonitored)) + exitcode = 2 +else: + print "monit: no unmonitored services" + exitcode = 1 + +sys.exit(exitcode)