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):
if not check.ok and (not only_old or now - check.failure_date > delta):
has_error = True
report += '-+' * 40 + '\n'
report += "%s: %s\nSince %s\n\t%s\n" % (check.target_name, check,
check.failure_date, check.errmsg.strip())
report += "Since %s\n%s:\n\t%s\n" % (check.failure_date,
check, check.errmsg.strip())
report += '-+' * 40 + "\n\n"
report += " Other checks (usually OK but may be in retry mode):\n"
for check in config.checks:
if check.ok:
report += "Check %s is %s\n" % (check,
report += "%s is %s\n" % (check,
"OK" if check.retry_count == 0 else "retrying")
return (report, has_error)
......
......@@ -7,14 +7,11 @@ from datetime import datetime
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.ipv6 = 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):
def add(self, checks, dests, **options):
......@@ -27,8 +24,9 @@ class Checks(list):
class Check(object):
def __init__(self, **options):
def __init__(self, dest, **options):
from . import config
self.dest = dest
self._options = options
self.retry = options.get('retry', 1)
self.retry_count = 0
......@@ -40,16 +38,18 @@ class Check(object):
self.errmsg = ''
self.last_exec = ''
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)
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.every,
self.retry_count,
self.retry,
self._options)
self.retry)
def setup(self):
pass
......@@ -117,50 +117,37 @@ class Check(object):
return p.returncode == 0
class CheckIP(Check):
class Check4(Check):
def __init__(self, host, **options):
super().__init__(**options)
self.target_name = host.name
options.setdefault('target_name', host.name)
super().__init__(host.ipv4, **options)
def __repr__(self):
return '<%s on %s>' % (super().__repr__(), self.addr)
class Check4(CheckIP):
class Check6(Check):
def __init__(self, host, **options):
super().__init__(host, **options)
self.addr = host.ipv4
class Check6(CheckIP):
def __init__(self, host, **options):
super().__init__(host, **options)
self.addr = host.ipv6
options.setdefault('target_name', host.name)
super().__init__(host.ipv6, **options)
class CheckPing4(Check4):
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)
class CheckPing6(Check6):
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)
class CheckDNSZone(Check):
def __init__(self, zone, **options):
super().__init__(**options)
self.zone = zone
self.target_name = "zone '%s'" % zone
def __repr__(self):
return '<%s for %s>' % (super().__repr__(), self.zone)
options.setdefault('target_name', 'zone ' + zone)
super().__init__(zone, **options)
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]:
command.append('-' + str(self._options['ip_version']))
return self.exec_with_timeout(command)
......@@ -168,7 +155,7 @@ class CheckDNSZone(Check):
class CheckDNSRec(Check):
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')
......@@ -189,7 +176,7 @@ class CheckDNSAut(Check):
class CheckHTTP(Check):
def build_command(self):
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:
command += ['-e', str(self._options['status'])]
if 'vhost' in self._options:
......@@ -230,7 +217,7 @@ class CheckHTTPS6(CheckHTTPS, Check6):
class CheckSMTP(Check):
def build_command(self):
command = ['/usr/lib/nagios/plugins/check_smtp',
'-H', self.addr,
'-H', self.dest,
'-f', self._options.get('from_addr',
'picomon@localhost.local'),
'-t', str(self.timeout)]
......@@ -256,7 +243,7 @@ class CheckSMTP6(CheckSMTP, Check6):
class CheckOpenVPN(Check):
def check(self):
command = ['/usr/lib/nagios/plugins/check_udp',
'-H', self.addr,
'-H', self.dest,
'-p', "1194",
'-m', "1",
'-M', "ok", # actualy just having a reply is enough
......@@ -277,7 +264,7 @@ class CheckOpenVPN6(CheckOpenVPN, Check6):
class CheckJabber(Check):
def check(self):
command = ['/usr/lib/nagios/plugins/check_jabber',
'-H', self.addr,
'-H', self.dest,
'-t', str(self.timeout)]
return self.exec_with_timeout(command, timeout=self.timeout + 1)
......
......@@ -114,7 +114,7 @@ def send_email_for_check(check):
check=check.__class__.__name__,
dest=check.target_name))
msg_text = "Check %s:\n" % str(check)
msg_text = "%s:\n" % check
if check.ok:
delta = datetime.now() - check.failure_date
# 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