aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2022-08-07 21:53:11 +0100
committerLuck <git@lucko.me>2022-08-07 21:53:11 +0100
commitc96f88f7c125109edcae382b8d10011e87125102 (patch)
tree41f6009c3700297558df2bf5205fe98307775cde /spark-common/src
parent1ebcc65460e85afd10b7e276892f2eb2f236701f (diff)
downloadspark-c96f88f7c125109edcae382b8d10011e87125102.tar.gz
spark-c96f88f7c125109edcae382b8d10011e87125102.tar.bz2
spark-c96f88f7c125109edcae382b8d10011e87125102.zip
Refactor Windows wmic utility
Diffstat (limited to 'spark-common/src')
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/monitor/LinuxProc.java (renamed from spark-common/src/main/java/me/lucko/spark/common/util/LinuxProc.java)2
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/monitor/WindowsWmic.java74
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/monitor/cpu/CpuInfo.java32
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/monitor/memory/MemoryInfo.java2
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/monitor/net/NetworkInterfaceInfo.java2
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/monitor/os/OperatingSystemInfo.java90
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/platform/PlatformStatisticsProvider.java7
7 files changed, 141 insertions, 68 deletions
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/monitor/LinuxProc.java
index 44fd3f9..563e247 100644
--- a/spark-common/src/main/java/me/lucko/spark/common/util/LinuxProc.java
+++ b/spark-common/src/main/java/me/lucko/spark/common/monitor/LinuxProc.java
@@ -18,7 +18,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-package me.lucko.spark.common.util;
+package me.lucko.spark.common.monitor;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
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) <luck@lucko.me>
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+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<String> read() {
+ if (SUPPORTED) {
+ ProcessBuilder process = new ProcessBuilder(this.cmdArgs).redirectErrorStream(true);
+ try (BufferedReader buf = new BufferedReader(new InputStreamReader(process.start().getInputStream()))) {
+ List<String> 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()