7 Commits
v1.0 ... v1.2

Author SHA1 Message Date
drewkerrigan
dd439998db added ability to reference array elements when the root element is an array 2015-05-06 12:19:18 -04:00
drewkerrigan
82f4eaa48a added ability to reference array elements when the root element is an array 2015-05-06 12:17:00 -04:00
Drew Kerrigan
9aa7ed57aa Merge pull request #7 from kovacshuni/arrays
Changing form [] to () because of Nagios issues. Fixes.
2015-05-05 10:30:10 -04:00
Hunor Kovács
4fa2c3b39a Updating readme with arrays. 2015-05-05 14:58:24 +03:00
Hunor Kovács
aa90ecad65 Changed from square brackets to normal paranthesis for arrays. Nagios doesn't like square brackets.
(http://sourceforge.net/p/nagios/nrpe/ci/master/tree/SECURITY illegal metachars section)
Fixed case when no dot after array (e.g. checks(0) wasn't working)
Extract paranthesis characters in attribute.
2015-05-05 14:32:16 +03:00
Drew Kerrigan
c678dfd518 Merge pull request #6 from kovacshuni/arrays
Arrays are supported as well. format: `alpha.beta[2].gamma.theta[0]`
2015-05-04 14:00:02 -04:00
Hunor Kovács
3b048f66c5 Arrays are supported as well. format: alpha.beta[2].gamma.theta[0]
Fix: Now keys that are in the middle of the expression that are not found in the data, can be checked upon, without the script throwing an error.
2015-05-04 20:45:46 +03:00
2 changed files with 67 additions and 8 deletions

View File

@@ -89,10 +89,28 @@ optional arguments:
(key,UnitOfMeasure,Min,Max). (key,UnitOfMeasure,Min,Max).
-s, --ssl HTTPS mode. -s, --ssl HTTPS mode.
-f SEPARATOR, --field_separator SEPARATOR -f SEPARATOR, --field_separator SEPARATOR
Json Field separator, defaults to "." Json Field separator, defaults to "." ; Select element
in an array with "(" ")"
-d, --debug Debug mode. -d, --debug Debug mode.
``` ```
Access a specific JSON field by following this syntax: `alpha.beta.gamma(3).theta.omega(0)`
Dots are field separators (changeable), parantheses are for entering arrays.
If the root of the JSON data is itself an array like the following:
```
[
{ "gauges": { "jvm.buffers.direct.capacity": {"value": 215415}}}
]
```
The beginning of the key should start with ($index) as in this example:
```
./check_http_json.py -H localhost:8081 -p metrics --key_exists "(0)_gauges_jvm.buffers.direct.capacity_value" -f _
```
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).
### Docker Info Example Plugin ### Docker Info Example Plugin

View File

@@ -45,6 +45,35 @@ class JsonHelper:
def __init__(self, json_data, separator): def __init__(self, json_data, separator):
self.data = json_data self.data = json_data
self.separator = separator self.separator = separator
self.arrayOpener = '('
self.arrayCloser = ')'
def getSubElement(self, key, data):
separatorIndex = key.find(self.separator)
partialKey = key[:separatorIndex]
remainingKey = key[separatorIndex + 1:]
if partialKey in data:
return self.get(remainingKey, data[partialKey])
else:
return (None, 'not_found')
def getSubArrayElement(self, key, data):
subElemKey = key[:key.find(self.arrayOpener)]
index = int(key[key.find(self.arrayOpener) + 1:key.find(self.arrayCloser)])
remainingKey = key[key.find(self.arrayCloser + self.separator) + 2:]
if key.find(self.arrayCloser + self.separator) == -1:
remainingKey = key[key.find(self.arrayCloser) + 1:]
if subElemKey in data:
if index < len(data[subElemKey]):
return self.get(remainingKey, data[subElemKey][index])
else:
return (None, 'not_found')
else:
if not subElemKey:
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)) == value
def lte(self, key, value): return self.exists(key) and float(self.get(key)) <= float(value) def lte(self, key, value): return self.exists(key) and float(self.get(key)) <= float(value)
@@ -57,13 +86,25 @@ class JsonHelper:
else: else:
data = self.data data = self.data
if self.separator in key: if len(key) <= 0:
return self.get(key[key.find(self.separator) + 1:], data[key[:key.find(self.separator)]]) return data
else:
if key in data: if key.find(self.separator) != -1 and key.find(self.arrayOpener) != -1 :
return data[key] if key.find(self.separator) < key.find(self.arrayOpener) :
return self.getSubElement(key, data)
else: else:
return (None, 'not_found') return self.getSubArrayElement(key, data)
else:
if key.find(self.separator) != -1 :
return self.getSubElement(key, data)
else:
if key.find(self.arrayOpener) != -1 :
return self.getSubArrayElement(key, data)
else:
if key in data:
return data[key]
else:
return (None, 'not_found')
class JsonRuleProcessor: class JsonRuleProcessor:
"""Perform checks and gather values from a JSON dict given rules and metrics definitions""" """Perform checks and gather values from a JSON dict given rules and metrics definitions"""
@@ -163,7 +204,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('-s', '--ssl', action='store_true', help='HTTPS mode.') 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('-f', '--field_separator', dest='separator', help='Json Field separator, defaults to "." ; Select element in an array with "(" ")"')
parser.add_argument('-d', '--debug', action='store_true', help='Debug mode.') parser.add_argument('-d', '--debug', action='store_true', help='Debug mode.')
return parser.parse_args() return parser.parse_args()