Code

Changed license to GPL-2+ (from GPL-2only).
[nagixsc.git] / nagixsc_xml2cfg.py
index 284a9d31dc0b14aa7c4ff642b2ff52538a210e37..5300ecab9fad6a754e2eed323f8567d8c92a9307 100755 (executable)
@@ -1,9 +1,27 @@
 #!/usr/bin/python
+#
+# Nag(ix)SC -- nagixsc_xml2cfg.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 socket
 import sys
 
 parser = optparse.OptionParser()
@@ -39,7 +57,7 @@ HOSTTEMPL='''define host {
        use             templ_host_default
 
        host_name       %(host_name)s
-       address         127.0.0.1
+       address         %(address)s
 }
 '''
 
@@ -103,6 +121,10 @@ if not status:
 checks = xml_to_dict(doc, options.verb, options.host, options.service)
 
 
+# Set default socket options
+if hasattr(socket, 'setdefaulttimeout'):
+       socket.setdefaulttimeout(2)
+
 # Loop over check results and search for new hosts and new services
 foundhosts = []
 
@@ -111,6 +133,10 @@ for check in checks:
                foundhosts.append(check['host_name'])
 
                if 'hosts' in options.output:
+                       try:
+                               check['address'] = socket.gethostbyname(check['host_name'])
+                       except socket.gaierror:
+                               check['address'] = '127.0.0.1'
                        print HOSTTEMPL % check
 
        if check['service_description'] and 'services' in options.output: