Code

Add "command_prefix" to conf files
[nagixsc.git] / nagixsc / __init__.py
index f3797658573c36000aa1541e0310bf09a5e09eff..827a104b26c849d855a4e919d3afcad22e342484 100644 (file)
@@ -70,7 +70,7 @@ def read_inifile(inifile):
 def exec_timeout_handler(signum, frame):
        raise ExecTimeoutError
 
-def exec_check(host_name, service_descr, cmdline, timeout=None, timeout_returncode=2):
+def exec_check(host_name, service_descr, cmdline, cmdprefix='', timeout=None, timeout_returncode=2):
        cmdarray = shlex.split(cmdline)
 
        check = {}
@@ -82,6 +82,15 @@ def exec_check(host_name, service_descr, cmdline, timeout=None, timeout_returnco
                check['returncode'] = 127
                return check
 
+       check['commandline'] = cmdline
+       check['command'] = cmdarray[0].split(os.path.sep)[-1]
+
+       if cmdprefix:
+               check['fullcommandline'] = cmdprefix + ' ' + cmdline
+               cmdarray = shlex.split(cmdprefix) + cmdarray
+       else:
+               check['fullcommandline'] = cmdline
+
        if timeout:
                signal.signal(signal.SIGALRM, exec_timeout_handler)
                signal.alarm(timeout)
@@ -128,6 +137,18 @@ def conf2dict(config, opt_host=None, opt_service=None):
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
                timeout_returncode = 2
 
+       # Read "add_pnp4nagios_template_hint" from "[nagixsc]", default "False"
+       try:
+               add_pnp4nagios_template_hint = config.getboolean('nagixsc','add_pnp4nagios_template_hint')
+       except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
+               add_pnp4nagios_template_hint = False
+
+       # Read "command_prefix" from "[nagixsc]", default "" (empty string)
+       try:
+               cmdprefix_conffile = config.get('nagixsc','command_prefix')
+       except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
+               cmdprefix_conffile = ''
+
        # Sections are Hosts (not 'nagixsc'), options in sections are Services
        hosts = config.sections()
        if 'nagixsc' in hosts:
@@ -149,10 +170,18 @@ def conf2dict(config, opt_host=None, opt_service=None):
 
 
                services = config.options(host)
+               # Look for host/section specific "command_prefix"
+               if '_command_prefix' in services:
+                       cmdprefix = config.get(host, '_command_prefix')
+               else:
+                       cmdprefix = cmdprefix_conffile
+
                # 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, timeout, timeout_returncode)
+                       check = exec_check(host_name, None, cmdline, cmdprefix, timeout, timeout_returncode)
+                       if add_pnp4nagios_template_hint and '|' in check['output']:
+                               check['output'] += ' [%s]' % check['command']
                        checks.append(check)
 
 
@@ -168,7 +197,9 @@ def conf2dict(config, opt_host=None, opt_service=None):
                        if service[0] != '_':
                                cmdline = config.get(host, service)
 
-                               check = exec_check(host_name, service, cmdline, timeout, timeout_returncode)
+                               check = exec_check(host_name, service, cmdline, cmdprefix, timeout, timeout_returncode)
+                               if add_pnp4nagios_template_hint and '|' in check['output']:
+                                       check['output'] += ' [%s]' % check['command']
                                checks.append(check)
 
        return checks
@@ -541,7 +572,7 @@ def daemonize(pidfile=None, stdin='/dev/null', stdout='/dev/null', stderr='/dev/
 
 ##############################################################################
 
-class MyHTTPServer(BaseHTTPServer.HTTPServer):
+class MyHTTPServer(SocketServer.ForkingMixIn, BaseHTTPServer.HTTPServer):
        def __init__(self, server_address, HandlerClass, ssl=False, sslpemfile=None):
                if ssl:
                        # FIXME: SSL is in Py2.6