Code

obsses: Debug output, typo
[nagixsc.git] / nagixsc_xml2cfg.py
1 #!/usr/bin/python
3 #import base64
4 import datetime
5 import libxml2
6 import optparse
7 import sys
9 parser = optparse.OptionParser()
11 parser.add_option('-u', '', dest='url', help='URL of status file (xml)')
12 parser.add_option('-l', '', dest='httpuser', help='HTTP user name')
13 parser.add_option('-a', '', dest='httppasswd', help='HTTP password')
14 parser.add_option('-f', '', dest='file', help='(Path and) file name of status file')
15 parser.add_option('-S', '', dest='schemacheck', help='Check XML against DTD')
16 parser.add_option('-H', '', dest='host', help='Hostname to search for in XML file')
17 parser.add_option('-D', '', dest='service', help='Service description to search for in XML file')
18 parser.add_option('', '--host-template', dest='tmpl_host', help='Filename of host template')
19 parser.add_option('', '--service-template', dest='tmpl_service', help='Filename of service template')
20 parser.add_option('-O', '', dest='output', help='Output "hosts", "services" or "both" (default)')
21 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
23 parser.set_defaults(url=None)
24 parser.set_defaults(httpuser=None)
25 parser.set_defaults(httppasswd=None)
26 parser.set_defaults(file='nagixsc.xml')
27 parser.set_defaults(schemacheck='')
28 parser.set_defaults(host=None)
29 parser.set_defaults(service=None)
30 parser.set_defaults(output=None)
31 parser.set_defaults(tmpl_host=None)
32 parser.set_defaults(tmpl_service=None)
33 parser.set_defaults(verb=0)
35 (options, args) = parser.parse_args()
37 # Hard coded default for host template
38 HOSTTEMPL='''define host {
39         use             templ_host_default
41         host_name       %(host_name)s
42         address         127.0.0.1
43 }
44 '''
46 # Hard coded default for service template
47 SERVICETEMPL='''define service {
48         use                     templ_service_passive
50         host_name               %(host_name)s
51         service_description     %(service_description)s
52         check_command           check_passive
53 }
54 '''
56 ##############################################################################
58 from nagixsc import *
60 ##############################################################################
62 # Output
63 if not options.output in [None, 'both', 'hosts', 'services']:
64         print 'Unknown output mode "%s"!' % options.output
65         sys.exit(1)
67 if options.output in [None, 'both']:
68         options.output = ['hosts', 'services']
69 else:
70         options.output = [options.output,]
72 # Read host and/or service template
73 if options.tmpl_host and 'hosts' in options.output:
74         HOSTTEMPL = open(options.tmpl_host).read()
75 if options.tmpl_service and 'services' in options.output:
76         SERVICETEMPL = open(options.tmpl_service).read()
78 # Get URL or file
79 doc = read_xml(options)
81 # Check XML against DTD
82 if options.schemacheck:
83         dtd = libxml2.parseDTD(None, options.schemacheck)
84         ctxt = libxml2.newValidCtxt()
85         ret = doc.validateDtd(ctxt, dtd)
86         if ret != 1:
87                 print "error doing DTD validation"
88                 sys.exit(1)
89         dtd.freeDtd()
90         del dtd
91         del ctxt
94 # Check XML file basics
95 (status, statusstring) = xml_check_version(doc)
96 debug(1, options.verb, statusstring)
97 if not status:
98         print statusstring
99         sys.exit(127)
102 # Put XML to Python dict
103 checks = xml_to_dict(doc, options.verb, options.host, options.service)
106 # Loop over check results and search for new hosts and new services
107 foundhosts = []
109 for check in checks:
110         if not check['host_name'] in foundhosts:
111                 foundhosts.append(check['host_name'])
113                 if 'hosts' in options.output:
114                         print HOSTTEMPL % check
116         if check['service_description'] and 'services' in options.output:
117                 print SERVICETEMPL % check