Code

New function write_xml
[nagixsc.git] / nagixsc_conf2xml.py
1 #!/usr/bin/python
3 import optparse
4 import sys
5 import urllib2
7 ##############################################################################
9 from nagixsc import *
11 ##############################################################################
13 parser = optparse.OptionParser()
15 parser.add_option('-c', '', dest='conffile', help='Config file')
16 parser.add_option('-o', '', dest='outfile', help='Output file name, "-" for STDOUT or HTTP POST URL')
17 parser.add_option('-e', '', dest='encoding', help='Encoding ("%s")' % '", "'.join(available_encodings()) )
18 parser.add_option('-H', '', dest='host', help='Hostname/section to search for in config file')
19 parser.add_option('-D', '', dest='service', help='Service description to search for in config file (needs -H)')
20 parser.add_option('-l', '', dest='httpuser', help='HTTP user name, if outfile is HTTP(S) URL')
21 parser.add_option('-a', '', dest='httppasswd', help='HTTP password, if outfile is HTTP(S) URL')
22 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
24 parser.set_defaults(conffile='nagixsc.conf')
25 parser.set_defaults(outfile='-')
26 parser.set_defaults(encoding='base64')
27 parser.set_defaults(host=None)
28 parser.set_defaults(service=None)
29 parser.set_defaults(verb=0)
31 (options, args) = parser.parse_args()
33 ##############################################################################
35 if not check_encoding(options.encoding):
36         print 'Wrong encoding method "%s"!' % options.encoding
37         print 'Could be one of: "%s"' % '", "'.join(available_encodings())
38         sys.exit(127)
40 ##############################################################################
42 config = read_inifile(options.conffile)
44 if not config:
45         print 'Config file "%s" could not be read!' % options.conffile
46         sys.exit(5)
48 # Execute checks, build dict
49 checks = conf2dict(config, options.host, options.service)
51 # Convert to XML
52 xmldoc = xml_from_dict(checks, options.encoding)
54 # Output
55 write_xml(xmldoc, options.outfile, options.httpuser, options.httppasswd)