Page 1 of 1

Handling complex arguments

Posted: Wed Mar 11, 2015 4:20 pm
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

Re: Handling complex arguments

Posted: Wed Mar 11, 2015 4:44 pm
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.

Re: Handling complex arguments

Posted: Wed Mar 11, 2015 5:28 pm
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.

Re: Handling complex arguments

Posted: Thu Mar 12, 2015 11:39 am
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\"

Re: Handling complex arguments

Posted: Sun Mar 15, 2015 1:42 pm
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

Re: Handling complex arguments

Posted: Mon Mar 16, 2015 8:30 am
by tmcdonald
I'll be closing this thread now, but feel free to open another if you need anything in the future!