snmptraphelper.java
来自「opennms得相关源码 请大家看看」· Java 代码 · 共 1,139 行 · 第 1/3 页
JAVA
1,139 行
//// 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.//// Copyright (C) 2003 Tavve Software Company. 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.scriptd.helper;import java.io.IOException;import java.net.InetAddress;import java.util.HashMap;import org.apache.log4j.Category;import org.opennms.core.utils.Base64;import org.opennms.core.utils.ThreadCategory;import org.opennms.netmgt.trapd.EventConstants;import org.opennms.netmgt.xml.event.Event;import org.opennms.netmgt.xml.event.Parm;import org.opennms.netmgt.xml.event.Snmp;import org.opennms.netmgt.xml.event.Value;import org.opennms.protocols.ip.IPv4Address;import org.opennms.protocols.snmp.SnmpBadConversionException;import org.opennms.protocols.snmp.SnmpCounter32;import org.opennms.protocols.snmp.SnmpCounter64;import org.opennms.protocols.snmp.SnmpGauge32;import org.opennms.protocols.snmp.SnmpIPAddress;import org.opennms.protocols.snmp.SnmpInt32;import org.opennms.protocols.snmp.SnmpNull;import org.opennms.protocols.snmp.SnmpObjectId;import org.opennms.protocols.snmp.SnmpOctetString;import org.opennms.protocols.snmp.SnmpOpaque;import org.opennms.protocols.snmp.SnmpParameters;import org.opennms.protocols.snmp.SnmpPduEncodingException;import org.opennms.protocols.snmp.SnmpPduPacket;import org.opennms.protocols.snmp.SnmpPduRequest;import org.opennms.protocols.snmp.SnmpPduTrap;import org.opennms.protocols.snmp.SnmpPeer;import org.opennms.protocols.snmp.SnmpTimeTicks;import org.opennms.protocols.snmp.SnmpTrapHandler;import org.opennms.protocols.snmp.SnmpTrapSession;import org.opennms.protocols.snmp.SnmpVarBind;import org.opennms.protocols.snmp.asn1.AsnEncodingException;/** * This "helper" class provides a convenience interface for generating and * forwarding SNMP traps. This class was created in order to make it easier to * write simple scripts to generate traps based on events or to forward traps, * using scripting languages that are able to access Java classes (such as * BeanShell). * * @author <a href="mailto:jim.doble@tavve.com">Jim Doble </a> * @author <a href="http://www.opennms.org/">OpenNMS.org </a> * */public class SnmpTrapHelper implements SnmpTrapHandler { /** * The sysUpTimeOID, which should be the first varbind in a V2 trap */ private static final String SNMP_SYSUPTIME_OID = ".1.3.6.1.2.1.1.3.0"; /** * The snmpTrapOID, which should be the second varbind in a V2 trap */ private static final String SNMP_TRAP_OID = ".1.3.6.1.6.3.1.1.4.1.0"; /** * The snmpTrapAddress, which may occur in a V2 trap */ private static final String SNMP_TRAP_ADDRESS_OID = ".1.3.6.1.6.3.18.1.3.0"; /** * The snmpTrapCommunity, which may occur in a V2 trap */ private static final String SNMP_TRAP_COMMUNITY_OID = ".1.3.6.1.6.3.18.1.4.0"; /** * The snmp trap enterprise OID, which if present in a V2 trap is the last * varbind */ private static final String SNMP_TRAP_ENTERPRISE_OID = ".1.3.6.1.6.3.1.1.4.3.0"; /** * OID prefix for generic SNMP traps */ private static final String SNMP_TRAPS = ".1.3.6.1.6.3.1.1.5"; /** * The SNMP generic value for an enterprise-specific trap */ private static final int ENTERPRISE_SPECIFIC = 6; /** * Map of factories for generating different types of SNMP variable binding * content */ private HashMap m_factoryMap; /** * Trap session for sending traps */ private SnmpTrapSession m_trapSession; /** * Constructs a new SNMPTrapHelper. */ public SnmpTrapHelper() { // create the trap session try { // The port -1 tells SnmpPortal to find any unused port on // the system. m_trapSession = new SnmpTrapSession(this, -1); } catch (Exception e) { Category log = ThreadCategory.getInstance(SnmpTrapHelper.class); log.error("SnmpTrapHelper failed to open trap session: " + e.getMessage()); } // create and populate the factory map m_factoryMap = new HashMap(); m_factoryMap.put(EventConstants.TYPE_SNMP_OCTET_STRING, new SnmpOctetStringFactory()); m_factoryMap.put(EventConstants.TYPE_SNMP_INT32, new SnmpInt32Factory()); m_factoryMap.put(EventConstants.TYPE_SNMP_NULL, new SnmpNullFactory()); m_factoryMap.put(EventConstants.TYPE_SNMP_OBJECT_IDENTIFIER, new SnmpObjectIdFactory()); m_factoryMap.put(EventConstants.TYPE_SNMP_IPADDRESS, new SnmpIPAddressFactory()); m_factoryMap.put(EventConstants.TYPE_SNMP_TIMETICKS, new SnmpTimeTicksFactory()); m_factoryMap.put(EventConstants.TYPE_SNMP_COUNTER32, new SnmpCounter32Factory()); m_factoryMap.put(EventConstants.TYPE_SNMP_GAUGE32, new SnmpGauge32Factory()); m_factoryMap.put(EventConstants.TYPE_SNMP_OPAQUE, new SnmpOpaqueFactory()); m_factoryMap.put(EventConstants.TYPE_SNMP_COUNTER64, new SnmpCounter64Factory()); } /** * Stops the SnmpTrapHelper. If there is a valid SnmpTrapSession, that trap * session is stopped. */ public void stop() { if (m_trapSession != null) { m_trapSession.close(); } } // BEGIN: Implement SnmpTrapHandler public void snmpReceivedTrap(SnmpTrapSession session, InetAddress agent, int port, SnmpOctetString community, SnmpPduPacket pdu) { Category log = ThreadCategory.getInstance(SnmpTrapHelper.class); log.error("SnmpTrapHelper received unexpected trap"); } public void snmpReceivedTrap(SnmpTrapSession session, InetAddress agent, int port, SnmpOctetString community, SnmpPduTrap pdu) { Category log = ThreadCategory.getInstance(SnmpTrapHelper.class); log.error("SnmpTrapHelper received unexpected trap"); } public void snmpTrapSessionError(SnmpTrapSession session, int error, Object ref) { Category log = ThreadCategory.getInstance(SnmpTrapHelper.class); log.error("Trap session error in SnmpTrapHelper"); } // END: Implement SnmpTrapHandler /** * Common interface for all variabe binding factories */ private interface VarBindFactory { /** * Constructs a new SnmpVarBind with the specified name and value. The * value is assumed to have been encoded with the specified encoding * (i.e. XML_ENCODING_TEXT, or XML_ENCODING_BASE64). * * @param name * The name (a.k.a. "id") of the variable binding to be * created * @param encoding * Describes the way in which the value content has been * encoded (i.e. XML_ENCODING_TEXT, or XML_ENCODING_BASE64) * @param value * The variable binding value * * @return The newly-created variable binding * @exception Throws * SnmpTrapHelperException if the variable binding cannot * be created for any reason (e.g. encoding not * supported, invalid value, etc.). */ public SnmpVarBind getVarBind(String name, String encoding, String value) throws SnmpTrapHelperException; } /** * Variable binding factory for SnmpOctetString */ private class SnmpOctetStringFactory implements VarBindFactory { /** * Constructs a new SnmpVarBind with the specified name and value. The * value will be encoded as an SnmpOctetString. The value is assumed to * have been encoded with the specified encoding (i.e. * XML_ENCODING_TEXT, or XML_ENCODING_BASE64). * * @param name * The name (a.k.a. "id") of the variable binding to be * created * @param encoding * Describes the way in which the value content has been * encoded (i.e. XML_ENCODING_TEXT, or XML_ENCODING_BASE64) * @param value * The variable binding value * * @return The newly-created variable binding * @exception Throws * SnmpTrapHelperException if the variable binding cannot * be created for any reason (e.g. encoding not * supported, invalid value, etc.). */ public SnmpVarBind getVarBind(String name, String encoding, String value) throws SnmpTrapHelperException { if (EventConstants.XML_ENCODING_TEXT.equals(encoding)) { return new SnmpVarBind(name, new SnmpOctetString(value.getBytes())); } else if (EventConstants.XML_ENCODING_BASE64.equals(encoding)) { return new SnmpVarBind(name, new SnmpOctetString(Base64.decodeBase64(value.toCharArray()))); } else { throw new SnmpTrapHelperException("Encoding " + encoding + "is invalid for SnmpOctetString"); } } } /** * Variable binding factory for SnmpInt32 */ private class SnmpInt32Factory implements VarBindFactory { /** * Constructs a new SnmpVarBind with the specified name and value. The * value will be encoded as an SnmpInt32. The value is assumed to have * been encoded with the specified encoding (only XML_ENCODING_TEXT is * supported). * * @param name * The name (a.k.a. "id") of the variable binding to be * created * @param encoding * Describes the way in which the value content has been * encoded (i.e. XML_ENCODING_TEXT, or XML_ENCODING_BASE64) * @param value * The variable binding value * * @return The newly-created variable binding * @exception Throws * SnmpTrapHelperException if the variable binding cannot * be created for any reason (e.g. encoding not * supported, invalid value, etc.). */ public SnmpVarBind getVarBind(String name, String encoding, String value) throws SnmpTrapHelperException { if (EventConstants.XML_ENCODING_TEXT.equals(encoding)) { try { return new SnmpVarBind(name, new SnmpInt32(value)); } catch (NumberFormatException e) { throw new SnmpTrapHelperException("Value " + value + "is invalid for SnmpInt32"); } catch (NullPointerException e) { throw new SnmpTrapHelperException("Value is null for SnmpInt32"); } } else { throw new SnmpTrapHelperException("Encoding " + encoding + "is invalid for SnmpInt32"); } } } /** * Variable binding factory for SnmpNull */ private class SnmpNullFactory implements VarBindFactory { /** * Constructs a new SnmpVarBind with the specified name and value. The * value will be encoded as an SnmpNull.The value and encoding * parameters are ignored. * * @param name * The name (a.k.a. "id") of the variable binding to be * created * @param encoding * This parameter value is ignored. * @param value * This parameter value is ignored. * * @return The newly-created variable binding */ public SnmpVarBind getVarBind(String name, String encoding, String value) { return new SnmpVarBind(name, new SnmpNull()); } } /** * Variable binding factory for SnmpObjectId */ private class SnmpObjectIdFactory implements VarBindFactory { /** * Constructs a new SnmpVarBind with the specified name and value. The * value will be encoded as an SnmpObjectId. The value is assumed to * have been encoded with the specified encoding (only XML_ENCODING_TEXT * is supported). * * @param name * The name (a.k.a. "id") of the variable binding to be * created * @param encoding * Describes the way in which the value content has been * encoded (i.e. XML_ENCODING_TEXT, or XML_ENCODING_BASE64) * @param value * The variable binding value * * @return The newly-created variable binding * @exception Throws * SnmpTrapHelperException if the variable binding cannot * be created for any reason (e.g. encoding not * supported, invalid value, etc.). */ public SnmpVarBind getVarBind(String name, String encoding, String value) throws SnmpTrapHelperException { if (EventConstants.XML_ENCODING_TEXT.equals(encoding)) { return new SnmpVarBind(name, new SnmpObjectId(value)); } else { throw new SnmpTrapHelperException("Encoding " + encoding + "is invalid for SnmpObjectId"); } } } /** * Variable binding factory for SnmpIPAddress */ private class SnmpIPAddressFactory implements VarBindFactory { /** * Constructs a new SnmpVarBind with the specified name and value. The * value will be encoded as an SnmpIPAddress. The value is assumed to * have been encoded with the specified encoding (only XML_ENCODING_TEXT * is supported). * * @param name * The name (a.k.a. "id") of the variable binding to be * created * @param encoding * Describes the way in which the value content has been * encoded (i.e. XML_ENCODING_TEXT, or XML_ENCODING_BASE64) * @param value * The variable binding value
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?