Page 1 of 1
API cant use templates?
Posted: Tue Oct 27, 2015 2:44 pm
by agustin.roca
Hi guys,
Just trying to add new hosts with API; but it asks for values that should be using templates in the way I design it so i cant use the API. Is there a plan to make the API able to use templates? so that i dont have to use single values on every host?
Cheers
Re: API cant use templates?
Posted: Tue Oct 27, 2015 2:50 pm
by BanditBBS
Yeah, that was a feature request of mine, it was put in to not require the field. It already supports templates. finding command, will edit post in a moment...
Edit:
https://support.nagios.com/forum/viewto ... 16&t=35061
Re: API cant use templates?
Posted: Tue Oct 27, 2015 3:49 pm
by lmiltchev
finding command, will edit post in a moment...
Bandit, I am really curious to see your command. I don't think the "use" directive is included in the API (yet), so using templates should not be possible at the moment... This is something I would like to see, too.

Re: API cant use templates?
Posted: Tue Oct 27, 2015 4:03 pm
by BanditBBS
Code: Select all
$result=`curl -XPOST "http://xxxxxxxxx.com/nagiosxi/api/v1/config/host?apikey=xxxxxxxx&pretty=1" -d "host_name=$hostname&address=$address&use=$hosttemplate&force=1"`;
I was previously told that ANYTHING you can define on a host/service you can do with the API.
It works, you can trust me

Re: API cant use templates?
Posted: Tue Oct 27, 2015 5:06 pm
by lmiltchev
It didn't work for me. In fact, I have tried using the "&use=<template>" in the past but no luck. I was hoping that adding the "&force=1" will do the trick, but it didn't.
I seemed like it did when I ran the command from the CLI:
Code: Select all
curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/config/host?apikey=pkus66vt&pretty=1" -d "host_name=localhost&address=127.0.0.1&use=linux-server&contacts=batkataad&force=1&applyconfig=1"
{
"success": "Successfully added localhost to the system. Config applied, Nagios Core was restarted."
}
but in the CCM, I can see that the status is "Not Applied" and I have some weird directives in the config:
Code: Select all
max_check_attempts 0
check_period
notification_interval 0
notification_period
And yes, I didn't have any config errros prior to running the API command and I could apply configuration.
I may be wrong but I think this is cause by the "required arguments" defined in the "/usr/local/nagiosxi/html/api/includes/utils-api.inc.php"...
Code: Select all
$required = array('host_name', 'address', 'max_check_attempts', 'check_period', 'notification_interval', 'notification_period');
Re: API cant use templates?
Posted: Tue Oct 27, 2015 5:16 pm
by BanditBBS
That's really weird, because before I replied I tested it and it worked. I didnt pay attention to other fields yet as this has only been in dev. The max attempts is being filled with a 0, so that is bad.
Code: Select all
###############################################################################
#
# Host configuration file
#
# Created by: Nagios Core Config Manager 2.5.0
# Date: 2015-10-27 16:03:17
# Version: Nagios 3.x config file
#
# --- DO NOT EDIT THIS FILE BY HAND ---
# Nagios CCM will overwrite all manual settings during the next update if you
# would like to edit files manually, place them in the 'static' directory or
# import your configs into the CCM by placing them in the 'import' directory.
#
###############################################################################
define host {
host_name test668
use extn_generic_host
address 127.0.0.1
max_check_attempts 0
check_period
notification_interval 0
notification_period
register 1
}
###############################################################################
#
# Host configuration file
#
# END OF FILE
#
###############################################################################
So I agree, there is an issue, the API is still writing it even if you leave it out

So that, plus the calling the API too quickly and bugs breaking my use of it
I can confirm that this is causing the problem:
Code: Select all
$required = array('host_name', 'address', 'max_check_attempts', 'check_period', 'notification_interval', 'notification_period');
and changing it to
Code: Select all
$required = array('host_name', 'address');
fixed the problem. So the code needs added to change the $required array if force=1.
I added bug report 765
Re: API cant use templates?
Posted: Wed Oct 28, 2015 1:40 pm
by lmiltchev
Thanks, Bandit! I also created an internal bug report (TASK ID 6782).
Re: API cant use templates?
Posted: Thu Oct 29, 2015 9:22 am
by agustin.roca
Great Guys! Thanks.
I will patch it locally but lets hope its patched in the next release so it doesnt mess the change!
Awesome work fellows.
Cheers!
Re: API cant use templates?
Posted: Thu Oct 29, 2015 10:16 am
by lmiltchev
FYI, the bug has been fixed in our dev revision and task 6782 has been closed. The fix should be in the next release of XI. You will still need to use "&force=1" to add a template since it then forces it to create even without the requirements, i.e.
Code: Select all
curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/config/host?apikey=pkus66vt&pretty=1" -d "host_name=myhost&address=x.x.x.x&use=mytemplate&force=1&applyconfig=1"