|
|
@@ -1,6 +1,8 @@
|
|
|
import ch.qos.logback.classic.Level;
|
|
|
import ch.qos.logback.classic.Logger;
|
|
|
import ch.qos.logback.classic.LoggerContext;
|
|
|
+import cn.reghao.jutil.jdk.converter.IpAddressConverter;
|
|
|
+import cn.reghao.jutil.jdk.thread.ThreadPoolWrapper;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import oshi.SystemInfo;
|
|
|
import oshi.hardware.*;
|
|
|
@@ -11,6 +13,8 @@ import oshi.util.Util;
|
|
|
import java.io.IOException;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.ScheduledExecutorService;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.function.Predicate;
|
|
|
|
|
|
/**
|
|
|
@@ -228,18 +232,65 @@ public class AgentTest {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) throws IOException {
|
|
|
+ static void setLogLevel() {
|
|
|
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
|
|
|
Logger rootLogger = loggerContext.getLogger("ROOT");
|
|
|
rootLogger.setLevel(Level.INFO);
|
|
|
+ }
|
|
|
+
|
|
|
+ static class StatTask implements Runnable {
|
|
|
+ private final OperatingSystem os;
|
|
|
+
|
|
|
+ public StatTask(OperatingSystem os) {
|
|
|
+ this.os = os;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void run() {
|
|
|
+ os.getProcesses().forEach(osProcess -> {
|
|
|
+ int pid = osProcess.getProcessID();
|
|
|
+ String name = osProcess.getName();
|
|
|
+ System.out.printf("%s -> %s\n", pid, name);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws IOException, InterruptedException {
|
|
|
+ //setLogLevel();
|
|
|
+ ScheduledExecutorService scheduler = ThreadPoolWrapper.scheduledThreadPool("heartbeat", 5);
|
|
|
|
|
|
SystemInfo si = new SystemInfo();
|
|
|
HardwareAbstractionLayer hal = si.getHardware();
|
|
|
OperatingSystem os = si.getOperatingSystem();
|
|
|
|
|
|
- Predicate<OSProcess> chromeProcessFilter = p -> p.getName().equalsIgnoreCase("Chrome");
|
|
|
+ StatTask statTask = new StatTask(os);
|
|
|
+ scheduler.scheduleAtFixedRate(statTask, 0, 1, TimeUnit.SECONDS);
|
|
|
+ Thread.sleep(3600_000);
|
|
|
+
|
|
|
+ os.getProcesses().forEach(osProcess -> {
|
|
|
+ int pid = osProcess.getProcessID();
|
|
|
+ String name = osProcess.getName();
|
|
|
+ System.out.printf("%s -> %s\n", pid, name);
|
|
|
+ });
|
|
|
+
|
|
|
+ IpAddressConverter ipAddressConverter = new IpAddressConverter();
|
|
|
+ os.getInternetProtocolStats().getConnections().forEach(ipConnection -> {
|
|
|
+ String type = ipConnection.getType();
|
|
|
+ String state = ipConnection.getState().name();
|
|
|
+ int pid = ipConnection.getowningProcessId();
|
|
|
+ byte[] localAddress = ipConnection.getLocalAddress();
|
|
|
+ int localPort = ipConnection.getLocalPort();
|
|
|
+ byte[] remoteAddress = ipConnection.getForeignAddress();
|
|
|
+ int remotePort = ipConnection.getForeignPort();
|
|
|
+
|
|
|
+ if (localPort != 3306) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ System.out.printf("%s %s %s %s\n", pid, type, state, localPort);
|
|
|
+ });
|
|
|
+
|
|
|
+ /*Predicate<OSProcess> chromeProcessFilter = p -> p.getName().equalsIgnoreCase("Chrome");
|
|
|
List<OSProcess> procs = os.getProcesses(chromeProcessFilter, OperatingSystem.ProcessSorting.CPU_DESC, 10);
|
|
|
- procs.forEach(System.out::println);
|
|
|
+ procs.forEach(System.out::println);*/
|
|
|
/*
|
|
|
System.out.println("Checking computer system...");
|
|
|
printComputerSystem(hal.getComputerSystem());
|