Page 1 of 1

Renaming Hosts via API

Posted: Tue May 18, 2021 11:39 am
by optionstechnology
I use this command to rename the localhost to its actual system name-

Code: Select all

curl -XPUT -k "https://$NAGSERVER/nagiosxi/api/v1/config/host/localhost?apikey=$NAGAPI&pretty=1&host_name=$NAGSERVER"
But, whenever I run that the services assigned to localhost do not automatically reload their hostname, and break the system from restarting-

Code: Select all

An error occurred while attempting to apply your configuration to Nagios Core. Monitoring engine configuration files have been rolled back to their last known good checkpoint.

Error: Could not find any host matching 'localhost' (config file '/usr/local/nagios/etc/services/localhost.cfg', starting on line 104)
Error: Failed to expand host list 'localhost' for service 'Total Processes' (/usr/local/nagios/etc/services/localhost.cfg:104)
To fix this I literally just need to open one of the services, change nothing, and click on save and that is enough to fix it

Problem is I need an automatic fix for this -- so, how do I avoid this, or failing that automate my workaround of the error?

Re: Renaming Hosts via API

Posted: Wed May 19, 2021 11:37 am
by vtrac
Hi,
How are you doing?

I tested this out on my Nagios XI 5.8.3.
You first need to change "localhost" to some new host
Example:

Code: Select all

curl -XPUT "http://x.x.x.x/nagiosxi/api/v1/config/host/localhost?apikey=yourAPIKEY&pretty=1&host_name=myNewHost"
Then you also need to change all your "services" as well.
However, you must do this for each "localhost" services.

Example, change "PING" service of "localhost":

Code: Select all

curl -XPUT "http://x.x.x.x/nagiosxi/api/v1/config/service/localhost/PING?apikey=yourAPIKEY&pretty=1&host_name=myNewHost&config_name=myNewHost"
Once done, you must also do "ApplyConfig":
Example:

Code: Select all

curl -XPUT "http://x.x.x.x/nagiosxi/api/v1/config/host/localhost?apikey=yourAPIKEY&pretty=1&applyconfig=1"

Best Regards,
Vinh