From 82f4eaa48a3d440a7827b0a977cd345324f66766 Mon Sep 17 00:00:00 2001 From: drewkerrigan Date: Wed, 6 May 2015 12:17:00 -0400 Subject: [PATCH] added ability to reference array elements when the root element is an array --- README.md | 14 ++++++++++++++ check_http_json.py | 5 ++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a13929d..b5f6995 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,20 @@ optional arguments: 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": {"value1": 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). ### Docker Info Example Plugin diff --git a/check_http_json.py b/check_http_json.py index 34af286..b22be00 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -70,7 +70,10 @@ class JsonHelper: else: return (None, 'not_found') else: - return (None, 'not_found') + 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 lte(self, key, value): return self.exists(key) and float(self.get(key)) <= float(value)