邮箱官方网站注册做seo的网站
news/
2025/9/26 23:33:06/
文章来源:
邮箱官方网站注册,做seo的网站,阿土伯网站做产品推广咋样,互联网公司排名2023文章目录 引言一、快速入门1.1 OSHI的简介1.2 引入依赖1.3 涉及的包#xff08;package#xff09;1.4 涉及的核心类 二、操作系统信息#xff1a;OperatingSystem2.1 总揽2.2 文件系统信息#xff1a;FileSystem2.3 网络参数信息#xff1a;NetworkParams2.4 进程信息package1.4 涉及的核心类 二、操作系统信息OperatingSystem2.1 总揽2.2 文件系统信息FileSystem2.3 网络参数信息NetworkParams2.4 进程信息OSProcess2.5 线程信息OSThread 三、硬件信息Hardware3.1 硬件信息HardwareAbstractionLayer3.1.1 总揽3.1.2 磁盘信息HWDiskStore3.1.3 内存信息Memory3.1.4 显卡信息graphicsCard3.1.5 网络接口信息NetworkIF3.1.6 电源信息PowerSource3.1.7 传感器信息Sensors3.1.8 usb设备信息UsbDevice 3.2 CPU信息CentralProcessor3.3 计算机系统ComputerSystem 四、使用场景4.1 获取 CPU 信息4.2 获取内存信息4.3 获取服务器信息4.4 获取Java虚拟机信息4.5 获取系统文件相关信息4.6 获取磁盘信息 小结 引言 最近我们负责的某行业的业务平台出现了一个非常奇葩的问题。这个项目涉及到软件平台厂家、服务器厂家、存储厂家以及真正的用户等四方关系而我们主要负责软件平台的研发工作。平台正常运行了2年多了去年年底存储盘阵中出现历史巡检数据丢失或者无法使用的诡异问题软件、服务以及存储各方对根据运行日志进行了审计各自都说没有问题接着请了比较权威的第三方测评单位召开了5次讨论会也没有分析出真正的原因 因为每家只能证明自身没有问题并不能证明问题真正的原因是什么环节所以只能几家摊钱恢复数据。 俗话说“吃一堑长一智”。业务平台作为应用层是最接近用户的。平台一出问题给用户的第一印象就是这个平台太烂了又出问题了。为了以后出现问题软件平台可以对问题进行实时追踪和根据历史数据运行记录进行审计便于更快速的缩小问题范围更精确的定位问题。
一、快速入门
1.1 OSHI的简介 谈到监控最快捷的还是使用Java本身进行对JVM和操作系统的监控方便快捷又可靠。OSHIOperating System and Hardware Information是Java的免费的基于JNA的本机操作系统和硬件信息库。它提供了一组简单易用的API可以用于检索和监控诸如操作系统类型、处理器信息、内存使用情况、硬盘信息、网络接口等系统和硬件相关的数据。oshi支持的主要功能包括
获取操作系统信息可以获取操作系统的名称、版本、构建信息、位数等。还可以获取操作系统的启动时间、系统负载、当前进程数等信息。获取硬件信息可以获取处理器CPU的信息包括型号、频率、核心数、温度等。还可以获取内存RAM的信息包括总容量、可用容量、使用率等。此外还可以获取硬盘磁盘的信息包括型号、容量、使用率等。还可以获取电池、网卡、USB设备等硬件的信息。获取网络信息可以获取网络接口网卡的信息包括名称、IP地址、MAC地址、接收和发送的数据量等。监控系统和硬件可以通过OSHI提供的API实时监控和收集系统和硬件的数据例如CPU使用率、内存使用情况、网络流量等。 总的来说OSHI是一个强大而易于使用的Java库可用于获取和监控操作系统和硬件信息帮助你编写更具交互性和功能性的系统监控、硬件信息获取和性能调优的应用程序。 oshi官方参考文档https://www.oshi.ooo/oshi-core/apidocs/ 1.2 引入依赖 OSHI 不需要安装任何额外的本地库因此接入起来很方便。只需将OSHI库添加到项目的依赖项中并使用OSHI提供的API来获取所需的系统和硬件信息可以根据需要选择性地获取和处理各种信息。通过配置 Maven 依赖来引入 OSHI如下所示
!-- https://mvnrepository.com/artifact/com.github.oshi/oshi-core --
dependencygroupIdcom.github.oshi/groupIdartifactIdoshi-core/artifactIdversion6.4.1/version
/dependency这里注意的是不同版本的 JDK 环境需要引用不同版本的包。由于笔者使用的JDK8这里引入的是 oshi-core-6.4.1。 1.3 涉及的包package
包路径说明oshi.driver.linux提供查询Linux信息的函数oshi.driver.linux.proc提供用于查询Linux / proc伪文件系统中的统计信息的函数oshi.driver.mac提供查询Mac Info的功能oshi.driver.mac.disk提供查询Mac Disk Info的功能oshi.driver.mac.net提供查询MAC网络信息的功能oshi.driver.unix提供用于查询所有UNIX系统通用的信息的功能oshi.driver.unix.aix提供有关AIX的信息的函数oshi.driver.unix.aix.perfstat使用libperfstat API提供有关AIX的信息查询信息的功能oshi.driver.unix.freebsd提供查询FreeBSD信息的函数oshi.driver.unix.freebsd.disk提供查询FreeBSD磁盘信息的函数oshi.driver.unix.openbsd.disk提供查询OpenBSD信息的函数oshi.driver.unix.solaris提供查询Solaris信息的功能oshi.driver.unix.solaris.disk提供查询Solaris磁盘信息的功能oshi.driver.unix.solaris.kstat提供查询Solaris KSTAT信息的函数oshi.driver.windows提供用于在Windows中查询信息的函数oshi.driver.windows.perfmon提供用于在Windows性能监视器或WMI计数器表中中查询计数器的功能oshi.driver.windows.registry提供用于在Windows注册表中查询数据的函数oshi.driver.windows.wmi提供函数以在WMI类中查询属性oshi.hardware提供跨平台实现以检索CPU存储器显示磁盘网络接口电源传感器和USB设备等硬件信息oshi.hardware.common为公共代码提供抽象类oshi.hardware.platform.linux提供有关Linux系统上的内存电源和处理器等硬件的信息oshi.hardware.platform.mac提供有关Mac系统上的内存电源和处理器等硬件的信息oshi.hardware.platform.unix为基于UNIX的操作系统提供映射oshi.hardware.platform.unix.aix提供关于AIX系统上的内存电源和处理器等硬件的信息oshi.hardware.platform.unix.free提供有关内存电源和FreeBSD系统上的处理器等硬件的信息oshi.hardware.platform.unix.open提供有关OpenBSD系统上的内存电源和处理器等硬件的信息oshi.hardware.platform.unix.sola提供有关Solaris系统上的内存电源和处理器等硬件的信息oshi.hardware.platform.windows提供有关Windows系统上的内存电源和处理器等硬件的信息oshi.jna.platform.linux为Linux提供JNA库的扩展oshi.jna.platform.mac为MacOS提供JNA库的扩展oshi.jna.platform.unix为UNIX提供JNA库的扩展oshi.jna.platform.windows为Windows提供JNA库的扩展oshi.software.common为公共代码提供抽象类oshi.software.os提供跨平台实现来检索OS、文件系统和处理信息oshi.software.os.linux提供有关Linux上的软件和操作系统的信息oshi.software.os.mac提供有关MacOS上的软件和操作系统的信息oshi.software.os.unix.aix提供有关AIX上的软件和操作系统的信息oshi.software.os.unix.freebsd提供有关FreeBSD上的软件和操作系统的信息oshi.software.os.unix.openbsd提供有关OpenBSD上的软件和操作系统的信息oshi.software.os.unix.solaris提供有关Solaris上的软件和操作系统的信息oshi.software.os.windows提供有关Windows上的软件和操作系统的信息oshi.util提供解析、格式化和其他访问的实用程序oshi.util.platform.linux为Linux提供实用程序oshi.util.platform.mac提供Mac的实用程序。oshi.util.platform.unix.freebsd为FreeBSD提供实用程序oshi.util.platform.unix.openbsd为OpenBSD提供实用程序oshi.util.platform.unix.solaris为Solaris提供实用程序oshi.util.platform.windows为Windows提供实用程序oshi.util.tuples提供封装多个对象的类旨在从方法中作为返回类型
1.4 涉及的核心类 #mermaid-svg-vb8LYOJCCv2LD0r8 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-vb8LYOJCCv2LD0r8 .error-icon{fill:#552222;}#mermaid-svg-vb8LYOJCCv2LD0r8 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-vb8LYOJCCv2LD0r8 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-vb8LYOJCCv2LD0r8 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-vb8LYOJCCv2LD0r8 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-vb8LYOJCCv2LD0r8 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-vb8LYOJCCv2LD0r8 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-vb8LYOJCCv2LD0r8 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-vb8LYOJCCv2LD0r8 .marker.cross{stroke:#333333;}#mermaid-svg-vb8LYOJCCv2LD0r8 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-vb8LYOJCCv2LD0r8 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-vb8LYOJCCv2LD0r8 .cluster-label text{fill:#333;}#mermaid-svg-vb8LYOJCCv2LD0r8 .cluster-label span{color:#333;}#mermaid-svg-vb8LYOJCCv2LD0r8 .label text,#mermaid-svg-vb8LYOJCCv2LD0r8 span{fill:#333;color:#333;}#mermaid-svg-vb8LYOJCCv2LD0r8 .node rect,#mermaid-svg-vb8LYOJCCv2LD0r8 .node circle,#mermaid-svg-vb8LYOJCCv2LD0r8 .node ellipse,#mermaid-svg-vb8LYOJCCv2LD0r8 .node polygon,#mermaid-svg-vb8LYOJCCv2LD0r8 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-vb8LYOJCCv2LD0r8 .node .label{text-align:center;}#mermaid-svg-vb8LYOJCCv2LD0r8 .node.clickable{cursor:pointer;}#mermaid-svg-vb8LYOJCCv2LD0r8 .arrowheadPath{fill:#333333;}#mermaid-svg-vb8LYOJCCv2LD0r8 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-vb8LYOJCCv2LD0r8 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-vb8LYOJCCv2LD0r8 .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-vb8LYOJCCv2LD0r8 .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-vb8LYOJCCv2LD0r8 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-vb8LYOJCCv2LD0r8 .cluster text{fill:#333;}#mermaid-svg-vb8LYOJCCv2LD0r8 .cluster span{color:#333;}#mermaid-svg-vb8LYOJCCv2LD0r8 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-vb8LYOJCCv2LD0r8 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} SystemInfo 操作系统信息 OperatingSystem 硬件信息 Hardware 平台信息 Platform 方法说明getOperatingSystem()获取操作系统信息getHardware()获取硬件信息getCurrentPlatform()
SystemInfo systemInfo new SystemInfo();二、操作系统信息OperatingSystem
2.1 总揽 #mermaid-svg-eaSDzdQVztQcUEFF {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-eaSDzdQVztQcUEFF .error-icon{fill:#552222;}#mermaid-svg-eaSDzdQVztQcUEFF .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-eaSDzdQVztQcUEFF .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-eaSDzdQVztQcUEFF .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-eaSDzdQVztQcUEFF .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-eaSDzdQVztQcUEFF .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-eaSDzdQVztQcUEFF .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-eaSDzdQVztQcUEFF .marker{fill:#333333;stroke:#333333;}#mermaid-svg-eaSDzdQVztQcUEFF .marker.cross{stroke:#333333;}#mermaid-svg-eaSDzdQVztQcUEFF svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-eaSDzdQVztQcUEFF .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-eaSDzdQVztQcUEFF .cluster-label text{fill:#333;}#mermaid-svg-eaSDzdQVztQcUEFF .cluster-label span{color:#333;}#mermaid-svg-eaSDzdQVztQcUEFF .label text,#mermaid-svg-eaSDzdQVztQcUEFF span{fill:#333;color:#333;}#mermaid-svg-eaSDzdQVztQcUEFF .node rect,#mermaid-svg-eaSDzdQVztQcUEFF .node circle,#mermaid-svg-eaSDzdQVztQcUEFF .node ellipse,#mermaid-svg-eaSDzdQVztQcUEFF .node polygon,#mermaid-svg-eaSDzdQVztQcUEFF .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-eaSDzdQVztQcUEFF .node .label{text-align:center;}#mermaid-svg-eaSDzdQVztQcUEFF .node.clickable{cursor:pointer;}#mermaid-svg-eaSDzdQVztQcUEFF .arrowheadPath{fill:#333333;}#mermaid-svg-eaSDzdQVztQcUEFF .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-eaSDzdQVztQcUEFF .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-eaSDzdQVztQcUEFF .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-eaSDzdQVztQcUEFF .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-eaSDzdQVztQcUEFF .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-eaSDzdQVztQcUEFF .cluster text{fill:#333;}#mermaid-svg-eaSDzdQVztQcUEFF .cluster span{color:#333;}#mermaid-svg-eaSDzdQVztQcUEFF div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-eaSDzdQVztQcUEFF :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} OperatingSystem OSProcess OSThread OSDesktopWindow FileSystem InternetProtocolStats NetworkParams OSService OSSession OSVersionInfo 操作系统OS是计算机上的软件该软件管理不同程序使用其硬件的方式并调节用户控制计算机的方式。
方法说明getBitness()操作系统位数**getChildProcesses(int var1, int var2, OperatingSystem.ProcessSort var3) **获取子进程getCurrentProcess()getCurrentThread()getDesktopWindows(boolean visibleOnly)有关操作系统GUI桌面上窗口的信息getFamily()获取操作系统名称例如 WindowsgetManufacturer()获取供货商例如 MicrosoftgetFileSystem()获取系统硬盘信息getInternetProtocolStats()getNetworkParams()获取网络相关参数getProcess(int pId)通过进程id 获取一个进程信息getProcessCount()获取进程数量**getProcesses(Collection pId) **获取多个进程信息**getProcesses(int pId, OperatingSystem.ProcessSort var2) **通过进程id获取进程信息并设置排序方式getProcessId()获取进程idgetServices()系统服务信息getSystemBootTime()系统启动的大致时间getSystemUptime()操作系统结束时间getThreadCount()获取线程数getThreadId()getVersionInfo()获取操作系统版本详细信息信息getSessions()有关当前登录到操作系统的用户的信息
OperatingSystem operatingSystem new SystemInfo().getOperatingSystem();2.2 文件系统信息FileSystem
OperatingSystem operatingSystem new SystemInfo().getOperatingSystem();
FileSystem fileSystem operatingSystem.getFileSystem();#mermaid-svg-88qN0BGM1A0E4oGs {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-88qN0BGM1A0E4oGs .error-icon{fill:#552222;}#mermaid-svg-88qN0BGM1A0E4oGs .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-88qN0BGM1A0E4oGs .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-88qN0BGM1A0E4oGs .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-88qN0BGM1A0E4oGs .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-88qN0BGM1A0E4oGs .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-88qN0BGM1A0E4oGs .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-88qN0BGM1A0E4oGs .marker{fill:#333333;stroke:#333333;}#mermaid-svg-88qN0BGM1A0E4oGs .marker.cross{stroke:#333333;}#mermaid-svg-88qN0BGM1A0E4oGs svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-88qN0BGM1A0E4oGs .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-88qN0BGM1A0E4oGs .cluster-label text{fill:#333;}#mermaid-svg-88qN0BGM1A0E4oGs .cluster-label span{color:#333;}#mermaid-svg-88qN0BGM1A0E4oGs .label text,#mermaid-svg-88qN0BGM1A0E4oGs span{fill:#333;color:#333;}#mermaid-svg-88qN0BGM1A0E4oGs .node rect,#mermaid-svg-88qN0BGM1A0E4oGs .node circle,#mermaid-svg-88qN0BGM1A0E4oGs .node ellipse,#mermaid-svg-88qN0BGM1A0E4oGs .node polygon,#mermaid-svg-88qN0BGM1A0E4oGs .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-88qN0BGM1A0E4oGs .node .label{text-align:center;}#mermaid-svg-88qN0BGM1A0E4oGs .node.clickable{cursor:pointer;}#mermaid-svg-88qN0BGM1A0E4oGs .arrowheadPath{fill:#333333;}#mermaid-svg-88qN0BGM1A0E4oGs .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-88qN0BGM1A0E4oGs .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-88qN0BGM1A0E4oGs .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-88qN0BGM1A0E4oGs .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-88qN0BGM1A0E4oGs .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-88qN0BGM1A0E4oGs .cluster text{fill:#333;}#mermaid-svg-88qN0BGM1A0E4oGs .cluster span{color:#333;}#mermaid-svg-88qN0BGM1A0E4oGs div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-88qN0BGM1A0E4oGs :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} OperatingSystem FileSystem OSFileStore 文件系统是一种逻辑安排通常在分层树中其中文件被放置用于存储和检索。 类名方法说明FileSystemgetMaxFileDescriptors()最大文件描述符getOpenFileDescriptors()打开文件描述符getFileStores()获取盘符相关信息OSFileStoregetDescription说明getFreeInodesgetFreeSpacegetLabel标签getLogicalVolume文件系统的逻辑卷名getMount文件系统的挂载点getName磁盘名称getOptions文件系统的选项getTotalInodesgetTotalSpace总大小getType盘符类型getUsableSpace剩余大小getUUIDgetVolume文件系统的卷名updateAttributes
2.3 网络参数信息NetworkParams
方法说明getDnsServers()获取域名地址getHostName()获取主机名getDomainName()获取域名getIpv4DefaultGateway()获取默认Ipv4getIpv6DefaultGateway()获取默认Ipv6
OperatingSystem operatingSystem new SystemInfo().getOperatingSystem();
NetworkParams networkParams operatingSystem.getNetworkParams();2.4 进程信息OSProcess
方法说明getName()获取进程名称getPath()获取进程所在位置getCommandLine()获取命令行getCurrentWorkingDirectory()获取当前工作目录getUser()获取用户信息getUserID()获取用户idgetGroup()获取组信息getGroupID()获取组idgetState()状态getProcessID()获取进程idgetParentProcessID()获取父进程idgetThreadCount()获取线程数getPriority()优先级getVirtualSize()虚拟大小getResidentSetSize()实际使用物理内存getKernelTime()内核时间getUserTime()用户时间getUpTime()正常运行时间getStartTime()开始时间getBytesRead()读取字节getBytesWritten()写入字节getOpenFiles()打开文件数量
/*** 获取进程信息*/
public static MapString, Object getProcessesInfo() {OperatingSystem operatingSystem new SystemInfo().getOperatingSystem();HardwareAbstractionLayer hardware new SystemInfo().getHardware();GlobalMemory globalMemory hardware.getMemory();MapString, Object processesInfoMap new ConcurrentHashMap();processesInfoMap.put(processCount, operatingSystem.getProcessCount());processesInfoMap.put(threadCount, operatingSystem.getThreadCount());ListOSProcess osProcessList operatingSystem.getProcesses(OperatingSystem.ProcessFiltering.ALL_PROCESSES, OperatingSystem.ProcessSorting.CPU_DESC, 100);ListMapString, Object osProcessMapList new ArrayList(osProcessList.size());for (int i 0; i osProcessList.size(); i) {OSProcess osProcess osProcessList.get(i);MapString, Object osProcessMap new ConcurrentHashMap();osProcessMap.put(index, i);// 进程idosProcessMap.put(id, osProcess.getProcessID());// 父进程idosProcessMap.put(pid, osProcess.getParentProcessID());// 状态osProcessMap.put(state, osProcess.getState().name());// 命令行osProcessMap.put(commandLine, osProcess.getCommandLine());// 内核时间osProcessMap.put(kernelTime, osProcess.getKernelTime());// 用户时间osProcessMap.put(userTime, osProcess.getUserTime());// 正常运行时间osProcessMap.put(upTime, osProcess.getUpTime());// 开始时间osProcessMap.put(startTime, osProcess.getStartTime());// 读取字节osProcessMap.put(bytesRead, osProcess.getBytesRead());// 写入字节osProcessMap.put(bytesWritten, osProcess.getBytesWritten());// 打开文件数量osProcessMap.put(openFiles, osProcess.getOpenFiles());// 优先级osProcessMap.put(priority, osProcess.getPriority());// 线程数osProcessMap.put(threadCount, osProcess.getThreadCount());// 组信息osProcessMap.put(group, osProcess.getGroup());// 组idosProcessMap.put(groupId, osProcess.getGroupID());// 用户信息osProcessMap.put(user, osProcess.getUser());// 用户idosProcessMap.put(userId, osProcess.getUserID());// 获取当前工作目录osProcessMap.put(currentWorkingDirectory, osProcess.getCurrentWorkingDirectory());// 进程所在位置osProcessMap.put(path, osProcess.getPath());// 虚拟大小osProcessMap.put(virtualMemSize, FormatUtil.formatBytes(osProcess.getVirtualSize()));// 实际使用物理内存osProcessMap.put(residentSetSize, FormatUtil.formatBytes(osProcess.getResidentSetSize()));// 进程名称osProcessMap.put(processName, osProcess.getName());// 进程的命令行参数列表osProcessMap.put(arguments, osProcess.getArguments());// 环境变量osProcessMap.put(environmentVariables, osProcess.getEnvironmentVariables());// cpu 使用率osProcessMap.put(cpuUsageRate, 100d * (osProcess.getKernelTime() osProcess.getUserTime()) / osProcess.getUpTime());// 内存 使用率osProcessMap.put(memUsageRate, 100d * osProcess.getResidentSetSize() / globalMemory.getTotal());osProcessMap.put(softOpenFileLimit, osProcess.getSoftOpenFileLimit());osProcessMap.put(hardOpenFileLimit, osProcess.getHardOpenFileLimit());osProcessMap.put(processCpuLoadCumulative, osProcess.getProcessCpuLoadCumulative());osProcessMap.put(processCpuLoadBetweenTicks, osProcess.getProcessCpuLoadBetweenTicks(osProcess));osProcessMap.put(bitness, osProcess.getBitness());osProcessMap.put(affinityMask, osProcess.getAffinityMask());osProcessMap.put(minorFaults, osProcess.getMinorFaults());osProcessMap.put(majorFaults, osProcess.getMajorFaults());osProcessMapList.add(osProcessMap);}processesInfoMap.put(osProcessMapList, osProcessMapList);return processesInfoMap;
}这里提到一个类 OSProcess.State其是一个内部枚举类用于表示操作系统进程的状态定义了以下几种状态
状态值说明RUNNING进程正在运行中BLOCKED进程正在等待某个资源无法继续执行WAITING进程正在等待某个事件发生例如I/O操作完成ZOMBIE进程已经结束但其父进程尚未获取其退出状态信息TERMINATED进程已经正常结束或异常终止 除了以上状态外OSProcess.State 类还定义了一些其他状态。不过根据不同的操作系统和Java版本OSProcess.State类中可能还有一些其他状态。 2.5 线程信息OSThread
在oshi库中OSThread类表示操作系统线程。它提供了获取与操作系统线程相关的信息的方法。
方法说明getContextSwitchesgetKernelTimegetMajorFaultsgetMinorFaultsgetName当前线程的名称getOwningProcessIdgetPriority当前线程的优先级getStartMemoryAddressgetStartTimegetState获取当前线程的状态例如新建、可运行、阻塞等getThreadCpuLoadBetweenTicksgetThreadCpuLoadCumulativegetThreadId当前线程的IDgetUpTimegetUserTimeupdateAttributes
三、硬件信息Hardware
3.1 硬件信息HardwareAbstractionLayer
3.1.1 总揽 #mermaid-svg-64jP48bil7WVgrqy {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-64jP48bil7WVgrqy .error-icon{fill:#552222;}#mermaid-svg-64jP48bil7WVgrqy .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-64jP48bil7WVgrqy .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-64jP48bil7WVgrqy .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-64jP48bil7WVgrqy .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-64jP48bil7WVgrqy .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-64jP48bil7WVgrqy .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-64jP48bil7WVgrqy .marker{fill:#333333;stroke:#333333;}#mermaid-svg-64jP48bil7WVgrqy .marker.cross{stroke:#333333;}#mermaid-svg-64jP48bil7WVgrqy svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-64jP48bil7WVgrqy .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-64jP48bil7WVgrqy .cluster-label text{fill:#333;}#mermaid-svg-64jP48bil7WVgrqy .cluster-label span{color:#333;}#mermaid-svg-64jP48bil7WVgrqy .label text,#mermaid-svg-64jP48bil7WVgrqy span{fill:#333;color:#333;}#mermaid-svg-64jP48bil7WVgrqy .node rect,#mermaid-svg-64jP48bil7WVgrqy .node circle,#mermaid-svg-64jP48bil7WVgrqy .node ellipse,#mermaid-svg-64jP48bil7WVgrqy .node polygon,#mermaid-svg-64jP48bil7WVgrqy .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-64jP48bil7WVgrqy .node .label{text-align:center;}#mermaid-svg-64jP48bil7WVgrqy .node.clickable{cursor:pointer;}#mermaid-svg-64jP48bil7WVgrqy .arrowheadPath{fill:#333333;}#mermaid-svg-64jP48bil7WVgrqy .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-64jP48bil7WVgrqy .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-64jP48bil7WVgrqy .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-64jP48bil7WVgrqy .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-64jP48bil7WVgrqy .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-64jP48bil7WVgrqy .cluster text{fill:#333;}#mermaid-svg-64jP48bil7WVgrqy .cluster span{color:#333;}#mermaid-svg-64jP48bil7WVgrqy div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-64jP48bil7WVgrqy :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} HardwareAbstractionLayer 显卡 GraphicsCard 内存 Memory 传感器 Sensors 磁盘存储 HWDiskStore 电源 PowerSource 网卡 NetworkIF 声卡 SoundCard 显示器 Display // 获取硬件信息
HardwareAbstractionLayer hardware new SystemInfo().getHardware();方法说明getComputerSystem()获取计算机系统信息getProcessor()获取处理器信息getMemory()获取内存信息getPowerSources()获取电源信息getDiskStores()获取硬件磁盘信息getNetworkIFs()获取网络信息getDisplays()获取显示信息getSensors()获取传感器信息getUsbDevices(boolean var1)获取USB设备信息
3.1.2 磁盘信息HWDiskStore
// 获取硬件信息
HardwareAbstractionLayer hardware new SystemInfo().getHardware();
ListHWDiskStore diskStores hardware.getDiskStores();#mermaid-svg-FY5KuDGNh1pkezbl {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-FY5KuDGNh1pkezbl .error-icon{fill:#552222;}#mermaid-svg-FY5KuDGNh1pkezbl .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-FY5KuDGNh1pkezbl .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-FY5KuDGNh1pkezbl .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-FY5KuDGNh1pkezbl .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-FY5KuDGNh1pkezbl .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-FY5KuDGNh1pkezbl .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-FY5KuDGNh1pkezbl .marker{fill:#333333;stroke:#333333;}#mermaid-svg-FY5KuDGNh1pkezbl .marker.cross{stroke:#333333;}#mermaid-svg-FY5KuDGNh1pkezbl svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-FY5KuDGNh1pkezbl .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-FY5KuDGNh1pkezbl .cluster-label text{fill:#333;}#mermaid-svg-FY5KuDGNh1pkezbl .cluster-label span{color:#333;}#mermaid-svg-FY5KuDGNh1pkezbl .label text,#mermaid-svg-FY5KuDGNh1pkezbl span{fill:#333;color:#333;}#mermaid-svg-FY5KuDGNh1pkezbl .node rect,#mermaid-svg-FY5KuDGNh1pkezbl .node circle,#mermaid-svg-FY5KuDGNh1pkezbl .node ellipse,#mermaid-svg-FY5KuDGNh1pkezbl .node polygon,#mermaid-svg-FY5KuDGNh1pkezbl .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-FY5KuDGNh1pkezbl .node .label{text-align:center;}#mermaid-svg-FY5KuDGNh1pkezbl .node.clickable{cursor:pointer;}#mermaid-svg-FY5KuDGNh1pkezbl .arrowheadPath{fill:#333333;}#mermaid-svg-FY5KuDGNh1pkezbl .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-FY5KuDGNh1pkezbl .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-FY5KuDGNh1pkezbl .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-FY5KuDGNh1pkezbl .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-FY5KuDGNh1pkezbl .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-FY5KuDGNh1pkezbl .cluster text{fill:#333;}#mermaid-svg-FY5KuDGNh1pkezbl .cluster span{color:#333;}#mermaid-svg-FY5KuDGNh1pkezbl div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-FY5KuDGNh1pkezbl :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} HWDiskStore 分区 HWPartition 类名属性类型说明HWDiskStoregetName()获取名称getModel()获取型号getSerial()获取序列号getSize()获取大小getReads()读长getReadBytes()读取字节getWrites()写长getWriteBytes()写入字节getTransferTime()获取转移时间getPartitions()获取分区getTimeStamp()获取时间戳getCurrentQueueLength()当前队列长度HWPartition
3.1.3 内存信息Memory
HardwareAbstractionLayer hardware new SystemInfo().getHardware();
GlobalMemory globalMemory hardware.getMemory();
VirtualMemory virtualMemory globalMemory.getVirtualMemory();
ListPhysicalMemory physicalMemoryList globalMemory.getPhysicalMemory();#mermaid-svg-PrJqkkHTRuhunf55 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-PrJqkkHTRuhunf55 .error-icon{fill:#552222;}#mermaid-svg-PrJqkkHTRuhunf55 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-PrJqkkHTRuhunf55 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-PrJqkkHTRuhunf55 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-PrJqkkHTRuhunf55 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-PrJqkkHTRuhunf55 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-PrJqkkHTRuhunf55 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-PrJqkkHTRuhunf55 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-PrJqkkHTRuhunf55 .marker.cross{stroke:#333333;}#mermaid-svg-PrJqkkHTRuhunf55 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-PrJqkkHTRuhunf55 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-PrJqkkHTRuhunf55 .cluster-label text{fill:#333;}#mermaid-svg-PrJqkkHTRuhunf55 .cluster-label span{color:#333;}#mermaid-svg-PrJqkkHTRuhunf55 .label text,#mermaid-svg-PrJqkkHTRuhunf55 span{fill:#333;color:#333;}#mermaid-svg-PrJqkkHTRuhunf55 .node rect,#mermaid-svg-PrJqkkHTRuhunf55 .node circle,#mermaid-svg-PrJqkkHTRuhunf55 .node ellipse,#mermaid-svg-PrJqkkHTRuhunf55 .node polygon,#mermaid-svg-PrJqkkHTRuhunf55 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-PrJqkkHTRuhunf55 .node .label{text-align:center;}#mermaid-svg-PrJqkkHTRuhunf55 .node.clickable{cursor:pointer;}#mermaid-svg-PrJqkkHTRuhunf55 .arrowheadPath{fill:#333333;}#mermaid-svg-PrJqkkHTRuhunf55 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-PrJqkkHTRuhunf55 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-PrJqkkHTRuhunf55 .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-PrJqkkHTRuhunf55 .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-PrJqkkHTRuhunf55 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-PrJqkkHTRuhunf55 .cluster text{fill:#333;}#mermaid-svg-PrJqkkHTRuhunf55 .cluster span{color:#333;}#mermaid-svg-PrJqkkHTRuhunf55 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-PrJqkkHTRuhunf55 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 内存信息:Memory GlobalMemory VirtualMemory PhysicalMemory 类名说明GlobalMemory负责跟踪计算机物理内存(RAM)以及可用虚拟内存使用情况VirtualMemory用于获取虚拟内存的相关信息PhysicalMemory用于获取物理内存的相关信息
类对象方法类型说明GlobalMemorygetTotal()long获取以字节为单位的实际内存数量getAvailable()long获取以字节为单位的当前可用的物理内存量getSwapTotal()long获取可用虚拟总内存getSwapUsed()long获取已用虚拟总内存getVirtualMemory()getPhysicalMemory()ListVirtualMemorygetSwapTotal()long用于获取交换空间的总大小getSwapUsed()long用于获取已使用的交换空间的大小getVirtualMax();long用于获取系统支持的最大虚拟内存大小getVirtualInUse()long用于获取当前正在使用的虚拟内存的大小getSwapPagesIn()long于获取最近从硬盘上换入的页面数getSwapPagesOut()longPhysicalMemorygetBankLabel()String用于获取内存条的标签或标识符getCapacity()long用于获取物理内存的容量getClockSpeed()long用于获取物理内存的时钟速度getManufacturer()StringgetMemoryType()String
3.1.4 显卡信息graphicsCard
HardwareAbstractionLayer hardware new SystemInfo().getHardware();
ListGraphicsCard graphicsCards hardware.getGraphicsCards()方法说明getName()名称getDeviceId()设备号getVendor()制造商getVersionInfo()版本信息
3.1.5 网络接口信息NetworkIF
HardwareAbstractionLayer hardware new SystemInfo().getHardware();
ListNetworkIF networkIFs hardware.getNetworkIFs();方法说明getName()获取名称getDisplayName()获取显示名称getMTU()获取最大传输单元getMacaddr()获取MAC地址getIPv4addr()获取IPv4getIPv6addr()获取IPv6getBytesRecv()获取接收字节数getBytesSent()获取发送字节数getPacketsRecv()获取接收数据包getPacketsSent()获取发送数据包getInErrors()是否可达正常 0getOutErrors()响应错误无错误 0getSpeed()获取速率getTimeStamp()获取时间戳
3.1.6 电源信息PowerSource
HardwareAbstractionLayer hardware new SystemInfo().getHardware();
ListPowerSource powerSources hardware.getPowerSources();方法说明getAmperage()安培getCapacityUnits()容量单位getChemistry()getCurrentCapacity()当前容量getCycleCount()循环次数getDesignCapacity()初始容量getDeviceName()设备名字getManufactureDate()制造日期getManufacturer()制造商getMaxCapacity()电池当前最大容量getName()获取名称getPowerUsageRate()电池使用率getRemainingCapacityPercent()电量剩余百分比getSerialNumber()getTemperature()当前温度getTimeRemainingEstimated()getTimeRemainingInstant()getVoltage()电压isCharging()isDischarging()isPowerOnLine()
3.1.7 传感器信息Sensors
HardwareAbstractionLayer hardware new SystemInfo().getHardware();
Sensors sensors hardware.getSensors();方法说明getCpuTemperature()获取CPU温度getFanSpeeds()获取风扇速度getCpuVoltage()获取CPU电压
3.1.8 usb设备信息UsbDevice
HardwareAbstractionLayer hardware new SystemInfo().getHardware();
ListUsbDevice usbDevices hardware.getUsbDevices(true);方法说明getName()获取名称getVendor()获取供应商getVendorId()获取供应商idgetProductId()获取商品idgetSerialNumber()获取序列号getConnectedDevices()获取连接设备
3.2 CPU信息CentralProcessor #mermaid-svg-ADEyq3ZYNSg3Esx3 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .error-icon{fill:#552222;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .marker.cross{stroke:#333333;}#mermaid-svg-ADEyq3ZYNSg3Esx3 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .cluster-label text{fill:#333;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .cluster-label span{color:#333;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .label text,#mermaid-svg-ADEyq3ZYNSg3Esx3 span{fill:#333;color:#333;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .node rect,#mermaid-svg-ADEyq3ZYNSg3Esx3 .node circle,#mermaid-svg-ADEyq3ZYNSg3Esx3 .node ellipse,#mermaid-svg-ADEyq3ZYNSg3Esx3 .node polygon,#mermaid-svg-ADEyq3ZYNSg3Esx3 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .node .label{text-align:center;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .node.clickable{cursor:pointer;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .arrowheadPath{fill:#333333;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .cluster text{fill:#333;}#mermaid-svg-ADEyq3ZYNSg3Esx3 .cluster span{color:#333;}#mermaid-svg-ADEyq3ZYNSg3Esx3 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ADEyq3ZYNSg3Esx3 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} CentralProcessor LogicalProcessor PhysicalProcessor ProcessorIdentifier HardwareAbstractionLayer hardware new SystemInfo().getHardware();
CentralProcessor processor hardware.getProcessor();类名方法说明CentralProcessorgetVendor()获取供应商getName()获取cpu名称getVendorFreq()获取供应商频率getProcessorID()获取处理器idgetIdentifier()获取标识符isCpu64bit()判断cpu是否为64位的getStepping()获取cpu步进getModel()获取型号getFamily()获取家族getSystemCpuLoadBetweenTicks()获取cpu负载间隔刻度getSystemCpuLoadTicks()获取cpu负载刻度getSystemCpuLoad()获取cpu负载getSystemLoadAverage()获取cpu平均负载getSystemLoadAverage(int var1)获取cpu平均负载getProcessorCpuLoadBetweenTicks()获取处理器cpu负载间隔刻度getProcessorCpuLoadTicks()获取处理器cpu负载刻度getSystemUptime()获取正常运行时间getLogicalProcessorCount()获取逻辑处理器数量getPhysicalProcessorCount()获取物理处理器数量getPhysicalPackageCount()获取物理包装数量getContextSwitches()获取上下文切换数量getInterrupts()获取中断LogicalProcessorgetNumaNodeNUMA节点getPhysicalPackageNumber物理包数getPhysicalProcessorNumber物理处理器数getProcessorGroup处理器组getProcessorNumber处理器序号PhysicalProcessorgetEfficiency效率getIdString字符串idgetPhysicalPackageNumber物理包数getPhysicalProcessorNumber物理处理器数ProcessorIdentifiergetFamily族getIdentifier标识符getMicroarchitecture微架构getModel型号getName名称getProcessorID处理器编号getStepping步进getVendorcpu供应商getVendorFreq厂家频率isCpu64bit是否是64位系统
3.3 计算机系统ComputerSystem 计算机系统代表计算机系统/产品的物理硬件包括BIOS /固件和主板、逻辑板等。 // 获取硬件信息
HardwareAbstractionLayer hardware new SystemInfo().getHardware();
ComputerSystem computerSystem hardware.getComputerSystem();#mermaid-svg-Cys4G4GwltQBbeRl {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-Cys4G4GwltQBbeRl .error-icon{fill:#552222;}#mermaid-svg-Cys4G4GwltQBbeRl .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Cys4G4GwltQBbeRl .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-Cys4G4GwltQBbeRl .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Cys4G4GwltQBbeRl .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Cys4G4GwltQBbeRl .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Cys4G4GwltQBbeRl .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Cys4G4GwltQBbeRl .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Cys4G4GwltQBbeRl .marker.cross{stroke:#333333;}#mermaid-svg-Cys4G4GwltQBbeRl svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Cys4G4GwltQBbeRl .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-Cys4G4GwltQBbeRl .cluster-label text{fill:#333;}#mermaid-svg-Cys4G4GwltQBbeRl .cluster-label span{color:#333;}#mermaid-svg-Cys4G4GwltQBbeRl .label text,#mermaid-svg-Cys4G4GwltQBbeRl span{fill:#333;color:#333;}#mermaid-svg-Cys4G4GwltQBbeRl .node rect,#mermaid-svg-Cys4G4GwltQBbeRl .node circle,#mermaid-svg-Cys4G4GwltQBbeRl .node ellipse,#mermaid-svg-Cys4G4GwltQBbeRl .node polygon,#mermaid-svg-Cys4G4GwltQBbeRl .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Cys4G4GwltQBbeRl .node .label{text-align:center;}#mermaid-svg-Cys4G4GwltQBbeRl .node.clickable{cursor:pointer;}#mermaid-svg-Cys4G4GwltQBbeRl .arrowheadPath{fill:#333333;}#mermaid-svg-Cys4G4GwltQBbeRl .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Cys4G4GwltQBbeRl .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Cys4G4GwltQBbeRl .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-Cys4G4GwltQBbeRl .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-Cys4G4GwltQBbeRl .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Cys4G4GwltQBbeRl .cluster text{fill:#333;}#mermaid-svg-Cys4G4GwltQBbeRl .cluster span{color:#333;}#mermaid-svg-Cys4G4GwltQBbeRl div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Cys4G4GwltQBbeRl :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} ComputerSystem 固件信息 Firmware 主板信息 Baseboard 属性子属性类型说明manufacturerString制造商modelString型号serialNumberString序列号Firware固件信息nameString固件名称versionString固件版本describeString固件描述manufacturerString制造商releaseDateString发行日期Baseboard主板信息versionString主板版本modelString模型serialNumberString序列号manufacturerString制造商
四、使用场景
4.1 获取 CPU 信息 想要获取 CPU 信息就需要CentralProcessor 类中央处理器作为参数oshi库获取CentralProcessor 类步骤为 首先实例化内置的SystemInfo类该类用于访问系统和硬件信息。 然后根据此类的内置方法获取硬件抽象层类 HardwareAbstractionLayer 类主要用于将硬件抽象化允许计算机操作系统在逻辑层而不是硬件层与硬件设备交互。 最后根据HardwareAbstractionLayer类内置方法获取CentralProcessor 类提供setCpuInfo方法参数
public static MapString, Object getCpuInfo() throws InterruptedException {HardwareAbstractionLayer hardware new SystemInfo().getHardware();MapString, Object cpuInfo Maps.newHashMap();CentralProcessor processor hardware.getProcessor();// 第一次获取获取系统范围的 CPU 负载滴答计数器long[] prevTicks processor.getSystemCpuLoadTicks();//等待一段时间java.lang.Thread.sleep(1000);//第二次获取获取系统范围的 CPU 负载滴答计数器long[] ticks processor.getSystemCpuLoadTicks();long user ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];long nice ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];long cSys ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];long idle ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];long ioWait ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.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 totalCpu user nice cSys idle ioWait irq softIrq steal;// cpu供应商cpuInfo.put(cpuVendor, processor.getProcessorIdentifier().getVendor());// cpu名称cpuInfo.put(cpuName, processor.getProcessorIdentifier().getName());// CPU核心数cpuInfo.put(cpuNum, processor.getLogicalProcessorCount());// CPU总的使用率cpuInfo.put(totalPercent, totalCpu);// CPU用户使用率表示用户模式下花费的CPU时间cpuInfo.put(usedPercent, new DecimalFormat(#.##%).format(user * 1.0 / totalCpu));// CPU当前等待率表示等待I/O操作的进程所花费的CPU时间cpuInfo.put(waitPercent, new DecimalFormat(#.##%).format(ioWait * 1.0 / totalCpu));// CPU系统使用率表示系统进程所花费的CPU时间cpuInfo.put(sysPercent, new DecimalFormat(#.##%).format(cSys * 1.0 / totalCpu));// CPU当前空闲率表示空闲进程所花费的CPU时间cpuInfo.put(idlePercent, new DecimalFormat(#.##%).format(idle * 1.0 / totalCpu));// 表示在nice值为099之间的进程所花费的CPU时间cpuInfo.put(nicePercent, new DecimalFormat(#.##%).format(nice * 1.0 / totalCpu));// 表示处理中断的进程所花费的CPU时间cpuInfo.put(irqPercent, new DecimalFormat(#.##%).format(irq * 1.0 / totalCpu));// 表示处理软中断的进程所花费的CPU时间cpuInfo.put(softIrqPercent, new DecimalFormat(#.##%).format(softIrq * 1.0 / totalCpu));// 表示被其他线程“偷走”的CPU时间cpuInfo.put(stealPercent, new DecimalFormat(#.##%).format(steal * 1.0 / totalCpu));// 物理处理器数量cpuInfo.put(physicalProcessorCount, processor.getPhysicalProcessorCount());return cpuInfo;
}4.2 获取内存信息 想要获取内存信息这就需要硬件抽象层类提供的GlobalMemory类作为参数通过GlobalMemory类的内置方法来获取内存信息。
public static MapString, Object getMemoryInfo() {HardwareAbstractionLayer hardware new SystemInfo().getHardware();GlobalMemory globalMemory hardware.getMemory();MapString, Object gmMap Maps.newHashMap();// 总内存gmMap.put(total, FormatUtil.formatBytes(globalMemory.getTotal()));// 剩余【当前可用的物理内存量】gmMap.put(available, FormatUtil.formatBytes(globalMemory.getAvailable()));// 已使用gmMap.put(used, FormatUtil.formatBytes(globalMemory.getTotal() - globalMemory.getAvailable()));// 空闲率gmMap.put(freeRate, 100d * (globalMemory.getAvailable()) / globalMemory.getTotal());// 使用率gmMap.put(usageRate, 100d * (globalMemory.getTotal() - globalMemory.getAvailable()) / globalMemory.getTotal());gmMap.put(pageSize, globalMemory.getPageSize());VirtualMemory virtualMemory globalMemory.getVirtualMemory();MapString, Object vmMap Maps.newHashMap();vmMap.put(toString, virtualMemory);vmMap.put(swapTotal, FormatUtil.formatBytes(virtualMemory.getSwapTotal()));vmMap.put(swapUsed, FormatUtil.formatBytes(virtualMemory.getSwapUsed()));vmMap.put(swapUsageRate, 100d * virtualMemory.getSwapUsed() / virtualMemory.getSwapTotal());vmMap.put(virtualMax, FormatUtil.formatBytes(virtualMemory.getVirtualMax()));vmMap.put(virtualInUse, FormatUtil.formatBytes(virtualMemory.getVirtualInUse()));vmMap.put(virtualUsageRate, 100d * virtualMemory.getVirtualInUse() / virtualMemory.getVirtualMax());gmMap.put(virtualMemory, vmMap);ListPhysicalMemory physicalMemoryList globalMemory.getPhysicalMemory();ListMapString, Object pmInfoList new ArrayList(physicalMemoryList.size());for (PhysicalMemory pm : physicalMemoryList) {MapString, Object pmMap Maps.newHashMap();pmMap.put(toString, String.valueOf(pm));pmMap.put(bankLabel, pm.getBankLabel());pmMap.put(manufacturer, pm.getManufacturer());pmMap.put(capacity, FormatUtil.formatBytes(pm.getCapacity()));pmMap.put(memoryType, pm.getMemoryType());pmMap.put(clockSpeed, FormatUtil.formatHertz(pm.getClockSpeed()));pmInfoList.add(pmMap);}gmMap.put(physicalMemoryList, pmInfoList);return gmMap;
}4.3 获取服务器信息 首先调用Java内置的System类的 getProperties() 方法获取当前系统属性集并作为一个Properties对象返回并通过对该属性集通过传参查询得到操作系统名称、系统架构、项目路径等信息。
public static MapString, Object getSysInfo() throws UnknownHostException {MapString, Object sysInfo Maps.newHashMap();Properties props System.getProperties();// 操作系统sysInfo.put(osName, props.getProperty(os.name));// 系统架构sysInfo.put(osArch, props.getProperty(os.arch));// 服务器名称sysInfo.put(computerName, InetAddress.getLocalHost().getHostName());// 服务器IPsysInfo.put(computerIp, InetAddress.getLocalHost().getHostAddress());// 项目路径sysInfo.put(userDir, props.getProperty(user.dir));// 操作系统信息sysInfo.put(desktop, props.getProperty(sun.desktop));return sysInfo;
}4.4 获取Java虚拟机信息 首先通过System系统类获取当前系统属性集后使用Properties对象传参查询的得到JDK版本与路径通过获得Java内置的RunTime对象每个Java应用程序都有一个单独的Runtime类实例它允许应用程序与运行应用程序的环境进行接口。当前运行时可以从getRuntime方法中获得获取Java虚拟机的总内存量、 Java 虚拟机试图使用的最大内存量、Java虚拟机中的可用内存量。
public static MapString, Object getJvmInfo() {MapString, Object jvmInfo new ConcurrentHashMap();Properties props System.getProperties();Runtime runtime Runtime.getRuntime();long jvmTotalMemoryByte runtime.totalMemory();long jvmFreeMemoryByte runtime.freeMemory();long jvmUseMemoryByte jvmTotalMemoryByte - jvmFreeMemoryByte;// jvm总内存jvmInfo.put(totalMemory, FormatUtil.formatBytes(jvmTotalMemoryByte));// JVM空闲内存jvmInfo.put(freeMemory, FormatUtil.formatBytes(jvmFreeMemoryByte));// Jvm已使用内存jvmInfo.put(usedMemory, FormatUtil.formatBytes(jvmUseMemoryByte));// JVM最大可用内存总数jvmInfo.put(maxMemory, FormatUtil.formatBytes(runtime.maxMemory()));// jvm内存使用率jvmInfo.put(useRate, new DecimalFormat(#.##%).format((jvmUseMemoryByte) * 1.0 / jvmTotalMemoryByte));// jvm内存空闲率jvmInfo.put(freeRate, new DecimalFormat(#.##%).format((jvmFreeMemoryByte) * 1.0 / jvmTotalMemoryByte));// Java版本jvmInfo.put(jdkVersion, props.getProperty(java.version));// jdk安装目录jvmInfo.put(jdkHome, props.getProperty(java.home));// java类包的路径props.getProperty(java.class.path);//java虚拟机制造商props.getProperty(java.vm.vendor);// jdk bin目录props.getProperty(sun.boot.library.path);// java运行环境版本props.getProperty(java.runtime.version);// 依赖路径props.getProperty(java.library.path);// java制造商props.getProperty(java.vendor);// java虚拟机信息props.getProperty(java.vm.info);// java虚拟机版本props.getProperty(java.vm.version);// java类版本props.getProperty(java.class.version);return jvmInfo;
}4.5 获取系统文件相关信息 首先通过 SystemInfo类获取当前平台操作系统的新实例再根据 OperatingSystem.getFileSystem()方法实例化FileSystem对象再通过 getFileStores() 获取本机上文件存储实例化OSFileStore对象列表表示存储池、设备、分区、卷、具体的文件系统或其他实现特定的文件存储方法信息该方法会返回OSFileStore对象列表。最后遍历此集合获取磁盘状态信息。
public static MapString, Object getFileSystemInfo() {MapString, Object fsInfo new ConcurrentHashMap();FileSystem fileSystem new SystemInfo().getOperatingSystem().getFileSystem();ListMapString, Object fileSystemInfos new ArrayList();// 打开文件描述符fsInfo.put(openFileDescriptors, fileSystem.getOpenFileDescriptors());// 最大文件描述符fsInfo.put(maxFileDescriptors, fileSystem.getMaxFileDescriptors());fsInfo.put(fileDescriptors, String.format(%d/%d, fileSystem.getOpenFileDescriptors(), fileSystem.getMaxFileDescriptors()));fsInfo.put(fdUsageRate, (100d * fileSystem.getOpenFileDescriptors() / fileSystem.getMaxFileDescriptors()) %);ListOSFileStore fileStores fileSystem.getFileStores();for (int i 0; i fileStores.size(); i) {MapString, Object fileStoreInfo Maps.newHashMap();fileStoreInfo.put(index, i);OSFileStore osFileStore fileStores.get(i);// 磁盘名称fileStoreInfo.put(name, osFileStore.getName());// 文件系统的卷名fileStoreInfo.put(volume, osFileStore.getVolume());// 标签fileStoreInfo.put(label, osFileStore.getLabel());// 文件系统的逻辑卷名fileStoreInfo.put(logicalVolume, osFileStore.getLogicalVolume());// 文件系统的挂载点fileStoreInfo.put(mount, osFileStore.getMount());// 说明fileStoreInfo.put(description, osFileStore.getDescription());// 盘符类型fileStoreInfo.put(sysTypeName, osFileStore.getType());// 文件系统的选项fileStoreInfo.put(options, osFileStore.getOptions());fileStoreInfo.put(uuid, osFileStore.getUUID());fileStoreInfo.put(freeSpace, osFileStore.getFreeSpace());// 剩余大小fileStoreInfo.put(usableSpace, FormatUtil.formatBytes(osFileStore.getUsableSpace()));// 总大小fileStoreInfo.put(totalSpace, FormatUtil.formatBytes(osFileStore.getTotalSpace()));fileStoreInfo.put(freeInodes, FormatUtil.formatValue(osFileStore.getFreeInodes(), ));fileStoreInfo.put(totalInodes, FormatUtil.formatValue(osFileStore.getTotalInodes(), ));// 已经使用量fileStoreInfo.put(usedSpace, FormatUtil.formatBytes(osFileStore.getTotalSpace() - osFileStore.getUsableSpace()));// 使用率String usageRate 0;if (osFileStore.getTotalSpace() 0) {usageRate new DecimalFormat(#.##%).format((osFileStore.getTotalSpace() - osFileStore.getUsableSpace()) * 1.0 / osFileStore.getTotalSpace());}fileStoreInfo.put(usageRate, usageRate);fileStoreInfo.put(inodesUsageRate, 100d * osFileStore.getFreeInodes() / osFileStore.getTotalInodes());fileSystemInfos.add(fileStoreInfo);}fsInfo.put(fileStores, fileSystemInfos);return fsInfo;
}4.6 获取磁盘信息
public static ListMapString, Object getDiskStoreInfo() {HardwareAbstractionLayer hardware new SystemInfo().getHardware();ListHWDiskStore diskStores hardware.getDiskStores();ListMapString, Object dsList new ArrayList(diskStores.size());for (int i 0; i diskStores.size(); i) {HWDiskStore hwDiskStore diskStores.get(i);MapString, Object hwDsMap new ConcurrentHashMap();hwDsMap.put(index, i);// 磁盘名称hwDsMap.put(name, hwDiskStore.getName());hwDsMap.put(currentQueueLength, hwDiskStore.getCurrentQueueLength());// 型号hwDsMap.put(model, hwDiskStore.getModel());// 序列号hwDsMap.put(serial, hwDiskStore.getSerial());// 大小hwDsMap.put(size, FormatUtil.formatBytes(hwDiskStore.getSize()));// 读长hwDsMap.put(reads, FormatUtil.formatBytes(hwDiskStore.getReads()));// 写长hwDsMap.put(writes, FormatUtil.formatBytes(hwDiskStore.getWrites()));// 读取字节hwDsMap.put(readBytes, hwDiskStore.getReadBytes());// 写入字节hwDsMap.put(writeBytes, hwDiskStore.getWriteBytes());// 转移时间hwDsMap.put(transferTime, hwDiskStore.getTransferTime());// 时间戳hwDsMap.put(timeStamp, hwDiskStore.getTimeStamp());ListHWPartition partitions hwDiskStore.getPartitions();ListMapString, Object partitionList new ArrayList(partitions.size());for (HWPartition partition : partitions) {MapString, Object partitionMap Maps.newHashMap();// 分区大小partitionMap.put(size, FormatUtil.formatBytes(partition.getSize()));// 分区名称partitionMap.put(name, partition.getName());// 分区类型partitionMap.put(type, partition.getType());partitionMap.put(identification, partition.getIdentification());partitionMap.put(major, partition.getMajor());partitionMap.put(uuid, partition.getUuid());partitionMap.put(mountPoint, partition.getMountPoint());partitionMap.put(minor, partition.getMinor());partitionList.add(partitionMap);}hwDsMap.put(partitionList, partitionList);dsList.add(hwDsMap);}return dsList;
}小结 把今天最好的表现当作明天最新的起点 投身于天地这熔炉一个人可以被毁灭但绝不会被打败一旦决定了心中所想便绝无动摇。迈向光明之路注定荆棘丛生自己选择的路即使再荒谬、再艰难跪着也要走下去放弃曾令人想要逃离但绝境重生方为宿命。若结果并非所愿那就在尘埃落定前奋力一搏
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/918902.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!