sigar获取系统信息
时间: 2023-05-19 20:07:02 浏览: 229
sigar 获取系统信息
Sigar 是一个跨平台的系统信息收集库,可以获取系统的 CPU、内存、磁盘、网络等信息。以下是获取系统信息的示例代码:
```java
import org.hyperic.sigar.*;
public class SigarDemo {
public static void main(String[] args) throws SigarException {
Sigar sigar = new Sigar();
System.out.println("CPU信息:");
CpuInfo[] cpuInfos = sigar.getCpuInfoList();
for (CpuInfo cpuInfo : cpuInfos) {
System.out.println("CPU型号:" + cpuInfo.getModel());
System.out.println("CPU频率:" + cpuInfo.getMhz() + "MHz");
System.out.println("CPU核数:" + cpuInfo.getTotalCores());
}
System.out.println("内存信息:");
Mem mem = sigar.getMem();
System.out.println("总内存:" + mem.getTotal() / 1024 / 1024 + "MB");
System.out.println("已用内存:" + mem.getUsed() / 1024 / 1024 + "MB");
System.out.println("剩余内存:" + mem.getFree() / 1024 / 1024 + "MB");
System.out.println("磁盘信息:");
FileSystem[] fileSystems = sigar.getFileSystemList();
for (FileSystem fileSystem : fileSystems) {
System.out.println("盘符:" + fileSystem.getDirName());
System.out.println("盘符类型:" + fileSystem.getTypeName());
FileSystemUsage usage = sigar.getFileSystemUsage(fileSystem.getDirName());
System.out.println("总大小:" + usage.getTotal() / 1024 / 1024 + "MB");
System.out.println("已用大小:" + usage.getUsed() / 1024 / 1024 + "MB");
System.out.println("剩余大小:" + usage.getFree() / 1024 / 1024 + "MB");
}
System.out.println("网络信息:");
NetInterfaceConfig[] netConfigs = sigar.getNetInterfaceConfigList();
for (NetInterfaceConfig netConfig : netConfigs) {
System.out.println("网络设备名:" + netConfig.getName());
System.out.println("IP地址:" + netConfig.getAddress());
System.out.println("子网掩码:" + netConfig.getNetmask());
}
}
}
```
注意:需要引入 sigar.jar 和 sigar-amd64-winnt.dll(或其它平台对应的库文件)。
阅读全文