WLS9通过JMX获取服务器信息

106 篇文章 0 订阅

 

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable;
import java.rmi.Remote
import javax.management.MBeanServer
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;

public class EnvDetail {

     public static String username = "system"; 
     public static String password = "password";
     public static int chkValue;
     public static String hostname = null;
     public static String port = null; 
     
     private static MBeanServerConnection connection;
     private static JMXConnector connector;
     private static final ObjectName service; // Initializing the object name for DomainRuntimeServiceMBean
     // so it can be used throughout the class. 
     static {   
          try {
               service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean"); 
               }catch (MalformedObjectNameException e)
               { 
                    throw new AssertionError(e.getMessage());
                    } 
               } 
     /* * Initialize connection to the Domain Runtime MBean Server */ 
     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(); 
          } /* * Print an array of ServerRuntimeMBeans. * This MBean is the root of the runtime MBean hierarchy, and * each server in the domain hosts its own instance. */
     public static ObjectName[] getServerRuntimes() throws Exception {  
          return (ObjectName[]) connection.getAttribute(service, "ServerRuntimes");
          
          } 
     
     public void getJvmRuntime() throws Exception {
          ObjectName[] serverRT = getServerRuntimes();
          int length = (int) serverRT.length;
          for (int i = 0; i < length; i++) {
          String name = (String) connection.getAttribute(serverRT,"Name");
          ObjectName jvmRT = (ObjectName) connection.getAttribute(serverRT[i],"JVMRuntime");
          System.out.println("\n..<"+name+" : .JVMRuntime>");
          Long heapSMX =(Long)connection.getAttribute(jvmRT, "HeapSizeMax");
          System.out.println("HeapSizeMax :" Math.round((heapSMX.longValue()/1048576*100000)/100000) " MB ("+connection.getAttribute(jvmRT, "HeapSizeMax")+")" );
          Long heapSC =(Long)connection.getAttribute(jvmRT, "HeapSizeCurrent");
          System.out.println("HeapSizeCurrent :" Math.round((heapSC.longValue()/1048576*100000)/100000) " MB ("+connection.getAttribute(jvmRT, "HeapSizeCurrent")+" bytes)");
          Long heapFC =(Long)connection.getAttribute(jvmRT, "HeapFreeCurrent");
          System.out.println("HeapFreeCurrent :" Math.round((heapFC.longValue()/1048576*100000)/100000) " MB ("+connection.getAttribute(jvmRT, "HeapFreeCurrent")+" bytes)");
          System.out.println("HeapFreePercent :" + connection.getAttribute(jvmRT, "HeapFreePercent")+"%");
          System.out.println("JavaVendor :" + connection.getAttribute(jvmRT, "JavaVendor"));
          System.out.println("JavaVersion :" + connection.getAttribute(jvmRT, "JavaVersion"));
          Long uptime =(Long)connection.getAttribute(jvmRT, "Uptime");     
          System.out.println("Uptime :" + connection.getAttribute(jvmRT, "Uptime")+" milliseconds (" + (uptime*.001)+" Seconds)");
          System.out.println("******************\n");
          }
          
     }
     public void getJdbcRuntime() throws Exception {
                    ObjectName[] serverRT = getServerRuntimes();
          int length = (int) serverRT.length;
          for (int i = 0; i < length; i++) {
          String name = (String) connection.getAttribute(serverRT[i],"Name");
          ObjectName[] appRT =
          (ObjectName[]) connection.getAttribute(new ObjectName("com.bea:Name="+name+",ServerRuntime="+name+",Location="+name+",Type=JDBCServiceRuntime"),"JDBCDataSourceRuntimeMBeans");
          int appLength = (int) appRT.length;
          for (int x = 0; x < appLength; x++) {
          System.out.println("\n.. .<"+name+" : JDBCDataSourceRuntimeMBeans>" + (String)connection.getAttribute(appRT[x], "Name")+"");
          System.out.println("ActiveConnectionsCurrentCount : " + connection.getAttribute(appRT[x], "ActiveConnectionsCurrentCount"));
          System.out.println("ActiveConnectionsAverageCount : " + connection.getAttribute(appRT[x], "ActiveConnectionsAverageCount"));
          System.out.println("ActiveConnectionsAverageCount : " + connection.getAttribute(appRT[x], "ActiveConnectionsAverageCount"));
          System.out.println("ConnectionsTotalCount : " + connection.getAttribute(appRT[x], "ConnectionsTotalCount"));
          System.out.println("CurrCapacity : " + connection.getAttribute(appRT[x], "CurrCapacity"));
          System.out.println("CurrCapacityHighCount : " + connection.getAttribute(appRT[x], "CurrCapacityHighCount"));
          System.out.println("HighestNumAvailable : " + connection.getAttribute(appRT[x], "HighestNumAvailable"));
          System.out.println("HighestNumAvailable : " + connection.getAttribute(appRT[x], "HighestNumAvailable"));
          System.out.println("LeakedConnectionCount : " + connection.getAttribute(appRT[x], "LeakedConnectionCount"));
          System.out.println("WaitSecondsHighCount : " + connection.getAttribute(appRT[x], "WaitSecondsHighCount"));
          System.out.println("WaitingForConnectionCurrentCount: " + connection.getAttribute(appRT[x], "WaitingForConnectionCurrentCount"));
          System.out.println("WaitingForConnectionFailureTotal: " + connection.getAttribute(appRT[x], "WaitingForConnectionFailureTotal"));
          System.out.println("WaitingForConnectionTotal : " + connection.getAttribute(appRT[x], "WaitingForConnectionTotal"));
          System.out.println("WaitingForConnectionHighCount : " + connection.getAttribute(appRT[x], "WaitingForConnectionHighCount"));
          System.out.println(".\n");
          }
          }
     
     }
     public void getThreadStateandName() throws Exception { 
     ObjectName[] serverRT = getServerRuntimes();
     
          
          //System.out.println("got server runtimes"); 
          int length = (int) serverRT.length;
          for (int i = 0; i < length; i++) {
               String name = (String) connection.getAttribute(serverRT[i], "Name");
               String state = (String) connection.getAttribute(serverRT[i],"State");
               System.out.println("Server name: " + name + ". Server state: " + state);
               ObjectName ThreadRT =(ObjectName) connection.getAttribute(serverRT[i], "ThreadPoolRuntime"); 
               
               System.out.println("ExecuteThreadTotalCount :"+ (Object)connection. getAttribute(ThreadRT,"ExecuteThreadTotalCount")); 
               System.out.println("StandbyThreadCount:"+ (Object)connection.getAttribute(ThreadRT, "StandbyThreadCount"));
               
               Integer e_ThreadTotal =(Integer)(Object)connection. getAttribute(ThreadRT,"ExecuteThreadTotalCount");               
               Integer e_standbyCount = (Integer)connection.getAttribute(ThreadRT, "StandbyThreadCount");
               int eThreadTotal = e_ThreadTotal.intValue();
               int estandbyCount = e_standbyCount.intValue();
               
               System.out.println("ExecuteActiveThreadCount:"+(eThreadTotal-estandbyCount));
               
               System.out.println("ExecuteThreadIdleCount :"+ (Object)connection. getAttribute(ThreadRT,"ExecuteThreadIdleCount"));                
               System.out.println("ExecuteHoggingThreadCount:"+ (Object)connection.getAttribute(ThreadRT, "HoggingThreadCount"));
               System.out.println("Throughput :"+ (Object)connection.getAttribute(ThreadRT, "Throughput"));
               System.out.println("HealthState :"+ (Object)connection.getAttribute(ThreadRT, "HealthState"));               
               
               //System.out.println("QueueLength:"+ (Object)connection.getAttribute(ThreadRT, "QueueLength"));
               //System.out.println("StandbyThreadCount:"+ (Object)connection.getAttribute(ThreadRT, "StandbyThreadCount"));
               //System.out.println("ExecuteActiveThreadCount:"+ (Object[])connection.getAttribute(ThreadRT, "ExecuteThreads"));
               
               //System.out.println("ExecuteActiveThreadCount:"+ ((Object[])connection.getAttribute(ThreadRT, "ExecuteThreads")).size());

               //System.out.println("ExecuteActiveThreadCount:"+ ((Object[])connection.getAttribute(ThreadRT, "ExecuteThreads")).length);
               
                         
               }
     }

     public void getExecuteAll() throws Exception { 
          System.out.println("*****************-Heap-****************");
          getJvmRuntime();
          System.out.println("*****************-JDBC-****************");
          getJdbcRuntime();
          System.out.println("*****************-Thread-****************");
          getThreadStateandName();
     }
     
public static void main(String[] args) throws Exception {
     if (args.length != 5) {     
               System.out.println("\n CheckValue ------> HeapCheck : 1\n");
               System.out.println(" CheckValue ------> DBCConnectionCheck : 2\n");
               System.out.println(" CheckValue ------> ThreadStatusCheck : 3\n");
               System.out.println(" CheckValue ------> All : 0\n");                    
               System.out.println("Usage: java EnvDetail HostName Port CheckValue\n");
               //System.out.println(args[0]);
               String hostname =args[0];
               //System.out.println(args[1]);
               String port= args[1];
               //System.out.println(args[2]);
               chkValue = Integer.parseInt(args[2]);
          EnvDetail ts = new EnvDetail();
          initConnection(hostname,port, username, password);
          
          switch (chkValue) {
          case 1: ts.getJvmRuntime(); break;
          case 2: ts.getJdbcRuntime(); break;
          case 3: ts.getThreadStateandName(); break;
          case 0: ts.getExecuteAll(); break;
          case 5: System.out.println("CheckValue5"); break;
                    }
                         return;
               }
     }//main method
}// class

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值