Java 使用oshi获取当前服务器状态cpu、内存、存储等核心信息

简介

OSHI 是基于 JNA 的(本地)操作系统和硬件信息库。它不需要安装任何其他额外的本地库,旨在提供一种跨平台的实现来检索系统信息,例如操作系统版本、进程、内存和 CPU 使用率、磁盘和分区、设备、传感器等。

使用 OSHI 可以对应用程序进行监控,可以对应用程序所在的服务器资源进行监控,还可以监控到其他许多指标,如下:

1、计算机系统和固件,底板
2、操作系统和版本 / 内部版本
3、物理(核心)和逻辑(超线程)CPU,处理器组,NUMA 节点
4、系统和每个处理器的负载百分比和滴答计数器
5、CPU 正常运行时间,进程和线程
6、进程正常运行时间,CPU,内存使用率,用户 / 组,命令行
7、已使用 / 可用的物理和虚拟内存
8、挂载的文件系统(类型,可用空间和总空间)
9、磁盘驱动器(型号,序列号,大小)和分区
10、网络接口(IP,带宽输入 / 输出)
11、电池状态(电量百分比,剩余时间,电量使用情况统计信息)
12、连接的显示器(带有 EDID 信息)
13、USB 设备
14、传感器(温度,风扇速度,电压)

支持的平台:

Windows
Linux
macOS
UNIX (AIX, FreeBSD, OpenBSD, Solaris)

相关资料

github 地址:https://github.com/oshi/oshi
API 文档:http://oshi.github.io/oshi/apidocs/

maven依赖

       <dependency>
            <groupId>com.github.oshi</groupId>
            <artifactId>oshi-core</artifactId>
            <version>6.3.2</version>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>5.12.1</version>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna-platform</artifactId>
            <version>5.12.1</version>
        </dependency>

在这里插入图片描述

oshi-官方示例

此外,该oshi-demo模块包括一个OshiGui类,它实现了一个基本的 Swing GUI,为在 UI、监控或警报应用程序中使用 OSHI 的潜在可视化提供建议,如下所示。有关基于此方法的更高级 GUI,请参阅MooInfo 项目。

获取CUP信息代码

获取时与windows窗口等查看CUP利用率的信息有差异,本身CUP利用率存在很大的波动。

public static CpuEntity getCpu() throws InterruptedException {
        SystemInfo systemInfo = new SystemInfo();
        GlobalConfig.set(GlobalConfig.OSHI_OS_WINDOWS_CPU_UTILITY, Boolean.TRUE);
        CentralProcessor processor = systemInfo.getHardware().getProcessor();
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        // 睡眠1s
        TimeUnit.SECONDS.sleep(1);
        long[] ticks = processor.getSystemCpuLoadTicks();
        long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
        long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
        long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
        long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
        long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
        long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
        long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
        long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
        long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
        CpuEntity cpuEntity = new CpuEntity();
        cpuEntity.setSys(new DecimalFormat("#.##").format(cSys * 1.0 / totalCpu));
        cpuEntity.setUser(new DecimalFormat("#.##").format(user * 1.0 / totalCpu));
        cpuEntity.setWait(new DecimalFormat("#.##").format(iowait * 1.0 / totalCpu));
        cpuEntity.setWait(new DecimalFormat("#.##").format(idle * 1.0 / totalCpu));
        //  user + system + nice + iowait + irq + softirq + steal
        long cpuUtilization = user + nice + cSys + iowait + irq + softirq + steal;
        cpuEntity.setCombined(new DecimalFormat("#.##").format((cpuUtilization * 1.0 / totalCpu)*100));
        return cpuEntity;
    }

获取内存信息

    public static MemoryEntity getMemory() {
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        MemoryEntity memoryEntity = new MemoryEntity();
        memoryEntity.setMemTotal(osmxb.getTotalPhysicalMemorySize() / 1024 / 1024 / 1024);
        memoryEntity.setMemUsed((osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / 1024 / 1024 / 1024);
        return memoryEntity;
    }

获取磁盘信息

  			File[] roots = File.listRoots();
            Long useSum = 0l;
            Long totalSum = 0l;
            for (File file : roots) {
                long free = file.getFreeSpace();
                long total = file.getTotalSpace();
                long use = total - free;
                useSum += change(use);
                totalSum += change(total);
            }
  • 20
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一码归一码@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值