From c96f88f7c125109edcae382b8d10011e87125102 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 7 Aug 2022 21:53:11 +0100 Subject: Refactor Windows wmic utility --- .../me/lucko/spark/common/monitor/LinuxProc.java | 89 +++++++++++++++++++++ .../me/lucko/spark/common/monitor/WindowsWmic.java | 74 ++++++++++++++++++ .../me/lucko/spark/common/monitor/cpu/CpuInfo.java | 32 +++----- .../spark/common/monitor/memory/MemoryInfo.java | 2 +- .../common/monitor/net/NetworkInterfaceInfo.java | 2 +- .../common/monitor/os/OperatingSystemInfo.java | 90 ++++++++++++---------- .../platform/PlatformStatisticsProvider.java | 7 +- .../java/me/lucko/spark/common/util/LinuxProc.java | 89 --------------------- 8 files changed, 229 insertions(+), 156 deletions(-) create mode 100644 spark-common/src/main/java/me/lucko/spark/common/monitor/LinuxProc.java create mode 100644 spark-common/src/main/java/me/lucko/spark/common/monitor/WindowsWmic.java delete mode 100644 spark-common/src/main/java/me/lucko/spark/common/util/LinuxProc.java (limited to 'spark-common/src/main') diff --git a/spark-common/src/main/java/me/lucko/spark/common/monitor/LinuxProc.java b/spark-common/src/main/java/me/lucko/spark/common/monitor/LinuxProc.java new file mode 100644 index 0000000..563e247 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/monitor/LinuxProc.java @@ -0,0 +1,89 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.monitor; + +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collections; +import java.util.List; + +/** + * Utility for reading from /proc/ on Linux systems. + */ +public enum LinuxProc { + + /** + * Information about the system CPU. + */ + CPUINFO("/proc/cpuinfo"), + + /** + * Information about the system memory. + */ + MEMINFO("/proc/meminfo"), + + /** + * Information about the system network usage. + */ + NET_DEV("/proc/net/dev"), + + /** + * Information about the operating system distro. + */ + OSINFO("/etc/os-release"); + + private final Path path; + + LinuxProc(String path) { + this.path = resolvePath(path); + } + + private static @Nullable Path resolvePath(String path) { + try { + Path p = Paths.get(path); + if (Files.isReadable(p)) { + return p; + } + } catch (Exception e) { + // ignore + } + return null; + } + + public @NonNull List read() { + if (this.path != null) { + try { + return Files.readAllLines(this.path, StandardCharsets.UTF_8); + } catch (IOException e) { + // ignore + } + } + + return Collections.emptyList(); + } + +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/monitor/WindowsWmic.java b/spark-common/src/main/java/me/lucko/spark/common/monitor/WindowsWmic.java new file mode 100644 index 0000000..6b602d9 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/monitor/WindowsWmic.java @@ -0,0 +1,74 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.monitor; + +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Utility for reading from wmic (Windows Management Instrumentation Commandline) on Windows systems. + */ +public enum WindowsWmic { + + /** + * Gets the CPU name + */ + CPU_GET_NAME("wmic", "cpu", "get", "name", "/FORMAT:list"), + + /** + * Gets the operating system name (caption) and version. + */ + OS_GET_CAPTION_AND_VERSION("wmic", "os", "get", "caption,version", "/FORMAT:list"); + + private static final boolean SUPPORTED = System.getProperty("os.name").startsWith("Windows"); + + private final String[] cmdArgs; + + WindowsWmic(String... cmdArgs) { + this.cmdArgs = cmdArgs; + } + + public @NonNull List read() { + if (SUPPORTED) { + ProcessBuilder process = new ProcessBuilder(this.cmdArgs).redirectErrorStream(true); + try (BufferedReader buf = new BufferedReader(new InputStreamReader(process.start().getInputStream()))) { + List lines = new ArrayList<>(); + + String line; + while ((line = buf.readLine()) != null) { + lines.add(line); + } + + return lines; + } catch (Exception e) { + // ignore + } + } + + return Collections.emptyList(); + } +} + diff --git a/spark-common/src/main/java/me/lucko/spark/common/monitor/cpu/CpuInfo.java b/spark-common/src/main/java/me/lucko/spark/common/monitor/cpu/CpuInfo.java index fcb70c0..9954bd5 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/monitor/cpu/CpuInfo.java +++ b/spark-common/src/main/java/me/lucko/spark/common/monitor/cpu/CpuInfo.java @@ -20,11 +20,9 @@ package me.lucko.spark.common.monitor.cpu; -import me.lucko.spark.common.util.LinuxProc; +import me.lucko.spark.common.monitor.LinuxProc; +import me.lucko.spark.common.monitor.WindowsWmic; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; import java.util.regex.Pattern; /** @@ -41,27 +39,19 @@ public enum CpuInfo { * @return the cpu model */ public static String queryCpuModel() { - if (System.getProperty("os.name").startsWith("Windows")) { - final String[] args = { "wmic", "cpu", "get", "name", "/FORMAT:list" }; - try (final BufferedReader buf = new BufferedReader(new InputStreamReader(new ProcessBuilder(args).redirectErrorStream(true).start().getInputStream()))) { - String line; - while ((line = buf.readLine()) != null) { - if (line.startsWith("Name")) { - return line.substring(5).trim(); - } - } - } catch (final IOException e) { - return ""; + for (String line : LinuxProc.CPUINFO.read()) { + String[] splitLine = SPACE_COLON_SPACE_PATTERN.split(line); + if (splitLine[0].equals("model name") || splitLine[0].equals("Processor")) { + return splitLine[1]; } - } else { - for (String line : LinuxProc.CPUINFO.read()) { - String[] splitLine = SPACE_COLON_SPACE_PATTERN.split(line); + } - if (splitLine[0].equals("model name") || splitLine[0].equals("Processor")) { - return splitLine[1]; - } + for (String line : WindowsWmic.CPU_GET_NAME.read()) { + if (line.startsWith("Name")) { + return line.substring(5).trim(); } } + return ""; } diff --git a/spark-common/src/main/java/me/lucko/spark/common/monitor/memory/MemoryInfo.java b/spark-common/src/main/java/me/lucko/spark/common/monitor/memory/MemoryInfo.java index 226f75b..8f63f71 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/monitor/memory/MemoryInfo.java +++ b/spark-common/src/main/java/me/lucko/spark/common/monitor/memory/MemoryInfo.java @@ -20,7 +20,7 @@ package me.lucko.spark.common.monitor.memory; -import me.lucko.spark.common.util.LinuxProc; +import me.lucko.spark.common.monitor.LinuxProc; import java.lang.management.ManagementFactory; import java.util.regex.Matcher; diff --git a/spark-common/src/main/java/me/lucko/spark/common/monitor/net/NetworkInterfaceInfo.java b/spark-common/src/main/java/me/lucko/spark/common/monitor/net/NetworkInterfaceInfo.java index bd9e187..332077a 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/monitor/net/NetworkInterfaceInfo.java +++ b/spark-common/src/main/java/me/lucko/spark/common/monitor/net/NetworkInterfaceInfo.java @@ -22,7 +22,7 @@ package me.lucko.spark.common.monitor.net; import com.google.common.collect.ImmutableMap; -import me.lucko.spark.common.util.LinuxProc; +import me.lucko.spark.common.monitor.LinuxProc; import org.checkerframework.checker.nullness.qual.NonNull; diff --git a/spark-common/src/main/java/me/lucko/spark/common/monitor/os/OperatingSystemInfo.java b/spark-common/src/main/java/me/lucko/spark/common/monitor/os/OperatingSystemInfo.java index 4eeebd1..1c2732c 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/monitor/os/OperatingSystemInfo.java +++ b/spark-common/src/main/java/me/lucko/spark/common/monitor/os/OperatingSystemInfo.java @@ -20,59 +20,67 @@ package me.lucko.spark.common.monitor.os; -import me.lucko.spark.common.util.LinuxProc; +import me.lucko.spark.common.monitor.LinuxProc; +import me.lucko.spark.common.monitor.WindowsWmic; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; +/** + * Small utility to query the operating system name & version. + */ +public final class OperatingSystemInfo { + private final String name; + private final String version; + private final String arch; + + public OperatingSystemInfo(String name, String version, String arch) { + this.name = name; + this.version = version; + this.arch = arch; + } -public enum OperatingSystemInfo { - ; + public String name() { + return this.name; + } - private static String name = null; - private static String version = null; + public String version() { + return this.version; + } + + public String arch() { + return this.arch; + } - static { - final String osNameJavaProp = System.getProperty("os.name"); + public static OperatingSystemInfo poll() { + String name = null; + String version = null; - if (osNameJavaProp.startsWith("Windows")) { - final String[] args = { "wmic", "os", "get", "caption,version", "/FORMAT:list" }; - try (final BufferedReader buf = new BufferedReader(new InputStreamReader(new ProcessBuilder(args).redirectErrorStream(true).start().getInputStream()))) { - String line; - while ((line = buf.readLine()) != null) { - if (line.startsWith("Caption")) { - name = line.substring(18).trim(); - } else if (line.startsWith("Version")) { - version = line.substring(8).trim(); - } - } - } catch (final IOException | IndexOutOfBoundsException e) { - // ignore + for (String line : LinuxProc.OSINFO.read()) { + if (line.startsWith("PRETTY_NAME") && line.length() > 13) { + name = line.substring(13).replace('"', ' ').trim(); } - } else { - for (final String line : LinuxProc.OSINFO.read()) { - if (line.startsWith("PRETTY_NAME")) { - try { - name = line.substring(13).replace('"', ' ').trim(); - } catch (final IndexOutOfBoundsException e) { - // ignore - } - } + } + + for (String line : WindowsWmic.OS_GET_CAPTION_AND_VERSION.read()) { + if (line.startsWith("Caption") && line.length() > 18) { + // Caption=Microsoft Windows something + // \----------------/ = 18 chars + name = line.substring(18).trim(); + } else if (line.startsWith("Version")) { + // Version=10.0.something + // \------/ = 8 chars + version = line.substring(8).trim(); } } - if (name == null) - name = osNameJavaProp; + if (name == null) { + name = System.getProperty("os.name"); + } - if (version == null) + if (version == null) { version = System.getProperty("os.version"); - } + } - public static String getName() { - return name; - } + String arch = System.getProperty("os.arch"); - public static String getVersion() { - return version; + return new OperatingSystemInfo(name, version, arch); } } diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/PlatformStatisticsProvider.java b/spark-common/src/main/java/me/lucko/spark/common/platform/PlatformStatisticsProvider.java index e5be647..1eb9753 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/platform/PlatformStatisticsProvider.java +++ b/spark-common/src/main/java/me/lucko/spark/common/platform/PlatformStatisticsProvider.java @@ -51,6 +51,7 @@ public class PlatformStatisticsProvider { public SystemStatistics getSystemStatistics() { RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean(); + OperatingSystemInfo osInfo = OperatingSystemInfo.poll(); SystemStatistics.Builder builder = SystemStatistics.newBuilder() .setCpu(SystemStatistics.Cpu.newBuilder() @@ -87,9 +88,9 @@ public class PlatformStatisticsProvider { .build() ) .setOs(SystemStatistics.Os.newBuilder() - .setArch(System.getProperty("os.arch")) - .setName(OperatingSystemInfo.getName()) - .setVersion(OperatingSystemInfo.getVersion()) + .setArch(osInfo.arch()) + .setName(osInfo.name()) + .setVersion(osInfo.version()) .build() ) .setJava(SystemStatistics.Java.newBuilder() diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/LinuxProc.java b/spark-common/src/main/java/me/lucko/spark/common/util/LinuxProc.java deleted file mode 100644 index 44fd3f9..0000000 --- a/spark-common/src/main/java/me/lucko/spark/common/util/LinuxProc.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * This file is part of spark. - * - * Copyright (c) lucko (Luck) - * Copyright (c) contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package me.lucko.spark.common.util; - -import org.checkerframework.checker.nullness.qual.NonNull; -import org.checkerframework.checker.nullness.qual.Nullable; - -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Collections; -import java.util.List; - -/** - * Utility for reading from /proc/ on Linux systems. - */ -public enum LinuxProc { - - /** - * Information about the system CPU. - */ - CPUINFO("/proc/cpuinfo"), - - /** - * Information about the system memory. - */ - MEMINFO("/proc/meminfo"), - - /** - * Information about the system network usage. - */ - NET_DEV("/proc/net/dev"), - - /** - * Information about the operating system distro. - */ - OSINFO("/etc/os-release"); - - private final Path path; - - LinuxProc(String path) { - this.path = resolvePath(path); - } - - private static @Nullable Path resolvePath(String path) { - try { - Path p = Paths.get(path); - if (Files.isReadable(p)) { - return p; - } - } catch (Exception e) { - // ignore - } - return null; - } - - public @NonNull List read() { - if (this.path != null) { - try { - return Files.readAllLines(this.path, StandardCharsets.UTF_8); - } catch (IOException e) { - // ignore - } - } - - return Collections.emptyList(); - } - -} -- cgit