private DatasourcesPopulator(
      T wrapped,
      Probe<?, ?> defProbe,
      ExtractInfo ei,
      Map<String, ? extends Plottable> customData,
      List<DsDesc> allds,
      String name) {
    HostsList hl = defProbe.getHostList();

    if (wrapped instanceof RrdGraphDef) {
      graphDef = (RrdGraphDef) wrapped;
      dp = null;
    } else if (wrapped instanceof DataProcessor) {
      dp = (DataProcessor) wrapped;
      graphDef = null;
    } else {
      throw new RuntimeException();
    }

    // The datasources already found
    Set<String> datasources = new HashSet<String>();

    // The needed extractors
    Map<Probe<?, ?>, Extractor> probeDS = new HashMap<Probe<?, ?>, Extractor>(1);
    probeDS.put(defProbe, defProbe.getMainStore().getExtractor());

    for (DsDesc ds : allds) {
      boolean complete;
      // Not a data source, don't try to add it in datasources
      if (!ds.graphType.datasource()) {
        complete = true;
      }
      // The graph is a percentile
      else if (ds.percentile != null) {
        complete = true;
        if (!datasources.contains(ds.name)) {
          percentile(ds.name, ds.dsName, ds.percentile);
          datasources.add(ds.name);
        }
      }
      // A rpn datasource
      else if (ds.rpn != null) {
        complete = true;
        if (!datasources.contains(ds.name)) {
          datasource(ds.name, ds.rpn);
          datasources.add(ds.name);
        }
      }
      // A legend
      else if (ds.graphType == GraphType.LEGEND) {
        complete = true;
      }
      // Does the datas existe in the provided values
      // It override existing values in the probe
      else if (customData != null && customData.containsKey(ds.dsName)) {
        complete = true;
        if (!datasources.contains(ds.name)) {
          datasource(ds.name, customData.get(ds.dsName));
          datasources.add(ds.name);
          logger.trace(delayedFormatString("custom data found for %s", ds.dsName));
        }
      }
      // Last but common case, datasource refers to a rrd
      // Or they might be on the associated rrd
      else {
        Probe<?, ?> probe = defProbe;
        if (ds.dspath != null) {
          // If the host is not defined, use the current host
          String pathHost = ds.dspath.host;
          if (pathHost == null) {
            pathHost = defProbe.getHost().getName();
          }
          logger.trace(
              delayedFormatString(
                  "External probe path: %s/%s/%s", pathHost, ds.dspath.probe, ds.dsName));
          probe = hl.getProbeByPath(pathHost, ds.dspath.probe);
          if (probe == null) {
            logger.error("Invalide probe: " + pathHost + "/" + ds.dspath.probe);
            continue;
          }
        }
        if (!probe.dsExist(ds.dsName)) {
          logger.error("Invalide datasource " + ds.dsName + ", not found in " + probe);
          continue;
        }
        complete = true;

        // Add the dsName for the probe found
        if (!probeDS.containsKey(probe)) {
          probeDS.put(probe, probe.getMainStore().getExtractor());
        }
        Extractor ex = probeDS.get(probe);
        if (!datasources.contains(ds.name)) {
          ex.addSource(ds.name, ds.dsName);
          datasources.add(ds.name);
        } else {
          logger.error(
              "Datasource '" + ds.dsName + "' defined twice in " + name + ", for found: " + ds);
        }
      }
      if (complete) {
        toDo.add(ds);
      } else {
        logger.debug("Error for " + ds);
        logger.error("No way to plot " + ds.name + " in " + name + " found");
      }
    }

    // Fill the graphdef with extracted data
    for (Extractor x : probeDS.values()) {
      if (graphDef != null) {
        x.fill(graphDef, ei);
      } else {
        x.fill(dp, ei);
      }
      x.release();
    }

    logger.trace(delayedFormatString("Datasource: %s", datasources));
    if (graphDef != null) {
      logger.trace(delayedFormatString("Todo: : %s", toDo));
    }
  }
Exemple #2
0
  /**
   * Fill a GraphDef with values as defined by the graph desc
   *
   * @param graphDef the GraphDef to configure
   * @param defProbe The probe to get values from
   * @param customData some custom data, they override existing values in the associated probe
   */
  public void fillGraphDef(
      RrdGraphDef graphDef, Probe<?, ?> defProbe, Map<String, ? extends Plottable> customData) {
    HostsList hl = defProbe.getHostList();
    List<DsDesc> toDo = new ArrayList<DsDesc>();
    // The datasources already found
    Set<String> datasources = new HashSet<String>();

    for (DsDesc ds : allds) {
      boolean complete = false;
      // not a data source, don't try to add it in datasources
      if (!ds.graphType.datasource()) {
        complete = true;
      }
      // The graph is a percentile
      else if (ds.percentile != null) {
        complete = true;
        graphDef.percentile(ds.name, ds.dsName, ds.percentile);
        datasources.add(ds.name);
      }
      // A rpn datasource
      else if (ds.rpn != null) {
        complete = true;
        if (!datasources.contains(ds.name)) {
          graphDef.datasource(ds.name, ds.rpn);
          datasources.add(ds.name);
        }
      } else if (ds.graphType == GraphType.LEGEND) {
        complete = true;
      }
      // Does the datas existe in the provided values
      // It override existing values in the probe
      else if (customData != null && customData.containsKey(ds.dsName)) {
        complete = true;
        if (!datasources.contains(ds.name)) {
          graphDef.datasource(ds.name, customData.get(ds.dsName));
          datasources.add(ds.name);
          logger.trace(Util.delayedFormatString("custom data found for %s", ds.dsName));
        }
      }
      // Last but common case, datasource refers to a rrd
      // Or they might be on the associated rrd
      else {
        Probe<?, ?> probe = defProbe;
        if (ds.dspath != null) {
          if (logger.isTraceEnabled())
            logger.trace(
                "External probe path: " + ds.dspath.host + "/" + ds.dspath.probe + "/" + ds.dsName);
          probe = hl.getProbeByPath(ds.dspath.host, ds.dspath.probe);
          if (probe == null) {
            logger.error("Invalide probe: " + ds.dspath.host + "/" + ds.dspath.probe);
            continue;
          }
        }
        if (!probe.dsExist(ds.dsName)) {
          logger.error("Invalide datasource " + ds.dsName + ", not found in " + probe);
          continue;
        }

        complete = true;
        if (!datasources.contains(ds.name)) {
          String rrdName = probe.getRrdName();
          graphDef.datasource(ds.name, rrdName, ds.dsName, ds.cf);
          datasources.add(ds.name);
        } else {
          logger.error(
              "Datasource '" + ds.name + "' defined twice in " + name + ", for found: " + ds);
        }
      }
      if (complete) {
        toDo.add(ds);
      } else {
        logger.debug("Error for " + ds);
        logger.error("No way to plot " + ds.name + " in " + name + " found");
      }
    }
    // The title line, only if values block is required
    if (withSummary) {
      graphDef.comment(""); // We simulate the color box
      graphDef.comment(MANYSPACE.substring(0, Math.min(maxLengthLegend, MANYSPACE.length()) + 2));
      graphDef.comment("Current");
      graphDef.comment("  Average");
      graphDef.comment("  Minimum");
      graphDef.comment("  Maximum");
      graphDef.comment("\\l");
    }

    if (logger.isTraceEnabled()) {
      logger.trace("Datasource: " + datasources);
      logger.trace("Todo: " + toDo);
    }

    String shortLegend = withSummary ? " \\g" : null;
    for (DsDesc ds : toDo) {
      ds.graphType.draw(graphDef, ds.name, ds.color, shortLegend);
      if (withSummary && ds.graphType.legend())
        addLegend(graphDef, ds.name, ds.graphType, ds.legend);
    }
  }