Downtime.java 2.68 KB
package com.sitech.ismp.check.downtime;

import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import org.apache.log4j.Logger;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author frank  zmm@honggroup.com.cn
 * @Description:  宕机检测MBean实现类
 * @Package com.sitech.ismp.check.downtime
 * @ClassName: com.sitech.ismp.check.downtime.Downtime
 * @date 2017年04月15日 13:43
 */
public class Downtime implements DowntimeMBean {

    private static Logger log= Logger.getLogger(Downtime.class);


    DowntimeDao dao=new DowntimeDao();
    DowntimeHistoryDao historyDao=new DowntimeHistoryDao();
    SimpleDateFormat formaF=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 检查主机的连接状态。如果连接异常,需要每隔30秒连续连接3次,如果3次连接都异常,则视为主机宕机,发送严重告警。
     * 1 将需要监控的设备添加到内存数据库中。
     * 2 对表中的主机进行ping操作。
     * 3 对表中的主机进行SSH登录认证
     * 4 发送告警信息
     * @param params
     * @return
     */
    @Override
    public Vector<TblATO_KPIDETAIL> checkConnectState(HashMap<String,String> params){
        dao.addDowntimeHost(params);
        String ipAddr=params.get("DEVICE_IP");
        boolean flag=isNeedExecuteCheckMethod(ipAddr);
        if(flag){
            log.info("*************"+ formaF.format(new Date()) +" execute method checkConnectState .................");
            // 将需要监控的设备添加到内存数据库中。
            DowntimeBean bean=dao.map2DowntimeBean(params);
            DowntimeServer server=new DowntimeServer(bean);
            // 包括 2-4 需要执行的内容。 如果监控的设备很多的时候,使用单线程可能一个监控周期都不能ping完所有的主机。
            server.run();
            log.info("@@@@@@@@@@@@@@@@@@@@@@***** finish run method *****@@@@@@@@@@@@@@@@@@@@@@@@@@@");
            log.info(bean.toString());
        }
        Vector<TblATO_KPIDETAIL> bean = new Vector<TblATO_KPIDETAIL>();
        return bean;
    }



    /**
     * 是否执行checkConnectState 方法。
     * 判断当前IP是否发送过告警信息。每天一个主机只发送1条告警信息。
     * @return
     */
    private boolean isNeedExecuteCheckMethod(String ipAddr){
        boolean flag=true; // 是否该ipAddr已经发送过消息。
        SimpleDateFormat formaF=new SimpleDateFormat("yyyy-MM-dd");
        String currentDay=formaF.format(new Date());
        List<DowntimeHistoryBean> list=historyDao.getDowntimeHistoryByIp(ipAddr,currentDay);
        if(null !=list && list.size()>0){
            flag=false;
        }
        return flag;
    }


}