added -s for Json Field Separator to deal with fieldnames containing '.'

This commit is contained in:
Drew Kerrigan 2015-04-24 11:32:03 -04:00
parent 46f045131c
commit 171826f046
2 changed files with 33 additions and 13 deletions

View File

@ -53,6 +53,7 @@ usage: check_http_json.py [-h] -H HOST [-p PATH]
[-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 ...]]] [-d]
[-s SEPARATOR]
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
@ -85,6 +86,8 @@ optional arguments:
this parameter are: (key), (key,UnitOfMeasure), this parameter are: (key), (key,UnitOfMeasure),
(key,UnitOfMeasure,Min,Max). (key,UnitOfMeasure,Min,Max).
-d, --debug Debug mode. -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). 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. 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 ## License
Copyright 2014-2015 Drew Kerrigan. Copyright 2014-2015 Drew Kerrigan.

View File

@ -40,8 +40,9 @@ class NagiosHelper:
class JsonHelper: class JsonHelper:
"""Perform simple comparison operations against values in a given JSON dict""" """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.data = json_data
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 str(self.get(key)) <= value
@ -54,8 +55,8 @@ class JsonHelper:
else: else:
data = self.data data = self.data
if '.' in key: if self.separator in key:
return self.get(key[key.find('.') + 1:], data[key[:key.find('.')]]) return self.get(key[key.find(self.separator) + 1:], data[key[:key.find(self.separator)]])
else: else:
if key in data: if key in data:
return data[key] return data[key]
@ -67,33 +68,37 @@ class JsonRuleProcessor:
def __init__(self, json_data, rules_args): def __init__(self, json_data, rules_args):
self.data = json_data self.data = json_data
self.rules = rules_args 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): def isAlive(self):
"""Return a tuple with liveness and reason for not liveness given existence, equality, and comparison rules""" """Return a tuple with liveness and reason for not liveness given existence, equality, and comparison rules"""
reason = '' reason = ''
helper = JsonHelper(self.data)
if self.rules.key_list != None: if self.rules.key_list != None:
for k in self.rules.key_list: 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 reason += " Key %s did not exist." % k
if self.rules.key_value_list != None: if self.rules.key_value_list != None:
for kv in self.rules.key_value_list: for kv in self.rules.key_value_list:
k, v = kv.split(',') 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) reason += " Value %s for key %s did not match." % (v, k)
if self.rules.key_lte_list != None: if self.rules.key_lte_list != None:
for kv in self.rules.key_lte_list: for kv in self.rules.key_lte_list:
k, v = kv.split(',') 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) reason += " Value %s was not less than or equal to value for key %s." % (v, k)
if self.rules.key_gte_list != None: if self.rules.key_gte_list != None:
for kv in self.rules.key_gte_list: for kv in self.rules.key_gte_list:
k, v = kv.split(',') 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) reason += " Value %s was not greater than or equal to value for key %s." % (v, k)
is_alive = (reason == '') is_alive = (reason == '')
@ -103,7 +108,6 @@ class JsonRuleProcessor:
def getMetrics(self): def getMetrics(self):
"""Return a Nagios specific performance metrics string given keys and parameter definitions""" """Return a Nagios specific performance metrics string given keys and parameter definitions"""
metrics = '' metrics = ''
helper = JsonHelper(self.data)
if self.rules.metric_list != None: if self.rules.metric_list != None:
for metric in self.rules.metric_list: for metric in self.rules.metric_list:
@ -111,8 +115,6 @@ class JsonRuleProcessor:
minimum = maximum = warn_range = crit_range = 0 minimum = maximum = warn_range = crit_range = 0
uom = '' uom = ''
vals = metric.split(',')
if ',' in metric: if ',' in metric:
vals = metric.split(',') vals = metric.split(',')
@ -123,8 +125,8 @@ class JsonRuleProcessor:
if len(vals) == 6: if len(vals) == 6:
key,uom,minimum,maximum,warn_range,crit_range = vals key,uom,minimum,maximum,warn_range,crit_range = vals
if helper.exists(key): if self.helper.exists(key):
metrics += "'%s'=%s" % (key, helper.get(key)) metrics += "'%s'=%s" % (key, self.helper.get(key))
if uom: metrics += uom if uom: metrics += uom
metrics += ";%s" % minimum metrics += ";%s" % minimum
metrics += ";%s" % maximum 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\ 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('-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()