I am automating the creation of host objects in Nagios. The idea is that when Nagios client is installed on a host, it will then "register" itself via the REST API using a Chef resource.
This is pretty easily done using curl, but curl doesn't give me the flexibility I want or the ability to run on Windows.
Is it possible that Nagios XI has a REST API that doesn't understand JSON?
I cannot figure out how to get this:
curl -k -XPOST "https://nagiosserver/nagiosxi/api/v1/co ... udfkjdncjk fhakjcnjanckjchkenajklcnakljcnakjcnakjcnk&pretty=1" -d "host_name=testname&address=172.17.17.17&use=Non-Prod_App_Linux_Servers&parents=Datacenter_GW_Vlan100_Network&applyconfig=1&is_active=1&force=1"
into what I use for Rest API calls on many, many other platforms:
require 'rest-client'
require 'json'
def add_nagios_host(name, ip_address)
begin
response = RestClient::Request.execute(
:url => "https://nagiosserver/nagiosxi/api/v1/config/host/",
method: :post,
:verify_ssl => false,
:headers => {"Content-Type":"application/json", "accept":"application/json"},
:apikey => 'sddjkfjkwifakxmckdkdkdkdkjkmvwriivjasdvmkzxcvmasdvmasdkmmvcccmv',
:payload => {
"host_name": "test-server",
"address": "172.17.17.17",
"use": "Non-Prod_App_Linux_Servers",
"parents": "Datacenter_GW_Vlan100_Network",
"applyconfig": 1,
"is_active": 1,
"force": 1
}.to_json
)
rescue RestClient::ExceptionWithResponse => e
puts e.response
end
end
Nagios XI Rest API using JSON
Re: Nagios XI Rest API using JSON
The API will return XML or JSON but does not accept it when creating objects. The curl command uses the Content-Type of application/x-www-form-urlencoded:
Code: Select all
POST /nagiosxi/api/v1/config/host?apikey=verylongapikey&pretty=1 HTTP/1.1
User-Agent: curl/7.29.0
Host: 192.168.55.2
Accept: */*
Content-Length: 142
Content-Type: application/x-www-form-urlencoded
host_name=testname&address=172.17.17.17&use=Non-Prod_App_Linux_Servers&parents=Datacenter_GW_Vlan100_Network&applyconfig=1&is_active=1&force=1
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
-
- Posts: 3
- Joined: Fri Oct 12, 2018 12:34 pm
Re: Nagios XI Rest API using JSON
Is that going to change anytime soon?
Re: Nagios XI Rest API using JSON
I don't believe it's on the roadmap but will file a feature request for it.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
-
- Posts: 96
- Joined: Tue Jan 26, 2016 5:29 pm
Re: Nagios XI Rest API using JSON
Good Afternoon,
Caveat1. It's Friday afternoon & I may be totally misunderstanding what y'all are talking about. If True, please disregard.
Caveat2. I think that's Ruby or Rails or Ruby on Rails. No idea.
Caveat3. I can't speak to the Chef, Register, etc. requirements. Therefore, my sol'n is limited in scope.
Caveat3. I use Python & that's what my solution is in. The Requests lib is your friend.
http://docs.python-requests.org/en/master/
Requirement: execute API Calls, without the use of Curl, via a cross platform (to include windows) language.
This completes adding objects via the API, without the use of curl. I'm currently running instances of Python 3 on CENTOS, RHEL, & Windows. As I mentioned, I made some changes to increase clarity & hopefully simplicity. I tried to keep this as copypasta as possible, but I'm sure some of the formatting/indents something is jacked up. If this isn't what you're looking for. My apologies, hopefully someone can find some use with this.
Cheers,
Max
Caveat1. It's Friday afternoon & I may be totally misunderstanding what y'all are talking about. If True, please disregard.
Caveat2. I think that's Ruby or Rails or Ruby on Rails. No idea.
Caveat3. I can't speak to the Chef, Register, etc. requirements. Therefore, my sol'n is limited in scope.
Caveat3. I use Python & that's what my solution is in. The Requests lib is your friend.
http://docs.python-requests.org/en/master/
Requirement: execute API Calls, without the use of Curl, via a cross platform (to include windows) language.
Code: Select all
# Author: Maxwell Ramirez
# Date: October 2018
import urllib3
import requests
def apply_service():
# Remember to change NAG_INST & API_KEY
r = requests.get("https://NAG_INST/nagiosxi/api/v1/system/applyconfig?apikey=API_KEY, verify=False).json()
print(r)
def create_contact(self, dat, print_result=0):
# Passing in the Dictionary given in main should result in something like this printing & being added to Nagios.
# payload_contact = '''
# define contact{
# contact_name Alice
# alias Alice_Alias
# email Foo@mail.com
# use xi_contact_generic
# force 1
# }
# '''
payload_contact = 'define contact{\n'
# keys of the dictionary dat
# We're going for 4 LHS Spaces 'Key' 'value'\n
for i in dat:
payload_contact += ' {} {}\n'.format(i, dat[i])
payload_contact += '}'
# Remember to change NAG_INST & API_KEY
r = requests.post("https://NAG_INST/nagiosxi/api/v1/config/import?apikey=API_KEY", data=payload_contact, verify=False)
if print_result:
print(payload_contact)
print(r.text)
print(r.url)
print()
# I'm leveraging classes & I'd probably pass in a list at worst case. However, for clarity I'm sending all the vars.
def create_hosts(host_name_in, address_in, use_in, hostgroups_in, NAG_INST, API_KEY):
payload = {'host_name': host_name_in, 'address': address_in, 'use': use_in, 'force': 1, 'hostgroups': hostgroups_in}
r = requests.post(
"http://NAG_INST/nagiosxi/api/v1/config/host?apikey=API_KEY", data=payload, verify=False).json()
print(r)
def main():
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Args for Nagios objects as a dictionary.
payload_contact_dict = {'contact_name': 'Alice', 'alias': 'Alice_Alias', 'email': 'foo@mail.com', 'use': 'xi_contact_generic', 'force': 1}
# Note: I use templates via the 'use' arg. However, pass in any valid Nagios object Key-Value pair.
payload_service_template_dict = {'name': 'some_service_template', 'use': 'Foo_Service_Template', 'check_command': 'check_xi_ncpa!-t $USER5$ -M ', 'register': 0}
# Create stuff on Nagios
# if all you want to do is make hosts. You can appeal to the API Directly. Similarly for known Objects(services, groups, commands, etc)
create_hosts(host_name_in, address_in, use_in, hostgroups_in, NAG_INST, API_KEY)
# These objects are not implemented yet & are closer to your original ask. We must use import config.
## creates a single contact using payload_contact_dict.
# The '1' is to activate the optional print parameter. Omitted as in 'create_service_template' won't print anything.
# create_contact(payload_contact_dict, 1)
## For brevity I left off this fn. It's extremely similar to create_contact.
# create_service_template(payload_service_template_dict)
apply_service()
if __name__ == "__main__":
main()
Cheers,
Max
Re: Nagios XI Rest API using JSON
Thanks for sharing @Maxwellb99!
@wayneagostino, let us know if you have any further questions.
@wayneagostino, let us know if you have any further questions.
Be sure to check out our Knowledgebase for helpful articles and solutions!