我想使用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服务器上执行上述类,然后通过网络浏览器http://yourserver连接到该类,您将以漂亮的JSON格式获取所有OSHI的统计信息。
{
  "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"
      } ]
    },

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

07-27 18:02