Commit c2efddd5 authored by Jonathan Michalon's avatar Jonathan Michalon

Cleanup and fix inconsistencies in check definitions

This removes some useless classes/__repr__/redefinitions and
cleans-up the check's destination handling between classes and in emails.
parent 037db59a
...@@ -31,13 +31,13 @@ def __create_report(only_old=False): ...@@ -31,13 +31,13 @@ def __create_report(only_old=False):
if not check.ok and (not only_old or now - check.failure_date > delta): if not check.ok and (not only_old or now - check.failure_date > delta):
has_error = True has_error = True
report += '-+' * 40 + '\n' report += '-+' * 40 + '\n'
report += "%s: %s\nSince %s\n\t%s\n" % (check.target_name, check, report += "Since %s\n%s:\n\t%s\n" % (check.failure_date,
check.failure_date, check.errmsg.strip()) check, check.errmsg.strip())
report += '-+' * 40 + "\n\n" report += '-+' * 40 + "\n\n"
report += " Other checks (usually OK but may be in retry mode):\n" report += " Other checks (usually OK but may be in retry mode):\n"
for check in config.checks: for check in config.checks:
if check.ok: if check.ok:
report += "Check %s is %s\n" % (check, report += "%s is %s\n" % (check,
"OK" if check.retry_count == 0 else "retrying") "OK" if check.retry_count == 0 else "retrying")
return (report, has_error) return (report, has_error)
......
...@@ -7,14 +7,11 @@ from datetime import datetime ...@@ -7,14 +7,11 @@ from datetime import datetime
class Host(object): class Host(object):
def __init__(self, ipv4='192.0.2.1', ipv6='2001:db8::1', name=None): def __init__(self, ipv4='undefined', ipv6='undefined', name=None):
self.ipv4 = ipv4 self.ipv4 = ipv4
self.ipv6 = ipv6 self.ipv6 = ipv6
self.name = name if name is not None else "%s/%s" % (ipv4, ipv6) self.name = name if name is not None else "%s/%s" % (ipv4, ipv6)
def __repr__(self):
return '<Host ipv4="%s" ipv6="%s">' % (self.ipv4, self.ipv6)
class Checks(list): class Checks(list):
def add(self, checks, dests, **options): def add(self, checks, dests, **options):
...@@ -27,8 +24,9 @@ class Checks(list): ...@@ -27,8 +24,9 @@ class Checks(list):
class Check(object): class Check(object):
def __init__(self, **options): def __init__(self, dest, **options):
from . import config from . import config
self.dest = dest
self._options = options self._options = options
self.retry = options.get('retry', 1) self.retry = options.get('retry', 1)
self.retry_count = 0 self.retry_count = 0
...@@ -40,16 +38,18 @@ class Check(object): ...@@ -40,16 +38,18 @@ class Check(object):
self.errmsg = '' self.errmsg = ''
self.last_exec = '' self.last_exec = ''
self.ok = True self.ok = True
self.target_name = options.get('target_name', 'Unknown') self.target_name = options.pop('target_name', dest)
self.timeout = options.get('timeout', 2) self.timeout = options.get('timeout', 2)
def __repr__(self): def __repr__(self):
return '{:<15s} N={}/{}, R={}/{}, {}'.format(self.__class__.__name__, return '<{}, {}({}) with {} (N={}/{}, R={}/{})>'.format(self.target_name,
self.__class__.__name__,
self.dest,
self._options,
self.run_count, self.run_count,
self.every, self.every,
self.retry_count, self.retry_count,
self.retry, self.retry)
self._options)
def setup(self): def setup(self):
pass pass
...@@ -117,50 +117,37 @@ class Check(object): ...@@ -117,50 +117,37 @@ class Check(object):
return p.returncode == 0 return p.returncode == 0
class CheckIP(Check): class Check4(Check):
def __init__(self, host, **options): def __init__(self, host, **options):
super().__init__(**options) options.setdefault('target_name', host.name)
self.target_name = host.name super().__init__(host.ipv4, **options)
def __repr__(self):
return '<%s on %s>' % (super().__repr__(), self.addr)
class Check6(Check):
class Check4(CheckIP):
def __init__(self, host, **options): def __init__(self, host, **options):
super().__init__(host, **options) options.setdefault('target_name', host.name)
self.addr = host.ipv4 super().__init__(host.ipv6, **options)
class Check6(CheckIP):
def __init__(self, host, **options):
super().__init__(host, **options)
self.addr = host.ipv6
class CheckPing4(Check4): class CheckPing4(Check4):
def check(self): def check(self):
command = ['/bin/ping', '-c', '1', '-W', str(self.timeout), self.addr] command = ['/bin/ping', '-c', '1', '-W', str(self.timeout), self.dest]
return self.exec_with_timeout(command, timeout=self.timeout + 1) return self.exec_with_timeout(command, timeout=self.timeout + 1)
class CheckPing6(Check6): class CheckPing6(Check6):
def check(self): def check(self):
command = ['/bin/ping6', '-c', '1', '-W', str(self.timeout), self.addr] command = ['/bin/ping6', '-c', '1', '-W', str(self.timeout), self.dest]
return self.exec_with_timeout(command, timeout=self.timeout + 1) return self.exec_with_timeout(command, timeout=self.timeout + 1)
class CheckDNSZone(Check): class CheckDNSZone(Check):
def __init__(self, zone, **options): def __init__(self, zone, **options):
super().__init__(**options) options.setdefault('target_name', 'zone ' + zone)
self.zone = zone super().__init__(zone, **options)
self.target_name = "zone '%s'" % zone
def __repr__(self):
return '<%s for %s>' % (super().__repr__(), self.zone)
def check(self): def check(self):
command = ['check_dns_soa', '-H', self.zone] command = ['check_dns_soa', '-H', self.dest]
if self._options.get('ip_version', 0) in [4, 6]: if self._options.get('ip_version', 0) in [4, 6]:
command.append('-' + str(self._options['ip_version'])) command.append('-' + str(self._options['ip_version']))
return self.exec_with_timeout(command) return self.exec_with_timeout(command)
...@@ -168,7 +155,7 @@ class CheckDNSZone(Check): ...@@ -168,7 +155,7 @@ class CheckDNSZone(Check):
class CheckDNSRec(Check): class CheckDNSRec(Check):
def check(self): def check(self):
command = ['dig', 'www.google.com', '@' + self.addr] command = ['dig', 'www.google.com', '@' + self.dest]
return self.exec_with_timeout(command, pattern='status: NOERROR') return self.exec_with_timeout(command, pattern='status: NOERROR')
...@@ -189,7 +176,7 @@ class CheckDNSAut(Check): ...@@ -189,7 +176,7 @@ class CheckDNSAut(Check):
class CheckHTTP(Check): class CheckHTTP(Check):
def build_command(self): def build_command(self):
command = ['/usr/lib/nagios/plugins/check_http', command = ['/usr/lib/nagios/plugins/check_http',
'-I', self.addr, '-t', str(self.timeout)] '-I', self.dest, '-t', str(self.timeout)]
if 'status' in self._options: if 'status' in self._options:
command += ['-e', str(self._options['status'])] command += ['-e', str(self._options['status'])]
if 'vhost' in self._options: if 'vhost' in self._options:
...@@ -230,7 +217,7 @@ class CheckHTTPS6(CheckHTTPS, Check6): ...@@ -230,7 +217,7 @@ class CheckHTTPS6(CheckHTTPS, Check6):
class CheckSMTP(Check): class CheckSMTP(Check):
def build_command(self): def build_command(self):
command = ['/usr/lib/nagios/plugins/check_smtp', command = ['/usr/lib/nagios/plugins/check_smtp',
'-H', self.addr, '-H', self.dest,
'-f', self._options.get('from_addr', '-f', self._options.get('from_addr',
'picomon@localhost.local'), 'picomon@localhost.local'),
'-t', str(self.timeout)] '-t', str(self.timeout)]
...@@ -256,7 +243,7 @@ class CheckSMTP6(CheckSMTP, Check6): ...@@ -256,7 +243,7 @@ class CheckSMTP6(CheckSMTP, Check6):
class CheckOpenVPN(Check): class CheckOpenVPN(Check):
def check(self): def check(self):
command = ['/usr/lib/nagios/plugins/check_udp', command = ['/usr/lib/nagios/plugins/check_udp',
'-H', self.addr, '-H', self.dest,
'-p', "1194", '-p', "1194",
'-m', "1", '-m', "1",
'-M', "ok", # actualy just having a reply is enough '-M', "ok", # actualy just having a reply is enough
...@@ -277,7 +264,7 @@ class CheckOpenVPN6(CheckOpenVPN, Check6): ...@@ -277,7 +264,7 @@ class CheckOpenVPN6(CheckOpenVPN, Check6):
class CheckJabber(Check): class CheckJabber(Check):
def check(self): def check(self):
command = ['/usr/lib/nagios/plugins/check_jabber', command = ['/usr/lib/nagios/plugins/check_jabber',
'-H', self.addr, '-H', self.dest,
'-t', str(self.timeout)] '-t', str(self.timeout)]
return self.exec_with_timeout(command, timeout=self.timeout + 1) return self.exec_with_timeout(command, timeout=self.timeout + 1)
......
...@@ -114,7 +114,7 @@ def send_email_for_check(check): ...@@ -114,7 +114,7 @@ def send_email_for_check(check):
check=check.__class__.__name__, check=check.__class__.__name__,
dest=check.target_name)) dest=check.target_name))
msg_text = "Check %s:\n" % str(check) msg_text = "%s:\n" % check
if check.ok: if check.ok:
delta = datetime.now() - check.failure_date delta = datetime.now() - check.failure_date
# remove microsec # remove microsec
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment