From 008c9352fbe944e7913d5bcc6391732f323ffe19 Mon Sep 17 00:00:00 2001 From: godotg Date: Tue, 7 May 2024 11:11:36 +0800 Subject: [PATCH] ref[monitor]: move monitor to MonitorUtils --- .../com/zfoo/monitor/util/MonitorUtils.java | 106 ++++++++++++++++++ .../java/com/zfoo/monitor/util/OSUtils.java | 68 ++--------- .../com/zfoo/monitor/ApplicationTest.java | 7 +- 3 files changed, 118 insertions(+), 63 deletions(-) create mode 100644 monitor/src/main/java/com/zfoo/monitor/util/MonitorUtils.java diff --git a/monitor/src/main/java/com/zfoo/monitor/util/MonitorUtils.java b/monitor/src/main/java/com/zfoo/monitor/util/MonitorUtils.java new file mode 100644 index 000000000..e04540b7f --- /dev/null +++ b/monitor/src/main/java/com/zfoo/monitor/util/MonitorUtils.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.monitor.util; + +import com.zfoo.monitor.*; +import com.zfoo.net.util.NetUtils; +import com.zfoo.protocol.exception.RunException; +import com.zfoo.protocol.util.FileUtils; +import com.zfoo.protocol.util.IOUtils; +import com.zfoo.protocol.util.StringUtils; +import com.zfoo.protocol.util.UuidUtils; +import com.zfoo.scheduler.util.TimeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import oshi.hardware.HWDiskStore; +import oshi.hardware.HardwareAbstractionLayer; +import oshi.hardware.NetworkIF; +import oshi.software.os.OperatingSystem; + +import java.io.File; +import java.io.InputStream; +import java.text.NumberFormat; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + +/** + * 监控工具类 + * + * @author godotg + */ +public abstract class MonitorUtils { + + private static final Logger logger = LoggerFactory.getLogger(MonitorUtils.class); + + private static Uptime maxUptime; + private static Map maxDfMap; + private static Memory maxFree; + private static Map maxSarMap; + + static { + initMonitor(); + } + + public static void initMonitor() { + maxUptime = OSUtils.uptime(); + maxDfMap = new ConcurrentHashMap<>(OSUtils.df().stream().collect(Collectors.toMap(key -> key.getName(), value -> value))); + maxFree = OSUtils.free(); + maxSarMap = new ConcurrentHashMap<>(OSUtils.sar().stream().collect(Collectors.toMap(key -> key.getName(), value -> value))); + } + + public static Monitor maxMonitor() { + var uuid = UuidUtils.getUUID(); + var monitor = Monitor.valueOf(uuid, maxUptime, new ArrayList<>(maxDfMap.values()), maxFree, new ArrayList<>(maxSarMap.values())); + + initMonitor(); + return monitor; + } + + public static Monitor monitor() { + var uuid = UuidUtils.getUUID(); + var uptime = OSUtils.uptime(); + var df = OSUtils.df(); + var free = OSUtils.free(); + var sar = OSUtils.sar(); + + if (uptime.compareTo(maxUptime) > 0) { + maxUptime = uptime; + } + + for (var fileSystem : df) { + var maxFileSystem = maxDfMap.get(fileSystem.getName()); + if (maxFileSystem != null && fileSystem.compareTo(maxFileSystem) > 0) { + maxDfMap.put(fileSystem.getName(), fileSystem); + } + } + + if (free.compareTo(maxFree) > 0) { + maxFree = free; + } + + for (var networkIF : sar) { + var maxNetworkIF = maxSarMap.get(networkIF.getName()); + if (maxNetworkIF != null && networkIF.compareTo(maxNetworkIF) > 0) { + maxSarMap.put(maxNetworkIF.getName(), networkIF); + } + } + + return Monitor.valueOf(uuid, uptime, df, free, sar); + } + +} diff --git a/monitor/src/main/java/com/zfoo/monitor/util/OSUtils.java b/monitor/src/main/java/com/zfoo/monitor/util/OSUtils.java index 873872365..06f62ad35 100644 --- a/monitor/src/main/java/com/zfoo/monitor/util/OSUtils.java +++ b/monitor/src/main/java/com/zfoo/monitor/util/OSUtils.java @@ -249,66 +249,20 @@ public static List sar() { return sars; } - private static Uptime maxUptime; - private static Map maxDfMap; - private static Memory maxFree; - private static Map maxSarMap; - - static { - initMonitor(); - } - - public static void initMonitor() { - maxUptime = uptime(); - maxDfMap = new ConcurrentHashMap<>(df().stream().collect(Collectors.toMap(key -> key.getName(), value -> value))); - maxFree = free(); - maxSarMap = new ConcurrentHashMap<>(sar().stream().collect(Collectors.toMap(key -> key.getName(), value -> value))); - } - - public static Monitor maxMonitor() { - var uuid = UuidUtils.getUUID(); - var monitor = Monitor.valueOf(uuid, maxUptime, new ArrayList<>(maxDfMap.values()), maxFree, new ArrayList<>(maxSarMap.values())); - - initMonitor(); - return monitor; - } - - public static Monitor monitor() { - var uuid = UuidUtils.getUUID(); - var uptime = uptime(); - var df = df(); - var free = free(); - var sar = sar(); - - if (uptime.compareTo(maxUptime) > 0) { - maxUptime = uptime; - } - - for (var fileSystem : df) { - var maxFileSystem = maxDfMap.get(fileSystem.getName()); - if (maxFileSystem != null && fileSystem.compareTo(maxFileSystem) > 0) { - maxDfMap.put(fileSystem.getName(), fileSystem); - } - } - - if (free.compareTo(maxFree) > 0) { - maxFree = free; - } - - for (var networkIF : sar) { - var maxNetworkIF = maxSarMap.get(networkIF.getName()); - if (maxNetworkIF != null && networkIF.compareTo(maxNetworkIF) > 0) { - maxSarMap.put(maxNetworkIF.getName(), networkIF); - } - } - - return Monitor.valueOf(uuid, uptime, df, free, sar); + // ----------------------------------------------------------------------------------------------------------------- + public static SystemInfo os() { + var processor = hardware.getProcessor(); + var cpuLogicCore = processor.getLogicalProcessorCount(); + var cpuName = processor.getProcessorIdentifier().getName(); + return SystemInfo.valueOf(NetUtils.getLocalhostStr(), os.toString(), os.toString(), cpuLogicCore, cpuName); } + // ----------------------------------------------------------------------------------------------------------------- public static String execCommand(String command) { return execCommand(command, null); } + public static String execCommand(String command, String workingDirectory) { Process process = null; InputStream inputStream = null; @@ -354,10 +308,4 @@ public static String execCommand(String command, String workingDirectory) { return StringUtils.EMPTY; } - public static SystemInfo os() { - var processor = hardware.getProcessor(); - var cpuLogicCore = processor.getLogicalProcessorCount(); - var cpuName = processor.getProcessorIdentifier().getName(); - return SystemInfo.valueOf(NetUtils.getLocalhostStr(), os.toString(), os.toString(), cpuLogicCore, cpuName); - } } diff --git a/monitor/src/test/java/com/zfoo/monitor/ApplicationTest.java b/monitor/src/test/java/com/zfoo/monitor/ApplicationTest.java index 65ba8860a..aa002f74d 100644 --- a/monitor/src/test/java/com/zfoo/monitor/ApplicationTest.java +++ b/monitor/src/test/java/com/zfoo/monitor/ApplicationTest.java @@ -14,6 +14,7 @@ package com.zfoo.monitor; import com.zfoo.monitor.util.JvmUtils; +import com.zfoo.monitor.util.MonitorUtils; import com.zfoo.monitor.util.OSUtils; import com.zfoo.protocol.util.JsonUtils; import com.zfoo.protocol.util.ThreadUtils; @@ -97,12 +98,12 @@ public void toPercentTest() { @Test public void monitorTest() { - var monitor = OSUtils.monitor(); + var monitor = MonitorUtils.monitor(); System.out.println(monitor); ThreadUtils.sleep(1000); - monitor = OSUtils.monitor(); + monitor = MonitorUtils.monitor(); System.out.println(monitor); - monitor = OSUtils.maxMonitor(); + monitor = MonitorUtils.maxMonitor(); System.out.println(monitor); }