Page 1 of 4

newlines in JSON

Posted: Wed Jun 25, 2014 6:06 pm
by jssingh
I think I'm not doing something right with output that has newlines to make it show up in the JSON APIs. I made all newlines:

Code: Select all

\\n
so that they are escaped. And if I look that the HTML page for the service, I see the newlines (and I checked the page source, there is

Code: Select all

<BR>
everywhere I expect it. However, the JSON does not include anything to show newlines. Should I be using a different delimiter?

for what it's worth, the same thing happens when I use:

Code: Select all

\n

Re: newlines in JSON

Posted: Thu Jun 26, 2014 2:40 pm
by tmcdonald
Can you show the (sanitized) URL you are using and the relevant page source it provides, as well as the command-line output from your check?

Re: newlines in JSON

Posted: Mon Jul 07, 2014 4:25 pm
by jssingh
Sorry for the delay in the response:

I created a mock plugin to demonstrate the issue with a minimum amount of output. And apparently somewhere along the way I changed something in the script that sends data via nrdp because I don't see line breaks in the html (but I don't know what I did!)

Here is the code:

Code: Select all

-bash-4.1$ cat multiline_output.pl 
#!/usr/bin/perl -w

print "testing multi line output\\\\n";
print "hello world\\\\n";
print "this is another line\\\\n";


exit 0;
-bash-4.1$
the output:

Code: Select all

-bash-4.1$ ./multiline_output.pl 
testing multi line output\\nhello world\\nthis is another line\\n-bash-4.1$ 
what I see in the JSON query (nagios4/cgi-bin/statusjson.cgi?query=service&hostname=pbspl233&servicedescription=qstatNoUn):

Code: Select all

{
  "format_version": 0,
  "result": {
    "query_time": 1404768234000,
    "cgi": "statusjson.cgi",
    "user": "nagiosadmin",
    "query": "service",
    "query_status": "beta",
    "program_start": 1404168963000,
    "last_data_update": 1404768233000,
    "type_code": 0,
    "type_text": "Success",
    "message": ""
  },
  "data": {
    "service": {
      "host_name": "pbspl233",
      "description": "qstatNoUn",
      "host_name": "pbspl233",
      "description": "qstatNoUn",
      "plugin_output": "testing multi line outputhello worldthis is another line",
      "long_plugin_output": "",
      "perf_data": "",
      "max_attemps": 8,
      "current_attempt": 1,
      "status": 2,
      "last_update": 1404768232000,
      "has_been_checked": true,
      "should_be_scheduled": false,
      "last_check": 1404768166000,
      "check_options": 0,
      "check_type": 1,
      "checks_enabled": false,
      "last_state_change": 1404168981000,
      "last_hard_state_change": 1404168981000,
      "last_hard_state": 0,
      "last_time_ok": 1404768166000,
      "last_time_warning": 0,
      "last_time_unknown": 0,
      "last_time_critical": 0,
      "state_type": 1,
      "last_notification": 0,
      "next_notification": 0,
      "no_more_notifications": false,
      "notifications_enabled": true,
      "problem_has_been_acknowledged": false,
      "acknowledgement_type": 0,
      "current_notification_number": 0,
      "accept_passive_checks": true,
      "event_handler_enabled": true,
      "flap_detection_enabled": false,
      "is_flapping": false,
      "percent_state_change": 0.00,
      "latency": 0.00,
      "execution_time": 0.00,
      "scheduled_downtime_depth": 0,
      "process_performance_data": true,
      "obsess": false
    }
  }
}
Any suggestions will be appreciated.

thanks!

Re: newlines in JSON

Posted: Tue Jul 08, 2014 10:20 am
by abrist
I am bringing this thread to the attention of the core dev who worked on the json cgis. Could you work backwards and try to discover what changed in your most recent round of tests?

Re: newlines in JSON

Posted: Tue Jul 08, 2014 4:49 pm
by jssingh
I tried running everything from the command line, which seems to have worked better.

This is the output from the command line:

Code: Select all

pbspl233.nas.nasa.gov:     Tue Jul  8 14:33:37 2014\\n Server reports 2 jobs total (T:0 Q:0 H:0 W:0 R:2 E:0 B:0)\\n\\n  Host       CPUs Tasks Jobs Info\\n  ---------- ---- ----- ---- -------------\\n   377 hosts 3016     0   -- har\\n    14 hosts   16     0   -- har down\\n   200 hosts 1600  1600   -- har in-use\\n    48 hosts    0     0   -- har offline\\n  r701i0n3      8     0    0 har rebooting\\n\\n                                         Req'd     Elap\\nJobID      Queue Jobname        CPUs Nds wallt Ss wallt Eff Group Model\\n---------- ----- -------------- ---- --- ----- -- ----- --- ----- -------\\n12736.p233 long  bubblesuperref  800 100 16:00 R  11:27 99% e1120 100:har\\n   Job run at Tue Jul 08 at 03:05 on (r706i0n0:ncpus=8)+(r706i0n1:ncpus=8)...\\n12741.p233 long  bubble0_75      800 100 16:00 R  09:22 99% e1120 100:har\\n   Job run at Tue Jul 08 at 05:10 on (r701i0n13:ncpus=8)+(r701i1n0:ncpus=8...
So I sent it via nrdp on the command line:

Code: Select all

/usr/bin/php /usr/local/nagios/libexec/send_nrdp.php --url=<URL> --token=<TOKEN> --host=pbspl233 --service="qstat" --state=OK --output="pbspl233.nas.nasa.gov:     Tue Jul  8 14:33:37 2014\\n Server reports 2 jobs total (T:0 Q:0 H:0 W:0 R:2 E:0 B:0)\\n\\n  Host       CPUs Tasks Jobs Info\\n  ---------- ---- ----- ---- -------------\\n   377 hosts 3016     0   -- har\\n    14 hosts   16     0   -- har down\\n   200 hosts 1600  1600   -- har in-use\\n    48 hosts    0     0   -- har offline\\n  r701i0n3      8     0    0 har rebooting\\n\\n                                         Req'd     Elap\\nJobID      Queue Jobname        CPUs Nds wallt Ss wallt Eff Group Model\\n---------- ----- -------------- ---- --- ----- -- ----- --- ----- -------\\n12736.p233 long  bubblesuperref  800 100 16:00 R  11:27 99% e1120 100:har\\n   Job run at Tue Jul 08 at 03:05 on (r706i0n0:ncpus=8)+(r706i0n1:ncpus=8)...\\n12741.p233 long  bubble0_75      800 100 16:00 R  09:22 99% e1120 100:har\\n   Job run at Tue Jul 08 at 05:10 on (r701i0n13:ncpus=8)+(r701i1n0:ncpus=8..."
On the nagios status page (html) it shows up with the proper line breaks:
this is the cgi: nagios4/cgi-bin/extinfo.cgi?type=2&host=pbspl233&service=qstat

Code: Select all

<TD CLASS='dataVal'>pbspl233.nas.nasa.gov:     Tue Jul  8 14:33:37 2014<BR> Server reports 2 jobs total (T:0 Q:0 H:0 W:0 R:2 E:0 B:0)<BR>\n  Host       CPUs Tasks Jobs Info<BR>  ---------- ---- ----- ---- -------------<BR>   377 hosts 3016     0   -- har<BR>    14 hosts   16     0   -- har down<BR>   200 hosts 1600  1600   -- har in-use<BR>    48 hosts    0     0   -- har offline<BR>  r701i0n3      8     0    0 har rebooting<BR>\n                                         Req'd     Elap<BR>JobID      Queue Jobname        CPUs Nds wallt Ss wallt Eff Group Model<BR>---------- ----- -------------- ---- --- ----- -- ----- --- ----- -------<BR>12736.p233 long  bubblesuperref  800 100 16:00 R  11:27 99% e1120 100:har<BR>   Job run at Tue Jul 08 at 03:05 on (r706i0n0:ncpus=8)+(r706i0n1:ncpus=8)...<BR>12741.p233 long  bubble0_75      800 100 16:00 R  09:22 99% e1120 100:har<BR>   Job run at Tue Jul 08 at 05:10 on (r701i0n13:ncpus=8)+(r701i1n0:ncpus=8...</TD>
and then in the JSON all traces of those line breaks are gone:
this is the JSON query: nagios4/cgi-bin/statusjson.cgi?query=service&hostname=pbspl233&servicedescription=qstat

Code: Select all

{
  "format_version": 0,
  "result": {
    "query_time": 1404855897000,
    "cgi": "statusjson.cgi",
    "user": "nagiosadmin",
    "query": "service",
    "query_status": "beta",
    "program_start": 1404168963000,
    "last_data_update": 1404855893000,
    "type_code": 0,
    "type_text": "Success",
    "message": ""
  },
  "data": {
    "service": {
      "host_name": "pbspl233",
      "description": "qstat",
      "host_name": "pbspl233",
      "description": "qstat",
      "plugin_output": "pbspl233.nas.nasa.gov:     Tue Jul  8 14:33:37 2014",
      "long_plugin_output": " Server reports 2 jobs total (T:0 Q:0 H:0 W:0 R:2 E:0 B:0)\n  Host       CPUs Tasks Jobs Info  ---------- ---- ----- ---- -------------   377 hosts 3016     0   -- har    14 hosts   16     0   -- har down   200 hosts 1600  1600   -- har in-use    48 hosts    0     0   -- har offline  r701i0n3      8     0    0 har rebooting\n                                         Req'd     ElapJobID      Queue Jobname        CPUs Nds wallt Ss wallt Eff Group Model---------- ----- -------------- ---- --- ----- -- ----- --- ----- -------12736.p233 long  bubblesuperref  800 100 16:00 R  11:27 99% e1120 100:har   Job run at Tue Jul 08 at 03:05 on (r706i0n0:ncpus=8)+(r706i0n1:ncpus=8)...12741.p233 long  bubble0_75      800 100 16:00 R  09:22 99% e1120 100:har   Job run at Tue Jul 08 at 05:10 on (r701i0n13:ncpus=8)+(r701i1n0:ncpus=8...",
      "perf_data": "",
      "max_attemps": 8,
      "current_attempt": 1,
      "status": 2,
      "last_update": 1404855893000,
      "has_been_checked": true,
      "should_be_scheduled": false,
      "last_check": 1404855884000,
      "check_options": 0,
      "check_type": 1,
      "checks_enabled": false,
      "last_state_change": 1403202596000,
      "last_hard_state_change": 1403202596000,
      "last_hard_state": 0,
      "last_time_ok": 1404855884000,
      "last_time_warning": 0,
      "last_time_unknown": 0,
      "last_time_critical": 0,
      "state_type": 1,
      "last_notification": 0,
      "next_notification": 0,
      "no_more_notifications": false,
      "notifications_enabled": true,
      "problem_has_been_acknowledged": false,
      "acknowledgement_type": 0,
      "current_notification_number": 0,
      "accept_passive_checks": true,
      "event_handler_enabled": true,
      "flap_detection_enabled": false,
      "is_flapping": false,
      "percent_state_change": 0.00,
      "latency": 0.00,
      "execution_time": 0.00,
      "scheduled_downtime_depth": 0,
      "process_performance_data": true,
      "obsess": false
    }
  }
}

Re: newlines in JSON

Posted: Wed Jul 09, 2014 3:53 pm
by abrist
The json output looks good. Did you hunt down your issue in your script, or do you still need assistance?

Re: newlines in JSON

Posted: Wed Jul 09, 2014 4:08 pm
by jssingh
most of the data in the JSON looks good ... it's just that if you look at every

Code: Select all

<BR>
in the HTML and compare it to the JSON, there is nothing to tell you there is a newline there.

There a couple

Code: Select all

\n
in there that I think are confusing the issue, but if you look at the HTML, those are not converted to line breaks in the HTML either, so that seems to be a different issue.

It makes parsing the data very difficult if we don't have the newlines in there.

Re: newlines in JSON

Posted: Wed Jul 09, 2014 4:57 pm
by abrist
Ahhh. Yep, it looks like the <br> tags are definitely getting stripped. I will talk to Eric[0] this Friday and see if there is an easy fix.

Re: newlines in JSON

Posted: Wed Jul 09, 2014 5:01 pm
by jssingh
thanks!

Re: newlines in JSON

Posted: Mon Jul 14, 2014 9:56 am
by slansing
If we don't reply Friday morning for whatever reason, please give this thread a bump with a message, thanks!