ifxtable.java
来自「opennms得相关源码 请大家看看」· Java 代码 · 共 642 行 · 第 1/2 页
JAVA
642 行
log.warn("snmpReceivedPDI: agent does not support interfaces mib!"); } else if (vb.getValue() instanceof org.opennms.protocols.snmp.SnmpV2Error) { m_error = true; if (log.isDebugEnabled()) log.debug("snmpReceivedPDU: varbind: " + vb.getName() + " error: '" + vb.getValue() + "'"); } if (!m_error) { SnmpInt32 temp = (SnmpInt32) vb.getValue(); m_ifNumber = temp.getValue(); if (log.isDebugEnabled()) log.debug("snmpReceivedPdu: Number of interfaces = " + m_ifNumber); // // Now that we know the number of interfaces we can can // allocate // the temp storage to hold all the response variable // bindings // tempStorage = new SnmpVarBind[m_ifNumber * IfXTableEntry.getElementListSize()]; SnmpPduPacket nxt = null; if (m_version == SnmpSMI.SNMPV2) { nxt = IfXTableEntry.getBulkPdu(m_ifNumber); } else { nxt = IfXTableEntry.getNextPdu(); } session.send(nxt, this); doNotify = false; } } // Handle SNMPv2 GetBulk responses... else if (m_version == SnmpSMI.SNMPV2) { if (log.isDebugEnabled()) { log.debug("snmpReceivedPdu: Handling GETBULK packet"); } int length = pdu.getLength(); for (int y = 0; y < pdu.getLength(); y++) { // Check for v2 error in each returned varbind SnmpVarBind vb = pdu.getVarBindAt(y); 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; } tempStorage[m_responses] = vb; m_responses++; } 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. // // Calculate maxRepetitions for next GETBULK packet int maxReps = (m_ifNumber * IfXTableEntry.getElementListSize()) - m_responses; if (log.isDebugEnabled()) log.debug("snmpReceivedPdu: calculated number of maxRepetitions = " + maxReps); if (maxReps > 0 && m_stopAt.compare(pdu.getVarBindAt(length - 1).getName()) > 0) { SnmpObjectId id = new SnmpObjectId(pdu.getVarBindAt(length - 1).getName()); SnmpVarBind[] newvblist = { new SnmpVarBind(id) }; SnmpPduPacket nxt = new SnmpPduBulk(0, maxReps, newvblist); nxt.setRequestId(SnmpPduPacket.nextSequence()); if (log.isDebugEnabled()) log.debug("smnpReceivedPDU: Starting new GETBULK packet at OID = " + id.toString() + ", with request ID: " + nxt.getRequestId()); session.send(nxt, this); doNotify = false; } else { if (log.isDebugEnabled()) log.debug("smnpReceivedPDU: All SNMPv2 data received, processing."); // all the data has been retrieved from the MIB, so // now // we must enter it into our maps. Each map will // hold all // the MIB variable values per interface. /* * DEBUG ONLY if (log.isDebugEnabled()) { // Dump * content of tempStorage for (int x=0; x * <m_responses; x++) { log.debug("snmpReceivedPdu: * tempStorage[" + x + "] oid: " + * tempStorage[x].getName().toString()); } * log.debug("snmpReceivedPdu: done dumping temp * storage!!!"); * } */ // // Create an IfXTableEntry for each interface using // the variable // names and values stored in the temporary storage // array. // // Unlike the ifTable, the ifXTable doesn't have the // benefit of the // ifIndex object...so use the variable name of the // first ifXTableEntry // (ifName) to derive the ifIndex as each interface // is processed. // The last decimal value (the instance id) in the // variable name will be // extracted and used as the ifIndex for the // interface. For example, given // a variable name of '.1.3.6.1.2.1.31.1.1.1.5', the // last decimal value '5' // is parsed out and used as the ifIndex. if (log.isDebugEnabled()) log.debug("snmpReceivedPdu: processing temp storage array..."); for (int x = 0; x < m_ifNumber; x++) { SnmpVarBind[] templist = new SnmpVarBind[IfXTableEntry.NUM_OIDS]; // Extract the ifIndex from the current // SnmpVarBind's object id String from_oid = tempStorage[x].getName().toString(); SnmpObjectId id = new SnmpObjectId(from_oid); int[] ids = id.getIdentifiers(); int ifIndex = ids[ids.length - 1]; // parse each oid to get index int tempcount = 0; for (int j = 0; j < m_responses && tempcount < IfXTableEntry.NUM_OIDS; j++) { // Extract the "instance" id from the // current SnmpVarBind's object id from_oid = tempStorage[j].getName().toString(); id = new SnmpObjectId(from_oid); ids = id.getIdentifiers(); int instance_id = ids[ids.length - 1]; // if the indexes match, store it within // templist if (instance_id == ifIndex) { templist[tempcount++] = tempStorage[j]; } } // create VarBind list from templist. SnmpVarBind[] vblist = new SnmpVarBind[tempcount]; for (int a = 0; a < tempcount; a++) { /* * DEBUG if (log.isDebugEnabled()) * log.debug("snmpReceivedPdu: oid = " + * templist[a].getName().toString() + " * value = " + * templist[a].getValue().toString()); */ vblist[a] = templist[a]; } // create new IfXTableEntry with all variables // for a // particular index. IfXTableEntry ent = new IfXTableEntry(vblist); m_entries.add(ent); } // end for() } } // end if (!m_error) } // end if (m_version == SnmpSMI.SNMPV2) // Handle SNMPv1 GetNext responses else if (m_version == SnmpSMI.SNMPV1) { if (log.isDebugEnabled()) log.debug("snmpReceivedPdu: Handling GETNEXT packet, response count: " + m_responses); // if the response count is less than the number of // interfaces, continue to // store info and generate packets for gathering data. if (m_responses < m_ifNumber) { SnmpVarBind[] vblist = pdu.toVarBindArray(); IfXTableEntry ent = new IfXTableEntry(vblist); m_entries.add(ent); SnmpPduRequest nxt = new SnmpPduRequest(SnmpPduPacket.GETNEXT); for (int x = 0; x < pdu.getLength(); x++) { nxt.addVarBind(new SnmpVarBind(pdu.getVarBindAt(x).getName())); } nxt.setRequestId(SnmpPduPacket.nextSequence()); session.send(nxt, this); doNotify = false; m_responses++; } } // end if (m_version == SnmpSMI.SNMPV1 } // end if (errStatus != SnmpPduPacket.ErrNoError) } // end if (command != SnmpPduPacket.RESPONSE) // // call the notifyAll() method on self, and // the signalAll() method on the signaler // if (doNotify) { synchronized (this) { notifyAll(); } if (m_signal != null) { synchronized (m_signal) { m_signal.signalAll(); } } } } /** * <P> * This method is part of the SnmpHandler interface and called when an * internal error happens in a session. This is usually the result of an I/O * error. This method will not be called if the session times out sending a * packet, see snmpTimeoutError for timeout handling. * </P> * * @param session * The session that had an unexpected error * @param error * The error condition * @param pdu * The PDU being sent when the error occured * * @see #snmpTimeoutError * @see org.opennms.protocols.snmp.SnmpHandler SnmpHandler */ public void snmpInternalError(SnmpSession session, int error, SnmpSyntax pdu) { Category log = ThreadCategory.getInstance(getClass()); if (log.isDebugEnabled()) { log.debug("snmpInternal: error " + error + " for: " + session.getPeer().getPeer()); } m_error = true; synchronized (this) { notifyAll(); } if (m_signal != null) { synchronized (m_signal) { m_signal.signalAll(); } } } /** * <P> * This method is part of the SnmpHandler interface and is invoked when the * SnmpSession does not receive a reply after exhausting the retransmission * attempts. * </P> * * @param session * The session invoking the error handler * @param pdu * The PDU that the remote failed to respond to. * * @see org.opennms.protocols.snmp.SnmpHandler SnmpHandler * */ public void snmpTimeoutError(SnmpSession session, SnmpSyntax pdu) { Category log = ThreadCategory.getInstance(getClass()); if (log.isDebugEnabled()) { log.debug("snmpTimeoutError: for " + session.getPeer().getPeer()); } m_error = true; synchronized (this) { notifyAll(); } if (m_signal != null) { synchronized (m_signal) { m_signal.signalAll(); } } } /** * <P> * This method converts the physical address, normally six bytes, into a * hexidecimal string. The string is not prefixed with the traditional <EM> * "0x"</EM>, but is the raw hexidecimal string in upper case. * </P> * * <P> * <EM>NOTICE</EM>: The string is converted based on the starndard * US-ASCII table. Each NIBBLE is converted to an integer and added to the * character '0' (Zero). * </P> * * @param physAddr * The physical address to convert to a string. * * @return The converted physical address as a hexidecimal string. * */ public static String toHexString(byte[] physAddr) { // // Check to make sure that there // is enough data. // if (physAddr == null || physAddr.length == 0) { return null; } // // Convert the actual data // StringBuffer buf = new StringBuffer(12); for (int i = 0; i < physAddr.length; i++) { int b = (int) physAddr[i]; buf.append(m_hexDigit[(b >> 4) & 0xf]); // based upon US-ASCII buf.append(m_hexDigit[(b & 0xf)]); // based upon US-ASCII } return buf.toString().toUpperCase(); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?