json output

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
User avatar
WillemDH
Posts: 2320
Joined: Wed Mar 20, 2013 5:49 am
Location: Ghent
Contact:

json output

Post by WillemDH »

Hello,

As I'm in progress replacing Reacor with RunDeck, I need to replace the Nagios quick actions to a POST webrequest executing a RunDeck job. Instead of a nice popup showing the output of the Reactor chain I'm stuck with using 'command' as action type. Executing the 'command' quick action results in a tab being opened which shows the command being run.
The output which is in json is shown in one huge line containing the output of the webrequest.

So I tried to append ' | python -m json.tool' to the command which in cli results in a +- pretty json output. In Nagios XI however nothing is outputted at all and the command doesn't seem to run at all anymore.

And example command:
curl -s -XPOST -H "Accept:application/json" -H "X-Rundeck-Auth-Token:riDb5npzfdzdSvj6PXLkbTbQ" --data-urlencode "argString=-target citrixserver -loadevaluator Drain_Server" "https://rundeck:4443/api/17/job/099f518 ... 704dde/run"
Output:

Code: Select all

{"id":386,"href":"https://rundeck:4443/api/17/execution/386","permalink":"https://rundeck:4443/project/DAF/execution/show/386","status":"running","project":"DAF","user":"rundeck_daf","date-started":{"unixtime":1470388707755,"date":"2016-08-05T09:18:27Z"},"job":{"id":"099f51884b704dde","averageDuration":21456,"name":"DAF-Citrix-Drain-Server","group":"Citrix/Templates","project":"DAF","description":"Manages draining of Citrix servers.","options":{"duration":"43200","loadevaluator":"Drain_Server","target":"citrixserver ","service":"CTX_Loadevaluator","logserver":"logserver:5610","comment":"Created by RunDeck"},"href":"https://rundeck:4443/api/17/job/099f51704dde","permalink":"https://rundeck:4443/project/DAF/job/show/099f5b704dde"},"description":"job: Windows/Templates/DAF-Windows-Robocopy -- -target ${option.target} -sourcehost \"gateway\" -sourcefolder \"C:\\DAF\\DAF_Sources\\Scripts\" -destinationfolder \"C:\\Program Files\\NSClient++\\scripts\\powershell\" [... 3 steps]","argstring":"-comment \"Created by RunDeck\" -duration 43200 -loadevaluator Drain_Server -logserver logserver:5610 -service CTX_Loadevaluator -target citrixserver "}
The above output is shown in one line which is totally screwed. So why is appending '| python -m json.tool' not outputting anything at all? I'm just trying to get a nicer json in the opened page after clicking the quick action. In cli this works fine.

Or maybe there is another way to make a quick action output json in a pretty way or at least not in a oneliner?

Grtz
Nagios XI 5.8.1
https://outsideit.net
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: json output

Post by rkennedy »

Hi Willem,

I understand the concept, with what you're looking to do, but I'm having a hard time visualizing what / where you actually changed things. (specifically the JSON response) Could you provide a few screenshots to help display context?

Am I right in assuming, that you want to shorten the JSON response?
Former Nagios Employee
User avatar
WillemDH
Posts: 2320
Joined: Wed Mar 20, 2013 5:49 am
Location: Ghent
Contact:

Re: json output

Post by WillemDH »

Hello,

I don't want to shorten it. I want to 'not show it in one line', but make it readable.
Please post the json I put in my original post in this url:
http://jsonprettyprint.net/

Something like this would be nice.

Willem
Nagios XI 5.8.1
https://outsideit.net
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: json output

Post by rkennedy »

Got it regarding the JSON -- I thought you mentioned shortening it last week when I initially looked at the post.

I'm still not getting the part you're looking to modify. Could you please post a screenshot or two, showing us what you're seeing? The picture will speak a million words.
Former Nagios Employee
User avatar
WillemDH
Posts: 2320
Joined: Wed Mar 20, 2013 5:49 am
Location: Ghent
Contact:

Re: json output

Post by WillemDH »

I actually found out that from the moment I'm adding ' | python -m json.tool' at the end of a curl webrequest in the command textbox of the actions component that the curl command is no longer executed. Not sure if it is related to the pipe symbol or the fact that I'm calling python to make the curl webrequest json output more readable.

Please review my screenshots. You can see the output in CLI, the output without ' | python -m json.tool' and the output with ' | python -m json.tool' .
You do not have the required permissions to view the files attached to this post.
Nagios XI 5.8.1
https://outsideit.net
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: json output

Post by mcapra »

Are you sure this isn't something specific to how Rundeck is spitting out the JSON?

Testing against the the Core back-end CGIs output, I get pretty JSON when I try it as an Action in Nagios XI 5.2.9:
2016_08_11_15_14_37_192.168.67.1_nagiosxi_includes_components_actions_runcmd.php_action_0_uid_r04hlu.png
I was able to replicate the behavior where piping the output into the Python tool removes the output when run from Nagios XI. Wrapping the entire command (CURL + JSON pretty) in a script was able to solve the issue, though I realize that might be a bit annoying to configure.

The issue i'm noticing is that, when entering a command for the Actions component, the pipe character is actually being escaped by php's escapeshellcmd function.

As a demonstration:

Code: Select all


//this gets sent to PHP
curl http://nagiosadmin:[email protected]/nagios/cgi-bin/objectjson.cgi?query=host&hostname=192.168.3.146 | python -m json.tool

//PHP calls escapeshellcmd and converts it to this:
curl http://nagiosadmin:[email protected]/nagios/cgi-bin/objectjson.cgi\?query=host\&hostname=192.168.3.146 \| python -m json.tool

So the output is redirected to stderr since CURL doesn't know what to do with \|. Meaning nothing gets printed since PHP is looking for stdout.

But yeah, the core issue is the Actions component is using escapeshellcmd and messing with the pipe character. If you were feeling particularly brave (DISCLAIMER: THIS IS A BAD AND DANGEROUS THING TO DO) you could alter the component to not use escapeshellcmd. But again, that's a pretty dangerous thing to do and will definitely not persist through updates and definitely leaves your system open to incredibly bad things.
You do not have the required permissions to view the files attached to this post.
Former Nagios employee
https://www.mcapra.com/
User avatar
WillemDH
Posts: 2320
Joined: Wed Mar 20, 2013 5:49 am
Location: Ghent
Contact:

Re: json output

Post by WillemDH »

Solved it bu creating a curl wrapper script containing

Code: Select all

#!/bin/bash
curl "$@" | python -m json.tool
It's still in test. Please leave this open for a while.
Nagios XI 5.8.1
https://outsideit.net
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: json output

Post by mcapra »

Sure thing! Let us know if there's any updates.
Former Nagios employee
https://www.mcapra.com/
Locked