snmpnodecollector.java
来自「opennms得相关源码 请大家看看」· Java 代码 · 共 663 行 · 第 1/2 页
JAVA
663 行
//// 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 Aug 29: Fixed the case when SNMPv1 data is gathered that doesn't exist and there isn't any more to gather. - dj@opennms.org // 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.collectd;import java.util.List;import org.apache.log4j.Category;import org.apache.log4j.Priority;import org.opennms.core.utils.ThreadCategory;import org.opennms.netmgt.utils.Signaler;import org.opennms.protocols.snmp.SnmpHandler;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;/** * The SnmpNodeCollector class is responsible for performing the actual SNMP * data collection for a node over a specified network interface. The * SnmpNodeCollector implements the SnmpHandler class in order to receive * notifications when an SNMP reply is received or error occurs. * * The SnmpNodeCollector is provided a list of MIB objects to collect and an * interface over which to collect the data. Data collection can be via SNMPv1 * GetNext requests or SNMPv2 GetBulk requests depending upon the parms used to * construct the collector. * * @author <A HREF="mailto:mike@opennms.org">Mike </A> * @author <A>Jon Whetzel </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> * * @version 1.1.1.1 * */public class SnmpNodeCollector implements SnmpHandler { /** * Flag indicating the success or failure of the informational query. If the * flag is set to false then either part of all of the information was * unable to be retreived. If it is set to true then all of the data was * received from the remote host. */ private boolean m_error; /** * Reason that the SNMP request failed. Please see * org.opennms.protocols.snmp.SnmpPduPacket class for a list of possible * SNMP error codes. This variable only has meaning of m_error flag is true. */ private int m_errorStatus; /** * Array of SNMPv1 error strings. Please see * org.opennms.protocols.snmp.SnmpPduPacket class for list of SNMP error * codes which serve as indices into this string array. */ private static String[] m_errorText = { "ErrNoError", "ErrTooBig", "ErrNoSuchName", "ErrBadValue", "ErrReadOnly", "ErrGenError" }; /** * If the SNMP collection failed due to a problem with one or more varbinds * (for example if a particular object oid is requested which is not * implemented in the target's SNMP agent) then this value will be set equal * to the *first* failing varbind in the request. This variable only has * meaning if m_error flag is true. Will be set to -1 if the SNMP collection * failed for an unrelated reason. */ private int m_errorIndex; /** * Flag indicating if the SNMP collection failed due to the SNMP request * timing out. Its value only has meaning if m_error flag is true. */ private boolean m_timeout; /** * 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. */ private Signaler m_signal; /** * List of MibObject objects to be collected. */ private List m_objList; /** * Initialized to zero. As each response PDU is received this value is * incremented by the number of vars contained in the response. Processing * will continue until this value reaches the total number of oids in the * MibObject list (m_olbjList) and all oid values have been retrieved. */ private int m_oidListIndex; /** * Used to store the collected MIB data. */ private SNMPCollectorEntry m_collectorEntry; /** * Used for classifying the SNMP version of the session. * */ private int m_version; /** * Holds the IP Address of the primary SNMP iterface. */ private String m_primaryIf; /** * Max number of variables permitted in a single outgoing SNMP PDU request.. */ private int m_maxVarsPerPdu; /** * The default constructor is marked private and will always throw an * exception. This is done to disallow the default constructor. The reason * is that this object requires several arguments to perform it's duties. * * @exception java.lang.UnsupportedOperationException * Always thrown from this method since it is not supported. */ private SnmpNodeCollector() throws UnsupportedOperationException { throw new UnsupportedOperationException("Default Constructor not supported"); } /** * The class constructor is used to initialize the collector and send out * the initial SNMP packet requesting data. The data is then received and * store by the object. When all the data has been collected the passed * signaler object is <EM>notified</EM> using the notifyAll() method. * * @param session * The SNMP session with the remote agent. * @param signaler * The object signaled when data collection is done. * @param objList * The list of object id's to be collected. * @param maxVarsPerPdu * Max number of vars permitted in a single PDU */ public SnmpNodeCollector(SnmpSession session, Signaler signaler, List objList, int maxVarsPerPdu) { super(); // Log4j category // Category log = ThreadCategory.getInstance(getClass()); m_error = false; m_errorIndex = -1; m_timeout = false; m_collectorEntry = null; // Process parameters // m_primaryIf = session.getPeer().getPeer().getHostAddress(); m_version = session.getPeer().getParameters().getVersion(); m_signal = signaler; m_objList = objList; m_oidListIndex = 0; m_maxVarsPerPdu = maxVarsPerPdu; if (log.isDebugEnabled()) log.debug("SnmpNodeCollector: totalOids=" + objList.size() + " maxVarsPerPdu=" + maxVarsPerPdu); // Create initial PDU request and send it to the remote host. // SnmpPduPacket pdu = getNextPdu(m_primaryIf); if (log.isDebugEnabled()) log.debug("SnmpNodeCollector: sending initial SNMP get/getBulk request PDU for " + m_primaryIf); session.send(pdu, this); } /** * This method will take an OID, and generate the succeeding OID. This will * be used for examining responses from SNMPv2 GETBULK packets when doing * SNMPv2 collection, so that we can keep all the data for a particular * object, and throw out the rest. * * @param oid * Object identifier from which to generate the stop oid * * @return SnmpObjectId object which represents the oid on which to stop the * bulk collection. */ public static SnmpObjectId stop_oid(String oid) { // Log4j category // Category log = ThreadCategory.getInstance(SnmpNodeCollector.class); SnmpObjectId id = new SnmpObjectId(oid); int[] ids = id.getIdentifiers(); ++ids[ids.length - 1]; id.setIdentifiers(ids); if (log.isDebugEnabled()) log.debug("stop_oid: stop_oid = " + id.toString()); return id; } /** * This method constructs the next SnmpPduPacket (pdu) for sending to the * remote node. The version of SNMP supported will determine whether a GET * or GETBULK PDU is built. * * For SNMPv1, GET commands are built. Each constructed PDU contains * varbinds for all the objects to be collected. * * For SNMPv2, GETBULK commands are built. Each constructed PDU contains * varbinds for all the objects to be collected. * * @param ifAddress * Interface address of the remote agent * * @return An SnmpPduPacket appropriate for the SNMP version supported. * * @see org.opennms.protocols.snmp.SnmpNull SnmpNull * @see org.opennms.protocols.snmp.SnmpPduPacket SnmpPduPacket */ public SnmpPduPacket getNextPdu(String ifAddress) { // Log4j category // Category log = ThreadCategory.getInstance(getClass()); SnmpPduPacket pdu = null; int nonRepeaters = 0; // Applicable to SNMPv2 only // SNMPv2 Support if (m_version == SnmpSMI.SNMPV2) { pdu = new SnmpPduBulk(); } // SNMPv1 Support else { pdu = new SnmpPduRequest(SnmpPduPacket.GET); } pdu.setRequestId(SnmpPduPacket.nextSequence()); // Generate new SnmpVarBind object. Add each oid from the // object list until max var count is reached or there are no // more objects to collect. Object[] oidArray = m_objList.toArray(); for (int ii = m_oidListIndex; ii < m_objList.size() && (ii - m_oidListIndex) < m_maxVarsPerPdu; ii++) { MibObject mibObject = ((MibObject) oidArray[ii]); SnmpObjectId oid = null; String instanceStr = mibObject.getInstance(); // // instance must be one of the following: // 1) null. In this case the object's oid value is used as-is. // 2) a sequence of period-separated decimal values indicating the // instance to be retrieved. This value will be appended to the // objects oid. In the case of SNMPv2 the final decimal value will // be decremented by one due to the fact that SNMP GetNext is used. // 3) an integer value indicating the instance of the MIB object to // be // retrieved. This value will be appended to the object's oid. // In the case of SNMPv2 the final decimal value will // be decremented by one due to the fact that SNMP GetNext is used. // if (instanceStr == null) { log.warn("No instance specified for MIB object " + mibObject.getOid()); oid = new SnmpObjectId(mibObject.getOid()); } else if (m_version == SnmpSMI.SNMPV1) { oid = new SnmpObjectId(mibObject.getOid() + "." + instanceStr); } else if (m_version == SnmpSMI.SNMPV2) { String instancePrefix = null; String lastDecimalValue = null; // Sequence of period-separated decimal values? // if (instanceStr.indexOf(".") != -1) { // Extract last decimal value so we can decrement it by one int lastPeriod = instanceStr.lastIndexOf('.'); instancePrefix = instanceStr.substring(0, lastPeriod); lastDecimalValue = instanceStr.substring(lastPeriod + 1); } // Single integer value? else { lastDecimalValue = instanceStr; } // convert the lastDecimalValue string to an integer int lastValue = -1; try { lastValue = Integer.parseInt(lastDecimalValue); } catch (NumberFormatException nfe) { // If the value cannot be converted to an integer just // use a string of "0".
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?