added option -I (--insecure) to disable certificate validation

This commit is contained in:
janrot 2016-06-28 18:25:42 +02:00
parent 9c7465f8bb
commit a1ec0fa3c7
2 changed files with 16 additions and 6 deletions

View File

@ -16,7 +16,7 @@ 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] usage: check_http_json.py [-h] [-d] [-s] -H HOST [-P PORT] [-p PATH]
[-t TIMEOUT] [-B AUTH] [-D DATA] [-A HEADERS] [-t TIMEOUT] [-B AUTH] [-D DATA] [-A HEADERS] [-I]
[-f SEPARATOR] [-f SEPARATOR]
[-w [KEY_THRESHOLD_WARNING [KEY_THRESHOLD_WARNING ...]]] [-w [KEY_THRESHOLD_WARNING [KEY_THRESHOLD_WARNING ...]]]
[-c [KEY_THRESHOLD_CRITICAL [KEY_THRESHOLD_CRITICAL ...]]] [-c [KEY_THRESHOLD_CRITICAL [KEY_THRESHOLD_CRITICAL ...]]]
@ -44,6 +44,7 @@ optional arguments:
-D DATA, --data DATA The http payload to send as a POST -D DATA, --data DATA The http payload to send as a POST
-A HEADERS, --headers HEADERS -A HEADERS, --headers HEADERS
The http headers in JSON format. The http headers in JSON format.
-I, --insecure Do not validate certificates
-f SEPARATOR, --field_separator SEPARATOR -f SEPARATOR, --field_separator SEPARATOR
Json Field separator, defaults to "." ; Select element Json Field separator, defaults to "." ; Select element
in an array with "(" ")" in an array with "(" ")"
@ -81,6 +82,7 @@ optional arguments:
formats for this parameter are: (key[>alias]), formats for this parameter are: (key[>alias]),
(key[>alias],UnitOfMeasure), (key[>alias],UnitOfMeasure),
(key[>alias],UnitOfMeasure,WarnRange,CriticalRange). (key[>alias],UnitOfMeasure,WarnRange,CriticalRange).
``` ```
## Examples ## Examples

View File

@ -7,9 +7,10 @@ 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, base64 import urllib2, base64
import json import json
import argparse import argparse
import ssl
import sys import sys
from pprint import pprint from pprint import pprint
from urllib2 import HTTPError from urllib2 import HTTPError
@ -276,6 +277,7 @@ def parseArgs():
parser.add_argument('-B', '--basic-auth', dest='auth', help='Basic auth string "username:password"') 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('-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('-A', '--headers', dest='headers', help='The http headers in JSON format.')
parser.add_argument('-I', '--insecure', dest='insecure', help='Do not validate certificates', action='store_true')
parser.add_argument('-f', '--field_separator', dest='separator', parser.add_argument('-f', '--field_separator', dest='separator',
help='Json Field separator, defaults to "." ; Select element in an array with "(" ")"') help='Json Field separator, defaults to "." ; Select element in an array with "(" ")"')
parser.add_argument('-w', '--warning', dest='key_threshold_warning', nargs='*', parser.add_argument('-w', '--warning', dest='key_threshold_warning', nargs='*',
@ -405,6 +407,12 @@ if __name__ == "__main__":
if args.path: url += "/%s" % args.path if args.path: url += "/%s" % args.path
debugPrint(args.debug, "url:%s" % url) debugPrint(args.debug, "url:%s" % url)
# Attempt to reach the endpoint # Attempt to reach the endpoint
ctx = ssl.create_default_context()
if args.insecure:
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
try: try:
req = urllib2.Request(url) req = urllib2.Request(url)
req.add_header("User-Agent", "nagios-http-json") req.add_header("User-Agent", "nagios-http-json")
@ -417,13 +425,13 @@ if __name__ == "__main__":
for header in headers: for header in headers:
req.add_header(header, headers[header]) req.add_header(header, headers[header])
if args.timeout and args.data: if args.timeout and args.data:
response = urllib2.urlopen(req, timeout=args.timeout, data=args.data) response = urllib2.urlopen(req, timeout=args.timeout, data=args.data, context=ctx)
elif args.timeout: elif args.timeout:
response = urllib2.urlopen(req, timeout=args.timeout) response = urllib2.urlopen(req, timeout=args.timeout, context=ctx)
elif args.data: elif args.data:
response = urllib2.urlopen(req, data=args.data) response = urllib2.urlopen(req, data=args.data, context=ctx)
else: else:
response = urllib2.urlopen(req) response = urllib2.urlopen(req, context=ctx)
except HTTPError as e: except HTTPError as e:
nagios.append_unknown("HTTPError[%s], url:%s" % (str(e.code), url)) nagios.append_unknown("HTTPError[%s], url:%s" % (str(e.code), url))
except URLError as e: except URLError as e: