我想用OSHI来监控我的远程Linux服务器的性能状态,但是官方的api好像只监控机器的性能状态。
我是否必须将java代码放在服务器上才能获得服务器的性能状态?我可以使用自己的机器来监控远程服务器的性能状态吗?
我想用OSHI来监控我的远程Linux服务器的性能状态,但是官方的api好像只监控机器的性能状态。
我是否必须将java代码放在服务器上才能获得服务器的性能状态?我可以使用自己的机器来监控远程服务器的性能状态吗?
正如您所指出的,OSHI 仅用于从本地计算机读取信息。您必须使用 OSHI 在远程计算机上运行程序来获取统计信息。
OSHI 项目的第 249 期概述了一些选项,包括 Dropwizard Metrics 库,您可以使用它启用带有数据的 JMX 端口。但是,以这种方式捕获大量指标可能会增加比所需更多的开销。
或者, Jackson 的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 服务器上执行上述类,然后通过 Web 浏览器连接到它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 的网络响应中是多么容易。