FileCollThread.java 16.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
package com.sitech.ismp.coll.busi;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sitech.base.AgentProperties;
import com.sitech.ismp.coll.busi.util.SystemUtil;
import com.sitech.util.FileUtils;
import com.sitech.util.Formater;
import com.sitech.util.RandomGUID;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import com.sitech.ismp.coll.CollBase;

/**
 * ClassName:FileCollThread
 * Description: 文件积压数量采集
 *
 * @author   Linxc
 * @version  
 * @since    Ver 1.1
 * @Date	 2012	Feb 22, 2012		5:30:00 PM
 */
public class FileCollThread implements Runnable {
	private Logger logger = Logger.getLogger("BUSI_COLL");

    private static final String BUSI_SCRIPT_PATH = "../script/busi/";
    private static final String BUSI_NOTICE_PATH = "../notice/busi/";
    private static final String TMP_NOTICE_DIR = AgentProperties.AGENT_HOME+"/script/busi/busi_tmp";
    /** 分隔线 */
    static final String SPLIT_LINE = "*******************************************************************************************#";
    static final String RESULT_PATH = "../result/";
	// 文件采集目录
	private String filePath;
	// 主机名称
	private String deviceName;
	// 业务类型
	private String unitId;
	// 业务类型
	private String interval;

    private String kbpClass;
	
	// 根据关键字过滤
	private String[] keyFilter;

	// 根据时间过滤
	private String dateFilter;

    private String schId;

    //采集参数对象数组
    private JSONArray paramArr;

    private String noticePath;
	
	public FileCollThread(HashMap<String, String> params) {
		kbpClass = params.get("KBPCLASS");
		filePath = params.get("FILEPATH");
		deviceName = params.get("DEVICENAME");		
		unitId = kbpClass + ":" + filePath;
		if (params.get("FILENAMEKEY") != null) {
			keyFilter = params.get("FILENAMEKEY").split(",");
		}
        schId = params.get("SCHEDULE_ID");
		dateFilter = params.get("DATEFORMAT");
		interval = params.get("COLL_INTERVAL");
        paramArr = JSONArray.fromObject(params.get("FILECOLLPARAMS"));
        String execUser = params.get("EXEC_USER");
        if (null == execUser || "".equals(execUser.trim())) {
            execUser = System.getProperty("user.name");
        }
        this.noticePath = AgentProperties.AGENT_HOME + "/notice/" + execUser + "/";
	}

	public void run() {
		logger.info("Begin collFile...");
        CollBase collResult = new CollBase();
        try{
            String cllScriptContent = createCollScript();
            String shName = schId+".sh";

            writeFile(shName, cllScriptContent);

//            exec(shName);
            writeNoticeFile(shName);
            // 获得脚本执行结果
            List<String> result = getResult(schId);
            if(null == result || result.isEmpty()){
                throw new Exception("Cannot get file coll results");
            }
            List<Map<String,String>> datas = parseResult(result);
            logger.info("CollData size:"+datas.size());
            String tmpFilePath = "";
            if(filePath.endsWith("/")){
                tmpFilePath = filePath;
            }else{
                tmpFilePath = filePath + "/";
            }
            for(Map<String,String> dt : datas){
                String fileCollSortId = dt.get("FILE_COLL_SORT_ID");
                if(!isNotNull(fileCollSortId)){
                    logger.info("[Exception] FILE COLL SORT_ID is null!");
                    continue;
                }
                String bakFileName = dt.get("BakFileName");
                String bakFileNum = dt.get("BakFileNum");
                String blankFileNum = dt.get("BlankFileNum");
                String lastMdfFileName = dt.get("LastMdfFileName");
                logger.info("CollResult[FILE_COLL_SORT_ID:"+fileCollSortId+",BakFileName:"+bakFileName+",BakFileNum:"
                        +bakFileNum+",BlankFileNum:"+blankFileNum+",LastMdfFileName:"+lastMdfFileName+"]");
                if(isNotNull(bakFileName)){//文件积压时长 PM-01-99-002 分钟
                    logger.info("Begin coll FileBakTime : "+tmpFilePath+bakFileName.trim());
                    File file = new File(tmpFilePath+bakFileName.trim());
                    long bakTime = file.lastModified();
                    long actTime = System.currentTimeMillis();
                    double val = (actTime-bakTime)/(1000*60);
                    collResult.addKPI(kbpClass+":"+fileCollSortId, "PM-01-99-002", Double.toString(val), interval);
                }
                if(isNotNull(bakFileNum)){//文件积压数量 PM-01-99-001 个数
                    logger.info("Begin coll FileBakNum : "+bakFileNum);
                    collResult.addKPI(kbpClass+":"+fileCollSortId, "PM-01-99-001", bakFileNum, interval);
                }
                if(isNotNull(blankFileNum)){//空文件数量 FM-01-99-076 个数
                    logger.info("Begin coll BlankFileNum : "+blankFileNum);
                    collResult.addKPI(kbpClass+":"+fileCollSortId, "FM-01-99-076", blankFileNum, interval);
                }
                if(isNotNull(lastMdfFileName)){//文件未修改时长 PM-01-99-003 分钟
                    logger.info("Begin coll FileLastMdfTime : "+tmpFilePath+lastMdfFileName.trim());
                    File file = new File(tmpFilePath+lastMdfFileName.trim());
                    long bakTime = file.lastModified();
                    long actTime = System.currentTimeMillis();
                    double val = (actTime-bakTime)/(1000*60);
                    collResult.addKPI(kbpClass+":"+fileCollSortId, "PM-01-99-003", Double.toString(val), interval);
                }
//                collResult.addKPI(unitId, "PM-01-11-008-01", Integer.toString(size), interval);

            }
            collResult.addKPI(unitId, "CM-01-11-008-01", filePath, interval);
            collResult.addKPI(unitId, "CM-01-11-008-03", deviceName, interval);
            collResult.saveKPI2File();
        }catch (Exception e){
            logger.error("Exception while collFile...",e);
        }
		logger.info("End collFile...");
	}

    private boolean isNotNull(String str){
        boolean flag = false;
        if(null!=str && !"".equals(str.trim())){
            flag = true;
        }
        return flag;
    }

    private void writeFile(String shellName, String content) {
        try{
            String shellPath = SystemUtil.SHELL_SCRIPT_PATH;
//            String shellPath = "E:\\";
            FileUtils fu = new FileUtils();
            if (fu.isExists(shellPath, shellName)) {
                fu.delFile(shellPath, shellName);
            }
            FileUtils.writeFile(shellPath, shellName, content, false);
        }catch (Exception e){
            logger.error("Exception while writeFile["+schId+".sh]",e);
        }
    }

    private String createCollScript(){
        StringBuffer sb = new StringBuffer();
        sb.append(FileCollConstants.SHELL_TITLE).append(FileCollConstants.TAB_LINE);

        for(int i=0;i<paramArr.size();i++){
            JSONObject paramObj = paramArr.getJSONObject(i);
            String fileSortId = (String) paramObj.get("fileSortId");
            sb.append(FileCollConstants.ECHO_SORT_ID.replaceFirst("#FILE_COLL_SORT_ID#",fileSortId).replaceFirst("#SCHEDULE_ID#", schId)).append(FileCollConstants.TAB_LINE);
            String fileKeyWord = (String) paramObj.get("fileKeyWord");
            String fileBackBlockTimeKpi = String.valueOf(paramObj.get("fileBackBlockTimeKpi"));//文件积压时长
            String fileBackBlockNumKpi = String.valueOf(paramObj.get("fileBackBlockNumKpi"));//文件积压数量
            String blankFileNumKpi = String.valueOf(paramObj.get("blankFileNumKpi"));//空文件数量
            String fileMdfLastTimeKpi = String.valueOf(paramObj.get("fileMdfLastTimeKpi"));//文件未修改时长
            String[] keyWordArr = fileKeyWord.split(",");
            StringBuffer grep_key = new StringBuffer();
            for(String key:keyWordArr){
                grep_key.append(" grep "+key+" | ");
            }
            if("1".equals(fileBackBlockTimeKpi)){//文件积压时长
                sb.append("BakFileName=`"+FileCollConstants.LS_LTR_FILE+" "+filePath+"| grep ^[^d] | awk '{print $9}' | "+grep_key+" sed -n '1p'`").append(FileCollConstants.TAB_LINE);
                sb.append("echo BakFileName=$BakFileName >> "+AgentProperties.AGENT_HOME+"/result_temp/"+schId).append(FileCollConstants.TAB_LINE);
            }
            if("1".equals(fileBackBlockNumKpi)){//文件积压数量
                sb.append("BakFileNum=`"+FileCollConstants.LS_L_FILE+" "+filePath+"| grep ^[^d] | awk '{print $9}' | "+grep_key+" wc -l`").append(FileCollConstants.TAB_LINE);
                sb.append("echo BakFileNum=$BakFileNum >> "+AgentProperties.AGENT_HOME+"/result_temp/"+schId).append(FileCollConstants.TAB_LINE);
            }
            if("1".equals(blankFileNumKpi)){//空文件数量
                sb.append("BlankFileNum=`"+FileCollConstants.LS_L_FILE+" "+filePath+"| grep ^[^d] | awk '$5==0{print $9}' |"+grep_key+" wc -l`").append(FileCollConstants.TAB_LINE);
                sb.append("echo BlankFileNum=$BlankFileNum >> "+AgentProperties.AGENT_HOME+"/result_temp/"+schId).append(FileCollConstants.TAB_LINE);
            }
            if("1".equals(fileMdfLastTimeKpi)){//文件未修改时长
                sb.append("LastMdfFileName=`"+FileCollConstants.LS_LT_FILE+" "+filePath+"| grep ^[^d] |awk '{print $9}' | "+grep_key+" sed -n '1p'`").append(FileCollConstants.TAB_LINE);
                sb.append("echo LastMdfFileName=$LastMdfFileName >> "+AgentProperties.AGENT_HOME+"/result_temp/"+schId).append(FileCollConstants.TAB_LINE);
            }
            sb.append(FileCollConstants.SPLIT_LINE.replaceFirst("#SCHEDULE_ID#",schId)).append(FileCollConstants.TAB_LINE);
        }
        sb.append(FileCollConstants.MV_CMD.replaceFirst("#SCHEDULE_ID#",schId));
        return sb.toString();
    }

	private String parseDateFilter(String dateFilter) {
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(dateFilter);
		return dateFormat.format(date);
	}

    /**
     * 通知执行脚本
     * 通知方式:写一个文件(文件名为采集脚本名称,如:getCpu.sh、getMem.sh)到notice目录,
     * contab会定时执行脚本扫描该目录,得到文件名,并执行脚本
     *
     * @throws
     * @since Ver 1.1
     */
    private void exec(String shellName){
        String noticeFileName = "../notice/busi/" + shellName;
        File file = null;

        try {
            file = new File(noticeFileName);
            if (file.exists()) {
                return;
            } else {
                file.createNewFile();
            }
        } catch (Exception e) {
            logger.error("Exception while exec(" + noticeFileName + ")", e);
        }
    }

    /**
     * 生成notice文件
     */
    private void writeNoticeFile(String shellName) {
        try {
            String noticeContent = getTmpNoticeContent(shellName);
            //guid作为requestid
            String guid = RandomGUID.getRandomGUID();
            String fileName = "notice_" + guid + ".sh";

            logger.info("[" + schId + ".sh"  + "]  Create TmpNoticeFile[" + fileName + "]");
            FileUtils.writeFile(TMP_NOTICE_DIR, fileName, noticeContent, false);

            logger.info("[" + schId + ".sh" + "]  Create NoticeFile[" + noticePath + fileName + "]");
            FileUtils.createNewFile(noticePath, fileName);
        } catch (Exception e) {
            logger.info("[" + schId + ".sh" + "]  Exception While Create NoticeFile", e);
        }
    }

    private String getTmpNoticeContent(String shellName){
        String content = FileCollConstants.NOTICE_CONTENT;
        content = content.replaceAll("#AGENT_HOME#", AgentProperties.AGENT_HOME);
        content = content.replaceAll("#AGENT_ID#", AgentProperties.AGENT_ID);
//        content = content.replaceAll("#REQUEST_ID#", props.getGuid());
        content = content.replaceAll("#SCHEDULE_ID#", schId);
//        //1.脚本测试 2.调度任务同步 3.脚本下发 4.附件下发 5.附件验证
//        content = content.replaceAll("#OPERATE_TYPE#", OPR_TYPE_SHELL);
//        content = content.replaceAll("#TRRIGGER_TYPE#", "AUTO");
//        content = content.replaceAll("#SEQ#", SEQ_1_NOTICE);
        content = content.replaceAll("#SHELL_NAME#", shellName);
        return content;
    }

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

        StringBuffer sb = new StringBuffer();
        // 超时时间:3分钟
        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("[File COLL] Timeout, exit.");
                break;
            }

            File[] filelist = file.listFiles();
            if (filelist == null || filelist.length == 0) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    logger.error("[File 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("[File COLL] Exception while readFile:" + resultFileName, e);
                } finally {
                    if (br != null) {
                        try {
                            br.close();
                        } catch (IOException e) {
                            logger.error("[File COLL] IOException:", e);
                        }
                    }
                    logger.info("[File 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<Map<String,String>> parseResult(List<String> result) {
        List<Map<String,String>> list = new ArrayList<Map<String,String>>();
        Map<String,String> map = new HashMap<String, String>();
        int i = 0;
        for (String line : result) {
            if (line.trim().equals(SPLIT_LINE)) {
                list.add(map);
                map = new HashMap<String, String>();
                i++;
                continue;
            }else if(line.trim().endsWith(SPLIT_LINE)){
                logger.info("LINE_DATA:"+line);
                line = line.replace(SPLIT_LINE,"");
                String[] vals = line.split("=");
                map.put(vals[0].trim(), vals.length>1?vals[1].trim():"");
                list.add(map);
            }else{
                logger.info("LINE_DATA:"+line);
                String[] vals = line.split("=");
                map.put(vals[0].trim(), vals.length>1?vals[1].trim():"");
            }
        }
        return list;
    }

	public static void main(String[] args) {
		HashMap<String, String> params = new HashMap<String, String>();
		
		params.put("KBPCLASS", "11-11-11");
		params.put("FILEPATH", "E:\\bnms6.0.0\\02 Design\\ArchDesign");
		params.put("DEVICENAME", "yf4(127.0.0.1)");
		params.put("KEY_FILTER", "bomc6.0");
		params.put("DATE_FILTER", "yyyyMMdd");
        params.put("FILECOLLPARAMS", "[{\"fileSortId\":\"fwqfewq\",\"fileKeyWord\":\"tt,11\",\"fileBackBlockTimeKpi\":1,\"fileBackBlockNumKpi\":1,\"blankFileNumKpi\":1,\"fileMdfLastTimeKpi\":1},{\"fileSortId\":\"fewqsdfew\",\"fileKeyWord\":\"11\",\"fileBackBlockTimeKpi\":0,\"fileBackBlockNumKpi\":1,\"blankFileNumKpi\":1,\"fileMdfLastTimeKpi\":0}]");
		
		new FileCollThread(params).run();
	}

}