Code

New function write_xml
authorSven Velt <sven@velt.de>
Mon, 6 Sep 2010 09:15:35 +0000 (11:15 +0200)
committerSven Velt <sven@velt.de>
Mon, 6 Sep 2010 09:15:35 +0000 (11:15 +0200)
Signed-off-by: Sven Velt <sven@velt.de>
nagixsc/__init__.py
nagixsc_conf2xml.py
nagixsc_live2xml.py

index c47c0ea64ae85accf780046295bd1048b1461834..03884e36b9f22df2a4cda5d51ede2c410382fe4d 100644 (file)
@@ -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 = []
index 5d5da3c04cc9a7b031d3f6908992d9876ff5590c..508f0d0e8196e074940e4ea0c1e58240cfc50b72 100755 (executable)
@@ -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)
 
index 3e2729f12cafcbf1111a50263054f8cf9c798477..4702f95207361b548af9b0e1d3801d5ba97cd6ef 100755 (executable)
@@ -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)