From 0cf56d45d95d86a8a3631ffce27b01f2a178b2fd Mon Sep 17 00:00:00 2001 From: Sven Velt Date: Thu, 17 Feb 2011 15:34:32 +0100 Subject: [PATCH] xml2cfg: Try to lookup IP for host_name Signed-off-by: Sven Velt --- nagixsc_xml2cfg.py | 11 ++++++++++- sample-configs/xml2cfg_tmpl/host.tmpl | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/nagixsc_xml2cfg.py b/nagixsc_xml2cfg.py index 284a9d3..9cbd179 100755 --- a/nagixsc_xml2cfg.py +++ b/nagixsc_xml2cfg.py @@ -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: diff --git a/sample-configs/xml2cfg_tmpl/host.tmpl b/sample-configs/xml2cfg_tmpl/host.tmpl index 7846d81..6a54e04 100644 --- a/sample-configs/xml2cfg_tmpl/host.tmpl +++ b/sample-configs/xml2cfg_tmpl/host.tmpl @@ -2,5 +2,5 @@ define host { use templ_host_default host_name %(host_name)s - address 127.0.0.1 + address %(address)s } -- 2.30.2