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

@ -31,7 +31,7 @@ class NagiosHelper:
text += "|%s" % self.performance_data
return text
def setCodeAndMessage(self, code, text):
def setCodeAndMessage(self, code, text):
self.code = code
self.message_text = text
@ -74,8 +74,8 @@ class JsonHelper:
return self.get(remainingKey, data[index])
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'))
@ -95,7 +95,7 @@ class JsonHelper:
else:
return self.getSubArrayElement(key, data)
else:
if key.find(self.separator) != -1 :
if key.find(self.separator) != -1 :
return self.getSubElement(key, data)
else:
if key.find(self.arrayOpener) != -1 :
@ -178,7 +178,7 @@ class JsonRuleProcessor:
metrics += ' '
return "%s" % metrics
def parseArgs():
@ -190,17 +190,19 @@ 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('-e', '--key_exists', dest='key_list', nargs='*',
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.')
parser.add_argument('-l', '--key_lte', dest='key_lte_list', nargs='*',
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.\
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.')
parser.add_argument('-g', '--key_gte', dest='key_gte_list', nargs='*',
parser.add_argument('-g', '--key_gte', dest='key_gte_list', nargs='*',
help='Checks that these keys and values (key,value key2,value2) are greater than or equal to\
the returned json value to determine status.')
parser.add_argument('-m', '--key_metric', dest='metric_list', nargs='*',
parser.add_argument('-m', '--key_metric', dest='metric_list', nargs='*',
help='Gathers the values of these keys (key,UnitOfMeasure,Min,Max,WarnRange,CriticalRange) for Nagios performance data.\
More information about Range format and units of measure for nagios can be found at https://nagios-plugins.org/doc/guidelines.html\
Additional formats for this parameter are: (key), (key,UnitOfMeasure), (key,UnitOfMeasure,Min,Max).')
@ -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)