use of org.rrd4j.core.Datasource 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();
}
use of org.rrd4j.core.Datasource 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));
}
use of org.rrd4j.core.Datasource 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());
}
Aggregations