欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

weblogic集群节点监控

程序员文章站 2022-04-16 23:26:07
...
由于项目需要,对weblogic集群做监控,并定时采集系统中队列执行时长、各节点占用内存
等情况。
第一步:获取MBeanHome
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对象
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();
    }
}


第三步:处理集群节点状态数据
import java.util.ArrayList;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.huawei.netforce.server.Handler;
import com.huawei.netforce.server.ServerRuntime;
import com.huawei.netforce.server.common.ServiceNode;
import com.huawei.netforce.server.handlers.LogHandler;
import com.huawei.netforce.server.handlers.MsgHandler;

public class ServerRuntimeJob implements Job
{
    private static List handlers = new ArrayList(); 
    
    static
    {
        //短信处理 如内存不足、队列堵塞等情况发送告警短信
        handlers.add(new MsgHandler());
        //日志处理 如将执行时间较长的队列记录日志,定期优化
        handlers.add(new LogHandler());
    }
    
    //quartz定时执行 一般为5分钟执行一次    
    public void execute(JobExecutionContext arg0) throws JobExecutionException
    {
        ServerRuntime runtime = ServerRuntime.getInstance();
        
        List list = runtime.buildServer(runtime.getServerRuntimes());
        try
        {
            processServerNode(list);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    //处理集群各服务节点状态数据
    private void processServerNode(List list) throws Exception
    {
        ServiceNode node = null;
        for(int i = 0; i < list.size(); i++)
        {
            node = (ServiceNode)list.get(i);
            handle(node);
        }
    }
    
    //handles链中的各Handle分别处理
    private void handle(ServiceNode node) throws Exception
    {
        for(int i = 0; i < handlers.size(); i++)
        {
            Handler handler = (Handler)handlers.get(i);
            handler.handle(node);
        }
    }
}
相关标签: Weblogic quartz