Giter Site home page Giter Site logo

Comments (9)

waoki avatar waoki commented on July 26, 2024 1

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.

ClouDevops101 avatar ClouDevops101 commented on July 26, 2024

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.

khba avatar khba commented on July 26, 2024

Got the same issue too.

from check_json.

senorsmile avatar senorsmile commented on July 26, 2024

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.

waoki avatar waoki commented on July 26, 2024

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.

kwisatz avatar kwisatz commented on July 26, 2024

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.

kwisatz avatar kwisatz commented on July 26, 2024

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.

waoki avatar waoki commented on July 26, 2024

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.

ErrafayM avatar ErrafayM commented on July 26, 2024

I have same issue, anyone got the right fix?

from check_json.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.