AnalysisRule.java 4.54 KB
package com.sitech.ismp.coll.busi.e2e.rule;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.log4j.Logger;

import bsh.Interpreter;

import com.sitech.base.AgentProperties;
import com.sitech.base.Config;
import com.sitech.database.dao.TbCfgEventDao;
import com.sitech.database.domain.TbCfgEvent;
import com.sitech.ismp.app.event.ExpressionHelper;
import com.sitech.ismp.coll.busi.e2e.dao.TbE2eFile2dbFinishTagDao;
import com.sitech.ismp.coll.busi.e2e.dao.TbE2eProcessFinishTagDao;
import com.sitech.ismp.coll.busi.e2e.domain.TbE2eFile2dbFinishTag;
import com.sitech.ismp.messageObject.AlarmEvent;
import com.sitech.util.RandomGUID;
import com.sitech.util.mq.TunnelFactory;

/**
 * ClassName:AnalysisRule 业务分析规则抽象类
 * 
 * @author linxc
 * @version
 * @since Ver 1.1
 * @Date 2012 May 11, 2012 2:59:55 PM
 */
public abstract class AnalysisRule implements Runnable {
	protected static int CRM_ORDER = 1;
	
	protected static int ZQYZS = 21;
	
	
	protected Logger logger = Logger.getLogger("BUSI_COLL");
	protected Logger error = Logger.getLogger("ERROR");
	protected Map<String, String> params;

	protected String kbpClass;
	protected String productType;

	protected TbE2eFile2dbFinishTagDao fileFinishTagdao = null;
	protected TbE2eProcessFinishTagDao processTagDao = null;
	
	protected List<TbCfgEvent> eventConfigList = null;
	
	public AnalysisRule(Map<String, String> params) {
		this.params = params;
		this.kbpClass = params.get("KBP_CLASS");
		this.productType = params.get("TAG_TYPE");
		this.fileFinishTagdao = new TbE2eFile2dbFinishTagDao(params);
		this.processTagDao = new TbE2eProcessFinishTagDao(params);
	}

	public void run() {
		TbCfgEventDao eventConfigdao = new TbCfgEventDao();
		eventConfigList = eventConfigdao.queryEventCfgByUnitId(kbpClass);
		excute();
	}

	protected abstract void excute();

	/**
	 * 根据文件类型和处理规则查询采集入库的文件
	 */
	protected List<TbE2eFile2dbFinishTag> getTbE2eFile2dbFinishTag(
			int fileType, int ruleType) {
		List<TbE2eFile2dbFinishTag> tagList = fileFinishTagdao
				.selectTbE2eFile2dbFinishTag(fileType, ruleType);
		
		return tagList;
	}
	
	protected boolean kpiToEvent(String unitId, String kpiId, String kpiValue) {
		return kpiToEvent(unitId, kpiId, kpiValue, 1, "");
	}
	
	protected boolean kpiToEvent(String unitId, String kpiId, String kpiValue, int eventType, String extEventInfo) {
		try {
			TbCfgEvent config = ExpressionHelper.getMostMatchExpression(unitId,
					kpiId, eventConfigList);

			// 没有匹配的告警配置
			if (config == null) {
				return false;
			}

			Vector<String> expressionSet = new Vector<String>();
			expressionSet.add(config.getEXPRESSION1());
			expressionSet.add(config.getEXPRESSION2());
			expressionSet.add(config.getEXPRESSION3());
			expressionSet.add(config.getEXPRESSION4());
			expressionSet.add(config.getEXPRESSION5());

			for (int i = 0; i < expressionSet.size(); i++) {
				String EXPRESSION = (String) expressionSet.get(i);
				if (EXPRESSION == null || EXPRESSION.trim().equals("")) {
					continue;
				}

				Interpreter interpreter = new Interpreter();
				interpreter.set("UNIT_ID", unitId);
				interpreter.set("KPI_VALUE", kpiValue);
				interpreter.set("KPI_ID", kpiId);
				interpreter.eval("result=" + EXPRESSION);
				Boolean bool = (Boolean) interpreter.get("result");

				if (bool.booleanValue()) { // 条件成立,生成新事件, 设置告警字段
					AlarmEvent event = new AlarmEvent();
			
					event.setAgentId(AgentProperties.AGENT_ID);
					event.setEventId(RandomGUID.getRandomGUID());
					event.setCfgExpression(EXPRESSION);
					event.setCfgGuid(config.getGUID());
					event.setCllTime(new Date());
					event.setGenerantTime(new Date());
					event.setEventClass((short) getEventLevel(i));
					event.setEventTitle(unitId + "的" + kpiId + "值为: "
							+ kpiValue);
					event.setKpiId(kpiId);
					event.setKpiValue(kpiValue);
					event.setUnitId(unitId);
					// 告警类型 1: 告警, 2:预警
					// agent生成的全部为告警
					event.setEventType(eventType);
					
					event.setExtEventInfo(extEventInfo);
					
					sendEvent2Workstation(event);

					return true;
				} // end if
			} // end for


		} catch (Exception e) {
			logger.error("Exception while kpiToEvent", e);
		}
		
		return false;
	}
	
	private short getEventLevel(int i) { // 根据序号产生告警级别
		return (short) (i + 1);
	}
	
	public boolean sendEvent2Workstation(AlarmEvent event) throws Exception {
		TunnelFactory.getTunnel(Config.Q_WORSTATION).writeData(event);
		return true;
	}
}