Add Basic Auth support

This commit is contained in:
Chris Proto 2014-06-12 12:13:38 -06:00
parent a322e17c63
commit 1c57ed3360

View File

@ -7,7 +7,7 @@ Generic Nagios plugin which checks json values from a given endpoint against arg
and determines the status and performance data for that service. and determines the status and performance data for that service.
""" """
import httplib, urllib, urllib2 import httplib, urllib, urllib2, base64
import json import json
import argparse import argparse
from pprint import pprint from pprint import pprint
@ -142,6 +142,7 @@ def parseArgs():
and determines the status and performance data for that service') and determines the status and performance data for that service')
parser.add_argument('-H', '--host', dest='host', required=True, help='Host.') parser.add_argument('-H', '--host', dest='host', required=True, help='Host.')
parser.add_argument('-B', '--basic-auth', dest='auth', required=False, help='Basic auth string "username:password"')
parser.add_argument('-p', '--path', dest='path', help='Path.') parser.add_argument('-p', '--path', dest='path', help='Path.')
parser.add_argument('-e', '--key_exists', dest='key_list', nargs='*', parser.add_argument('-e', '--key_exists', dest='key_list', nargs='*',
help='Checks existence of these keys to determine status.') help='Checks existence of these keys to determine status.')
@ -180,6 +181,9 @@ if __name__ == "__main__":
# Attempt to reach the endpoint # Attempt to reach the endpoint
try: try:
req = urllib2.Request(url) req = urllib2.Request(url)
if args.auth:
base64str = base64.encodestring(args.auth).replace('\n', '')
req.add_header('Authorization', 'Basic %s' % base64str)
response = urllib2.urlopen(req) response = urllib2.urlopen(req)
except HTTPError as e: except HTTPError as e:
nagios.unknown("HTTPError[%s], url:%s" % (str(e.code), url)) nagios.unknown("HTTPError[%s], url:%s" % (str(e.code), url))