API how to get the second level of an attibut

Hello,
With an API command how to get only second-level information?

For ex:
https://IcingaServer.domaine:5665/v1/objects/services/ServerName!CPU413?attrs=last_check_result
Result :
{
“results”: [
{
“attrs”: {
“last_check_result”: {
“active”: true,
“check_source”: “IcingaServer.cma-cgm.com”,
“command”: [
“/opt/monitoring/plugins/check_cpu.sh”,
“-c”,
“90”,
“-w”,
“80”
],
“execution_end”: 1714998355.358391,
“execution_start”: 1714998354.286959,
“exit_status”: 0,
“output”: “OK - CPU Usage 54% used”,
“performance_data”: [
“CPU_total=54;80;90;;”,
“CPU_user=45;80;90;;”,
“CPU_system=9;80;90;;”,
“CPU_idle=46;;;;”,
“CPU_iowait=0;80;90;;”
],
“previous_hard_state”: 0,
“schedule_end”: 1714998355.358476,
“schedule_start”: 1714998354.286542,

But how do you write the command to get only the “output” line of the “last_check_result” attribute for example?

Thanks

  • Director version : 1.9.1
  • Icinga Web 2 version and modules : 2.11.1
  • Icinga 2 version : 2.14-0.1
  • Operating System and version: linux redhat 7.6
  • Webserver, PHP versions: 7.3
  • Icingadb : 1.0.1

As your query already reduces the output through the attrs parameter, I would guess that you are familiar with the API Querying Objects documentation.

AFAIK, there is no way to let the Icinga 2 API modify the response in the way you want. Thus, you have to perform filtering and modifications on your end, i.e., with jq. A jq query might look like the following:

curl -k -s -u root:icinga 'https://localhost:5665/v1/objects/services/dummy-914%21random%20fortune?attrs=last_check_result' \
  | jq '.results[].attrs.last_check_result.output'

Of course, you can do this in any programming or scripting language understanding JSON, which are luckily almost any languages nowadays.

2 Likes