From 1c57ed3360e7eb54199a62ab11c9d7da799343be Mon Sep 17 00:00:00 2001 From: Chris Proto Date: Thu, 12 Jun 2014 12:13:38 -0600 Subject: [PATCH] Add Basic Auth support --- check_http_json.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/check_http_json.py b/check_http_json.py index 2db2fd1..927f3ba 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -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. """ -import httplib, urllib, urllib2 +import httplib, urllib, urllib2, base64 import json import argparse from pprint import pprint @@ -142,6 +142,7 @@ def parseArgs(): and determines the status and performance data for that service') 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('-e', '--key_exists', dest='key_list', nargs='*', help='Checks existence of these keys to determine status.') @@ -180,6 +181,9 @@ if __name__ == "__main__": # Attempt to reach the endpoint try: 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) except HTTPError as e: nagios.unknown("HTTPError[%s], url:%s" % (str(e.code), url)) @@ -205,4 +209,4 @@ if __name__ == "__main__": # Print Nagios specific string and exit appropriately print nagios.getMessage() - exit(nagios.code) \ No newline at end of file + exit(nagios.code)