From fbebf05f76e1efc7f4a052974f7b649213f12574 Mon Sep 17 00:00:00 2001 From: Beri Date: Fri, 8 Jan 2016 16:21:00 +0100 Subject: [PATCH] Support for custom HTTP Headers added using -A parameter. --- README.md | 9 ++++++++- check_http_json.py | 8 +++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 98150c9..63c06c5 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,8 @@ Executing `./check_http_json.py -h` will yield the following details: ``` usage: check_http_json.py [-h] [-d] [-s] -H HOST [-P PORT] [-p PATH] - [-t TIMEOUT] [-B AUTH] [-D DATA] [-f SEPARATOR] + [-t TIMEOUT] [-B AUTH] [-D DATA] [-A HEADERS] + [-f SEPARATOR] [-w [KEY_THRESHOLD_WARNING [KEY_THRESHOLD_WARNING ...]]] [-c [KEY_THRESHOLD_CRITICAL [KEY_THRESHOLD_CRITICAL ...]]] [-e [KEY_LIST [KEY_LIST ...]]] @@ -41,6 +42,8 @@ optional arguments: -B AUTH, --basic-auth AUTH Basic auth string "username:password" -D DATA, --data DATA The http payload to send as a POST + -A HEADERS, --headers HEADERS + The http headers in JSON format. -f SEPARATOR, --field_separator SEPARATOR Json Field separator, defaults to "." ; Select element in an array with "(" ")" @@ -156,6 +159,10 @@ optional arguments: More info about Nagios Range format and Units of Measure can be found at [https://nagios-plugins.org/doc/guidelines.html](https://nagios-plugins.org/doc/guidelines.html). +#### Using Headers + +* `./check_http_json.py -H : -p -A '{"content-type": "application/json"}' -w "metric,RANGE"` + ## Nagios Installation ### Requirements diff --git a/check_http_json.py b/check_http_json.py index 05a65df..8dd1327 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -56,7 +56,7 @@ class NagiosHelper: def append_critical(self, critical_message): self.critical_message += critical_message def append_unknown(self, unknown_message): - self.critical_message += critical_message + self.critical_message += unknown_message def append_metrics(self, (performance_data, warning_message, critical_message)): self.performance_data += performance_data self.append_warning(warning_message) @@ -275,6 +275,7 @@ def parseArgs(): parser.add_argument('-t', '--timeout', type=int, help='Connection timeout (seconds)') parser.add_argument('-B', '--basic-auth', dest='auth', help='Basic auth string "username:password"') parser.add_argument('-D', '--data', dest='data', help='The http payload to send as a POST') + parser.add_argument('-A', '--headers', dest='headers', help='The http headers in JSON format.') parser.add_argument('-f', '--field_separator', dest='separator', help='Json Field separator, defaults to "." ; Select element in an array with "(" ")"') parser.add_argument('-w', '--warning', dest='key_threshold_warning', nargs='*', @@ -409,6 +410,11 @@ if __name__ == "__main__": if args.auth: base64str = base64.encodestring(args.auth).replace('\n', '') req.add_header('Authorization', 'Basic %s' % base64str) + if args.headers: + headers=json.loads(args.headers) + debugPrint(args.debug, "Headers:\n %s" % headers) + for header in headers: + req.add_header(header, headers[header]) if args.timeout and args.data: response = urllib2.urlopen(req, timeout=args.timeout, data=args.data) elif args.timeout: