Search This Blog

Wednesday, February 10, 2016

Monitor Weblogic via JMX code

Enable JMX via java options in setDomainenv.cmd/sh   located in your user_projects/domain/your_domain/bin/

Go to last and find out JAVA_OPTIONS

Append the JMX parameters to the options

set JAVA_OPTIONS=%JAVA_OPTIONS% -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8888 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

Restart the server run this java code , this will monitor memory,threads and deadlocks on your weblogic server and add it to an alert file. You can add SMTP details to get an email in case the thresh hold is reached.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.HashMap;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.openmbean.CompositeData;
import javax.management.remote.*;

/**
 *
 * @author AB839805
 */
public class JMXCron {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, MBeanException, AttributeNotFoundException, InstanceNotFoundException, ReflectionException, MalformedObjectNameException  {
   BufferedWriter writer =null;
        String host ="your server ip",threadState = "";
int port = 8888;
HashMap map = new HashMap();
boolean deadLockFound = false;
String[] credentials = new String[2];
credentials[0] = "username";
credentials[1] = "password";
map.put("jmx.remote.credentials", credentials);
JMXConnector c = JMXConnectorFactory.newJMXConnector(createConnectionURL(host, port), map);
c.connect();

ObjectName runtimeService = new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.runtime.RuntimeServiceMBean");

//ObjectName sr = (ObjectName) c.getMBeanServerConnection().getAttribute(runtimeService,"ServerRuntime");
//ThreadPoolRuntimeMBean threadPoolRuntimeMBean = runtimeService.getThreadPoolRuntime();
//ObjectName[] managedServers = (ObjectName[])c.getMBeanServerConnection().getAttribute(runtimeService, "ServerRuntimes");
 //String threadCount = (String) c.getMBeanServerConnection().getAttribute(
   //             new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME), "ThreadCount").toString();
 
 ThreadMXBean thMxB = ManagementFactory.getThreadMXBean();
 long [] runningThreads = thMxB.getAllThreadIds();


 for( int i =0;i<runningThreads.length;i++){
     threadState += thMxB.getThreadInfo(runningThreads[i]).getThreadState().toString()+"/";
    }
  System.out.println("thread state "+threadState);

 if(thMxB.findDeadlockedThreads()!=null)
 deadLockFound = true;

 System.out.println("thread count"+thMxB.getThreadCount());

//weblogic.health.HealthState tpHealthState = (weblogic.health.HealthState) connection.getAttribute(serverTP, "HealthState");

Object oM = c.getMBeanServerConnection().getAttribute(new ObjectName("java.lang:type=Memory"), "HeapMemoryUsage");
Object oT = c.getMBeanServerConnection().getAttribute(new ObjectName("java.lang:type=Threading"), "ThreadCount");


CompositeData cd = (CompositeData) oM;
Long used = (Long) cd.get("used");
 Long max = (Long) cd.get("max");
Long percentage = ((used * 100) / max);

System.out.println("Percentage of memory used  "+percentage + "%" );

if(percentage>10 ){
   
   //writer = new PrintWriter("d:\\alert.log", "UTF-8");
   writer = new BufferedWriter(new FileWriter("d:\\alert.log ", true));
writer.newLine();
writer.append("Percentage of memory used  "+percentage + "% "+(new Date())+"\n Thread Count "+runningThreads.length+" Thread State "+threadState);
writer.close();
}
if(deadLockFound ){
   
   //writer = new PrintWriter("d:\\alert.log", "UTF-8");
   writer = new BufferedWriter(new FileWriter("d:\\alert.log", true));
writer.newLine();
writer.append("Dead Lock Found at "+(new Date()));
writer.close();
}

    }
    private static JMXServiceURL createConnectionURL(String host, int port) throws MalformedURLException
{
    return new JMXServiceURL("rmi", "", 0, "/jndi/rmi://" + host + ":" + port + "/jmxrmi");
}
   
}
///////////////


It will output like 
run:
thread state TIMED_WAITING/TIMED_WAITING/TIMED_WAITING/TIMED_WAITING/RUNNABLE/RUNNABLE/WAITING/WAITING/RUNNABLE/
thread count9
Percentage of memory used  30%




In case you want to automate , run it as a cron.

Make a shell file and give chmod+x yourshell file 

contents of shell file 
//

#!/bin/bash
cd /your path where java file is located 
java JMXCron  // name of your java file 


now go to crontab -e on unix and edit the cron tab file so that you can schedule this JMX cron to run at a scheduled time.

To run this Memory/Thread/Deadlock analysis on weblogic every minute.

////////////

* * * * * //jmx.sh  /Monitoring/cron.log


It will give the log output to cron.log and will generate a cumulative memory report.







No comments:

Post a Comment