Code

Import md5 from hashlib/md5, dep. on Py version
[nagixsc.git] / nagixsc_xml2nagios.py
index 4296ee5d099a473b5c9f5e5f8b0e0596c70ee96e..58d5c0707e9bdb86a79bdc27ea52cdc7bbec7cbf 100755 (executable)
@@ -16,6 +16,8 @@ MODEs = [ 'passive', 'passive_check', 'checkresult', 'checkresult_check', 'activ
 parser = optparse.OptionParser()
 
 parser.add_option('-u', '', dest='url', help='URL of status file (xml)')
+parser.add_option('-l', '', dest='httpuser', help='HTTP user name')
+parser.add_option('-a', '', dest='httppasswd', help='HTTP password')
 parser.add_option('-f', '', dest='file', help='(Path and) file name of status file')
 parser.add_option('-S', '', dest='schemacheck', help='Check XML against DTD')
 parser.add_option('-s', '', dest='seconds', type='int', help='Maximum age in seconds of xml timestamp')
@@ -28,6 +30,8 @@ parser.add_option('-D', '', dest='service', help='Service description to search
 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
 
 parser.set_defaults(url=None)
+parser.set_defaults(httpuser=None)
+parser.set_defaults(httppasswd=None)
 parser.set_defaults(file='nagixsc.xml')
 parser.set_defaults(schemacheck='')
 parser.set_defaults(seconds=14400)
@@ -88,17 +92,11 @@ elif options.mode == 'active':
 
 ##############################################################################
 
-now = int(datetime.datetime.now().strftime('%s'))
-
 # Get URL or file
-if options.url != None:
-       import urllib2
+doc = read_xml(options)
 
-       response = urllib2.urlopen(options.url)
-       doc = libxml2.parseDoc(response.read())
-       response.close()
-else:
-       doc = libxml2.parseFile(options.file)
+# Now timestamp AFTER getting the XML file
+now = int(datetime.datetime.now().strftime('%s'))
 
 
 # Check XML against DTD
@@ -143,6 +141,7 @@ for check in checks:
 # Next steps depend on mode, output results
 # MODE: passive
 if options.mode == 'passive' or options.mode == 'passive_check':
+       count_services = 0
        # Prepare
        if options.verb <= 2:
                pipe = open(options.pipe, "w")
@@ -151,7 +150,13 @@ if options.mode == 'passive' or options.mode == 'passive_check':
 
        # Output
        for check in checks:
-               line = '[%s] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%s;%s' % (now, check['host_name'], check['service_description'], check['returncode'], check['output'].replace('\n', '\\n'))
+               count_services += 1
+               if check['service_description'] == None or check['service_description'] == '':
+                       # Host check
+                       line = '[%s] PROCESS_HOST_CHECK_RESULT;%s;%s;%s' % (now, check['host_name'], check['returncode'], check['output'].replace('\n', '\\n'))
+               else:
+                       # Service check
+                       line = '[%s] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%s;%s' % (now, check['host_name'], check['service_description'], check['returncode'], check['output'].replace('\n', '\\n'))
 
                if pipe:
                        pipe.write(line + '\n')
@@ -173,11 +178,11 @@ if options.mode == 'passive' or options.mode == 'passive_check':
                if options.markold:
                        if (now - filetimestamp) > options.seconds:
                                returnstring = 'WARNING'
-                               output = 'Check results are %s(>%s) seconds old' % ((now-filetimestamp), options.seconds)
+                               output = '%s check results written, which are %s(>%s) seconds old' % (count_services, (now-filetimestamp), options.seconds)
                                returncode = 1
 
                if not output:
-                       output = 'Check results are %s seconds old' % (now-filetimestamp)
+                       output = '%s check results written which are %s seconds old' % (count_services, (now-filetimestamp))
 
                print 'Nag(ix)SC %s - %s' % (returnstring, output)
                sys.exit(returncode)
@@ -199,7 +204,12 @@ elif options.mode == 'checkresult' or options.mode == 'checkresult_check':
                filename = os.path.join(options.checkresultdir, 'c' + ''.join([random.choice(chars) for i in range(6)]))
                try:
                        crfile = open(filename, "w")
-                       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, datetime.datetime.now().ctime(), check['host_name'], check['service_description'], timestamp, timestamp, check['returncode'], check['output'].replace('\n', '\\n') ) )
+                       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, datetime.datetime.now().ctime(), 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, datetime.datetime.now().ctime(), check['host_name'], check['service_description'], timestamp, timestamp, check['returncode'], check['output'].replace('\n', '\\n') ) )
                        crfile.close()
 
                        # Create OK file