CollLinuxHost.java 11 KB
package com.sitech.ismp.coll.centercoll;

import com.sitech.ismp.check.downtime.CheckConnectionState;
import com.sitech.ismp.check.downtime.ParamterBean;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.DES3;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Vector;

public class CollLinuxHost implements CollLinuxHostMBean {
	private static Logger log= Logger.getLogger(CollLinuxHost.class);

	CheckConnectionState server = null;
	String kbpClass="10-10-24-10"; // linux 主机的标识

	public CollLinuxHost() {
		log.info("create CollLinuxHost ");
		server = new CheckConnectionState();
	}

	public Vector collLinux(HashMap params) {
		CollLinuxWithCMD cmd = new CollLinuxWithCMD();
		return cmd.collLinux(params);
	}

	public Vector getConfig(HashMap params) {

		CollLinuxWithCMD cmd = new CollLinuxWithCMD();
		return cmd.getConfig(params);
	}// end getConfig

	public Vector getCpu(HashMap params) {
		CollLinuxWithCMD cmd = new CollLinuxWithCMD();
		return cmd.getCpu(params);
	}

	public Vector getMemory(HashMap params) {
		CollLinuxWithCMD cmd = new CollLinuxWithCMD();
		return cmd.getMemory(params);
	}

	public Vector getDisk(HashMap params) {
		CollLinuxWithCMD cmd = new CollLinuxWithCMD();
		return cmd.getDisk(params);
	}// end getDisk

	public Vector getFileSystem(HashMap params) {
		CollLinuxWithCMD cmd = new CollLinuxWithCMD();
		return cmd.getFileSystem(params);

	}

	public Vector getUserProNum(HashMap params) {
		CollLinuxWithCMD cmd = new CollLinuxWithCMD();
		return cmd.getUserProNum(params);

	}

	public Vector getSyslog(HashMap params) {
		CollLinuxWithCMD cmd = new CollLinuxWithCMD();
		return cmd.getSyslog(params);

	}

	public Vector getSpecProcess(HashMap params) {
		CollLinuxWithCMD cmd = new CollLinuxWithCMD();
		return cmd.getSpecProcess(params);

	}

    public Vector getBusiProcess(HashMap params) {
        CollLinuxWithCMD cmd = new CollLinuxWithCMD();
        return cmd.getBusiProcess(params);
    }

    public Vector getPortListenings(HashMap params) {
        CollLinuxWithCMD cmd = new CollLinuxWithCMD();
        return cmd.getPortListenings(params);
    }

    @Override
    public Vector getProcessTop5Cpu(HashMap params) {
        CollLinuxWithCMD cmd = new CollLinuxWithCMD();
        return cmd.getProcessTop5Cpu(params);
    }

    @Override
    public Vector getProcessTop5Mem(HashMap params) {
        CollLinuxWithCMD cmd = new CollLinuxWithCMD();
        return cmd.getProcessTop5Mem(params);
    }

    public Vector getSyslogBylogPathUseGrep(HashMap params) {
		CollLinuxWithCMD cmd = new CollLinuxWithCMD();
		return cmd.getSyslogBylogPathUseGrep(params);
	}

	/**
	 * 获取linux集中式采集的 ping 连接状态
	 * @param params
	 * @return
	 */
    public Vector getPing(HashMap params) {
		Vector<TblATO_KPIDETAIL> v=new Vector<TblATO_KPIDETAIL>();
		String isPing = params.get("isPing")+""; // frank 2017-05-19 添加。Y进行ping操作,N不执行
		if("Y".equals(isPing)){
			String ip=params.get("IP_ADDR")+"";
			if(!StringUtils.isEmpty(ip)){
				String connectStatCode="FM-00-10-001-999"; // linux 主机连接状态
				String hostName=params.get("HOSTNAME")+""; // 资源标识
				String unitId = kbpClass+":" + hostName + "-total";
				ParamterBean bean =new ParamterBean();
				bean.setDEVICE_IP(ip);
				bean.setType("linux");
				log.info("*** execute linux getPing  ** ip=["+ip+"]** unitId=["+unitId+"]*********");

				/**
				 * getPingState 方法执行后会将状态插入到表中,然后取当前时间
				 * 根基IP查询失败的次数,如果失败次数为3次,则确定为宕机,发送告警。
				 */
				CheckConnectionState server=new CheckConnectionState();
				server.getPingState(bean);

				// 根据IP获取ping的错误次数。如果是 DOWN 的话,发送告警信息。
				String state=server.getPingErrorState(ip);
				TblATO_KPIDETAIL kpidetail= new TblATO_KPIDETAIL();
				log.info("***** unitId**"+unitId+"** state **"+state+"*******");
				try {
					kpidetail.setKPI_ID(connectStatCode);
					kpidetail.setUNIT_ID(unitId);
					kpidetail.setKPI_VALUE(state);
					kpidetail.setCLL_TIME(new Date());
				} catch (ParseException e) {
					e.printStackTrace();
				}
				v.add(kpidetail);
			}
		}
		return v;

	}


	/**
	 * 获取linux集中式采集的ssh连接状态
	 * @param params
	 * @return
	 */
	@Override
	public Vector getSSH(HashMap params) {
		Vector<TblATO_KPIDETAIL> v=new Vector<TblATO_KPIDETAIL>();
		String ip=params.get("IP_ADDR")+"";
		String loginStatCode="FM-00-10-001-998"; // linux 主机登录状态
		ParamterBean bean =new ParamterBean();
		String tempPort=params.get("PROTOCOL_PORT")+"";
		int port=0;
		if(!StringUtils.isEmpty(tempPort)){
			port=Integer.parseInt(tempPort);
		}
		String hostName=params.get("HOSTNAME")+"";
		String username=params.get("USERNAME")+"";
		String protocol=params.get("PROTOCOL")+"";
		String password=params.get("PASSWORD")+"";
		password=DES3.decrypt(password);

		String unitId = kbpClass+":" + hostName + "-total";
		log.info("*** execute linux getPing  ** ip=["+ip+"]** unitId=["+unitId+"]**port="+port+"***hostName="+hostName+"****");

		bean.setDEVICE_IP(ip);
		bean.setPROTOCOL_PORT(port);
		bean.setUSER_NAME(username);
		bean.setPASSWORD(password);
		bean.setPROTOCOL(protocol);
		String state=server.getConnectionState4SshOrTelnet(bean);

		TblATO_KPIDETAIL kpidetail=new TblATO_KPIDETAIL();
		try {
			kpidetail.setKPI_ID(loginStatCode);
			kpidetail.setUNIT_ID(unitId);
			kpidetail.setKPI_VALUE(state);
			kpidetail.setCLL_TIME(new Date());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		v.add(kpidetail);
		return v;
	}

    public Vector getDemsg(HashMap params) {
        CollLinuxWithCMD cmd = new CollLinuxWithCMD();
        return cmd.getDemsg(params);
    }

    public Vector getNet(HashMap params) {
        CollLinuxWithCMD cmd = new CollLinuxWithCMD();
        return cmd.getNet(params);
    }



	public static void main(String[] args) {
		System.out
				.println("***************Linux Center Collect Test Begin*********************");
        //System.out.println(DES3.decrypt("cloud"));
//		String ipAddr = "172.21.3.38";
//		String username = "root";
//		String password = "111111";
//		String protocal = "ssh";
//		String hostName = "app-123";
		String ipAddr = "172.21.0.201";
		String username = "ssdbomc";
		String password = "bomcssd";
		String protocal = "ssh";
		String hostName = "172_21_0_201";
        String processName = "pf,tomcat";
        String listeningPort = "22,48781";
        String correctListening = "1,1";
        String pingIp = "172.21.0.201";
        String pingUsername = "ssdbomc";
        String pingPwd = "bomcssd";
        String pingProtocal = "ssh";
        String pointIPs = "172.21.0.31,172.21.0.203";
        String demsgKey = "Detected Tx Unit Hang";
//		String ipAddr = read("IP_ADDR(remote host ip):\n");
//		String username = read("USERNAME:\n");
//		String password = read("PASSWORD:\n");
//		String protocal = read("PROTOCAL(ssh/telent):\n");
//		String hostName = read("HOST_NAME(remote host name):\n");
//        String processName = read("PROCESS_NAME:\n");
//        String listeningPort = read("LISTENING_PORT:\n");
//        String correctListening = read("CORRECT_LISTENING:\n");
//		String methodStr = read("METHOD(0:getAll, 1:getConfig, 2:getCpu, 3:getMemory, 4:getDisk, " +
//                "5:getFileSystem, 6:getPortListenings, 7:getBusiProcess, 8:getProcessTop5Mem, 9:getProcessTop5Cpu):\n");

        String methodStr = "13";
		System.out.println("IP_ADDR=" + ipAddr + ", USERNAME=" + username
				+ ", PASSWORD=" + password + ", PROTOTAL=" + protocal
				+ ", HOST_NAME=" + hostName + ", METHOD=" + methodStr);

		HashMap<String, String> params = new HashMap<String, String>();
		params.put("IP_ADDR", ipAddr);
		params.put("USERNAME", username);
		params.put("PASSWORD", DES3.encrypt(password));
		params.put("PROTOCOL", protocal);
		params.put("HOSTNAME", hostName);
		params.put("PROCESS_NAME", processName);
		params.put("LISTENING_PORT", listeningPort);
		params.put("CORRECT_LISTENING", correctListening);
        params.put("PING_IP_ADDR",pingIp);
        params.put("PING_USERNAME",pingUsername);
        params.put("PING_PASSWORD",DES3.encrypt(pingPwd));
        params.put("PING_PROTOCOL",pingProtocal);
		params.put("POINT_IPS", pointIPs);
        params.put("DEMSG_KEY", demsgKey);

		int method = Integer.parseInt(methodStr);
		Vector<TblATO_KPIDETAIL> result = new Vector<TblATO_KPIDETAIL>();

		CollLinuxHost collector = new CollLinuxHost();

		switch (method) {
		case 0:
			result.addAll(collector.getConfig(params));
			result.addAll(collector.getCpu(params));
			result.addAll(collector.getMemory(params));
			result.addAll(collector.getDisk(params));
			result.addAll(collector.getFileSystem(params));
			result.addAll(collector.getPortListenings(params));
			result.addAll(collector.getBusiProcess(params));
			result.addAll(collector.getProcessTop5Mem(params));
			result.addAll(collector.getProcessTop5Cpu(params));
			break;
		case 1:
			result.addAll(collector.getConfig(params));
			break;
		case 2:
			result.addAll(collector.getCpu(params));
			break;
		case 3:
			result.addAll(collector.getMemory(params));
			break;
		case 4:
			result.addAll(collector.getDisk(params));
			break;
		case 5:
			result.addAll(collector.getFileSystem(params));
			break;
		case 6:
			result.addAll(collector.getPortListenings(params));
			break;
		case 7:
			result.addAll(collector.getBusiProcess(params));
			break;
		case 8:
			result.addAll(collector.getProcessTop5Mem(params));
			break;
		case 9:
			result.addAll(collector.getProcessTop5Cpu(params));
			break;
        case 10:
            result.addAll(collector.getDemsg(params));
            break;
        case 11:
            result.addAll(collector.getPing(params));
            break;
        case 12:
            result.addAll(collector.getUserProNum(params));
            break;
        case 13:
            result.addAll(collector.getNet(params));
            break;
		default:
			break;
		}

		System.out
				.println("***************Linux Center Collect Test End*********************");
		System.out.println();
		for (int i = 0; i < result.size(); i++) {
			TblATO_KPIDETAIL record = (TblATO_KPIDETAIL) result.get(i);
//			System.out.println(record.UNIT_ID + "\t" + record.KPI_ID + "\t" + record.KPI_VALUE);
            String insSql = "INSERT INTO TB_ATO_KPIDETAIL (KPI_ID, UNIT_ID, CLL_TIME, KPI_VALUE, EXT_INFO, DB_TIME, EXT_UNIT_ID, `INTERVAL`) " +
                    "VALUES ('#KPI_ID#', '#UNIT_ID#', now(), '#KPI_VALUE#', null, now(), '10-10-24:172_21_0_201', #INTERVAL#);";
            insSql = insSql.replace("#KPI_ID#", record.KPI_ID);
            insSql = insSql.replace("#UNIT_ID#", record.UNIT_ID);
            insSql = insSql.replace("#KPI_VALUE#", record.KPI_VALUE);
            insSql = insSql.replace("#INTERVAL#", record.INTERVAL);
            System.out.println(insSql);
		}
	}
	
	private static String read(String prompt) {
		Scanner scanner = new Scanner(System.in);
		System.out.print(prompt);
		return scanner.nextLine();
	}
}