CollDossWithCMD.java 7.36 KB
package com.sitech.ismp.coll.storage.doss;

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.Map;
import java.util.StringTokenizer;
import java.util.Vector;

/**
 * Created by mooker on 14/11/25.
 */
public class CollDossWithCMD {

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

    String PRE_UNITID = "10-14-14"; // 分布式对象存储标识

    RomoteController tt = null;
    String neat_host_name = "";
    String storageName = "";
    String ipAddr;
    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");
        storageName = (String) params.get("STORAGENAME");
        this.neat_host_name = Formater.neatenunitid(storageName);

        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 Vector getConfig(HashMap params) {
        // 保存采集结果,并返回值
        CollBase collResult = new CollBase();
        try {
            init(params);
            String totalUnitId = this.PRE_UNITID + "-10:" + neat_host_name + "-total";

            if(!tt.isAuthorized()){
                collResult.addKPI(totalUnitId, "FM-00-01-001-06", "Disconnect");
                return collResult.KPISet;
            }else{
                collResult.addKPI(totalUnitId, "FM-00-01-001-06", "UP");
            }

            Vector diskInfo = tt.getResultAsVector("swift-recon -d | grep 'Disk usage:'");
            String diskSpace = split((String) diskInfo.elementAt(0), 6);
            // 存储配置容量 CM-00-05-002-01
            collResult.addKPI(totalUnitId, "CM-00-05-002-01", Formater.formatDecimalKpivalue(String.valueOf(Double.valueOf(diskSpace) / 1024 / 1024 / 1024 / 1024)));
            String diskUsed = split((String) diskInfo.elementAt(2), 7);
            // 存储空间使用百分比 PM-40-04-001-12
            collResult.addKPI(totalUnitId, "PM-40-04-001-12", Formater.formatDecimalKpivalue(diskUsed.substring(0, diskUsed.length() - 1)));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            release();
        }
        return collResult.getKPISet();
    }

    public Vector getSocket(HashMap params) {
        // 保存采集结果,并返回值
        CollBase collResult = new CollBase();
        try {
            init(params);
            String totalUnitId = this.PRE_UNITID + "-20:" + neat_host_name + "-socket";

            if (!tt.isAuthorized()) {
                return collResult.KPISet;
            }

            Vector socketInfo = tt.getResultAsVector("swift-recon --sockstat | grep avg");
            for (int i = 0; i < socketInfo.size(); i++) {
                String _socketInfo = (String) socketInfo.elementAt(i);
                String socketKey = split(_socketInfo, 0);
                String socketAvgValue = split(_socketInfo, 6);
                if (socketKey.contains("[tcp_in_use]")) {
                    // tcp_in_use TCP使用 PM-00-08-001-01
                    collResult.addKPI(totalUnitId, "PM-00-08-001-01", socketAvgValue.substring(0, socketAvgValue.length() - 1));
                } else if (socketKey.contains("[time_wait]")) {
                    // time_wait 等待时间 PM-00-08-001-02
                    collResult.addKPI(totalUnitId, "PM-00-08-001-02", socketAvgValue.substring(0, socketAvgValue.length() - 1));
                } else if (socketKey.contains("[tcp6_in_use]")) {
                    // tcp6_in_use TCP6使用 PM-00-08-001-03
                    collResult.addKPI(totalUnitId, "PM-00-08-001-03", socketAvgValue.substring(0, socketAvgValue.length() - 1));
                } else if (socketKey.contains("[tcp_mem_allocated_bytes]")) {
                    // tcp_mem_allocated_bytes TCP内存分配的字节 PM-00-08-001-04
                    collResult.addKPI(totalUnitId, "PM-00-08-001-04", socketAvgValue.substring(0, socketAvgValue.length() - 1));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            release();
        }
        return collResult.getKPISet();
    }

    public Vector getLoad(HashMap params) {
        // 保存采集结果,并返回值
        CollBase collResult = new CollBase();
        try {
            init(params);
            String totalUnitId = this.PRE_UNITID + "-21:" + neat_host_name + "-load";

            if (!tt.isAuthorized()) {
                return collResult.KPISet;
            }

            Vector socketInfo = tt.getResultAsVector("swift-recon -l | grep m_load_avg");
            for (int i = 0; i < socketInfo.size(); i++) {
                String _socketInfo = (String) socketInfo.elementAt(i);
                String socketKey = split(_socketInfo, 0);
                String socketAvgValue = split(_socketInfo, 6);
                if (socketKey.contains("[1m_load_avg]")) {
                    // 1m_load_avg TCP使用 PM-00-08-002-01
                    collResult.addKPI(totalUnitId, "PM-00-08-002-01", socketAvgValue.substring(0, socketAvgValue.length() - 1));
                } else if (socketKey.contains("[5m_load_avg]")) {
                    // 5m_load_avg 等待时间 PM-00-08-002-02
                    collResult.addKPI(totalUnitId, "PM-00-08-002-02", socketAvgValue.substring(0, socketAvgValue.length() - 1));
                } else if (socketKey.contains("[15m_load_avg]")) {
                    // 15m_load_avg TCP6使用 PM-00-08-002-03
                    collResult.addKPI(totalUnitId, "PM-00-08-002-03", socketAvgValue.substring(0, socketAvgValue.length() - 1));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            release();
        }
        return collResult.getKPISet();
    }

    public String split(String str, int pos) {
        StringTokenizer st = new StringTokenizer(str);
        Vector values = new Vector();
        while (st.hasMoreTokens()) {
            values.add(st.nextToken());
        }
        if (pos >= 0 && pos < values.size()) {
            return (String) values.elementAt(pos);
        }
        return "";
    }

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