Przeglądaj źródła

update agent module

reghao 9 miesięcy temu
rodzic
commit
8b7fde268f

+ 54 - 3
agent/src/test/java/AgentTest.java

@@ -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());

+ 26 - 0
agent/src/test/java/ShellTest.java

@@ -0,0 +1,26 @@
+import cn.reghao.jutil.jdk.shell.ShellExecutor;
+import cn.reghao.jutil.jdk.shell.ShellResult;
+
+import java.util.List;
+
+/**
+ * @author reghao
+ * @date 2025-05-26 10:29:04
+ */
+public class ShellTest {
+    static void execShell() {
+        ShellExecutor shellExecutor = new ShellExecutor();
+        String cmd1 = "/usr/bin/nginx -t";
+        String cmd2 = "/usr/bin/nginx -s reload";
+        List<String> list = List.of(cmd1, cmd2);
+        for (String cmd : list) {
+            ShellResult shellResult = shellExecutor.exec(cmd.split("\\s+"));
+            String result = shellResult.getResult();
+            System.out.println(result);
+        }
+    }
+
+    public static void main(String[] args) {
+        execShell();
+    }
+}