From 1a62d51714e40523dde39ace390953dc1daababc Mon Sep 17 00:00:00 2001 From: Sven Velt Date: Thu, 26 Aug 2010 11:50:24 +0200 Subject: [PATCH] xml2cfg: Read templates from files Also select if it outputs hosts and/or services Signed-off-by: Sven Velt --- nagixsc_xml2cfg.py | 29 ++++++++++++++++++++++-- sample-configs/xml2cfg_tmpl/host.tmpl | 6 +++++ sample-configs/xml2cfg_tmpl/service.tmpl | 7 ++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 sample-configs/xml2cfg_tmpl/host.tmpl create mode 100644 sample-configs/xml2cfg_tmpl/service.tmpl diff --git a/nagixsc_xml2cfg.py b/nagixsc_xml2cfg.py index 9f4a62d..284a9d3 100755 --- a/nagixsc_xml2cfg.py +++ b/nagixsc_xml2cfg.py @@ -15,6 +15,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,10 +27,14 @@ 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 @@ -36,6 +43,7 @@ HOSTTEMPL='''define host { } ''' +# Hard coded default for service template SERVICETEMPL='''define service { use templ_service_passive @@ -51,6 +59,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) @@ -86,8 +110,9 @@ for check in checks: if not check['host_name'] in foundhosts: foundhosts.append(check['host_name']) - print HOSTTEMPL % check + if 'hosts' in options.output: + print HOSTTEMPL % check - if check['service_description']: + if check['service_description'] and 'services' in options.output: print SERVICETEMPL % check diff --git a/sample-configs/xml2cfg_tmpl/host.tmpl b/sample-configs/xml2cfg_tmpl/host.tmpl new file mode 100644 index 0000000..7846d81 --- /dev/null +++ b/sample-configs/xml2cfg_tmpl/host.tmpl @@ -0,0 +1,6 @@ +define host { + use templ_host_default + + host_name %(host_name)s + address 127.0.0.1 +} diff --git a/sample-configs/xml2cfg_tmpl/service.tmpl b/sample-configs/xml2cfg_tmpl/service.tmpl new file mode 100644 index 0000000..5182c30 --- /dev/null +++ b/sample-configs/xml2cfg_tmpl/service.tmpl @@ -0,0 +1,7 @@ +define service { + use templ_service_passive + + host_name %(host_name)s + service_description %(service_description)s + check_command check_passive +} -- 2.30.2