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.
Zeedinstein
Posts: 7
Joined: Mon Jul 11, 2016 8:50 am

Connecting to Nagios API via a Website

Post by Zeedinstein »

Hi,

I'm busying building an application that monitors a few servers and other websites via API's. I need help in getting json info from our nagios server to display the info on the web app that I am developing.
Can you help me in figuring out how to do this? I am using Jquery to get the json data via the url but I'm receiving an empty object. I think is maybe an authorization error. Where can I get the API key or a way to sign in to authorize it.

Is there any other way of doing?

Thanks
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Connecting to Nagios API via a Website

Post by mcapra »

Are you making use of the JSON Query Generator? Pretty handy for this sort of thing:
http://<your nagios server>/nagios/jsonquery.html

Since Nagios uses basic authorization, you will need to pass valid credentials when you do the jQuery request.

You can use the beforeSend function to set the headers of your request to include credentials.
http://api.jquery.com/jQuery.ajax/

Might look something like this:

Code: Select all

beforeSend: function (xhr) {
    xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
},
You should also be able to include the basic authorization credentials in the URL like so (depending on how the request is structured):

Code: Select all

http://username:password@<your nagios server>/nagios/cgi-bin/statusjson.cgi?query=myquerystring
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 »

Yes I have been using the /jsonquery.html , It's very useful thanks.

My problems is I can't seem to get the Authorization to work, I've tried putting the username and password in the url, but that's not working, I'm still getting an empty object.

I've never really used JQuery before so I don't really know how to create a basic authorization and pass the nagios credentials.
I'm looking at the link for jQuery.ajax but I'm still a bit confused.

I really appreciate the help.
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Connecting to Nagios API via a Website

Post by rkennedy »

Can you show us the output of a CURL running with -v, so we can see where it's failing to authenticate? Are there any special characters in your password? Depending what they are, it could mess up the authentication process.

I just tested on my machine, and here's how it will look for example -

Code: Select all

[root@localhost ~]# curl http://nagiosadmin:[email protected]/nagios/cgi-bin/statusjson.cgi?query=myquerystring -v

Code: Select all

* About to connect() to 192.168.4.179 port 80 (#0)
*   Trying 192.168.4.179... connected
* Connected to 192.168.4.179 (192.168.4.179) port 80 (#0)
* Server auth using Basic with user 'nagiosadmin'
> GET /nagios/cgi-bin/statusjson.cgi?query=myquerystring HTTP/1.1
> Authorization: Basic bmFnaW9zYWRtaW46UGFzc3dvcmQwMQ==
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 192.168.4.179
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Thu, 14 Jul 2016 14:30:37 GMT
< Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips PHP/5.4.16
< Cache-Control: no-store
< Pragma: no-cache
< Expires: Thu, 01 Jan 1970 00:00:00 GMT
< Last-Modified: Thu, 14 Jul 2016 14:30:37 GMT
< Transfer-Encoding: chunked
< Content-Type: application/json; charset=utf-8
<
Former Nagios Employee
Zeedinstein
Posts: 7
Joined: Mon Jul 11, 2016 8:50 am

Re: Connecting to Nagios API via a Website

Post by Zeedinstein »

Okay there was a symbol in the password conflicting with the curl command. So the curl command output is:

Code: Select all

* Hostname was NOT found in DNS cache
*   Trying 41.87.218.55...
* Connected to 41.87.218.55 (41.87.218.55) port 80 (#0)
* Server auth using Basic with user 'test_admin'
> GET /nagios/cgi-bin/statusjson.cgi?query=myquerystring HTTP/1.1
> Authorization: Basic dGVzdF9hZG1pbjpwYXNzd29yZDQzMjE=
> User-Agent: curl/7.35.0
> Host: 41.87.218.55
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 15 Jul 2016 08:29:48 GMT
* Server Apache/2.4.23 (Ubuntu) is not blacklisted
< Server: Apache/2.4.23 (Ubuntu)
< Cache-Control: no-store
< Pragma: no-cache
< Expires: Thu, 01 Jan 1970 00:00:00 GMT
< Last-Modified: Fri, 15 Jul 2016 08:29:48 GMT
< Transfer-Encoding: chunked
< Content-Type: application/json; charset=utf-8
<


Now in my application I'm still getting a 401 unauthorized.

My code:

Code: Select all

function make_base_auth(user, password) {
  var tok = user + ':' + password;
  // Base64 encoding for basic auth encoding username:password
  var hash = btoa(tok);
  // return the auth header
  return "Basic " + hash;
}

// get the data
function getData() {
  var name, code;
  var serviceURL = "http://41.87.218.55/nagios/cgi-bin/objectjson.cgi?query=hostlist&parenthost=none&childhost=none";
  // getting the auth header value
  var basicAuth = make_base_auth(user, password );
  $.support.cors = true;
  $.ajax({
    // using http get as the command type
    type: "GET",
    // accepting json as the return type
    dataType: "json",
    crossDomain: true,
    url: serviceURL,
    // setting the basic auth header
    beforeSend: function(xhr) {
      xhr.setRequestHeader("Authorization", basicAuth);
    },
    success: function(data) {
      alert(data);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
      console.log(errorThrown);
    }
  });
}
                   
                   
alert(getData());
error.png
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Connecting to Nagios API via a Website

Post by mcapra »

Try changing serviceURL to include the basic authentication credentials as well as removing the additional header settings:

Replace nagiosadmin:[email protected] with your credentials and the appropriate server:

Code: Select all

function getData() {
  var name, code;
  var serviceURL = "http://nagiosadmin:[email protected]/nagios/cgi-bin/objectjson.cgi?query=hostlist&parenthost=none&childhost=none";
  $.support.cors = true;
  $.ajax({
    type: "GET",
    dataType: "json",
    crossDomain: true,
    url: serviceURL,
    success: function(data) {
      alert(data);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
      console.log(errorThrown);
    }
  });
}
That function works for me with jQuery 3.1.0 against Nagios Core 4. You may need to manipulate the response depending on what you're ultimately doing with it, but that returns the expected object for me.
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 »

Okay so I replaced nagiosadmin:[email protected] that I put my credentials and server but now I'm still getting the 401 unauthorized.

Code: Select all

jquery-3.1.0.js:9392 
GET http://41.87.218.55/nagios/cgi-bin/objectjson.cgi?query=hostlist&parenthost=none&childhost=none 401 (Unauthorized)
send @ jquery-3.1.0.js:9392
ajax @ jquery-3.1.0.js:8999
getData @ myJavaAng.js:187
(anonymous function) @ myJavaAng.js:203
I really don't know what I'm doing wrong, I'm using jQuery 3.1.0 as you used it, Nagios Core 4.1.1.
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Connecting to Nagios API via a Website

Post by rkennedy »

I know you mentioned the CURL was having issues due to a symbol. Just to make sure - does your password still have any special characters in it, or did you change it?
Former Nagios Employee
Zeedinstein
Posts: 7
Joined: Mon Jul 11, 2016 8:50 am

Re: Connecting to Nagios API via a Website

Post by Zeedinstein »

I have changed the password to contain no symbols now, The curl command is giving back a json response as shown in the previous response. I'm not sure what's wrong.
Zeedinstein
Posts: 7
Joined: Mon Jul 11, 2016 8:50 am

Re: Connecting to Nagios API via a Website

Post by Zeedinstein »

Is there maybe something I have to do on the server-side?
Locked