Page 1 of 1
Deleting host via API
Posted: Mon Feb 27, 2017 5:16 am
by ibnetworking
Hi guys,
We've automated our nagios monitoring using a variety of scripts, mainly written in python, and it appears to be working correctly.
However, we're running into an issue when a host needs to be removed.
The logic is, if a service gets deleted, and that is the last service on that host, delete the host as well. Simple.
Here, we find that host is never deleted correctly, throwing the following error:
Code: Select all
{
"error": "Host cannot be deleted using this method. Must be deleted through the CCM."
}
When we include a delay of a couple seconds between deleting the service and the host , like:
it works, and host gets deleted properly.
Is this an issue with the host delete API being called to quick after service delete is called? I'm guessing it doesn't have the time to remove all service information correctly from the DB and throws a dependency error?
Please advise what's the best course of actions to take here. Having a delay of a couple of seconds isn't an issue but isn't ideal. Just wanted to check if this is the best solution for now?
Regards,
Re: Deleting host via API
Posted: Mon Feb 27, 2017 1:32 pm
by mcapra
Assuming you're running an apply config at the appropriate stage, the sleep is probably the best option right now. My suspicion is that (not unlike your guess) there's a nit-picky timing issue between when the host's services are removed from the database VS when the host attempts to resolve it's dependencies.
I see the same behavior using this:
Code: Select all
import pycurl
import StringIO
def curl(url):
response = StringIO.StringIO()
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.WRITEFUNCTION, response.write)
c.setopt(c.HTTPHEADER, ['Content-Type: application/json','Accept-Charset: UTF-8'])
c.setopt(c.CUSTOMREQUEST, 'DELETE')
c.perform()
c.close()
ret = str(response.getvalue())
response.close()
return ret
print curl('http://192.168.67.1/nagiosxi/api/v1/config/service?apikey=KR2LLsBuhmmFnS4dbmeURW0culVlv39vbbBVW8pet69bXdH8CUiK8DcFX7gMpohD&pretty=1&host_name=192.168.67.105&service_description=%2F%20Disk%20Usage')
print curl('http://192.168.67.1/nagiosxi/api/v1/config/service?apikey=KR2LLsBuhmmFnS4dbmeURW0culVlv39vbbBVW8pet69bXdH8CUiK8DcFX7gMpohD&pretty=1&host_name=192.168.67.105&service_description=CPU%20Usage')
print curl('http://192.168.67.1/nagiosxi/api/v1/config/service?apikey=KR2LLsBuhmmFnS4dbmeURW0culVlv39vbbBVW8pet69bXdH8CUiK8DcFX7gMpohD&pretty=1&host_name=192.168.67.105&service_description=Memory%20Usage')
print curl('http://192.168.67.1/nagiosxi/api/v1/config/service?apikey=KR2LLsBuhmmFnS4dbmeURW0culVlv39vbbBVW8pet69bXdH8CUiK8DcFX7gMpohD&pretty=1&host_name=192.168.67.105&service_description=Swap%20Usage')
print curl('http://192.168.67.1/nagiosxi/api/v1/config/host?apikey=KR2LLsBuhmmFnS4dbmeURW0culVlv39vbbBVW8pet69bXdH8CUiK8DcFX7gMpohD&pretty=1&host_name=192.168.67.105&applyconfig=1')
Which produces:
Code: Select all
{
"success": "Removed 192.168.67.105 :: \/ Disk Usage from the system. Config imported but not yet applied."
}
{
"success": "Removed 192.168.67.105 :: CPU Usage from the system. Config imported but not yet applied."
}
{
"success": "Removed 192.168.67.105 :: Memory Usage from the system. Config imported but not yet applied."
}
{
"success": "Removed 192.168.67.105 :: Swap Usage from the system. Config imported but not yet applied."
}
{
"error": "Host cannot be deleted using this method. Must be deleted through the CCM."
}
Though it's all good if I add in a sleep before deleting the host. The API is currently undergoing some reworks that should resolve this, though I don't have a timeframe on when that'll be live. Ideally, there should be no response until the API call is 100% absolutely done though that's not how it works currently.
Re: Deleting host via API
Posted: Tue Feb 28, 2017 3:57 am
by ibnetworking
Hello,
got it.
Thanks for your help.
Re: Deleting host via API
Posted: Tue Feb 28, 2017 10:25 am
by dwhitfield
It sounds like this issue has been resolved. Is it okay if we lock this thread? Thanks for choosing the Nagios forums!
Re: Deleting host via API
Posted: Wed Mar 01, 2017 3:06 am
by ibnetworking
Hi,
well it hasn't been resolved, only acknowledged.
But yes, you can lock it.
Re: Deleting host via API
Posted: Wed Mar 01, 2017 11:42 am
by dwhitfield
Fair enough. Locking.