diff --git a/README.md b/README.md index 4b3a290..09705be 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ usage: check_http_json.py [-h] -H HOST [-p PATH] [-l [KEY_LTE_LIST [KEY_LTE_LIST ...]]] [-g [KEY_GTE_LIST [KEY_GTE_LIST ...]]] [-m [METRIC_LIST [METRIC_LIST ...]]] [-d] + [-s SEPARATOR] Nagios plugin which checks json values from a given endpoint against argument specified rules and determines the status and performance data for that @@ -85,6 +86,8 @@ optional arguments: this parameter are: (key), (key,UnitOfMeasure), (key,UnitOfMeasure,Min,Max). -d, --debug Debug mode. + -s SEPARATOR, --separator SEPARATOR + Json Field separator, defaults to "." ``` 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). @@ -213,6 +216,20 @@ If I change the command to have the parameter -q parameter `State.Running,False` OK: Status OK. ``` +### Dropwizard / Fieldnames Containing '.' Example + +Simply choose a separator to deal with data such as this: + +``` +{ "gauges": { "jvm.buffers.direct.capacity": {"value": 215415}}} +``` + +In this example I've chosen `_` to separate `guages` from `jvm` and `capacity` from `value`. The CLI invocation then becomes: + +``` +./check_http_json.py -H localhost:8081 -p metrics --key_exists gauges_jvm.buffers.direct.capacity_value -s _ +``` + ## License Copyright 2014-2015 Drew Kerrigan. diff --git a/check_http_json.py b/check_http_json.py index 2db2fd1..7c2dc5a 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -40,8 +40,9 @@ class NagiosHelper: class JsonHelper: """Perform simple comparison operations against values in a given JSON dict""" - def __init__(self, json_data): + def __init__(self, json_data, separator): self.data = json_data + 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 @@ -54,8 +55,8 @@ class JsonHelper: else: data = self.data - if '.' in key: - return self.get(key[key.find('.') + 1:], data[key[:key.find('.')]]) + if self.separator in key: + return self.get(key[key.find(self.separator) + 1:], data[key[:key.find(self.separator)]]) else: if key in data: return data[key] @@ -67,33 +68,37 @@ class JsonRuleProcessor: def __init__(self, json_data, rules_args): self.data = json_data self.rules = rules_args + separator = '.' + if self.rules.separator: separator = self.rules.separator + self.helper = JsonHelper(self.data, separator) + + debugPrint(rules_args.debug, "separator:%s" % separator) def isAlive(self): """Return a tuple with liveness and reason for not liveness given existence, equality, and comparison rules""" reason = '' - helper = JsonHelper(self.data) if self.rules.key_list != None: for k in self.rules.key_list: - if (helper.exists(k) == False): + if (self.helper.exists(k) == False): reason += " Key %s did not exist." % k if self.rules.key_value_list != None: for kv in self.rules.key_value_list: k, v = kv.split(',') - if (helper.equals(k, v) == False): + if (self.helper.equals(k, v) == False): reason += " Value %s for key %s did not match." % (v, k) if self.rules.key_lte_list != None: for kv in self.rules.key_lte_list: k, v = kv.split(',') - if (helper.lte(k, v) == False): + if (self.helper.lte(k, v) == False): reason += " Value %s was not less than or equal to value for key %s." % (v, k) if self.rules.key_gte_list != None: for kv in self.rules.key_gte_list: k, v = kv.split(',') - if (helper.gte(k, v) == False): + if (self.helper.gte(k, v) == False): reason += " Value %s was not greater than or equal to value for key %s." % (v, k) is_alive = (reason == '') @@ -103,7 +108,6 @@ class JsonRuleProcessor: def getMetrics(self): """Return a Nagios specific performance metrics string given keys and parameter definitions""" metrics = '' - helper = JsonHelper(self.data) if self.rules.metric_list != None: for metric in self.rules.metric_list: @@ -111,8 +115,6 @@ class JsonRuleProcessor: minimum = maximum = warn_range = crit_range = 0 uom = '' - vals = metric.split(',') - if ',' in metric: vals = metric.split(',') @@ -123,8 +125,8 @@ class JsonRuleProcessor: if len(vals) == 6: key,uom,minimum,maximum,warn_range,crit_range = vals - if helper.exists(key): - metrics += "'%s'=%s" % (key, helper.get(key)) + if self.helper.exists(key): + metrics += "'%s'=%s" % (key, self.helper.get(key)) if uom: metrics += uom metrics += ";%s" % minimum metrics += ";%s" % maximum @@ -158,6 +160,7 @@ def parseArgs(): 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('-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()