Page 1 of 1

Modifying a host via the REST API

Posted: Fri Apr 20, 2018 3:28 pm
by Matthew.Cary
In our ongoing quest to use the rest API to automate the provisioning of Nagios XI I have another question:

I understand how to add a host via the API, and I know how to delete a host via the API, but how does one *modify* a host? What if I want to use the API to add or remove a hostgroup from an existing host? Or change the Template it is using?

I don't seem to be able to find this in the documentation.

Re: Modifying a host via the REST API

Posted: Sat Apr 21, 2018 5:07 pm
by krutaw
Matthew.Cary wrote:In our ongoing quest to use the rest API to automate the provisioning of Nagios XI I have another question:

I understand how to add a host via the API, and I know how to delete a host via the API, but how does one *modify* a host? What if I want to use the API to add or remove a hostgroup from an existing host? Or change the Template it is using?

I don't seem to be able to find this in the documentation.
Yup, we do it all the time, though probably not how you think. When we goto "modify" a host, we first make a call to read the host properties to build the necessary post data, and then append/replace the necessary bits that need to change. Then we simply post to add the host again on the nagios server where it lives (I have a small fleet of them.) When you re-add the host, it'll simply overwrite the existing host entry, keeping the same table ID's in the backend nagiosql database.

Hope that helps! ;)

Re: Modifying a host via the REST API

Posted: Mon Apr 23, 2018 9:08 am
by lmiltchev
@Matthew.Cary As krutaw mentioned, modifying via the REST API is possible by re-adding the host, overwrting the "old" object. Let us know if you have any more questions.

Re: Modifying a host via the REST API

Posted: Mon Apr 23, 2018 2:19 pm
by krutaw
krutaw wrote:
Matthew.Cary wrote:In our ongoing quest to use the rest API to automate the provisioning of Nagios XI I have another question:

I understand how to add a host via the API, and I know how to delete a host via the API, but how does one *modify* a host? What if I want to use the API to add or remove a hostgroup from an existing host? Or change the Template it is using?

I don't seem to be able to find this in the documentation.
Yup, we do it all the time, though probably not how you think. When we goto "modify" a host, we first make a call to read the host properties to build the necessary post data, and then append/replace the necessary bits that need to change. Then we simply post to add the host again on the nagios server where it lives (I have a small fleet of them.) When you re-add the host, it'll simply overwrite the existing host entry, keeping the same table ID's in the backend nagiosql database.

Hope that helps! ;)
I forgot to mention that if you have multiple Nagios servers that you check to see which server a given host is monitored by before submitting the post so that you don't end up doing double the monitoring by accident. I literally wrote a "get_host" function for my chatops script that determines which of my 5 nagios servers a given server is monitored by so I don't accidentally duplicate effort.

Re: Modifying a host via the REST API

Posted: Mon Apr 23, 2018 3:02 pm
by npolovenko
Thanks, @krutaw! That's a good note.
@Matthew.Cary, Let us know if you have any questions.