Code

Split write_xml(), add write_xml_or_die()
[nagixsc.git] / nagixsc_conf2xml.py
index 3105730351fd09073b363356dc0f8b0b3c58ae07..153c910914b86da2b08849acfe15c7122578e1da 100755 (executable)
@@ -2,6 +2,7 @@
 
 import optparse
 import sys
+import urllib2
 
 ##############################################################################
 
@@ -12,10 +13,13 @@ from nagixsc import *
 parser = optparse.OptionParser()
 
 parser.add_option('-c', '', dest='conffile', help='Config file')
-parser.add_option('-o', '', dest='outfile', help='Output file')
+parser.add_option('-o', '', dest='outfile', help='Output file name, "-" for STDOUT or HTTP POST URL')
 parser.add_option('-e', '', dest='encoding', help='Encoding ("%s")' % '", "'.join(available_encodings()) )
 parser.add_option('-H', '', dest='host', help='Hostname/section to search for in config file')
 parser.add_option('-D', '', dest='service', help='Service description to search for in config file (needs -H)')
+parser.add_option('-l', '', dest='httpuser', help='HTTP user name, if outfile is HTTP(S) URL')
+parser.add_option('-a', '', dest='httppasswd', help='HTTP password, if outfile is HTTP(S) URL')
+parser.add_option('-q', '', action='store_true', dest='quiet', help='Be quiet')
 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
 
 parser.set_defaults(conffile='nagixsc.conf')
@@ -42,12 +46,14 @@ if not config:
        print 'Config file "%s" could not be read!' % options.conffile
        sys.exit(5)
 
+# Execute checks, build dict
 checks = conf2dict(config, options.host, options.service)
 
+# Convert to XML
 xmldoc = xml_from_dict(checks, options.encoding)
-if options.outfile == '-':
-       xmldoc.saveFormatFile('-', format=1)
-else:
-       xmldoc.saveFile(options.outfile)
 
+# Output
+response = write_xml_or_die(xmldoc, options.outfile, options.httpuser, options.httppasswd)
+if response and not options.quiet:
+       print response