Page 1 of 2

Bug in API

Posted: Thu Oct 08, 2015 4:55 pm
by BanditBBS
I just found a killer bug(for me) in the new API.

I am trying to automate adding our new hosts into XI with the basic checks. I have the host going in(works fine) and then a list of 11 services in 11 separate CURL statements being called. Only the last one gets added. I can rearrange all I want and only the last one of the list of services is added. Now, I put an apply changes API call between 2 of then and a sleep(30) and the one before the apply and the last one are both added. I would not be able to of course do that in production.

So, my question, is this a bug or by design?

To show you, the CURLS all work:

Code: Select all

{ "success": "Successfully added test667 to the system. Config imported but not yet applied." } 
{ "success": "Successfully added test667 :: Load to the system. Config imported but not yet applied." } 
{ "success": "Successfully added test667 :: CPU to the system. Config imported but not yet applied." } 
{ "success": "Successfully added test667 :: Disk to the system. Config imported but not yet applied." } 
{ "success": "Successfully added test667 :: Cron Scheduler to the system. Config imported but not yet applied." } 
{ "success": "Successfully added test667 :: Log File to the system. Config imported but not yet applied." } 
{ "success": "Successfully added test667 :: Memory to the system. Config imported but not yet applied." } 
{ "success": "Successfully added test667 :: NFS to the system. Config imported but not yet applied." } 
{ "success": "Successfully added test667 :: Read-Only File System to the system. Config imported but not yet applied." } 
{ "success": "Successfully added test667 :: Swap to the system. Config imported but not yet applied." } 
{ "success": "Successfully added test667 :: Total Processes to the system. Config imported but not yet applied." } 
{ "success": "Successfully added test667 :: Zombie Processes to the system. Config imported but not yet applied." }

Re: Bug in API

Posted: Thu Oct 08, 2015 4:59 pm
by tmcdonald
I'll bet we are overwriting the unique ID in the DB. Do this:

Make a curl call to add a service, then run:

echo "use nagiosql; select id, service_description from tbl_service order by id desc limit 0,3;" | mysql -u root -p<yourpassword>

then do another curl call for a different service and run the same thing again. Post results.

Re: Bug in API

Posted: Thu Oct 08, 2015 5:12 pm
by jolson
I attempted to recreate this bug in a test environment and could not do so - after adding the services via the API, they appeared properly in the Core Config Manager. After the configuration was applied, everything was written out properly. I figured I would throw my experience in here - could you post the API calls you used specifically?

Re: Bug in API

Posted: Thu Oct 08, 2015 6:49 pm
by BanditBBS
jolson wrote:I attempted to recreate this bug in a test environment and could not do so - after adding the services via the API, they appeared properly in the Core Config Manager. After the configuration was applied, everything was written out properly. I figured I would throw my experience in here - could you post the API calls you used specifically?
I have to agree, it works flawlessly from cli, as I tested many times after Trevor's reply. I'm VPN'd into work and going to debug this until I find the cause or the solution. My php is good, so it is really confusing me as to why its doing it. The only thing different between it and cli is the php is being called from within XI itself in a component. I'm going to hardcoded some variables and run the php from cli and verify that it is good. When that is proven, then will try it with everything hardcoded and from component again and see if it doesn't work. I'll be updating this thread :)

Re: Bug in API

Posted: Thu Oct 08, 2015 7:50 pm
by BanditBBS
Ok, here is the code output:

Code: Select all

Everything worked, adding to NagiosXI..... 
Host : OK : { "success": "Successfully added test667 to the system. Config imported but not yet applied." } 
Load : OK : { "success": "Successfully added test667 :: Load to the system. Config imported but not yet applied." } 
CPU : OK : { "success": "Successfully added test667 :: CPU to the system. Config imported but not yet applied." } 
Disk : OK : { "success": "Successfully added test667 :: Disk to the system. Config imported but not yet applied." } 
Cron : OK : { "success": "Successfully added test667 :: Cron Scheduler to the system. Config imported but not yet applied." } 
/var/log/messages : OK : { "success": "Successfully added test667 :: Log File to the system. Config imported but not yet applied." } 
Memory : OK : { "success": "Successfully added test667 :: Memory to the system. Config imported but not yet applied." } 
NFS : OK : { "success": "Successfully added test667 :: NFS to the system. Config imported but not yet applied." } 
Read Only FS : OK : { "success": "Successfully added test667 :: Read-Only File System to the system. Config imported but not yet applied." } 
Swap : OK : { "success": "Successfully added test667 :: Swap to the system. Config imported but not yet applied." } 
Total Procs : OK : { "success": "Successfully added test667 :: Total Processes to the system. Config imported but not yet applied." } 
Zombie Procs : OK : { "success": "Successfully added test667 :: Zombie Processes to the system. Config imported but not yet applied." } 
That is the same output no matter if it works or not. I found a work around to make it work and can take a pretty good guess at what is causing the problem.

The Issue: Only the host and the 11th service was being added even though I got success on all 11 services. They are configured in the php and 12 curl statements one right after another, so firing off very quick(and all receiving the success).
The fix: Add a "sleep(3);" between each curl(api call). The host and all 11 services are then added to database.
The observation: Change the sleep to 2 instead of 3 and only half the items get added to database even though all return with success. There is some issue hitting the API to quickly, even though it returns success.

So I can work around the issue by slowing the script down by a total of 30+ seconds and probably will change the sleep to a 4 just to be safe. Hate to do it, but looks like it is needed.

Edit: Created bug report - 766

Re: Bug in API

Posted: Fri Oct 09, 2015 1:06 pm
by lmiltchev
I was able to recreate the issue and I will do some more digging into it (before filing a bug report). I thought I could use a "workaround"... I used "applyconfig=0" in all of my commands and "applyconfig=1" in the last one. This didn't work however - the effect was the same - only the last service was added. When I used "sleep 5", all of the 10 services were added. I am waiting on a feedback from our developers.

Re: Bug in API

Posted: Fri Oct 09, 2015 1:42 pm
by lmiltchev
Update: I just submitted an internal bug report (TASK ID 6598).

Re: Bug in API

Posted: Wed Nov 04, 2015 5:47 pm
by BanditBBS
You guys are going to hate this post, so, sorry in advance!

I just saw http://tracker.nagios.com/view.php?id=766 was marked as fixed in 5.2.1. Any ETA on arrival of this awesome bugfix version? :) This bug is the only thing slowing me down from automating adding of newly deployed hosts here, something that really needs to get done.

Delete this if you want, I just had to ask, lol!

Re: Bug in API

Posted: Wed Nov 04, 2015 5:57 pm
by tmcdonald
You are going to hate this response, so, not sorry in advance!

No idea :) Up to the devs, I was pushing for as close to weekly or tweekly (my word for "every two weeks" since "bi-weekly" means different things depending on who you ask) but that did not happen. Soon? It'll be soon.

Re: Bug in API

Posted: Tue Nov 10, 2015 11:13 am
by BanditBBS
This bug is still present in 5.2.1. I just added a host with 11 services and only the host and 7 of the services were added, even though all 11 got the success message.