Bulk custom host variables - via API or other means?

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
jseldon
Posts: 5
Joined: Thu Jul 18, 2024 1:50 pm

Bulk custom host variables - via API or other means?

Post 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
snapier3
Posts: 144
Joined: Tue Apr 23, 2019 7:12 pm

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

Post 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
jseldon
Posts: 5
Joined: Thu Jul 18, 2024 1:50 pm

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

Post 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
Last edited by jseldon on Wed Sep 11, 2024 11:02 am, edited 1 time in total.
gwesterman
Posts: 269
Joined: Wed Aug 23, 2023 11:29 am

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

Post 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!
jseldon
Posts: 5
Joined: Thu Jul 18, 2024 1:50 pm

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

Post 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!
Last edited by jseldon on Wed Sep 11, 2024 1:50 pm, edited 1 time in total.
snapier3
Posts: 144
Joined: Tue Apr 23, 2019 7:12 pm

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

Post 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...
gwesterman
Posts: 269
Joined: Wed Aug 23, 2023 11:29 am

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

Post 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!
Locked