[转帖]weblogic集群节点监控_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 5214 | 回复: 0   主题: [转帖]weblogic集群节点监控        下一篇 
dwx8023
注册用户
等级:上尉
经验:612
发帖:114
精华:0
注册:2011-11-8
状态:离线
发送短消息息给dwx8023 加好友    发送短消息息给dwx8023 发消息
发表于: IP:您无权察看 2011-11-10 15:34:54 | [全部帖] [楼主帖] 楼主

由于项目需要,对weblogic集群做监控,并定时采集系统中队列执行时长、各节点占用内存
等情况。
第一步:获取MBeanHome

Java代码

  1. import javax.naming.Context; 
  2. import javax.naming.NamingException; 
  3. import weblogic.jndi.Environment; 
  4. import weblogic.management.MBeanHome; 
  5. publicclass WebLogicBeanHome 
  6. { 
  7.       private MBeanHome mBeanHome; 
  8.        
  9.       privatestaticfinal WebLogicBeanHome home = new WebLogicBeanHome(); 
  10.        
  11.       private WebLogicBeanHome() 
  12.        { 
  13.              init(); 
  14.        } 
  15.        
  16.       privatevoid init() 
  17.        { 
  18.              Environment environment = new Environment(); 
  19.              environment.setProviderUrl("t3://localhost:7001"); 
  20.              environment.setSecurityPrincipal("weblogic"); 
  21.              environment.setSecurityCredentials("weblogic"); 
  22.              Context context; 
  23.             try
  24.              { 
  25.                    context = environment.getInitialContext(); 
  26.                    mBeanHome = (MBeanHome) context.lookup(MBeanHome.ADMIN_JNDI_NAME); 
  27.              } 
  28.             catch(NamingException e) 
  29.              { 
  30.                    e.printStackTrace(); 
  31.              } 
  32.             catch(Exception ex) 
  33.              { 
  34.                    ex.printStackTrace(); 
  35.              } 
  36.        } 
  37.        
  38.       publicstatic WebLogicBeanHome getInstance() 
  39.        { 
  40.             return home; 
  41.        } 
  42.        
  43.       public MBeanHome getMBeanHome() 
  44.        { 
  45.             return mBeanHome; 
  46.        } 
  47.        
  48.       public String getServerName() 
  49.        { 
  50.             return mBeanHome.getMBeanServer().getServerName(); 
  51.        } 
  52.        
  53.       public String getDomainName() 
  54.        { 
  55.             return mBeanHome.getDomainName(); 
  56.        } 
  57.        
  58.       public Set getMBeansByType(String type) 
  59.        { 
  60.             if(type == null || type.trim().length() == 0) 
  61.              { 
  62.                   returnnull; 
  63.              } 
  64.             else
  65.              { 
  66.                   try
  67.                    { 
  68.                         return mBeanHome.getMBeansByType(type); 
  69.                    } 
  70.                   catch(Exception e) 
  71.                    { 
  72.                          e.printStackTrace(); 
  73.                          init(); 
  74.                         return mBeanHome.getMBeansByType(type); 
  75.                    } 
  76.              } 
  77.        } 
  78.        
  79.       public Set getAllMBeans() 
  80.        { 
  81.             return mBeanHome.getAllMBeans(); 
  82.        } 
  83. } 


import javax.naming.Context;
import javax.naming.NamingException;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;

public class WebLogicBeanHome
{
       private MBeanHome mBeanHome;

       private static final WebLogicBeanHome home = new WebLogicBeanHome();

       private WebLogicBeanHome()
       {
             init();
       }

       private void init()
       {
             Environment environment = new Environment();
             environment.setProviderUrl("t3://localhost:7001");
             environment.setSecurityPrincipal("weblogic");
             environment.setSecurityCredentials("weblogic");
             Context context;
             try
             {
                   context = environment.getInitialContext();
                   mBeanHome = (MBeanHome) context.lookup(MBeanHome.ADMIN_JNDI_NAME);
             }
             catch(NamingException e)
             {
                   e.printStackTrace();
             }
             catch(Exception ex)
             {
                   ex.printStackTrace();
             }
       }

       public static WebLogicBeanHome getInstance()
       {
             return home;
       }

       public MBeanHome getMBeanHome()
       {
             return mBeanHome;
       }

       public String getServerName()
       {
             return mBeanHome.getMBeanServer().getServerName();
       }

       public String getDomainName()
       {
             return mBeanHome.getDomainName();
       }

       public Set getMBeansByType(String type)
       {
             if(type == null || type.trim().length() == 0)
             {
                   return null;
             }
             else
             {
                   try
                   {
                         return mBeanHome.getMBeansByType(type);
                   }
                   catch(Exception e)
                   {
                         e.printStackTrace();
                         init();
                         return mBeanHome.getMBeansByType(type);
                   }
             }
       }

       public Set getAllMBeans()
       {
             return mBeanHome.getAllMBeans();
       }
}



第二步:将集群各节点状态信息构造成ServiceNode对象

Java代码

  1. import java.util.ArrayList; 
  2. import java.util.Iterator; 
  3. import java.util.List; 
  4. import java.util.Set; 
  5. import com.huawei.netforce.server.common.ExecuteQueue; 
  6. import com.huawei.netforce.server.common.ServiceNode; 
  7. import com.huawei.netforce.server.home.WebLogicBeanHome; 
  8. import weblogic.management.runtime.ExecuteQueueRuntimeMBean; 
  9. import weblogic.management.runtime.ExecuteThread; 
  10. import weblogic.management.runtime.JVMRuntimeMBean; 
  11. import weblogic.management.runtime.ServerRuntimeMBean_Stub; 
  12. publicclass ServerRuntime 
  13. { 
  14.       publicstaticfinal String RUNNING = "RUNNING"; 
  15.        
  16.       privatestaticfinal ServerRuntime server = new ServerRuntime(); 
  17.       
  18.       private ServerRuntime() 
  19.        { 
  20.             
  21.        } 
  22.       
  23.       publicstatic ServerRuntime getInstance() 
  24.        { 
  25.             return server; 
  26.        } 
  27.       
  28.       /** 
  29.        * 建造集群节点信息 
  30.        * @param set 
  31.        * @return 
  32.        */
  33.       public List buildServer(Set set) 
  34.        { 
  35.              List list = new ArrayList(); 
  36.             if(set == null) 
  37.             return list; 
  38.              Iterator it = set.iterator(); 
  39.             
  40.             while(it.hasNext()) 
  41.              { 
  42.                    ServerRuntimeMBean_Stub stub = (ServerRuntimeMBean_Stub) it.next(); 
  43.                    ServiceNode node = new ServiceNode(); 
  44.                   //节点名称 
  45.                    node.setName(stub.getName()); 
  46.                   //节点地址 
  47.                    node.setHost(stub.getListenAddress()); 
  48.                   //节点端口 
  49.                    node.setPort(String.valueOf(stub.getListenPort())); 
  50.                   //节点状态 
  51.                    node.setStatus(stub.getState()); 
  52.                   //节点当前可用内存 
  53.                    node.setFreeHeap(getJVMCurrentFreeHeap(stub)); 
  54.                   //节点总内存 
  55.                    node.setTotalHeap(getJVMCurrentHeapSize(stub)); 
  56.                   //节点总队列数 
  57.                    node.setQueueTotal(getExecuteQueueRuntime(stub).getExecuteThreadTotalCount()); 
  58.                   //节点当前占用队列数 
  59.                    node.setQueueUsed(node.getQueueTotal() - getExecuteQueueRuntime(stub).getExecuteThreadCurrentIdleCount()); 
  60.                   //节点队列详细列表 
  61.                    buildExcuteQueues(node, getExecuteQueueRuntime(stub).getExecuteThreads()); 
  62.                    list.add(node); 
  63.              } 
  64.             return list; 
  65.        } 
  66.        
  67.       /** 
  68.        * 建造队列线程信息 
  69.        * 
  70.        * @param eths 
  71.        */
  72.       privatevoid buildExcuteQueues(ServiceNode node, ExecuteThread[] eths) 
  73.        { 
  74.             if(eths == null) 
  75.              { 
  76.                   return; 
  77.              } 
  78.             for(int i = 0; i < eths.length; i++) 
  79.              { 
  80.                    ExecuteThread eThread = eths[i]; 
  81.                   if(eThread.getCurrentRequest() == null || "".equals(eThread.getCurrentRequest()) || "null".equals(eThread.getCurrentRequest())) 
  82.                    { 
  83.                         continue; 
  84.                    } 
  85.                    ExecuteQueue queue = new ExecuteQueue(); 
  86.                    queue.setNumber(i); 
  87.                    queue.setName(eThread.getName()); 
  88.                   //队列请求地址URL 
  89.                    queue.setRequest(eThread.getCurrentRequest()); 
  90.                   //队列请求时间 
  91.                    queue.setStartTime(eThread.getCurrentRequestStartTime()); 
  92.                    queue.setTotal(eThread.getServicedRequestTotalCount()); 
  93.                    node.add(queue); 
  94.              } 
  95.        } 
  96.       
  97.       public Set getServerRuntimes() 
  98.        { 
  99.             return WebLogicBeanHome.getInstance().getMBeansByType(Constant.ServerRuntime); 
  100.        } 
  101.       
  102.       privateboolean isStoped(ServerRuntimeMBean_Stub mBean) 
  103.        { 
  104.             return !RUNNING.equalsIgnoreCase(mBean.getState()); 
  105.        } 
  106.       
  107.       private JVMRuntimeMBean getJVMRuntime(ServerRuntimeMBean_Stub mBean) 
  108.        { 
  109.             return mBean.getJVMRuntime(); 
  110.        } 
  111.        
  112.       private ExecuteQueueRuntimeMBean getExecuteQueueRuntime(ServerRuntimeMBean_Stub mBean) 
  113.        { 
  114.             return mBean.getExecuteQueueRuntime(); 
  115.        } 
  116.       
  117.       privatelong getJVMCurrentFreeHeap(ServerRuntimeMBean_Stub mBean) 
  118.        { 
  119.             return getJVMRuntime(mBean).getHeapFreeCurrent(); 
  120.        } 
  121.       
  122.       privatelong getJVMCurrentHeapSize(ServerRuntimeMBean_Stub mBean) 
  123.        { 
  124.             return getJVMRuntime(mBean).getHeapSizeCurrent(); 
  125.        } 
  126.       
  127.       private String getOSVersion(ServerRuntimeMBean_Stub mBean) 
  128.        { 
  129.             return getJVMRuntime(mBean).getOSVersion(); 
  130.        } 
  131.       
  132.       private String getOSName(ServerRuntimeMBean_Stub mBean) 
  133.        { 
  134.             return getJVMRuntime(mBean).getOSName(); 
  135.        } 
  136.       
  137.       private String getJavaVersion(ServerRuntimeMBean_Stub mBean) 
  138.        { 
  139.             return getJVMRuntime(mBean).getJavaVersion(); 
  140.        } 
  141.       
  142.       private String getJavaVendor(ServerRuntimeMBean_Stub mBean) 
  143.        { 
  144.             return getJVMRuntime(mBean).getJavaVendor(); 
  145.        } 
  146. } 


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.huawei.netforce.server.common.ExecuteQueue;
import com.huawei.netforce.server.common.ServiceNode;
import com.huawei.netforce.server.home.WebLogicBeanHome;

import weblogic.management.runtime.ExecuteQueueRuntimeMBean;
import weblogic.management.runtime.ExecuteThread;
import weblogic.management.runtime.JVMRuntimeMBean;
import weblogic.management.runtime.ServerRuntimeMBean_Stub;

public class ServerRuntime
{
       public static final String RUNNING = "RUNNING";

       private static final ServerRuntime server = new ServerRuntime();

       private ServerRuntime()
       {

       }

       public static ServerRuntime getInstance()
       {
             return server;
       }

       /**
       * 建造集群节点信息
       * @param set
       * @return
       */
       public List buildServer(Set set)
       {
             List list = new ArrayList();
             if(set == null)
             return list;
             Iterator it = set.iterator();

             while(it.hasNext())
             {
                   ServerRuntimeMBean_Stub stub = (ServerRuntimeMBean_Stub) it.next();
                   ServiceNode node = new ServiceNode();
                   //节点名称
                   node.setName(stub.getName());
                   //节点地址
                   node.setHost(stub.getListenAddress());
                   //节点端口
                   node.setPort(String.valueOf(stub.getListenPort()));
                   //节点状态
                   node.setStatus(stub.getState());
                   //节点当前可用内存
                   node.setFreeHeap(getJVMCurrentFreeHeap(stub));
                   //节点总内存
                   node.setTotalHeap(getJVMCurrentHeapSize(stub));
                   //节点总队列数
                   node.setQueueTotal(getExecuteQueueRuntime(stub).getExecuteThreadTotalCount());
                   //节点当前占用队列数
                   node.setQueueUsed(node.getQueueTotal() - getExecuteQueueRuntime(stub).getExecuteThreadCurrentIdleCount());
                   //节点队列详细列表
                   buildExcuteQueues(node, getExecuteQueueRuntime(stub).getExecuteThreads());
                   list.add(node);
             }
             return list;
       }

       /**
       * 建造队列线程信息
       *
       * @param eths
       */
       private void buildExcuteQueues(ServiceNode node, ExecuteThread[] eths)
       {
             if(eths == null)
             {
                   return;
             }
             for(int i = 0; i < eths.length; i++)
             {
                   ExecuteThread eThread = eths[i];
                   if(eThread.getCurrentRequest() == null || "".equals(eThread.getCurrentRequest()) || "null".equals(eThread.getCurrentRequest()))
                   {
                         continue;
                   }
                   ExecuteQueue queue = new ExecuteQueue();
                   queue.setNumber(i);
                   queue.setName(eThread.getName());
                   //队列请求地址URL
                   queue.setRequest(eThread.getCurrentRequest());
                   //队列请求时间
                   queue.setStartTime(eThread.getCurrentRequestStartTime());
                   queue.setTotal(eThread.getServicedRequestTotalCount());
                   node.add(queue);
             }
       }

       public Set getServerRuntimes()
       {
             return WebLogicBeanHome.getInstance().getMBeansByType(Constant.ServerRuntime);
       }

       private boolean isStoped(ServerRuntimeMBean_Stub mBean)
       {
             return !RUNNING.equalsIgnoreCase(mBean.getState());
       }

       private JVMRuntimeMBean getJVMRuntime(ServerRuntimeMBean_Stub mBean)
       {
             return mBean.getJVMRuntime();
       }

       private ExecuteQueueRuntimeMBean getExecuteQueueRuntime(ServerRuntimeMBean_Stub mBean)
       {
             return mBean.getExecuteQueueRuntime();
       }

       private long getJVMCurrentFreeHeap(ServerRuntimeMBean_Stub mBean)
       {
             return getJVMRuntime(mBean).getHeapFreeCurrent();
       }

       private long getJVMCurrentHeapSize(ServerRuntimeMBean_Stub mBean)
       {
             return getJVMRuntime(mBean).getHeapSizeCurrent();
       }

       private String getOSVersion(ServerRuntimeMBean_Stub mBean)
       {
             return getJVMRuntime(mBean).getOSVersion();
       }

       private String getOSName(ServerRuntimeMBean_Stub mBean)
       {
             return getJVMRuntime(mBean).getOSName();
       }

       private String getJavaVersion(ServerRuntimeMBean_Stub mBean)
       {
             return getJVMRuntime(mBean).getJavaVersion();
       }

       private String getJavaVendor(ServerRuntimeMBean_Stub mBean)
       {
             return getJVMRuntime(mBean).getJavaVendor();
       }
}



第三步:处理集群节点状态数据

Java代码

  1. import java.util.ArrayList; 
  2. import java.util.List; 
  3. import org.quartz.Job; 
  4. import org.quartz.JobExecutionContext; 
  5. import org.quartz.JobExecutionException; 
  6. import com.huawei.netforce.server.Handler; 
  7. import com.huawei.netforce.server.ServerRuntime; 
  8. import com.huawei.netforce.server.common.ServiceNode; 
  9. import com.huawei.netforce.server.handlers.LogHandler; 
  10. import com.huawei.netforce.server.handlers.MsgHandler; 
  11. publicclass ServerRuntimeJob implements Job 
  12. { 
  13.       privatestatic List handlers = new ArrayList(); 
  14.        
  15.       static
  16.        { 
  17.             //短信处理 如内存不足、队列堵塞等情况发送告警短信 
  18.              handlers.add(new MsgHandler()); 
  19.             //日志处理 如将执行时间较长的队列记录日志,定期优化 
  20.              handlers.add(new LogHandler()); 
  21.        } 
  22.        
  23.       //quartz定时执行 一般为5分钟执行一次 
  24.       publicvoid execute(JobExecutionContext arg0) throws JobExecutionException 
  25.        { 
  26.              ServerRuntime runtime = ServerRuntime.getInstance(); 
  27.              
  28.              List list = runtime.buildServer(runtime.getServerRuntimes()); 
  29.             try
  30.              { 
  31.                    processServerNode(list); 
  32.              } 
  33.             catch(Exception e) 
  34.              { 
  35.                    e.printStackTrace(); 
  36.              } 
  37.        } 
  38.       
  39.       //处理集群各服务节点状态数据 
  40.       privatevoid processServerNode(List list) throws Exception 
  41.        { 
  42.              ServiceNode node = null; 
  43.             for(int i = 0; i < list.size(); i++) 
  44.              { 
  45.                    node = (ServiceNode)list.get(i); 
  46.                    handle(node); 
  47.              } 
  48.        } 
  49.        
  50.       //handles链中的各Handle分别处理 
  51.       privatevoid handle(ServiceNode node) throws Exception 
  52.        { 
  53.             for(int i = 0; i < handlers.size(); i++) 
  54.              { 
  55.                    Handler handler = (Handler)handlers.get(i); 
  56.                    handler.handle(node); 
  57.              } 
  58.        } 
  59. } 




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论