snmp4j中的Manager不会监听陷阱

时间:2015-07-23 16:33:21

标签: java scala akka snmp4j

陷阱来自Agent但是Manager没有侦听陷阱。我写了一个代理代码并在localhost上发送了陷阱。我尝试使用MibBrowser从代理接收陷阱但是经理没有收听。

我在Scala中执行此操作,在java上,相同的代码正在运行。

代理商代码

case class SnmpDevice(host: String, port: Int, readCommunity: String, writeCommunity: String)

class NETrapSender {
  def createCommunityTarget(snmpDevice: SnmpDevice): CommunityTarget = {
  try {
    var comtarget: CommunityTarget = null
    if (snmpDevice != null && snmpDevice.host != null && snmpDevice.port > 0 && snmpDevice.readCommunity != null) {
      comtarget = new CommunityTarget()
      comtarget.setCommunity(new OctetString(snmpDevice.readCommunity))
      comtarget.setVersion(SnmpConstants.version2c)
      comtarget.setAddress(new UdpAddress(snmpDevice.host + "/" + snmpDevice.port))
      comtarget.setRetries(2)
      comtarget.setTimeout(5000)
      comtarget
    }
    comtarget
} catch {
  case e: Exception =>
    e.printStackTrace()
    throw e
  }

}

def createPDU(trapOid: String): PDU = {
  var pdu: PDU = new PDU
  try {
  pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new OctetString(new Date().toString())));
  pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(trapOid)));
  pdu.add(new VariableBinding(SnmpConstants.snmpTrapAddress, new IpAddress("127.0.0.1")));
  pdu.add(new VariableBinding(new OID(trapOid), new OctetString("Major")));

  //pdu.setType(PDU.INFORM) // Inform type request
  pdu.setType(PDU.NOTIFICATION)
  pdu
} catch {
  case e: Exception =>
    e.printStackTrace()
    throw e
 }

}

def sendTrap(snmpDevice: SnmpDevice, trapOid: String) {
 val udptransportMapping = new DefaultUdpTransportMapping()
 udptransportMapping.listen()
 val snmp = new Snmp(udptransportMapping)

 val target = createCommunityTarget(snmpDevice)
 val pdu = createPDU(trapOid)
 println(" Sending V2 Trap to " + snmpDevice.host + " on Port " + snmpDevice.port);

val response = snmp.inform(pdu, target)
 println(" repsonse event::" + response.getResponse)
 snmp.close
 //for INFROM
 /*val responseEvent: ResponseEvent = snmp.inform(pdu, target)
 println(" repsonse event::" + responseEvent.getResponse)*/

}  }

object NETrapSender extends App{
   val trapSenderObj = new NETrapSender
   val device = SnmpDevice("127.0.0.1", 2222, "public", "private")
   val trapOID = "1.3.6.1.6.3.1.1.5.1" //coldStart 
   trapSenderObj.sendTrap(device, trapOID)
}

Listner(经理)代码:

class TrapListener(snmpDevice: SnmpDevice) extends Thread {
 val lock: AnyRef = new AnyRef

 override def run(): Unit = {
  listen()
}

def listen(){
  lock.synchronized {
    val address = new UdpAddress(snmpDevice.host + "/" + snmpDevice.port)
    val transport: DefaultUdpTransportMapping = new DefaultUdpTransportMapping();
    transport.listen()
    val msDispatcher: MessageDispatcher = new MessageDispatcherImpl
    msDispatcher.addMessageProcessingModel(new MPv2c)

    SecurityProtocols.getInstance.addDefaultProtocols()
    SecurityProtocols.getInstance.addPrivacyProtocol(new Priv3DES)
    val snmp: Snmp = new Snmp(msDispatcher, transport);
    println(" before processing")
    val trapPrinter = new CommandResponder() {
     override def processPdu(e: CommandResponderEvent) {
      println("WONN")
      lock.synchronized {
        println("inside process pdu")
        val command = e.getPDU();
        if (command != null) {
          System.out.println("command:: " + command.toString());
        }
      }
    }
  };
  snmp.addCommandResponder(trapPrinter);
  println(" listening on ..." + snmpDevice.host)
  lock.wait(1000 * 60 *1)
 }

}

object TrapListner extends App {
 val snmpDevice = SnmpDevice("127.0.0.1", 2222, "public", "private")
new TrapListener(snmpDevice).start()
val trapOID = "1.3.6.1.6.3.1.1.5.1" //coldStart
new NETrapSender().sendTrap(snmpDevice, trapOID)

}

代理正在发送陷阱,我已经在mibBrowser上测试了。但是经理无法接收陷阱。看看Scala的一些问题等待,同步。在java工作正常的情况下尝试相同。

我们可以使用scala中的任何其他方法。我尝试使用Future,Blocking block但没有得到任何结果。

0 个答案:

没有答案