⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eventutil.java

📁 opennms得相关源码 请大家看看
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc.  All rights reserved.// OpenNMS(R) is a derivative work, containing both original code, included code and modified// code that was published under the GNU General Public License. Copyrights for modified // and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.//// Modifications://// 2006 Jun 05: Added asset description and asset comment tags.// 2002 Nov 14: Added non-blocking I/O classes to speed capsd and pollers.// 2002 Nov 13: Added two new notification fields: nodelabel and interfaceresolve.//// Original code base Copyright (C) 1999-2001 Oculan Corp.  All rights reserved.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.                                                            //// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//       // For more information contact: //      OpenNMS Licensing       <license@opennms.org>//      http://www.opennms.org///      http://www.opennms.com///// Tab Size = 8//package org.opennms.netmgt.eventd;import java.math.BigInteger;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Date;import java.util.Enumeration;import java.text.DateFormat;import org.apache.log4j.Category;import org.opennms.core.utils.Base64;import org.opennms.core.utils.ThreadCategory;import org.opennms.netmgt.EventConstants;import org.opennms.netmgt.config.DatabaseConnectionFactory;import org.opennms.netmgt.xml.event.Event;import org.opennms.netmgt.xml.event.Parm;import org.opennms.netmgt.xml.event.Parms;import org.opennms.netmgt.xml.event.Snmp;import org.opennms.netmgt.xml.event.Value;import org.opennms.protocols.snmp.SnmpCounter64;import org.opennms.protocols.snmp.SnmpIPAddress;import org.opennms.protocols.snmp.SnmpInt32;import org.opennms.protocols.snmp.SnmpObjectId;import org.opennms.protocols.snmp.SnmpOctetString;import org.opennms.protocols.snmp.SnmpUInt32;/** * EventUtil is used primarily for the event parm expansion - has methods used * by all the event components to send in the event and the element to expanded * and have the 'expanded' value sent back *  * @author <A HREF="mailto:sowmya@opennms.org">Sowmya Kumaraswamy </A> * @author <A HREF="mailto:weave@oculan.com">Brain Weaver </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> */public final class EventUtil {	/**	 * The Event ID xml	 */	static final String TAG_EVENT_DB_ID = "eventid";	/**	 * The UEI xml tag	 */	static final String TAG_UEI = "uei";	/**	 * The event source xml tag	 */	static final String TAG_SOURCE = "source";	/**	* The event descr xml tag	*/	static final String TAG_DESCR = "descr";	/**	* The event logmsg xml tag	*/	static final String TAG_LOGMSG = "logmsg";	/**	 * The event time xml tag	 */	static final String TAG_TIME = "time";	/**	 * The event nodeid xml tag	 */	static final String TAG_NODEID = "nodeid";	/**	 * The event nodeid xml tag	 */	static final String TAG_NODELABEL = "nodelabel";	/**	 * The event host xml tag	 */	static final String TAG_HOST = "host";	/**	 * The event interface xml tag	 */	static final String TAG_INTERFACE = "interface";	/**	 * The reverse DNS lookup of the interface	 */	static final String TAG_INTERFACE_RESOLVE = "interfaceresolve";	/**	 * The reverse DNS lookup of the interface	 */	static final String TAG_IFALIAS = "ifalias";		/**	 * The event snmp id xml tag	 */	static final String TAG_SNMP_ID = "id";	/**	 * The SNMP xml tag	 */	static final String TAG_SNMP = "snmp";	/**	 * The event snmp idtext xml tag	 */	static final String TAG_SNMP_IDTEXT = "idtext";	/**	 * The event snmp version xml tag	 */	static final String TAG_SNMP_VERSION = "version";	/**	 * The event snmp specific xml tag	 */	static final String TAG_SNMP_SPECIFIC = "specific";	/**	 * The event snmp generic xml tag	 */	static final String TAG_SNMP_GENERIC = "generic";	/**	 * The event snmp community xml tag	 */	static final String TAG_SNMP_COMMUNITY = "community";	/**	 * The event snmp host xml tag	 */	static final String TAG_SNMPHOST = "snmphost";	/**	 * The event service xml tag	 */	static final String TAG_SERVICE = "service";	/**	 * The event severity xml tag	 */	static final String TAG_SEVERITY = "severity";	/**	 * The event operinstruct xml tag	 */	static final String TAG_OPERINSTR = "operinstruct";	/**	 * The event mouseovertext xml tag	 */	static final String TAG_MOUSEOVERTEXT = "mouseovertext";	/**	 * The asset description field	 */	static final String TAG_ASSET_DESCR = "assetdescr";	/**	 * The asset comment field	 */	static final String TAG_ASSET_COMMENT = "assetcomment";	/**	 * The '%' sign used to indicate parms to be expanded	 */	final static char PERCENT = '%';	/**	 * The string that should be expanded to a list of all parm names	 */	final static String PARMS_NAMES = "parm[names-all]";	/**	 * The string that should be expanded to a list of all parm values	 */	final static String PARMS_VALUES = "parm[values-all]";	/**	 * The string that should be expanded to a list of all parms	 */	final static String PARMS_ALL = "parm[all]";	/**	 * The string that starts the expansion for a parm - used to lookup values	 * of parameters by their names	 */	final static String PARM_BEGIN = "parm[";	/**	 * The length of PARM_BEGIN	 */	final static int PARM_BEGIN_LENGTH = 5;	/**	 * The string that should be expanded to the number of parms	 */	final static String NUM_PARMS_STR = "parm[##]";	/**	 * The string that starts a parm number - used to lookup values of	 * parameters by their position	 */	final static String PARM_NUM_PREFIX = "parm[#";	/**	 * The length of PARM_NUM_PREFIX	 */	final static int PARM_NUM_PREFIX_LENGTH = 6;	/**	 * The string that ends the expansion of a parm	 */	final static String PARM_END_SUFFIX = "]";	/**	 * For expansion of the '%parms[all]%' - the parm name and value are added	 * as delimiter separated list of ' <parmName>= <value>' strings	 */	final static char NAME_VAL_DELIM = '=';	/**	 */	final static char SPACE_DELIM = ' ';	/**	 * The values and the corresponding attributes of an element are added	 * delimited by ATTRIB_DELIM	 */	final static char ATTRIB_DELIM = ',';	/**	 * Converts the value of a parm ('Value') of the instance to a string	 */	public static String getValueAsString(Value pvalue) {		if (pvalue == null)			return null;		String result = "";		String encoding = pvalue.getEncoding();		Object value = pvalue.getContent();		if (encoding.equals("text")) {			if (value instanceof String)				result = (String) value;			else if (value instanceof Number)				result = value.toString();			else if (value instanceof SnmpInt32)				result = Integer.toString(((SnmpInt32) value).getValue());			else if (value instanceof SnmpUInt32)				result = Long.toString(((SnmpUInt32) value).getValue());			else if (value instanceof SnmpCounter64)				result = ((SnmpCounter64) value).getValue().toString();			else if (value instanceof SnmpIPAddress)				result = value.toString();			else if (value instanceof SnmpOctetString)				result = new String(((SnmpOctetString) value).getString());			else if (value instanceof SnmpObjectId)				result = value.toString();		} else if (encoding.equals("base64")) {			if (value instanceof String)				result = new String(Base64.encodeBase64(((String) value)						.getBytes()));			else if (value instanceof Number) {				byte[] ibuf = null;				if (value instanceof Short) {					ibuf = new byte[2];					ibuf[0] = (byte) ((((Number) value).shortValue() >> 8) & 0xff);					ibuf[1] = (byte) (((Number) value).shortValue() & 0xff);				} else if (value instanceof Integer) {					ibuf = new byte[4];					ibuf[0] = (byte) ((((Number) value).intValue() >> 24) & 0xff);					ibuf[1] = (byte) ((((Number) value).intValue() >> 16) & 0xff);					ibuf[2] = (byte) ((((Number) value).intValue() >> 8) & 0xff);					ibuf[3] = (byte) (((Number) value).intValue() & 0xff);				} else if (value instanceof Long) {					ibuf = new byte[8];					ibuf[0] = (byte) ((((Number) value).longValue() >> 56) & 0xffL);					ibuf[1] = (byte) ((((Number) value).longValue() >> 48) & 0xffL);					ibuf[2] = (byte) ((((Number) value).longValue() >> 40) & 0xffL);					ibuf[3] = (byte) ((((Number) value).longValue() >> 32) & 0xffL);					ibuf[4] = (byte) ((((Number) value).longValue() >> 24) & 0xffL);					ibuf[5] = (byte) ((((Number) value).longValue() >> 16) & 0xffL);					ibuf[6] = (byte) ((((Number) value).longValue() >> 8) & 0xffL);					ibuf[7] = (byte) (((Number) value).longValue() & 0xffL);				} else if (value instanceof BigInteger) {					ibuf = ((BigInteger) value).toByteArray();				}				result = new String(Base64.encodeBase64(ibuf));			} else if (value instanceof SnmpInt32) {				byte[] ibuf = new byte[4];				ibuf[0] = (byte) ((((SnmpInt32) value).getValue() >> 24) & 0xff);				ibuf[1] = (byte) ((((SnmpInt32) value).getValue() >> 16) & 0xff);				ibuf[2] = (byte) ((((SnmpInt32) value).getValue() >> 8) & 0xff);				ibuf[3] = (byte) (((SnmpInt32) value).getValue() & 0xff);				result = new String(Base64.encodeBase64(ibuf));			} else if (value instanceof SnmpUInt32) {				byte[] ibuf = new byte[4];				ibuf[0] = (byte) ((((SnmpUInt32) value).getValue() >> 24) & 0xffL);				ibuf[1] = (byte) ((((SnmpUInt32) value).getValue() >> 16) & 0xffL);				ibuf[2] = (byte) ((((SnmpUInt32) value).getValue() >> 8) & 0xffL);				ibuf[3] = (byte) (((SnmpUInt32) value).getValue() & 0xffL);				result = new String(Base64.encodeBase64(ibuf));			} else if (value instanceof SnmpCounter64) {				byte[] ibuf = ((SnmpCounter64) value).getValue().toByteArray();				result = new String(Base64.encodeBase64(ibuf));			} else if (value instanceof SnmpOctetString) {				result = new String(Base64						.encodeBase64(((SnmpOctetString) value).getString()));			} else if (value instanceof SnmpObjectId) {				result = new String(Base64.encodeBase64(value.toString()						.getBytes()));			}		}		return (result == null ? null : result.trim());	}	/**	 * <P>	 * This method is used to escape required values from strings that may	 * contain those values. If the passed string contains the passed value then	 * the character is reformatted into its <EM>%dd</EM> format.	 * </P>	 * 	 * 	 * @param inStr	 *            string that might contain the delimiter	 * @param delimchar	 *            delimiter to escape	 * 	 * @return The string with the delimiter escaped as in URLs	 * 	 * @see #ATTRIB_DELIM	 */	public static String escape(String inStr, char delimchar) {		// integer equivalent of the delimiter		int delim = delimchar;		// convert this to a '%<int>' string		String delimEscStr = "%" + String.valueOf(delim);		// the buffer to return		StringBuffer outBuffer = new StringBuffer(inStr);		int index = 0;		int delimIndex = inStr.indexOf(delimchar, index);		while (delimIndex != -1) {			// delete the delimiter and add the escape string			outBuffer.deleteCharAt(delimIndex);			outBuffer.insert(delimIndex, delimEscStr);			index = delimIndex + delimEscStr.length() + 1;			delimIndex = outBuffer.toString().indexOf(delimchar, index);		}		return outBuffer.toString();	}	/**	 * Get the value of the parm for the event	 * 	 * @param parm	 *            the parm for which value is needed from the event	 * @param event	 *            the event whose parm value is required	 * 	 * @return value of the event parm/element	 */	public static String getValueOfParm(String parm, Event event) {		String retParmVal = null;		if (parm.equals(TAG_UEI)) {			retParmVal = event.getUei();		}		if (parm.equals(TAG_EVENT_DB_ID)) {			if (event.hasDbid()) {				retParmVal = Integer.toString(event.getDbid());			} else {				retParmVal = "eventid-unknown";			}		} else if (parm.equals(TAG_SOURCE)) {			retParmVal = event.getSource();		} else if (parm.equals(TAG_DESCR)) {			retParmVal = event.getDescr();		} else if (parm.equals(TAG_LOGMSG)) {			retParmVal = event.getLogmsg().getContent();		} else if (parm.equals(TAG_NODEID)) {			retParmVal = Long.toString(event.getNodeid());		} else if (parm.equals(TAG_NODELABEL)) {			retParmVal = Long.toString(event.getNodeid());			String nodeLabel = null;			if (event.getNodeid() > 0) {				try {					nodeLabel = getNodeLabel(event.getNodeid());				} catch (SQLException sqlE) {					// do nothing				}			}			if (nodeLabel != null)				retParmVal = nodeLabel;			else				retParmVal = "Unknown";		} else if (parm.equals(TAG_TIME)) {			String eventTime = event.getTime(); //This will be in GMT			try {				Date actualDate = EventConstants.parseToDate(eventTime);				DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL,						DateFormat.FULL);				retParmVal = df.format(actualDate);			} catch (java.text.ParseException e) {				Category log = ThreadCategory.getInstance();				log.error("could not parse event date \"" + eventTime + "\": ",						e);				//Give up and just use the original string - don't bother with				// messing around				retParmVal = eventTime;			}		} else if (parm.equals(TAG_HOST)) {			retParmVal = event.getHost();		} else if (parm.equals(TAG_INTERFACE)) {			retParmVal = event.getInterface();		} else if (parm.equals(TAG_INTERFACE_RESOLVE)) {			retParmVal = event.getInterface();			try {				java.net.InetAddress inet = java.net.InetAddress						.getByName(retParmVal);				retParmVal = inet.getHostName();			} catch (java.net.UnknownHostException e) {			}		} else if (parm.equals(TAG_IFALIAS)) {			String ifAlias = null;			if (event.getNodeid() > 0					&& event.getInterface() != null) {				try {					ifAlias = getIfAlias(event.getNodeid(), event.getInterface());				} catch (SQLException sqlE) {					// do nothing					ThreadCategory.getInstance(EventUtil.class).info("ifAlias Unavailable for " + event.getNodeid() + ":" + event.getInterface(), sqlE);				}			}			if (ifAlias != null)				retParmVal = ifAlias;			else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -