X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=nagixsc%2F__init__.py;h=19ef4166cd963bfd5381b8a512144f299273a289;hb=01ccc55fb5e04ca469f62371661e5c8c3e231577;hp=c47c0ea64ae85accf780046295bd1048b1461834;hpb=779ef24a34fda65966d0c143a48a489aeee9acd0;p=nagixsc.git diff --git a/nagixsc/__init__.py b/nagixsc/__init__.py index c47c0ea..19ef416 100644 --- a/nagixsc/__init__.py +++ b/nagixsc/__init__.py @@ -13,6 +13,7 @@ import socket import string import subprocess import sys +import urllib2 def debug(level, verb, string): if level <= verb: @@ -81,6 +82,9 @@ 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 timeout: signal.signal(signal.SIGALRM, exec_timeout_handler) signal.alarm(timeout) @@ -127,6 +131,12 @@ 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 + # Sections are Hosts (not 'nagixsc'), options in sections are Services hosts = config.sections() if 'nagixsc' in hosts: @@ -152,6 +162,8 @@ def conf2dict(config, opt_host=None, opt_service=None): 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) + if add_pnp4nagios_template_hint and '|' in check['output']: + check['output'] += ' [%s]' % check['command'] checks.append(check) @@ -168,6 +180,8 @@ def conf2dict(config, opt_host=None, opt_service=None): cmdline = config.get(host, service) check = exec_check(host_name, service, cmdline, timeout, timeout_returncode) + if add_pnp4nagios_template_hint and '|' in check['output']: + check['output'] += ' [%s]' % check['command'] checks.append(check) return checks @@ -194,14 +208,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') @@ -216,12 +229,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() + random.seed() for check in checks: count_services += 1 @@ -254,7 +268,6 @@ def dict2out_checkresult(checks, xmltimestamp, opt_checkresultdir, opt_verb): def read_xml(options): if options.url != None: - import urllib2 if options.httpuser and options.httppasswd: passman = urllib2.HTTPPasswordMgrWithDefaultRealm() @@ -285,6 +298,34 @@ def read_xml_from_string(content): return libxml2.parseDoc(content) +def write_xml(xmldoc, outfile, httpuser=None, httppasswd=None): + if outfile.startswith('http'): + (headers, body) = encode_multipart(xmldoc, httpuser, httppasswd) + 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 + + ############################################################################## def xml_check_version(xmldoc): @@ -446,7 +487,7 @@ def reset_future_timestamp(timestamp, now): ############################################################################## -def encode_multipart(xmldoc, httpuser, httppasswd): +def encode_multipart(xmldoc, httpuser=None, httppasswd=None): BOUNDARY = mimetools.choose_boundary() CRLF = '\r\n' L = []