文章目錄
- 簡介
- 相關資料
- maven依賴
- oshi-官方示例
- 獲取CUP信息代碼
- 獲取內存信息
- 獲取磁盤信息
簡介
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();// 睡眠1sTimeUnit.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 + steallong 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);}