Page 1 of 1

Nagios API help requested

Posted: Thu May 11, 2017 2:08 pm
by SteveBeauchemin
I have an advanced user who adds and deletes from Nagios XI using the API. This is a good thing. I encourage it.

Sometimes we have an issue with the Addition of a host.

If a Host is being added, and that host calls for a 'Parent', but the Parent does not yet exist, something bad happens. What happens it that the API creates the 'Parent' as a blank Host, in a Deactivated state. The actual host being defined is created, and points to the deactivated Parent. The API does this.

From that point forward, Nagios commit will fail.

If the system is in that state during the off hours, and the normal Nagios XI cron jobs run, maybe at 2 or 3:00 AM, the cron will restart Nagios, and Nagios will fail to restart. Then the Nagios "Monitoring Engine" will be red until someone notices and fixes the problem. Basically, manually run the "Write Configs" and "Verify Files", read the output, and fix or delete the host with no parent.

The solution to this could be to simply have the API user check for the Parent before adding the Child host.

But - what if both the Parent and Child host are created in the same batch. In the API, if I create a Host but have not yet run a commit, I cannot see if that host has been added yet.

Can I ? If so, how.

The API will let me know if a host exists after a commit. When adding hosts in a batch, we wait until we have added them all and then commit the change.

How can we see, using the API, that a host is created and the commit for that host is pending?

The API user has asked me the following questions and made the following comments:
I agree that the API must be improved. Here are my suggestions to Nagios.

1) The API should not be a second class citizen to the web interfaces. Currently there are time where the API cannot delete an object but the web interface can. This should not be.
2) The API should be able to set a 'cascade' flag that can delete a host object and then all object directly tied to that host are also deleted (or re-parented)
3) The API should have a COMMIT/ROLLBACK functionality where I can make a set of changes and commit all of them at once, and if one fails, then all the changes get rolled back.
4) The API should be able to create all objects, templates, services, etc.
Could someone please assist me with a solution here?

Thanks

Steve B

Re: Nagios API help requested

Posted: Fri May 12, 2017 10:09 am
by tgriep
Currently the API does not have all of the features that most people are requesting but it is continually getting updated and hopefully in the future, it will do what is suggested.

The API cannot directly check to see if a object has been created on the Nagios Server but not applied.
The closest thing you could do is to get the status of the host and if it is running it will return valid data but if the host is not running, it will return a record count of 0.

Re: Nagios API help requested

Posted: Fri May 12, 2017 12:15 pm
by SteveBeauchemin
Clarification please.
The closest thing you could do is to get the status of the host and if it is running it will return valid data but if the host is not running, it will return a record count of 0.
If I use the API, and make a host. An API query will return data? or 0?
Is that before being applied? after being applied?

We know we can get data from an existing host once the API is applied.

The problem comes in Parent/Child situations. The API making a bogus Parent host just because an API call to create a different host says it has a Parent is the part that hurts.

Can we make the API fail if the Parent host does not exist? You php developers can put something quick and dirty together for me right?

Steve B

Re: Nagios API help requested

Posted: Fri May 12, 2017 1:59 pm
by tgriep
Here is the clarification you requested
If I run a Host Status check using the API

Code: Select all

curl -XGET "https://192.168.112.129/nagiosxi/api/v1/objects/host?apikey=j8hf9ss5&pretty=1&host_name=localhost" -k
If the host exists and it running, it will return this.

Code: Select all

 {
    "hostlist": {
        "recordcount": "1",
        "host": {
            "@attributes": {
                "id": "135"
            },
            "instance_id": "1",
            "host_name": "localhost",
            "is_active": "1",
            "config_type": "1",
            "alias": "localhost alias",
            "display_name": "localhost",
            "address": "localhost",
            "check_interval": "5",
            "retry_interval": "1",
            "max_check_attempts": "10",
            "first_notification_delay": "0",
            "notification_interval": "120",
            "passive_checks_enabled": "1",
            "active_checks_enabled": "1",
            "notifications_enabled": "1",
            "notes": "",
            "notes_url": "http:\/\/hostname\/$HOSTGROUPNAME$:$HOSTNAME$",
            "action_url": "",
            "icon_image": "",
            "icon_image_alt": "",
            "statusmap_image": ""
        }
    }
}
If a host status was run against a host that is in the CCM but not applied,

Code: Select all

curl -XGET "https://192.168.112.129/nagiosxi/api/v1/objects/host?apikey=j8hf9ss5&pretty=1&host_name=badhost" -k 

You would receive this output.

Code: Select all

{
    "hostlist": {
        "recordcount": "0"
    }
}
I think the API cannot see if a host is configured in the CCM / database but not applied, the API to me looks like it can only get the status of applied configs.

Getting the functionality you want probably will not be a quick and dirty fix.

The best thing is to create the parent and apply it and then go back and create the hosts using that parent.
That would work the same as using the CCM, you cannot add a parent to a host until it is added to the system.

Having the API create a parent host automatically when a host is created, would be difficult. What check command do you apply to the parent, IP address, what template, etc... would you use.

Re: Nagios API help requested

Posted: Mon May 15, 2017 10:15 am
by SteveBeauchemin
Thanks for the addition information. It is more clear and I will pass this on.

Just to be more clear in one thing. You say
Having the API create a parent host automatically when a host is created, would be difficult. What check command do you apply to the parent, IP address, what template, etc... would you use.
But that is exactly what is happening. The API is creating the Parent host, just with no actual useful setup, and in a deactivated state. It should just fail to create the new host that is defined with a non existing parent.

In any case, I'll find some other way to get this working. I just needed to make sure that you knew about the situation and could make the API better in the future as a result.

Thanks. This can be closed.

Steve B