diff --git a/nagixsc.py b/nagixsc.py
index 21d2b0c1b9e5b5f46e400674e3e6ac65489b2891..56c4cf3ead117ca047a818085912232a5ad514d4 100644 (file)
--- a/nagixsc.py
+++ b/nagixsc.py
import base64
import datetime
import libxml2
+import mimetools
+import os
+import random
import shlex
import socket
+import string
import subprocess
import sys
return checks
+##############################################################################
+
+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')
+
+ # Prepare
+ if opt_verb <= 2:
+ pipe = open(opt_pipe, "w")
+ else:
+ pipe = None
+
+ # Output
+ for check in checks:
+ count_services += 1
+ if check.has_key('timestamp'):
+ 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'))
+ else:
+ # Service check
+ line = FORMAT_SERVICE % (now, check['host_name'], check['service_description'], check['returncode'], check['output'].replace('\n', '\\n'))
+
+ if pipe:
+ pipe.write(line + '\n')
+ debug(2, opt_verb, line)
+
+ # Close
+ if pipe:
+ pipe.close()
+ else:
+ print "Passive check results NOT written to Nagios pipe due to -vvv!"
+
+ return count_services
+
+
+def dict2out_checkresult(checks, xmltimestamp, opt_checkresultdir, opt_verb):
+ count_services = 0
+ count_failed = 0
+ list_failed = []
+ chars = string.letters + string.digits
+ ctimestamp = datetime.datetime.now().ctime()
+
+ for check in checks:
+ count_services += 1
+ if check.has_key('timestamp'):
+ timestamp = check['timestamp']
+ else:
+ timestamp = xmltimestamp
+
+ filename = os.path.join(opt_checkresultdir, 'c' + ''.join([random.choice(chars) for i in range(6)]))
+ try:
+ crfile = open(filename, "w")
+ if check['service_description'] == None or check['service_description'] == '':
+ # Host check
+ crfile.write('### Active Check Result File ###\nfile_time=%s\n\n### Nagios Service Check Result ###\n# Time: %s\nhost_name=%s\ncheck_type=0\ncheck_options=0\nscheduled_check=1\nreschedule_check=1\nlatency=0.0\nstart_time=%s.00\nfinish_time=%s.05\nearly_timeout=0\nexited_ok=1\nreturn_code=%s\noutput=%s\n' % (timestamp, ctimestamp, check['host_name'], timestamp, timestamp, check['returncode'], check['output'].replace('\n', '\\n') ) )
+ else:
+ # Service check
+ crfile.write('### Active Check Result File ###\nfile_time=%s\n\n### Nagios Service Check Result ###\n# Time: %s\nhost_name=%s\nservice_description=%s\ncheck_type=0\ncheck_options=0\nscheduled_check=1\nreschedule_check=1\nlatency=0.0\nstart_time=%s.00\nfinish_time=%s.05\nearly_timeout=0\nexited_ok=1\nreturn_code=%s\noutput=%s\n' % (timestamp, ctimestamp, check['host_name'], check['service_description'], timestamp, timestamp, check['returncode'], check['output'].replace('\n', '\\n') ) )
+ crfile.close()
+
+ # Create OK file
+ open(filename + '.ok', 'w').close()
+ except:
+ count_failed += 1
+ list_failed.append([filename, check['host_name'], check['service_description']])
+
+ return (count_services, count_failed, list_failed)
+
+
##############################################################################
def read_xml(options):
return doc
+def read_xml_from_string(content):
+ return libxml2.parseDoc(content)
+
+
##############################################################################
def xml_check_version(xmldoc):
##############################################################################
+def encode_multipart(xmldoc, httpuser, httppasswd):
+ BOUNDARY = mimetools.choose_boundary()
+ CRLF = '\r\n'
+ L = []
+ L.append('--' + BOUNDARY)
+ L.append('Content-Disposition: form-data; name="xmlfile"; filename="xmlfile"')
+ L.append('Content-Type: application/xml')
+ L.append('')
+ L.append(xmldoc.serialize())
+ L.append('--' + BOUNDARY + '--')
+ L.append('')
+ body = CRLF.join(L)
+ content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
+ headers = {'Content-Type': content_type, 'Content-Length': str(len(body))}
+
+ if httpuser and httppasswd:
+ headers['Authorization'] = 'Basic %s' % base64.b64encode(':'.join([httpuser, httppasswd]))
+
+ return (headers, body)
+
+##############################################################################
+
class MyHTTPServer(BaseHTTPServer.HTTPServer):
def __init__(self, server_address, HandlerClass, ssl=False, sslpemfile=None):
if ssl: