Java小强个人技术博客站点    手机版
当前位置: 首页 >> 开源 >> OSHI查看系统信息

OSHI查看系统信息

10911 开源 | 2021-3-23

OSHI.是一个基于JNA的免费的本地操作系统和Java的硬件信息库。它不需要安装任何额外的本机库,旨在提供跨平台的实现来检索系统信息,如操作系统版本、进程、内存和CPU使用情况、磁盘和分区、设备、传感器等。(OSHI is a free JNA-based (native) Operating System and Hardware Information library for Java. It does not require the installation of any additional native libraries and aims to provide a cross-platform implementation to retrieve system information, such as OS version, processes, memory & CPU usage, disks & partitions, devices, sensors, etc.)


项目地址:https://github.com/oshi/oshi 


支持以下内容:

Computer System and firmware, baseboard

Operating System and Version/Build

Physical (core) and Logical (hyperthreaded) CPUs, processor groups, NUMA nodes

System and per-processor load, usage tick counters, interrupts, uptime

Process uptime, CPU, memory usage, user/group, command line args, thread details

Physical and virtual memory used/available

Mounted filesystems (type, usable and total space, options, reads and writes)

Disk drives (model, serial, size, reads and writes) and partitions

Network interfaces (IPs, bandwidth in/out), network parameters, TCP/UDP statistics

Battery state (% capacity, time remaining, power usage stats)

USB Devices

Connected displays (with EDID info), graphics and audio cards

Sensors (temperature, fan speeds, voltage) on some hardware


Maven引用:

<dependency>
	<groupId>com.github.oshi</groupId>
	<artifactId>oshi-core</artifactId>
	<version>3.9.1</version>
</dependency>


示例代码:

package com.mt.test.oshi;

import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;

import java.text.DecimalFormat;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

/**
 * @author colin.cheng
 * @version V1.0
 * @date Created In 16:04 2019/8/16
 */
public class OshiTest {

	public static void main(String[] args) {
		while (true) {
			try {
				OshiTest.printlnCpuInfo();
				OshiTest.MemInfo();
				OshiTest.getThread();
				OshiTest.setSysInfo();
				OshiTest.setJvmInfo();
				TimeUnit.SECONDS.sleep(5);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private static void printlnCpuInfo() throws InterruptedException {
		// System.out.println("----------------cpu信息----------------");
		SystemInfo systemInfo = new SystemInfo();
		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;
		System.out.println("----------------cpu信息----------------");
		System.out.println("cpu核数:" + processor.getLogicalProcessorCount());
		System.out.println("cpu系统使用率:" + new DecimalFormat("#.##%").format(cSys * 1.0 / totalCpu));
		System.out.println("cpu用户使用率:" + new DecimalFormat("#.##%").format(user * 1.0 / totalCpu));
		System.out.println("cpu当前等待率:" + new DecimalFormat("#.##%").format(iowait * 1.0 / totalCpu));
		System.out.println("cpu当前使用率:" + new DecimalFormat("#.##%").format(1.0 - (idle * 1.0 / totalCpu)));
	}

	public static void MemInfo() {
		System.out.println("----------------主机内存信息----------------");
		SystemInfo systemInfo = new SystemInfo();
		GlobalMemory memory = systemInfo.getHardware().getMemory();
		// 总内存
		long totalByte = memory.getTotal();
		// 剩余
		long acaliableByte = memory.getAvailable();
		System.out.println("总内存 = " + formatByte(totalByte));
		System.out.println("使用" + formatByte(totalByte - acaliableByte));
		System.out.println("剩余内存 = " + formatByte(acaliableByte));
		System.out.println("使用率:" + new DecimalFormat("#.##%").format((totalByte - acaliableByte) * 1.0 / totalByte));
	}

	public static void setSysInfo() {
		System.out.println("----------------操作系统信息----------------");
		Properties props = System.getProperties();
		// 系统名称
		String osName = props.getProperty("os.name");
		// 架构名称
		String osArch = props.getProperty("os.arch");
		System.out.println("操作系统名 = " + osName);
		System.out.println("系统架构 = " + osArch);
	}

	public static void setJvmInfo() {
		System.out.println("----------------jvm信息----------------");
		Properties props = System.getProperties();
		Runtime runtime = Runtime.getRuntime();
		// jvm总内存
		long jvmTotalMemoryByte = runtime.totalMemory();
		// jvm最大可申请
		long jvmMaxMoryByte = runtime.maxMemory();
		// 空闲空间
		long freeMemoryByte = runtime.freeMemory();
		// jdk版本
		String jdkVersion = props.getProperty("java.version");
		// jdk路径
		String jdkHome = props.getProperty("java.home");
		System.out.println("jvm内存总量 = " + formatByte(jvmTotalMemoryByte));
		System.out.println("jvm已使用内存 = " + formatByte(jvmTotalMemoryByte - freeMemoryByte));
		System.out.println("jvm剩余内存 = " + formatByte(freeMemoryByte));
		System.out.println("jvm内存使用率 = "
				+ new DecimalFormat("#.##%").format((jvmTotalMemoryByte - freeMemoryByte) * 1.0 / jvmTotalMemoryByte));
		System.out.println("java版本 = " + jdkVersion);
		// System.out.println("jdkHome = " + jdkHome);
	}

	public static void getThread() {
		System.out.println("----------------线程信息----------------");
		ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();

		while (currentGroup.getParent() != null) {
			// 返回此线程组的父线程组
			currentGroup = currentGroup.getParent();
		}
		// 此线程组中活动线程的估计数
		int noThreads = currentGroup.activeCount();

		Thread[] lstThreads = new Thread[noThreads];
		// 把对此线程组中的所有活动子组的引用复制到指定数组中。
		currentGroup.enumerate(lstThreads);
		for (Thread thread : lstThreads) {
			System.out.println("线程数量:" + noThreads + " 线程id:" + thread.getId() + " 线程名称:" + thread.getName() + " 线程状态:"
					+ thread.getState());
		}
	}

	public static String formatByte(long byteNumber) {
		// 换算单位
		double FORMAT = 1024.0;
		double kbNumber = byteNumber / FORMAT;
		if (kbNumber < FORMAT) {
			return new DecimalFormat("#.##KB").format(kbNumber);
		}
		double mbNumber = kbNumber / FORMAT;
		if (mbNumber < FORMAT) {
			return new DecimalFormat("#.##MB").format(mbNumber);
		}
		double gbNumber = mbNumber / FORMAT;
		if (gbNumber < FORMAT) {
			return new DecimalFormat("#.##GB").format(gbNumber);
		}
		double tbNumber = gbNumber / FORMAT;
		return new DecimalFormat("#.##TB").format(tbNumber);
	}
}


输出信息:

11:25:56.946 [main] DEBUG oshi.hardware.common.AbstractCentralProcessor - Oracle MXBean detected.
11:25:57.075 [main] DEBUG oshi.hardware.platform.windows.WindowsCentralProcessor - Initialized Processor
----------------cpu信息----------------
cpu核数:4
cpu系统使用率:3.45%
cpu用户使用率:3.85%
cpu当前等待率:0%
cpu当前使用率:7.3%
----------------主机内存信息----------------
总内存 = 7.92GB
使用5.51GB
剩余内存 = 2.41GB
使用率:69.54%
----------------线程信息----------------
线程数量:5 线程id:2 线程名称:Reference Handler 线程状态:WAITING
线程数量:5 线程id:3 线程名称:Finalizer 线程状态:WAITING
线程数量:5 线程id:4 线程名称:Signal Dispatcher 线程状态:RUNNABLE
线程数量:5 线程id:5 线程名称:Attach Listener 线程状态:RUNNABLE
线程数量:5 线程id:1 线程名称:main 线程状态:RUNNABLE
----------------操作系统信息----------------
操作系统名 = Windows 7
系统架构 = amd64
----------------jvm信息----------------
jvm内存总量 = 123MB
jvm已使用内存 = 20.37MB
jvm剩余内存 = 102.63MB
jvm内存使用率 = 16.56%
java版本 = 1.8.0_51


END

推荐您阅读更多有关于“ cpu OSHI 跨平台 硬件信息 磁盘 设备 ”的文章

上一篇:Java Native Interface(JNA)介绍 下一篇:Java工具类库Hutool

猜你喜欢

发表评论:

评论:

回复 Java小强 评论于 2021-03-23 11:31
官方示例代码:https://github.com/oshi/oshi/blob/master/oshi-core/src/test/java/oshi/SystemInfoTest.java