X-Git-Url: https://git.tokkee.org/?p=nagixsc.git;a=blobdiff_plain;f=nagixsc_xml2cfg.py;h=5300ecab9fad6a754e2eed323f8567d8c92a9307;hp=9f4a62d85a8948ae0e1813260250873adf2b4b48;hb=HEAD;hpb=39c8293ac83558a5948cbc50015b15e68a0ac777 diff --git a/nagixsc_xml2cfg.py b/nagixsc_xml2cfg.py index 9f4a62d..5300eca 100755 --- a/nagixsc_xml2cfg.py +++ b/nagixsc_xml2cfg.py @@ -1,9 +1,27 @@ #!/usr/bin/python +# +# Nag(ix)SC -- nagixsc_xml2cfg.py +# +# Copyright (C) 2009-2010 Sven Velt +# +# 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() @@ -15,6 +33,9 @@ parser.add_option('-f', '', dest='file', help='(Path and) file name of status fi parser.add_option('-S', '', dest='schemacheck', help='Check XML against DTD') parser.add_option('-H', '', dest='host', help='Hostname to search for in XML file') parser.add_option('-D', '', dest='service', help='Service description to search for in XML file') +parser.add_option('', '--host-template', dest='tmpl_host', help='Filename of host template') +parser.add_option('', '--service-template', dest='tmpl_service', help='Filename of service template') +parser.add_option('-O', '', dest='output', help='Output "hosts", "services" or "both" (default)') parser.add_option('-v', '', action='count', dest='verb', help='Verbose output') parser.set_defaults(url=None) @@ -24,18 +45,23 @@ parser.set_defaults(file='nagixsc.xml') parser.set_defaults(schemacheck='') parser.set_defaults(host=None) parser.set_defaults(service=None) +parser.set_defaults(output=None) +parser.set_defaults(tmpl_host=None) +parser.set_defaults(tmpl_service=None) parser.set_defaults(verb=0) (options, args) = parser.parse_args() +# Hard coded default for host template HOSTTEMPL='''define host { use templ_host_default host_name %(host_name)s - address 127.0.0.1 + address %(address)s } ''' +# Hard coded default for service template SERVICETEMPL='''define service { use templ_service_passive @@ -51,6 +77,22 @@ from nagixsc import * ############################################################################## +# Output +if not options.output in [None, 'both', 'hosts', 'services']: + print 'Unknown output mode "%s"!' % options.output + sys.exit(1) + +if options.output in [None, 'both']: + options.output = ['hosts', 'services'] +else: + options.output = [options.output,] + +# Read host and/or service template +if options.tmpl_host and 'hosts' in options.output: + HOSTTEMPL = open(options.tmpl_host).read() +if options.tmpl_service and 'services' in options.output: + SERVICETEMPL = open(options.tmpl_service).read() + # Get URL or file doc = read_xml(options) @@ -79,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 = [] @@ -86,8 +132,13 @@ for check in checks: if not check['host_name'] in foundhosts: foundhosts.append(check['host_name']) - print HOSTTEMPL % check + 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']: + if check['service_description'] and 'services' in options.output: print SERVICETEMPL % check