Example #1
0
  public static String construct(String uid, String key) throws JSONException, SigarException {
    JSONObject construct = new JSONObject();
    construct.put("uid", uid);
    construct.put("key", key);
    construct.put("hostname", sigar.getNetInfo().getHostName());

    JSONObject obj = new JSONObject();
    obj.put("uptime", formatUptime(sigar.getUptime().getUptime()));
    obj.put("load1", ((int) (sigar.getCpuPerc().getCombined() * 100)));
    obj.put("load5", 0);
    obj.put("load15", 0);
    construct.put("uplo", obj);

    Mem mem = sigar.getMem();

    JSONObject memory = new JSONObject();
    memory.put("total", byteToMByte(mem.getTotal()));
    memory.put("used", byteToMByte(mem.getUsed()));
    memory.put("free", byteToMByte(mem.getFree()));
    memory.put("bufcac", 0);

    construct.put("ram", memory);

    LinkedList<HashMap<String, Object>> fsystems = new LinkedList<HashMap<String, Object>>();
    long total = 0;
    long used = 0;
    long free = 0;
    for (File file : File.listRoots()) {
      long ftotal = file.getTotalSpace();
      long ffree = file.getFreeSpace();
      long fused = ftotal - ffree;

      HashMap<String, Object> system = new HashMap<String, Object>();
      system.put("mount", file.getPath());
      system.put("total", byteToKByte(ftotal));
      system.put("used", byteToKByte(fused));
      system.put("avail", byteToKByte(ffree));
      fsystems.add(system);

      total += ftotal;
      free += ffree;
      used += fused;
    }

    HashMap<String, Object> bla = new HashMap<String, Object>();
    bla.put("single", fsystems);
    HashMap<String, Object> tot = new HashMap<String, Object>();
    tot.put("total", byteToKByte(total));
    tot.put("free", byteToKByte(free));
    tot.put("used", byteToKByte(used));
    bla.put("total", tot);

    construct.put("disk", bla);

    return construct.toString();
  }
Example #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();
    }
  }