diff --git a/nagixsc/__init__.py b/nagixsc/__init__.py
index f3797658573c36000aa1541e0310bf09a5e09eff..a2435134dbb5500b37acde1b917fe3155aa2fd1c 100644 (file)
--- a/nagixsc/__init__.py
+++ b/nagixsc/__init__.py
import ConfigParser
import SocketServer
import base64
-import datetime
import libxml2
import mimetools
import os
import string
import subprocess
import sys
+import time
import urllib2
def debug(level, verb, string):
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 = {}
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 +116,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
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:
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)
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
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:
count_failed = 0
list_failed = []
chars = string.letters + string.digits
- ctimestamp = datetime.datetime.now().ctime()
+ ctimestamp = time.ctime()
random.seed()
for check in checks:
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:
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 +572,34 @@ def daemonize(pidfile=None, stdin='/dev/null', stdout='/dev/null', stderr='/dev/
##############################################################################
-class MyHTTPServer(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()