查看linux远程服务器性能,java - 如何使用oshi远程监视Linux服务器的性能状态? - 堆栈内存溢出...

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

OSHI项目的第249期概述了一些选项,包括Dropwizard Metrics库,您可以使用它们使用数据启用JMX端口。 但是,以这种方式捕获大量指标可能会增加不必要的开销。

另外, 杰克逊的 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"

},

"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,

"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的网络响应中是多么容易。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 OSHI(Open System Hardware Information)这个 Java 库来获取系统硬件信息,包括 CPU、内存、磁盘等状态信息。下面是一个简单的示例代码,可以根据 IP 地址获取 Linux 服务器的 CPU、内存、磁盘等详细信息: ```java import oshi.SystemInfo; import oshi.hardware.CentralProcessor; import oshi.hardware.GlobalMemory; import oshi.hardware.HardwareAbstractionLayer; import oshi.software.os.FileSystem; import oshi.software.os.OSFileStore; import oshi.software.os.OperatingSystem; import oshi.util.FormatUtil; import java.net.InetAddress; import java.net.UnknownHostException; public class SystemInfoDemo { public static void main(String[] args) throws UnknownHostException { String ipAddress = "192.168.0.1"; // Linux 服务器的 IP 地址 // 根据 IP 地址获取 InetAddress 对象 InetAddress inetAddress = InetAddress.getByName(ipAddress); // 创建 SystemInfo 对象 SystemInfo systemInfo = new SystemInfo(inetAddress); // 获取 HardwareAbstractionLayer 对象 HardwareAbstractionLayer hardware = systemInfo.getHardware(); // 获取 CentralProcessor 对象 CentralProcessor processor = hardware.getProcessor(); // 获取 GlobalMemory 对象 GlobalMemory memory = hardware.getMemory(); // 获取 OperatingSystem 对象 OperatingSystem os = systemInfo.getOperatingSystem(); // 获取 FileSystem 对象 FileSystem fileSystem = os.getFileSystem(); // 输出 CPU 信息 System.out.println("CPU: " + processor.getPhysicalProcessorCount() + " cores"); System.out.println("CPU load: " + processor.getSystemCpuLoad() * 100 + "%"); // 输出内存信息 System.out.println("Memory: " + FormatUtil.formatBytes(memory.getTotal()) + " total"); // 输出磁盘信息 OSFileStore[] fileStores = fileSystem.getFileStores(); for (OSFileStore fileStore : fileStores) { System.out.println("Drive: " + fileStore.getName()); System.out.println("Total space: " + FormatUtil.formatBytes(fileStore.getTotalSpace())); System.out.println("Usable space: " + FormatUtil.formatBytes(fileStore.getUsableSpace())); } } } ``` 需要注意的是,使用 OSHI 库获取系统硬件信息需要在 Linux 服务器上安装并运行 OSHI 的 native 库,具体安装方法可以参考 OSHI 官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值