Page 2 of 3

Re: Automate daily mass delete hosts

Posted: Wed Mar 29, 2017 5:22 pm
by nagwindmon
will try to run my script with advised changes... in meantime what would you recommend for deleting hosts by utilizing XI scripts as I do now or REST API?
if REST API is preferred way do you mind to provide a sample how would I run it by cron?

Thanks!

Re: Automate daily mass delete hosts

Posted: Thu Mar 30, 2017 7:54 am
by nagwindmon
a quick update: using XI scripts is pretty much direct deletes into database so may not be a best practice so made an attempt to utilize REST API delete and got the following error:

[nagios@esu4v385 nagios]$ curl -k -XDELETE "http://esu4v385/nagiosxi/api/v1/config/ ... CVT3511956"
{
"error": "Host cannot be deleted using this method. Must be deleted through the CCM."
}


but the host looks like does not have any services or any other dependencies:

Please advise!

Re: Automate daily mass delete hosts

Posted: Thu Mar 30, 2017 10:43 am
by mcapra
Since that Host object has relationships with both the DRT_CVT and MCY-708-CST Hostgroups, you will first need to eliminate those dependencies before you can delete the Host object.

Since the API doesn't currently allow you to cherry-pick specific hosts out of hostgroups, you would essentially need to GET those hostgroups, modify them to not include CVT3511956, then delete and re-create them.

Re: Automate daily mass delete hosts

Posted: Thu Mar 30, 2017 10:56 am
by nagwindmon
where do you see item labeled as 'Dependent relationship' in the output?
In my example there is not a 'Dependent relationship' that should prohibit deletion...

Re: Automate daily mass delete hosts

Posted: Thu Mar 30, 2017 11:01 am
by mcapra
That aside, @lmiltchev tested this and even if there is a host->hostgroup and hostgroup->host dependency for a given host it shouldn't affect API deletion. I didn't have any problems deleting a host in that configuration.

You could try adding force=1 to your API request and see if that makes a difference. Your assessment is correct as far as I can tell that there shouldn't be any relationships preventing this host from being deleted.

Re: Automate daily mass delete hosts

Posted: Thu Mar 30, 2017 11:05 am
by lmiltchev
Thanks @mcapra!

nagwindmon, what is the Nagios XI version that you are currently using? It is possible that this is a bug with API, that was fixed in the later versions.

My test host was added to 3 hostgroups:
example01.PNG
but I was able to delete it just fine via the REST API.

Code: Select all

[root@main-nagios-xi ~]# curl -XDELETE "http://x.x.x.x/nagiosxi/api/v1/config/host?apikey=LTltbjobR0X3V5ViDIitYaI8hjsjoFBaOcWYukamF7oAsD8lhJRvSPWq8I3PjTf7&pretty=1&host_name=test&applyconfig=1"
{
    "success": "Removed test from the system. Config applied, Nagios Core was restarted."
}

Re: Automate daily mass delete hosts

Posted: Thu Mar 30, 2017 11:16 am
by nagwindmon
Nagios XI 5.2.0

Re: Automate daily mass delete hosts

Posted: Thu Mar 30, 2017 11:59 am
by lmiltchev
There were MANY, MANY bug fixes, and added features since 5.2.0. I would strongly recommend upgrading to the latest. You particular issue was probably fixed in 5.2.8. I see this in the changelog:
- Fixed API not deleting some hosts correctly [TPS#8500] -BH
https://assets.nagios.com/downloads/nag ... NGES-5.TXT

Looking at the task, I see this:
Description: Using rest Api cannot delete a host that is a member of a hostgroup

Steps To Reproduce:

1) Add a host and put it in a hostgroup
2) Try to delete the host using this cmd

curl -XDELETE "https://nagios/nagiosxi/api/v1/config/h ... lyconfig=1 "

3) results in -

"error": "Host cannot be deleted using this method. Must be deleted through the CCM."

Re: Automate daily mass delete hosts

Posted: Thu Mar 30, 2017 12:07 pm
by nagwindmon
got it, thanks lmiltchev! as always very much appreciate your inputs on this forum

Re: Automate daily mass delete hosts

Posted: Thu Mar 30, 2017 12:33 pm
by cdienger
Did you have any other questions or are we okay closing this?