fix:完善多机证书导入功能

This commit is contained in:
waner
2025-05-14 14:25:34 +08:00
parent 55a9cfbdbe
commit d3ff2e3646
10 changed files with 259 additions and 120 deletions
+1 -1
View File
@@ -120,5 +120,5 @@ public interface SydApi extends
public void setBindObject(Object bindObject);
void importCertToServers(List<String> addressList, int timeout, String cert);
void importCertToServers(List<String> addressList, int timeout, String cert, String bkCode, String snmpConfig);
}
@@ -137,6 +137,16 @@ public class SydApiException extends RuntimeException{
}
}
public SydApiException(String msg, int retCode, boolean flag){
super(String.format(" %04X", retCode) + ":" + ( null == msg ? transMsg(retCode) : msg ));
this.retCode = retCode;
if (null == msg || "".equals(msg)){
transMsg();
}else {
this.msg = msg;
}
}
public SydApiException(String msg, int ret, Throwable e) {
super("SydApiException(code =" + String.format(" %04x", ret) + " : " + "msg =" + (msg == null ? "未定义的错误" : msg) + ")", e);
this.retCode = ret;
@@ -11,10 +11,8 @@ import java.util.Map;
import java.util.Properties;
import java.util.Vector;
public class SnmpTrapSender {
private static volatile SnmpTrapSender instance;
public class SnmpTrapSender implements AutoCloseable{
private Snmp snmp;
private Properties config;
// 配置参数
private String ip;
@@ -23,50 +21,17 @@ public class SnmpTrapSender {
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;
public SnmpTrapSender(Map<String, String> map) throws IOException {
loadConfig(map);
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"));
port = Integer.parseInt(map.getOrDefault("snmp.target.port", "162"));
enterpriseOid = map.getOrDefault("enterprise.oid", null);
community = map.getOrDefault("snmp.community", null);
// version = Integer.parseInt(map.getOrDefault("snmp.version", "1"));
community = map.getOrDefault("snmp.community", "public");
version = Integer.parseInt(map.getOrDefault("snmp.version", "1"));
}
private void initializeSNMP() throws IOException {
@@ -109,10 +74,10 @@ public class SnmpTrapSender {
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 OID(enterpriseOid + ".1.1")));
pdu.add(new VariableBinding(new OID(enterpriseOid + ".2.1"),
new OctetString(message)));
pdu.add(new VariableBinding(new OID(enterpriseOid + ".1.2"),
pdu.add(new VariableBinding(new OID(enterpriseOid + ".2.2"),
new Integer32(severity)));
}
@@ -126,9 +91,11 @@ public class SnmpTrapSender {
return target;
}
@Override
public void close() throws IOException {
if (snmp != null) {
snmp.close();
snmp = null;
}
}
}
@@ -94,8 +94,9 @@ public class AsyncTaskExecutor {
private <T> CompletableFuture<T> handleRetry(TaskContext<T> context, Throwable ex) {
if (context.canRetry()) {
context.decreaseRetry();
System.out.printf("[重试调度] ID: %s | 延迟: %ss | 剩余重试: %d | 错误: %s%n",
System.out.printf("[重试调度] ID: %s| IP:%s | 延迟: %ss | 剩余重试: %d | 错误: %s%n",
context.getTaskId(),
context.getAddress(),
context.getRetryDelay().getSeconds(),
context.getRetriesLeft(),
ex.getCause().getMessage());