CollBussinessDirWithCMD.java 6.75 KB
package com.sitech.ismp.coll.centercoll;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.upload.RomoteController;
import com.sitech.util.upload.SSHThread;
import com.sitech.util.upload.TelnetThread;

public class CollBussinessDirWithCMD {

	private Logger logger = Logger.getLogger("COLL");
	private Vector result = new Vector();
	RomoteController tt = null;
	private String IP_ADDR = "";
	private String KBP_CLASS = "";

	/**
	 * 
	 * @param params
	 * @return
	 */
	public Vector collDirOverstock(HashMap params) {
		// TODO Auto-generated method stub
		try {
			String ip = (String) params.get("IP_ADDR");
			String pre_unit_id = (String) params.get("PRE_UNIT_ID");
			String dir_desc = (String) params.get("DIR_DESC");
			String dir_path = (String) params.get("DIR_PATH");
			String alarm_bonnet = (String) params.get("ALARM_BONNET");

			logger.info("DIROVER_PARAM_PRE:=" + ip + "," + pre_unit_id + ","
					+ dir_desc + "," + "dir_path=" + dir_path + ","
					+ alarm_bonnet + "," + alarm_bonnet);

			this.result.clear();
			init(params);
			logger.info("DIROVER_PARAM:=" + ip + "," + pre_unit_id + ","
					+ dir_desc);

			int alarm_bonnet_count = 0;
			if (alarm_bonnet != null && !alarm_bonnet.equals("")
					&& !alarm_bonnet.equals("null")) {
				alarm_bonnet_count = new Integer(alarm_bonnet).intValue();
			}

			String dir_path_pre = dir_path.replaceAll("/", "-");
			if (dir_path_pre.startsWith("-")) {
				dir_path_pre = dir_path_pre.substring(1, dir_path_pre.length());
			}
			if (dir_path_pre.endsWith("-")) {
				dir_path_pre = dir_path_pre.substring(0,
						dir_path_pre.length() - 1);
			}
			String pro_PRE_UNITID = pre_unit_id + ":" + dir_path_pre;
			logger.info("testsun-hostname+15=" + pro_PRE_UNITID);

			setResult(pro_PRE_UNITID, "CM-01-50-999-11", dir_path);
			setResult(pro_PRE_UNITID, "CM-01-10-11-005", dir_desc);

			String unit_process = "ls -l " + dir_path + " | wc -l ";

			Vector result = tt.getResultAsVector(unit_process);
			int result_size = Integer.parseInt(((String) result.get(0)).trim()) - 1;
			setResult(pro_PRE_UNITID, "PM-01-50-10-12", String
					.valueOf(result_size));
			if (result_size <= alarm_bonnet_count) {

				setResult(pro_PRE_UNITID, "FM-01-04-004-01", "natural");
			} else {

				setResult(pro_PRE_UNITID, "FM-01-04-004-01", "abnormal");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			release();
		}
		logger.info("end collDirOverstock : " + params.get("DIR_PATH"));
		logger.info("resut_size=" + this.result.size());
		return this.result;

	}

	/**
	 * @param params
	 * @return
	 */
	public Vector collLogContext(HashMap params) {
		// TODO Auto-generated method stub
		try {
			String ip = (String) params.get("IP_ADDR");
			String pre_unit_id = (String) params.get("PRE_UNIT_ID");
			String log_desc = (String) params.get("LOG_DESC");
			String log_path = (String) params.get("LOG_PATH");
			String date_format = (String) params.get("DATE_FORMAT");
			String key_str = (String) params.get("KEY_STR");

			logger.info("LOGCONTEXT_PARAM_PRE:=" + ip + "," + pre_unit_id + ","
					+ log_desc + "," + "log_path=" + log_path);

			this.result.clear();
			init(params);
			logger.info("DIROVER_PARAM:=" + ip + "," + pre_unit_id + ","
					+ log_desc);
			String log_path_pre = log_path.replaceAll("/", "-") + "_" + key_str;
			log_path_pre.replaceAll("grep ", "");
			log_path_pre.replaceAll("/", "-");
			char a = '"';
			log_path_pre = log_path_pre.replaceAll(String.valueOf(a), "");
			if (log_path_pre.startsWith("-")) {
				log_path_pre = log_path_pre.substring(1, log_path_pre.length());
			}
			if (log_path_pre.endsWith("-")) {
				log_path_pre = log_path_pre.substring(0,
						log_path_pre.length() - 1);
			}
			String pro_PRE_UNITID = pre_unit_id + ":" + log_path_pre;
			logger.info("testsun-hostname+15=" + pro_PRE_UNITID);

			setResult(pro_PRE_UNITID, "CM-01-10-11-005", log_desc);
			if (key_str == null || key_str.equals("")) {
				key_str = "�޹��˹ؼ���";
			}
			setResult(pro_PRE_UNITID, "CM-01-10-999-14", key_str);

			String newLog_path = log_path;
			String[] date_formats = this.splitToSS(date_format, "|");
			for (int i = 0; i < date_formats.length; i++) {
				SimpleDateFormat formatter = new java.text.SimpleDateFormat(
						date_formats[i]);
				Date curdate = new java.util.Date();
				String curr = formatter.format(curdate);
				newLog_path = newLog_path.replaceAll(date_formats[i], curr);
			}

			setResult(pro_PRE_UNITID, "CM-01-50-999-11", newLog_path);

			String unit_process = "cat " + newLog_path + "| " + key_str
					+ " | wc -l ";
			Vector result = tt.getResultAsVector(unit_process);
			int result_size = Integer.parseInt(((String) result.get(0)).trim());
			setResult(pro_PRE_UNITID, "PM-01-50-10-11", String
					.valueOf(result_size));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			release();
		}
		logger.info("end collLogContext : " + params.get("LOG_PATH"));
		logger.info("resut_size=" + this.result.size());
		return this.result;

	}

	public static String[] splitToSS(String str, String sp) {
		ArrayList al = split(str, sp);

		Object[] os = al.toArray();
		String[] ss = new String[os.length];
		for (int i = 0; i < os.length; i++) {
			ss[i] = (String) os[i];
		}
		return ss;
	}

	public static ArrayList split(String str, String sp) {
		int ip = 0;
		ArrayList al = new ArrayList();
		ip = str.indexOf(sp);
		if (ip != -1) {
			al.add(str.substring(0, ip));
			str = str.substring(ip + sp.length());
			if (str.length() != 0) {
				al.addAll(split(str, sp));
			}
		} else {
			al.add(str);
		}
		return al;
	}

	public void init(HashMap params) {
		this.IP_ADDR = (String) params.get("IP_ADDR");
		this.KBP_CLASS = (String) params.get("PRE_UNIT_ID");
		String user = (String) params.get("USERNAME");
		String passwd = (String) params.get("PASSWORD");
		String protol = (String) params.get("PROTOCOL");
		if (protol != null && protol.equals("telnet")) {
			tt = new TelnetThread(IP_ADDR, 23, user, passwd);
		} else {
			tt = new SSHThread(IP_ADDR, 22, user, passwd);
		}
		try {
			tt.initial();
			// isConnect = true;
		} catch (Exception e) {
			// isConnect = false;
			e.printStackTrace();
		}
	}

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

	private void setResult(String unitID, String kpiID, String value) {
		try {
			TblATO_KPIDETAIL record;
			record = new TblATO_KPIDETAIL();
			record.setCLL_TIME(new java.util.Date());
			record.setKPI_ID(kpiID);
			record.setKPI_VALUE(value);
			record.setUNIT_ID(unitID);
			record.setCLL_TIME(new Date());
			result.add(record);
			System.out.println("add record=" + record.KPI_VALUE);

		} catch (Exception e) {
			logger.error(e);
		}
	}
}