RESTAPI Get all hosts with special attribute and return hostname and ipv4 address

So far I have only worked with the WebAPI, which has to be adapted before switching to REST-API.
My task is quite simple. I need all hosts with a defined user defined attribute. For processing, I only need the host name and the IPv4 address. This was done in the previous solution with 2 lines of code.

curl -s "http://localhost/site/check_mk/webapi.py?action=get_all_hosts&_username=$user&_secret=$secret" |
	jq -c '.result[] | select( .attributes.tag_Geraetetyp == "platine") | {hostname: .hostname, ipaddress4: .attributes.ipaddress}'

I’m still struggling with the RestAPI and only know that you can use it to query all hosts:

/domain-types/host_config/collections/all

Unfortunately I don’t know how to define the restriction here. Can someone send me the correct parameters?

Hi Ronny,

I don’t know which version exactly you are using. But since 2.0 you can go inside the navigation bar, select Help and then open up the interactive GUI of the REST API.

I just took a look and I would use /objects/host_config/{host_name}

Regards!

The problem with this is that with almost 5000 hosts, I don’t want to query all hosts individually, which would also be a time problem.
I thought that I could simply filter the list with the RESTAPI.

I use 2.1 checkmk enterprise

your call is not that efficient as you are getting data and then showing it away.
just call a normal view with output_format instead, or create a view that have only the columns you need

you can also do this with live status and is perhaps the most efficient solution.

Hi Ronny,

perhaps you take a look here and get the inspritation:

checkmk-bash-rest-api/README.md at master · bpoje/checkmk-bash-rest-api (github.com)

Greets Bernd

Thanks Bernd.

I have currently solved my problem via the live status. It’s different than I imagined with RESTAPI, but it works.

Bash:

QUERY="GET hosts\nColumns: name address state host_tags\nOrderBy: state\nFilter: host_tags = 'Geraetetyp' 'platine'\n"
RESPONSE=$(echo -e "$QUERY" | openssl s_client -quiet -CAfile /root/checkmkca.pem -connect checkmk.example.local:6557 2>/dev/null | sort -u)

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.