CheckConnectionState.java 5.08 KB
package com.sitech.ismp.check.downtime;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author frank  zmm@honggroup.com.cn
 * @Description:  检查主机,数据库,中间件的连接状态。
 * 其中主机包含 Linux/unix ping ssh 2种状态;Windows ping snmp 2种状态;
 * 数据库  ping jdbc 2种状态; 中间件(Tomcat) ping jmx状态
 *
 * 需要区分 Windows与其他系统。在方法参数上,将map转换成CheckConnectionBean再使用。
 * @Package com.sitech.ismp.check.downtime
 * @ClassName: com.sitech.ismp.check.downtime.CheckConnectionState
 * @date 2017年04月22日 14:17
 */
public class CheckConnectionState {

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

    CheckConnectionService service=new CheckConnectionService();

    //******************************* 获取主机相关状态代码  *******************************//
    //******* 主机分为Linux/unix与Windows两大类,连接的时候前者使用ssh后者使用snmp  ************//
    //******************************* 获取主机相关状态代码  *******************************//

    /**
     * 获取设备的ping结果,能ping通返回 UP,否则返回 DOWN.
     * 需要连续ping3次,将ping的结果插入到 TB_CONNECTION_STATE.
     * @param bean
     * @return
     */
    public String getPingState(ParamterBean bean){
        String state=service.getPingState(bean);
        String type=bean.getType();
        if("windows".equals(type) || "linux".equals(type)|| "unix".equals(type)){
            String ip=bean.getDEVICE_IP();
            if(!StringUtils.isEmpty(ip)){
                ConnectionStateBean stateBean=new ConnectionStateBean();
                stateBean.setDEVICE_STATE(state); // 设备状态
                stateBean.setDEVICE_ID(UUID.randomUUID().toString());
                stateBean.setDEVICE_IP(bean.getDEVICE_IP());
                stateBean.setCREATE_DATE(new Date().getTime());
                service.addPingConnectionState(stateBean);
            }
        }
        return state;
    }

    /**
     * 根据IP获取ping不通的次数。只取集合中的前3条数据。当都为down的时候表明已经宕机,少于3次不算宕机。
     * @param ip
     * @return
     */
    public String getPingErrorState(String ip){
        List<ConnectionStateBean> list=service.getStatByParam(ip);
        int temp=0;
        String state=null;
        ConnectionStateBean bean=null;
        if(null !=list && list.size()>0){
            for(int i=0;i<3;i++){
                bean=list.get(i);
                state=bean.getDEVICE_STATE();
                if("DOWN".equals(state)){
                    temp++;
                }
            }
        }
        String stat="UP";
        if(temp==3){
            stat="DOWN";
        }
        return stat;
    }

    /**
     * 使用 SSH 或 Telnet 协议 获取设备的连接结果,能连接返回 UP,否则返回 DOWN.
     * @param bean
     */
    public String getConnectionState4SshOrTelnet(ParamterBean bean){
        String state=service.getConnectionState4SshOrTelnet(bean);
        return state;
    }

    /**
     * 使用 SNMP 协议 获取设备的连接结果,能连接返回 UP,否则返回 DOWN.
     * @param bean
     */
    public String getConnectionState4Snmp(ParamterBean bean){
        String state=service.getConnectionState4Snmp(bean);
        log.info("***** getConnectionState4Snmp ***state= "+state+"**************");
        return state;
    }

    /**
     * Oracle数据库连接状态
     * @param bean
     */
    public String getOracleConnectionState(ParamterBean bean){
        bean.setType("oracle");
        String state=service.getDbConnectionState(bean);
        return state;
    }

    public static void main(String [] args){
        CheckConnectionState ss=new CheckConnectionState();

        ParamterBean bean1=new ParamterBean();
        bean1.setUSER_NAME("Administrator");
        bean1.setPASSWORD("0213");
        bean1.setPROTOCOL_PORT(161);
        bean1.setDEVICE_IP("192.168.1.141");
        ss.getConnectionState4Snmp(bean1);

        ParamterBean bean=new ParamterBean();
        bean.setPASSWORD("tiger");
        bean.setUSER_NAME("scott");
        bean.setURL("jdbc:oracle:thin:@192.168.1.130:1521:ORCL");
        System.out.println(ss.getOracleConnectionState(bean));
    }

    /**
     * Mysql 数据库连接状态
     * @param bean
     */
    public String getMysqlConnectionState(ParamterBean bean){
        bean.setType("Mysql");
        String state=service.getDbConnectionState(bean);
        return state;
    }

    /**
     * SqlServer 数据库连接状态
     * @param bean
     */
    public void getSqlServerConnectionState(ParamterBean bean){
        bean.setType("SqlServer");
        String state=service.getDbConnectionState(bean);
    }

    /**
     * Db2 数据库连接状态
     * @param bean
     */
    public void getDb2ConnectionState(ParamterBean bean){
        bean.setType("Db2");
        String state=service.getDbConnectionState(bean);
    }




}