REST API to list all hosts

Hi,

I am trying to write a simple script with Powershell to list all monitored hosts in Icinga2 and add them to array (later compared with the another array). This is the closet url i found using __name attribute. But that returns others as well like “Joins, Meta, …” see below output.

I only want the hostname to return. How can I get only the hostname?

Is there any other way to do it?

https://localhost.local:5665/v1/objects/hosts?attrs=__name

{
“results”: [
{
“attrs”: {
“__name”: “host01.local”
},
“joins”: {},
“meta”: {},
“name”: “host01.local”,
“type”: “Host”
},

Thanks,
Max

Hi,

a call to the API will always return valid JSON, so it is not possible to get only the hostname without the extra stuff. You need to handle the JSON response in Powershell to fetch only the hostname.

The following Powershell code will only return the plain hostnames fetched from the API:

# Workaround for self signed certificates
# https://stackoverflow.com/questions/36456104/invoke-restmethod-ignore-self-signed-certs
if (-not("dummy" -as [type])) {
    add-type -TypeDefinition @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

public static class Dummy {
    public static bool ReturnTrue(object sender,
        X509Certificate certificate,
        X509Chain chain,
        SslPolicyErrors sslPolicyErrors) { return true; }

    public static RemoteCertificateValidationCallback GetDelegate() {
        return new RemoteCertificateValidationCallback(Dummy.ReturnTrue);
    }
}
"@
}

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = [dummy]::GetDelegate()


$icingaApiUser = "root"
$icingaApiPassword = "icinga"

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $icingaApiUser, $icingaApiPassword)))
$httpAuthInfo = "Basic $base64AuthInfo"
$httpAcceptInfo = "application/json"

$httpHeader = @{
    "Authorization" = $httpAuthInfo
    "Accept" = $httpAcceptInfo
}

$result = Invoke-RestMethod -Headers $httpHeader -Uri "https://icingahost:5665/v1/objects/hosts" -Method "GET" -ContentType "application/json"

foreach ($h in $result.results) {
    Write-Host $h.name
}

Note: You must edit the user, password and URL.

You can adopt the code snippet and modify it to your needs. For example you also have access to the last check result output:

foreach ($h in $result.results) {
    Write-Host $h.attrs.last_check_result.output
}

Cheers
Michael

3 Likes

Nice explanation :+1: To add, the core’s REST API is for programmers and automation, thus always returning a full JSON response when requested. Clients need to parse the output, many modern languages provide capabilities to parse JSON into native data types.

Cheers,
Michael

2 Likes

This is exactly what I need. Thank you Guys…

I’ve taken the liberty and modified the code a bit inside my Windows VM with adding it into the official docs. I was editing the API clients section anyways :wink:

3 Likes