X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=nagixsc%2F__init__.py;h=030b86da2805ddf724cf9892fb49d4e6a5a2d280;hb=700904cb0a254d0230fb829f7c0dacfe84676f6d;hp=85f39092a13ab1e105e002bd172e1293b15ebc41;hpb=fcf0c6b01294ced1de3f2ef03fc592daa939af06;p=nagixsc.git diff --git a/nagixsc/__init__.py b/nagixsc/__init__.py index 85f3909..030b86d 100644 --- a/nagixsc/__init__.py +++ b/nagixsc/__init__.py @@ -1,8 +1,24 @@ +# Nag(ix)SC -- __init__.py +# +# Copyright (C) 2009-2010 Sven Velt +# +# 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; only version 2 of the License is applicable. +# +# 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 +29,7 @@ import socket import string import subprocess import sys +import time import urllib2 def debug(level, verb, string): @@ -70,7 +87,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 +99,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 +133,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 +154,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 +187,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 +214,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 +228,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: @@ -221,7 +269,7 @@ def dict2out_checkresult(checks, xmltimestamp, opt_checkresultdir, opt_verb=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: @@ -342,7 +390,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: @@ -418,7 +466,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] @@ -541,23 +589,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()