Comments (9)
This happens because $json_response contains the structured JSON response, but the script tries to pull data out of it using the raw user-provided key. For example, it tries to look up $json_response->{'{foo}->{bar}'} instead of $json_response->{'foo'}->{'bar'}.
I've created a patch that partially fixes it, but I'm holding off on actually making a pull request until I have '-o *' support working.
from check_json.
Got the same issues :
./check_json.pl --url "http://api.aladhan.com/timingsByCity?city=Dubai&country=AE&method=2" --attribute '{code}->{value}' --warning :400 --critical :400 --perfvars '{code}->{value}'
Check JSON status API UNKNOWN - No value received
from check_json.
Got the same issue too.
from check_json.
I created a patch this morning and submitted a PR, without realizing this conversation had taken place. I haven't tested the patch with "*" though.
from check_json.
I never got around implementing a way for -o to pull individual things out of arrays because I fixed it as much as I needed (to pull individual keys out of deeply-nested data from PaperCut), but feel free to take what you need from https://github.com/waoki/check_json. I can create a PR if that would be helpful.
from check_json.
Neither of those two solutions work for me. They don't seem to be working with arrays. The only way I made it work was to change it to this:
220 my $perf_value;
221 my $perf_value_str = '$perf_value = $json_response->'.$key;
222 eval $perf_value_str;
223 print $perf_value;
E.g if you have something like:
"results": [
{
"status": {
"api": {
"http": {
"clients": 1.0
},
}
},
{
"status": {
"another_key": 2,
"andsoon": 1.345
}
]
Then $ptr->{$i}
, i.e. $json_response->results[0]
will result in an empty string.
Is that what @waoki was referring to in their commit message here waoki@df61837 ?
The fun part is that this works for this prefvalue: -p '{results}[0]->{status}->{api}->{http}->{clients}
but not for e.g. -p {results}[1]->{status}->{num_hosts_up}
.
from check_json.
I also think this behavior is weird, but maybe it's just me being tired or not understanding how perl works at all. I tried adding all perfdata keys to attributes, which – for reasons I don't understand – seems to work, but then:
~# /usr/lib/nagios/plugins/check_json -u https://localhost:6670/v1/status -w :5 -c :10 \
--attributes '{results}[0]->{status}->{api}->{http}->{clients},{results}[1]->{status}->{max_latency}' \
--ignoressl -p '{results}[0]->{status}->{api}->{http}->{clients},{results}[1]->{status}->{max_latency}' \
-e 1
Check JSON status API CRITICAL - Expected value (1) not found. Actual: 0.00407576560974121
~# /usr/lib/nagios/plugins/check_json -u https://localhost:6670/v1/status -w :5 -c :10 \
--attributes '{results}[0]->{status}->{api}->{http}->{clients},{results}[1]->{status}->{max_latency}' \
--ignoressl -p '{results}[0]->{status}->{api}->{http}->{clients},{results}[1]->{status}->{max_latency}' \
-e 1,
Check JSON status API CRITICAL - Expected value (1,) not found. Actual: 1
Why does the actual value change when I change the value of e
?
from check_json.
It's been a while since I looked at it, but yes, that's the kind of structure I was thinking about.
It looks like I started working on it, but then had to set it aside and never came back to it. I have these uncommitted changes sitting locally which look like initial work on the problem:
diff --git a/check_json.pl b/check_json.pl
index 39ffecf..b37a944 100755
--- a/check_json.pl
+++ b/check_json.pl
@@ -256,7 +256,8 @@ if ($np->opts->perfvars) {
# recurse_json($json_data, \&callback);
# recurse_json($json_data, \&callback, $path);
-# Invokes &callback for every terminal node in $json_data.
+# Invokes &callback for every terminal node in $json_data, unless it
+# is an array of scalars. This is needed for pretty-printing.
# &callback is passed the name and the value of the leaf, i.e.
# callback($label, $value)
# If $path is defined, recurse_json prepends it to $label. When recursing,
@@ -273,6 +274,18 @@ sub recurse_json {
} elsif (ref($ptr) eq 'ARRAY') {
@k = keys(@$ptr);
}
+
+ # If it's an array, do we need to recurse? Unfortunately, we must check
+ # whole structure first so we can make the right decision.
+ if (ref($ptr) eq 'ARRAY') {
+ foreach my $i (@k) {
+ if (ref($ptr->[$i])) {
+ &callback($path, $ptr);
+ return;
+ }
+ }
+ }
+
foreach my $i (@k) {
my $p = $path;
if (defined($p) && $p ne '') {
@@ -342,8 +355,7 @@ if ($np->opts->outputvars) {
}
}
- # depending on desired behavior, this could just be ref($ptr)
- if (ref($ptr) eq 'HASH') {
+ if (ref($ptr)) {
recurse_json($ptr, sub {
push(@statusmsg, format_output(@_));
}, '');
from check_json.
I have same issue, anyone got the right fix?
from check_json.
Related Issues (20)
- Plugin Check Error HOT 2
- check_json returns unknown if attributes does not exist HOT 1
- Doesn't seem to handle 302 connections HOT 1
- Howto handle authentication HOT 3
- Global symbol "@host" requires explicit package name HOT 3
- format of response? HOT 1
- check_value_str change breaking HOT 2
- uninitialized value when warn/crit thresholds not specified
- Feature request: Add arbitrary header support HOT 1
- Can't locate Nagios/Plugin.pm in @INC HOT 3
- Feature request: return perfvars, even if expected value doesn't match actual
- Use of uninitialized value $output_value in concatenation (.) or string at check_json.pl line 252 HOT 4
- I'm too stupid to do the simplest lookups HOT 4
- Nagios::Plugin is not supported anymore on cpan HOT 2
- RESTful API JSON Moxa Remote IO Module
- Booleans do not work
- Ignore SSL cert? HOT 3
- Bearer Token not supported
- Concatening metrics HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from check_json.