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.")
$ ./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