Handling complex arguments

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
stecino
Posts: 248
Joined: Thu Mar 14, 2013 4:42 pm

Handling complex arguments

Post by stecino »

Hello,

I have the following bash script that uses jq to parse a json file, based on the URL and values to parse for

Here is an example on the command line:
./parse_json.sh "http://blah.com:9000/admin/metrics" \'.gauges."\""heap.init""\".value\',\'.gauges."\""heap.used""\".value\',\'.gauges."\""heap.committed"\"".value\'
268435456 90739040 268435456
Stats | '.gauges."heap.init".value'=268435456;;;0;,'.gauges."heap.used".value'=90739040;;;0;,'.gauges."heap.committed".value'=90739040;;;0;

What I am doing here is breaking for single and double quotes so that parameter looks like this '.gauges."heap.init".value', '.gauges."heap.used".value",'gauges."heap.committed".value

This is my nagios configuration:

Code: Select all

define command{
        command_name    check_parse_json
        command_line    $USER1$/parse_json.sh $ARG1$ $ARG2$
}

define service{
        use                             misc-services
        host_name                       nagios
        service_description             Email Ingestion Heap Stats
        check_command                   check_parse_json!"http://blah.com:9000/admin/metrics"!\'.gauges."\""heap.init""\".value\',\'.gauges."\""heap.used""\".value\',\'.gauges."\""heap.committed"\"".value\'
        contact_groups                  network
        normal_check_interval           5
        notifications_enabled           0
}
Here is what I have in Nagios (Please see attached) It's all nulls. I know it's something with Nagios being able to handle the second parameter. How can I make this work?
Since I don't know how to pass this to Nagios, double quotes around the field name disappear.

Thanks in advance
Attachments
Parse.png
jdalrymple
Skynet Drone
Posts: 2620
Joined: Wed Feb 11, 2015 1:56 pm

Re: Handling complex arguments

Post by jdalrymple »

stecino...

complex is absolutely right. I'll be honest the behavior I was seeing based upon a few different ways I tried to escape various parts of your argument resulted in some pretty random behavior. To keep from going insane I suggest wrapping your argument into your script. If this isn't possible let us know and I'll keep trying to nail down why the escaping is so unpredictable in this string.
stecino
Posts: 248
Joined: Thu Mar 14, 2013 4:42 pm

Re: Handling complex arguments

Post by stecino »

jdalrymple wrote:stecino...

complex is absolutely right. I'll be honest the behavior I was seeing based upon a few different ways I tried to escape various parts of your argument resulted in some pretty random behavior. To keep from going insane I suggest wrapping your argument into your script. If this isn't possible let us know and I'll keep trying to nail down why the escaping is so unpredictable in this string.
I want to make this argument as dynamic parameter going into the script. So that I can setup monitors for any fields withing json. This should give freedom of extracting value from 1 to n fields and then generate performance data.
jdalrymple
Skynet Drone
Posts: 2620
Joined: Wed Feb 11, 2015 1:56 pm

Re: Handling complex arguments

Post by jdalrymple »

Fiddling with it a bit more - it became more obvious how to do it correctly.

Escape the following characters:

Code: Select all

\  "  '
With a backslash. You should end up with this in your service definition:

Code: Select all

check_command                   check_parse_json!"http://blah.com:9000/admin/metrics"!\\\'.gauges.\"\\\"\"heap.init\"\"\\\".value\\\',\\\'.gauges.\"\\\"\"heap.used\"\"\\\".value\\\',\\\'.gauges.\"\\\"\"heap.committed\"
stecino
Posts: 248
Joined: Thu Mar 14, 2013 4:42 pm

Re: Handling complex arguments

Post by stecino »

jdalrymple wrote:Fiddling with it a bit more - it became more obvious how to do it correctly.

Escape the following characters:

Code: Select all

\  "  '
With a backslash. You should end up with this in your service definition:

Code: Select all

check_command                   check_parse_json!"http://blah.com:9000/admin/metrics"!\\\'.gauges.\"\\\"\"heap.init\"\"\\\".value\\\',\\\'.gauges.\"\\\"\"heap.used\"\"\\\".value\\\',\\\'.gauges.\"\\\"\"heap.committed\"
It worked!! Thanks
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: Handling complex arguments

Post by tmcdonald »

I'll be closing this thread now, but feel free to open another if you need anything in the future!
Former Nagios employee
Locked