From 8ed4dffad9f52651aae67f226fbf01017330f1ea Mon Sep 17 00:00:00 2001 From: Sven Velt Date: Thu, 23 Sep 2010 15:32:39 +0200 Subject: [PATCH] Split write_xml(), add write_xml_or_die() Added "-q" to conf2xml and live2xml Signed-off-by: Sven Velt --- nagixsc/__init__.py | 28 +++++++++++++++++----------- nagixsc_conf2xml.py | 5 ++++- nagixsc_live2xml.py | 5 ++++- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/nagixsc/__init__.py b/nagixsc/__init__.py index 5bdde68..2a93b86 100644 --- a/nagixsc/__init__.py +++ b/nagixsc/__init__.py @@ -288,23 +288,29 @@ def read_xml_from_string(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 + response = urllib2.urlopen(urllib2.Request(outfile, body, headers)).read() + return response elif outfile == '-': xmldoc.saveFormatFile('-', format=1) + return None else: xmldoc.saveFile(outfile) + return None + + +def write_xml_or_die(xmldoc, outfile, httpuser=None, httppasswd=None): + try: + response = write_xml(xmldoc, outfile, httpuser=None, httppasswd=None) + except urllib2.HTTPError, error: + print error + sys.exit(11) + except urllib2.URLError, error: + print error.reason[1] + sys.exit(12) + + return response ############################################################################## diff --git a/nagixsc_conf2xml.py b/nagixsc_conf2xml.py index 508f0d0..153c910 100755 --- a/nagixsc_conf2xml.py +++ b/nagixsc_conf2xml.py @@ -19,6 +19,7 @@ parser.add_option('-H', '', dest='host', help='Hostname/section to search for in 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') @@ -52,5 +53,7 @@ checks = conf2dict(config, options.host, options.service) xmldoc = xml_from_dict(checks, options.encoding) # Output -write_xml(xmldoc, options.outfile, options.httpuser, options.httppasswd) +response = write_xml_or_die(xmldoc, options.outfile, options.httpuser, options.httppasswd) +if response and not options.quiet: + print response diff --git a/nagixsc_live2xml.py b/nagixsc_live2xml.py index 4702f95..281c675 100755 --- a/nagixsc_live2xml.py +++ b/nagixsc_live2xml.py @@ -18,6 +18,7 @@ parser.add_option('-H', '', dest='host', help='Hostname/section to search for in 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(socket=None) @@ -53,5 +54,7 @@ checks = livestatus2dict(s_opts, options.host, options.service) xmldoc = xml_from_dict(checks, options.encoding) # Output -write_xml(xmldoc, options.outfile, options.httpuser, options.httppasswd) +response = write_xml_or_die(xmldoc, options.outfile, options.httpuser, options.httppasswd) +if response and not options.quiet: + print response -- 2.30.2