[SNMP4J] Using SNMP4J in a java program

Frank Fock fock at agentpp.com
Mon Feb 28 22:43:50 CET 2011


Hi,

You can use the second example from
http://www.snmp4j.org/doc/org/snmp4j/Snmp.html
and just replace PDU.GETNEXT with PDU.TRAPV1
and remove the listener stuff (use "null"
parameter instead).

Best regards,
Frank

On 28.02.2011 20:28, Neil Cohen wrote:
> Hi,
>
> I am writing a Java program which needs to send SNMP traps to a remote
> server. This is a one-way communication - I'm sending the traps but not
> expecting to receive a response. Right now, I'm using snmp v1.
>
> Can someone point me at some simple example code that will do this?
> Right now, we fork a shell script which runs a command like this:
>
>    /usr/local/bin/snmptrap -v 1 -c public mysourcehost enterprises.1.2.3
> 11.22.33.44 6 1 '' enterprises.1.2.3.4.5.6 s "the text of the trap goes
> here...."
>
> I would like to use snmp4j to replace the shell script and just generate
> the trap.
>
> I'm an expert Java programmer, but not particularly experienced with SNMP.
>
> So far, I have started with this:
>
>    private void sendTrap(String alertMsg){
>           PDUv1 pdu = new PDUv1();
>           pdu.setAgentAddress(new IpAddress("1.1.1.1"));
>           pdu.setSpecificTrap(6);
>           pdu.setGenericTrap(1);
>           pdu.setEnterprise(new OID("1.2.3"));
>
>
>           Logger.info("SNMP: " + pdu.toString());
>       }
>
> This prints at least some of the information I need to send. I think I
> need to create a Target and set things like the destination address and
> community string. But then how do I send the trap itself? The only
> example I've seen so far looked like it needed a listener, and I'm not
> expecting any response (or am I)??
>
> Thanks in advance,
>
> nbc
>

-- 
AGENT++
http://www.agentpp.com
http://www.snmp4j.com
http://www.mibexplorer.com
http://www.mibdesigner.com




More information about the SNMP4J mailing list