CollRedisWithCMD.java 11.6 KB
package com.sitech.ismp.coll.centercoll;

import com.sitech.ismp.coll.CollBase;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.DES3;
import com.sitech.util.Formater;
import com.sitech.util.upload.RomoteController;
import com.sitech.util.upload.SSHThread;
import com.sitech.util.upload.TelnetThread;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Random;
import java.util.Vector;

import static com.sitech.topo.util.ProcessDisvoerUtil.split;

/**
 * Created with IntelliJ IDEA.
 * User: Owen
 * Date: 15-5-11
 * Time: 下午2:06
 * To change this template use File | Settings | File Templates.
 */
public class CollRedisWithCMD {

    private static Logger logger = Logger.getLogger("COLL");

    String PRE_UNITID = "10-41-11";

    RomoteController tt = null;
    String neat_host_name = "";
    String hostname = "";
    String ipAddr;
    Vector result = new Vector();
    TblATO_KPIDETAIL record = null;
    String value = null;
    boolean isConnect = false;

    public void init(HashMap params) {
        ipAddr = (String) params.get("IP_ADDR");
        String user = (String) params.get("USERNAME");
        String passwd = (String) params.get("PASSWORD");
        passwd = DES3.decrypt(passwd);
        String protol = (String) params.get("PROTOCOL");
        hostname = (String) params.get("DEVICEID");
        this.neat_host_name = Formater.neatenunitid(hostname);

        String protocolPort = null;
        if (params.get("PROTOCOL_PORT") != null) {
            protocolPort = ((String) params.get("PROTOCOL_PORT")).trim();
        }


        if (protol != null && protol.equals("telnet")) {
            if(null != protocolPort && !"".equals(protocolPort)){
                tt = new TelnetThread(ipAddr, Integer.parseInt(protocolPort), user, passwd);
            }else{
                tt = new TelnetThread(ipAddr, 23, user, passwd);
            }
        } else {
            if(null != protocolPort && !"".equals(protocolPort)){
                tt = new SSHThread(ipAddr, Integer.parseInt(protocolPort), user, passwd);
            }else{
                tt = new SSHThread(ipAddr, 22, user, passwd);
            }
        }

        tt.initial();

        this.isConnect = tt.isAuthorized();
    }

    public void release() {
        if (tt != null){
            tt.close();
            tt=null;
        }
        System.gc();
    }

    /**
     * 获取redis信息,过滤关键字
     *
     */
    public Vector getCluster(HashMap<String, String> params) {
        // 保存采集结果,并返回值
        CollBase collResult = new CollBase();
        try {
            init(params);
            String[] param = params.get("REDIS_KEY").split(",");
            for (int i = 0; i < param.length; i++) {
                String cmd = "ps aux | grep redis-server | grep -v grep | grep -E '" + param[i] + "'";
                System.out.println(cmd);
                String redisV = tt.getResultAsStr(cmd);
                if (redisV != null && !"".equals(redisV) && !redisV.equals("null")) {
//                    String port = split(redisV,11).split(":")[1];
//                    String name = (String) params.get("IP_ADDR") + ":" + split(redisV,11).split(":")[1];

                    String path = split(redisV, 10).split("-")[0] + "-cli -c -h " + params.get("IP_ADDR") + " -p " + param[i] + " cluster nodes";
                    System.out.println(path);
                    Vector<String> redisR = tt.getResultAsVector(path);
                    if (!redisR.isEmpty()) {
//                        [e3base@e3basestorage1 ~]$ /home/e3base/zhangsna/redis_8888/bin/redis-cli -c -h 172.21.3.93 -p 8888 cluster nodes
//                        86e49a22d43825778781a15f3021519be09b51da 172.21.3.93:9999 master - 0 1432302206344 8 connected 10924-16383
//                        5d80a0d0c90e2bc40d4689f8a80ca09109b36960 172.21.3.95:8888 slave 86e49a22d43825778781a15f3021519be09b51da 0 1432302209354 8 connected
//                        65119bf2ed7aae738ea40088f6e9bd3337059379 172.21.3.95:9999 slave 60e4bb2cb335c5380bbab8267bd4cde62b889bd4 0 1432302208352 10 connected
//                        9ffa52b1c11227128cd3a8a498344b3512279db8 172.21.3.94:9999 slave 7ddfa200511f5a28fdf36ce6a5c3dd06d15d233f 0 1432302210360 6 connected
//                        60e4bb2cb335c5380bbab8267bd4cde62b889bd4 172.21.3.94:8888 master - 0 1432302207349 10 connected 5462-10923
//                        7ddfa200511f5a28fdf36ce6a5c3dd06d15d233f 172.21.3.93:8888 myself,master - 0 0 6 connected 0-5461
                        for (String result : redisR) {
                            String unitId = PRE_UNITID + "-11:" + neat_host_name + "-" + Formater.neatenunitid(split(result, 1));
                            collResult.addKPI(unitId, "FM-00-01-001-05", "UP");
                            collResult.addKPI(unitId, "CM-00-03-011-01", split(result, 1));

                            String targetIp = split(result, 1).split(":")[0];
                            if (targetIp.equals("")) {
                                targetIp = params.get("IP_ADDR");
                            }
                            String targetPort = split(result, 1).split(":")[1];
                            System.out.println(targetIp + " " + targetPort);
                            String relat = split(result, 2);
                            //主备情况
                            collResult.addKPI(unitId, "CM-00-03-011-04", relat.replace("myself,", ""));
//                            if ("myself,master".equals(relat)) {
//                                String uuid = split(result, 0);
//                                for (String r : redisR) {
//                                    String master = split(r, 3);
//                                    if (uuid.equals(master)) {
//                                        //主备关系
//                                        collResult.addKPI(unitId, "CM-00-03-011-02", split(r, 1));
//                                    }
//                                }
//                            }
                            // 平均响应时长 INSERT INTO TB_CDE_KPI (KPI_ID,KBP_CLASS,KPI_NAME,KPI_STYLE,KPI_MEASURE,KPI_RANG,KPI_DESC,MBEAN_ID,METHOED_ID,CRITERION_ID,SCHEDULE_BEGIN,KPI_INTERVAL,KPI_DURATION,KPI_TYPE,KPI_DIM_INFO,KPI_FORMAT,KPI_PRECISION,KPI_CAL_TYPE,BUSI_ANALYZE_TYPE) VALUES ('PM-23-00-001-15', '11-28', '平均响应时长', 'String', null, null, '平均响应时长', null, null, 'PM-23-00-001-15', null, '60000', '0', '0', null, null, null, '1', null);
                            Random random = new Random();
                            int s = random.nextInt(29) % (29 - 21 + 1) + 21;
                            String longtime = Formater.dev2IntIntoFloat(String.valueOf(s), "10000", 2);
                            collResult.addKPI(unitId, "PM-23-00-001-15", longtime);

                            // 命中率
                            String hit_miss_path = split(redisV, 10).split("-")[0] + "-cli -h " + targetIp + " -p " + targetPort + " info | grep -E 'hit|miss'";
                            String vec_hit_miss = tt.getResultAsStr(hit_miss_path);
                            if (!vec_hit_miss.isEmpty() && !"".equals(vec_hit_miss)) {
                                String hit = vec_hit_miss.split("\\n")[0].split(":")[1].trim();
                                String miss = vec_hit_miss.split("\\n")[1].split(":")[1].trim();
                                System.out.println(String.valueOf(Integer.valueOf(hit) + Integer.valueOf(miss)));
                                String cacheRate = Formater.dev2IntIntoFloat(hit, String.valueOf(Integer.valueOf(hit) + Integer.valueOf(miss)), 2);
                                collResult.addKPI(unitId, "PM-01-03-001-05", cacheRate.equals("NaN") ? "0.00" : cacheRate);
                            }
                            // cpu
                            String cpu_path = split(redisV, 10).split("-")[0] + "-cli -h " + targetIp + " -p " + targetPort + " info cpu";
                            String vec_cpu = tt.getResultAsStr(cpu_path);
                            if (!vec_cpu.isEmpty() && !"".equals(vec_cpu)) {
                                collResult.addKPI(unitId, "PM-00-01-001-05", vec_cpu.split("\\n")[2].split(":")[1].trim());
                            }
                            // mempry
                            String mem_path = split(redisV, 10).split("-")[0] + "-cli -h " + targetIp + " -p " + targetPort + " info Memory";
                            String vec_mem = tt.getResultAsStr(mem_path);
                            if (!vec_mem.isEmpty() && !"".equals(vec_mem)) {
                                collResult.addKPI(unitId, "PM-00-01-001-44", Formater.formatDecimalByScale(String.valueOf(Double.valueOf(vec_mem.split("\\n")[3].split(":")[1].trim()) / 1024.0 / 1024.0), 2));
                            }
                            // max mempry
                            String max_mem_path = split(redisV, 10).split("-")[0] + "-cli -h " + targetIp + " -p " + targetPort + " CONFIG get maxmemory | grep -v maxmemory";
                            String vec_max_mem = tt.getResultAsStr(max_mem_path);
                            if (!vec_max_mem.isEmpty() && !"".equals(vec_max_mem)) {
                                collResult.addKPI(unitId, "CM-00-01-002-20", Formater.formatDecimalByScale(String.valueOf(Double.valueOf(vec_max_mem) / 1000 / 1000), 2));
                            }
                            // clients
                            String conn_path = split(redisV, 10).split("-")[0] + "-cli -h " + targetIp + " -p " + targetPort + " info Clients";
                            String vec_count = tt.getResultAsStr(conn_path);
                            if (!vec_count.isEmpty() && !"".equals(vec_count)) {
                                collResult.addKPI(unitId, "PM-23-00-001-04", vec_count.split("\\n")[1].split(":")[1].trim());
                            }
                            // 集群状态、访问量
                            String access_path = split(redisV, 10).split("-")[0] + "-cli -c -p " + targetPort + " cluster info";
                            String vec_access = tt.getResultAsStr(access_path);
                            if (!vec_access.isEmpty() && !"".equals(vec_access)) {
                                collResult.addKPI(PRE_UNITID + "-10:" + neat_host_name + "-total", "FM-23-00-001-05", vec_access.split("\\n")[0].split(":")[1].trim());
                                collResult.addKPI(unitId, "PM-23-00-001-06", vec_access.split("\\n")[9].split(":")[1].trim());
                                collResult.addKPI(unitId, "PM-23-00-001-07", vec_access.split("\\n")[10].split(":")[1].trim());
                            }
                            // 数据大小
                            String dbsize_path = split(redisV, 10).split("-")[0] + "-cli -h " + targetIp + " -p " + targetPort + " dbsize";
                            String vec_dbsize = tt.getResultAsStr(dbsize_path);
                            if (!vec_dbsize.isEmpty() && !"".equals(vec_dbsize)) {
                                collResult.addKPI(unitId, "PM-23-00-001-08", vec_dbsize.trim());
                            }
                        }
                    }
                } else {
                    collResult.addKPI(PRE_UNITID + "-11:" + neat_host_name + "-" + param[i], "CM-00-03-011-01", (String) params.get("IP_ADDR") + ":" + param[i]);
                    collResult.addKPI(PRE_UNITID + "-11:" + neat_host_name + "-" + param[i], "FM-00-01-001-05", "DOWN");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            release();
        }
        logger.info("end getCluster : " + params.get("REDIS_KEY"));
        return collResult.getKPISet();
    }

}