[转帖]weblogic集群节点监控_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 4024 | 回复: 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.       private MBeanHome mBeanHome; 
  7.        
  8.       privatestaticfinal WebLogicBeanHome home = new WebLogicBeanHome(); 
  9.        
  10.       private WebLogicBeanHome() 
  11.        { 
  12.              init(); 
  13.        } 
  14.        
  15.       privatevoid init() 
  16.        { 
  17.              Environment environment = new Environment(); 
  18.              environment.setProviderUrl("t3://localhost:7001"); 
  19.              environment.setSecurityPrincipal("weblogic"); 
  20.              environment.setSecurityCredentials("weblogic"); 
  21.              Context context; 
  22.             try
  23.              { 
  24.                    context = environment.getInitialContext(); 
  25.                    mBeanHome = (MBeanHome) context.lookup(MBeanHome.ADMIN_JNDI_NAME); 
  26.              } 
  27.             catch(NamingException e) 
  28.              { 
  29.                    e.printStackTrace(); 
  30.              } 
  31.             catch(Exception ex) 
  32.              { 
  33.                    ex.printStackTrace(); 
  34.              } 
  35.        } 
  36.        
  37.       publicstatic WebLogicBeanHome getInstance() 
  38.        { 
  39.             return home; 
  40.        } 
  41.        
  42.       public MBeanHome getMBeanHome() 
  43.        { 
  44.             return mBeanHome; 
  45.        } 
  46.        
  47.       public String getServerName() 
  48.        { 
  49.             return mBeanHome.getMBeanServer().getServerName(); 
  50.        } 
  51.        
  52.       public String getDomainName() 
  53.        { 
  54.             return mBeanHome.getDomainName(); 
  55.        } 
  56.        
  57.       public Set getMBeansByType(String type) 
  58.        { 
  59.             if(type == null || type.trim().length() == 0) 
  60.              { 
  61.                   returnnull; 
  62.              } 
  63.             else
  64.              { 
  65.                   try
  66.                    { 
  67.                         return mBeanHome.getMBeansByType(type); 
  68.                    } 
  69.                   catch(Exception e) 
  70.                    { 
  71.                          e.printStackTrace(); 
  72.                          init(); 
  73.                         return mBeanHome.getMBeansByType(type); 
  74.                    } 
  75.              } 
  76.        } 
  77.        
  78.       public Set getAllMBeans() 
  79.        { 
  80.             return mBeanHome.getAllMBeans(); 
  81.        } 


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


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




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