Skip to content

Commit

Permalink
ref[monitor]: move monitor to MonitorUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed May 7, 2024
1 parent 5019a89 commit 008c935
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 63 deletions.
106 changes: 106 additions & 0 deletions monitor/src/main/java/com/zfoo/monitor/util/MonitorUtils.java
Original file line number Diff line number Diff line change
@@ -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<String, DiskFileSystem> maxDfMap;
private static Memory maxFree;
private static Map<String, Sar> 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);
}

}
68 changes: 8 additions & 60 deletions monitor/src/main/java/com/zfoo/monitor/util/OSUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,66 +249,20 @@ public static List<Sar> sar() {
return sars;
}

private static Uptime maxUptime;
private static Map<String, DiskFileSystem> maxDfMap;
private static Memory maxFree;
private static Map<String, Sar> 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;
Expand Down Expand Up @@ -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);
}
}
7 changes: 4 additions & 3 deletions monitor/src/test/java/com/zfoo/monitor/ApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 008c935

Please sign in to comment.