Hi,

Jay SenSharma

Jay SenSharma

Here we are going to see the demonstration of a Server Healt State Monitoring with the help of JMX. Many times we need to not only monitor the State of the Server …rather we want to know what is the Health Status of the Server. WebLogic APIs contain a class ” weblogic.health.HealthState” which provides some constants variables to represent the Actual HealthState of a Server.At anytime the WebLogic Server may be in any of the following state:

.
public static final int HEALTH_OK;
public static final int HEALTH_WARN;
public static final int HEALTH_CRITICAL;
public static final int HEALTH_FAILED;
public static final int HEALTH_OVERLOADED;

Step1). Create a Directory somewhere in your file system like: “C:ServerHealthCheckDemo”

Step2). Now write the following JMX Code to monitor the HealthState of all the Servers Which are Part of our WebLogic Domain.
“ServerHealthStateMonitor.java”

import weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean;
import weblogic.management.runtime.ServerRuntimeMBean;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
import weblogic.management.jmx.MBeanServerInvocationHandler;
import java.util.Hashtable;
import java.io.IOException;
import java.net.MalformedURLException;
import weblogic.management.runtime.JDBCDataSourceRuntimeMBean;
import javax.management.*;
import javax.naming.*;

public class ServerHealthStateMonitor {
private static MBeanServerConnection connection;
private static JMXConnector connector;
private static final ObjectName service;
private static String combea = "com.bea:Name=";
private static String service1 = "DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean";
private static String service2 = "RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean";

static {
try {
service =
new ObjectName(combea + service1);
} catch (MalformedObjectNameException e) {
throw new AssertionError(e.getMessage());
}
}

public static void initConnection (String hostname, String portString, String username, String password) throws IOException,MalformedURLException {
String protocol = "t3";
Integer portInteger = Integer.valueOf(portString);
int port = portInteger.intValue();
String jndiroot = "/jndi/";
String mserver = "weblogic.management.mbeanservers.domainruntime";
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, jndiroot + mserver);
Hashtable h = new Hashtable();
h.put(Context.SECURITY_PRINCIPAL, username);
h.put(Context.SECURITY_CREDENTIALS, password);
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,"weblogic.management.remote");
connector = JMXConnectorFactory.connect(serviceURL, h);
connection = connector.getMBeanServerConnection();
}

public static ObjectName[] getServerRuntimes() throws Exception {
return (ObjectName[]) connection.getAttribute(service,"ServerRuntimes");
}

public void printNameAndState() throws Exception
{
ObjectName arr[]=getServerRuntimes();
for(ObjectName temp : arr)
System.out.println("nt servers: "+temp);
ObjectName domain = (ObjectName) connection.getAttribute(service,"DomainConfiguration");
System.out.println("Domain: " + domain.toString());
ObjectName[] servers = (ObjectName[]) connection.getAttribute(domain,"Servers");
for (ObjectName server : servers)
{
String aName = (String) connection.getAttribute(server,"Name");
try{
ObjectName ser= new ObjectName("com.bea:Name="+aName+",Location="+aName+",Type=ServerRuntime");
String serverState=(String) connection.getAttribute(ser,"State");
System.out.println("nt Server: "+aName+"t State: "+serverState);
weblogic.health.HealthState serverHealthState=( weblogic.health.HealthState) connection.getAttribute(ser,"HealthState");
int hState=serverHealthState.getState();
if(hState==weblogic.health.HealthState.HEALTH_OK)
System.out.println("t Server: "+aName+"t State Health: HEALTH_OK");
if(hState==weblogic.health.HealthState.HEALTH_WARN)
System.out.println("t Server: "+aName+"t State Health: HEALTH_WARN");
if(hState==weblogic.health.HealthState.HEALTH_CRITICAL)
System.out.println("t Server: "+aName+"t State Health: HEALTH_CRITICAL");
if(hState==weblogic.health.HealthState.HEALTH_FAILED)
System.out.println("t Server: "+aName+"t State Health: HEALTH_FAILED");
if(hState==weblogic.health.HealthState. HEALTH_OVERLOADED)
System.out.println("t Server: "+aName+"t State Health: HEALTH_OVERLOADED");
}
catch(javax.management.InstanceNotFoundException e)
{
System.out.println("nt Server: "+aName+"t State: SHUTDOWN (or Not Reachable)");
}
}
}

public static void main(String[] args) throws Exception {
String hostname = args[0];
String portString = args[1];
String username = args[2];
String password = args[3];
ServerHealthStateMonitor s = new ServerHealthStateMonitor();
initConnection(hostname, portString, username, password);
s.printNameAndState();
connector.close();
}
}

Step3). Now open a command prompt and then run the “setWLSEnv.cmd” to set the Environment variables like “CLASSPATH” and “PATH” then compile and run the program like following:

C:ServerHealthCheckDemo>javac ServerHealthStateMonitor.java
Note: ServerHealthStateMonitor.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

C:ServerHealthCheckDemo>java ServerHealthStateMonitor localhost 7001 weblogic weblogic

servers: com.bea:Name=AdminServer,Location=AdminServer,Type=ServerRuntime
Domain: com.bea:Name=7001_EJB_Domain,Location=7001_EJB_Domain,Type=Domain

Server: AdminServer State: RUNNING
Server: AdminServer State Health: HEALTH_OK

Server: MS1 State: SHUTDOWN (or Not Reachable)

Server: MS2 State: SHUTDOWN (or Not Reachable)

Server_HealthState_JMX

Server_HealthState_JMX

.
.
Thanks
Jay SenSharma

If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.