Below is the java code to retrieve server health data using JMX api's
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.raylabs.jmx; | |
import java.io.IOException; | |
import java.text.DecimalFormat; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import javax.management.MBeanServerConnection; | |
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.health.HealthState; | |
public class Class1 { | |
public Class1() { | |
super(); | |
} | |
public static void main(String[] args) { | |
String environment = "dev"; | |
JMXConnector connector = null; | |
String userName = "xxx"; | |
String password = "xxxxxx"; | |
String adminBPMHostname = | |
environment + "-admin.com"; | |
String adminBPMPort = "7001"; | |
String node1BPMHostname = | |
environment + "-node1.com"; | |
String node1BPMPort = "7002"; | |
String node2BPMHostname = | |
environment + "-node1.com"; | |
String node2BPMPort = "7002"; | |
String adminSOAHostname = | |
environment + "-admins.com"; | |
String adminSOAPort = "7001"; | |
String node1SOAHostname = | |
environment + "-node1s.com"; | |
String node1SOAPort = "7002"; | |
String node2SOAHostname = | |
environment + "-node2s.com"; | |
String node2SOAPort = "7002"; | |
List server = new ArrayList(); | |
server.add(adminBPMHostname + ":" + adminBPMPort); | |
server.add(node1BPMHostname + ":" + node1BPMPort); | |
server.add(node2BPMHostname + ":" + node2BPMPort); | |
server.add(adminSOAHostname + ":" + adminSOAPort); | |
server.add(node1SOAHostname + ":" + node1SOAPort); | |
server.add(node2SOAHostname + ":" + node2SOAPort); | |
for (int i = 0; i < server.size(); i++) { | |
try { | |
ObjectName SERVICE = | |
new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean"); | |
Map<String, String> map = new HashMap<String, String>(); | |
map.put(Context.SECURITY_PRINCIPAL, userName); | |
map.put(Context.SECURITY_CREDENTIALS, password); | |
map.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, | |
"weblogic.management.remote"); | |
map.put("jmx.remote.x.request.waiting.timeout", "20000"); | |
JMXServiceURL url = | |
new JMXServiceURL("service:jmx:t3://" + (String)server.get(i) + | |
"/jndi/weblogic.management.mbeanservers.runtime"); | |
connector = JMXConnectorFactory.connect(url, map); | |
MBeanServerConnection connection = | |
connector.getMBeanServerConnection(); | |
ObjectName serverRuntimeMBean = | |
(ObjectName)connection.getAttribute(SERVICE, | |
"ServerRuntime"); | |
System.out.println("Environment :" + environment); | |
String serverName = | |
(String)connection.getAttribute(serverRuntimeMBean, | |
"Name"); | |
System.out.println("Server name :" + serverName); | |
String state = | |
(String)connection.getAttribute(serverRuntimeMBean, | |
"State"); | |
System.out.println("Server State : " + state); | |
String listenAddress = | |
(String)connection.getAttribute(serverRuntimeMBean, | |
"ListenAddress"); | |
System.out.println("Listen Address : " + listenAddress); | |
Integer listenPort = | |
(Integer)connection.getAttribute(serverRuntimeMBean, | |
"ListenPort"); | |
System.out.println("Listen Port : " + listenPort); | |
HealthState health = | |
(HealthState)connection.getAttribute(serverRuntimeMBean, | |
"HealthState"); | |
int serverState = health.getState(); | |
if (serverState == HealthState.HEALTH_CRITICAL) { | |
System.out.println("Health : HEALTH_CRITICAL"); | |
} else if (serverState == HealthState.HEALTH_FAILED) { | |
System.out.println("Health : HEALTH_FAILED"); | |
} else if (serverState == HealthState.HEALTH_OK) { | |
System.out.println("Health : HEALTH_OK"); | |
} else if (serverState == HealthState.HEALTH_OVERLOADED) { | |
System.out.println("Health : HEALTH_OK"); | |
} else if (serverState == HealthState.HEALTH_WARN) { | |
System.out.println("Health : HEALTH_WARN"); | |
} else { | |
System.out.println("Health : UNDEFINED"); | |
} | |
// String dir= (String)connection.getAttribute(serverRuntimeMBean, "CurrentDirectory"); | |
// System.out.println("Current directory :"+dir); | |
ObjectName jvm = | |
(ObjectName)connection.getAttribute(serverRuntimeMBean, | |
"JVMRuntime"); | |
Long totalHeap = | |
(Long)connection.getAttribute(jvm, "TotalHeap"); | |
System.out.println("TotalHeap free:" + | |
bytesToString(totalHeap)); | |
Long freeHeap = (Long)connection.getAttribute(jvm, "FreeHeap"); | |
System.out.println("Free Heap:" + bytesToString(freeHeap)); | |
Long usedHeap = (Long)connection.getAttribute(jvm, "UsedHeap"); | |
System.out.println("Used Heap:" + bytesToString(usedHeap)); | |
Long totalPhysicalMemory = | |
(Long)connection.getAttribute(jvm, "TotalPhysicalMemory"); | |
System.out.println("Total Physical Memory:" + | |
bytesToString(totalPhysicalMemory)); | |
Long usedPhysicalMemory = | |
(Long)connection.getAttribute(jvm, "UsedPhysicalMemory"); | |
System.out.println("Used Physical Memory:" + | |
bytesToString(usedPhysicalMemory)); | |
Integer totalThreads = | |
(Integer)connection.getAttribute(jvm, "TotalNumberOfThreads"); | |
System.out.println("Total threads:" + totalThreads); | |
Integer daemonThreads = | |
(Integer)connection.getAttribute(jvm, "NumberOfDaemonThreads"); | |
System.out.println("Daemon threads:" + daemonThreads); | |
Double processorLoad = | |
(Double)connection.getAttribute(jvm, "AllProcessorsAverageLoad"); | |
System.out.println("Processor Load:" + processorLoad * 100 + | |
"%"); | |
Double jvmLoad = | |
(Double)connection.getAttribute(jvm, "JvmProcessorLoad"); | |
System.out.println("JVM Load:" + jvmLoad * 100 + "%"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
connector.close(); | |
System.out.println("Connection closed"); | |
System.out.println("---------------------------------------------------------------"); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
public static String bytesToString(long size) { | |
long Kb = 1 * 1024; | |
long Mb = Kb * 1024; | |
long Gb = Mb * 1024; | |
long Tb = Gb * 1024; | |
long Pb = Tb * 1024; | |
long Eb = Pb * 1024; | |
if (size < Kb) | |
return floatForm(size) + " byte"; | |
if (size >= Kb && size < Mb) | |
return floatForm((double)size / Kb) + " Kb"; | |
if (size >= Mb && size < Gb) | |
return floatForm((double)size / Mb) + " Mb"; | |
if (size >= Gb && size < Tb) | |
return floatForm((double)size / Gb) + " Gb"; | |
if (size >= Tb && size < Pb) | |
return floatForm((double)size / Tb) + " Tb"; | |
if (size >= Pb && size < Eb) | |
return floatForm((double)size / Pb) + " Pb"; | |
if (size >= Eb) | |
return floatForm((double)size / Eb) + " Eb"; | |
return "???"; | |
} | |
public static String floatForm(double d) { | |
return new DecimalFormat("#.##").format(d); | |
} | |
} |
Libraries
No comments:
Post a Comment