/** * Used to normalize the end date to the last update time but only if it's * close to it * * @param p the probe to check against * @param endDate the desired end date * @return the normalized end date */ public static Date endDate(Probe<?, ?> p, Date endDate) { // Date normalized = endDate; // We normalize the last update time, it can't be used directly long step = p.getStep(); Date lastUpdate = p.getLastUpdate(); // We dont want to graph past the last normalized update time // but only if we are within a step interval if(Math.abs(endDate.getTime() - lastUpdate.getTime()) <= (step * 1000L)) return normalize(lastUpdate, step); // Else rrd4j will manage the normalization itself return endDate; }
/** * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse) */ public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { boolean found = false; res.setContentType("text/plain"); res.addHeader("Cache-Control", "no-cache"); ServletOutputStream out = res.getOutputStream(); String rrdId = req.getParameter("id"); HostsList hl = getHostsList(); GraphNode g = hl.getGraphById(Integer.parseInt(rrdId)); if(g != null) { Probe<?, ?> p = g.getProbe(); if(p != null) { found = true; out.println("Last update:" + p.getLastUpdate()); Map<String, Number> lastValues = p.getLastValues(); for(Map.Entry<String, Number> e: lastValues.entrySet()) { String dsName = e.getKey(); out.println(dsName + ": " + e.getValue()); } } if(!found) { out.println("values not available"); } } }
protected void addlegend(RrdGraphDef graphDef) { Date lastUpdate = node.getProbe().getLastUpdate(); graphDef.comment("\\l"); graphDef.comment("\\l"); graphDef.comment("Last update: " + lastUpdateFormat.get().format(lastUpdate) + "\\L"); String unit = "SI"; if(!getGraphDesc().isSiUnit()) unit = "binary"; graphDef.comment("Unit type: " + unit + "\\r"); graphDef.comment("Period from " + lastUpdateFormat.get().format(start) + " to " + lastUpdateFormat.get().format(end) + "\\L"); graphDef.comment("Source type: " + node.getProbe().getSourceType() + "\\r"); }
ServletOutputStream out = res.getOutputStream(); Date lastupdate = p.getLastUpdate(); long age = (new Date().getTime() - lastupdate.getTime()) / 1000; out.println(dsName + ": " + val); out.println("Last update: " + p.getLastUpdate()); out.println("Last update age (ms): " + (new Date().getTime() - p.getLastUpdate().getTime()));
@Test public void create() throws Exception { @SuppressWarnings("unchecked") Probe<String, Number> p = GenerateProbe.quickProbe(testFolder); p.setPd(Full.getPd()); File rrdFile = new File(p.getMainStore().getPath()); Assert.assertTrue("Failed to create probe " + rrdFile.getAbsolutePath(), p.checkStore()); logger.debug("Created " + p + " stored in " + p.getMainStore().getStoreObject()); long endSec = Full.fill(p); logger.debug("fill time: " + endSec); logger.debug(p.getLastUpdate()); Period pr = Full.getPeriod(p, endSec); Graphics2D g2d = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics(); GraphDesc gd = Full.getGd(); gd.initializeLimits(g2d); GraphNode gn = new GraphNode(p, gd); Graph g = new Graph(gn); g.setPeriod(pr); int h = g.getDimension().height; int w = g.getDimension().width; File outputFile = testFolder.newFile(); OutputStream out = new FileOutputStream(outputFile); g.writePng(out); BufferedImage img = ImageIO.read(outputFile); Assert.assertEquals(h, img.getHeight()); Assert.assertEquals(w, img.getWidth()); Assert.assertTrue(rrdFile.exists()); Assert.assertTrue(rrdFile.length() > 0); }
@Test public void testFill() throws Exception { @SuppressWarnings("unchecked") Probe<?, ?> p = GenerateProbe.quickProbe(testFolder); p.setStep(30); p.getPd().add("test", DsType.GAUGE); Assert.assertTrue("Probe file creation failed", p.checkStore()); Extractor e = p.getMainStore().getExtractor(); e.addSource("test", "test"); String[] dsNames = e.getNames(); Assert.assertEquals("data source test not found", "test", dsNames[0]); long start = p.getLastUpdate().getTime(); for(int i = 1; i <= 30; i++) { JrdsSample s = p.newSample(); long sampletime = i * p.getStep() * 1000 + start; sampletime = (sampletime) - (sampletime % (p.getStep() * 1000)); s.setTime(new Date(sampletime)); s.put("test", i); p.getMainStore().commit(s); } ExtractInfo ei = ExtractInfo.get().make(new Date(start), new Date(start + 30 * p.getStep() * 1000)); DataProcessor dp = p.extract(ei); double[][] values = dp.getValues(); for(int i = 1; i <= 30; i++) { // Check raw values Assert.assertEquals("Wrong values stored", i, values[0][i], 1e-10); long sampletime = i * p.getStep() * 1000 + start; sampletime = (sampletime) - (sampletime % (p.getStep() * 1000)); } }