X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=nagixsc_xml2cfg.py;h=284a9d31dc0b14aa7c4ff642b2ff52538a210e37;hb=8887856ef2ce471b678419033f717750ee3494a6;hp=844dc42bf52ae97b8f7a6852fe9d58d011c26479;hpb=954cfce26facd575fbf1915039f3448c0092d998;p=nagixsc.git diff --git a/nagixsc_xml2cfg.py b/nagixsc_xml2cfg.py index 844dc42..284a9d3 100755 --- a/nagixsc_xml2cfg.py +++ b/nagixsc_xml2cfg.py @@ -9,15 +9,32 @@ 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('', '--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) +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(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 @@ -26,6 +43,7 @@ HOSTTEMPL='''define host { } ''' +# Hard coded default for service template SERVICETEMPL='''define service { use templ_service_passive @@ -41,27 +59,48 @@ from nagixsc import * ############################################################################## -# Get URL or file -if options.url != None: - import urllib2 +# Output +if not options.output in [None, 'both', 'hosts', 'services']: + print 'Unknown output mode "%s"!' % options.output + sys.exit(1) - response = urllib2.urlopen(options.url) - doc = libxml2.parseDoc(response.read()) - response.close() +if options.output in [None, 'both']: + options.output = ['hosts', 'services'] else: - doc = libxml2.parseFile(options.file) + 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) + +# 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 @@ -71,8 +110,9 @@ for check in checks: if not check['host_name'] in foundhosts: foundhosts.append(check['host_name']) - print HOSTTEMPL % check - - print SERVICETEMPL % check + if 'hosts' in options.output: + print HOSTTEMPL % check + if check['service_description'] and 'services' in options.output: + print SERVICETEMPL % check