ifxtable.java

来自「opennms得相关源码 请大家看看」· Java 代码 · 共 642 行 · 第 1/2 页

JAVA
642
字号
//// 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://// 2003 Jan 31: Cleaned up some unused imports.//// 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//// IfXTable.java,v 1.1.1.1 2001/11/11 17:34:36 ben Exp//package org.opennms.netmgt.capsd.snmp;import java.util.ArrayList;import java.util.List;import org.apache.log4j.Category;import org.opennms.core.utils.ThreadCategory;import org.opennms.netmgt.utils.Signaler;import org.opennms.protocols.snmp.SnmpHandler;import org.opennms.protocols.snmp.SnmpInt32;import org.opennms.protocols.snmp.SnmpObjectId;import org.opennms.protocols.snmp.SnmpPduBulk;import org.opennms.protocols.snmp.SnmpPduPacket;import org.opennms.protocols.snmp.SnmpPduRequest;import org.opennms.protocols.snmp.SnmpSMI;import org.opennms.protocols.snmp.SnmpSession;import org.opennms.protocols.snmp.SnmpSyntax;import org.opennms.protocols.snmp.SnmpVarBind;/** * <P> * The IfXTable uses a SnmpSession to collect the entries in the remote agent's * interface extensions table. It implements the SnmpHandler to receive * notifications and handle errors associated with the data collection. Data is * collected using a series of GETNEXT PDU request to walk multiple parts of the * interface table at once. The number of SNMP packets should not exceed the * number of interface + 1, assuming no lost packets or error conditions occur. * </P> *  * @author <A HREF="mailto:mike@opennms.org">Mike </A> * @author <A HREF="mailto:weave@oculan.com">Weave </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> *  * @version 1.1.1.1 *  * @see <A HREF="http://www.ietf.org/rfc/rfc2233.txt">RFC2233 </A> */public final class IfXTable implements SnmpHandler {    //    // Used to convert decimal to hex    //    private static final char[] m_hexDigit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };    /**     * <P>     * The list of interfaces from the remote's interface extensions table. The     * list contains a set of IfXTableEntry objects that were collected from the     * remote host.     * </P>     *      * @see IfXTableEntry     */    private List m_entries;    /**     * <P>     * Flag indicating if query was successful or if the collection failed.     * </P>     */    private boolean m_error;    /**     * <P>     * Used to synchronize the class to ensure that the session has finished     * collecting data before the value of success or failure is set, and     * control is returned to the caller.     * </P>     */    private Signaler m_signal;    /**     * <P>     * Used to generate the proper command for fetching the SNMP data from the     * agent (via GETBULK for version 2 or GETNEXT for version 1.     * </P>     */    private int m_version;    /**     * <P>     * The request id associated with the GetNext PDU generated to retrieve the     * number of interfaces associated with the remote host.     * </P>     */    private int m_ifNumberRequestId;    /**     * <P>     * This will be the OID where the information should cut off from the return     * packet from the GETBULK command.     * </P>     */    private SnmpObjectId m_stopAt = null;    /**     * <P>     * Used for storing the ifNumber variable from the MIB, the number of     * interfaces a device possesses.     * </P>     */    private int m_ifNumber;    /**     * <P>     * Used as a temporary storage space for all the data collected from the     * SNMP response packets for SNMP v2. After receiving all the data, the     * information will be sorted so that it mimics the SNMP v1 data storage;     * one map per interface containing all the necessary MIB values.     * </P>     */    private SnmpVarBind[] tempStorage = null;    /**     * <P>     * For SNMPv1 used to keep track of the number of SNMP response packets     * received.     *      * For SNMPv2 used to keep track of the total number of varbinds received in     * SNMP response packets.     *      * For both SNMPv1 and SNMPv2 this value is used to determine when all the     * necessary SNMP data has been retrieved.     * </P>     */    private int m_responses = 0;    /**     * <P>     * The default constructor is marked as private and will always throw an     * exception. This is done to disallow the constructor to be called. Since     * any instance of this class must have an SnmpSession and Signaler to     * properly work, the correct constructor must be used.     * </P>     *      * @exception java.lang.UnsupportedOperationException     *                Always thrown from this method since it is not supported.     *      * @see #IfXTable(SnmpSession, Signaler)     *      */    private IfXTable() throws UnsupportedOperationException {        throw new UnsupportedOperationException("Default constructor not supported!");    }    /**     * <P>     * Constructs an IfXTable object that is used to collect the interface     * elements from the remote agent. Once all the interfaces are collected, or     * there is an error in the collection the signaler object is <EM>notified     * </EM> to inform other threads.     * </P>     *      * @param session     *            The session with the remote agent.     * @param signaler     *            The object to notify waiters.     *      * @see IfXTableEntry     */    public IfXTable(SnmpSession session, Signaler signaler) {        m_signal = signaler;        m_entries = new ArrayList(2); // not synchronized.        m_error = false;        m_version = SnmpSMI.SNMPV1;        m_stopAt = IfXTableEntry.stop_oid();        // first process, attain ifNumber.        SnmpPduRequest pdu = IfXTableEntry.getIfNumberPdu();        m_ifNumberRequestId = pdu.getRequestId();        ThreadCategory.getInstance(getClass()).debug("IfXTable: ifNumber retrieval pdu request id: " + m_ifNumberRequestId);        session.send(pdu, this);    }    /**     * <P>     * Constructs an IfXTable object that is used to collect the interface     * elements from the remote agent. Once all the interfaces are collected, or     * there is an error in the collection the signaler object is <EM>notified     * </EM> to inform other threads.     * </P>     *      * @param session     *            The session with the remote agent.     * @param signaler     *            The object to notify waiters.     * @param version     *            SNMP version to use     *      * @see IfXTableEntry     */    public IfXTable(SnmpSession session, Signaler signaler, int version) {        m_signal = signaler;        m_entries = new ArrayList(2); // not synchronized.        m_error = false;        m_version = version;        m_stopAt = IfXTableEntry.stop_oid();        // first process, attain ifNumber.        SnmpPduRequest pdu = IfXTableEntry.getIfNumberPdu();        m_ifNumberRequestId = pdu.getRequestId();        ThreadCategory.getInstance(getClass()).debug("IfXTable: ifNumber retrieval pdu request id: " + m_ifNumberRequestId);        session.send(pdu, this);    }    /**     * <P>     * Returns the success or failure code for collection of the data.     * </P>     */    public boolean failed() {        return m_error;    }    /**     * <P>     * Returns the list of entry maps that can be used to access all the     * information about the interface extensions table.     * </P>     *      * @return The list of ifXTableEntry maps.     */    public List getEntries() {        return m_entries;    }    /**     * <P>     * This method is used to process received SNMP PDU packets from the remote     * agent. The method is part of the SnmpHandler interface and will be     * invoked when a PDU is successfully decoded. The method is passed the     * receiving session, the PDU command, and the actual PDU packet.     * </P>     *      * <P>     * When all the data has been received from the session the signaler object,     * initialized in the constructor, is signaled. In addition, the receiving     * instance will call notifyAll() on itself at the same time.     * </P>     *      * <P>     * For SNMP version 2 devices, all the received data enters a temporary     * array. After the collecting process, the method sorts the data so that     * each interface has its own map.     * </P>     *      * @param session     *            The SNMP Session that received the PDU     * @param command     *            The command contained in the received pdu     * @param pdu     *            The actual received PDU.     *      */    public void snmpReceivedPdu(SnmpSession session, int command, SnmpPduPacket pdu) {        boolean doNotify = true;        // lookup the category        //        Category log = ThreadCategory.getInstance(getClass());        if (command != SnmpPduPacket.RESPONSE) {            m_error = true;        } else {            //            // Check for error stored in request pdu            //            int errStatus = ((SnmpPduRequest) pdu).getErrorStatus();            if (errStatus != SnmpPduPacket.ErrNoError) {                m_error = true;            } else {                // Is this the response to our request to retrieve ifNumber?                // If so, begin gathering all the MIB data for the device                if (pdu.getRequestId() == m_ifNumberRequestId) {                    // Check for v2 error in varbind                    SnmpVarBind vb = pdu.getVarBindAt(0);                    SnmpObjectId ifNumberOid = new SnmpObjectId(".1.3.6.1.2.1.2.1");                    if (!ifNumberOid.isRootOf(vb.getName())) {                        m_error = true;

⌨️ 快捷键说明

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