Merge branch 'master' of github.com:drewkerrigan/nagios-http-json

This commit is contained in:
drewkerrigan 2015-10-05 10:36:46 -04:00
commit 55b979f3e2
2 changed files with 27 additions and 18 deletions

View File

@ -47,13 +47,13 @@ More info about options in Usage.
Executing `./check_http_json.py -h` will yield the following details:
```
usage: check_http_json.py [-h] -H HOST [-B AUTH] [-p PATH]
usage: check_http_json.py [-h] -H HOST [-P PORT] [-B AUTH] [-p PATH] [-D DATA]
[-e [KEY_LIST [KEY_LIST ...]]]
[-q [KEY_VALUE_LIST [KEY_VALUE_LIST ...]]]
[-l [KEY_LTE_LIST [KEY_LTE_LIST ...]]]
[-g [KEY_GTE_LIST [KEY_GTE_LIST ...]]]
[-m [METRIC_LIST [METRIC_LIST ...]]] [-s]
[-f SEPARATOR] [-d]
[-t TIMEOUT] [-f SEPARATOR] [-d]
Nagios plugin which checks json values from a given endpoint against argument
specified rules and determines the status and performance data for that
@ -62,12 +62,14 @@ service
optional arguments:
-h, --help show this help message and exit
-H HOST, --host HOST Host.
-H PORT, --port PORT TCP port.
-P PORT, --port PORT TCP port
-B AUTH, --basic-auth AUTH
Basic auth string "username:password"
-p PATH, --path PATH Path.
-D DATA, --data DATA The http payload to send as an POST.
-e [KEY_LIST [KEY_LIST ...]], --key_exists [KEY_LIST [KEY_LIST ...]]
Checks existence of these keys to determine status.
Checks existence of these keys to determine status. Multiple key values can
be delimited with colon (key,value1:value2).
-q [KEY_VALUE_LIST [KEY_VALUE_LIST ...]], --key_equals [KEY_VALUE_LIST [KEY_VALUE_LIST ...]]
Checks equality of these keys and values (key,value
key2,value2) to determine status.

View File

@ -75,7 +75,7 @@ class JsonHelper:
else:
return (None, 'not_found')
def equals(self, key, value): return self.exists(key) and str(self.get(key)) == value
def equals(self, key, value): return self.exists(key) and str(self.get(key)) in value.split(':')
def lte(self, key, value): return self.exists(key) and float(self.get(key)) <= float(value)
def gte(self, key, value): return self.exists(key) and float(self.get(key)) >= float(value)
def exists(self, key): return (self.get(key) != (None, 'not_found'))
@ -190,10 +190,12 @@ def parseArgs():
parser.add_argument('-P', '--port', dest='port', required=False, help='TCP port')
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('-D', '--data', dest='data', help='The http payload to send as a POST')
parser.add_argument('-e', '--key_exists', dest='key_list', nargs='*',
help='Checks existence of these keys to determine status.')
parser.add_argument('-q', '--key_equals', dest='key_value_list', nargs='*',
help='Checks equality of these keys and values (key,value key2,value2) to determine status.')
help='Checks equality of these keys and values (key,value key2,value2) to determine status.\
Multiple key values can be delimited with colon (key,value1:value2)')
parser.add_argument('-l', '--key_lte', dest='key_lte_list', nargs='*',
help='Checks that these keys and values (key,value key2,value2) are less than or equal to\
the returned json value to determine status.')
@ -238,8 +240,12 @@ if __name__ == "__main__":
if args.auth:
base64str = base64.encodestring(args.auth).replace('\n', '')
req.add_header('Authorization', 'Basic %s' % base64str)
if args.timeout:
if args.timeout and args.data:
response = urllib2.urlopen(req, timeout=args.timeout, data=args.data)
elif args.timeout:
response = urllib2.urlopen(req, timeout=args.timeout)
elif args.data:
response = urllib2.urlopen(req, data=args.data)
else:
response = urllib2.urlopen(req)
except HTTPError as e:
@ -257,9 +263,10 @@ if __name__ == "__main__":
processor = JsonRuleProcessor(data, args)
is_alive, reason = processor.isAlive()
# Gather metrics for display
nagios.performance_data = processor.getMetrics()
if is_alive:
# Rules all passed, attempt to get performance data
nagios.performance_data = processor.getMetrics()
nagios.ok("Status OK.")
else:
nagios.warning("Status check failed, reason:%s" % reason)