Icinga api commands

Hi

on your “bookmarks”, “experience”, do you have any API command line to count quickly how many hosts you have?
Or services?

In fact I want to make a script to list and monitore how many hosts, services i manage.

Tanks.
Alex

Hi.

icinga2 object list --type Host
icinga2 object list --type Service

# A (quite poor) example:
icinga2 object list --type Host | grep -c "of type 'Host'"

Or analogue:

curl -k -s -u api_user:api_pass 'https://your.server:5665/v1/objects/hosts'
# or to count (another poor example)
curl -k -s -u api_user:api_pass 'https://your.server:5665/v1/objects/hosts' | jq '.' | grep -c '__name'

curl -k -s -u api_user:api_pass 'https://your.server:5665/v1/objects/services'
2 Likes

If you dont need a filter for hosts and services you can use the icinga command from ITL to create a service that will deliver all data and write it to your perfdata write of your choice.

Else i would use the icinga console to get the needed counts as a number, there you can filter also. a simple example:

[root@koelin01 ~]# ICINGA2_API_USERNAME=root ICINGA2_API_PASSWORD=heinbloed icinga2 console --connect 'https://localhost:5665/' --eval 'get_objects(Host).len()'
23.0
3 Likes

Thanks for your answers that’s great.
All is ok for me
Have a good day

Alex

1 Like

Keep in mind that the debug console also means operating on an open heart, so use that with care. For production use cases, I’d recommend to use the /v1/objects/<type> URL endpoints and count the received results.

Cheers,
Michael

1 Like