CollSymantecSFWithCMDByCron.java 13.5 KB
package com.sitech.ismp.coll.storage.symantec;

import com.sitech.ismp.coll.CollBase;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.Formater;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.io.*;
import java.util.*;

public class CollSymantecSFWithCMDByCron extends CollBase {

    static final String RESULT_PATH = "../result/";
    static final String SH_COLL_SFINFO = "getSFInfo.sh";
    static final String RESULT_SFINFO = "SFInfoResult";


    String PRE_UNITID = "10-14-12"; //赛门铁克标识



    public Vector<TblATO_KPIDETAIL> getSFInfo(HashMap<String, String> params) {
        logger.info("begin getSymantecSFInfo");

        // 保存采集结果,并返回值
        CollBase collResult = new CollBase();

        // 执行脚本---------因脚本部分指令需root权限,故脚本执行由root用户crontab执行。
//        exec(SH_COLL_SFINFO);
        // 获得脚本执行结果
        List<String> result = getResult(RESULT_SFINFO);
        if(null == result || result.isEmpty()){
            return collResult.KPISet;
        }
        List<List<String>> list = parseResult(result, 9);

        String deviceId = params.get("DEVICE_ID");
        if(null == deviceId || "".equals(deviceId.trim())){
            // 获取主机名称 : uname -a
            List<String> vHostName = list.get(0);
            deviceId = split(vHostName.get(0), 1);
        }

        /*---------------1.心跳状态检查-----------------*/
        try {
            String memUnitId = PRE_UNITID + "-10:" + Formater.neatenunitid(deviceId) + "-total";

            List<String> heartbeat_result = list.get(1);
            logger.info("---------------heartbeat start-------------");

            if(null == heartbeat_result || heartbeat_result.size() < 1){
                throw new Exception("\"/sbin/lltstat -nvv configured\": null");
            }
            int count = 0;
            for (int i=2;i<heartbeat_result.size();i++){

                String temp = heartbeat_result.get(i);
                if(temp.contains("UP")){
                    count++;
                }
            }
            //心跳状态检查(UP行数)
            collResult.addKPI(memUnitId, "PM-00-06-001-09", Integer.toString(count));

        } catch (Exception e) {
            logger.error("Exception while getHeartbeatInfo!", e);
        }
        /*---------------2.Gabconfig端口检查-----------------*/
        try {
            String memUnitId = PRE_UNITID + "-10:" + Formater.neatenunitid(deviceId) + "-total";
            List<String> gabconfig_result = list.get(2);
            logger.info("---------------gabconfig start-------------");
            if(null == gabconfig_result || gabconfig_result.size() < 1){
                throw new Exception("\"/sbin/gabconfig -a\": null");
            }
            int count = 0;
            for (int i=2;i<gabconfig_result.size();i++){

                String temp = gabconfig_result.get(i);
                if(temp.startsWith("Port")){
                    count++;
                }
            }
            //Gabconfig端口检查(Port个数)
            collResult.addKPI(memUnitId, "PM-00-06-001-10", Integer.toString(count));

        } catch (Exception e) {
            logger.error("Exception while getGabconfigInfo!", e);
        }
        /*---------------3.磁盘链路状态检查-----------------*/
        try {
            String memUnitId = PRE_UNITID + "-11:" + Formater.neatenunitid(deviceId);
            List<String> dmpnode_result = list.get(3);
            logger.info("---------------dmpnode start-------------");
            if(null == dmpnode_result || dmpnode_result.size() < 1){
                throw new Exception("\"/usr/sbin/vxdmpadm getdmpnode\": null");
            }
            for (int i=2;i<dmpnode_result.size();i++){

                String temp = dmpnode_result.get(i);
                if(!split(temp,5).startsWith("0")){
                    String name = split(temp,0);

                    collResult.addKPI(memUnitId + "-" +name, "CM-00-06-011-01", name+"#"+split(temp,6));
                    collResult.addKPI(memUnitId + "-" +name, "PM-00-06-001-11", split(temp,5));
                }
            }

        } catch (Exception e) {
            logger.error("Exception while getdmpnodeInfo!", e);
        }
        /*---------------4.Volume状态检查-----------------*/
        try {
            String memUnitId = PRE_UNITID + "-10:" + Formater.neatenunitid(deviceId) + "-total";
            List<String> volume_result = list.get(4);
            logger.info("---------------volume start-------------");
            if(null == volume_result || volume_result.size() < 1){
                throw new Exception("\"vxprint -v\": null");
            }

            int count = 0;
            for (int i=3;i<volume_result.size();i++){

                String temp = volume_result.get(i);
                if(temp.contains("DISABLE")){
                    count++;
                }
            }
            //Volume状态检查(disable个数)
            collResult.addKPI(memUnitId, "PM-00-06-001-05", Integer.toString(count));

        } catch (Exception e) {
            logger.error("Exception while getVolumeInfo!", e);
        }

        /*---------------5.vxconfigd 状态检查-----------------*/
        try {
            String memUnitId = PRE_UNITID + "-10:" + Formater.neatenunitid(deviceId) + "-total";
            List<String> vxconfigd_result = list.get(5);
            logger.info("-------------vxconfigd start-------------");
            if(null == vxconfigd_result || vxconfigd_result.size() < 1){
                collResult.addKPI(memUnitId, "PM-00-06-001-08", "0");
            }else{
                collResult.addKPI(memUnitId, "PM-00-06-001-08", Integer.toString(vxconfigd_result.size()));
            }

        } catch (Exception e) {
            logger.error("Exception while getVxconfigdInfo!", e);
        }
        /*---------------6.disk group状态检查-----------------*/
        try {
            String memUnitId = PRE_UNITID + "-10:" + Formater.neatenunitid(deviceId) + "-total";
            List<String> diskGroup_result = list.get(6);
            logger.info("-------------diskGroup start-------------");
            if(null == diskGroup_result || diskGroup_result.size() < 1){
                throw new Exception("\"vxdg list\": null");
            }
            int count = 0;
            for (int i=1;i<diskGroup_result.size();i++){

                String temp = diskGroup_result.get(i);
                if(temp.contains("disable")){
                    count++;
                }
            }
            //DG状态检查(disable个数)
            collResult.addKPI(memUnitId, "PM-00-06-001-12", Integer.toString(count));

        } catch (Exception e) {
            logger.error("Exception while getDiskGroupInfo!", e);
        }
        /*---------------7.VCS Log检查-----------------*/
        try {
            String memUnitId = PRE_UNITID + "-10:" + Formater.neatenunitid(deviceId);
            List<String> VCSLog_result = list.get(7);
            logger.info("-------------VCSLog start-------------");
            int count = 0;
            if(!(null == VCSLog_result || VCSLog_result.size() < 1)){
                for (int i=1;i<VCSLog_result.size();i++){
                    count++;
                }
            }
            //VCS日志当日错误个数
            collResult.addKPI(memUnitId, "PM-00-06-001-06", Integer.toString(count));
        } catch (Exception e) {
            logger.error("Exception while getVCSLog!", e);
        }
        /*---------------8.DMP eventlog检查-----------------*/
        try {
            String memUnitId = PRE_UNITID + "-10:" + Formater.neatenunitid(deviceId);
            List<String> DMPLog_result = list.get(8);
            logger.info("-------------DMPLog start-------------");
            int count = 0;
            if(!(null == DMPLog_result || DMPLog_result.size() < 1)){
                for (int i=1;i<DMPLog_result.size();i++){
                    count++;
                }
            }
            //event.log日志检查
            collResult.addKPI(memUnitId, "PM-00-06-001-07", Integer.toString(count));
        } catch (Exception e) {
            logger.error("Exception while getDMPLog!", e);
        }
        return collResult.getKPISet();
    }


    public String getVetorString(List v) {
        StringBuffer sb = new StringBuffer();
        if (v != null && v.size() > 0) {
            for (int i = 0; i < v.size(); i++) {
                String s = (String) v.get(i);
                if (s != null && i < v.size() - 1) {
                    sb.append(s + "\n");
                }
            }
        }
        return sb.toString();
    }


    /**
     * 通知执行脚本
     * 通知方式:写一个文件(文件名为采集脚本名称,如:getCpu.sh、getMem.sh)到notice目录,
     * contab会定时执行脚本扫描该目录,得到文件名,并执行脚本
     *
     * @throws
     * @since Ver 1.1
     */
    private void exec(String shellName) {
        String noticePath = "../notice/ibm/";
        String noticeFileName = noticePath + shellName;
        PrintWriter kpiFileStream = null;
        try {
            kpiFileStream = new PrintWriter(new FileWriter(Formater.replaceSpace(noticeFileName), false), true);
            kpiFileStream.println(shellName);
        } catch (IOException e) {
            logger.error("[SymantecSF COLL] Exception while exec(" + shellName + ")", e);
        } finally {
            if (kpiFileStream != null) {
                kpiFileStream.flush();
                kpiFileStream.close();
            }
        }
    }


    /**
     * 获得脚本执行结果
     *
     * @throws
     * @since Ver 1.1
     */
    private List<String> getResult(String resultFileName) {
        try {
            Thread.sleep(20 * 1000L);
        } catch (InterruptedException e) {
            logger.warn("[SymantecSF COLL] InterruptedException.");
        }

        StringBuffer sb = new StringBuffer();
        // 超时时间:2分钟
        long timeout = 3 * 60 * 1000L;
        List<String> result = new ArrayList<String>();
        long beginTime = new Date().getTime();

        File file = new File(RESULT_PATH);
        while (true) {
            if (new Date().getTime() - beginTime > timeout) {
                // 超时退出
                logger.warn("[SymantecSF COLL] Timeout, exit.");
                break;
            }

            File[] filelist = file.listFiles();
            if (filelist == null || filelist.length == 0) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    logger.error("[SymantecSF COLL] InterruptedException", e);
                }
                continue;
            }

            File resultFile = null;
            // 根据文件名找到采集文件
            for (int i = 0; i < filelist.length; i++) {
                if (filelist[i].getName().startsWith(resultFileName)) {
                    resultFile = filelist[i];
                    break;
                }
            }

            if (resultFile != null) {
                BufferedReader br = null;
                try {
                    br = new BufferedReader(new FileReader(resultFile));
                    String line = "";
                    while ((line = br.readLine()) != null) {
                        result.add(line);
                        sb.append(line + "\n");
                    }
                    break;
                } catch (Exception e) {
                    logger.error("[SymantecSF COLL] Exception while readFile:" + resultFileName, e);
                } finally {
                    if (br != null) {
                        try {
                            br.close();
                        } catch (IOException e) {
                            logger.error("[SymantecSF COLL] IOException:", e);
                        }
                    }
                    logger.info("[SymantecSF COLL] Delete coll result file : " + resultFile.getName());
                    logger.info(RESULT_PATH + ":\n" + sb.toString());
                    resultFile.delete();
                }
            }
        }// end while

        return result;
    }

    /**
     * 解析每个命令的执行结果
     *
     * @throws
     * @since Ver 1.1
     */
    private List<List<String>> parseResult(List<String> result, int num) {
        List<List<String>> list = new ArrayList<List<String>>();
        for (int i = 0; i < num; i++) {
            list.add(new ArrayList<String>());
        }

        int i = 0;
        for (String line : result) {
            if (line.trim().equals(SPLIT_LINE)) {
                i++;
                continue;
            }else if(line.trim().endsWith(SPLIT_LINE)){
                line = line.replace(SPLIT_LINE,"");
                list.get(i).add(line);
                i++;
            }else{
                list.get(i).add(line);
            }
        }
        return list;
    }

    public static void main(String[] args) {
        HashMap<String, String > params = new HashMap<String, String>();
        CollSymantecSFWithCMDByCron coll = new CollSymantecSFWithCMDByCron();
        Vector<TblATO_KPIDETAIL> result = coll.getSFInfo(params);
        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);
        }
//        System.out.println("v  lv_doeridxro fsgen        ENABLED  31457280 -        ACTIVE   -       -".contains("ENABLED"));

    }

}