Invalid JSON due to non-escaped strings

Hello all,

I’ve encountered an issue stemming from how Icinga handles the strings in the JSON object it outputs. Namely, since Icinga doesn’t do any escaping of the content of the JSON strings, a check(whether it is the check’s plugin output, the check’s name, the command information from the check, etc.) can create a situation where the JSON object becomes invalid due to non-escaped special characters and/or control characters in the string.

An example is the following Event Stream response for a State Change of a check using the Invoke-IcingaCheckICMP Powershell plugin:

{
   "check_result":{
      "active":true,
      "check_source":"##HOSTNAME##",
      "command":[
         "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe",
         "-command",
         "Use-Icinga; exit Invoke-IcingaCheckICMP",
         "-Hostname",
         "##HOSTNAME##"
      ],
      "execution_end":1601929111.796,
      "execution_start":1601929105.53,
      "exit_status":2.0,
      "output":"[CRITICAL] Check package ICMP Check for ##HOSTNAME## - [CRITICAL] ICMP request to ##IP ADDRESS## with 64 bytes
\_ [CRITICAL] ICMP request to ##IP ADDRESS## with 64 bytes: Value 206ms is greater than threshold 200ms
",
      "performance_data":[
         "packet_loss=0%;20;50;0;100",
         "minimum_response_time=0ms;;",
         "maximum_response_time=206ms;;",
         "packet_count=5c;;",
         "average_response_time=41.4ms;100;200"
      ],
      "schedule_end":1601929111.796,
      "schedule_start":1601929111.796,
      "state":2.0,
      "ttl":0.0,
      "type":"CheckResult",
      "vars_after":{
         "attempt":1.0,
         "reachable":true,
         "state":2.0,
         "state_type":0.0
      },
      "vars_before":{
         "attempt":1.0,
         "reachable":true,
         "state":0.0,
         "state_type":1.0
      }
   },
   "host":"##HOSTNAME##",
   "service":"Powershell Connection to UHIWSSUTILD01.uhaul.amerco.org",
   "state":2.0,
   "state_type":0.0,
   "timestamp":1601929110.61568,
   "type":"StateChange"
}

If you place the above JSON into a formatter/validator such as the one at https://jsonformatter.curiousconcept.com/, it will flag:

  • The backslashes in the Command
  • The control characters(carriage return and line feed) and the backslash in the Output

Now, the above output is slightly modified from the standard output that would come from the Icinga Powershell Framework as Check package ICMP Check for ##HOSTNAME## would usually look like Check package "ICMP Check for ##HOSTNAME##"(thus inserting non-escaped double quotes in the output), but I placed modified New-IcingaCheck.psm1 and New-IcingaCheckPackage.psm1 files in the custom directory for the framework that removed the double quotes from the output.

What I am wondering is:

  1. Has anyone else encountered this issue and if so, how have you worked around it?
  2. Does this seem like a good feature request to have Icinga escape special and control characters in its JSON string output?

Your function call would be interesting to recreate this

yes

I actually encountered this while trying to setup the Icinga2 pack on Stackstorm. I had created https://github.com/StackStorm-Exchange/stackstorm-icinga2/issues/13 when I had initially discovered the issue. An exception would come up whenever the pack would pass off the JSON from Icinga to Python’s json.loads function.

Ok I understand. I don’t know stackstorm and I don’t know the python script of the pack. To recreate this situation, you have to create the same api request manulaly as the script is doing. After you could parse the json with other programs like jq or the website you wrote. … just an idea