-
Notifications
You must be signed in to change notification settings - Fork 7
Embedding jcheck_nrpe to query NRPE or JNRPE
The current tutorial focuses on embedding jcheck_nrpe inside your own application to be able to query an NRPE or JNRPE server. We are going to implement an application that will query the plugin running inside the server created in the tutorial Creating Nagios Plugins with Java.
To follow this tutorial is required to have both eclipse and the eclipse maven plugin installed and you should have completed the Creating Nagios Plugins with Java and have the JNRPE server up and running.
Open eclipse and create a new maven project :
- From the Java perspective, inside the project explorer panel, right click -> new project
- Search the maven folder and choose the 'Maven Project' item
- Click next
TODO image1
-
Now be sure to select the 'Create simple project (skip archetype selection) checkbox and click next.
-
Now you'll see a window asking for some information. Fill the data as follows:
Group Id : eu.ziccardi.jnrpe Artifact Id: jnrpe-client-tutorial
-
Ignore the rest and press 'Finish'.
TODO Maven project detail
You should end up with something like to image below.
The project explorer
We have created our maven project. In the next part of this article we will start the real coding
Writing the code
Before we can start writing the real code, there is still one step to follow: we have to tell maven that our project requires the jcheck_nrpe library.
To instruct maven, simply edit the pom.xml file you find inside your project and add the following section:
<dependencies>
<dependency>
<groupId>net.sf.jnrpe</groupId>
<artifactId>jcheck_nrpe</artifactId>
<version>2.0.3-RC5</version>
</dependency>
</dependencies>
Your pom.xml should now look like follows:
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>eu.ziccardi.jnrpe</groupId>
<artifactId>jnrpe-client-tutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>net.sf.jnrpe</groupId>
<artifactId>jcheck_nrpe</artifactId>
<version>2.0.3-RC5</version>
</dependency>
</dependencies>
</project>
Now Maven knows about jnrpe-lib. If you should ever see some error inside the project, try:
- Right-click on the project -> Maven -> update project
-
Right click on the project name -> new -> class
-
Eclipse will popup asking you the details of your new class. Fill the fields with the following values:
Package : eu.ziccardi.jnrpe.sample Name : Main Check the 'public static void main(String[] args)' option.
It should end producing the following code:
package eu.ziccardi.jnrpe.sample;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Ok, now we can start writing the code. The object we are going to use is the JNRPEClient class. Since our JNRPE server listens on the port 5666 at the address 127.0.0.1, we will write the following code :
JNRPEClient client = new JNRPEClient("127.0.0.1", 5666, false);
the last parameter, instructs JNRPEClient to access to the server with a plain socket (non SSL).
To query the JNRPE/NRPE server, we will use the following method of the JNRPEClient object:
public final ReturnValue sendCommand(final String sCommandName,
final String... arguments) throws JNRPEClientException
The first parameter is the name of the command we want to invoke. The second parameter is the list of arguments we want to pass to the command. The object ReturnValue contains the response received from JNRPE/NRPE server
An example invocation could be:
ReturnValue ret = client.sendCommand("check_disk", "/tmp", ":100", ":10");
The ret variable will contain both the return code of the check and its message string. In our case, we will simply print out both data:
System.out.println ("CHECK_DISK Last Status Code : " + ret.getStatus());
System.out.println ("CHECK_DISK Last Message : " + ret.getMessage());
You will end up with the following code:
package eu.ziccardi.jnrpe.sample;
import it.jnrpe.ReturnValue;
import it.jnrpe.client.JNRPEClient;
import it.jnrpe.client.JNRPEClientException;
public class Main {
public static void main(String[] args) {
JNRPEClient client = new JNRPEClient("127.0.0.1", 5666, false);
try {
ReturnValue ret = client.sendCommand("check_disk", "/tmp", ":100", ":10");
System.out.println ("CHECK_DISK Last Status Code : " + ret.getStatus())
System.out.println ("CHECK_DISK Last Message : " + ret.getMessage());
} catch (JNRPEClientException e) {
e.printStackTrace();
}
}
}
Launching the application, you will get an output similar to the following:
CHECK_DISK Last Status Code : OK
CHECK_DISK Last Message : CHECK_DISK : OK - Free space : 5859 MB|freespace=5859.324219;:100;:10;0.000000;5859.570312
Congratulation! You just invoked a command on JNRPE from your Java class!