예제 #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();
  }
  public static void getServerMemoryInfo(Sigar sigar, ServerStatus status) {
    try {
      Mem mem = sigar.getMem();
      status.setTotalMem(mem.getTotal() / (1024 * 1024));
      status.setUsedMem(mem.getUsed() / (1024 * 1024));
      status.setFreeMem(mem.getFree() / (1024 * 1024));
      // 交换区
      Swap swap = sigar.getSwap();
      status.setTotalSwap(swap.getTotal() / (1024 * 1024));
      status.setUsedSwap(swap.getUsed() / (1024 * 1024));
      status.setFreeSwap(swap.getFree() / (1024 * 1024));
    } catch (Exception e) {

    }
  }
예제 #3
0
파일: SigarTest.java 프로젝트: wxquan/Share
  /**
   * 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 ServerInfoFormMap memory(Sigar sigar) {
    ServerInfoFormMap monitorMap = new ServerInfoFormMap();
    try {
      Runtime r = Runtime.getRuntime();
      monitorMap.put("jvmTotal", Common.div(r.totalMemory(), (1024 * 1024), 2) + "M"); // java总内存
      monitorMap.put(
          "jvmUse",
          Common.div(r.totalMemory() - r.freeMemory(), (1024 * 1024), 2) + "M"); // JVM使用内存
      monitorMap.put("jvmFree", Common.div(r.freeMemory(), (1024 * 1024), 2) + "M"); // JVM剩余内存
      monitorMap.put(
          "jvmUsage", Common.div(r.totalMemory() - r.freeMemory(), r.totalMemory(), 2)); // JVM使用率

      Mem mem = sigar.getMem();
      // 内存总量
      monitorMap.put("ramTotal", Common.div(mem.getTotal(), (1024 * 1024 * 1024), 2) + "G"); // 内存总量
      monitorMap.put("ramUse", Common.div(mem.getUsed(), (1024 * 1024 * 1024), 2) + "G"); // 当前内存使用量
      monitorMap.put(
          "ramFree", Common.div(mem.getFree(), (1024 * 1024 * 1024), 2) + "G"); // 当前内存剩余量
      monitorMap.put("ramUsage", Common.div(mem.getUsed(), mem.getTotal(), 2)); // 内存使用率

      Swap swap = sigar.getSwap();
      // 交换区总量
      monitorMap.put("swapTotal", Common.div(swap.getTotal(), (1024 * 1024 * 1024), 2) + "G");
      // 当前交换区使用量
      monitorMap.put("swapUse", Common.div(swap.getUsed(), (1024 * 1024 * 1024), 2) + "G");
      // 当前交换区剩余量
      monitorMap.put("swapFree", Common.div(swap.getFree(), (1024 * 1024 * 1024), 2) + "G");
      monitorMap.put("swapUsage", Common.div(swap.getUsed(), swap.getTotal(), 2)); //

    } catch (Exception e) {
    }
    return monitorMap;
  }
  public static ServerInfoFormMap usage(Sigar sigar) {
    ServerInfoFormMap monitorMap = new ServerInfoFormMap();
    try {
      Runtime r = Runtime.getRuntime();
      monitorMap.put(
          "jvmUsage",
          Math.round(
              Common.div(r.totalMemory() - r.freeMemory(), r.totalMemory(), 2) * 100)); // JVM使用率

      Mem mem = sigar.getMem();
      // 内存总量
      monitorMap.put(
          "ramUsage", Math.round(Common.div(mem.getUsed(), mem.getTotal(), 2) * 100)); // 内存使用率

      List<ServerInfoFormMap> cpu = cpuInfos(sigar);
      double b = 0.0;
      for (ServerInfoFormMap m : cpu) {
        b += Double.valueOf(m.get("cpuTotal") + "");
      }
      monitorMap.put("cpuUsage", Math.round(b / cpu.size())); // cpu使用率
    } catch (Exception e) {
    }
    return monitorMap;
  }