From 6507bf36d366bffed2c98ae8297b639f563360de Mon Sep 17 00:00:00 2001 From: Sven Velt Date: Tue, 8 Dec 2009 14:26:58 +0100 Subject: [PATCH] Allow HTTP username and password (-l/-a) --- dummy_read_xml.py | 13 +++++-------- nagixsc.py | 32 ++++++++++++++++++++++++++++++++ nagixsc_xml2nagios.py | 13 +++++-------- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/dummy_read_xml.py b/dummy_read_xml.py index d74d64d..1cd4363 100755 --- a/dummy_read_xml.py +++ b/dummy_read_xml.py @@ -9,12 +9,16 @@ import sys parser = optparse.OptionParser() parser.add_option('-u', '', dest='url', help='URL of status file (xml)') +parser.add_option('-l', '', dest='httpuser', help='HTTP user name') +parser.add_option('-a', '', dest='httppasswd', help='HTTP password') parser.add_option('-f', '', dest='file', help='(Path and) file name of status file') parser.add_option('-s', '', dest='seconds', type='int', help='Maximum age in seconds of xml timestamp') parser.add_option('-m', '', action='store_true', dest='markold', help='Mark (Set state) of too old checks as UNKNOWN') parser.add_option('-v', '', action='count', dest='verb', help='Verbose output') parser.set_defaults(url=None) +parser.set_defaults(httpuser=None) +parser.set_defaults(httppasswd=None) parser.set_defaults(file='nagixsc.xml') parser.set_defaults(seconds=14400) parser.set_defaults(markold=False) @@ -31,14 +35,7 @@ from nagixsc import * now = int(datetime.datetime.now().strftime('%s')) # Get URL or file -if options.url != None: - import urllib2 - - response = urllib2.urlopen(options.url) - doc = libxml2.parseDoc(response.read()) - response.close() -else: - doc = libxml2.parseFile(options.file) +doc = read_xml(options) # Check XML file basics diff --git a/nagixsc.py b/nagixsc.py index bf204c0..4f83486 100644 --- a/nagixsc.py +++ b/nagixsc.py @@ -1,6 +1,7 @@ import base64 import datetime import libxml2 +import sys def debug(level, verb, string): if level <= verb: @@ -27,6 +28,37 @@ def encode(data, encoding=None): return base64.b64encode(data) +############################################################################## + +def read_xml(options): + if options.url != None: + import urllib2 + + if options.httpuser and options.httppasswd: + passman = urllib2.HTTPPasswordMgrWithDefaultRealm() + passman.add_password(None, options.url, options.httpuser, options.httppasswd) + authhandler = urllib2.HTTPBasicAuthHandler(passman) + opener = urllib2.build_opener(authhandler) + urllib2.install_opener(opener) + + try: + response = urllib2.urlopen(options.url) + except urllib2.HTTPError, error: + print error + sys.exit(0) + except urllib2.URLError, error: + print error.reason[1] + sys.exit(0) + + doc = libxml2.parseDoc(response.read()) + response.close() + + else: + doc = libxml2.parseFile(options.file) + + return doc + + ############################################################################## def xml_check_version(xmldoc): diff --git a/nagixsc_xml2nagios.py b/nagixsc_xml2nagios.py index 9b0a394..868bca5 100755 --- a/nagixsc_xml2nagios.py +++ b/nagixsc_xml2nagios.py @@ -16,6 +16,8 @@ MODEs = [ 'passive', 'passive_check', 'checkresult', 'checkresult_check', 'activ parser = optparse.OptionParser() parser.add_option('-u', '', dest='url', help='URL of status file (xml)') +parser.add_option('-l', '', dest='httpuser', help='HTTP user name') +parser.add_option('-a', '', dest='httppasswd', help='HTTP password') parser.add_option('-f', '', dest='file', help='(Path and) file name of status file') parser.add_option('-S', '', dest='schemacheck', help='Check XML against DTD') parser.add_option('-s', '', dest='seconds', type='int', help='Maximum age in seconds of xml timestamp') @@ -28,6 +30,8 @@ parser.add_option('-D', '', dest='service', help='Service description to search parser.add_option('-v', '', action='count', dest='verb', help='Verbose output') parser.set_defaults(url=None) +parser.set_defaults(httpuser=None) +parser.set_defaults(httppasswd=None) parser.set_defaults(file='nagixsc.xml') parser.set_defaults(schemacheck='') parser.set_defaults(seconds=14400) @@ -91,14 +95,7 @@ elif options.mode == 'active': now = int(datetime.datetime.now().strftime('%s')) # Get URL or file -if options.url != None: - import urllib2 - - response = urllib2.urlopen(options.url) - doc = libxml2.parseDoc(response.read()) - response.close() -else: - doc = libxml2.parseFile(options.file) +doc = read_xml(options) # Check XML against DTD -- 2.30.2