Page 1 of 1

Bulk custom host variables - via API or other means?

Posted: Tue Sep 10, 2024 6:20 pm
by jseldon
Hi all,

I'm looking to add some custom host variables to track the physical rack location of baremetal servers but I seem to be having no luck getting this to work with the API adding the variables via host PUTs. I can manually add them when editing the host in the CCM and it works fine but given that I've got a bunch and need to automate this for future devices being added I'd like to understand if I'm just not forming my posts correctly or if the API lacks
the feature.

Here's my python code to update the host:

Code: Select all

def update_nagios_host(nagios_host, location, cabinet_name):
    """Update Nagios host with custom variables and apply config."""
    # Construct the payload to send custom variables as JSON in the request body
    payload = {
        "customvars": {
            "_location": location,
            "_cabinetname": cabinet_name
        }
    }

    url = f"{NAGIOS_API_URL}/{nagios_host}?apikey={API_TOKEN}&pretty=1&applyconfig=1"

    # Send the PUT request with JSON payload to Nagios API
    response = requests.put(url, json=payload, verify=False)  # Bypassing SSL verification

    # Output the full response for debugging
    print("Response from Nagios when updating host:")
    print(response.text)

    if response.status_code != 200:
        print(f"Error updating Nagios host: {response.text}")
        sys.exit(1)

    print("Nagios host updated successfully, and configuration applied.")
The resulting output:


$ ./addlocation.py spdc0001
Original Hostname: spdc0001
Modified Nagios Hostname: rm-spdc0001.mgmt
Location: DATA HERE
Cabinet: DATA HERE
Connecting to Nagios API and updating host...
Response from Nagios when updating host:
{
"success": "Updated rm-spdc0001.mgmt in the system. Config applied, Nagios Core was restarted."
}

Nagios host updated successfully, and configuration applied.




Despite this showing as successful the host doesn't appear to have the custom variables added.

Assuming you can do it through the API, can anyone tell me what I might be doing wrong? I've looked around and I haven't found anything in my searches beyond older posts.

I'd also be open to other suggestions if the API isn't going to work.

Thanks

James

Re: Bulk custom host variables - via API or other means?

Posted: Wed Sep 11, 2024 10:14 am
by snapier3
Your PUT Payload would be this.

Code: Select all

payload = {
            "_location": location,
            "_cabinetname": cabinet_name
        }
These two variables will be access with the $_HOSTLOCATION$ and $_HOSTCABINETNAME$ Macros

Re: Bulk custom host variables - via API or other means?

Posted: Wed Sep 11, 2024 10:58 am
by jseldon
Thanks. I tried this - same result. Is there a different endpoint for custom vars I'm missing?

It seems like this should work.

I've verified it's not updating the configuration files.


snapier3 wrote: Wed Sep 11, 2024 10:14 am Your PUT Payload would be this.

Code: Select all

payload = {
            "_location": location,
            "_cabinetname": cabinet_name
        }
These two variables will be access with the $_HOSTLOCATION$ and $_HOSTCABINETNAME$ Macros

Re: Bulk custom host variables - via API or other means?

Posted: Wed Sep 11, 2024 12:16 pm
by gwesterman
Hi @jseldon,

I was able to get it to work with the following script:

Code: Select all

import requests

# Define the URL and parameters
url = "<IP ADDRESS>/nagiosxi/api/v1/config/host/<HOST_NAME>"
params = {
    'apikey': <API_KEY>',
    'pretty': '1',
    'address': '<ADDRESS>',
    '_testvar1': '30',
    '_testvar2': '31',
    'applyconfig': '1'
}

# Make the PUT request
response = requests.put(url, params=params, verify=False)

# Print the response text (or handle it as needed)
print(response.text)
I then verified that the custom var was applied by checking out the config snapshots / looking at the host's misc settings.

This looks roughly the same as your script so I'm not quite sure why it isn't working for you. How are you verifying that the changes were made? Maybe try hard-coding the location / cabinet_name in case there is an issue elsewhere in the script.

Let us know what you find.

Thank you!

Re: Bulk custom host variables - via API or other means?

Posted: Wed Sep 11, 2024 1:43 pm
by jseldon
Thanks for verifying! I was able to get your code working so this suggests I've got something formatted slightly wrong. I'll keep messing around to figure out why. I appreciate your help.

Cheers

James

gwesterman wrote: Wed Sep 11, 2024 12:16 pm Hi @jseldon,

I was able to get it to work with the following script:

Code: Select all

import requests

# Define the URL and parameters
url = "<IP ADDRESS>/nagiosxi/api/v1/config/host/<HOST_NAME>"
params = {
    'apikey': <API_KEY>',
    'pretty': '1',
    'address': '<ADDRESS>',
    '_testvar1': '30',
    '_testvar2': '31',
    'applyconfig': '1'
}

# Make the PUT request
response = requests.put(url, params=params, verify=False)

# Print the response text (or handle it as needed)
print(response.text)
I then verified that the custom var was applied by checking out the config snapshots / looking at the host's misc settings.

This looks roughly the same as your script so I'm not quite sure why it isn't working for you. How are you verifying that the changes were made? Maybe try hard-coding the location / cabinet_name in case there is an issue elsewhere in the script.

Let us know what you find.

Thank you!

Re: Bulk custom host variables - via API or other means?

Posted: Wed Sep 11, 2024 2:45 pm
by snapier3
This looks roughly the same as your script so I'm not quite sure why it isn't working for you. How are you verifying that the changes were made? Maybe try hard-coding the location / cabinet_name in case there is an issue elsewhere in the script.
@gwesterman
Great example, includes all the required fields for a successful PUT action via the API.

My blurb was not complete :? I was only looking at the customvars...

Re: Bulk custom host variables - via API or other means?

Posted: Thu Sep 12, 2024 10:13 am
by gwesterman
I'm glad I could help!

Since it looks like your issue has been resolved, I'm going to lock this thread.

Thank you!