CollSdfsWithCMD.java 5.92 KB
package com.sitech.ismp.coll.centercoll;

import com.sitech.ismp.coll.CollBase;
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.Date;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;

/**
 * Created by mooker on 15/6/23.
 */
public class CollSdfsWithCMD {

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

    String PRE_UNITID = "10-32-17";
    String SDFSTABLE = "sh #SDFS_HOME#tools/GetHtableInfo.sh 2>/dev/null";
    String SDFSFILENUM = "sh #SDFS_HOME#tools/GetFileNum.sh #FILESYSTEM#";

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

    boolean flag = false;

    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 init(HashMap params) {
        String user = null;
        String passwd = null;
        String protol = null;
        if (this.flag) {
            ipAddr = (String) params.get("PING_IP_ADDR");
            user = (String) params.get("PING_USERNAME");
            passwd = (String) params.get("PING_PASSWORD");
            protol = (String) params.get("PING_PROTOCOL");
        } else {
            ipAddr = (String) params.get("IP_ADDR");
            user = (String) params.get("USERNAME");
            passwd = (String) params.get("PASSWORD");
            protol = (String) params.get("PROTOCOL");
        }
        passwd = DES3.decrypt(passwd);
        hostname = (String) params.get("SDFSNAME");
        this.neat_host_name = Formater.neatenunitid(hostname);

        sdfshome = (String) params.get("SDFSHOME");
        if (sdfshome != null && !sdfshome.endsWith("/")) {
            sdfshome = sdfshome + "/";
        }

        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();
    }

    public Vector collHtable(HashMap params) {
        logger.info("begin getSDFS : ");
        // 保存采集结果,并返回值
        CollBase collResult = new CollBase();
        try {
            init(params);
            System.out.println(new Date() + " " + this.SDFSTABLE.replace("#SDFS_HOME#", sdfshome));
            Vector sdfsTable = tt.getResultAsVector(this.SDFSTABLE.replace("#SDFS_HOME#", sdfshome));
            System.out.println(new Date() + " GetHtableInfo execute end, " + sdfsTable.size());
            if (!sdfsTable.isEmpty()) {
                for (int i = 3; i < sdfsTable.size(); i++) {
                    String _sdfsTable = sdfsTable.get(i).toString();
                    if (null != _sdfsTable && !_sdfsTable.equals("") && _sdfsTable.length() > 0) {
                        String filesystem = split(_sdfsTable, 0);
                        String unitId = PRE_UNITID + "-11:" + neat_host_name + "-" + filesystem;
                        String total = split(_sdfsTable, 1);
                        String used = split(_sdfsTable, 2);
                        String free = split(_sdfsTable, 3);
                        String usedrate = split(_sdfsTable, 4);
                        System.out.println("SDFSFILENUM is: " + this.SDFSFILENUM.replace("#SDFS_HOME#", sdfshome).replace("#FILESYSTEM#", filesystem));
                        Vector sdfsFileNum = tt.getResultAsVector(this.SDFSFILENUM.replace("#SDFS_HOME#", sdfshome).replace("#FILESYSTEM#", filesystem));
                        if (!sdfsFileNum.isEmpty()) {
//                            System.out.print("sdfsFileNum is: " + sdfsFileNum.get(0).toString() + "【】");
                            collResult.addKPI(unitId, "PM-32-00-001-04", sdfsFileNum.get(0).toString());
                        }
//                        System.out.println(filesystem + " " + total + " " + used + " " + free + " " + usedrate);
                        collResult.addKPI(unitId, "CM-32-00-001-01", filesystem);
                        collResult.addKPI(unitId, "CM-32-00-001-02", total);
                        collResult.addKPI(unitId, "PM-32-00-001-01", Formater.convertKBMB2GB((used.substring(0, used.length() - 1)), used.substring(used.length() - 1)));
                        collResult.addKPI(unitId, "PM-32-00-001-02", Formater.convertKBMB2GB((free.substring(0, free.length() - 1)), free.substring(free.length() - 1)));
                        collResult.addKPI(unitId, "PM-32-00-001-03", usedrate);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            release();
        }
        logger.info("end getDemsg : " + params.get("DEMSG_KEY"));
        return collResult.getKPISet();
    }

}