From c1d405b1a5f8a43a020096e7d055b87f9f8d8179 Mon Sep 17 00:00:00 2001 From: Sven Velt Date: Tue, 8 Dec 2009 12:06:13 +0100 Subject: [PATCH] Added host checks --- examples/nagixsc_debian.conf | 3 +- examples/nagixsc_usrlocal.conf | 3 +- nagixsc.py | 50 ++++++++++++++++++++++++++++------ nagixsc_conf2xml.py | 29 ++++++++++++++++---- nagixsc_xml2nagios.py | 20 +++++++++++--- xml/nagixsc.dtd | 2 +- xml/nagixsc.xml | 2 ++ xml/nagixsc.xsd | 4 +++ 8 files changed, 92 insertions(+), 21 deletions(-) diff --git a/examples/nagixsc_debian.conf b/examples/nagixsc_debian.conf index 1b82196..8b47fd6 100644 --- a/examples/nagixsc_debian.conf +++ b/examples/nagixsc_debian.conf @@ -3,7 +3,8 @@ Reserved: For future use [host1] _underscores_at_start: reserved -_host_name: host1 +_use_section_name_as: host_name in nagios +_host_check: /usr/lib/nagios/plugins/check_host -H 127.0.0.1 Disk_Home: /usr/lib/nagios/plugins/check_disk -w 10% -c 5% -m -p /home Disk_Root: /usr/lib/nagios/plugins/check_disk -w 10% -c 5% -m -p / Load: /usr/lib/nagios/plugins/check_load -w 5,5,5 -c 10,10,10 diff --git a/examples/nagixsc_usrlocal.conf b/examples/nagixsc_usrlocal.conf index 2aac151..871ef64 100644 --- a/examples/nagixsc_usrlocal.conf +++ b/examples/nagixsc_usrlocal.conf @@ -3,7 +3,8 @@ Reserved: For future use [host1] _underscores_at_start: reserved -_host_name: host1 +_use_section_name_as: host_name in nagios +_host_check: /usr/local/nagios/libexec/check_host -H 127.0.0.1 Disk_Home: /usr/local/nagios/libexec/check_disk -w 10% -c 5% -m -p /home Disk_Root: /usr/local/nagios/libexec/check_disk -w 10% -c 5% -m -p / Load: /usr/local/nagios/libexec/check_load -w 5,5,5 -c 10,10,10 diff --git a/nagixsc.py b/nagixsc.py index 4903509..bf204c0 100644 --- a/nagixsc.py +++ b/nagixsc.py @@ -68,11 +68,34 @@ def xml_to_dict(xmldoc, verb=0, hostfilter=None, servicefilter=None): hostname = decode(xmlhostname.get_content(), xmlhostname.prop('encoding')) debug(2, verb, 'Found host "%s"' % hostname) + # Look for Host check result + if host.xpathEval('returncode'): + retcode = host.xpathEval('returncode')[0].get_content() + else: + retcode = None + + if host.xpathEval('output'): + xmloutput = host.xpathEval('output')[0] + output = decode(xmloutput.get_content(), xmloutput.prop('encoding')).rstrip() + else: + output = None + + if host.xpathEval('timestamp'): + timestamp = int(host.xpathEval('timestamp')[0].get_content()) + else: + timestamp = filetimestamp + + if retcode and output: + checks.append({'host_name':hostname, 'service_description':None, 'returncode':retcode, 'output':output, 'timestamp':timestamp}) + + + # Look for service filter if servicefilter: services = host.xpathEval('service[description="%s"] | service[description="%s"]' % (servicefilter, encode(servicefilter))) else: services = host.xpathEval('service') + # Loop over services in host for service in services: service_dict = {} @@ -81,7 +104,7 @@ def xml_to_dict(xmldoc, verb=0, hostfilter=None, servicefilter=None): srvdescr = decode(xmldescr.get_content(), xmldescr.prop('encoding')) retcode = service.xpathEval('returncode')[0].get_content() - output = decode(xmloutput.get_content(), xmldescr.prop('encoding')).rstrip() + output = decode(xmloutput.get_content(), xmloutput.prop('encoding')).rstrip() try: timestamp = int(service.xpathEval('timestamp')[0].get_content()) @@ -117,14 +140,23 @@ def xml_from_dict(checks, encoding='base64'): xmlhostname = xmlhost.newChild(None, 'name', encode(check['host_name'], encoding)) lasthost = check['host_name'] - xmlservice = xmlhost.newChild(None, 'service', None) - xmlname = xmlservice.newChild(None, 'description', encode(check['service_description'], encoding)) - xmlname.setProp('encoding', encoding) - xmlreturncode = xmlservice.newChild(None, 'returncode', str(check['returncode'])) - xmloutput = xmlservice.newChild(None, 'output', encode(check['output'], encoding)) - xmloutput.setProp('encoding', encoding) - if check.has_key('timestamp'): - xmltimestamp = xmlservice.newChild(None, 'timestamp', str(check['timestamp'])) + if check['service_description'] == '' or check['service_description'] == None: + # Host check result + xmlreturncode = xmlhost.newChild(None, 'returncode', str(check['returncode'])) + xmloutput = xmlhost.newChild(None, 'output', encode(check['output'], encoding)) + xmloutput.setProp('encoding', encoding) + if check.has_key('timestamp'): + xmltimestamp = xmlhost.newChild(None, 'timestamp', str(check['timestamp'])) + else: + # Service check result + xmlservice = xmlhost.newChild(None, 'service', None) + xmlname = xmlservice.newChild(None, 'description', encode(check['service_description'], encoding)) + xmlname.setProp('encoding', encoding) + xmlreturncode = xmlservice.newChild(None, 'returncode', str(check['returncode'])) + xmloutput = xmlservice.newChild(None, 'output', encode(check['output'], encoding)) + xmloutput.setProp('encoding', encoding) + if check.has_key('timestamp'): + xmltimestamp = xmlservice.newChild(None, 'timestamp', str(check['timestamp'])) return xmldoc diff --git a/nagixsc_conf2xml.py b/nagixsc_conf2xml.py index c58ad1e..149bdd1 100755 --- a/nagixsc_conf2xml.py +++ b/nagixsc_conf2xml.py @@ -37,6 +37,19 @@ if options.encoding not in available_encodings(): ############################################################################## +def exec_check(host_name, service_descr, cmdline): + try: + cmd = subprocess.Popen(cmdline.split(' '), stdout=subprocess.PIPE) + output = cmd.communicate()[0].rstrip() + retcode = cmd.returncode + except OSError: + output = 'Could not execute "%s"' % cmdline + retcode = 127 + + return {'host_name':host_name, 'service_description':service_descr, 'returncode':retcode, 'output':output, 'timestamp':datetime.datetime.now().strftime('%s')} + +############################################################################## + config = ConfigParser.RawConfigParser() config.optionxform = str # We need case-sensitive options conf_list = config.read(options.conffile) @@ -51,21 +64,27 @@ if 'nagixsc' in hosts: hosts.remove('nagixsc') for host in hosts: + # Overwrite section/host name with '_host_name' if config.has_option(host,'_host_name'): host_name = config.get(host,'_host_name') else: host_name = host + + # Look for host check + if config.has_option(host,'_host_check'): + cmdline = config.get(host, '_host_check') + check = exec_check(host_name, None, cmdline) + checks.append(check) + + for service in config.options(host): # If option starts with '_' it may be a NagixSC option in the future if service[0] != '_': cmdline = config.get(host, service) - cmd = subprocess.Popen(cmdline.split(' '), stdout=subprocess.PIPE) - output = cmd.communicate()[0].rstrip() - retcode = cmd.returncode - - checks.append({'host_name':host_name, 'service_description':service, 'returncode':retcode, 'output':output, 'timestamp':datetime.datetime.now().strftime('%s')}) + check = exec_check(host_name, service, cmdline) + checks.append(check) xmldoc = xml_from_dict(checks, options.encoding) diff --git a/nagixsc_xml2nagios.py b/nagixsc_xml2nagios.py index 4296ee5..9b0a394 100755 --- a/nagixsc_xml2nagios.py +++ b/nagixsc_xml2nagios.py @@ -143,6 +143,7 @@ for check in checks: # Next steps depend on mode, output results # MODE: passive if options.mode == 'passive' or options.mode == 'passive_check': + count_services = 0 # Prepare if options.verb <= 2: pipe = open(options.pipe, "w") @@ -151,7 +152,13 @@ if options.mode == 'passive' or options.mode == 'passive_check': # Output for check in checks: - line = '[%s] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%s;%s' % (now, check['host_name'], check['service_description'], check['returncode'], check['output'].replace('\n', '\\n')) + count_services += 1 + if check['service_description'] == None or check['service_description'] == '': + # Host check + line = '[%s] PROCESS_HOST_CHECK_RESULT;%s;%s;%s' % (now, check['host_name'], check['returncode'], check['output'].replace('\n', '\\n')) + else: + # Service check + line = '[%s] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%s;%s' % (now, check['host_name'], check['service_description'], check['returncode'], check['output'].replace('\n', '\\n')) if pipe: pipe.write(line + '\n') @@ -173,11 +180,11 @@ if options.mode == 'passive' or options.mode == 'passive_check': if options.markold: if (now - filetimestamp) > options.seconds: returnstring = 'WARNING' - output = 'Check results are %s(>%s) seconds old' % ((now-filetimestamp), options.seconds) + output = '%s check results written, which are %s(>%s) seconds old' % (count_services, (now-filetimestamp), options.seconds) returncode = 1 if not output: - output = 'Check results are %s seconds old' % (now-filetimestamp) + output = '%s check results written which are %s seconds old' % (count_services, (now-filetimestamp)) print 'Nag(ix)SC %s - %s' % (returnstring, output) sys.exit(returncode) @@ -199,7 +206,12 @@ elif options.mode == 'checkresult' or options.mode == 'checkresult_check': filename = os.path.join(options.checkresultdir, 'c' + ''.join([random.choice(chars) for i in range(6)])) try: crfile = open(filename, "w") - crfile.write('### Active Check Result File ###\nfile_time=%s\n\n### Nagios Service Check Result ###\n# Time: %s\nhost_name=%s\nservice_description=%s\ncheck_type=0\ncheck_options=0\nscheduled_check=1\nreschedule_check=1\nlatency=0.0\nstart_time=%s.00\nfinish_time=%s.05\nearly_timeout=0\nexited_ok=1\nreturn_code=%s\noutput=%s\n' % (timestamp, datetime.datetime.now().ctime(), check['host_name'], check['service_description'], timestamp, timestamp, check['returncode'], check['output'].replace('\n', '\\n') ) ) + if check['service_description'] == None or check['service_description'] == '': + # Host check + crfile.write('### Active Check Result File ###\nfile_time=%s\n\n### Nagios Service Check Result ###\n# Time: %s\nhost_name=%s\ncheck_type=0\ncheck_options=0\nscheduled_check=1\nreschedule_check=1\nlatency=0.0\nstart_time=%s.00\nfinish_time=%s.05\nearly_timeout=0\nexited_ok=1\nreturn_code=%s\noutput=%s\n' % (timestamp, datetime.datetime.now().ctime(), check['host_name'], timestamp, timestamp, check['returncode'], check['output'].replace('\n', '\\n') ) ) + else: + # Service check + crfile.write('### Active Check Result File ###\nfile_time=%s\n\n### Nagios Service Check Result ###\n# Time: %s\nhost_name=%s\nservice_description=%s\ncheck_type=0\ncheck_options=0\nscheduled_check=1\nreschedule_check=1\nlatency=0.0\nstart_time=%s.00\nfinish_time=%s.05\nearly_timeout=0\nexited_ok=1\nreturn_code=%s\noutput=%s\n' % (timestamp, datetime.datetime.now().ctime(), check['host_name'], check['service_description'], timestamp, timestamp, check['returncode'], check['output'].replace('\n', '\\n') ) ) crfile.close() # Create OK file diff --git a/xml/nagixsc.dtd b/xml/nagixsc.dtd index 171df16..fa6e453 100644 --- a/xml/nagixsc.dtd +++ b/xml/nagixsc.dtd @@ -3,7 +3,7 @@ - + diff --git a/xml/nagixsc.xml b/xml/nagixsc.xml index 5444ae9..d83074e 100644 --- a/xml/nagixsc.xml +++ b/xml/nagixsc.xml @@ -5,6 +5,8 @@ host1 + 0 + T0sgLSAxMjcuMC4wLjE6IHJ0YSAwLjA4NG1zLCBsb3N0IDAlfHJ0YT0wLjA4NG1zOzIwMC4wMDA7NTAwLjAwMDswOyBwbD0wJTs0MDs4MDs7Cg== RGlza19Ib21l diff --git a/xml/nagixsc.xsd b/xml/nagixsc.xsd index 5c82b91..6be18a3 100644 --- a/xml/nagixsc.xsd +++ b/xml/nagixsc.xsd @@ -7,6 +7,10 @@ + + + + -- 2.30.2