Code

xml2cfg: Try to lookup IP for host_name
authorSven Velt <sven@velt.de>
Thu, 17 Feb 2011 14:34:32 +0000 (15:34 +0100)
committerSven Velt <sven@velt.de>
Thu, 17 Feb 2011 14:34:32 +0000 (15:34 +0100)
Signed-off-by: Sven Velt <sven@velt.de>
nagixsc_xml2cfg.py
sample-configs/xml2cfg_tmpl/host.tmpl

index 284a9d31dc0b14aa7c4ff642b2ff52538a210e37..9cbd179cd93a4507512b58abf31362221fc73efb 100755 (executable)
@@ -4,6 +4,7 @@
 import datetime
 import libxml2
 import optparse
+import socket
 import sys
 
 parser = optparse.OptionParser()
@@ -39,7 +40,7 @@ HOSTTEMPL='''define host {
        use             templ_host_default
 
        host_name       %(host_name)s
-       address         127.0.0.1
+       address         %(address)s
 }
 '''
 
@@ -103,6 +104,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 +116,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:
index 7846d814c91c876b9ff17f93d7b655150f51cc2c..6a54e045b3eea20919a7665d7f8411aa8be81b80 100644 (file)
@@ -2,5 +2,5 @@ define host {
        use             templ_host_default
 
        host_name       %(host_name)s
-       address         127.0.0.1
+       address         %(address)s
 }