Page 1 of 1
Problem with JSON server output in nagios plugin.
Posted: Thu Jan 08, 2015 9:45 am
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.
Re: Problem with JSON server output in nagios plugin.
Posted: Thu Jan 08, 2015 12:51 pm
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.
Re: Problem with JSON server output in nagios plugin.
Posted: Thu Jan 08, 2015 7:22 pm
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
Re: Problem with JSON server output in nagios plugin.
Posted: Fri Jan 09, 2015 3:05 am
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?
Re: Problem with JSON server output in nagios plugin.
Posted: Fri Jan 09, 2015 3:20 am
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.
Thanks for your replies

Re: Problem with JSON server output in nagios plugin.
Posted: Fri Jan 09, 2015 3:02 pm
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.