Connecting to Nagios API via a Website

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Connecting to Nagios API via a Website

Post by mcapra »

The API is configured to adhere to the Same-origin Policy, which is what I think is happening here. Javascript/jQuery probably won't be of much help without making changes to the API in your Core installation (not recommended).

I was able to write a simple PHP script that accomplished what the previous jQuery function did (192.168.3.87 is a separate machine from where this script was run):

Code: Select all

<?php
$url = 'http://nagiosadmin:[email protected]/nagios/cgi-bin/objectjson.cgi?query=hostlist&parenthost=none&childhost=none';
$obj = json_decode(file_get_contents($url), true);

print_r($obj);
?>
Which returns the following and stores it in $obj:

Code: Select all

Array ( [format_version] => 0 [result] => Array ( [query_time] => 1468945372000 [cgi] => objectjson.cgi [user] => nagiosadmin [query] => hostlist [query_status] => released [program_start] => 1468867888000 [last_data_update] => 1468867888000 [type_code] => 0 [type_text] => Success [message] => ) [data] => Array ( [selectors] => Array ( [parenthost] => none [childhost] => none ) [hostlist] => Array ( [0] => localhost ) ) )
My [hostlist] on this Core installation is a bit boring and only contains localhost. There's probably options for just about every language. file_get_contents isn't doing anything terribly special.
Former Nagios employee
https://www.mcapra.com/
Zeedinstein
Posts: 7
Joined: Mon Jul 11, 2016 8:50 am

Re: Connecting to Nagios API via a Website

Post by Zeedinstein »

Hi thank you so much for the help it work and I'm receiving the correct information!
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Connecting to Nagios API via a Website

Post by mcapra »

Good news then! Is it alright if we lock this thread and mark the issue as resolved?
Former Nagios employee
https://www.mcapra.com/
Locked