Beispiel #1
0
  public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> metrics) {
    if (cpuInformation != null) {
      cpuInformation.refresh();

      Cpu cpu = null;
      CpuEntry currentCpu = null;
      CpuInfo cpuInfo = null; // this is probably gonna be used for traits only

      for (MeasurementScheduleRequest request : metrics) {
        String property = request.getName();

        if (property.startsWith("Cpu.")) {
          // Grab the current cpu info from SIGAR, only once for this cpu for all processed
          // schedules
          if (cpu == null) {
            cpu = cpuInformation.getCpu();
          }

          property = property.substring(property.indexOf(".") + 1);
          Long longValue = (Long) ObjectUtil.lookupAttributeProperty(cpu, property);
          // A value of -1 indicates SIGAR does not support the metric on the Agent platform type.
          if (longValue != null && longValue != -1) {
            report.addData(new MeasurementDataNumeric(request, longValue.doubleValue()));
          }
        } else if (property.startsWith("CpuPerc.")) {

          /*
           * ! we no longer use the SIGAR CpuPerc object to report cpu percentage metrics. See
           * ! RHQ-245 for an explanation.  We now calculate our own percentages using
           * ! the current raw cpu info from Sigar and the previous cpu numbers, cached per metric.
           * ! This allows us to avoid the problem in RHQ-245 while handling perCpu-perMetric schedule
           * ! granularity.
           */

          // Grab the current cpu info from SIGAR, only once for this cpu for all processed
          // schedules
          if (null == cpu) {
            cpu = cpuInformation.getCpu();
          }
          // Create a Cpu cacheEntry only once for this cpu for all processed schedules
          if (null == currentCpu) {
            currentCpu = new CpuEntry(cpu);
          }

          // Get the previous cpu numbers to be used for this metric and update the cache for the
          // next go.
          CpuEntry previousCpu = cpuCache.put(property, currentCpu);
          previousCpu = (null == previousCpu) ? startCpuEntry : previousCpu;

          // if for some reason the delta time is excessive then toss
          // the metric, since it depends on a reasonable interval between
          // prev and curr. This can happen due to avail down or a newly
          // activated metric. Allow up to twice the metric interval.
          Number num = null;
          long deltaTime = currentCpu.getTimestamp() - previousCpu.getTimestamp();

          if (deltaTime <= (2 * request.getInterval())) {
            // Use the same calculation that SIGAR uses to generate the percentages. The difference
            // is that
            // we use a safe "previous" cpu record.
            num = getPercentage(previousCpu.getCpu(), cpu, property);
          }

          // Uncomment to see details about the calculations.
          // System.out.println("\nCPU-" + cpuInformation.getCpuIndex() + " Interval="
          //    + ((currentCpu.getTimestamp() - previousCpu.getTimestamp()) / 1000) + " " + property
          // + "="
          //    + num + "\n   Prev=" + previousCpu + "\n   Curr=" + currentCpu);

          if (num != null) {
            report.addData(new MeasurementDataNumeric(request, num.doubleValue()));
          }
        } else if (property.startsWith("CpuInfo.")) {
          if (cpuInfo == null) {
            cpuInfo = cpuInformation.getCpuInfo();
          }

          property = property.substring(property.indexOf(".") + 1);
          Number num = ((Number) ObjectUtil.lookupAttributeProperty(cpuInfo, property));
          if (num != null) {
            report.addData(new MeasurementDataNumeric(request, num.doubleValue()));
          }
        } else if (property.startsWith("CpuTrait.")) {
          if (cpuInfo == null) {
            cpuInfo = cpuInformation.getCpuInfo();
          }
          property = property.substring(property.indexOf(".") + 1);
          Object o = ObjectUtil.lookupAttributeProperty(cpuInfo, property);
          if (o != null) {
            String res;
            if ("model".equals(property) || "vendor".equals(property)) {
              res = (String) o;
            } else {
              res = String.valueOf(o);
            }
            report.addData(new MeasurementDataTrait(request, res));
          }
        }
      }
    }
    return;
  }