java - Traps are not listened by Manager in snmp4j -
traps coming agent manager not listening traps.i write agent code , send traps on localhost.i tried mibbrowser traps received agent manager not listening.
i'm doing in scala, on java same code working.
agent code
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 " + 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 (manager) code:
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)
}
agent sending traps properly, i've tested on mibbrowser. manager not able receive traps.seems issue scala wait,synchronized. tried same on java working properly.
is there other approach in scala can use.i tried future,blocking block didn't result.
Comments
Post a Comment