コード例 #1
0
 private CpuModel saveModel(CpuModel currentCpuModel, CpuModel preCpuModel) {
   CpuModel model = new CpuModel();
   model.setUserTime(currentCpuModel.getUserTime() - preCpuModel.getUserTime());
   model.setNiceTime(currentCpuModel.getNiceTime() - preCpuModel.getNiceTime());
   model.setSysTime(currentCpuModel.getSysTime() - preCpuModel.getSysTime());
   model.setIdleTime(currentCpuModel.getIdleTime() - preCpuModel.getIdleTime());
   model.setIowaitTime(currentCpuModel.getIowaitTime() - preCpuModel.getIowaitTime());
   model.setIrqTime(currentCpuModel.getIrqTime() - preCpuModel.getIrqTime());
   model.setSoftirqTime(currentCpuModel.getSoftirqTime() - preCpuModel.getSoftirqTime());
   model.setStealstolenTime(
       currentCpuModel.getStealstolenTime() - preCpuModel.getStealstolenTime());
   model.setGuestTime(currentCpuModel.getGuestTime() - preCpuModel.getGuestTime());
   model.setTotalCpuTime(currentCpuModel.getTotalCpuTime() - preCpuModel.getTotalCpuTime());
   return model;
 }
コード例 #2
0
 @Override
 public CpuInfoModel doHandle(String[] cmd) {
   if (null == cmd || cmd.length < 1) {
     LOGGER.warn("cmdArray is empty!");
     return null;
   }
   LOGGER.info("start excute the command : " + cmd);
   // 当前的CPU模型
   CpuInfoModel cpuModelInfo = new CpuInfoModel();
   List<CpuModel> cpuList = new ArrayList<CpuModel>();
   CmdResult cmdResult = getCmdOperateService().execute(cmd);
   List<String> comResultList = cmdResult.getCommonResList();
   if (comResultList != null && comResultList.size() > 0) {
     for (String result : comResultList) {
       result = result.trim();
       LOGGER.debug(result);
       Matcher m = Pattern.compile("^[1-9][0-9]*$").matcher(result);
       if (m.find()) {
         cpuModelInfo.setCpuNumber(Integer.parseInt(m.group()));
       }
       if (result.indexOf(MetricsConstant.CPU_LOAD) != -1) {
         float[] loadValue = parseFloatArry(result);
         cpuModelInfo.setLoadValue(loadValue);
       }
       if (result.startsWith(MetricsConstant.CPU_PREFIX)) {
         // 第一个放进去的始终是总的CPU信息
         String[] data = result.split("\\s+");
         CpuModel cpuModel = new CpuModel();
         cpuModel.setUserTime(Long.parseLong(data[1]));
         cpuModel.setNiceTime(Long.parseLong(data[2]));
         cpuModel.setSysTime(Long.parseLong(data[3]));
         cpuModel.setIdleTime(Long.parseLong(data[4]));
         cpuModel.setIowaitTime(Long.parseLong(data[5]));
         cpuModel.setIrqTime(Long.parseLong(data[6]));
         cpuModel.setSoftirqTime(Long.parseLong(data[7]));
         cpuModel.setStealstolenTime(Long.parseLong(data[8]));
         if (data.length > 9) {
           cpuModel.setGuestTime(Long.parseLong(data[9]));
         }
         cpuModel.setTotalCpuTime(sumTotalCpuTime(cpuModel));
         cpuList.add(cpuModel);
       }
     }
     cpuModelInfo.setCpuList(cpuList);
   }
   // 要返回的CPU模型
   CpuInfoModel retCpuInfoModel = new CpuInfoModel();
   retCpuInfoModel.setCpuNumber(cpuModelInfo.getCpuNumber());
   retCpuInfoModel.setLoadValue(cpuModelInfo.getLoadValue());
   List<CpuModel> cpus = new ArrayList<CpuInfoModel.CpuModel>();
   if (prevInfoModel == null) {
     prevInfoModel = ObjectCopyHelper.cpoyObject(cpuModelInfo);
     cpuModelInfo = null;
     return prevInfoModel;
   } else {
     for (int index = 0; index < prevInfoModel.getCpuList().size(); index++) {
       // 存放两次CPU采样之间的时间差值
       CpuModel currentCpuModel = cpuModelInfo.getCpuList().get(index);
       CpuModel preCpuModel = prevInfoModel.getCpuList().get(index);
       CpuModel model = saveModel(currentCpuModel, preCpuModel);
       cpus.add(model);
     }
   }
   retCpuInfoModel.setCpuList(cpus);
   // 用当前的CPU模型覆盖上一次采样的CPU模型
   prevInfoModel = ObjectCopyHelper.cpoyObject(cpuModelInfo);
   cpuModelInfo = null;
   return retCpuInfoModel;
 }