merging + adding -f for Json Field Separator

This commit is contained in:
Drew Kerrigan 2015-04-24 11:38:27 -04:00
commit d1ba09f44c
2 changed files with 25 additions and 11 deletions

View File

@ -47,13 +47,13 @@ More info about options in Usage.
Executing `./check_http_json.py -h` will yield the following details: Executing `./check_http_json.py -h` will yield the following details:
``` ```
usage: check_http_json.py [-h] -H HOST [-p PATH] usage: check_http_json.py [-h] -H HOST [-B AUTH] [-p PATH]
[-e [KEY_LIST [KEY_LIST ...]]] [-e [KEY_LIST [KEY_LIST ...]]]
[-q [KEY_VALUE_LIST [KEY_VALUE_LIST ...]]] [-q [KEY_VALUE_LIST [KEY_VALUE_LIST ...]]]
[-l [KEY_LTE_LIST [KEY_LTE_LIST ...]]] [-l [KEY_LTE_LIST [KEY_LTE_LIST ...]]]
[-g [KEY_GTE_LIST [KEY_GTE_LIST ...]]] [-g [KEY_GTE_LIST [KEY_GTE_LIST ...]]]
[-m [METRIC_LIST [METRIC_LIST ...]]] [-d] [-m [METRIC_LIST [METRIC_LIST ...]]] [-s]
[-s SEPARATOR] [-f SEPARATOR] [-d]
Nagios plugin which checks json values from a given endpoint against argument Nagios plugin which checks json values from a given endpoint against argument
specified rules and determines the status and performance data for that specified rules and determines the status and performance data for that
@ -62,6 +62,8 @@ service
optional arguments: optional arguments:
-h, --help show this help message and exit -h, --help show this help message and exit
-H HOST, --host HOST Host. -H HOST, --host HOST Host.
-B AUTH, --basic-auth AUTH
Basic auth string "username:password"
-p PATH, --path PATH Path. -p PATH, --path PATH Path.
-e [KEY_LIST [KEY_LIST ...]], --key_exists [KEY_LIST [KEY_LIST ...]] -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.
@ -85,9 +87,10 @@ optional arguments:
plugins.org/doc/guidelines.html Additional formats for plugins.org/doc/guidelines.html Additional formats for
this parameter are: (key), (key,UnitOfMeasure), this parameter are: (key), (key,UnitOfMeasure),
(key,UnitOfMeasure,Min,Max). (key,UnitOfMeasure,Min,Max).
-d, --debug Debug mode. -s, --ssl HTTPS mode.
-s SEPARATOR, --separator SEPARATOR -f SEPARATOR, --field_separator SEPARATOR
Json Field separator, defaults to "." Json Field separator, defaults to "."
-d, --debug Debug mode.
``` ```
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). 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).

View File

@ -7,10 +7,12 @@ 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 import httplib, urllib, urllib2, base64
import json import json
import argparse import argparse
from pprint import pprint from pprint import pprint
from urllib2 import HTTPError
from urllib2 import URLError
class NagiosHelper: class NagiosHelper:
@ -45,8 +47,8 @@ class JsonHelper:
self.separator = separator self.separator = separator
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)) == value
def lte(self, key, value): return self.exists(key) and str(self.get(key)) <= value 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 str(self.get(key)) >= 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')) def exists(self, key): return (self.get(key) != (None, 'not_found'))
def get(self, key, temp_data=''): def get(self, key, temp_data=''):
"""Can navigate nested json keys with a dot format (Element.Key.NestedKey). Returns (None, 'not_found') if not found""" """Can navigate nested json keys with a dot format (Element.Key.NestedKey). Returns (None, 'not_found') if not found"""
@ -144,6 +146,7 @@ def parseArgs():
and determines the status and performance data for that service') and determines the status and performance data for that service')
parser.add_argument('-H', '--host', dest='host', required=True, help='Host.') 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('-p', '--path', dest='path', help='Path.')
parser.add_argument('-e', '--key_exists', dest='key_list', nargs='*', parser.add_argument('-e', '--key_exists', dest='key_list', nargs='*',
help='Checks existence of these keys to determine status.') help='Checks existence of these keys to determine status.')
@ -159,8 +162,9 @@ def parseArgs():
help='Gathers the values of these keys (key,UnitOfMeasure,Min,Max,WarnRange,CriticalRange) for Nagios performance data.\ 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\ 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).') Additional formats for this parameter are: (key), (key,UnitOfMeasure), (key,UnitOfMeasure,Min,Max).')
parser.add_argument('-s', '--ssl', action='store_true', help='HTTPS mode.')
parser.add_argument('-f', '--field_separator', dest='separator', help='Json Field separator, defaults to "."')
parser.add_argument('-d', '--debug', action='store_true', help='Debug mode.') parser.add_argument('-d', '--debug', action='store_true', help='Debug mode.')
parser.add_argument('-s', '--separator', dest='separator', help='Json Field separator, defaults to "."')
return parser.parse_args() return parser.parse_args()
@ -176,13 +180,20 @@ if __name__ == "__main__":
args = parseArgs() args = parseArgs()
nagios = NagiosHelper() nagios = NagiosHelper()
url = "http://%s" % args.host if args.ssl:
url = "https://%s" % args.host
else:
url = "http://%s" % args.host
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
try: try:
req = urllib2.Request(url) 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) response = urllib2.urlopen(req)
except HTTPError as e: except HTTPError as e:
nagios.unknown("HTTPError[%s], url:%s" % (str(e.code), url)) nagios.unknown("HTTPError[%s], url:%s" % (str(e.code), url))
@ -208,4 +219,4 @@ if __name__ == "__main__":
# Print Nagios specific string and exit appropriately # Print Nagios specific string and exit appropriately
print nagios.getMessage() print nagios.getMessage()
exit(nagios.code) exit(nagios.code)