ipaddrtable.java
来自「opennms得相关源码 请大家看看」· Java 代码 · 共 670 行 · 第 1/2 页
JAVA
670 行
//// 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 Sep 29: Modifications to allow for OpenNMS to handle duplicate IP Addresses.// 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//package org.opennms.netmgt.capsd.snmp;import java.net.InetAddress;import java.util.ArrayList;import java.util.Iterator;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.SnmpBadConversionException;import org.opennms.protocols.snmp.SnmpHandler;import org.opennms.protocols.snmp.SnmpIPAddress;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> * IpAddrTable uses a SnmpSession to collect the ipAddrTable entries It * implements the SnmpHandler to receive notifications when a reply is * received/error occurs in the SnmpSession used to send requests /recieve * replies. * </P> * * @author <A HREF="mailto:jamesz@opennms.org">James Zuo </A> * @author <A HREF="mailto:sowmya@opennms.org">Sowmya </A> * @author <A HREF="mailto:weave@oculan.com">Weave </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> * * @see <A HREF="http://www.ietf.org/rfc/rfc1213.txt">RFC1213 </A> */public class IpAddrTable implements SnmpHandler { /** * The interface to IP address table by ifIndex. */ private static String IP_ADDR_IF_INDEX = ".1.3.6.1.2.1.4.20.1.2"; /** * <P> * The list of collected IpAddrTableEntries built from the infomation * collected from the remote agent. * </P> */ private List m_entries; /** * <P> * Flag indicating if query was successful. * </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. */ private int m_version; /** * <P> * This will be the OID where the information should cut off from the return * packet from the GETBULK command. */ private SnmpObjectId m_stopAt = null; /** * <P> * This list will hold each instance of the specific MIB variable listed * within IpAddrTableEntry. By keeping these separate, we can generate our * own usable map from the variables. */ private List m_snmpVarBindList; /** * <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 #IpAddrTable(SnmpSession, Signaler) * */ private IpAddrTable() throws UnsupportedOperationException { throw new UnsupportedOperationException("Default constructor not supported!"); } /** * <P> * Constructs an IpAddrTable object that is used to collect the address * elements from the remote agent. Once all the elements 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 IpAddrTableEntry */ public IpAddrTable(SnmpSession session, Signaler signaler) { m_signal = signaler; m_entries = new ArrayList(); m_error = false; m_version = SnmpSMI.SNMPV1; m_stopAt = IpAddrTableEntry.stop_oid(); m_snmpVarBindList = new ArrayList(); SnmpPduPacket pdu = IpAddrTableEntry.getNextPdu(m_version); session.send(pdu, this); } /** * <P> * Constructs an IpAddrTable object that is used to collect the address * elements from the remote agent. Once all the elements 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 IpAddrTableEntry */ public IpAddrTable(SnmpSession session, Signaler signaler, int version) { m_signal = signaler; m_entries = new ArrayList(); m_error = false; m_version = version; m_stopAt = IpAddrTableEntry.stop_oid(); m_snmpVarBindList = new ArrayList(); SnmpPduPacket pdu = IpAddrTableEntry.getNextPdu(version); ThreadCategory.getInstance(getClass()).debug("IpAddrTable: initial pdu request id: " + pdu.getRequestId()); 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 table. * </P> * * @return The list of ifTableEntry 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> * * @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; Category log = ThreadCategory.getInstance(getClass()); if (log.isDebugEnabled()) { log.debug("snmpReceivedPdu: got SNMP response, current version: " + ((m_version == SnmpSMI.SNMPV1) ? "SNMPv1" : "SNMPv2")); } 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 { // The last variable in the list of elements // is always the first to run off the table, so // we only need to check that one. // // GETBULK response handling if (m_version == SnmpSMI.SNMPV2) { int numVarBinds = pdu.getLength(); for (int x = 0; x < numVarBinds; x++) { SnmpVarBind vb = pdu.getVarBindAt(x); if (vb.getValue() instanceof org.opennms.protocols.snmp.SnmpV2Error) { m_error = true; if (log.isDebugEnabled()) log.debug("snmpReceivedPDU: varbind: " + vb.getName() + " error: '" + vb.getValue() + "'"); break; } m_snmpVarBindList.add(vb); } if (!m_error) { // in case we did not receive all the data from the // first packet, must generate a new GETBULK packet // starting at the OID the previous one left off. // if (m_stopAt.compare(pdu.getVarBindAt(numVarBinds - 1).getName()) > 0) { SnmpObjectId id = new SnmpObjectId(pdu.getVarBindAt(numVarBinds - 1).getName()); SnmpVarBind[] newvblist = { new SnmpVarBind(id) }; SnmpPduPacket nxt = new SnmpPduBulk(0, 10, newvblist); nxt.setRequestId(SnmpPduPacket.nextSequence()); session.send(nxt, this); doNotify = false; } else { // Convert SNMP variable binding list to an array // for processing // SnmpVarBind[] tempStorage = new SnmpVarBind[m_snmpVarBindList.size()]; tempStorage = (SnmpVarBind[]) m_snmpVarBindList.toArray(tempStorage); // since the MIB does not store the number of // interfaces that have // IP addresses, the method must resort to an // alternative. By // counting the number of values found for the // ipAddrIfIndex variable, // we'll have the number of interfaces with IP's. // Each IP-bound // interface will have a value for each MIB variable // listed // int numInterfaces = 0; SnmpObjectId ipAddrIfIndex = new SnmpObjectId(IP_ADDR_IF_INDEX); while (ipAddrIfIndex.compare(tempStorage[numInterfaces].getName()) > 0) numInterfaces++; // store the IP Address Table data for each // interface into a map. // int numEntries = IpAddrTableEntry.getElementListSize(); for (int if_index = 0; if_index < numInterfaces; if_index++) { SnmpVarBind[] vblist = new SnmpVarBind[numEntries]; for (int vb_index = 0; vb_index < numEntries; vb_index++) { vblist[vb_index] = tempStorage[if_index + (vb_index * numInterfaces)]; } IpAddrTableEntry ent = new IpAddrTableEntry(vblist); m_entries.add(ent); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?