From aa90ecad657799af9bc013600cfd06e81469ecc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hunor=20Kova=CC=81cs?= Date: Tue, 5 May 2015 14:32:16 +0300 Subject: [PATCH 1/2] 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. --- check_http_json.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/check_http_json.py b/check_http_json.py index 1491e22..c156248 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -45,6 +45,8 @@ class JsonHelper: def __init__(self, json_data, separator): self.data = json_data self.separator = separator + self.arrayOpener = '(' + self.arrayCloser = ')' def getSubElement(self, key, data): separatorIndex = key.find(self.separator) @@ -56,9 +58,12 @@ class JsonHelper: return (None, 'not_found') def getSubArrayElement(self, key, data): - subElemKey = key[:key.find('[')] - index = int(key[key.find('[') + 1:key.find(']')]) - remainingKey = key[key.find('].') + 2:] + 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]) @@ -81,8 +86,8 @@ class JsonHelper: if len(key) <= 0: return data - if key.find(self.separator) != -1 and key.find('[') != -1 : - if key.find(self.separator) < key.find('[') : + if key.find(self.separator) != -1 and key.find(self.arrayOpener) != -1 : + if key.find(self.separator) < key.find(self.arrayOpener) : return self.getSubElement(key, data) else: return self.getSubArrayElement(key, data) @@ -90,7 +95,7 @@ class JsonHelper: if key.find(self.separator) != -1 : return self.getSubElement(key, data) else: - if key.find('[') != -1 : + if key.find(self.arrayOpener) != -1 : return self.getSubArrayElement(key, data) else: if key in data: From 4fa2c3b39ae8e6632b1b8b5536445551afd83bb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hunor=20Kova=CC=81cs?= Date: Tue, 5 May 2015 14:58:09 +0300 Subject: [PATCH 2/2] Updating readme with arrays. --- README.md | 6 +++++- check_http_json.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9f0547b..a13929d 100644 --- a/README.md +++ b/README.md @@ -89,10 +89,14 @@ optional arguments: (key,UnitOfMeasure,Min,Max). -s, --ssl HTTPS mode. -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. ``` +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. + 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 diff --git a/check_http_json.py b/check_http_json.py index c156248..34af286 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -201,7 +201,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('-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.') return parser.parse_args()