This commit is contained in:
waner
2025-05-15 13:58:53 +08:00
parent b7cd62a872
commit f8bf4b10fb
4 changed files with 70 additions and 59 deletions
@@ -1,6 +1,8 @@
package com.sunyard.snmp;
import org.snmp4j.*;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.*;
import org.snmp4j.security.*;
import org.snmp4j.smi.*;
@@ -40,33 +42,35 @@ public class SnmpTrapSender implements AutoCloseable{
snmp.listen();
}
public void sendTrap(String message, int severity) throws IOException {
public ResponseEvent sendTrap(String message, int severity) throws IOException {
ResponseEvent event = null;
switch (version) {
case SnmpConstants.version1:
sendV1Trap(message, severity);
event = sendV1Trap(message, severity);
break;
case SnmpConstants.version2c:
sendV2Trap(message, severity);
event = sendV2Trap(message, severity);
break;
default:
throw new IllegalArgumentException("不支持的SNMP版本");
}
return event;
}
private void sendV1Trap(String message, int severity) throws IOException {
private ResponseEvent sendV1Trap(String message, int severity) throws IOException {
CommunityTarget target = createCommunityTarget(SnmpConstants.version1);
PDUv1 pdu = new PDUv1();
configureCommonTrap(pdu, message, severity);
pdu.setGenericTrap(PDUv1.ENTERPRISE_SPECIFIC);
snmp.send(pdu, target);
return snmp.send(pdu, target);
}
private void sendV2Trap(String message, int severity) throws IOException {
private ResponseEvent sendV2Trap(String message, int severity) throws IOException {
CommunityTarget target = createCommunityTarget(SnmpConstants.version2c);
PDU pdu = new PDU();
configureCommonTrap(pdu, message, severity);
pdu.setType(PDU.TRAP);
snmp.send(pdu, target);
return snmp.send(pdu, target);
}