Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Monday, May 6, 2013

Monitoring Windows Resources through Java

This article describes about how to monitor CPU load, Memory free space and HDD free space in Windows using Java. We use com.sun.management.OperatingSystemMXBean class for monitor CPU load and memory usage. You have to have JDK 1.7 to use this class.

Monitoring CPU load


1.    OperatingSystemMXBean bean = (OperatingSystemMXBean)java.lang.management. ManagementFactory.getOperatingSystemMXBean();
2.    double dCpu_usage = (double)(bean.getSystemCpuLoad()*100);

Description

Line 1: Create an OperatingSystemMXBean object.
Line 2: getSystemCpuLoad() method will return the CPU load as a decimal value between 0 and 1 for the consumer. So you have to multiply it from 100 to display as a percentage.


Monitor Free memory space

1.   You have to create an OperatingSystemMXBean object same as in CPU load monitoring.
2.   Int lFree_mem = bean.getFreePhysicalMemorySize()/(1024*1024);

Description

Line 2: bean.getFreePhysicalMemorySize() will return free memory space in bytes. So you have to divide it 1024*1024 to convert into MB.


Monitor Free HDD space

1.   File drive = new File(“c:\\");
2.   int free_space = drive.getFreeSpace()/(1024*1024*1024);

Description

Line 1: Create a simple File (java.io.File) object and give your HDD letter as path of the file.
Line 2:  getFreeSpace() method will return free space of your HDD in bytes. To convert it into GB you have to divide it from 1024*1024*1024

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.