sydapi/src/main/java/com/sunyard/snmp/SnmpTrapSender.java
2025-05-08 16:46:30 +08:00

135 lines
4.5 KiB
Java

package com.sunyard.snmp;
import org.snmp4j.*;
import org.snmp4j.mp.*;
import org.snmp4j.security.*;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;
public class SnmpTrapSender {
private static volatile SnmpTrapSender instance;
private Snmp snmp;
private Properties config;
// 配置参数
private String ip;
private int port;
private String enterpriseOid;
private String community;
private int version;
private SnmpTrapSender() throws IOException {
// loadConfig();
ip = "172.16.18.113";
port = 162;
enterpriseOid = "1.3.6.1.4.1.2021.251.1";
community = "public";
version = 1;
initializeSNMP();
}
public static SnmpTrapSender getInstance() throws IOException {
if (instance == null) {
synchronized (SnmpTrapSender.class) {
if (instance == null) {
instance = new SnmpTrapSender();
}
}
}
return instance;
}
private void loadConfig() {
try (InputStream input = getClass().getClassLoader().getResourceAsStream("test.properties")) {
config = new Properties();
config.load(input);
ip = config.getProperty("snmp.target.ip");
port = Integer.parseInt(config.getProperty("snmp.target.port"));
enterpriseOid = config.getProperty("enterprise.oid");
community = config.getProperty("snmp.community");
version = Integer.parseInt(config.getProperty("snmp.version"));
} catch (IOException | NumberFormatException e) {
throw new RuntimeException("加载配置文件失败", e);
}
}
public void loadConfig(Map<String, String> map) {
ip = map.getOrDefault("snmp.target.ip", null);
// port = Integer.parseInt(map.getOrDefault("snmp.target.port", "0"));
enterpriseOid = map.getOrDefault("enterprise.oid", null);
community = map.getOrDefault("snmp.community", null);
// version = Integer.parseInt(map.getOrDefault("snmp.version", "1"));
}
private void initializeSNMP() throws IOException {
TransportMapping<?> transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
snmp.listen();
}
public void sendTrap(String message, int severity) throws IOException {
switch (version) {
case SnmpConstants.version1:
sendV1Trap(message, severity);
break;
case SnmpConstants.version2c:
sendV2Trap(message, severity);
break;
default:
throw new IllegalArgumentException("不支持的SNMP版本");
}
}
private void 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);
}
private void 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);
}
private void configureCommonTrap(PDU pdu, String message, int severity) {
pdu.add(new VariableBinding(SnmpConstants.sysUpTime,
new TimeTicks(System.currentTimeMillis()/1000)));
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID,
new OID(enterpriseOid + ".0.1")));
pdu.add(new VariableBinding(new OID(enterpriseOid + ".1.1"),
new OctetString(message)));
pdu.add(new VariableBinding(new OID(enterpriseOid + ".1.2"),
new Integer32(severity)));
}
private CommunityTarget createCommunityTarget(int version) {
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setAddress(GenericAddress.parse("udp:"+ip+"/"+port));
target.setVersion(version);
target.setTimeout(5000);
target.setRetries(3);
return target;
}
public void close() throws IOException {
if (snmp != null) {
snmp.close();
}
}
}