|
|
@@ -1,16 +1,12 @@
|
|
|
import ch.qos.logback.classic.Level;
|
|
|
import ch.qos.logback.classic.Logger;
|
|
|
import ch.qos.logback.classic.LoggerContext;
|
|
|
-import cn.reghao.bnt.common.docker.DockerImpl;
|
|
|
import cn.reghao.bnt.common.machine.model.NetworkCard;
|
|
|
import cn.reghao.jutil.jdk.converter.ByteConverter;
|
|
|
import cn.reghao.jutil.jdk.converter.DateTimeConverter;
|
|
|
-import cn.reghao.jutil.jdk.converter.IpAddressConverter;
|
|
|
import cn.reghao.jutil.jdk.thread.ThreadPoolWrapper;
|
|
|
-import com.github.dockerjava.api.command.InspectContainerResponse;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.Getter;
|
|
|
-import lombok.Setter;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import oshi.SystemInfo;
|
|
|
import oshi.hardware.*;
|
|
|
@@ -273,48 +269,6 @@ public class AgentTest {
|
|
|
rootLogger.setLevel(Level.INFO);
|
|
|
}
|
|
|
|
|
|
- static List<ListenProcess> getListenProcesses() {
|
|
|
- IpAddressConverter ipAddressConverter = new IpAddressConverter();
|
|
|
- List<SysProcess> sysProcessList = new ArrayList<>();
|
|
|
- os.getProcesses().forEach(osProcess -> {
|
|
|
- int pid = osProcess.getProcessID();
|
|
|
- String name = osProcess.getName();
|
|
|
- int ppid = osProcess.getParentProcessID();
|
|
|
- String state = osProcess.getState().name();
|
|
|
- List<OSThread> osThreadList = osProcess.getThreadDetails();
|
|
|
- sysProcessList.add(new SysProcess(pid, name, ppid));
|
|
|
- });
|
|
|
-
|
|
|
- String state = InternetProtocolStats.TcpState.LISTEN.name();
|
|
|
- List<ListenProcess> list = new ArrayList<>();
|
|
|
- os.getInternetProtocolStats().getConnections().forEach(ipConnection -> {
|
|
|
- if (!ipConnection.getState().name().equals(state)) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- String type = ipConnection.getType();
|
|
|
- int pid = ipConnection.getowningProcessId();
|
|
|
- byte[] localAddress = ipConnection.getLocalAddress();
|
|
|
- int localPort = ipConnection.getLocalPort();
|
|
|
- byte[] remoteAddress = ipConnection.getForeignAddress();
|
|
|
- int remotePort = ipConnection.getForeignPort();
|
|
|
- System.out.printf("%s %s %s %s\n", pid, type, state, localPort);
|
|
|
- list.add(new ListenProcess(pid, type, localPort));
|
|
|
- });
|
|
|
-
|
|
|
- Map<Integer, List<ListenProcess>> groupMap = list.stream().collect(Collectors.groupingBy(ListenProcess::getPid));
|
|
|
- os.getProcesses().forEach(osProcess -> {
|
|
|
- int pid = osProcess.getProcessID();
|
|
|
- List<ListenProcess> list1 = groupMap.get(pid);
|
|
|
- if (list1 != null && !list1.isEmpty()) {
|
|
|
- String name = osProcess.getName();
|
|
|
- list1.get(0).setName(name);
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
static OSProcess getProcess(int pid) {
|
|
|
List<OSProcess> list = os.getProcesses().stream()
|
|
|
.filter(osProcess -> osProcess.getProcessID() == pid)
|
|
|
@@ -337,47 +291,6 @@ public class AgentTest {
|
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
- static void dockerListTest() {
|
|
|
- List<ListenProcess> listenProcessList = getListenProcesses();
|
|
|
- Map<Integer, List<ListenProcess>> groupMap = listenProcessList.stream().collect(Collectors.groupingBy(ListenProcess::getPid));
|
|
|
-
|
|
|
- DockerImpl docker = new DockerImpl();
|
|
|
- List<InspectContainerResponse> list = docker.psAll();
|
|
|
- List<DockerProcess> dockerProcessList = new ArrayList<>();
|
|
|
- for (InspectContainerResponse response : list) {
|
|
|
- String imageId = response.getImageId();
|
|
|
- Boolean running = response.getState().getRunning();
|
|
|
- if (running != null && running) {
|
|
|
- Integer pid = response.getState().getPid();
|
|
|
- if (pid == null) {
|
|
|
- pid = -1;
|
|
|
- }
|
|
|
-
|
|
|
- String containerId = response.getId();
|
|
|
- String appId = response.getName().replace("/", "");
|
|
|
- System.out.printf("%s: %s -> %s\n", pid, containerId, appId);
|
|
|
- dockerProcessList.add(new DockerProcess(pid, containerId, appId));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- System.out.println();
|
|
|
- }
|
|
|
-
|
|
|
- @Setter
|
|
|
- @Getter
|
|
|
- static class ListenProcess {
|
|
|
- private int pid;
|
|
|
- private String name;
|
|
|
- private String type;
|
|
|
- private int port;
|
|
|
-
|
|
|
- public ListenProcess(int pid, String type, int port) {
|
|
|
- this.pid = pid;
|
|
|
- this.type = type;
|
|
|
- this.port = port;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
@AllArgsConstructor
|
|
|
@Getter
|
|
|
static class DockerProcess {
|
|
|
@@ -386,14 +299,6 @@ public class AgentTest {
|
|
|
private String appId;
|
|
|
}
|
|
|
|
|
|
- @AllArgsConstructor
|
|
|
- @Getter
|
|
|
- static class SysProcess {
|
|
|
- private int pid;
|
|
|
- private String name;
|
|
|
- private int ppid;
|
|
|
- }
|
|
|
-
|
|
|
static SystemInfo si = new SystemInfo();
|
|
|
static HardwareAbstractionLayer hal = si.getHardware();
|
|
|
static OperatingSystem os = si.getOperatingSystem();
|
|
|
@@ -422,8 +327,8 @@ public class AgentTest {
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
//setLogLevel();
|
|
|
- int pid = 1483161;
|
|
|
- getProcessInfo(pid);
|
|
|
+ //int pid = 1483161;
|
|
|
+ //getProcessInfo(pid);
|
|
|
//List<InternetProtocolStats.IPConnection> processConnections = getProcessConnections(pid, tcpState);
|
|
|
|
|
|
/*getListenProcesses();
|