Exemplo n.º 1
0
  /**
   * Return a host report based on the current state (similar to <code>gstat -a</code>).
   *
   * <p>Note: The report will not be accurate immediately as the {@link GangliaService} needs to
   * build up a model of the current state of the monitored hosts.
   *
   * @param hosts The hosts for which host reports will be returned.
   * @param reportOn The metrics to be reported for each host. The {@link IHostReport#getMetrics()}
   *     is an ordered map and will reflect the metrics in the order in which they are requested
   *     here.
   * @param comparator The comparator used to order the {@link IHostReport}s (optional).
   * @return The {@link IHostReport}s for each specified host ordered by the given {@link
   *     Comparator}.
   */
  public IHostReport[] getHostReport(
      final String[] hosts, final String[] reportOn, final Comparator<IHostReport> comparator) {

    if (reportOn == null || reportOn.length == 0) throw new IllegalArgumentException();

    //		if (comparator == null)
    //			throw new IllegalArgumentException();

    final IHostReport[] a = new IHostReport[hosts.length];

    for (int i = 0; i < a.length; i++) {

      final String hostName = hosts[i];

      // Note: This map preserves the insert order of the metrics.
      final Map<String, IGangliaMetricMessage> m =
          new LinkedHashMap<String, IGangliaMetricMessage>();

      for (String metricName : reportOn) {

        final TimestampMetricValue tmv = gangliaState.getMetric(hostName, metricName);

        if (tmv == null) {
          // No score for that metric for that host.
          continue;
        }

        final Object value = tmv.getValue();

        if (value == null) {
          // Should never happen.
          continue;
        }

        final IGangliaMetricMessage metricValue =
            metricFactory.newMetricMessage(hostName, tmv.getMetadata(), false /* spoof */, value);

        if (log.isDebugEnabled())
          log.debug(
              "host="
                  + hostName
                  + ", metric="
                  + metricName
                  + ", value="
                  + value
                  + ", record="
                  + metricValue);

        // Mock up a metric record.
        m.put(metricName /* metricName */, metricValue);
      }

      a[i] = new HostReport(hostName, m);
    }

    // Sort
    if (comparator != null) {

      Arrays.sort(a, comparator);
    }

    return a;
  }