Wednesday, May 20, 2009

Windows Process Monitor in java

It is required to monitor the status of few critical processes and services on business critical systems. Similarly other resources such as memory used and CPU time also require to be monitored.

These simple set of functions make use of the windows tasklist.exe utility to perform following functions:

  • Alert when a particular process, service, module is started.
  • Alert when memory usage of any particular or any process exceeds a certain value.
  • Alert if any particular or any process is not responding.
  • Alert if CPU Time for a particular process exceeds a certain value.
  • Monitor if a particular number of instances of a service, process, and module is running. If it falls below or goes above the mentioned number of instances- generate alert.
The java functions to use are given below.

import java.io.*;

import java.util.*;


public class GetProcess {

public static void main(String[] args){

//few example uses of the functions.

System.out.println(CheckIfProgramXIsNotResponding(""));

System.out.println(CheckIfProgramXIsUsingMoreThanYKBOfMemory("java.exe",4200));

System.out.println(CheckIfProgramXIsUsingMoreThanZOfCPUTime("System", 0,0,1));

System.out.println(CheckIfNInstancesOfProgramXAreExecuting("svchost.exe",5));

System.out.println(CheckIfNInstancesOfServiceXAreRunning("MDM",1));

System.out.println(CheckIfNInstancesOfModuleXAreRunning("ntdll.dll",29));

}

/*These functions can be used along with a configuration file or a front end to perform monitoring of Windows systems.*/

private static boolean CheckIfProgramXIsNotResponding(String ProgName){

//returns true if given program is NOT RESPONDING. If no argument is passed, returns true if any program is not responding.

String argument="tasklist.exe /NH /FI ".concat("\"").concat("STATUS eq NOT RESPONDING").concat("\"");

return checkProcessInfo(argument, ProgName);

}


private static boolean CheckIfProgramXIsUsingMoreThanYKBOfMemory(String ProgName,int MemoryInKb){

//returns true if given program is using more than Y KB of Memory.If no argument is passed, returns true if any programs memory is more than Y KB.

String line,argument="tasklist.exe /NH /FI ".concat("\"").concat("MEMUSAGE gt ")+MemoryInKb;

argument=argument.concat("\"");

return checkProcessInfo(argument, ProgName);

}


private static boolean CheckIfProgramXIsUsingMoreThanZOfCPUTime(String ProgName,int Hours,int Minutes,int Seconds){

//returns true if given program is using more than Z CPUTime.If no argument is passed, returns true if any programs memory is using more than Z CPUTime.

String line,argument="tasklist.exe /NH /FI ".concat("\"").concat("CPUTIME gt ")+Hours+":"+Minutes+":"+Seconds;

argument=argument.concat("\"");

return checkProcessInfo(argument, ProgName);

}


private static boolean CheckIfNInstancesOfProgramXAreExecuting(String ProgName,int n){

//returns true if N instances of ProgramX are executing.

String line,argument="tasklist.exe /NH /FI ".concat("\"").concat("IMAGENAME eq ").concat(ProgName).concat("\"");

return checkProcessCount(argument,ProgName,n);

}


private static boolean CheckIfNInstancesOfServiceXAreRunning(String ProgName,int n){

//returns true if N instances of Service X are running.

String line,argument="tasklist.exe /NH /FI ".concat("\"").concat("SERVICES eq ").concat(ProgName).concat("\"");

return checkProcessCount(argument,"",n);

}


private static boolean CheckIfNInstancesOfModuleXAreRunning(String ProgName,int n){

//returns true if N instances of Module X are running.

String line,argument="tasklist.exe /NH /M ".concat(ProgName);

return checkProcessCount(argument,"",n);

}


private static boolean checkProcessInfo(String argument,String ProgName){

String line;

try {

System.out.println(argument);

Process p = Runtime.getRuntime().exec(argument);

BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

while ((line = input.readLine()) != null) {

if (!line.trim().equals("")) {

if(line.startsWith(ProgName)){

return true;}//return status

}

}

input.close();

}

catch (Exception err) {

err.printStackTrace();

}

return false;

}


private static boolean checkProcessCount(String argument,String ProgName,int n){

String line;

int count=0;

try {

System.out.println(argument);

Process p = Runtime.getRuntime().exec(argument);

BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

while ((line = input.readLine()) != null) {

if (!line.trim().equals("")) {

if(line.startsWith(ProgName)){

count++;}

}

}

input.close();

}

catch (Exception err) {

err.printStackTrace();

}

System.out.println(count);

if(n==count){

return true;}//return status

return false;

}

}

1 comment:

spud tooley said...

hey man,

thanks for this. stepping out of my manager's hat for awhile to do the stuff i like the best - i haven't programmed in java in - what - seven years? geesh. anyway, this is a first step in what i'm doing ... helped me remember some things, too.

i appreciate this.

mike r.
atlanta, ga