From 855195b745afb64c34a89278a283a3fecb46f510 Mon Sep 17 00:00:00 2001 From: Sven Velt Date: Thu, 14 Jan 2010 00:20:00 +0100 Subject: [PATCH] Enhance conf2http (and conf2xml) "nagixsc_conf2http.py" does not call "nagixsc_conf2xml.py" as a subprocess anymore. Instead "conf2dict()" is now a function in "nagixsc.py" and can be called without fork()/exec(). --- nagixsc.py | 65 ++++++++++++++++++++++++++++++++++++++++++++ nagixsc_conf2http.py | 47 ++++++++++++++------------------ nagixsc_conf2xml.py | 56 +++----------------------------------- 3 files changed, 90 insertions(+), 78 deletions(-) diff --git a/nagixsc.py b/nagixsc.py index c5d8712..be3613d 100644 --- a/nagixsc.py +++ b/nagixsc.py @@ -1,3 +1,4 @@ +import ConfigParser import base64 import datetime import libxml2 @@ -37,6 +38,19 @@ def encode(data, encoding=None): return base64.b64encode(data) +############################################################################## + +def read_inifile(inifile): + config = ConfigParser.RawConfigParser() + config.optionxform = str # We need case-sensitive options + ini_list = config.read(inifile) + + if ini_list: + return config + else: + return False + + ############################################################################## def exec_check(host_name, service_descr, cmdline): @@ -51,6 +65,57 @@ def exec_check(host_name, service_descr, cmdline): return {'host_name':host_name, 'service_description':service_descr, 'returncode':retcode, 'output':output, 'timestamp':datetime.datetime.now().strftime('%s')} +############################################################################## + +def conf2dict(config, opt_host=None, opt_service=None): + checks = [] + + # Sections are Hosts (not 'nagixsc'), options in sections are Services + hosts = config.sections() + if 'nagixsc' in hosts: + hosts.remove('nagixsc') + + # Filter out host/section if it exists + if opt_host: + if opt_host in hosts: + hosts = [opt_host,] + else: + hosts = [] + + 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 + + + services = config.options(host) + # Look for host check + if '_host_check' in services and not opt_service: + cmdline = config.get(host, '_host_check') + check = exec_check(host_name, None, cmdline) + checks.append(check) + + + # Filter out service if given in cmd line options + if opt_service: + if opt_service in services: + services = [opt_service,] + else: + services = [] + + for service in services: + # If option starts with '_' it may be a NagixSC option in the future + if service[0] != '_': + cmdline = config.get(host, service) + + check = exec_check(host_name, service, cmdline) + checks.append(check) + + return checks + + ############################################################################## def read_xml(options): diff --git a/nagixsc_conf2http.py b/nagixsc_conf2http.py index 941fa1c..640539f 100755 --- a/nagixsc_conf2http.py +++ b/nagixsc_conf2http.py @@ -16,6 +16,10 @@ except ImportError: ############################################################################## +from nagixsc import * + +############################################################################## + parser = optparse.OptionParser() parser.add_option('-c', '', dest='cfgfile', help='Config file') @@ -82,49 +86,40 @@ class Conf2HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler): if len(path) >= 4: service = path[3] else: - service = '' + service = None if len(path) >= 3: host = path[2] else: - host = '' + host = None if len(path) >= 2: - configfile = path[1] + configfile = path[1] + '.conf' else: - configfile ='' + self.http_error(500, 'No config file specified') + return if re.search('\.\.', configfile): self.http_error(500, 'Found ".." in config file name') return - if configfile and not re.search('^[a-zA-Z0-9-_\.]+$', configfile): + if not re.search('^[a-zA-Z0-9-_\.]+$', configfile): self.http_error(500, 'Config file name contains invalid characters') return - if configfile: - configfile += '.conf' - cmdline += ' -c ' + os.path.join(config['conf_dir'], configfile) - - if host: - cmdline += ' -H %s' % host - if service: - cmdline += ' -D %s' % service + check_config = read_inifile(os.path.join(config['conf_dir'], configfile)) + if not check_config: + self.http_error(500, 'Could not read config file "%s"' % configfile) + return - try: - cmd = subprocess.Popen(cmdline.split(' '), stdout=subprocess.PIPE) - output = cmd.communicate()[0].rstrip() - retcode = cmd.returncode - except OSError: - self.http_error(500, 'Could not execute "%s"' % cmdline) + checks = conf2dict(check_config, host, service) + if not checks: + self.http_error(500, 'No checks executed') return - if retcode == 0: - self.send_response(200) - self.send_header('Content-Type', 'text/xml') - self.end_headers() - self.wfile.write(output) - else: - self.http_error(500, output) + self.send_response(200) + self.send_header('Content-Type', 'text/xml') + self.end_headers() + self.wfile.write(xml_from_dict(checks)) return diff --git a/nagixsc_conf2xml.py b/nagixsc_conf2xml.py index a5f3f13..3105730 100755 --- a/nagixsc_conf2xml.py +++ b/nagixsc_conf2xml.py @@ -1,6 +1,5 @@ #!/usr/bin/python -import ConfigParser import optparse import sys @@ -10,9 +9,6 @@ from nagixsc import * ############################################################################## -checks = [] - - parser = optparse.OptionParser() parser.add_option('-c', '', dest='conffile', help='Config file') @@ -40,57 +36,13 @@ if not check_encoding(options.encoding): ############################################################################## -config = ConfigParser.RawConfigParser() -config.optionxform = str # We need case-sensitive options -conf_list = config.read(options.conffile) +config = read_inifile(options.conffile) -if conf_list == []: +if not config: print 'Config file "%s" could not be read!' % options.conffile - sys.exit(127) - -# Sections are Hosts (not 'nagixsc'), options in sections are Services -hosts = config.sections() -if 'nagixsc' in hosts: - hosts.remove('nagixsc') - -# Filter out host/section if it exists -if options.host: - if options.host in hosts: - hosts = [options.host,] - else: - hosts = [] - -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 - - - services = config.options(host) - # Look for host check - if '_host_check' in services and not options.service: - cmdline = config.get(host, '_host_check') - check = exec_check(host_name, None, cmdline) - checks.append(check) - - - # Filter out service if it exists - if options.service: - if options.service in services: - services = [options.service,] - else: - services = [] - - for service in services: - # If option starts with '_' it may be a NagixSC option in the future - if service[0] != '_': - cmdline = config.get(host, service) - - check = exec_check(host_name, service, cmdline) - checks.append(check) + sys.exit(5) +checks = conf2dict(config, options.host, options.service) xmldoc = xml_from_dict(checks, options.encoding) if options.outfile == '-': -- 2.30.2