Hi,
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)
.
.
Thanks
Jay SenSharma
August 31st, 2010 on 10:18 am
Jay,
I had this error when i tried this code.
java.lang.AssertionError: Invalid character ‘:’ in key part of property
at ServerHealthStateMonitor.(ServerHealthStateMonitor.java:31)
I am passing these in the code.
public static void main(String[] args) throws Exception {
String hostname = “localhost”;
String portString = “7001”;
String username = “weblogic”;
String password = “weblogic”;
ServerHealthStateMonitor s = new ServerHealthStateMonitor();
initConnection(hostname, portString, username, password);
s.printNameAndState();
connector.close();
}
I am trying this on weblogic 10.3.
Thanks,
Naveen
August 31st, 2010 on 10:27 am
Hi Naveen,
Looks like something is not correct in your code…may be while copying pasting something went wrong at your end. So i m sending you my Program in E-Mail.. Please check your Mail.
.
.
Keep Posting 🙂
Thanks
Jay SenSharma
October 26th, 2010 on 1:23 am
Hi sir,
i want to find the health of the servers through prompt in solaris.
How can i find….pls help me out…
Thanks and Rgds,
Sandeep N
October 26th, 2010 on 1:28 am
I want the following arguments to check through prompt(in linux/solaris and windows) :
WebLogic Version
State
Activation Time
Availability
JVM Heap Size
Server Response Time
Web Application Details
Name
Number of Active Sessions
Maximum Number of Sessions
Total Number of Sessions
Number of Servlets
Database Connection Pools Details
Name
Connection Pool Size
Active Connections
Leaked Connections
Thread Waiting
Thread Pools Details
Name
Total Threads
Idle Threads
Threads In Use
Pending Requests Count
Pls sir i want these very urgently can u help me out ….these all should b checked through command prompt only….pls….pls…pls….
Thanks and Rgds,
Sandeep N
October 26th, 2010 on 9:37 am
Hi Sandeep,
U can use WLST to achieve monitoring of the above mentioned Attributes. SNMP Trap will be another best option to achieve this task. I would prefer using JMX Code to get all the above details.
.
.
Keep Posting 🙂
Thanks
Jay SenSharma
January 8th, 2011 on 4:47 am
Hi Sir,
This JMX code rocks!!!! Thanks a lot!
As we couldn’t make this through SNMP coz its not supported.
If we schedule a job to check the status through Cronjob, probably we can get alerts when the health_status is critical or something else.
Once again Thanks..
January 8th, 2011 on 11:14 am
Hi Shivam,
You are right…We can use the Same JMX code to run as a cronjob as well as we can setup “mailx” utility in the Operating System so that as soon as the Health state of a WebLogic Server changes it will fire an E-Mail to the Administrators.
.
.
Keep Posting 🙂
Thanks
Jay SenSharma
April 12th, 2011 on 7:26 pm
Hi
In my weblogic server, few manager server are in Overloaded, but when we run the above program it gives every manager server in health_ok state, which is wrong ,
could you help me to get the correct health state of manager server.
April 18th, 2011 on 10:41 pm
Hi Sai,
WLST script gets the data based on the MBean results…and WebLogic Console also uses the MBeans to get the health state. So if both are returning different values then Please open a Support Ticket with Oracle WebLogic Support Team.
.
.
Keep Posting 🙂
Thanks
Jay SenSharma
May 20th, 2011 on 7:52 pm
Hi Jay,
I spoke the weblogic team they are saying that
“Your code was just getting the HealthState from the ServerRuntimeMBean and using that. That’s not enough – that’s only one small aspect of server health. You also need to get the SubsystemHealthStates for a server and aggregate the results. Same for apps – you need to look at components within an app.”
Could you please write a code for subsystemHealthStates.. and post the same in with wiki..
Thanks,
Sai Kumar.
May 20th, 2011 on 8:49 pm
serverRuntimeMBean.getSubsystemHealthStates()
May 20th, 2011 on 9:28 pm
Hi Sai,
Please use the following method to get the Subsystem health State:
.
.
Keep Posting 🙂
Thanks
Jay SenSharma
September 7th, 2011 on 10:19 pm
hey Jay,
when i execute the above code, i get this error. The program compiled correctly. then when i execute it using java ServerHealthStateMonitor localhost portnumber weblogic weblogic, it gives me the below error:
Exception in Thread “Main Thread” java.lang.NoClassDefFoundError: ServerHealthStateMonitor.
Can you help me with this issue.
Thanks
Rakesh
September 8th, 2011 on 12:18 am
Hi rakeshreddy12345,
Please try to set the Classpath properly And make sure that you are adding the DOT at the end of the Classpath. the DOT syays to the JVM that “Pick the Classes from the current directory as well”
After Running the “setWLSEnv.sh” or “setWLSEnv.cmd” or “setDomainEnv.sh” or “setDomainEnv.cmd” scripts please do the following in the same command Window:
Example: For WINDOWS
set CLASSPATH=%CLASSPATH%;.;
Example: For UNIX Based OS
export CLASSPATH=$CLASSPATH:.:
.
.
Keep Posting 🙂
Thanks
Jay SenSharma
September 8th, 2011 on 3:42 pm
Hello Jay,
am fscing some problams in JRockit
My Issue is 1 machine in UNIX they have installd jrockit..i have deployed application in that managed server.i need to moniter through JRockit that domain in Unix Machine.i came to know we cont open JRockit in Unix…i am accessing domain through SSH..i want to open Jrockit in windows.what i need to do for this issue
I have added Parameters in Admin Server
SAVE_JAVA_OPTIONS=”${JAVA_OPTIONS}” -Xmanagement:ssl=false,authenticate=false,port=7099
i ahve opend JRockit in my local…I have created new connection.given hoste name(my server name),given port no 7099..after i have given right click on what ever i have created new connection.i got the error….i am not abel to connect that server
i have tried with out hoste name also there also i got the error….
any healp…plz
I did the currect or any i have missed…where i need to add these peramaters…
Regards
Reddy
October 14th, 2011 on 1:03 pm
Great code
February 23rd, 2012 on 2:34 pm
Hello Sir,
This is related to Weblogic (version 9) configuration with LR (version 9.5).
The LR 9.5 dosen’t supports This version of Weblogic, but still need to have some miore clarity on the same.
Below are the details:
Our Web logic Server IP consist of following parameters, :
We try to configure it through Web logic (SNMP) Graphs (present under Web Application Server Graphs section on Controller),
Once we add the Server name together with its Port Number ( 🙂 and Platform as UNIX, clicking on Add button in Resource Measurement section, an error is thrown (Error message: Cannot access the data from Web logic SNMP agent on host :).
We have also tried to do the same without selecting the Port Number while adding the Server name.
The same process has been repeated with the following changes also:
1) Defining the default port of our Web Logic server in the configuration file, snmp.cfg, and located in datmonitors folder.
The following were the changes that we made in snmp.cfg file:
(WebLogic
[cm_snmp_mon_weblogic]
port=
Also, as written in the Loadrunner Documentation for Monitor Reference, the following changes were also done, but the issue was still not resolved:
WebLogic
[cm_snmp_mon_isp]
port=
I receive “Could not open connection to the host, on port 23: Connection failed.” after typing ‘telnet : on command prompt.
In Addition to this, does it really matters which Weblogic version is installed on the Weblogic Server? Also, do we need to modify the any other file in Monitors folder located under Dat directory of Loadrunner.
Please let us know, in case something else that we are missing/wrong to configure the Web logic Server with our controller.
September 26th, 2013 on 10:48 am
Hi sir,
i want to set any alert as mail to my mail id when the servers are overloaded state and shutdown state. can you help me on this please.
Regards,
Siddartha
December 22nd, 2013 on 7:48 pm
Hi,
I am new one to this Weblogic. I have a task in my company to stop all the Weblogic servers in single command. So far we have 30 servers By manually only we stop and start the servers during Maintenance time. Will you please help me on this?
June 27th, 2014 on 1:15 pm
HI,
I have tried this script on my local machine, but i am getting an error as :
Error: Could not find or load main class ServerHealthStateMonitor
do i need to import any jar files for it.
Executed as:
javac ServerHealthStateMonitor.java
Note: ServerHealthStateMonitor.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
java ServerHealthStateMonitor localhost 7001 weblogic weblogic1
Regards,
Krishna
February 26th, 2016 on 5:02 pm
Hi,
I am getting the following error while running it.
java -cp . ServerHealthStateMonitor athd2a1 9022 appadmin weblogicAdmin1
Exception in thread “main” java.net.MalformedURLException: Unsupported protocol: t3
at javax.management.remote.JMXConnectorFactory.newJMXConnector(JMXConnectorFactory.java:357)
at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:267)
at ServerHealthStateMonitor.initConnection(ServerHealthStateMonitor.java:46)
at ServerHealthStateMonitor.main(ServerHealthStateMonitor.java:95)
Please check it and let me know what can be done to resolve it?
Thanks
Manisha
February 26th, 2016 on 6:06 pm
Hello Manisha,
The Error [ java.net.MalformedURLException: Unsupported protocol: t3] indicates that you have not included the weblogic client JARs in the classpath before running the “ServerHealthStateMonitor” code.
Can you please share the output of the following command here:
echo $CLASSPATH
OR
echo %CLASSPATH%
First command is for Unix based OS and the second is for Windows.
Regards
Jay SenSharma
February 26th, 2016 on 6:12 pm
Hello Manisha,
In the Unix based environment you should be using an additional ‘.’ (DOT) before the “setWLSEnv.sh” script so that the environment variables like CLASSPATH are set in the current shell.
Example:
Regards
Jay SenSharma
March 9th, 2018 on 3:13 am
Hello, I using JMX api to monitor the state of our web services but I am unable to access “state” property for the composite . Am not able to find any example or documentation. There is no method to get the attribute Value.
Set mySet = connection.queryNames(new ObjectName(“oracle.soa.config:*,j2eeType=SCAComposite*”), null);
Iterator it = mySet.iterator();
while (it.hasNext()) {
ObjectName myName = (ObjectName) it.next();
try {
System.out.println(“–> ” + myName.getCanonicalName());
// get all attributes
MBeanAttributeInfo[] atribs = connection.getMBeanInfo(myName).getAttributes();
for (int i = 0; i < atribs.length; i++) {
System.out.println(" Attribute: " + atribs[i].getName()
);
}