summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 828e84c)
raw | patch | inline | side by side (parent: 828e84c)
author | Sven Velt <sven@velt.de> | |
Wed, 17 Feb 2010 15:51:00 +0000 (16:51 +0100) | ||
committer | Sven Velt <sven@velt.de> | |
Wed, 17 Feb 2010 15:51:00 +0000 (16:51 +0100) |
It's not longer necessary to create and XML file, save it and
HTTP(S)-POST it with curl. Just use a URL as output file, add -l/-a if
login is required and be happy :)
HTTP(S)-POST it with curl. Just use a URL as output file, add -l/-a if
login is required and be happy :)
nagixsc.py | patch | blob | history | |
nagixsc_conf2xml.py | patch | blob | history |
diff --git a/nagixsc.py b/nagixsc.py
index 191d26e2f0a1e90b9f6197f450a8c2f53cc61ac4..56c4cf3ead117ca047a818085912232a5ad514d4 100644 (file)
--- a/nagixsc.py
+++ b/nagixsc.py
import base64
import datetime
import libxml2
+import mimetools
import os
import random
import shlex
##############################################################################
+def encode_multipart(xmldoc, httpuser, httppasswd):
+ BOUNDARY = mimetools.choose_boundary()
+ CRLF = '\r\n'
+ L = []
+ L.append('--' + BOUNDARY)
+ L.append('Content-Disposition: form-data; name="xmlfile"; filename="xmlfile"')
+ L.append('Content-Type: application/xml')
+ L.append('')
+ L.append(xmldoc.serialize())
+ L.append('--' + BOUNDARY + '--')
+ L.append('')
+ body = CRLF.join(L)
+ content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
+ headers = {'Content-Type': content_type, 'Content-Length': str(len(body))}
+
+ if httpuser and httppasswd:
+ headers['Authorization'] = 'Basic %s' % base64.b64encode(':'.join([httpuser, httppasswd]))
+
+ return (headers, body)
+
+##############################################################################
+
class MyHTTPServer(BaseHTTPServer.HTTPServer):
def __init__(self, server_address, HandlerClass, ssl=False, sslpemfile=None):
if ssl:
diff --git a/nagixsc_conf2xml.py b/nagixsc_conf2xml.py
index 3105730351fd09073b363356dc0f8b0b3c58ae07..5d5da3c04cc9a7b031d3f6908992d9876ff5590c 100755 (executable)
--- a/nagixsc_conf2xml.py
+++ b/nagixsc_conf2xml.py
import optparse
import sys
+import urllib2
##############################################################################
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')
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)
-