From 39c8293ac83558a5948cbc50015b15e68a0ac777 Mon Sep 17 00:00:00 2001 From: Sven Velt Date: Thu, 3 Jun 2010 17:18:24 +0200 Subject: [PATCH] Pimp xml2cfg with code from xml2nagios Signed-off-by: Sven Velt --- nagixsc_xml2cfg.py | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/nagixsc_xml2cfg.py b/nagixsc_xml2cfg.py index 3bb1857..9f4a62d 100755 --- a/nagixsc_xml2cfg.py +++ b/nagixsc_xml2cfg.py @@ -9,11 +9,21 @@ import sys parser = optparse.OptionParser() parser.add_option('-u', '', dest='url', help='URL of status file (xml)') +parser.add_option('-l', '', dest='httpuser', help='HTTP user name') +parser.add_option('-a', '', dest='httppasswd', help='HTTP password') parser.add_option('-f', '', dest='file', help='(Path and) file name of status file') +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('-v', '', action='count', dest='verb', help='Verbose output') parser.set_defaults(url=None) +parser.set_defaults(httpuser=None) +parser.set_defaults(httppasswd=None) parser.set_defaults(file='nagixsc.xml') +parser.set_defaults(schemacheck='') +parser.set_defaults(host=None) +parser.set_defaults(service=None) parser.set_defaults(verb=0) (options, args) = parser.parse_args() @@ -42,26 +52,31 @@ from nagixsc import * ############################################################################## # Get URL or file -if options.url != None: - import urllib2 +doc = read_xml(options) - response = urllib2.urlopen(options.url) - doc = libxml2.parseDoc(response.read()) - response.close() -else: - doc = libxml2.parseFile(options.file) +# Check XML against DTD +if options.schemacheck: + dtd = libxml2.parseDTD(None, options.schemacheck) + ctxt = libxml2.newValidCtxt() + ret = doc.validateDtd(ctxt, dtd) + if ret != 1: + print "error doing DTD validation" + sys.exit(1) + dtd.freeDtd() + del dtd + del ctxt # Check XML file basics -(status, string) = xml_check_version(doc) -debug(1, options.verb, string) +(status, statusstring) = xml_check_version(doc) +debug(1, options.verb, statusstring) if not status: - print string + print statusstring sys.exit(127) # Put XML to Python dict -checks = xml_to_dict(doc) +checks = xml_to_dict(doc, options.verb, options.host, options.service) # Loop over check results and search for new hosts and new services -- 2.30.2