Nagios API and Free Variables

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
SteveBeauchemin
Posts: 524
Joined: Mon Oct 14, 2013 7:19 pm

Nagios API and Free Variables

Post 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
XI 5.7.3 / Core 4.4.6 / NagVis 1.9.8 / LiveStatus 1.5.0p11 / RRDCached 1.7.0 / Redis 3.2.8 /
SNMPTT / Gearman 0.33-7 / Mod_Gearman 3.0.7 / NLS 2.0.8 / NNA 2.3.1 /
NSClient 0.5.0 / NRPE Solaris 3.2.1 Linux 3.2.1 HPUX 3.2.1
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Nagios API and Free Variables

Post 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
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Nagios API and Free Variables

Post 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
SteveBeauchemin
Posts: 524
Joined: Mon Oct 14, 2013 7:19 pm

Re: Nagios API and Free Variables

Post 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
XI 5.7.3 / Core 4.4.6 / NagVis 1.9.8 / LiveStatus 1.5.0p11 / RRDCached 1.7.0 / Redis 3.2.8 /
SNMPTT / Gearman 0.33-7 / Mod_Gearman 3.0.7 / NLS 2.0.8 / NNA 2.3.1 /
NSClient 0.5.0 / NRPE Solaris 3.2.1 Linux 3.2.1 HPUX 3.2.1
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Nagios API and Free Variables

Post by lmiltchev »

I will let you know some time on Thursday if that worked for me.
Sure. We will keep the thread open.
Be sure to check out our Knowledgebase for helpful articles and solutions!
SteveBeauchemin
Posts: 524
Joined: Mon Oct 14, 2013 7:19 pm

Re: Nagios API and Free Variables

Post 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
XI 5.7.3 / Core 4.4.6 / NagVis 1.9.8 / LiveStatus 1.5.0p11 / RRDCached 1.7.0 / Redis 3.2.8 /
SNMPTT / Gearman 0.33-7 / Mod_Gearman 3.0.7 / NLS 2.0.8 / NNA 2.3.1 /
NSClient 0.5.0 / NRPE Solaris 3.2.1 Linux 3.2.1 HPUX 3.2.1
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Nagios API and Free Variables

Post by ssax »

Awesome, I'm glad you like it! Is it okay if we mark this as resolved and lock the thread?
SteveBeauchemin
Posts: 524
Joined: Mon Oct 14, 2013 7:19 pm

Re: Nagios API and Free Variables

Post by SteveBeauchemin »

yes to the lock question.
XI 5.7.3 / Core 4.4.6 / NagVis 1.9.8 / LiveStatus 1.5.0p11 / RRDCached 1.7.0 / Redis 3.2.8 /
SNMPTT / Gearman 0.33-7 / Mod_Gearman 3.0.7 / NLS 2.0.8 / NNA 2.3.1 /
NSClient 0.5.0 / NRPE Solaris 3.2.1 Linux 3.2.1 HPUX 3.2.1
Locked