Search in sources :

Example 1 with FetchData

use of org.rrd4j.core.FetchData in project ddf by codice.

the class RrdJmxCollectorTest method collectData.

private void collectData(int numRrdStepIterations) throws Exception {
    String rrdFilename = jmxCollector.getRrdPath();
    rrdDb = new RrdDb(rrdFilename);
    Header header = rrdDb.getHeader();
    // Wait for "n" iterations of RRDB's sample rate, then see if MBean value was collected
    LOGGER.debug("Sleeping for {} seconds", header.getStep() * numRrdStepIterations);
    Thread.sleep((header.getStep() * numRrdStepIterations) * 1000);
    // LOGGER.debug(rrdDb.dump());
    long endTime = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTimeInMillis() / 1000;
    // +1 because the fetch gets data for times inclusively, e.g.,
    // endTime=12345, so startTime=12345-4=12341,
    // then fetch data for timestamps 12341, 12342, 12343, 12344, 12345 (which is 5 values)
    long startTime = endTime - numRrdStepIterations + 1;
    LOGGER.debug("startTime = {}, endTime = {}", startTime, endTime);
    FetchRequest fetchRequest = rrdDb.createFetchRequest(ConsolFun.TOTAL, startTime, endTime);
    FetchData fetchData = fetchRequest.fetchData();
    double[] values = fetchData.getValues(dataSourceName);
    assertThat(values.length, is(numRrdStepIterations));
    logFetchData(fetchData, "TOTAL");
    fetchRequest = rrdDb.createFetchRequest(ConsolFun.AVERAGE, startTime, endTime);
    fetchData = fetchRequest.fetchData();
    values = fetchData.getValues(dataSourceName);
    assertThat(values.length, is(numRrdStepIterations));
    logFetchData(fetchData, "AVERAGE");
    fetchRequest = rrdDb.createFetchRequest(ConsolFun.MIN, startTime, endTime);
    fetchData = fetchRequest.fetchData();
    values = fetchData.getValues(dataSourceName);
    assertThat(values.length, is(numRrdStepIterations));
    logFetchData(fetchData, "MIN");
    fetchRequest = rrdDb.createFetchRequest(ConsolFun.MAX, startTime, endTime);
    fetchData = fetchRequest.fetchData();
    values = fetchData.getValues(dataSourceName);
    assertThat(values.length, is(numRrdStepIterations));
    logFetchData(fetchData, "MAX");
}
Also used : Header(org.rrd4j.core.Header) FetchRequest(org.rrd4j.core.FetchRequest) RrdDb(org.rrd4j.core.RrdDb) FetchData(org.rrd4j.core.FetchData)

Example 2 with FetchData

use of org.rrd4j.core.FetchData in project ddf by codice.

the class RrdMetricsRetriever method dumpData.

private void dumpData(ConsolFun consolFun, String dataType, RrdDb rrdDb, String dsType, long startTime, long endTime) {
    String rrdFilename = rrdDb.getPath();
    LOGGER.trace("***********  START Dump of RRD file:  [{}]  ***************", rrdFilename);
    LOGGER.trace("metricsMaxThreshold = {}", metricsMaxThreshold);
    FetchRequest fetchRequest = rrdDb.createFetchRequest(consolFun, startTime, endTime);
    try {
        FetchData fetchData = fetchRequest.fetchData();
        LOGGER.trace("************  {}: {}  **************", dsType, dataType);
        // in seconds
        int rrdStep = RRD_STEP;
        long[] timestamps = fetchData.getTimestamps();
        double[] values = fetchData.getValues(0);
        double[] adjustedValues = new double[values.length];
        for (int i = 0; i < values.length; i++) {
            double adjustedValue = values[i] * rrdStep;
            adjustedValues[i] = adjustedValue;
            LOGGER.trace(getCalendarTime(timestamps[i]) + ":  " + values[i] + "   (adjusted value = " + adjustedValue + ",   floor = " + Math.floor(adjustedValue) + ",   round = " + Math.round(adjustedValue) + ")");
        }
        LOGGER.trace("adjustedValues.length = {}", adjustedValues.length);
        for (int i = 0; i < adjustedValues.length; i++) {
            if (adjustedValues[i] > metricsMaxThreshold) {
                LOGGER.trace("Value [{}] is an OUTLIER", adjustedValues[i]);
            }
        }
    } catch (IOException e) {
    }
    LOGGER.trace("***********  END Dump of RRD file:  [{}]  ***************", rrdFilename);
}
Also used : FetchRequest(org.rrd4j.core.FetchRequest) IOException(java.io.IOException) FetchData(org.rrd4j.core.FetchData)

Example 3 with FetchData

use of org.rrd4j.core.FetchData 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 4 with FetchData

use of org.rrd4j.core.FetchData in project openhab1-addons by openhab.

the class RRD4jService method query.

@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
    String itemName = filter.getItemName();
    RrdDb db = getDB(itemName);
    if (db != null) {
        ConsolFun consolidationFunction = getConsolidationFunction(db);
        long start = 0L;
        long end = filter.getEndDate() == null ? System.currentTimeMillis() / 1000 : filter.getEndDate().getTime() / 1000;
        try {
            if (filter.getBeginDate() == null) {
                // want to support
                if (filter.getOrdering() == Ordering.DESCENDING && filter.getPageSize() == 1 && filter.getPageNumber() == 0) {
                    if (filter.getEndDate() == null) {
                        // we are asked only for the most recent value!
                        double lastValue = db.getLastDatasourceValue(DATASOURCE_STATE);
                        if (!Double.isNaN(lastValue)) {
                            HistoricItem rrd4jItem = new RRD4jItem(itemName, mapToState(lastValue, itemName), new Date(db.getLastArchiveUpdateTime() * 1000));
                            return Collections.singletonList(rrd4jItem);
                        } else {
                            return Collections.emptyList();
                        }
                    } else {
                        start = end;
                    }
                } else {
                    throw new UnsupportedOperationException("rrd4j does not allow querys without a begin date, " + "unless order is descending and a single value is requested");
                }
            } else {
                start = filter.getBeginDate().getTime() / 1000;
            }
            FetchRequest request = db.createFetchRequest(consolidationFunction, start, end, 1);
            List<HistoricItem> items = new ArrayList<HistoricItem>();
            FetchData result = request.fetchData();
            long ts = result.getFirstTimestamp();
            long step = result.getRowCount() > 1 ? result.getStep() : 0;
            for (double value : result.getValues(DATASOURCE_STATE)) {
                if (!Double.isNaN(value) && (((ts >= start) && (ts <= end)) || (start == end))) {
                    RRD4jItem rrd4jItem = new RRD4jItem(itemName, mapToState(value, itemName), new Date(ts * 1000));
                    items.add(rrd4jItem);
                }
                ts += step;
            }
            return items;
        } catch (IOException e) {
            logger.warn("Could not query rrd4j database for item '{}': {}", itemName, e.getMessage());
        }
    }
    return Collections.emptyList();
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) FetchData(org.rrd4j.core.FetchData) Date(java.util.Date) ConsolFun(org.rrd4j.ConsolFun) FetchRequest(org.rrd4j.core.FetchRequest) RrdDb(org.rrd4j.core.RrdDb) HistoricItem(org.openhab.core.persistence.HistoricItem)

Example 5 with FetchData

use of org.rrd4j.core.FetchData in project ddf by codice.

the class MetricsEndpointTest method createRrdFile.

private void createRrdFile(int dateOffset, String metricName) throws Exception {
    // Create RRD file that Metrics Endpoint will detect
    rrdPath = TEST_DIR + metricName + ".rrd";
    int rrdStep = 60;
    RrdDef def = new RrdDef(rrdPath, rrdStep);
    long startTime = System.currentTimeMillis() / 1000 - dateOffset;
    def.setStartTime(startTime - rrdStep);
    def.addDatasource("data", DsType.COUNTER, 90, 0, Double.NaN);
    def.addArchive(ConsolFun.TOTAL, 0.5, 1, 5);
    rrdDb = RrdDbPool.getInstance().requestRrdDb(def);
    // Add enough samples to get one averaged sample stored into the RRD file
    long endTime = startTime;
    Sample sample = rrdDb.createSample();
    sample.setTime(endTime);
    sample.setValue("data", 100);
    sample.update();
    endTime += rrdStep;
    sample.setTime(endTime);
    sample.setValue("data", 200);
    sample.update();
    endTime += rrdStep;
    sample.setTime(endTime);
    sample.setValue("data", 100);
    sample.update();
    endTime += rrdStep;
    LOGGER.debug(rrdDb.dump());
    FetchRequest fetchRequest = rrdDb.createFetchRequest(ConsolFun.TOTAL, startTime, endTime);
    FetchData fetchData = fetchRequest.fetchData();
    LOGGER.debug(fetchData.dump());
    long[] timestamps = fetchData.getTimestamps();
    double[] values = fetchData.getValues(0);
    for (int i = 0; i < timestamps.length; i++) {
        LOGGER.debug("{}:  {}", getCalendarTime(timestamps[i]), values[i]);
    }
    rrdDb.close();
}
Also used : RrdDef(org.rrd4j.core.RrdDef) Sample(org.rrd4j.core.Sample) FetchRequest(org.rrd4j.core.FetchRequest) FetchData(org.rrd4j.core.FetchData)

Aggregations

FetchData (org.rrd4j.core.FetchData)7 FetchRequest (org.rrd4j.core.FetchRequest)7 RrdDb (org.rrd4j.core.RrdDb)5 IOException (java.io.IOException)2 DecimalFormat (java.text.DecimalFormat)2 DecimalFormatSymbols (java.text.DecimalFormatSymbols)2 ArrayList (java.util.ArrayList)2 MetricsGraphException (ddf.metrics.reporting.internal.MetricsGraphException)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 Date (java.util.Date)1 Attribute (javax.management.Attribute)1 AttributeList (javax.management.AttributeList)1 ObjectName (javax.management.ObjectName)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 GZIP (org.jboss.resteasy.annotations.GZIP)1 HistoricItem (org.openhab.core.persistence.HistoricItem)1