簡(jiǎn)介
OSHI 是基于 JNA 的(本地)操作系統(tǒng)和硬件信息庫。它不需要安裝任何其他額外的本地庫,旨在提供一種跨平臺(tái)的實(shí)現(xiàn)來檢索系統(tǒng)信息,例如操作系統(tǒng)版本、進(jìn)程、內(nèi)存和 CPU 使用率、磁盤和分區(qū)、設(shè)備、傳感器等。
使用 OSHI 可以對(duì)應(yīng)用程序進(jìn)行監(jiān)控,可以對(duì)應(yīng)用程序所在的服務(wù)器資源進(jìn)行監(jiān)控,還可以監(jiān)控到其他許多指標(biāo),如下:
1、計(jì)算機(jī)系統(tǒng)和固件,底板
2、操作系統(tǒng)和版本 / 內(nèi)部版本
3、物理(核心)和邏輯(超線程)CPU,處理器組,NUMA 節(jié)點(diǎn)
4、系統(tǒng)和每個(gè)處理器的負(fù)載百分比和滴答計(jì)數(shù)器
5、CPU 正常運(yùn)行時(shí)間,進(jìn)程和線程
6、進(jìn)程正常運(yùn)行時(shí)間,CPU,內(nèi)存使用率,用戶 / 組,命令行
7、已使用 / 可用的物理和虛擬內(nèi)存
8、掛載的文件系統(tǒng)(類型,可用空間和總空間)
9、磁盤驅(qū)動(dòng)器(型號(hào),序列號(hào),大?。┖头謪^(qū)
10、網(wǎng)絡(luò)接口(IP,帶寬輸入 / 輸出)
11、電池狀態(tài)(電量百分比,剩余時(shí)間,電量使用情況統(tǒng)計(jì)信息)
12、連接的顯示器(帶有 EDID 信息)
13、USB 設(shè)備
14、傳感器(溫度,風(fēng)扇速度,電壓)
支持的平臺(tái):
Windows
Linux
macOS
UNIX (AIX, FreeBSD, OpenBSD, Solaris)
相關(guān)資料
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模塊包括一個(gè)OshiGui類,它實(shí)現(xiàn)了一個(gè)基本的 Swing GUI,為在 UI、監(jiān)控或警報(bào)應(yīng)用程序中使用 OSHI 的潛在可視化提供建議,如下所示。有關(guān)基于此方法的更高級(jí) GUI,請(qǐng)參閱MooInfo 項(xiàng)目。文章來源:http://www.zghlxwxcb.cn/news/detail-775330.html
獲取CUP信息代碼
獲取時(shí)與windows窗口等查看CUP利用率的信息有差異,本身CUP利用率存在很大的波動(dòng)。文章來源地址http://www.zghlxwxcb.cn/news/detail-775330.html
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;
}
獲取內(nèi)存信息
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);
}
到了這里,關(guān)于Java 使用oshi獲取當(dāng)前服務(wù)器狀態(tài)cpu、內(nèi)存、存儲(chǔ)等核心信息的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!