Exemple #1
0
  Object execute() throws IOException {
    gdef = new RrdGraphDef();

    // OPTIONS

    // START, END
    String t1 = getOptionValue("s", "start", DEFAULT_START),
        t2 = getOptionValue("e", "end", DEFAULT_END);
    gdef.setTimeSpan(Util.getTimestamps(t1, t2));
    // X-GRID
    parseXGrid(getOptionValue("x", "x-grid"));
    // Y-GRID
    parseYGrid(getOptionValue("y", "y-grid"));
    // ALT-Y-GRID
    gdef.setAltYGrid(getBooleanOption("Y", "alt-y-grid"));
    // NO_MINOR
    gdef.setNoMinorGrid(getBooleanOption(null, "no-minor"));
    // ALT-Y-MRTG
    gdef.setAltYMrtg(getBooleanOption("R", "alt-y-mrtg"));
    // ALT-AUTOSCALE
    gdef.setAltAutoscale(getBooleanOption("A", "alt-autoscale"));
    // ALT-AUTOSCALE-MAX
    gdef.setAltAutoscaleMax(getBooleanOption("M", "alt-autoscale-max"));
    // UNITS-EXPONENT
    String opt = getOptionValue("X", "units-exponent");
    if (opt != null) {
      gdef.setUnitsExponent(parseInt(opt));
    }
    // UNITS-LENGTH
    opt = getOptionValue("L", "units-length");
    if (opt != null) {
      gdef.setUnitsLength(parseInt(opt));
    }
    // VERTICAL LABEL
    opt = getOptionValue("v", "vertical-label");
    if (opt != null) {
      gdef.setVerticalLabel(opt);
    }
    // WIDTH
    opt = getOptionValue("w", "width");
    if (opt != null) {
      gdef.setWidth(parseInt(opt));
    }
    // HEIGHT
    opt = getOptionValue("h", "height");
    if (opt != null) {
      gdef.setHeight(parseInt(opt));
    }
    // INTERLACED
    gdef.setInterlaced(getBooleanOption("i", "interlaced"));
    // IMGINFO
    opt = getOptionValue("f", "imginfo");
    if (opt != null) {
      gdef.setImageInfo(opt);
    }
    // IMGFORMAT
    opt = getOptionValue("a", "imgformat");
    if (opt != null) {
      gdef.setImageFormat(opt);
    }
    // BACKGROUND
    opt = getOptionValue("B", "background");
    if (opt != null) {
      gdef.setBackgroundImage(opt);
    }
    // OVERLAY
    opt = getOptionValue("O", "overlay");
    if (opt != null) {
      gdef.setOverlayImage(opt);
    }
    // UNIT
    opt = getOptionValue("U", "unit");
    if (opt != null) {
      gdef.setUnit(opt);
    }
    // LAZY
    gdef.setLazy(getBooleanOption("z", "lazy"));
    // UPPER-LIMIT
    opt = getOptionValue("u", "upper-limit");
    if (opt != null) {
      gdef.setMaxValue(parseDouble(opt));
    }
    // LOWER-LIMIT
    opt = getOptionValue("l", "lower-limit");
    if (opt != null) {
      gdef.setMinValue(parseDouble(opt));
    }
    // RIGID
    gdef.setRigid(getBooleanOption("r", "rigid"));
    // BASE
    opt = getOptionValue("b", "base");
    if (opt != null) {
      gdef.setBase(parseDouble(opt));
    }
    // LOGARITHMIC
    gdef.setLogarithmic(getBooleanOption("o", "logarithmic"));
    // COLORS
    parseColors(getMultipleOptionValues("c", "color"));
    // NO-LEGEND
    gdef.setNoLegend(getBooleanOption("g", "no-legend"));
    // ONLY_GRAPH
    gdef.setOnlyGraph(getBooleanOption("j", "only-graph"));
    // FORCE-RULES-LEGEND
    gdef.setForceRulesLegend(getBooleanOption("F", "force-rules-legend"));
    // TITLE
    opt = getOptionValue("t", "title");
    if (opt != null) {
      gdef.setTitle(opt);
    }
    // STEP
    opt = getOptionValue("S", "step");
    if (opt != null) {
      gdef.setStep(parseLong(opt));
    }

    // NON-OPTIONS

    String[] words = getRemainingWords();
    // the first word must be a filename
    if (words.length < 2) {
      throw new IllegalArgumentException("Image filename must be specified");
    }
    gdef.setFilename(words[1]);
    // parse remaining words, in no particular order
    for (int i = 2; i < words.length; i++) {
      if (words[i].startsWith("DEF:")) {
        parseDef(words[i]);
      } else if (words[i].startsWith("CDEF:")) {
        parseCDef(words[i]);
      } else if (words[i].startsWith("PRINT:")) {
        parsePrint(words[i]);
      } else if (words[i].startsWith("GPRINT:")) {
        parseGPrint(words[i]);
      } else if (words[i].startsWith("COMMENT:")) {
        parseComment(words[i]);
      } else if (words[i].startsWith("HRULE:")) {
        parseHRule(words[i]);
      } else if (words[i].startsWith("VRULE:")) {
        parseVRule(words[i]);
      } else if (words[i].startsWith("LINE1:")
          || words[i].startsWith("LINE2:")
          || words[i].startsWith("LINE3:")) {
        parseLine(words[i]);
      } else if (words[i].startsWith("AREA:")) {
        parseArea(words[i]);
      } else if (words[i].startsWith("STACK:")) {
        parseStack(words[i]);
      } else {
        throw new IllegalArgumentException("Unexpected GRAPH token encountered: " + words[i]);
      }
    }
    // create diagram finally
    RrdGraphInfo info = new RrdGraph(gdef).getRrdGraphInfo();
    if (info.getFilename().equals(RrdGraphConstants.IN_MEMORY_IMAGE)) {
      println(new String(info.getBytes()));
    } else {
      println(info.getWidth() + "x" + info.getHeight());
      String[] plines = info.getPrintLines();
      for (String pline : plines) {
        println(pline);
      }
      if (info.getImgInfo() != null && info.getImgInfo().length() > 0) {
        println(info.getImgInfo());
      }
    }
    return info;
  }
  @GET
  @Produces({MediaType.APPLICATION_JSON})
  public Response getChartSeries(
      @Context HttpHeaders headers,
      @QueryParam("rrd") String itemName,
      @QueryParam("ds") String consFunction,
      @QueryParam("start") String start,
      @QueryParam("end") String end,
      @QueryParam("res") long resolution) {
    if (logger.isDebugEnabled()) {
      logger.debug("Received GET request at '{}' for rrd '{}'.", uriInfo.getPath(), itemName);
    }
    String responseType = MediaType.APPLICATION_JSON;

    // RRD specific: no equivalent in PersistenceService known
    ConsolFun consilidationFunction = ConsolFun.valueOf(consFunction);

    // read the start/end time as they are provided in the RRD-way, we use
    // the RRD4j to read them
    long[] times = Util.getTimestamps(start, end);
    Date startTime = new Date();
    startTime.setTime(times[0] * 1000L);
    Date endTime = new Date();
    endTime.setTime(times[1] * 1000L);

    if (itemName.endsWith(".rrd")) {
      itemName = itemName.substring(0, itemName.length() - 4);
    }
    String[] parts = itemName.split(":");
    String service = "rrd4j";

    if (parts.length == 2) {
      itemName = parts[1];
      service = parts[0];
    }

    Item item;
    try {
      item = itemRegistry.getItem(itemName);
      logger.debug("item '{}' found ", item);

      // Prefer RRD-Service
      QueryablePersistenceService persistenceService = getPersistenceServices().get(service);
      // Fallback to first persistenceService from list
      if (persistenceService == null) {
        Iterator<Entry<String, QueryablePersistenceService>> pit =
            getPersistenceServices().entrySet().iterator();
        if (pit.hasNext()) {
          persistenceService = pit.next().getValue();
        } else {
          throw new IllegalArgumentException("No Persistence service found.");
        }
      }
      Object data = null;
      if (persistenceService.getId().equals("rrd4j")) {
        data =
            getRrdSeries(
                persistenceService, item, consilidationFunction, startTime, endTime, resolution);
      } else {
        data = getPersistenceSeries(persistenceService, item, startTime, endTime, resolution);
      }
      return Response.ok(data, responseType).build();
    } catch (ItemNotFoundException e1) {
      logger.error("Item '{}' not found error while requesting series data.", itemName);
    }
    return Response.serverError().build();
  }