Skip to content

Commit

Permalink
merging + adding -f for Json Field Separator
Browse files Browse the repository at this point in the history
  • Loading branch information
drewkerrigan committed Apr 24, 2015
2 parents 171826f + 23ffc5e commit d1ba09f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [-p PATH]
usage: check_http_json.py [-h] -H HOST [-B AUTH] [-p PATH]
[-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 ...]]] [-d]
[-s SEPARATOR]
[-m [METRIC_LIST [METRIC_LIST ...]]] [-s]
[-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
Expand All @@ -62,6 +62,8 @@ service
optional arguments:
-h, --help show this help message and exit
-H HOST, --host HOST Host.
-B AUTH, --basic-auth AUTH
Basic auth string "username:password"
-p PATH, --path PATH Path.
-e [KEY_LIST [KEY_LIST ...]], --key_exists [KEY_LIST [KEY_LIST ...]]
Checks existence of these keys to determine status.
Expand All @@ -85,9 +87,10 @@ optional arguments:
plugins.org/doc/guidelines.html Additional formats for
this parameter are: (key), (key,UnitOfMeasure),
(key,UnitOfMeasure,Min,Max).
-d, --debug Debug mode.
-s SEPARATOR, --separator SEPARATOR
-s, --ssl HTTPS mode.
-f SEPARATOR, --field_separator SEPARATOR
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).
Expand Down
23 changes: 17 additions & 6 deletions check_http_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
and determines the status and performance data for that service.
"""

import httplib, urllib, urllib2
import httplib, urllib, urllib2, base64
import json
import argparse
from pprint import pprint
from urllib2 import HTTPError
from urllib2 import URLError


class NagiosHelper:
Expand Down Expand Up @@ -45,8 +47,8 @@ def __init__(self, json_data, separator):
self.separator = separator

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 gte(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 float(self.get(key)) >= float(value)
def exists(self, key): return (self.get(key) != (None, 'not_found'))
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"""
Expand Down Expand Up @@ -144,6 +146,7 @@ def parseArgs():
and determines the status and performance data for that service')

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('-e', '--key_exists', dest='key_list', nargs='*',
help='Checks existence of these keys to determine status.')
Expand All @@ -159,8 +162,9 @@ def parseArgs():
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).')
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('-s', '--separator', dest='separator', help='Json Field separator, defaults to "."')

return parser.parse_args()

Expand All @@ -176,13 +180,20 @@ def debugPrint(debug_flag, message, pretty_flag=False):
args = parseArgs()
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
debugPrint(args.debug, "url:%s" % url)

# Attempt to reach the endpoint
try:
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)
except HTTPError as e:
nagios.unknown("HTTPError[%s], url:%s" % (str(e.code), url))
Expand All @@ -208,4 +219,4 @@ def debugPrint(debug_flag, message, pretty_flag=False):

# Print Nagios specific string and exit appropriately
print nagios.getMessage()
exit(nagios.code)
exit(nagios.code)

0 comments on commit d1ba09f

Please sign in to comment.