Page 1 of 1

Nagios API and Free Variables

Posted: Wed Jun 01, 2016 12:19 pm
by SteveBeauchemin
I would like to use the API to add hosts to Nagios XI.

Part of adding hosts this way would involve using Free Variables, so I can communicate that the host was added dynamically. Also, the Free Variables would provide important information used by service tests.

So far, I have been unable to add a host via the API and have Free Variables show up.

Is the API limited to adding only standard items? Is there a way for the API to add Free Variables?

Thanks

Steve B

Re: Nagios API and Free Variables

Posted: Wed Jun 01, 2016 3:43 pm
by ssax
I'm unable to get it to add the free variables either, I'll see if I can get you a workaround.

I don't think it currently supports it, here is a feature request for it:

Code: Select all

NEW TASK ID 8675 created - Nagios XI Feature Request: API - Add the ability to add free variables

Re: Nagios API and Free Variables

Posted: Wed Jun 01, 2016 4:11 pm
by ssax
Do you only need it on the host level?

*** This was written for Nagios XI 5.2.8

*** Note: This will get reverted if you upgrade and will need to be reimplemented.
*** Make sure that you have known-good backups/vm snapshots before making any modifications

If so, edit this file:

Code: Select all

/usr/local/nagiosxi/html/api/includes/utils-api.inc.php
Around line 539 change this code:

Code: Select all

        foreach ($others as $o) {
            if (array_key_exists($o, $args)) {
                if ($o == 'check_command') { $args[$o] = stripslashes($args[$o]); }
                $obj[$o] = $args[$o];
            }
        }
To this:

Code: Select all

        foreach ($others as $o) {
            if (array_key_exists($o, $args)) {
                if ($o == 'check_command') { $args[$o] = stripslashes($args[$o]); }
                $obj[$o] = $args[$o];
            }
        }

        // Add any free variables that we find (they must start with an underscore)
        foreach ($args as $key => $value) {
            if (substr($key, 0, 1) === '_') {
                $obj[$key] = $value;
            }
        }
Then you can just prepend any free variables with underscores (as is required per the documentation) like so:

Code: Select all

&_variable1name=variable1value&_variable2name=variable2value

Re: Nagios API and Free Variables

Posted: Wed Jun 01, 2016 6:20 pm
by SteveBeauchemin
I do only need it at the host level, and it won't be the first time I've had to change the php code to get stuff doing what I need.

Awesome feedback!

I will let you know some time on Thursday if that worked for me.

Thanks

Steve B

Re: Nagios API and Free Variables

Posted: Thu Jun 02, 2016 9:03 am
by lmiltchev
I will let you know some time on Thursday if that worked for me.
Sure. We will keep the thread open.

Re: Nagios API and Free Variables

Posted: Fri Jun 03, 2016 6:42 pm
by SteveBeauchemin
Did I say Thursday? I meant Friday.

I added the couple lines in today and it's awesome. The API just got better, smarter...

Someone should make that a permanent change.

Thanks

Steve B

Re: Nagios API and Free Variables

Posted: Mon Jun 06, 2016 9:54 am
by ssax
Awesome, I'm glad you like it! Is it okay if we mark this as resolved and lock the thread?

Re: Nagios API and Free Variables

Posted: Wed Jun 08, 2016 12:50 pm
by SteveBeauchemin
yes to the lock question.