示例图

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

API文档地址 硬件信息接口 系统信息接口

MAVEN

<!-- https://mvnrepository.com/artifact/com.github.oshi/oshi-core -->
<dependency>
  <groupId>com.github.oshi</groupId>
  <artifactId>oshi-core</artifactId>
  <version>5.7.3</version>
</dependency>

获取本机ip地址

SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
name = si.getOperatingSystem().toString();
// 获取ip地址
List<NetworkIF> lists = si.getHardware().getNetworkIFs();
for (NetworkIF networkIF : lists) {
  if (networkIF.getIPv4addr().length > 0) {
    String ip = "";
    for (String ipp : networkIF.getIPv4addr()) {//遍历ipv4地址
      if (ip.length() > 0)
        ip += ".";
      ip += ipp;
    }
    if (!ip.startsWith("169") && !ip.startsWith("127"))//过滤本地地址
      ips.add(ip);
  }
}

获取连接数

si.getOperatingSystem().getInternetProtocolStats().getConnections().size();

进程数

si.getOperatingSystem().getProcessCount();

线程数

si.getOperatingSystem().getThreadCount();

系统启动时长

si.getOperatingSystem().getSystemUptime();

获取cpu信息

// CPU信息
HardwareAbstractionLayer hal = si.getHardware();
CentralProcessor processor=hal.getProcessor()
long[] prevTicks = processor.getSystemCpuLoadTicks();
Util.sleep(OSHI_WAIT_SECOND);
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;
cpu.setCpuNum(processor.getLogicalProcessorCount());//cpu个数
cpu.setTotal(1.0 - (idle * 1.0 / totalCpu));
cpu.setSys(cSys * 1.0 / totalCpu);
cpu.setUsed(user * 1.0 / totalCpu);
cpu.setWait(iowait * 1.0 / totalCpu);
cpu.setTotal(Arith.round(cpu.getTotal() * 100, 2));
cpu.setSys(Arith.round(cpu.getSys() * 100, 2));
cpu.setUsed(Arith.round(cpu.getUsed() * 100, 2));//cpu使用率
cpu.setWait(Arith.round(cpu.getWait() * 100, 2));
cpu.setName(processor.getProcessorIdentifier().getName());//cpu型号

获取内存信息

GlobalMemory memory=hal.getMemory();
memory.getTotal()//内存总数
memory.getAvailable()//内存空闲
memory.getTotal() - memory.getAvailable()//内存使用

获取磁盘信息

OperatingSystem os=si.getOperatingSystem()
FileSystem fileSystem = os.getFileSystem();
List<OSFileStore> fsArray = fileSystem.getFileStores();
for (OSFileStore fs : fsArray) {
  long free = fs.getUsableSpace();
  long total = fs.getTotalSpace();
  long used = total - free;
  SysFile sysFile = new SysFile();
  sysFile.setDirName(fs.getMount());
  sysFile.setSysTypeName(fs.getType());
  sysFile.setTypeName(fs.getName());
  sysFile.setTotal(Arith.round(getGbFileSize(total), 2));//总容量
  sysFile.setFree(Arith.round(getGbFileSize(free), 2));//空闲大小
  sysFile.setUsed(Arith.round(getGbFileSize(used), 2));//已用空间
  if (total > 0) {
    sysFile.setUsage(Arith.mul(Arith.div(used, total, 4), 100));//使用率
  } else {
    sysFile.setUsage(0);
  }
  if (sysFile.getTotal() < 5) {//过滤小于5GB的磁盘,一般都是光驱
    continue;
  }

  sysFiles.add(sysFile);
}

获取系统服务

OSService[] oss = si.getOperatingSystem().getServices();
for (OSService osService : oss) {
    //检查状态
    if(osService.getState().name().equals("STOPPED")){
      //TO DO 启用服务
    }
}