Monday, May 6, 2013

Executing a Jar (Java Plugin) using Shell Script with Nagios 3.5


Execute a Java Nagios plug-in.


Nagios is unable to recognize the exit statements of compiled jar plug-in.  So a java plug-ins can’t be directly executed in Nagios. Shell scripts can be used to execute a Java plug-ins in Nagios. These are the steps you should follow to do that.

Step 1: Write the Nagios plug-in using Java.


  • Nagios requires an information statement and exit value.
  • But you don’t need to use exit values such as System.exit(2) in java plugin
  • Java plug-in should output those two statements as ordinary STD outputs instead of exit values.
  1.  Exit value: System.out.println(2)
  2.  Information statement: System.out.println(“CPU load is 20%”)

Step 2: Convert your java plug-in in to jar.


Step 3: Write a shell script as follows.
  1. output=`java -jar /usr/local/nagios/libexec/your_app.jar $1 $2 $3 $4 $5 $6
  2. set  `echo $output`
  3. echo $output | cut -c 3-100
  4. exit $1


Description.

Line 1: Execute the jar and take the output to a variable called ‘output’.  $1 $2 $3 $4 $5 $6 are command line arguments which should be provided to the java plug-in. When the plug-in writes those two lines (Exit value and information statement) to STD out, the variable (output) is assigned a value like ‘2 CPU load is 20%’. ‘2’ is the exit value. ‘CPU load is 20%’ is the information statement.

Line 2: Set first character to $1 variable. The first character is exit value.

Line 3: echo information statement to STD out. ‘$output | cut -c 3-100’ means we take from 3rd character to 100th character from ‘output’ variable.

Line 4: Exit from shell script using exit statement.


Step 4: Place jar and shell in /usr/local/nagios/libexec folder.

Step 5: Configure Nagios to take the shell script as a plug-in.

                

No comments:

Post a Comment