📄 snmpadaptorserver.java
字号:
// Diff start if(enterpOid != null) pdu.enterprise = enterpOid; else pdu.enterprise = enterpriseOid ; //Diff end pdu.genericTrap = generic ; pdu.specificTrap = specific ; //Diff start if(time != null) pdu.timeStamp = time.longValue(); else pdu.timeStamp = getSysUpTime(); //Diff end if (varBindList != null) { pdu.varBindList = new SnmpVarBind[varBindList.size()] ; varBindList.copyInto(pdu.varBindList); } else pdu.varBindList = null ; if (agentAddr == null) { // If the local host cannot be determined, // we put 0.0.0.0 in agentAddr try { final InetAddress inetAddr = (address!=null)?address:InetAddress.getLocalHost(); agentAddr = handleMultipleIpVersion(inetAddr.getAddress()); } catch (UnknownHostException e) { byte[] zeroedAddr = new byte[4]; agentAddr = handleMultipleIpVersion(zeroedAddr); } } pdu.agentAddr = agentAddr; // Next, send the pdu to the specified destination // // Diff start if(addr != null) sendTrapPdu(addr, pdu) ; else sendTrapPdu(pdu); //End diff } /** * Sends a trap using SNMP V2 trap format. * <BR>The trap is sent to the specified <CODE>SnmpPeer</CODE> destination. * <BR>The community string used is the one located in the * <CODE>SnmpPeer</CODE> parameters * (<CODE>SnmpParameters.getRdCommunity() </CODE>). * <BR>The variable list included in the outgoing trap is composed of * the following items: * <UL> * <LI><CODE>sysUpTime.0</CODE> with the value specified by * <CODE>time</CODE></LI> * <LI><CODE>snmpTrapOid.0</CODE> with the value specified by * <CODE>trapOid</CODE></LI> * <LI><CODE>all the (oid,values)</CODE> from the specified * <CODE>varBindList</CODE></LI> * </UL> * * @param peer The <CODE>SnmpPeer</CODE> destination of the trap. * @param trapOid The OID identifying the trap. * @param varBindList A list of <CODE>SnmpVarBind</CODE> instances or null. * @param time The time stamp (overwrite the current time). * * @exception IOException An I/O error occurred while sending the trap. * @exception SnmpStatusException If the trap exceeds the limit * defined by <CODE>bufferSize</CODE>. * * @since 1.5 */ public void snmpV2Trap(SnmpPeer peer, SnmpOid trapOid, SnmpVarBindList varBindList, SnmpTimeticks time) throws IOException, SnmpStatusException { SnmpParameters p = (SnmpParameters) peer.getParams(); snmpV2Trap(peer.getDestAddr(), peer.getDestPort(), p.getRdCommunity(), trapOid, varBindList, time); } /** * Sends a trap using SNMP V2 trap format. * <BR>The trap is sent to each destination defined in the ACL file * (if available). If no ACL file or no destinations are available, * the trap is sent to the local host. * <BR>The variable list included in the outgoing trap is composed of * the following items: * <UL> * <LI><CODE>sysUpTime.0</CODE> with its current value</LI> * <LI><CODE>snmpTrapOid.0</CODE> with the value specified by * <CODE>trapOid</CODE></LI> * <LI><CODE>all the (oid,values)</CODE> from the specified * <CODE>varBindList</CODE></LI> * </UL> * * @param trapOid The OID identifying the trap. * @param varBindList A list of <CODE>SnmpVarBind</CODE> instances or null. * * @exception IOException An I/O error occurred while sending the trap. * @exception SnmpStatusException If the trap exceeds the limit defined * by <CODE>bufferSize</CODE>. */ public void snmpV2Trap(SnmpOid trapOid, SnmpVarBindList varBindList) throws IOException, SnmpStatusException { if (isTraceOn()) { trace("snmpV2Trap", "trapOid=" + trapOid); } // First, make an SNMP V2 trap pdu // We clone varBindList and insert sysUpTime and snmpTrapOid // SnmpPduRequest pdu = new SnmpPduRequest() ; pdu.address = null ; pdu.port = trapPort ; pdu.type = pduV2TrapPdu ; pdu.version = snmpVersionTwo ; pdu.community = null ; SnmpVarBindList fullVbl ; if (varBindList != null) fullVbl = (SnmpVarBindList)varBindList.clone() ; else fullVbl = new SnmpVarBindList(2) ; SnmpTimeticks sysUpTimeValue = new SnmpTimeticks(getSysUpTime()) ; fullVbl.insertElementAt(new SnmpVarBind(snmpTrapOidOid, trapOid), 0) ; fullVbl.insertElementAt(new SnmpVarBind(sysUpTimeOid, sysUpTimeValue), 0); pdu.varBindList = new SnmpVarBind[fullVbl.size()] ; fullVbl.copyInto(pdu.varBindList) ; // Next, send the pdu to all destinations defined in ACL // sendTrapPdu(pdu) ; } /** * Sends a trap using SNMP V2 trap format. * <BR>The trap is sent to the specified <CODE>InetAddress</CODE> * destination using the specified community string (and the ACL file * is not used). * <BR>The variable list included in the outgoing trap is composed of * the following items: * <UL> * <LI><CODE>sysUpTime.0</CODE> with its current value</LI> * <LI><CODE>snmpTrapOid.0</CODE> with the value specified by * <CODE>trapOid</CODE></LI> * <LI><CODE>all the (oid,values)</CODE> from the specified * <CODE>varBindList</CODE></LI> * </UL> * * @param addr The <CODE>InetAddress</CODE> destination of the trap. * @param cs The community string to be used for the trap. * @param trapOid The OID identifying the trap. * @param varBindList A list of <CODE>SnmpVarBind</CODE> instances or null. * * @exception IOException An I/O error occurred while sending the trap. * @exception SnmpStatusException If the trap exceeds the limit * defined by <CODE>bufferSize</CODE>. */ public void snmpV2Trap(InetAddress addr, String cs, SnmpOid trapOid, SnmpVarBindList varBindList) throws IOException, SnmpStatusException { if (isTraceOn()) { trace("snmpV2Trap", "trapOid=" + trapOid); } // First, make an SNMP V2 trap pdu // We clone varBindList and insert sysUpTime and snmpTrapOid // SnmpPduRequest pdu = new SnmpPduRequest() ; pdu.address = null ; pdu.port = trapPort ; pdu.type = pduV2TrapPdu ; pdu.version = snmpVersionTwo ; if(cs != null) pdu.community = cs.getBytes(); else pdu.community = null; SnmpVarBindList fullVbl ; if (varBindList != null) fullVbl = (SnmpVarBindList)varBindList.clone() ; else fullVbl = new SnmpVarBindList(2) ; SnmpTimeticks sysUpTimeValue = new SnmpTimeticks(getSysUpTime()) ; fullVbl.insertElementAt(new SnmpVarBind(snmpTrapOidOid, trapOid), 0) ; fullVbl.insertElementAt(new SnmpVarBind(sysUpTimeOid, sysUpTimeValue), 0); pdu.varBindList = new SnmpVarBind[fullVbl.size()] ; fullVbl.copyInto(pdu.varBindList) ; // Next, send the pdu to the specified destination // if(addr != null) sendTrapPdu(addr, pdu); else sendTrapPdu(pdu); } /** * Sends a trap using SNMP V2 trap format. * <BR>The trap is sent to the specified <CODE>InetAddress</CODE> * destination using the specified parameters (and the ACL file is not * used). * Note that if the specified <CODE>InetAddress</CODE> destination is null, * then the ACL file mechanism is used. * <BR>The variable list included in the outgoing trap is composed of the * following items: * <UL> * <LI><CODE>sysUpTime.0</CODE> with the value specified by * <CODE>time</CODE></LI> * <LI><CODE>snmpTrapOid.0</CODE> with the value specified by * <CODE>trapOid</CODE></LI> * <LI><CODE>all the (oid,values)</CODE> from the specified * <CODE>varBindList</CODE></LI> * </UL> * * @param addr The <CODE>InetAddress</CODE> destination of the trap. * @param cs The community string to be used for the trap. * @param trapOid The OID identifying the trap. * @param varBindList A list of <CODE>SnmpVarBind</CODE> instances or null. * @param time The time stamp (overwrite the current time). * * @exception IOException An I/O error occurred while sending the trap. * @exception SnmpStatusException If the trap exceeds the limit * defined by <CODE>bufferSize</CODE>. * * @since 1.5 */ public void snmpV2Trap(InetAddress addr, String cs, SnmpOid trapOid, SnmpVarBindList varBindList, SnmpTimeticks time) throws IOException, SnmpStatusException { snmpV2Trap(addr, trapPort, cs, trapOid, varBindList, time); } private void snmpV2Trap(InetAddress addr, int port, String cs, SnmpOid trapOid, SnmpVarBindList varBindList, SnmpTimeticks time) throws IOException, SnmpStatusException { if (isTraceOn()) { trace("snmpV2Trap", "trapOid=" + trapOid + "\ncommunity=" + cs + "\naddr=" + addr + "\nvarBindList=" + varBindList + "\ntime=" + time + "\ntrapPort=" + port); } // First, make an SNMP V2 trap pdu // We clone varBindList and insert sysUpTime and snmpTrapOid // SnmpPduRequest pdu = new SnmpPduRequest() ; pdu.address = null ; pdu.port = port ; pdu.type = pduV2TrapPdu ; pdu.version = snmpVersionTwo ; if(cs != null) pdu.community = cs.getBytes(); else pdu.community = null; SnmpVarBindList fullVbl ; if (varBindList != null) fullVbl = (SnmpVarBindList)varBindList.clone() ; else fullVbl = new SnmpVarBindList(2) ; // Only difference with other SnmpTimeticks sysUpTimeValue = null; if(time != null) sysUpTimeValue = time; else sysUpTimeValue = new SnmpTimeticks(getSysUpTime()) ; //End of diff fullVbl.insertElementAt(new SnmpVarBind(snmpTrapOidOid, trapOid), 0) ; fullVbl.insertElementAt(new SnmpVarBind(sysUpTimeOid, sysUpTimeValue), 0); pdu.varBindList = new SnmpVarBind[fullVbl.size()] ; fullVbl.copyInto(pdu.varBindList) ; // Next, send the pdu to the specified destination // // Diff start if(addr != null) sendTrapPdu(addr, pdu) ; else sendTrapPdu(pdu); //End diff } /** * Send the specified trap PDU to the passed <CODE>InetAddress</CODE>. * @param address The destination address. * @param pdu The pdu to send. * @exception IOException An I/O error occurred while sending the trap. * @exception SnmpStatusException If the trap exceeds the limit * defined by <CODE>bufferSize</CODE>. * * @since 1.5 */ public void snmpPduTrap(InetAddress address, SnmpPduPacket pdu) throws IOException, SnmpStatusException { if(address != null) sendTrapPdu(address, pdu); else sendTrapPdu(pdu); } /** * Send the specified trap PDU to the passed <CODE>SnmpPeer</CODE>. * @param peer The destination peer. The Read community string is used of * <CODE>SnmpParameters</CODE> is used as the trap community string. * @param pdu The pdu to send. * @exception IOException An I/O error occurred while sending the trap. * @exception SnmpStatusException If the trap exceeds the limit defined * by <CODE>bufferSize</CODE>. * @since 1.5 */ public void snmpPduTrap(SnmpPeer peer, SnmpPduPacket pdu) throws IOException, SnmpStatusException { if(peer != null) { pdu.port = peer.getDestPort(); sendTrapPdu(peer.getDestAddr(), pdu); } else { pdu.port = getTrapPort().intValue(); sendTrapPdu(pdu); } } /** * Send the specified trap PDU to every destinations from the ACL file. */ private void sendTrapPdu(SnmpPduPacket pdu) throws SnmpStatusException, IOException { // Make an SNMP message from the pdu // SnmpMessage msg = null ; try { msg = (SnmpMessage)pduFactory.encodeSnmpPdu(pdu, bufferSize) ; if (msg == null) { throw new SnmpStatusException( SnmpDefinitions.snmpRspAuthorizationError) ; } } catch (SnmpTooBigException x) { if (isDebugOn()) { debug("sendTrapPdu", "trap pdu is too big"); debug("sendTrapPdu", "trap hasn't been sent to anyone"); } throw new SnmpStatusException(SnmpDefinitions.snmpRspTooBig) ; // FIXME: is the right exception to throw ? // We could simply forward SnmpTooBigException ? } // Now send the SNMP message to each destination // int sendingCount = 0 ; openTrapSocketIfNeeded() ; if (ipacl != null) { Enumeration ed = ((InetAddressAcl)ipacl).getTrapDestinations() ; while (ed.hasMoreElements()) { msg.address = (InetAddress)ed.nextElement() ; Enumeration ec = ((InetAddressAcl)ipacl). getTrapCommunities(msg.address) ; while (ec.hasMoreElements()) { msg.community = ((String)ec.nextElement()).getBytes() ; try { sendTrapMessage(msg) ; sendingCount++ ; } catch (SnmpTooBigException x) { if (isDebugOn()) { debug("sendTrapPdu", "trap pdu is too big"); debug("sendTrapPdu", "trap hasn't been sent to "+ msg.address); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -