Code

Changed license to GPL-2+ (from GPL-2only).
[nagixsc.git] / nagixsc / __init__.py
index 03884e36b9f22df2a4cda5d51ede2c410382fe4d..de16082654224f367a259ff8c58e04c2afc45b87 100644 (file)
@@ -1,8 +1,25 @@
+# Nag(ix)SC -- __init__.py
+#
+# Copyright (C) 2009-2010 Sven Velt <sv@teamix.net>
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation; either version 2 of the License, or (at your
+# option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+
 import BaseHTTPServer
 import ConfigParser
 import SocketServer
 import base64
-import datetime
 import libxml2
 import mimetools
 import os
@@ -13,6 +30,7 @@ import socket
 import string
 import subprocess
 import sys
+import time
 import urllib2
 
 def debug(level, verb, string):
@@ -70,7 +88,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 +100,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)
@@ -107,7 +134,7 @@ def exec_check(host_name, service_descr, cmdline, timeout=None, timeout_returnco
                except OSError:
                        pass
 
-       check['timestamp'] = datetime.datetime.now().strftime('%s')
+       check['timestamp'] = str(long(time.time()))
        return check
 
 
@@ -128,6 +155,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 +188,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 +215,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
@@ -180,7 +229,7 @@ def dict2out_passive(checks, xmltimestamp, opt_pipe, opt_verb=0):
        FORMAT_HOST = '[%s] PROCESS_HOST_CHECK_RESULT;%s;%s;%s'
        FORMAT_SERVICE = '[%s] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%s;%s'
        count_services = 0
-       now = datetime.datetime.now().strftime('%s')
+       now = str(long(time.time()))
 
        # Prepare
        if opt_verb <= 2:
@@ -195,14 +244,13 @@ def dict2out_passive(checks, xmltimestamp, opt_pipe, opt_verb=0):
                        timestamp = check['timestamp']
                else:
                        timestamp = xmltimestamp
-               count_services += 1
 
                if check['service_description'] == None or check['service_description'] == '':
                        # Host check
-                       line = FORMAT_HOST % (now, check['host_name'], check['returncode'], check['output'].replace('\n', '\\n'))
+                       line = FORMAT_HOST % (timestamp, check['host_name'], check['returncode'], check['output'].replace('\n', '\\n'))
                else:
                        # Service check
-                       line =  FORMAT_SERVICE % (now, check['host_name'], check['service_description'], check['returncode'], check['output'].replace('\n', '\\n'))
+                       line =  FORMAT_SERVICE % (timestamp, check['host_name'], check['service_description'], check['returncode'], check['output'].replace('\n', '\\n'))
 
                if pipe:
                        pipe.write(line + '\n')
@@ -217,12 +265,13 @@ def dict2out_passive(checks, xmltimestamp, opt_pipe, opt_verb=0):
        return count_services
 
 
-def dict2out_checkresult(checks, xmltimestamp, opt_checkresultdir, opt_verb):
+def dict2out_checkresult(checks, xmltimestamp, opt_checkresultdir, opt_verb=0):
        count_services = 0
        count_failed = 0
        list_failed = []
        chars = string.letters + string.digits
-       ctimestamp = datetime.datetime.now().ctime()
+       ctimestamp = time.ctime()
+       random.seed()
 
        for check in checks:
                count_services += 1
@@ -288,23 +337,29 @@ def read_xml_from_string(content):
 def write_xml(xmldoc, outfile, httpuser=None, httppasswd=None):
        if outfile.startswith('http'):
                (headers, body) = encode_multipart(xmldoc, httpuser, httppasswd)
-
-               try:
-                       response = urllib2.urlopen(urllib2.Request(outfile, body, headers)).read()
-               except urllib2.HTTPError, error:
-                       print error
-                       sys.exit(11)
-               except urllib2.URLError, error:
-                       print error.reason[1]
-                       sys.exit(12)
-
-               print response
+               response = urllib2.urlopen(urllib2.Request(outfile, body, headers)).read()
+               return response
 
        elif outfile == '-':
                xmldoc.saveFormatFile('-', format=1)
+               return None
 
        else:
                xmldoc.saveFile(outfile)
+               return None
+
+
+def write_xml_or_die(xmldoc, outfile, httpuser=None, httppasswd=None):
+       try:
+               response = write_xml(xmldoc, outfile, httpuser, httppasswd)
+       except urllib2.HTTPError, error:
+               print error
+               sys.exit(11)
+       except urllib2.URLError, error:
+               print error.reason[1]
+               sys.exit(12)
+
+       return response
 
 
 ##############################################################################
@@ -336,7 +391,7 @@ def xml_get_timestamp(xmldoc):
 
 def xml_to_dict(xmldoc, verb=0, hostfilter=None, servicefilter=None):
        checks = []
-       now = int(datetime.datetime.now().strftime('%s'))
+       now = long(time.time())
        filetimestamp = reset_future_timestamp(xml_get_timestamp(xmldoc), now)
 
        if hostfilter:
@@ -412,7 +467,7 @@ def xml_from_dict(checks, encoding='base64'):
        xmldoc = libxml2.newDoc('1.0')
        xmlroot = xmldoc.newChild(None, 'nagixsc', None)
        xmlroot.setProp('version', '1.0')
-       xmltimestamp = xmlroot.newChild(None, 'timestamp', datetime.datetime.now().strftime('%s'))
+       xmltimestamp = xmlroot.newChild(None, 'timestamp', str(long(time.time())))
 
        for entry in db:
                check = entry[1]
@@ -535,23 +590,34 @@ def daemonize(pidfile=None, stdin='/dev/null', stdout='/dev/null', stderr='/dev/
 
 ##############################################################################
 
-class MyHTTPServer(SocketServer.ForkingMixIn, BaseHTTPServer.HTTPServer):
+if 'ForkingMixIn' in SocketServer.__dict__:
+       MixInClass = SocketServer.ForkingMixIn
+else:
+       MixInClass = SocketServer.ThreadingMixIn
+
+class MyHTTPServer(MixInClass, BaseHTTPServer.HTTPServer):
        def __init__(self, server_address, HandlerClass, ssl=False, sslpemfile=None):
+               SocketServer.BaseServer.__init__(self, server_address, HandlerClass)
+
                if ssl:
-                       # FIXME: SSL is in Py2.6
                        try:
-                               from OpenSSL import SSL
+                               import ssl
+                               self.socket = ssl.wrap_socket(socket.socket(self.address_family, self.socket_type), keyfile=sslpemfile, certfile=sslpemfile)
+
                        except:
-                               print 'No Python OpenSSL wrapper/bindings found!'
-                               sys.exit(127)
-
-                       SocketServer.BaseServer.__init__(self, server_address, HandlerClass)
-                       context = SSL.Context(SSL.SSLv23_METHOD)
-                       context.use_privatekey_file (sslpemfile)
-                       context.use_certificate_file(sslpemfile)
-                       self.socket = SSL.Connection(context, socket.socket(self.address_family, self.socket_type))
+
+                               try:
+                                       from OpenSSL import SSL
+                               except:
+                                       print 'No Python SSL or OpenSSL wrapper/bindings found!'
+                                       sys.exit(127)
+
+                               context = SSL.Context(SSL.SSLv23_METHOD)
+                               context.use_privatekey_file (sslpemfile)
+                               context.use_certificate_file(sslpemfile)
+                               self.socket = SSL.Connection(context, socket.socket(self.address_family, self.socket_type))
+
                else:
-                       SocketServer.BaseServer.__init__(self, server_address, HandlerClass)
                        self.socket = socket.socket(self.address_family, self.socket_type)
 
                self.server_bind()