Raj N
Raj N

Reputation: 249

SNMP4j - sned(pdu, target) method always returning null

I am using MIB browser engine as SNMP manager and sending trap from Java application as follow Scenario 1 -( Valid IP address) Scenario 2 -( Invalid IP address) In both scenario getting null as a Response

public class TrapSenderVersion2 {

public static void main(String[] args) {
    TrapSenderVersion2 trapV2 = new TrapSenderVersion2();
    trapV2.sendTrap_Version2();
}
public void sendTrap_Version2() {
    try {
        // Create Transport Mapping
        TransportMapping transport = new DefaultUdpTransportMapping();
        transport.listen();

        // Create Target
        CommunityTarget cTarget = new CommunityTarget();
        cTarget.setCommunity(new OctetString("public"));
        cTarget.setVersion(SnmpConstants.version1);
        cTarget.setAddress(new UdpAddress("10.133.14.35/162"));
        cTarget.setRetries(2);
        cTarget.setTimeout(5000);

        // Create PDU for V2
        PDU pdu = new PDU();

        // need to specify the system up time
        pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new OctetString(new Date().toString())));
        pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(TrapOid)));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.30450.1.9.1.1.6.0"), new OctetString("23b77493-74dd-489a-9c99-61db6c97a2e1"))); 
        pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.30450.1.9.1.1.7.0"), new OctetString("EventType")));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.30450.1.9.1.1.8.0"), new OctetString("ServiceType")));

        pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.30450.1.9.1.1.9.0"), new OctetString("inp44vpdl002-COLLECTOR")));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.30450.1.9.1.1.10.0"), new OctetString("14.140.156.15")));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.30450.1.9.1.1.11.0"), new OctetString("Collector"))); 
        pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.30450.1.9.1.1.12.0"), new OctetString("Pune")));

        pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.30450.1.9.1.1.13.0"), new OctetString("This is a addtional info text")));


        pdu.setType(PDU.NOTIFICATION);

        // Send the PDU
        Snmp snmp = new Snmp(transport);
        System.out.println("Sending V2 Trap... Check Wheather NMS is Listening or not? ");
        ResponseEvent re = snmp.send(pdu, cTarget);
        System.out.println("ResponseEvent " + re);
        snmp.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Upvotes: 0

Views: 601

Answers (1)

Raj N
Raj N

Reputation: 249

Following line solved my problem

pdu.setType(PDU.INFORM);

I got the answer by debugging snmp4j source code, that its all depend upon PDU type. When we set PDU type as Trap, Notificationm then response will alway return as null.

So there is type called "INFORM" which acknowledge to request with ResponseEvent.

Upvotes: 0

Related Questions