如何使用 oshi 远程监控 Linux 服务器的性能状态?

How can I remotely monitor the performance status of a Linux server with oshi?

我想用OSHI监控我的远程Linux服务器的性能状态,但是官方的api好像只能监控本机的性能状态。

是否必须将 java 代码放在服务器上才能获取服务器的性能状态?我可以用自己的机器监控远程服务器的性能状态吗?

如您所说,OSHI 仅用于从本地计算机读取信息。您必须 运行 在远程计算机上使用 OSHI 获取统计信息的程序。

Issue 249 on the OSHI project 概述了一些选项,包括 Dropwizard Metrics 库,您可以使用它启用数据的 JMX 端口。但是,以这种方式捕获大量指标可能会增加比需要更多的开销。

或者Jackson's ObjectMapper 能够处理OSHI 的任何对象。您可以轻松地设置 Web 服务器来销售 JSON(或 XML 或 CSV 等)。这是转储整个 SystemInfo 对象的快速示例:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.fasterxml.jackson.databind.ObjectMapper;

import oshi.SystemInfo;

public class WebSocket {
    public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
        ServerSocket server = new ServerSocket(80);
        try {
            System.out.println("Server has started on 127.0.0.1:80.\r\nWaiting for a connection...");
            Socket client = server.accept();
            System.out.println("A client connected.");
            InputStream in = client.getInputStream();
            OutputStream out = client.getOutputStream();
            Scanner s = new Scanner(in, "UTF-8");
            try {
                String data = s.useDelimiter("\r\n\r\n").next();
                Matcher get = Pattern.compile("^GET").matcher(data);
                if (get.find()) {
                    SystemInfo si = new SystemInfo();
                    ObjectMapper mapper = new ObjectMapper();
                    byte[] response = ("HTTP/1.1 200 OK\r\n" + "Content-Type: application/json\r\n"
                            + "Accept: application/json\r\n"
                            // end header
                            + "\r\n"
                            // write JSON
                            + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(si))
                                    .getBytes(StandardCharsets.UTF_8);
                    out.write(response, 0, response.length);
                }
            } finally {
                s.close();
            }
        } finally {
            server.close();
        }
    }
}

在你的Linux服务器上执行上面的class,然后在http://yourserver通过网络浏览器连接到它,你会得到漂亮的OSHI的所有统计数据JSON.

{
  "hardware" : {
    "computerSystem" : {
      "firmware" : {
        "manufacturer" : "Apple Inc.",
        "version" : "1037.40.124.0.0 (iBridge: 17.16.11081.0.0,0)",
        "description" : "EFI64",
        "name" : "boot.efi",
        "releaseDate" : "10/17/2019"
      },

      <snip>

    "processor" : {
      "maxFreq" : 2300000000,
      "currentFreq" : [ 2300000000, 2300000000, 2300000000, 2300000000, 2300000000, 2300000000, 2300000000, 2300000000, 2300000000, 2300000000, 2300000000, 2300000000, 2300000000, 2300000000, 2300000000, 2300000000 ],
      "contextSwitches" : 156099,
      "interrupts" : 1836212,
      "systemCpuLoadTicks" : [ 37060587, 0, 22431664, -1856553863, 0, 0, 0, 0 ],
      "processorCpuLoadTicks" : [ [ 8458566, 0, 7386132, 140274450, 0, 0, 0, 0 ], [ 120919, 0, 109162, 155889021, 0, 0, 0, 0 ], [ 7848726, 0, 4826688, 143443690, 0, 0, 0, 0 ], [ 117655, 0, 116672, 155884776, 0, 0, 0, 0 ], [ 5675383, 0, 3300677, 147143043, 0, 0, 0, 0 ], [ 116186, 0, 111427, 155891490, 0, 0, 0, 0 ], [ 4235682, 0, 2440832, 149442588, 0, 0, 0, 0 ], [ 114003, 0, 111121, 155893978, 0, 0, 0, 0 ], [ 3471025, 0, 1700387, 150947690, 0, 0, 0, 0 ], [ 111883, 0, 101912, 155905307, 0, 0, 0, 0 ], [ 2713535, 0, 962449, 152443118, 0, 0, 0, 0 ], [ 109036, 0, 73592, 155936474, 0, 0, 0, 0 ], [ 2023118, 0, 626887, 153469097, 0, 0, 0, 0 ], [ 104912, 0, 50922, 155963268, 0, 0, 0, 0 ], [ 1738287, 0, 472098, 153908718, 0, 0, 0, 0 ], [ 101671, 0, 40706, 155976725, 0, 0, 0, 0 ] ],
      "physicalPackageCount" : 1,
      "physicalProcessorCount" : 8,
      "logicalProcessorCount" : 16,

      <snip>

      "identifier" : "Intel64 Family 6 Model 158 Stepping 13",
      "model" : "158",
      "processorIdentifier" : {
        "processorID" : "BFEBFBFF000906ED",
        "cpu64bit" : true,
        "identifier" : "Intel64 Family 6 Model 158 Stepping 13",
        "model" : "158",
        "vendor" : "GenuineIntel",
        "stepping" : "13",
        "vendorFreq" : 2300000000,
        "family" : "6",
        "name" : "Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz"
      },
      "vendor" : "GenuineIntel",
      "stepping" : "13",
      "processorID" : "BFEBFBFF000906ED",
      "cpu64bit" : true,
      "vendorFreq" : 2300000000,
      "family" : "6",
      "name" : "Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz"
    },
    "memory" : {
      "available" : 13347446784,
      "total" : 34359738368,
      "pageSize" : 4096,
      "virtualMemory" : {
        "swapTotal" : 7516192768,
        "swapUsed" : 5921832960,
        "swapPagesIn" : 71667220,
        "swapPagesOut" : 809694
      },
      "physicalMemory" : [ {
        "bankLabel" : "BANK 0/ChannelA-DIMM",
        "capacity" : 17179869184,
        "clockSpeed" : 2400000000,
        "manufacturer" : "Micron",
        "memoryType" : "DDR4"
      }, {
        "bankLabel" : "BANK 2/ChannelB-DIMM",
        "capacity" : 17179869184,
        "clockSpeed" : 2400000000,
        "manufacturer" : "Micron",
        "memoryType" : "DDR4"
      } ]
    },

显然您希望建立一个比网络浏览器更好的机制 JSON,并可能将您的响应范围缩小到您关心的对象。但希望这表明将 OSHI 包含到任何现有的基于 Java 的网络响应中是多么容易。