use snmptrap to send

1
2
3
4
snmptrap -v 2c -c public 192.168.0.1 "" .1.3.6.1.4.1.89691.100.2.1
.1.3.6.1.4.1.89691.100.1.1 s “API_CATEGORY”
.1.3.6.1.4.1.89691.100.1.2 i 1
.1.3.6.1.4.1.89691.100.1.3 s “Alarm Caused by an exception”

use java Snmp4j to send

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Snmp4jTest {
private Logger log = LoggerFactory.getLogger(this.getClass());
@Test
public void test() throws Exception {
// listen
TransportMapping transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
transport.listen();
// target
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public")); // public
target.setAddress(new UdpAddress("192.168.0.59" + "/" + "162")); //new UdpAddress("172.16.72.49/162")
target.setRetries(1);
target.setTimeout(3000);
target.setVersion(SnmpConstants.version2c);
// pdu
PDU pdu = new PDU();
pdu.setType(PDU.TRAP);
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(".1.3.6.1.4.1.89691.100.2.1"))); // necessary for TRAP
pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.89691.100.1.1"), new OctetString("API_CATEGORY")));
pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.89691.100.1.2"), new Integer32(1)));
pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.89691.100.1.3"), new OctetString("Alarm")));
log.debug("pdu: " + pdu.toString());
log.debug("target: " + target.toString());
ResponseEvent event = snmp.send(pdu, target);
log.debug("event: " + event);
}
}