Search in sources :

Example 1 with DsType

use of org.rrd4j.DsType in project ddf by codice.

the class RrdMetricsRetriever method getMetricData.

/**
     * Retrieves the RRD stored data for the specified metric over the specified time range.
     *
     * @param rrdFilename the name of the RRD file containing the metric's data
     * @param startTime   start time, in seconds since Unix epoch, to fetch metric's data
     * @param endTime     end time, in seconds since Unix epoch, to fetch metric's data
     * @return domain object containing the metric's sampled data, which consists of the timestamps
     * and their associated values, and the total count of the sampled data
     * @throws IOException
     * @throws MetricsGraphException
     */
public MetricData getMetricData(String rrdFilename, long startTime, long endTime) throws IOException, MetricsGraphException {
    LOGGER.trace("ENTERING: getMetricData");
    // 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.");
    }
    // The step (sample) interval that determines how often RRD collects the metric's data
    long rrdStep = rrdDb.getRrdDef().getStep();
    // Retrieve the RRD file's data source type to determine how (later)
    // to store the metric's data for presentation.
    DsType dataSourceType = rrdDb.getDatasource(0).getType();
    // Fetch the metric's data from the RRD file for the specified time range
    FetchRequest fetchRequest = rrdDb.createFetchRequest(ConsolFun.TOTAL, startTime, endTime);
    FetchData fetchData = fetchRequest.fetchData();
    long[] timestamps = fetchData.getTimestamps();
    double[] values = fetchData.getValues(0);
    // Done retrieving data from the RRD database - close it, otherwise no one else will
    // be able to access it later.
    rrdDb.close();
    // The lists of the metric's timestamps and their associated values that have non-"NaN"
    // values
    List<Long> validTimestamps = new ArrayList<Long>();
    List<Double> validValues = new ArrayList<Double>();
    long totalCount = 0;
    MetricData metricData = new MetricData();
    if (dataSourceType == DsType.COUNTER || dataSourceType == DsType.DERIVE) {
        // Counters are for constantly incrementing data, hence they can
        // have a summation of their totals
        metricData.setHasTotalCount(true);
        for (int i = 0; i < timestamps.length; i++) {
            long timestamp = timestamps[i];
            // have been set to NaN as a placeholder when the RRD file was created)
            if (timestamp >= startTime && timestamp <= endTime && !Double.toString(values[i]).equals("NaN")) {
                // RRD averages the collected samples over the step interval.
                // To "undo" this averaging and get the actual count, need to
                // multiply the sampled data value by the RRD step interval.
                double nonAveragedValue = (double) (values[i] * rrdStep);
                validTimestamps.add(timestamp);
                validValues.add(nonAveragedValue);
                totalCount += (long) nonAveragedValue;
            }
        }
    } else if (dataSourceType == DsType.GAUGE) {
        // Gauges are for data that waxes and wanes, hence no total count
        metricData.setHasTotalCount(false);
        for (int i = 0; i < timestamps.length; i++) {
            long timestamp = timestamps[i];
            // have been set to NaN as a placeholder when the RRD file was created)
            if (timestamp >= startTime && timestamp <= endTime && !Double.toString(values[i]).equals("NaN")) {
                validTimestamps.add(timestamp);
                validValues.add(values[i]);
            }
        }
    }
    metricData.setTimestamps(validTimestamps);
    metricData.setValues(validValues);
    metricData.setTotalCount(totalCount);
    LOGGER.trace("EXITING: getMetricData");
    return metricData;
}
Also used : ArrayList(java.util.ArrayList) FetchData(org.rrd4j.core.FetchData) DsType(org.rrd4j.DsType) FetchRequest(org.rrd4j.core.FetchRequest) MetricsGraphException(ddf.metrics.reporting.internal.MetricsGraphException) RrdDb(org.rrd4j.core.RrdDb)

Example 2 with DsType

use of org.rrd4j.DsType 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 3 with DsType

use of org.rrd4j.DsType in project ddf by codice.

the class RrdJmxCollectorTest method testRrdFileCreationForGaugeDataSource.

@Test
public void testRrdFileCreationForGaugeDataSource() throws Exception {
    // Set sample rate to 1 sec (default is 60 seconds) so that unit test runs quickly
    String mbeanAttributeName = "Uptime";
    String metricName = name.getMethodName();
    int sampleRate = 1;
    createJmxCollector(mbeanAttributeName, metricName, RrdJmxCollector.GAUGE_DATA_SOURCE_TYPE, sampleRate);
    String rrdFilename = jmxCollector.getRrdPath();
    assertThat(rrdFilename, is(TEST_DIR + metricName + RrdJmxCollector.RRD_FILENAME_SUFFIX));
    rrdDb = new RrdDb(rrdFilename);
    assertThat(rrdDb, not(nullValue()));
    assertThat(rrdDb.isClosed(), is(false));
    Header header = rrdDb.getHeader();
    assertThat(header, not(nullValue()));
    assertThat(header.getStep(), is((long) sampleRate));
    assertThat(rrdDb.getDsCount(), is(1));
    Datasource dataSource = rrdDb.getDatasource(dataSourceName);
    assertThat(dataSource, not(nullValue()));
    DsType dataSourceType = dataSource.getType();
    assertThat(dataSourceType, is(DsType.GAUGE));
    assertThat(rrdDb.getArcCount(), is(8));
    Archive archive = rrdDb.getArchive(ConsolFun.MIN, 1);
    assertThat(archive, not(nullValue()));
    assertThat(archive.getRows(), is(60));
    archive = rrdDb.getArchive(ConsolFun.MIN, 15);
    assertThat(archive, not(nullValue()));
    assertThat(archive.getRows(), is(RrdJmxCollector.ONE_YEAR_IN_15_MINUTE_STEPS));
    archive = rrdDb.getArchive(ConsolFun.MAX, 1);
    assertThat(archive, not(nullValue()));
    assertThat(archive.getRows(), is(60));
    archive = rrdDb.getArchive(ConsolFun.MAX, 15);
    assertThat(archive, not(nullValue()));
    assertThat(archive.getRows(), is(RrdJmxCollector.ONE_YEAR_IN_15_MINUTE_STEPS));
    archive = rrdDb.getArchive(ConsolFun.AVERAGE, 1);
    assertThat(archive, not(nullValue()));
    assertThat(archive.getRows(), is(60));
    archive = rrdDb.getArchive(ConsolFun.AVERAGE, 15);
    assertThat(archive, not(nullValue()));
    assertThat(archive.getRows(), is(RrdJmxCollector.ONE_YEAR_IN_15_MINUTE_STEPS));
}
Also used : Datasource(org.rrd4j.core.Datasource) Archive(org.rrd4j.core.Archive) Header(org.rrd4j.core.Header) RrdDb(org.rrd4j.core.RrdDb) DsType(org.rrd4j.DsType) Test(org.junit.Test)

Example 4 with DsType

use of org.rrd4j.DsType in project ddf by codice.

the class RrdJmxCollectorTest method testRrdFileCreationForDeriveDataSource.

@Test
public void testRrdFileCreationForDeriveDataSource() throws Exception {
    // Set sample rate to 1 sec (default is 60 seconds) so that unit test runs quickly
    String mbeanAttributeName = "Uptime";
    String metricName = name.getMethodName();
    int sampleRate = 1;
    createJmxCollector(mbeanAttributeName, metricName, RrdJmxCollector.DERIVE_DATA_SOURCE_TYPE, sampleRate);
    String rrdFilename = jmxCollector.getRrdPath();
    assertThat(rrdFilename, is(TEST_DIR + metricName + RrdJmxCollector.RRD_FILENAME_SUFFIX));
    rrdDb = new RrdDb(rrdFilename);
    assertThat(rrdDb, not(nullValue()));
    assertThat(rrdDb.isClosed(), is(false));
    Header header = rrdDb.getHeader();
    assertThat(header, not(nullValue()));
    assertThat(header.getStep(), is((long) sampleRate));
    assertThat(rrdDb.getDsCount(), is(1));
    Datasource dataSource = rrdDb.getDatasource(dataSourceName);
    assertThat(dataSource, not(nullValue()));
    DsType dataSourceType = dataSource.getType();
    assertThat(dataSourceType, is(DsType.DERIVE));
    assertThat(rrdDb.getArcCount(), is(8));
    Archive archive = rrdDb.getArchive(ConsolFun.AVERAGE, 1);
    assertThat(archive, not(nullValue()));
    assertThat(archive.getRows(), is(60));
    archive = rrdDb.getArchive(ConsolFun.AVERAGE, 15);
    assertThat(archive, not(nullValue()));
    assertThat(archive.getRows(), is(RrdJmxCollector.ONE_YEAR_IN_15_MINUTE_STEPS));
    archive = rrdDb.getArchive(ConsolFun.TOTAL, 1);
    assertThat(archive, not(nullValue()));
    assertThat(archive.getRows(), is(60));
    archive = rrdDb.getArchive(ConsolFun.TOTAL, 15);
    assertThat(archive, not(nullValue()));
    assertThat(archive.getRows(), is(RrdJmxCollector.ONE_YEAR_IN_15_MINUTE_STEPS));
// LOGGER.debug(rrdDb.dump());
}
Also used : Datasource(org.rrd4j.core.Datasource) Archive(org.rrd4j.core.Archive) Header(org.rrd4j.core.Header) RrdDb(org.rrd4j.core.RrdDb) DsType(org.rrd4j.DsType) Test(org.junit.Test)

Example 5 with DsType

use of org.rrd4j.DsType in project ddf by codice.

the class SampleDataGenerator method main.

public static void main(String[] args) {
    if (args.length == 1) {
        try {
            String installLoc = args[0];
            File metricsDir = new File(installLoc, "/data/metrics");
            File[] files = metricsDir.listFiles();
            if (files != null) {
                for (File metricsFile : files) {
                    String metricsFileName = metricsFile.getName();
                    if (!metricsFileName.endsWith(".rrd")) {
                        continue;
                    }
                    RrdDb oldDb = new RrdDb(metricsFile.getAbsolutePath());
                    if (oldDb.getDsCount() > 1) {
                        continue;
                    }
                    DsType dsType = oldDb.getDatasource(0).getType();
                    String newDb = "target/" + metricsFileName;
                    long startTime = new DateTime().minusYears(1).getMillis();
                    int sampleSize = (int) ((new DateTime().getMillis() - startTime) / (60 * 1000));
                    new RrdMetricsRetrieverTest.RrdFileBuilder().rrdFileName(newDb).dsType(dsType).numSamples(sampleSize).numRows(sampleSize).startTime(startTime).build();
                    FileUtils.copyFile(new File(newDb), metricsFile);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    exit(0);
}
Also used : RrdDb(org.rrd4j.core.RrdDb) File(java.io.File) DateTime(org.joda.time.DateTime) DsType(org.rrd4j.DsType)

Aggregations

DsType (org.rrd4j.DsType)5 RrdDb (org.rrd4j.core.RrdDb)5 Datasource (org.rrd4j.core.Datasource)3 MetricsGraphException (ddf.metrics.reporting.internal.MetricsGraphException)2 Test (org.junit.Test)2 Archive (org.rrd4j.core.Archive)2 Header (org.rrd4j.core.Header)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 DateTime (org.joda.time.DateTime)1 FetchData (org.rrd4j.core.FetchData)1 FetchRequest (org.rrd4j.core.FetchRequest)1 RrdGraph (org.rrd4j.graph.RrdGraph)1 RrdGraphDef (org.rrd4j.graph.RrdGraphDef)1