Nagios XI Rest API using JSON
Posted: Tue Oct 16, 2018 1:34 pm
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
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