Search in sources :

Example 1 with RrdGraph

use of org.rrd4j.graph.RrdGraph in project ddf by codice.

the class RrdMetricsRetriever method createGraph.

@Override
public byte[] createGraph(String metricName, String rrdFilename, long startTime, long endTime, String verticalAxisLabel, String title) throws IOException, MetricsGraphException {
    // Create RRD DB in read-only mode for the specified RRD file
    RrdDb rrdDb = new RrdDb(rrdFilename, true);
    // we have a problem)
    if (rrdDb.getDsCount() != 1) {
        throw new MetricsGraphException("Only one data source per RRD file is supported - RRD file " + rrdFilename + " has " + rrdDb.getDsCount() + " data sources.");
    }
    // Define attributes of the graph to be created for this metric
    RrdGraphDef graphDef = new RrdGraphDef();
    graphDef.setTimeSpan(startTime, endTime);
    graphDef.setImageFormat("PNG");
    graphDef.setShowSignature(false);
    graphDef.setStep(RRD_STEP);
    graphDef.setVerticalLabel(verticalAxisLabel);
    graphDef.setHeight(500);
    graphDef.setWidth(1000);
    graphDef.setTitle(title);
    // Since we have verified only one datasource in RRD file/RRDb, then know
    // that we can index by zero safely and get the metric's data
    Datasource dataSource = rrdDb.getDatasource(0);
    DsType dataSourceType = dataSource.getType();
    // generated graph by default will show data per rrdStep interval)
    if (dataSourceType == DsType.COUNTER || dataSourceType == DsType.DERIVE) {
        if (LOGGER.isTraceEnabled()) {
            dumpData(ConsolFun.TOTAL, "TOTAL", rrdDb, dataSourceType.name(), startTime, endTime);
        }
        // If we ever needed to adjust the metric's data collected by RRD by the archive step
        // (which is the rrdStep * archiveSampleCount) this is how to do it.
        // FetchRequest fetchRequest = rrdDb.createFetchRequest(ConsolFun.AVERAGE, startTime,
        // endTime);
        // Archive archive = rrdDb.findMatchingArchive(fetchRequest);
        // long archiveStep = archive.getArcStep();
        // LOGGER.debug("archiveStep = " + archiveStep);
        long rrdStep = rrdDb.getRrdDef().getStep();
        LOGGER.debug("rrdStep = {}", rrdStep);
        // Still TBD if we want to graph the AVERAGE data on the same graph
        // graphDef.comment(metricName + "   ");
        // graphDef.datasource("myAverage", rrdFilename, "data", ConsolFun.AVERAGE);
        // graphDef.datasource("realAverage", "myAverage," + rrdStep + ",*");
        // graphDef.line("realAverage", Color.GREEN, "Average", 2);
        // Multiplied by the rrdStep to "undo" the automatic averaging that RRD does
        // when it collects TOTAL data - we want the actual totals for the step, not
        // the average of the totals.
        graphDef.datasource("myTotal", rrdFilename, dataSource.getName(), ConsolFun.TOTAL);
        graphDef.datasource("realTotal", "myTotal," + rrdStep + ",*");
        // If real total exceeds the threshold value used to constrain/filter spike data out,
        // then set total to UNKNOWN, which means this sample will not be graphed. This prevents
        // spike data that is typically 4.2E+09 (graphed as 4.3G) from corrupting the RRD graph.
        graphDef.datasource("constrainedTotal", "realTotal," + metricsMaxThreshold + ",GT,UNKN,realTotal,IF");
        graphDef.line("constrainedTotal", Color.BLUE, convertCamelCase(metricName), 2);
        // Add some spacing between the graph and the summary stats shown beneath the graph
        graphDef.comment("\\s");
        graphDef.comment("\\s");
        graphDef.comment("\\c");
        // Average, Min, and Max over all of the TOTAL data - displayed at bottom of the graph
        graphDef.gprint("constrainedTotal", ConsolFun.AVERAGE, "Average = %.3f%s");
        graphDef.gprint("constrainedTotal", ConsolFun.MIN, "Min = %.3f%s");
        graphDef.gprint("constrainedTotal", ConsolFun.MAX, "Max = %.3f%s");
    } else if (dataSourceType == DsType.GAUGE) {
        if (LOGGER.isTraceEnabled()) {
            dumpData(ConsolFun.AVERAGE, "AVERAGE", rrdDb, dataSourceType.name(), startTime, endTime);
        }
        graphDef.datasource("myAverage", rrdFilename, dataSource.getName(), ConsolFun.AVERAGE);
        graphDef.line("myAverage", Color.RED, convertCamelCase(metricName), 2);
        // Add some spacing between the graph and the summary stats shown beneath the graph
        graphDef.comment("\\s");
        graphDef.comment("\\s");
        graphDef.comment("\\c");
        // Average, Min, and Max over all of the AVERAGE data - displayed at bottom of the graph
        graphDef.gprint("myAverage", ConsolFun.AVERAGE, "Average = %.3f%s");
        graphDef.gprint("myAverage", ConsolFun.MIN, "Min = %.3f%s");
        graphDef.gprint("myAverage", ConsolFun.MAX, "Max = %.3f%s");
    } else {
        rrdDb.close();
        throw new MetricsGraphException("Unsupported data source type " + dataSourceType.name() + " in RRD file " + rrdFilename + ", only DERIVE, COUNTER and GAUGE data source types supported.");
    }
    rrdDb.close();
    // Use "-" as filename so that RRD creates the graph only in memory (no file is
    // created, hence no file locking problems due to race conditions between multiple clients)
    graphDef.setFilename("-");
    RrdGraph graph = new RrdGraph(graphDef);
    return graph.getRrdGraphInfo().getBytes();
}
Also used : RrdGraphDef(org.rrd4j.graph.RrdGraphDef) Datasource(org.rrd4j.core.Datasource) RrdGraph(org.rrd4j.graph.RrdGraph) MetricsGraphException(ddf.metrics.reporting.internal.MetricsGraphException) RrdDb(org.rrd4j.core.RrdDb) DsType(org.rrd4j.DsType)

Example 2 with RrdGraph

use of org.rrd4j.graph.RrdGraph in project openhab1-addons by openhab.

the class RRD4jChartServlet method createChart.

@Override
public BufferedImage createChart(String service, String theme, Date startTime, Date endTime, int height, int width, String items, String groups) throws ItemNotFoundException {
    RrdGraphDef graphDef = new RrdGraphDef();
    long period = (startTime.getTime() - endTime.getTime()) / 1000;
    graphDef.setWidth(width);
    graphDef.setHeight(height);
    graphDef.setAntiAliasing(true);
    graphDef.setImageFormat("PNG");
    graphDef.setStartTime(period);
    graphDef.setTextAntiAliasing(true);
    graphDef.setLargeFont(new Font("SansSerif", Font.PLAIN, 15));
    graphDef.setSmallFont(new Font("SansSerif", Font.PLAIN, 11));
    int seriesCounter = 0;
    // Loop through all the items
    if (items != null) {
        String[] itemNames = items.split(",");
        for (String itemName : itemNames) {
            Item item = itemUIRegistry.getItem(itemName);
            addLine(graphDef, item, seriesCounter++);
        }
    }
    // Loop through all the groups and add each item from each group
    if (groups != null) {
        String[] groupNames = groups.split(",");
        for (String groupName : groupNames) {
            Item item = itemUIRegistry.getItem(groupName);
            if (item instanceof GroupItem) {
                GroupItem groupItem = (GroupItem) item;
                for (Item member : groupItem.getMembers()) {
                    addLine(graphDef, member, seriesCounter++);
                }
            } else {
                throw new ItemNotFoundException("Item '" + item.getName() + "' defined in groups is not a group.");
            }
        }
    }
    // Write the chart as a PNG image
    RrdGraph graph;
    try {
        graph = new RrdGraph(graphDef);
        BufferedImage bi = new BufferedImage(graph.getRrdGraphInfo().getWidth(), graph.getRrdGraphInfo().getHeight(), BufferedImage.TYPE_INT_RGB);
        graph.render(bi.getGraphics());
        return bi;
    } catch (IOException e) {
        logger.error("Error generating graph: {}", e);
    }
    return null;
}
Also used : RrdGraphDef(org.rrd4j.graph.RrdGraphDef) NumberItem(org.openhab.core.library.items.NumberItem) GroupItem(org.openhab.core.items.GroupItem) Item(org.openhab.core.items.Item) RrdGraph(org.rrd4j.graph.RrdGraph) GroupItem(org.openhab.core.items.GroupItem) IOException(java.io.IOException) Font(java.awt.Font) BufferedImage(java.awt.image.BufferedImage) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Aggregations

RrdGraph (org.rrd4j.graph.RrdGraph)2 RrdGraphDef (org.rrd4j.graph.RrdGraphDef)2 MetricsGraphException (ddf.metrics.reporting.internal.MetricsGraphException)1 Font (java.awt.Font)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 GroupItem (org.openhab.core.items.GroupItem)1 Item (org.openhab.core.items.Item)1 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)1 NumberItem (org.openhab.core.library.items.NumberItem)1 DsType (org.rrd4j.DsType)1 Datasource (org.rrd4j.core.Datasource)1 RrdDb (org.rrd4j.core.RrdDb)1