use of org.jrobin.graph.RrdGraphDef in project opennms by OpenNMS.
the class JRobinRrdStrategyTest method testSampleVDEFPercentile.
@Test
public void testSampleVDEFPercentile() throws Exception {
Double[] vals = { 39.0, 94.0, 95.0, 101.0, 155.0, 262.0, 274.0, 302.0, 319.0, 402.0, 466.0, 468.0, 494.0, 549.0, 550.0, 575.0, 600.0, 615.0, 625.0, 703.0, 729.0, 824.0, 976.0, 1018.0, 1036.0, 1138.0, 1195.0, 1265.0, 1287.0, 1323.0, 1410.0, 1443.0, 1516.0, 1538.0, 1664.0, 1686.0, 1801.0, 1912.0, 1921.0, 1929.0, 1936.0, 1941.0, 1985.0, 2003.0, 2010.0, 2013.0, 2082.0, 2106.0, 2213.0, 2358.0, 2394.0, 2572.0, 2616.0, 2627.0, 2676.0, 2694.0, 2736.0, 2740.0, 2966.0, 3005.0, 3037.0, 3041.0, 3146.0, 3194.0, 3228.0, 3235.0, 3243.0, 3339.0, 3365.0, 3414.0, 3440.0, 3454.0, 3567.0, 3570.0, 3615.0, 3619.0, 3802.0, 3831.0, 3864.0, 4061.0, 4084.0, 4106.0, 4233.0, 4328.0, 4362.0, 4372.0, 4376.0, 4388.0, 4413.0, 4527.0, 4612.0, 4643.0, 4684.0, 4750.0, 4799.0, 4810.0, 4824.0, 4825.0, 4871.0, 4932.0, 5028.0, 5112.0, 5118.0, 5163.0, 5198.0, 5256.0, 5296.0, 5413.0, 5471.0, 5568.0, 5628.0, 5645.0, 5733.0, 5790.0, 5851.0, 5886.0, 5927.0, 5937.0, 6018.0, 6027.0, 6046.0, 6145.0, 6147.0, 6289.0, 6371.0, 6384.0, 6393.0, 6431.0, 6469.0, 6543.0, 6649.0, 6772.0, 6864.0, 6943.0, 7009.0, 7014.0, 7037.0, 7258.0, 7356.0, 7364.0, 7386.0, 7387.0, 7399.0, 7450.0, 7519.0, 7527.0, 7578.0, 7632.0, 7709.0, 7849.0, 7896.0, 7952.0, 7980.0, 8050.0, 8126.0, 8152.0, 8165.0, 8332.0, 8347.0, 8520.0, 8522.0, 8542.0, 8587.0, 8621.0, 8678.0, 8721.0, 8739.0, 8765.0, 8889.0, 8951.0, 8962.0, 9082.0, 9149.0, 9199.0, 9278.0, 9334.0, 9339.0, 9345.0, 9365.0, 9383.0, 9402.0, 9471.0, 9483.0, 9492.0, 9496.0, 9532.0, 9553.0, 9563.0, 9571.0, 9574.0, 100000.0, 120000.0, 150000.0, 200000.0, 500000.0, 1000000.0, 2000000.0, 4000000.0, 8000000.0, 16000000.0 };
File rrdFile = createRrdFile();
RrdDb openedFile = m_strategy.openFile(rrdFile.getAbsolutePath());
// This file's step size is 300
int endTime = (int) (System.currentTimeMillis() / 1000);
endTime -= (endTime % 300);
int startTime = endTime - (200 * 300);
// Got to throw away the first sample
m_strategy.updateFile(openedFile, "huh?", (startTime - 300) + ":" + "0.0");
int sampleTime = startTime;
for (double val : vals) {
m_strategy.updateFile(openedFile, "huh?", sampleTime + ":" + val);
sampleTime += 300;
}
m_strategy.closeFile(openedFile);
String[] command;
RrdGraphDef graphDef;
RrdGraph graph;
RrdGraphInfo info;
String[] printLines;
command = new String[] { "--start=" + (startTime - 300), "--end=" + (endTime + 300), "DEF:baz=" + rrdFile.getAbsolutePath().replace("\\", "\\\\") + ":bar:AVERAGE", "VDEF:avg=baz,AVERAGE", "VDEF:min=baz,MIN", "VDEF:max=baz,MAX", "VDEF:tot=baz,TOTAL", "VDEF:nfp=baz,95,PERCENT", "PRINT:avg:AVERAGE:\"%le\"", "PRINT:min:AVERAGE:\"%le\"", "PRINT:max:AVERAGE:\"%le\"", "PRINT:tot:AVERAGE:\"%le\"", "PRINT:nfp:AVERAGE:\"%le\"" };
graphDef = ((JRobinRrdStrategy) m_strategy).createGraphDef(new File(""), command);
graph = new RrdGraph(graphDef);
assertNotNull("graph object", graph);
info = graph.getRrdGraphInfo();
assertNotNull("graph info object", info);
printLines = info.getPrintLines();
assertNotNull("graph printLines - DEF", printLines);
assertEquals("graph printLines - DEF size", 5, printLines.length);
assertEquals("graph printLines - DEF item 0", "1.649453e+05", printLines[0]);
assertEquals("graph printLines - DEF item 1", "3.900000e+01", printLines[1]);
assertEquals("graph printLines - DEF item 2", "1.600000e+07", printLines[2]);
assertEquals("graph printLines - DEF item 3", "9.896721e+09", printLines[3]);
assertEquals("graph printLines - DEF item 4", "9.574000e+03", printLines[4]);
// Now do it with a CDEF
command = new String[] { "--start=" + (startTime - 300), "--end=" + (endTime + 300), "DEF:baz=" + rrdFile.getAbsolutePath().replace("\\", "\\\\") + ":bar:AVERAGE", "CDEF:bazX1=baz,1,*", "CDEF:bazX1P0=bazX1,0,+", "VDEF:avg=bazX1,AVERAGE", "VDEF:min=bazX1,MIN", "VDEF:max=bazX1,MAX", "VDEF:tot=bazX1,TOTAL", "VDEF:nfp=bazX1,95,PERCENT", "VDEF:nfp2=bazX1P0,95,PERCENT", "PRINT:avg:AVERAGE:\"%le\"", "PRINT:min:AVERAGE:\"%le\"", "PRINT:max:AVERAGE:\"%le\"", "PRINT:tot:AVERAGE:\"%le\"", "PRINT:nfp:AVERAGE:\"%le\"", "PRINT:nfp2:AVERAGE:\"%le\"" };
graphDef = ((JRobinRrdStrategy) m_strategy).createGraphDef(new File(""), command);
graph = new RrdGraph(graphDef);
assertNotNull("graph object", graph);
info = graph.getRrdGraphInfo();
assertNotNull("graph info object", info);
printLines = info.getPrintLines();
assertNotNull("graph printLines - CDEF", printLines);
assertEquals("graph printLines - CDEF size", 6, printLines.length);
assertEquals("graph printLines - CDEF item 0", "1.649453e+05", printLines[0]);
assertEquals("graph printLines - CDEF item 1", "3.900000e+01", printLines[1]);
assertEquals("graph printLines - CDEF item 2", "1.600000e+07", printLines[2]);
assertEquals("graph printLines - CDEF item 3", "9.896721e+09", printLines[3]);
assertEquals("graph printLines - CDEF item 4", "9.574000e+03", printLines[4]);
assertEquals("graph printLines - CDEF item 5", "9.574000e+03", printLines[5]);
}
use of org.jrobin.graph.RrdGraphDef in project opennms by OpenNMS.
the class JRobinRrdStrategyTest method testPrint.
@Test
public void testPrint() throws Exception {
long end = System.currentTimeMillis();
long start = end - (24 * 60 * 60 * 1000);
String[] command = new String[] { "--start=" + start, "--end=" + end, "CDEF:something=1", "PRINT:something:AVERAGE:\"%le\"" };
RrdGraphDef graphDef = ((JRobinRrdStrategy) m_strategy).createGraphDef(new File(""), command);
RrdGraph graph = new RrdGraph(graphDef);
assertNotNull("graph object", graph);
RrdGraphInfo info = graph.getRrdGraphInfo();
assertNotNull("graph info object", info);
String[] printLines = info.getPrintLines();
assertNotNull("graph printLines", printLines);
assertEquals("graph printLines size", 1, printLines.length);
assertEquals("graph printLines item 0", "1.000000e+00", printLines[0]);
double d = Double.parseDouble(printLines[0]);
assertEquals("graph printLines item 0 as a double", 1.0, d, 0.0);
}
use of org.jrobin.graph.RrdGraphDef in project opennms by OpenNMS.
the class JRobinRrdStrategyTest method testCommentWithNewlines.
@Test
public void testCommentWithNewlines() throws Exception {
long end = System.currentTimeMillis();
long start = end - (24 * 60 * 60 * 1000);
String[] command = new String[] { "--start=" + start, "--end=" + end, "COMMENT:foo\\n" };
String[] command2 = new String[] { "--start=" + start, "--end=" + end, "COMMENT:foo\\n", "COMMENT:foo2\\n" };
RrdGraphDef graphDef = ((JRobinRrdStrategy) m_strategy).createGraphDef(new File(""), command);
RrdGraph graph = new RrdGraph(graphDef);
assertNotNull("graph object", graph);
int firstHeight = graph.getRrdGraphInfo().getHeight();
RrdGraphDef graphDef2 = ((JRobinRrdStrategy) m_strategy).createGraphDef(new File(""), command2);
RrdGraph graph2 = new RrdGraph(graphDef2);
assertNotNull("second graph object", graph2);
int secondHeight = graph2.getRrdGraphInfo().getHeight();
assertFalse("first graph height " + firstHeight + " and second graph height " + secondHeight + " should not be equal... there should be another newline in the second one making it taller", firstHeight == secondHeight);
}
use of org.jrobin.graph.RrdGraphDef in project opennms by OpenNMS.
the class JRobinRrdStrategy method createGraphReturnDetails.
/**
* {@inheritDoc}
*
* This constructs a graphDef by parsing the rrdtool style command and using
* the values to create the JRobin graphDef. It does not understand the 'AT
* style' time arguments however. Also there may be some rrdtool parameters
* that it does not understand. These will be ignored. The graphDef will be
* used to construct an RrdGraph and a PNG image will be created. An input
* stream returning the bytes of the PNG image is returned.
*/
@Override
public RrdGraphDetails createGraphReturnDetails(final String command, final File workDir) throws IOException, org.opennms.netmgt.rrd.RrdException {
try {
String[] commandArray = tokenize(command, " \t", false);
RrdGraphDef graphDef = createGraphDef(workDir, commandArray);
graphDef.setSignature("OpenNMS/JRobin");
RrdGraph graph = new RrdGraph(graphDef);
/*
* We use a custom RrdGraphDetails object here instead of the
* DefaultRrdGraphDetails because we won't have an InputStream
* available if no graphing commands were used, e.g.: if we only
* use PRINT or if the user goofs up a graph definition.
*
* We want to throw an RrdException if the caller calls
* RrdGraphDetails.getInputStream and no graphing commands were
* used. If they just call RrdGraphDetails.getPrintLines, though,
* we don't want to throw an exception.
*/
return new JRobinRrdGraphDetails(graph, command);
} catch (Throwable e) {
LOG.error("JRobin: exception occurred creating graph", e);
throw new org.opennms.netmgt.rrd.RrdException("An exception occurred creating the graph: " + e.getMessage(), e);
}
}
use of org.jrobin.graph.RrdGraphDef in project i2p.i2p by i2p.
the class SummaryRenderer method render.
/**
* Single or two-data-source graph.
*
* @param lsnr2 2nd data source to plot on same graph, or null. Not recommended for events.
* @param titleOverride If non-null, overrides the title
* @since 0.9.6 consolidated from StatSummarizer for bw.combined
*/
public void render(OutputStream out, int width, int height, boolean hideLegend, boolean hideGrid, boolean hideTitle, boolean showEvents, int periodCount, int endp, boolean showCredit, SummaryListener lsnr2, String titleOverride) throws IOException {
// prevent NaNs if we are skewed ahead of system time
long end = Math.min(_listener.now(), System.currentTimeMillis()) - 75 * 1000;
long period = _listener.getRate().getPeriod();
if (endp > 0)
end -= period * endp;
if (periodCount <= 0 || periodCount > _listener.getRows())
periodCount = _listener.getRows();
long start = end - (period * periodCount);
// long begin = System.currentTimeMillis();
ImageOutputStream ios = null;
try {
RrdGraphDef def = new RrdGraphDef();
// Override defaults
def.setColor(RrdGraphDef.COLOR_BACK, BACK_COLOR);
def.setColor(RrdGraphDef.COLOR_SHADEA, SHADEA_COLOR);
def.setColor(RrdGraphDef.COLOR_SHADEB, SHADEB_COLOR);
def.setColor(RrdGraphDef.COLOR_GRID, GRID_COLOR);
def.setColor(RrdGraphDef.COLOR_MGRID, MGRID_COLOR);
def.setColor(RrdGraphDef.COLOR_FONT, FONT_COLOR);
def.setColor(RrdGraphDef.COLOR_FRAME, FRAME_COLOR);
def.setFont(RrdGraphDef.FONTTAG_DEFAULT, new Font(DEFAULT_FONT_NAME, Font.PLAIN, 10));
def.setFont(RrdGraphDef.FONTTAG_TITLE, new Font(DEFAULT_FONT_NAME, Font.PLAIN, 10));
def.setFont(RrdGraphDef.FONTTAG_AXIS, new Font("Droid Sans Mono", Font.PLAIN, 10));
def.setFont(RrdGraphDef.FONTTAG_UNIT, new Font(DEFAULT_FONT_NAME, Font.PLAIN, 10));
def.setFont(RrdGraphDef.FONTTAG_LEGEND, new Font("Droid Sans Mono", Font.PLAIN, 10));
// improve text legibility
String lang = Messages.getLanguage(_context);
Font small = def.getSmallFont();
Font large = def.getLargeFont();
if ("ar".equals(lang) || "jp".equals(lang) || ("zh".equals(lang) && !IS_WIN)) {
small = small.deriveFont(small.getSize2D() + 2.0f);
large = large.deriveFont(Font.PLAIN, large.getSize2D() + 3.0f);
} else {
// small = small.deriveFont(small.getSize2D() + 1.0f);
// if specified font family is missing, jrobin will use fallback
small = new Font("Droid Sans Mono", Font.PLAIN, 10);
// large = large.deriveFont(large.getSize2D() + 1.0f);
large = new Font("Droid Sans", Font.PLAIN, 13);
}
def.setSmallFont(small);
def.setLargeFont(large);
def.setTimeSpan(start / 1000, end / 1000);
def.setMinValue(0d);
String name = _listener.getRate().getRateStat().getName();
// if ((name.startsWith("bw.") || name.indexOf("Size") >= 0 || name.indexOf("Bps") >= 0 || name.indexOf("memory") >= 0)
if ((name.indexOf("Size") >= 0 || name.indexOf("memory") >= 0) && !showEvents)
def.setBase(1024);
if (titleOverride != null) {
def.setTitle(titleOverride);
} else if (!hideTitle) {
String title;
String p;
// we want the formatting and translation of formatDuration2(), except not zh, and not the
if (IS_WIN && "zh".equals(Messages.getLanguage(_context)))
p = DataHelper.formatDuration(period);
else
p = DataHelper.formatDuration2(period).replace(" ", " ");
if (showEvents)
title = name + ' ' + _t("events in {0}", p);
else
title = name + ' ' + _t("averaged for {0}", p);
def.setTitle(title);
}
String path = _listener.getData().getPath();
String[] dsNames = _listener.getData().getDsNames();
String plotName;
String descr;
if (showEvents) {
// include the average event count on the plot
plotName = dsNames[1];
descr = _t("Events per period");
} else {
// include the average value
plotName = dsNames[0];
// The descriptions are not tagged in the createRateStat calls
// (there are over 500 of them)
// but the descriptions for the default graphs are tagged in
// Strings.java
descr = _t(_listener.getRate().getRateStat().getDescription());
}
// long started = ((RouterContext)_context).router().getWhenStarted();
// if (started > start && started < end)
// def.vrule(started / 1000, RESTART_BAR_COLOR, _t("Restart"), 4.0f);
def.datasource(plotName, path, plotName, SummaryListener.CF, _listener.getBackendName());
if (descr.length() > 0) {
def.area(plotName, AREA_COLOR, descr + "\\l");
} else {
def.area(plotName, AREA_COLOR);
}
if (!hideLegend) {
def.gprint(plotName, SummaryListener.CF, " " + _t("Avg") + ": %.2f%s");
def.gprint(plotName, "MAX", ' ' + _t("Max") + ": %.2f%S");
def.gprint(plotName, "LAST", ' ' + _t("Now") + ": %.2f%S\\l");
}
String plotName2 = null;
if (lsnr2 != null) {
String[] dsNames2 = lsnr2.getData().getDsNames();
plotName2 = dsNames2[0];
String path2 = lsnr2.getData().getPath();
String descr2 = _t(lsnr2.getRate().getRateStat().getDescription());
def.datasource(plotName2, path2, plotName2, SummaryListener.CF, lsnr2.getBackendName());
def.line(plotName2, LINE_COLOR, descr2 + "\\l", 2);
if (!hideLegend) {
def.gprint(plotName2, SummaryListener.CF, " " + _t("Avg") + ": %.2f%s");
def.gprint(plotName2, "MAX", ' ' + _t("Max") + ": %.2f%S");
def.gprint(plotName2, "LAST", ' ' + _t("Now") + ": %.2f%S\\l");
}
}
if (!hideLegend) {
// '07 Jul 21:09' with month name in the system locale
// TODO: Fix Arabic time display
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM HH:mm");
Map<Long, String> events = ((RouterContext) _context).router().eventLog().getEvents(EventLog.STARTED, start);
for (Map.Entry<Long, String> event : events.entrySet()) {
long started = event.getKey().longValue();
if (started > start && started < end) {
// String legend = _t("Restart") + ' ' + sdf.format(new Date(started)) + " UTC " + event.getValue() + "\\l";
if ("ar".equals(lang)) {
String legend = _t("Restart") + ' ' + sdf.format(new Date(started)) + " - " + event.getValue() + "\\l";
def.vrule(started / 1000, RESTART_BAR_COLOR, legend, 2.0f);
} else {
String legend = _t("Restart") + ' ' + sdf.format(new Date(started)) + " [" + event.getValue() + "]\\l";
def.vrule(started / 1000, RESTART_BAR_COLOR, legend, 2.0f);
}
}
}
def.comment(sdf.format(new Date(start)) + " — " + sdf.format(new Date(end)) + " UTC\\r");
}
if (!showCredit)
def.setShowSignature(false);
/*
// these four lines set up a graph plotting both values and events on the same chart
// (but with the same coordinates, so the values may look pretty skewed)
def.datasource(dsNames[0], path, dsNames[0], "AVERAGE", "MEMORY");
def.datasource(dsNames[1], path, dsNames[1], "AVERAGE", "MEMORY");
def.area(dsNames[0], AREA_COLOR, _listener.getRate().getRateStat().getDescription());
def.line(dsNames[1], LINE_COLOR, "Events per period");
*/
if (hideLegend)
def.setNoLegend(true);
if (hideGrid) {
def.setDrawXGrid(false);
def.setDrawYGrid(false);
}
// System.out.println("rendering: path=" + path + " dsNames[0]=" + dsNames[0] + " dsNames[1]=" + dsNames[1] + " lsnr.getName=" + _listener.getName());
def.setAntiAliasing(false);
// System.out.println("Rendering: \n" + def.exportXmlTemplate());
// System.out.println("*****************\nData: \n" + _listener.getData().dump());
def.setWidth(width);
def.setHeight(height);
def.setImageFormat("PNG");
def.setLazy(true);
RrdGraph graph;
try {
// NPE here if system is missing fonts - see ticket #915
graph = new RrdGraph(def);
} catch (NullPointerException npe) {
_log.error("Error rendering", npe);
StatSummarizer.setDisabled();
throw new IOException("Error rendering - disabling graph generation. Missing font? See http://trac.i2p2.i2p/ticket/915");
}
int totalWidth = graph.getRrdGraphInfo().getWidth();
int totalHeight = graph.getRrdGraphInfo().getHeight();
BufferedImage img = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_USHORT_565_RGB);
Graphics gfx = img.getGraphics();
graph.render(gfx);
ios = new MemoryCacheImageOutputStream(out);
ImageIO.write(img, "png", ios);
// System.out.println("Graph created");
// File t = File.createTempFile("jrobinData", ".xml");
// _listener.getData().dumpXml(new FileOutputStream(t));
// System.out.println("plotted: " + (data != null ? data.length : 0) + " bytes in " + timeToPlot
// ); // + ", data written to " + t.getAbsolutePath());
} catch (RrdException re) {
_log.error("Error rendering", re);
throw new IOException("Error plotting: " + re.getMessage());
} catch (IOException ioe) {
// typically org.mortbay.jetty.EofException extends java.io.EOFException
if (_log.shouldLog(Log.WARN))
_log.warn("Error rendering", ioe);
throw ioe;
} catch (OutOfMemoryError oom) {
_log.error("Error rendering", oom);
throw new IOException("Error plotting: " + oom.getMessage());
} finally {
// this does not close the underlying stream
if (ios != null)
try {
ios.close();
} catch (IOException ioe) {
}
}
}
Aggregations