feat:多机证书导入功能
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
import com.sunyard.snmp.SnmpTrapSender;
|
||||
import com.sunyard.task.AsyncTaskExecutor;
|
||||
import com.sunyard.task.TaskContext;
|
||||
import com.sunyard.task.TaskOutcome;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SimpleTest {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// testt();
|
||||
testSnmp();
|
||||
}
|
||||
|
||||
public static void testt() {
|
||||
|
||||
AsyncTaskExecutor executor = new AsyncTaskExecutor();
|
||||
// 使用示例
|
||||
List<TaskContext<String>> tasks = Arrays.asList(
|
||||
createTaskContext("1", 1000, "cert"),
|
||||
createTaskContext("2", 1000, "cert"),
|
||||
createTaskContext("3", 1000, "cert"),
|
||||
createTaskContext("4", 1000, "cert")
|
||||
);
|
||||
|
||||
List<TaskOutcome<String>> outcomes = executor.execBatchTasks(tasks);
|
||||
executor.shutdown();
|
||||
// 分离成功失败
|
||||
List<String> successes = outcomes.stream()
|
||||
.filter(TaskOutcome::isSuccess)
|
||||
.map(TaskOutcome::getResult)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<TaskOutcome<String>> failures = outcomes.stream()
|
||||
.filter(out -> !out.isSuccess())
|
||||
.collect(Collectors.toList());
|
||||
String result = "[" + failures.stream()
|
||||
.map(p -> p.getContext().getAddress())
|
||||
.collect(Collectors.joining(",")) + "]";
|
||||
String message = "信雅达签名服务器:" + result + ",证书: " + "cert" + ",导入失败";
|
||||
System.out.println(message);
|
||||
System.out.println("成功节点: " + successes);
|
||||
System.out.println("失败详情: ");
|
||||
failures.forEach(f ->
|
||||
System.out.println(f.getContext().getAddress() + ": " + f.getError().getMessage())
|
||||
);
|
||||
}
|
||||
|
||||
private static TaskContext<String> createTaskContext(String address, int timeout, String cert) {
|
||||
Supplier<String> task = () -> importS(address);
|
||||
return new TaskContext<>(task, 2, Duration.ofSeconds(1), address);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void test() throws InterruptedException {
|
||||
List<String> addressList = new ArrayList<>();
|
||||
addressList.add("1");
|
||||
addressList.add("2");
|
||||
addressList.add("3");
|
||||
addressList.add("4");
|
||||
addressList.add("6");
|
||||
CompletableFuture<String>[] futures = new CompletableFuture[addressList.size()];
|
||||
for(int i = 0; i < addressList.size(); i++){
|
||||
int finalI = i;
|
||||
futures[i] = CompletableFuture.supplyAsync(() -> {
|
||||
return importS(addressList.get(finalI));
|
||||
});
|
||||
}
|
||||
// Thread.sleep(2000);
|
||||
//汇总失败的任务
|
||||
List<String> failedTasks = new ArrayList<>();
|
||||
for(int i = 0; i < futures.length; i++){
|
||||
if(futures[i].isCompletedExceptionally()){
|
||||
failedTasks.add(addressList.get(i));
|
||||
}
|
||||
}
|
||||
for (String s : failedTasks) {
|
||||
System.out.println(s);
|
||||
try {
|
||||
String path = "/Users/waner/Work" + "/" + s + ".txt";
|
||||
File file = new File(path);
|
||||
file.createNewFile();
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String importS(String s){
|
||||
// System.out.println("handle:" + s);
|
||||
if (Integer.parseInt(s) % 2 == 0) {
|
||||
System.out.println("handle1:" + s);
|
||||
throw new RuntimeException();
|
||||
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public static void testSnmp() throws IOException {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("snmp.target.ip", "172.16.18.113");
|
||||
map.put("snmp.target.port", "162");
|
||||
map.put("enterprise.oid", "11");
|
||||
map.put("snmp.community", "public");
|
||||
map.put("snmp.version", "1");
|
||||
// SnmpTrapSender.getInstance().loadConfig(map);
|
||||
SnmpTrapSender.getInstance().sendTrap("serve ip[127.0.0.1], import cert failed", 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import org.snmp4j.CommunityTarget;
|
||||
import org.snmp4j.PDU;
|
||||
import org.snmp4j.Snmp;
|
||||
import org.snmp4j.TransportMapping;
|
||||
import org.snmp4j.mp.SnmpConstants;
|
||||
import org.snmp4j.smi.Address;
|
||||
import org.snmp4j.smi.GenericAddress;
|
||||
import org.snmp4j.smi.OID;
|
||||
import org.snmp4j.smi.OctetString;
|
||||
import org.snmp4j.smi.UdpAddress;
|
||||
import org.snmp4j.smi.VariableBinding;
|
||||
import org.snmp4j.transport.DefaultUdpTransportMapping;
|
||||
|
||||
public class SnmpTrapSenderV2c {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
// 1. 创建SNMP对象并监听
|
||||
TransportMapping<?> transport = new DefaultUdpTransportMapping();
|
||||
Snmp snmp = new Snmp(transport);
|
||||
transport.listen();
|
||||
|
||||
// 2. 创建目标地址
|
||||
Address targetAddress = new UdpAddress("172.16.18.50/162"); // 接收方的IP和端口
|
||||
CommunityTarget target = new CommunityTarget();
|
||||
target.setCommunity(new OctetString("public")); // 社区字符串
|
||||
target.setAddress(targetAddress);
|
||||
target.setVersion(SnmpConstants.version2c);
|
||||
target.setTimeout(3000);
|
||||
target.setRetries(1);
|
||||
|
||||
// 3. 创建PDU(协议数据单元)
|
||||
PDU pdu = new PDU();
|
||||
|
||||
// 必须的系统信息
|
||||
pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new OctetString("12345")));
|
||||
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID("1.3.6.1.6.3.1.1.5.3")));
|
||||
|
||||
// 添加自定义变量绑定(可选)
|
||||
pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.1.0"), new OctetString("My Device")));
|
||||
pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new OctetString("Critical Error")));
|
||||
|
||||
pdu.setType(PDU.TRAP);
|
||||
|
||||
// 4. 发送Trap
|
||||
snmp.send(pdu, target);
|
||||
System.out.println("SNMPv2c Trap 发送成功");
|
||||
|
||||
snmp.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
package sample;
|
||||
|
||||
import com.sunyard.SydApiException;
|
||||
import com.sunyard.entity.ServerConfig;
|
||||
import com.sunyard.proto.Util;
|
||||
import com.sunyard.snmp.SnmpTrapSender;
|
||||
import com.sunyard.util.CertUtil;
|
||||
import com.sunyard.util.SYMUtil;
|
||||
import org.junit.After;
|
||||
@@ -10,8 +12,10 @@ import org.junit.Test;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
import racal.sunyard.main.SydApiBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
|
||||
@@ -25,21 +29,26 @@ public class Test4NongxinCW {
|
||||
|
||||
byte[] orgData = new byte[100];
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
// 通过设置debug的属性开启debug,如果不设置则默认不打开
|
||||
api = (SydApi4j) new SydApi4j().connect("172.16.17.34", 8889, null, 1000);
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
try {
|
||||
this.api.disconnect();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
// @Before
|
||||
// public void before() {
|
||||
// // 通过设置debug的属性开启debug,如果不设置则默认不打开
|
||||
// api = (SydApi4j) new SydApi4j().connect("172.16.17.34", 8889, null, 1000);
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void connection() {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @After
|
||||
// public void after() {
|
||||
// try {
|
||||
// this.api.disconnect();
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
|
||||
@Test
|
||||
public void nakeSign(){
|
||||
@@ -163,4 +172,38 @@ public class Test4NongxinCW {
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void importAllCert() {
|
||||
List<String> masterNodes = new ArrayList<>();
|
||||
masterNodes.add("192.168.55.80:3336");
|
||||
masterNodes.add("172.16.128.2:8889");
|
||||
masterNodes.add("172.16.128.3:8889");
|
||||
masterNodes.add("172.16.128.4:8889");
|
||||
int timeout = 1000;
|
||||
String pwd = null;
|
||||
String cert = "";
|
||||
SydApi4j sydApi4j = new SydApi4j();
|
||||
// sydApi4j.initSnmpTrapConfig("");
|
||||
sydApi4j.importCertToServers(masterNodes, timeout, cert);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void snmpTest() {
|
||||
try {
|
||||
SnmpTrapSender sender = SnmpTrapSender.getInstance();
|
||||
|
||||
// 发送不同级别的日志
|
||||
sender.sendTrap("Application started", 0);
|
||||
sender.sendTrap("High CPU usage", 1);
|
||||
sender.sendTrap("Database connection lost", 5);
|
||||
|
||||
sender.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user