public static void getServerCpuInfo(Sigar sigar, ServerStatus status) {
   try {
     CpuInfo infos[] = sigar.getCpuInfoList();
     CpuPerc cpuList[] = sigar.getCpuPercList();
     double totalUse = 0L;
     for (int i = 0; i < infos.length; i++) {
       CpuPerc perc = cpuList[i];
       ServerStatus.CpuInfoVo cpuInfo = new ServerStatus.CpuInfoVo();
       cpuInfo.setId(infos[i].hashCode() + "");
       cpuInfo.setCacheSize(infos[i].getCacheSize());
       cpuInfo.setModel(infos[i].getModel());
       cpuInfo.setUsed(CpuPerc.format(perc.getCombined()));
       cpuInfo.setUsedOrigVal(perc.getCombined());
       cpuInfo.setIdle(CpuPerc.format(perc.getIdle()));
       cpuInfo.setTotalMHz(infos[i].getMhz());
       cpuInfo.setVendor(infos[i].getVendor());
       status.getCpuInfos().add(cpuInfo);
       totalUse += perc.getCombined();
     }
     String cpuu = CpuPerc.format(totalUse / status.getCpuInfos().size());
     cpuu = cpuu.substring(0, cpuu.length() - 1);
     status.setCpuUsage(cpuu);
   } catch (Exception e) {
   }
 }
Exemple #2
0
  /**
   * main(这里用一句话描述这个方法的作用) TODO(这里描述这个方法适用条件 – 可选) TODO(这里描述这个方法的执行流程 – 可选) TODO(这里描述这个方法的使用方法 – 可选)
   * TODO(这里描述这个方法的注意事项 – 可选) @Title: main @Description: TODO
   *
   * @param @param args 设定文件
   * @return void 返回类型
   * @throws
   */
  public static void main(String[] args) {

    Sigar sigar = new Sigar();
    try {
      Mem mem = sigar.getMem();
      CpuPerc cpuCerc = sigar.getCpuPerc();
      System.out.println("*****当前CPU使用情况 :");
      System.out.println("#总使用率: " + cpuCerc.getCombined() * 100 + "%");
      System.out.println("#用户使用率(user): " + cpuCerc.getUser() * 100 + "%");
      System.out.println("#系统使用率(sys): " + cpuCerc.getSys() * 100 + "%");
      System.out.println("#当前空闲率(idel): " + cpuCerc.getIdle() * 100 + "%");
      System.out.println("\n*****当前内存使用情况 :");
      System.out.println("#内存总量:" + mem.getTotal() / 1024 / 1024 + "M");
      System.out.println("#已使用内存:" + mem.getUsed() / 1024 / 1024 + "M");
      System.out.println("#剩余内存:" + mem.getFree() / 1024 / 1024 + "M");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 public static List<ServerInfoFormMap> cpuInfos(Sigar sigar) {
   List<ServerInfoFormMap> monitorMaps = new ArrayList<ServerInfoFormMap>();
   try {
     CpuPerc cpuList[] = sigar.getCpuPercList();
     for (CpuPerc cpuPerc : cpuList) {
       ServerInfoFormMap monitorMap = new ServerInfoFormMap();
       monitorMap.put("cpuUserUse", Math.round(cpuPerc.getUser() * 100)); // 用户使用率
       monitorMap.put("cpuSysUse", Math.round(cpuPerc.getSys() * 100)); // 系统使用率
       monitorMap.put("cpuWait", Math.round(cpuPerc.getWait() * 100)); // 当前等待率
       monitorMap.put("cpuFree", Math.round(cpuPerc.getIdle() * 100)); // 当前空闲率
       monitorMap.put("cpuTotal", Math.round(cpuPerc.getCombined() * 100)); // 总的使用率
       monitorMaps.add(monitorMap);
     }
   } catch (Exception e) {
   }
   return monitorMaps;
 }
 private void output(CpuPerc cpu) {
   println("User Time....." + CpuPerc.format(cpu.getUser())); // 用户使用率
   println("Sys Time......" + CpuPerc.format(cpu.getSys())); // 系统CPU使用率
   println("Idle Time....." + CpuPerc.format(cpu.getIdle())); // 当前空闲率
   println("Wait Time....." + CpuPerc.format(cpu.getWait())); // 当前等待率
   println("Nice Time....." + CpuPerc.format(cpu.getNice()));
   println("Combined......" + CpuPerc.format(cpu.getCombined())); // 总的使用率
   println("Irq Time......" + CpuPerc.format(cpu.getIrq()));
   println("");
 }