Code

Changed license to GPL-2+ (from GPL-2only).
[nagixsc.git] / nagixsc_xml2nagios.py
index 96b6a522b6090c3e9055c2ef7fca84b92b82cf54..fae228ee3acd3fe4abced8b0a472fe281eb3d1ed 100755 (executable)
@@ -1,11 +1,29 @@
 #!/usr/bin/python
+#
+# Nag(ix)SC -- nagixsc_xml2nagios.py
+#
+# Copyright (C) 2009-2010 Sven Velt <sv@teamix.net>
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation; either version 2 of the License, or (at your
+# option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
 #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 +101,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 +111,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
@@ -160,9 +177,10 @@ if options.mode == 'passive' or options.mode == 'passive_check':
 elif options.mode.startswith('checkresult'):
        (count_services, count_failed, list_failed) = dict2out_checkresult(checks, xml_get_timestamp(doc), options.checkresultdir, options.verb)
 
-       if list_failed and options.mode == 'checkresult':
-               for entry in list_failed:
-                       print 'Could not write checkresult files "%s(.ok)" for "%s"/"%s"!' % (entry[0], entry[1], entry[2])
+       if options.mode == 'checkresult':
+               if list_failed:
+                       for entry in list_failed:
+                               print 'Could not write checkresult files "%s(.ok)" for "%s"/"%s"!' % (entry[0], entry[1], entry[2])
 
                if count_failed == 0:
                        sys.exit(0)
@@ -193,9 +211,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']