API cant use templates?

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
agustin.roca
Posts: 39
Joined: Tue Apr 28, 2015 10:28 am

API cant use templates?

Post 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
User avatar
BanditBBS
Posts: 2474
Joined: Tue May 31, 2011 12:57 pm
Location: Scio, OH
Contact:

Re: API cant use templates?

Post 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
2 of XI5.6.14 Prod/DR/DEV - Nagios LogServer 2 Nodes
See my projects on the Exchange at BanditBBS - Also check out my Nagios stuff on my personal page at Bandit's Home and at github
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: API cant use templates?

Post 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. :)
Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
BanditBBS
Posts: 2474
Joined: Tue May 31, 2011 12:57 pm
Location: Scio, OH
Contact:

Re: API cant use templates?

Post 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 :)
2 of XI5.6.14 Prod/DR/DEV - Nagios LogServer 2 Nodes
See my projects on the Exchange at BanditBBS - Also check out my Nagios stuff on my personal page at Bandit's Home and at github
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: API cant use templates?

Post 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');
Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
BanditBBS
Posts: 2474
Joined: Tue May 31, 2011 12:57 pm
Location: Scio, OH
Contact:

Re: API cant use templates?

Post 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
2 of XI5.6.14 Prod/DR/DEV - Nagios LogServer 2 Nodes
See my projects on the Exchange at BanditBBS - Also check out my Nagios stuff on my personal page at Bandit's Home and at github
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: API cant use templates?

Post by lmiltchev »

Thanks, Bandit! I also created an internal bug report (TASK ID 6782).
Be sure to check out our Knowledgebase for helpful articles and solutions!
agustin.roca
Posts: 39
Joined: Tue Apr 28, 2015 10:28 am

Re: API cant use templates?

Post 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!
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: API cant use templates?

Post 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"
Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked