REST API URL to export view

I made a view in the GUI, can click to it and get it to display export as CSV or JSON…but how can I just get this view with a simple powershell Invoke-RestMethod or INvoke-WebRequest, the only thing that is returning is the html of the login page.

I already use the api with powershell

$url = "https://fqdn/site/check_mk/api/v0/domain-types/host_config/collections/all"
$headers = @{
            "Authorization"="Bearer $($Credential.UserName) $($Credential.GetNetworkCredential().Password)"
            "Accept"="application/json"
            "Content-Type"="application/json"
        }
        $iwr = Invoke-WebRequest -Uri $url -Headers $headers -Method Get  
        $response = $iwr.Content | ConvertFrom-Json
        $hosts = $response.value

But it returns 600 hosts! My view in the GUI only has 226.

Some say you can add output=json to the url, but that didn’t work with this. I don’t want to use a hardcoded secret either as this is a script to be ran manually once in a while. Instead I use Get-Credential and pass the authentication via headers.

If there is no export=json that I can use with my method, then what can I use to filter out bad data from the all hosts api endpoint? the queries from the redoc seem incredibly obtuse.

The REST API endpoint you query gives you all configured hosts in Checkmk by default, not a subset. You would need to filter the output yourself, based on host attributes.

Maybe this endpoint is more to your linking: https://$MYHOST/$MYSITE/check_mk/api/doc/#tag/Host-status It gives you filtering capabilities at request time, so you only get the subset of hosts you are interested in.

You can export any view by appending output_format=json and _username= and _secret= to the view.py-URL.

Unfortunately this is not documented at all.

Thanks, I took a look at the query, but I can’t seem to find the right way to make the request to get hostname, criticality and ip address for only monitored hosts. If I go to the interactive rest api (swagger) page it doesn’t even have the endpoint for that to test it interactively. I’ll just have to do this by hand. Thanks for the help all.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed. Contact an admin if you think this should be re-opened.