API and Hostgroups

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
SteveBeauchemin
Posts: 524
Joined: Mon Oct 14, 2013 7:19 pm

API and Hostgroups

Post by SteveBeauchemin »

Is there syntax where I can add a host to a hostgroup via api or external means? Any creative method would be okay.

I do not want to add a hostgroup to a host. I want to add a Host to an existing Hostgroup.

For some reason I am not able to see that in my API docs.

Thanks

Steve B
XI 5.7.3 / Core 4.4.6 / NagVis 1.9.8 / LiveStatus 1.5.0p11 / RRDCached 1.7.0 / Redis 3.2.8 /
SNMPTT / Gearman 0.33-7 / Mod_Gearman 3.0.7 / NLS 2.0.8 / NNA 2.3.1 /
NSClient 0.5.0 / NRPE Solaris 3.2.1 Linux 3.2.1 HPUX 3.2.1
IPOInS
Posts: 25
Joined: Tue Jan 14, 2020 6:08 am

Re: API and Hostgroups

Post by IPOInS »

Hi Steve,

Using the standard API call available in Nagios XI

'config/hostgroup/<hostgroup_name>'

You can actually include the members parameter (which is what you normally see when you query (GET) a host group).

Here's the syntax for the PUT rest method:
https://<NAGIOSSERVER>/nagiosxi/api/v1/config/hostgroup/<HOSTGROUPNAME>?apikey=<APIKEY>&members=<COMPUTERNAME>

Although it's not documented in the Nagios API Help pages this works for me. No issues detected. I've found this to be the same with a few other API calls (such as updating host and service) where undocumented parameters can be used. By simply querying an object via the API to return the column names these in turn can be used as parameters to update them.

I'd always advise testing any of these undocumented parameters in a test environment first.
Nagios support might have their own views on the use of such undocumented parameters.
SteveBeauchemin
Posts: 524
Joined: Mon Oct 14, 2013 7:19 pm

Re: API and Hostgroups

Post by SteveBeauchemin »

Thank you for the information.

My company is starting to use Ansible to deploy Agent software. It sends an email when completed. I take the email in and process it, making a Servicenow INC, and also making a Host in Nagios. I wanted to be able to add the host to specific Hostgroups for the Service assignments. But in the docs, all I saw was the inverse, adding a hostgroup to a Host.

This will go a long way to automating my process.

Thanks again.

Steve B
XI 5.7.3 / Core 4.4.6 / NagVis 1.9.8 / LiveStatus 1.5.0p11 / RRDCached 1.7.0 / Redis 3.2.8 /
SNMPTT / Gearman 0.33-7 / Mod_Gearman 3.0.7 / NLS 2.0.8 / NNA 2.3.1 /
NSClient 0.5.0 / NRPE Solaris 3.2.1 Linux 3.2.1 HPUX 3.2.1
benjaminsmith
Posts: 5324
Joined: Wed Aug 22, 2018 4:39 pm
Location: saint paul

Re: API and Hostgroups

Post by benjaminsmith »

@IPOInS Thanks for the input!

Just let us know if you need anything else.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
SteveBeauchemin
Posts: 524
Joined: Mon Oct 14, 2013 7:19 pm

Re: API and Hostgroups

Post by SteveBeauchemin »

Finally had time to try this out.

After reading in all the host additions, I now have one host assigned to the hostgroup.

It would appear that this is not an additive process? It wipes out existing members and adds the one new item?

Is there syntax I can use such that when I add a new host, I can add it to a hostgroup without destroying the current grouping?

Thanks

Steve B
XI 5.7.3 / Core 4.4.6 / NagVis 1.9.8 / LiveStatus 1.5.0p11 / RRDCached 1.7.0 / Redis 3.2.8 /
SNMPTT / Gearman 0.33-7 / Mod_Gearman 3.0.7 / NLS 2.0.8 / NNA 2.3.1 /
NSClient 0.5.0 / NRPE Solaris 3.2.1 Linux 3.2.1 HPUX 3.2.1
SteveBeauchemin
Posts: 524
Joined: Mon Oct 14, 2013 7:19 pm

Re: API and Hostgroups

Post by SteveBeauchemin »

Any suggestions on how to programmatically add hosts to a hostgroup?

I need to get this working.

Please advise.

Steve B
XI 5.7.3 / Core 4.4.6 / NagVis 1.9.8 / LiveStatus 1.5.0p11 / RRDCached 1.7.0 / Redis 3.2.8 /
SNMPTT / Gearman 0.33-7 / Mod_Gearman 3.0.7 / NLS 2.0.8 / NNA 2.3.1 /
NSClient 0.5.0 / NRPE Solaris 3.2.1 Linux 3.2.1 HPUX 3.2.1
benjaminsmith
Posts: 5324
Joined: Wed Aug 22, 2018 4:39 pm
Location: saint paul

Re: API and Hostgroups

Post by benjaminsmith »

Hi Steve,

Unfortunately, there isn't a direct way of doing this in the current API. In order to achieve this programmatically, you would need to gather the current members using the API and then update the host group using the API command suggested in this post.

The following API call can be used to get the current members of your hostgroups.

Code: Select all

curl -XGET "https://192.168.23.113/nagiosxi/api/v1/config/hostgroup?apikey=<APIKEYHERE>&pretty=1"
Hope that helps answer your question.

Benjamin
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
SteveBeauchemin
Posts: 524
Joined: Mon Oct 14, 2013 7:19 pm

Re: API and Hostgroups

Post by SteveBeauchemin »

Benjamin,

Thanks for the information.

Can I get an enhancement request added:
Can we use the API to add one Host to an existing Hostgroup without needing to recreate the group?

I already have a large infrastructure. I am tripling our monitoring footprint. My company has been acquired 2 times in the last 3 years. We grew from 35K employee count to 75K to now 125K. The only way I can keep up with this growth is to automate more things.

They like the Nagios work I do and want it everywhere now. I'm starting to panic a little.

Adding a Host to a Hostgroup should be a simple thing to automate. Please make it easy...

Please add a request for this to your developers. It is important to me that this become simple.

Thanks

Steve B
XI 5.7.3 / Core 4.4.6 / NagVis 1.9.8 / LiveStatus 1.5.0p11 / RRDCached 1.7.0 / Redis 3.2.8 /
SNMPTT / Gearman 0.33-7 / Mod_Gearman 3.0.7 / NLS 2.0.8 / NNA 2.3.1 /
NSClient 0.5.0 / NRPE Solaris 3.2.1 Linux 3.2.1 HPUX 3.2.1
benjaminsmith
Posts: 5324
Joined: Wed Aug 22, 2018 4:39 pm
Location: saint paul

Re: API and Hostgroups

Post by benjaminsmith »

Hi Steve,
Please add a request for this to your developers. It is important to me that this become simple.
I've submitted the request on your behalf.

Sounds you got a full plate these days!

--Benjamin
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
inversecow
Posts: 44
Joined: Wed Sep 25, 2019 4:17 pm

Re: API and Hostgroups

Post by inversecow »

Ahoy folks,

Interesting topic, and one that I also was reviewing semi-recently!

Important question to ask yourself is, what is your ENV "source of truth"?
If your answer is the SN-CMDB (or whatever CMDB you are using), than possibly you do not need to scrap "current memberships" out of XI.
Instead, perhaps it is better to simply "enforce" what the CMDB has on record?
This may save some processing time, if this holds for your scenario.

Similar scenario where dealing with a "large set of ENVs & sites" with limited number of hands.
Sometimes a "dirty shell-script" is just the thing to clear the immediate hump, and to give a concise "prototype", which you can then "automate better" with Ansible.
I agree having the ability to "update" host-group memberships would be certainly more efficient, but this may well "get the job done" meanwhile.

Here is what I do (in shell-script), for "LOCATION (LOC)" related host-groups (my use-case, for the ability for site-specific "scheduled downtime" during changes).
This keeps subject host-groups "fresh" and current, as of the last execution (which would ideally become a scheduled task, out of CRON or TOWER).

In reviewing this matter, I found it more "cost effective" in terms of XI check-in processing time (and related system load & nagios core downtime due to the large scale size of my deployment).
The alternative was to update HG membership on a per-node basis (*very expensive* in terms of processing resources and incurred nagios core downtime due to multiple check-ins involved, by an order of your target node count.)

Disclaimer this is still a work in progress, as I have yet to automate the Service Now (SN) CMDB API call to grab a node list (vs manually collecting / shipping a CSV).
Once that is done, its fun time writing the playbook to wrap all of this.

- grab a CSV export of your desired hostgroup memberships (gross, but it worked in a pinch)
TODO: replace this with cool API calls to SN-CMDB to collect desired hosts for XI host-groups

- set control variables, and verify paths as relevant for your scenario(s)

- execute as needed

# shell script (made generic, and using "control variables" for flexibility)
```
XI='nagios.instance.access.url'; \
API='coolapikey'; \
HG='target_hostgroup_name'; \
CCM_HOSTS=$( curl -XGET "https://"$XI"/nagiosxi/api/v1/config/host?apikey="$API"&pretty=1" -k | grep 'host_name' | awk '{print $2}' | sort -u | tr -d '"' | tr -d ',' ); \
LOC_HOSTS=$( cat ~/cmdb_ci_server_CLIENT_LOC.csv | awk -F, '{print $3}' | grep -v 'primary_node_name\|No Monitoring yet' | sort -u | sed '/^$/d' ); \
REG_HOSTS=$( echo "$CCM_HOSTS" | grep "$LOC_HOSTS" | sed ':a;N;$!ba;s/\n/,/g' ); \
curl -XPUT "https://"$XI"/nagiosxi/api/v1/config/hostgroup/"$HG"?apikey="$API"&pretty=1&members="$REG_HOSTS"" -vk;
```

TODO: convert this to a cool set of Ansible based plays

Good luck,
-Rowan
“And who better understands the Unix-nature?” Master Foo asked.
“Is it he who writes the ten thousand lines, or he who, perceiving the emptiness of the task, gains merit by not coding?”
Master Foo - The ten thousand Lines
Unix Koans of Master Foo
Locked