Page 1 of 2
RESTP API client-side scripts for Nagios 4.1 ?
Posted: Thu Jun 23, 2016 1:17 pm
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/
Re: RESTP API client-side for Nagios 4.1 ?
Posted: Thu Jun 23, 2016 2:25 pm
by tmcdonald
None that I am aware of, and I did some searching but could not find any harnesses around the API.
Re: RESTP API client-side for Nagios 4.1 ?
Posted: Thu Jun 23, 2016 2:51 pm
by nagmoto
Thanks for the research work. Please resolve this ticket.
Re: RESTP API client-side for Nagios 4.1 ?
Posted: Thu Jun 23, 2016 4:42 pm
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?
Re: RESTP API client-side for Nagios 4.1 ?
Posted: Thu Jul 21, 2016 6:52 am
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.
Re: RESTP API client-side for Nagios 4.1 ?
Posted: Thu Jul 21, 2016 9:32 am
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.
Re: RESTP API client-side for Nagios 4.1 ?
Posted: Tue Jul 26, 2016 10:36 am
by nagmoto
Thanks for providing simple example for 3 languages. please leave this thread opened. I plan to provide php and golang examples.
Re: RESTP API client-side scripts for Nagios 4.1 ?
Posted: Wed Jul 27, 2016 9:02 am
by mcapra
Great! Will leave this thread open.
Re: RESTP API client-side scripts for Nagios 4.1 ?
Posted: Wed Jul 27, 2016 4:25 pm
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)
Re: RESTP API client-side scripts for Nagios 4.1 ?
Posted: Wed Jul 27, 2016 4:36 pm
by mcapra
Thanks for sharing! That looks like Python though
