From aebc0e2ce064c87ce033372bc656e14cbd78c45d Mon Sep 17 00:00:00 2001 From: Sven Velt Date: Mon, 6 Sep 2010 11:15:35 +0200 Subject: [PATCH] New function write_xml Signed-off-by: Sven Velt --- nagixsc/__init__.py | 26 ++++++++++++++++++++++++-- nagixsc_conf2xml.py | 23 ++++------------------- nagixsc_live2xml.py | 20 +------------------- 3 files changed, 29 insertions(+), 40 deletions(-) diff --git a/nagixsc/__init__.py b/nagixsc/__init__.py index c47c0ea..03884e3 100644 --- a/nagixsc/__init__.py +++ b/nagixsc/__init__.py @@ -13,6 +13,7 @@ import socket import string import subprocess import sys +import urllib2 def debug(level, verb, string): if level <= verb: @@ -254,7 +255,6 @@ def dict2out_checkresult(checks, xmltimestamp, opt_checkresultdir, opt_verb): def read_xml(options): if options.url != None: - import urllib2 if options.httpuser and options.httppasswd: passman = urllib2.HTTPPasswordMgrWithDefaultRealm() @@ -285,6 +285,28 @@ def read_xml_from_string(content): return libxml2.parseDoc(content) +def write_xml(xmldoc, outfile, httpuser=None, httppasswd=None): + if outfile.startswith('http'): + (headers, body) = encode_multipart(xmldoc, httpuser, httppasswd) + + try: + response = urllib2.urlopen(urllib2.Request(outfile, body, headers)).read() + except urllib2.HTTPError, error: + print error + sys.exit(11) + except urllib2.URLError, error: + print error.reason[1] + sys.exit(12) + + print response + + elif outfile == '-': + xmldoc.saveFormatFile('-', format=1) + + else: + xmldoc.saveFile(outfile) + + ############################################################################## def xml_check_version(xmldoc): @@ -446,7 +468,7 @@ def reset_future_timestamp(timestamp, now): ############################################################################## -def encode_multipart(xmldoc, httpuser, httppasswd): +def encode_multipart(xmldoc, httpuser=None, httppasswd=None): BOUNDARY = mimetools.choose_boundary() CRLF = '\r\n' L = [] diff --git a/nagixsc_conf2xml.py b/nagixsc_conf2xml.py index 5d5da3c..508f0d0 100755 --- a/nagixsc_conf2xml.py +++ b/nagixsc_conf2xml.py @@ -45,27 +45,12 @@ 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.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) +# Output +write_xml(xmldoc, options.outfile, options.httpuser, options.httppasswd) diff --git a/nagixsc_live2xml.py b/nagixsc_live2xml.py index 3e2729f..4702f95 100755 --- a/nagixsc_live2xml.py +++ b/nagixsc_live2xml.py @@ -53,23 +53,5 @@ checks = livestatus2dict(s_opts, options.host, options.service) xmldoc = xml_from_dict(checks, options.encoding) # Output -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) +write_xml(xmldoc, options.outfile, options.httpuser, options.httppasswd) -- 2.30.2