X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=nagixsc.py;h=21d2b0c1b9e5b5f46e400674e3e6ac65489b2891;hb=3ce04dca4f92dc1486d31fdd4676a5cb4e05a810;hp=d65aa09591406e85d9fbde3c1e79206433e5859b;hpb=4d0f7d8dac760342cf06ead0dbc4df753f51dfec;p=nagixsc.git diff --git a/nagixsc.py b/nagixsc.py index d65aa09..21d2b0c 100644 --- a/nagixsc.py +++ b/nagixsc.py @@ -1,7 +1,11 @@ +import BaseHTTPServer +import ConfigParser +import SocketServer import base64 import datetime import libxml2 import shlex +import socket import subprocess import sys @@ -16,6 +20,13 @@ def available_encodings(): return ['base64', 'plain',] +def check_encoding(enc): + if enc in available_encodings(): + return True + else: + return False + + def decode(data, encoding): if encoding == 'plain': return data @@ -30,6 +41,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): @@ -44,6 +68,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): @@ -225,3 +300,36 @@ def reset_future_timestamp(timestamp, now): else: return now +############################################################################## + +class MyHTTPServer(BaseHTTPServer.HTTPServer): + def __init__(self, server_address, HandlerClass, ssl=False, sslpemfile=None): + if ssl: + # FIXME: SSL is in Py2.6 + try: + from OpenSSL import SSL + 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)) + else: + SocketServer.BaseServer.__init__(self, server_address, HandlerClass) + self.socket = socket.socket(self.address_family, self.socket_type) + + self.server_bind() + self.server_activate() + + +class MyHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): + def setup(self): + self.connection = self.request + self.rfile = socket._fileobject(self.request, "rb", self.rbufsize) + self.wfile = socket._fileobject(self.request, "wb", self.wbufsize) + +############################################################################## +