Code

conf2xml: Added HTTP POST support
[nagixsc.git] / nagixsc_conf2xml.py
index 3105730351fd09073b363356dc0f8b0b3c58ae07..5d5da3c04cc9a7b031d3f6908992d9876ff5590c 100755 (executable)
@@ -2,6 +2,7 @@
 
 import optparse
 import sys
+import urllib2
 
 ##############################################################################
 
@@ -12,10 +13,12 @@ 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('-v', '', action='count', dest='verb', help='Verbose output')
 
 parser.set_defaults(conffile='nagixsc.conf')
@@ -45,9 +48,24 @@ if not config:
 checks = conf2dict(config, options.host, options.service)
 
 xmldoc = xml_from_dict(checks, options.encoding)
-if options.outfile == '-':
+
+if options.outfile.startswith('http'):
+       (headers, body) = encode_multipart(xmldoc, options.httpuser, options.httppasswd)
+
+       try:
+               response = urllib2.urlopen(urllib2.Request(options.outfile, body, headers)).read()
+       except urllib2.HTTPError, error:
+               print error
+               sys.exit(6)
+       except urllib2.URLError, error:
+               print error.reason[1]
+               sys.exit(7)
+
+       print response
+
+elif options.outfile == '-':
        xmldoc.saveFormatFile('-', format=1)
+
 else:
        xmldoc.saveFile(options.outfile)
 
-