Problem with JSON server output in nagios plugin.

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
xitkov
Posts: 3
Joined: Thu Jan 08, 2015 8:43 am

Problem with JSON server output in nagios plugin.

Post by xitkov »

Hi all,

We have nagios plugins that throw JSON output, which we then parse in another command (which makes alerts).

The problem is JSON output can have characters such as [$,&,\,etc] because of html data, and nagios acts strange if
these are encountered. Most of the times nagios will strip characters and the JSON then becomes invalid,
and sometimes nagios will fail to run the notification command at all.
We are already encoding [',",<,>].

What is the best way to properly get the service output on the notification command?
One more thing is, the ';' character is replaced by ':', why?
And also is this the way to do it, or is there a better way to send bigger outputs like we are throwing?

Thanks in advance.
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: Problem with JSON server output in nagios plugin.

Post by tmcdonald »

Since this would all be displayed in the browser, you could try HTML-escaping the characters:

http://www.ascii.cl/htmlcodes.htm

This should make them display properly in a web page, but anything further down the road that would need to handle the output may not understand HTML entities.
Former Nagios employee
User avatar
Box293
Too Basu
Posts: 5126
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia
Contact:

Re: Problem with JSON server output in nagios plugin.

Post by Box293 »

xitkov wrote:We have nagios plugins that throw JSON output, which we then parse in another command (which makes alerts).
Can you be a little more specific?
Is the plugin run on the Nagios server and receiving back JSON data?
When does the other command get run and how?

FYI I have a very similar situation and here is how I overcome the problem:
  • On the Nagios server I am inside a PHP script
    The PHP script executes a check_by_ssh request to a remote server
    The remote server runs the command (perl script)
    The perl script then does it's job by collecting some data and storing it in a JSON string
    I then encode the JSON string so it ends up just as one long string of garbled characters
    The perl script prints the encoded string and exits
    Nagios server receives the encoded string
    The PHP script receives the encoded string as multiple lines
    I concatenate all the lines together to get one long encoded string
    I then decode that string back to a JSON
    I then use the PHP json_decode() function to turn that into an array
    Now I can access the data as an array
Yeah I know thats a lot of steps but for the task I perform it works beautifully
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
xitkov
Posts: 3
Joined: Thu Jan 08, 2015 8:43 am

Re: Problem with JSON server output in nagios plugin.

Post by xitkov »

tmcdonald wrote:Since this would all be displayed in the browser, you could try HTML-escaping the characters:

http://www.ascii.cl/htmlcodes.htm

This should make them display properly in a web page, but anything further down the road that would need to handle the output may not understand HTML entities.
I have tried escaping HTML characters, but Nagios replaces ';' with ':', so again I have to encode ';' also, or use a non-standard encoding/decoding scheme.

Is encoding the characters the only way to achive this?
xitkov
Posts: 3
Joined: Thu Jan 08, 2015 8:43 am

Re: Problem with JSON server output in nagios plugin.

Post by xitkov »

Box293 wrote:Can you be a little more specific?
Like the way you are using Nagios, we are also using php scripts as plugins and use encoding/decoding too.
Box293 wrote:Is the plugin run on the Nagios server and receiving back JSON data?
Yes, the plugin runs on Nagios server and it returns JSON data in case of alert.
Nagios then runs a "service notification command" and there it passes the data on the command line (using $SERVER_OUTPUT$ macro).
Box293 wrote:When does the other command get run and how?
Like I said the other command runs when the first command returns some problem for the service.

Looks like encoding the output is the only way this can be achieved natively, and also there's that 4 KiB limit, so we are moving ahead with our custom solution which won't require encoding/decoding and won't have such size limit too. :D
Thanks for your replies :)
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: Problem with JSON server output in nagios plugin.

Post by tmcdonald »

Sorry we couldn't be of more assistance. I am definitely curious about the semi-colon issue however. I might ask a dev about that.
Former Nagios employee
Locked