📄 snmpadaptorserver.java
字号:
/* * @(#)file SnmpAdaptorServer.java * @(#)author Sun Microsystems, Inc. * @(#)version 4.99 * @(#)date 08/09/12 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * */package com.sun.jmx.snmp.daemon;// java imports//import java.util.Vector;import java.util.Enumeration;import java.net.DatagramSocket;import java.net.DatagramPacket;import java.net.InetAddress;import java.net.SocketException;import java.net.UnknownHostException;import java.io.ObjectInputStream;import java.io.IOException;import java.io.InterruptedIOException;// jmx imports//import javax.management.MBeanServer;import javax.management.MBeanRegistration;import javax.management.ObjectName;import javax.management.InstanceAlreadyExistsException;import com.sun.jmx.snmp.SnmpIpAddress;import com.sun.jmx.snmp.SnmpMessage;import com.sun.jmx.snmp.SnmpOid;import com.sun.jmx.snmp.SnmpPduFactory;import com.sun.jmx.snmp.SnmpPduPacket;import com.sun.jmx.snmp.SnmpPduRequest;import com.sun.jmx.snmp.SnmpPduTrap;import com.sun.jmx.snmp.SnmpTimeticks;import com.sun.jmx.snmp.SnmpVarBind;import com.sun.jmx.snmp.SnmpVarBindList;import com.sun.jmx.snmp.SnmpDefinitions;import com.sun.jmx.snmp.SnmpStatusException;import com.sun.jmx.snmp.SnmpTooBigException;import com.sun.jmx.snmp.InetAddressAcl;import com.sun.jmx.snmp.SnmpPeer;import com.sun.jmx.snmp.SnmpParameters;// SNMP Runtime imports//import com.sun.jmx.snmp.SnmpPduFactoryBER;import com.sun.jmx.snmp.agent.SnmpMibAgent;import com.sun.jmx.snmp.agent.SnmpMibHandler;import com.sun.jmx.snmp.agent.SnmpUserDataFactory;import com.sun.jmx.snmp.agent.SnmpErrorHandlerAgent;import com.sun.jmx.snmp.IPAcl.SnmpAcl;import com.sun.jmx.snmp.tasks.ThreadService;/** * Implements an adaptor on top of the SNMP protocol. * <P> * When this SNMP protocol adaptor is started it creates a datagram socket * and is able to receive requests and send traps or inform requests. * When it is stopped, the socket is closed and neither requests * and nor traps/inform request are processed. * <P> * The default port number of the socket is 161. This default value can be * changed by specifying a port number: * <UL> * <LI>in the object constructor</LI> * <LI>using the {@link com.sun.jmx.snmp.daemon.CommunicatorServer#setPort * setPort} method before starting the adaptor</LI> * </UL> * The default object name is defined by {@link * com.sun.jmx.snmp.ServiceName#DOMAIN com.sun.jmx.snmp.ServiceName.DOMAIN} * and {@link com.sun.jmx.snmp.ServiceName#SNMP_ADAPTOR_SERVER * com.sun.jmx.snmp.ServiceName.SNMP_ADAPTOR_SERVER}. * <P> * The SNMP protocol adaptor supports versions 1 and 2 of the SNMP protocol * in a stateless way: when it receives a v1 request, it replies with a v1 * response, when it receives a v2 request it replies with a v2 response. * <BR>The method {@link #snmpV1Trap snmpV1Trap} sends traps using SNMP v1 * format. * The method {@link #snmpV2Trap snmpV2Trap} sends traps using SNMP v2 format. * The method {@link #snmpInformRequest snmpInformRequest} sends inform * requests using SNMP v2 format. * <P> * To receive data packets, the SNMP protocol adaptor uses a buffer * which size can be configured using the property <CODE>bufferSize</CODE> * (default value is 1024). * Packets which do not fit into the buffer are rejected. * Increasing <CODE>bufferSize</CODE> allows the exchange of bigger packets. * However, the underlying networking system may impose a limit on the size * of UDP packets. * Packets which size exceed this limit will be rejected, no matter what * the value of <CODE>bufferSize</CODE> actually is. * <P> * An SNMP protocol adaptor may serve several managers concurrently. The * number of concurrent managers can be limited using the property * <CODE>maxActiveClientCount</CODE>. * <p> * The SNMP protocol adaptor specifies a default value (10) for the * <CODE>maxActiveClientCount</CODE> property. When the adaptor is stopped, * the active requests are interrupted and an error result is sent to * the managers. * <p><b>This API is a Sun Microsystems internal API and is subject * to change without notice.</b></p> */public class SnmpAdaptorServer extends CommunicatorServer implements SnmpAdaptorServerMBean, MBeanRegistration, SnmpDefinitions, SnmpMibHandler { // PRIVATE VARIABLES //------------------ /** * Port number for sending SNMP traps. * <BR>The default value is 162. */ private int trapPort = 162; /** * Port number for sending SNMP inform requests. * <BR>The default value is 162. */ private int informPort = 162; /** * The <CODE>InetAddress</CODE> used when creating the datagram socket. * <BR>It is specified when creating the SNMP protocol adaptor. * If not specified, the local host machine is used. */ InetAddress address = null; /** * The IP address based ACL used by this SNMP protocol adaptor. */ private Object ipacl = null; /** * The factory object. */ private SnmpPduFactory pduFactory = null; /** * The user-data factory object. */ private SnmpUserDataFactory userDataFactory = null; /** * Indicates if the SNMP protocol adaptor sends a response in case * of authentication failure */ private boolean authRespEnabled = true; /** * Indicates if authentication traps are enabled. */ private boolean authTrapEnabled = true; /** * The enterprise OID. * <BR>The default value is "1.3.6.1.4.1.42". */ private SnmpOid enterpriseOid = new SnmpOid("1.3.6.1.4.1.42"); /** * The buffer size of the SNMP protocol adaptor. * This buffer size is used for both incoming request and outgoing * inform requests. * <BR>The default value is 1024. */ int bufferSize = 1024; private transient long startUpTime = 0; private transient DatagramSocket socket = null; transient DatagramSocket trapSocket = null; private transient SnmpSession informSession = null; private transient DatagramPacket packet = null; transient Vector mibs = new Vector(); private transient SnmpMibTree root; /** * Whether ACL must be used. */ private transient boolean useAcl = true; // SENDING SNMP INFORMS STUFF //--------------------------- /** * Number of times to try an inform request before giving up. * The default number is 3. */ private int maxTries = 3 ; /** * The amount of time to wait for an inform response from the manager. * The default amount of time is 3000 millisec. */ private int timeout = 3 * 1000 ; // VARIABLES REQUIRED FOR IMPLEMENTING SNMP GROUP (MIBII) //------------------------------------------------------- /** * The <CODE>snmpOutTraps</CODE> value defined in MIB-II. */ int snmpOutTraps=0; /** * The <CODE>snmpOutGetResponses</CODE> value defined in MIB-II. */ private int snmpOutGetResponses=0; /** * The <CODE>snmpOutGenErrs</CODE> value defined in MIB-II. */ private int snmpOutGenErrs=0; /** * The <CODE>snmpOutBadValues</CODE> value defined in MIB-II. */ private int snmpOutBadValues=0; /** * The <CODE>snmpOutNoSuchNames</CODE> value defined in MIB-II. */ private int snmpOutNoSuchNames=0; /** * The <CODE>snmpOutTooBigs</CODE> value defined in MIB-II. */ private int snmpOutTooBigs=0; /** * The <CODE>snmpOutPkts</CODE> value defined in MIB-II. */ int snmpOutPkts=0; /** * The <CODE>snmpInASNParseErrs</CODE> value defined in MIB-II. */ private int snmpInASNParseErrs=0; /** * The <CODE>snmpInBadCommunityUses</CODE> value defined in MIB-II. */ private int snmpInBadCommunityUses=0; /** * The <CODE>snmpInBadCommunityNames</CODE> value defined in MIB-II. */ private int snmpInBadCommunityNames=0; /** * The <CODE>snmpInBadVersions</CODE> value defined in MIB-II. */ private int snmpInBadVersions=0; /** * The <CODE>snmpInGetRequests</CODE> value defined in MIB-II. */ private int snmpInGetRequests=0; /** * The <CODE>snmpInGetNexts</CODE> value defined in MIB-II. */ private int snmpInGetNexts=0; /** * The <CODE>snmpInSetRequests</CODE> value defined in MIB-II. */ private int snmpInSetRequests=0; /** * The <CODE>snmpInPkts</CODE> value defined in MIB-II. */ private int snmpInPkts=0; /** * The <CODE>snmpInTotalReqVars</CODE> value defined in MIB-II. */ private int snmpInTotalReqVars=0; /** * The <CODE>snmpInTotalSetVars</CODE> value defined in MIB-II. */ private int snmpInTotalSetVars=0; /** * The <CODE>snmpInTotalSetVars</CODE> value defined in rfc 1907 MIB-II. */ private int snmpSilentDrops=0; private static final String InterruptSysCallMsg = "Interrupted system call"; static final SnmpOid sysUpTimeOid = new SnmpOid("1.3.6.1.2.1.1.3.0") ; static final SnmpOid snmpTrapOidOid = new SnmpOid("1.3.6.1.6.3.1.1.4.1.0"); private ThreadService threadService; private static int threadNumber = 6; static { String s = System.getProperty("com.sun.jmx.snmp.threadnumber"); if (s != null) { try { threadNumber = Integer.parseInt(System.getProperty(s)); } catch (Exception e) { // ??? System.err.println("Got wrong value for " + "com.sun.jmx.snmp.threadnumber: "+s); System.err.println("Use the default value: "+threadNumber); } } } // PUBLIC CONSTRUCTORS //-------------------- /** * Initializes this SNMP protocol adaptor using the default port (161). * Use the {@link com.sun.jmx.snmp.IPAcl.SnmpAcl} default * implementation of the <CODE>InetAddressAcl</CODE> interface. */ public SnmpAdaptorServer() { this(true, null, com.sun.jmx.snmp.ServiceName.SNMP_ADAPTOR_PORT, null) ; } /** * Initializes this SNMP protocol adaptor using the specified port. * Use the {@link com.sun.jmx.snmp.IPAcl.SnmpAcl} default * implementation of the <CODE>InetAddressAcl</CODE> interface. * * @param port The port number for sending SNMP responses. */ public SnmpAdaptorServer(int port) { this(true, null, port, null) ; } /** * Initializes this SNMP protocol adaptor using the default port (161) * and the specified IP address based ACL implementation. * * @param acl The <CODE>InetAddressAcl</CODE> implementation. * <code>null</code> means no ACL - everybody is authorized. * * @since 1.5 */ public SnmpAdaptorServer(InetAddressAcl acl) { this(false, acl, com.sun.jmx.snmp.ServiceName.SNMP_ADAPTOR_PORT, null) ; } /** * Initializes this SNMP protocol adaptor using the default port (161) * and the * specified <CODE>InetAddress</CODE>. * Use the {@link com.sun.jmx.snmp.IPAcl.SnmpAcl} default * implementation of the <CODE>InetAddressAcl</CODE> interface. * * @param addr The IP address to bind. */ public SnmpAdaptorServer(InetAddress addr) { this(true, null, com.sun.jmx.snmp.ServiceName.SNMP_ADAPTOR_PORT, addr) ; } /** * Initializes this SNMP protocol adaptor using the specified port and the * specified IP address based ACL implementation. * * @param acl The <CODE>InetAddressAcl</CODE> implementation. * <code>null</code> means no ACL - everybody is authorized. * @param port The port number for sending SNMP responses. * * @since 1.5 */ public SnmpAdaptorServer(InetAddressAcl acl, int port) { this(false, acl, port, null) ; } /** * Initializes this SNMP protocol adaptor using the specified port and the * specified <CODE>InetAddress</CODE>. * Use the {@link com.sun.jmx.snmp.IPAcl.SnmpAcl} default * implementation of the <CODE>InetAddressAcl</CODE> interface. * * @param port The port number for sending SNMP responses. * @param addr The IP address to bind. */ public SnmpAdaptorServer(int port, InetAddress addr) { this(true, null, port, addr) ; } /** * Initializes this SNMP protocol adaptor using the specified IP * address based ACL implementation and the specified * <CODE>InetAddress</CODE>. * * @param acl The <CODE>InetAddressAcl</CODE> implementation.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -