/* * 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(); } }