龙空技术网

Sigar获取硬件信息

说说开发那些事 36

前言:

如今同学们对“java获取客户端硬件信息”大约比较注重,看官们都需要学习一些“java获取客户端硬件信息”的相关内容。那么小编在网摘上网罗了一些关于“java获取客户端硬件信息””的相关内容,希望我们能喜欢,看官们一起来学习一下吧!

准备工作

Sigar可以获取系统信息,如CPU主板、网卡、硬盘、内存信息或者使用率。下载依赖库,将hyperic-sigar-1.6.4\sigar-bin\lib下的文件按以下方式配置:

Windows下配置:选择sigar-amd64-winnt.dll或者sigar-x86-winnt.dll拷贝到C:\Windows\System32中Linux下配置:将libsigar-amd64-linux.so或者libsigar-x86-linux.so拷贝到/usr/lib64或者/lib64或者/usr/lib,在不起作用的情况下,修改sudo chmod 744文件权限调用sigar的本质是dll或者so的调用,需要注意jdk8的版本需要为231版本,否则过高的版本会报以下错误

#  # A fatal error has been detected by the Java Runtime Environment:  #  #  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000010014ed4, pid=7196, tid=12596  #  # JRE version: Java(TM) SE Runtime Environment (17.0.6+9) (build 17.0.6+9-LTS-190)  # Java VM: Java HotSpot(TM) 64-Bit Server VM (17.0.6+9-LTS-190, mixed mode, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, windows-amd64)  # Problematic frame:  # C  [sigar-amd64-winnt.dll+0x14ed4]  #  # No core dump will be written. Minidumps are not enabled by default on client versions of Windows  #  # If you would like to submit a bug report, please visit:  #     # The crash happened outside the Java Virtual Machine in native code.  # See problematic frame for where to report the bug.  #

示例代码

引入maven依赖

<dependency>      <groupId>org.fusesource</groupId>      <artifactId>sigar</artifactId>      <version>1.6.4</version>  </dependency>
示例代码
@Slf4j  public class SigarTest {        private final static Sigar sigar = new Sigar();      private final static int MB = 1024 * 1024;        @Test      public void os() throws Exception {          final OperatingSystem os = OperatingSystem.getInstance();          log.info("OS架构:{},OSCPU端模式:{}, OS生产商:{}, OS版本:{}, OS描述:{}", os.getArch(), os.getCpuEndian(),                  os.getVendor(), os.getVersion(), os.getDescription());      }        @Test      public void who() throws Exception {          final Who[] whoList = sigar.getWhoList();          final int length = whoList.length;          for (int i = 0; i < length; i++) {              final Who who = whoList[i];              log.info("当前用户:{}, 登录host:{}, 用户控制台:{}, 登录时间:{}", who.getUser(), who.getHost(), who.getDevice(), who.getTime());          }      }        @Test      public void net() throws Exception {          final String[] netInterfaceList = sigar.getNetInterfaceList();          for (int i = 0; i < netInterfaceList.length; i++) {              final String networkName = netInterfaceList[i];              final NetInterfaceConfig config = sigar.getNetInterfaceConfig(networkName);              if(Objects.equals(NetFlags.NULL_HWADDR, config.getHwaddr()) || Objects.equals(NetFlags.ANY_ADDR, config.getAddress())) {                  continue;              }              log.info("网络名:{}, IP地址:{}, 子网掩码{}, 网卡MAC:{}, flags:{}", networkName,                      config.getAddress(), config.getNetmask(), config.getHwaddr(), config.getFlags());          }      }        @Test      public void memory() throws Exception {          //内存          final Mem mem = sigar.getMem();          log.info("内存总量:{}MB", mem.getTotal() / MB);       //内存总量          log.info("当前内存使用量:{}MB", mem.getUsed() / MB);   //当前内存使用量          log.info("当前内存剩余量:{}MB", mem.getFree() / MB);   //当前内存剩余量            //交换区          final Swap swap = sigar.getSwap();          log.info("Swap总量:{}MB", swap.getTotal() / MB);          log.info("Swap当前使用量:{}MB", swap.getUsed() / MB);          log.info("Swap剩余量:{}MB", swap.getFree() / MB);      }        @Test      public void cpu() throws Exception {          final CpuInfo[] cpuInfoList = sigar.getCpuInfoList();   //CPU信息          final CpuPerc[] cpuPercList = sigar.getCpuPercList();   //CPU运行特性          final int coreSize = cpuInfoList.length;   //核心数          for (int i = 0; i < coreSize; i++) {              final CpuInfo cpuInfo = cpuInfoList[i];              final CpuPerc cpuPerc = cpuPercList[i];              log.info("第{}块CPU的信息-频率:{}GHz,生产商:{},类别:{},缓存数量:{},CPU用户使用率:{},系统使用率:{}", i + 1, cpuInfo.getMhz() / 1000.0,                      cpuInfo.getVendor(), cpuInfo.getModel(), cpuInfo.getCacheSize(), CpuPerc.format(cpuPerc.getUser()), CpuPerc.format(cpuPerc.getSys()));          }      }  }

标签: #java获取客户端硬件信息