Code

Enhance conf2http (and conf2xml)
authorSven Velt <sven@velt.de>
Wed, 13 Jan 2010 23:20:00 +0000 (00:20 +0100)
committerSven Velt <sven@velt.de>
Wed, 13 Jan 2010 23:20:00 +0000 (00:20 +0100)
"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
nagixsc_conf2http.py
nagixsc_conf2xml.py

index c5d8712517fda97a9b23ce9b815ab3b1ceb4a41d..be3613d8d57ead1a9907bd52ed4bcb17516d8907 100644 (file)
@@ -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):
index 941fa1ccb122438ac01fd8b68df70dbed6d442bd..640539fc98013311286c65ead7f022575501e282 100755 (executable)
@@ -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
 
index a5f3f1374e5f117abd3d075a70fc1d550398eeb3..3105730351fd09073b363356dc0f8b0b3c58ae07 100755 (executable)
@@ -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 == '-':