RESTP API client-side scripts for Nagios 4.1 ?

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.
nagmoto
Posts: 195
Joined: Fri Jan 09, 2015 8:05 am

RESTP API client-side scripts for Nagios 4.1 ?

Post by nagmoto »

Hi,

I followed R1 doc and able to query the server status with following this URL https://nagios01.test.com/nagios/jsonquery.html

Do you know if there are query scripts written in java,Python or Perl ?
I am hoping to provide some simple example scripts for my users to jumpstart with.

R1: https://labs.nagios.com/2014/06/19/expl ... -7-part-1/
Last edited by nagmoto on Wed Jul 27, 2016 7:55 am, edited 1 time in total.
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: RESTP API client-side for Nagios 4.1 ?

Post by tmcdonald »

None that I am aware of, and I did some searching but could not find any harnesses around the API.
Former Nagios employee
nagmoto
Posts: 195
Joined: Fri Jan 09, 2015 8:05 am

Re: RESTP API client-side for Nagios 4.1 ?

Post by nagmoto »

Thanks for the research work. Please resolve this ticket.
Last edited by nagmoto on Thu Jul 21, 2016 6:49 am, edited 1 time in total.
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: RESTP API client-side for Nagios 4.1 ?

Post by tmcdonald »

Sorry, a bit confused by your word choice. Are you asking for more information or thanking me and letting me know the thread can be closed?
Former Nagios employee
nagmoto
Posts: 195
Joined: Fri Jan 09, 2015 8:05 am

Re: RESTP API client-side for Nagios 4.1 ?

Post by nagmoto »

Please resolve this ticket, I was hoping to see if others had developed some client-side scripts in different languages to interact with new JSON API server.
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: RESTP API client-side for Nagios 4.1 ?

Post by mcapra »

Here's a Java PoC (requires org.apache.commons.codec):

Code: Select all

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.codec.binary.Base64;

public class JSONQuery {

	public static void main(String[] args) {

		try {
			String webPage = "http://192.168.4.246/nagios/cgi-bin/statusjson.cgi?query=host&hostname=localhost";
			String name = "nagiosadmin";
			String password = "welcome";

			String authString = name + ":" + password;
			byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
			String authStringEnc = new String(authEncBytes);
			URL url = new URL(webPage);
			URLConnection urlConnection = url.openConnection();
			urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
			InputStream is = urlConnection.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);

			int numCharsRead;
			char[] charArray = new char[1024];
			StringBuffer sb = new StringBuffer();
			while ((numCharsRead = isr.read(charArray)) > 0) {
				sb.append(charArray, 0, numCharsRead);
			}
			String result = sb.toString();

			System.out.println("*** BEGIN ***");
			System.out.println(result);
			System.out.println("*** END ***");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
And a PHP PoC:

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);
?>
And a Perl PoC (note that the @ is escaped):

Code: Select all

#!/usr/bin/perl

use LWP::Simple;                # From CPAN
use JSON qw( decode_json );     # From CPAN
use Data::Dumper;               # Perl core module
use strict;                     # Good practice
use warnings;                   # Good practice

my $trendsurl = "http://nagiosadmin:welcome\@192.168.3.87/nagios/cgi-bin/objectjson.cgi?query=hostlist&parenthost=none&childhost=none";

my $json = get( $trendsurl );
die "Could not get $trendsurl!" unless defined $json;

my $decoded_json = decode_json( $json );

print Dumper $decoded_json;
Replace the IP and basic auth credentials. As @tmcdonald pointed out, I don't think there are any harnesses written for a particular language. However, JSON objects are generic enough that pretty much every language has a library for interpreting them once you have them.
Former Nagios employee
https://www.mcapra.com/
nagmoto
Posts: 195
Joined: Fri Jan 09, 2015 8:05 am

Re: RESTP API client-side for Nagios 4.1 ?

Post by nagmoto »

Thanks for providing simple example for 3 languages. please leave this thread opened. I plan to provide php and golang examples.
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: RESTP API client-side scripts for Nagios 4.1 ?

Post by mcapra »

Great! Will leave this thread open.
Former Nagios employee
https://www.mcapra.com/
nagmoto
Posts: 195
Joined: Fri Jan 09, 2015 8:05 am

Re: RESTP API client-side scripts for Nagios 4.1 ?

Post by nagmoto »

Here is pyhon example code.

Code: Select all


#!/usr/bin/python
#
# ---------------------------- IMPORT  DECLARATION ---------------------------
#
import os,sys

# ---------------------------- CONSTANT DECLARATION ---------------------------
nagiossite  = "nagios.test.com"
user        = "myid"
pwd         = 'mypassword'

#
# ---------------------------- FUNCTION  DECLARATION ---------------------------
#


def nagios4_query():
    import requests
    import json
    '''INPUT:  nothing for now
    OUTPUT:  return json content from query
    REF: http://stackoverflow.com/questions/28667684/python-requests-getting-sslerror
    '''

    base_url    = "https://"+ nagiossite +"/nagios/cgi-bin/objectjson.cgi?query=servicelist&parenthost=none&childhost=none&details=true"
    snurl_sysid = base_url 
    headers = {"Accept":"application/json"}
    # verify = true by default, require server certificate .pem file
    r = requests.get(snurl_sysid, auth=(user, pwd), headers=headers, verify=False )
    if r.status_code != 200: 
        mystatus = 'Status:' +  str(r.status_code) +  'Headers:' + \
                   str(r.headers) +  'Error Response:' + str(r.json())
        return  1 # error
    mystatus = 'Status:' +  str(r.status_code) +  'Headers:' + \
               str(r.headers) +  'Error Response:' + str(r.json())
    print mystatus
#
# **************************** MAIN SCRIPT ************************************
#
if __name__ == '__main__':
  nagios4_query()
exit(0)
Last edited by nagmoto on Thu Jul 28, 2016 4:50 pm, edited 3 times in total.
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: RESTP API client-side scripts for Nagios 4.1 ?

Post by mcapra »

Thanks for sharing! That looks like Python though ;)
Former Nagios employee
https://www.mcapra.com/
Locked