登录
    Technology changes quickly but people's minds change slowly.

java 监控系统,cpu 磁盘等

技术宅 破玉 1202次浏览 0个评论

     一般的都会考虑到sigar,但是sigar需要 dll 文件和 so文件等动态链接库,比较麻烦。今天看到一个不需要其他任何外部动态链接库的java 包,特此记录下
https://github.com/oshi/oshi
该项目需要引入
jna、jna-platform jar ,直接加在 pom文件中就可以。
API文档
一段 参考代码:

@Slf4j
@Service
public class SystemService {
    public  String getComputerIdentifier() {
        SystemInfo systemInfo = new SystemInfo();
        LinuxDisks disks=new LinuxDisks();
        OperatingSystem operatingSystem = systemInfo.getOperatingSystem();
        HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware();
        CentralProcessor centralProcessor = hardwareAbstractionLayer.getProcessor();
        ComputerSystem computerSystem = hardwareAbstractionLayer.getComputerSystem();

        String vendor = operatingSystem.getManufacturer();
        String processorSerialNumber = computerSystem.getSerialNumber();
        String processorIdentifier = centralProcessor.getProcessorIdentifier().getIdentifier();
        int processors = centralProcessor.getLogicalProcessorCount();

        String delimiter = "-";
       HWDiskStore [] ha= disks.getDisks();
        Arrays.stream(ha).forEach(a-> Arrays.stream(a.getPartitions()).forEach(hwPartition -> System.out.println( hwPartition.getName())));
        return String.format("%08x", vendor.hashCode()) + delimiter
                + String.format("%08x", processorSerialNumber.hashCode()) + delimiter
                + String.format("%08x", processorIdentifier.hashCode()) + delimiter + processors;
    }

    public void logSysInfo(){
        log.info(getComputerIdentifier());
    }
}

华裳绕指柔, 版权所有丨如未注明 , 均为原创|转载请注明java 监控系统,cpu 磁盘等
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址