Deleting host via API

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
ibnetworking
Posts: 19
Joined: Mon Jan 16, 2017 4:32 am

Deleting host via API

Post 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:

Code: Select all

time.sleep(5)
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,
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Deleting host via API

Post 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.
Former Nagios employee
https://www.mcapra.com/
ibnetworking
Posts: 19
Joined: Mon Jan 16, 2017 4:32 am

Re: Deleting host via API

Post by ibnetworking »

Hello,

got it.

Thanks for your help.
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: Deleting host via API

Post by dwhitfield »

It sounds like this issue has been resolved. Is it okay if we lock this thread? Thanks for choosing the Nagios forums!
ibnetworking
Posts: 19
Joined: Mon Jan 16, 2017 4:32 am

Re: Deleting host via API

Post by ibnetworking »

Hi,

well it hasn't been resolved, only acknowledged.
But yes, you can lock it.
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: Deleting host via API

Post by dwhitfield »

Fair enough. Locking.
Locked