Code

Migrating from "datetime" to "time"
[nagixsc.git] / nagixsc_xml2nagios.py
index f16c8a832278d51d6786d4df13e10267d9df4094..e05f6134fa8312ef3cbe08ee8e17124aec49686e 100755 (executable)
@@ -1,11 +1,11 @@
 #!/usr/bin/python
 
 #import base64
-import datetime
 import libxml2
 import optparse
 import os
 import sys
+import time
 
 NAGIOSCMDs = [ '/usr/local/nagios/var/rw/nagios.cmd', '/var/lib/nagios3/rw/nagios.cmd', ]
 CHECKRESULTDIRs = [ '/usr/local/nagios/var/spool/checkresults', '/var/lib/nagios3/spool/checkresults', ]
@@ -83,10 +83,9 @@ elif options.mode == 'checkresult' or options.mode == 'checkresult_check':
 elif options.mode == 'active':
        debug(1, options.verb, 'Running in active/plugin mode')
        if options.host == None:
-               debug(1, options.verb, 'No host specified, using first host in XML file')
+               debug(1, options.verb, 'No host specified on command line')
        if options.service == None:
-               print 'No service specified on command line!'
-               sys.exit(127)
+               debug(1, options.verb, 'No service specified on command line, looking at XML file later')
 
 ##############################################################################
 
@@ -94,7 +93,7 @@ elif options.mode == 'active':
 doc = read_xml(options)
 
 # Now timestamp AFTER getting the XML file
-now = int(datetime.datetime.now().strftime('%s'))
+now = long(time.time())
 
 
 # Check XML against DTD
@@ -194,9 +193,15 @@ elif options.mode == 'active':
 
        if len(checks) > 1:
                print 'Nag(ix)SC UNKNOWN - Found more (%s) than one host/service!' % len(checks)
+               print 'Found: ' + ', '.join(['%s/%s' % (c['host_name'], c['service_description']) for c in checks])
                sys.exit(3)
        elif len(checks) == 0:
-               print 'Nag(ix)SC UNKNOWN - No check for "%s"/"%s" found in XML' % (options.host, options.service)
+               output = 'Nag(ix)SC UNKNOWN - No check found in XML'
+               if options.host:
+                       output += ' - Host filter: "%s"' % options.host
+               if options.service:
+                       output += ' - Service filter: "%s"' % options.service
+               print output
                sys.exit(3)
 
        print checks[0]['output']