/** * Validates a JRB. * * <p>It assumes storeByGroup=true * * @param file the JRB file instance * @param dsnames the array of data source names * @param dsvalues the array of data source values * @throws Exception the exception */ public void validateJrb(File file, String[] dsnames, Double[] dsvalues) throws Exception { Assert.assertTrue(file.exists()); RrdDb jrb = new RrdDb(file); Assert.assertEquals(dsnames.length, jrb.getDsCount()); for (int i = 0; i < dsnames.length; i++) { Datasource ds = jrb.getDatasource(dsnames[i]); Assert.assertNotNull(ds); Assert.assertEquals(dsvalues[i], Double.valueOf(ds.getLastValue())); } }
double getLastValue() throws IOException { try { // request RRD database reference from the pool final RrdDb rrdDb = rrdPool.requestRrdDb(rrdFileName); try { return rrdDb.getLastDatasourceValue(getDataSourceName()); } finally { // release RRD database reference rrdPool.release(rrdDb); } } catch (final RrdException e) { throw createIOException(e); } }
static void releaseRrdDbReference(RrdDb rrdDb) throws IOException, RrdException { if (rrdDbPoolUsed) { RrdDbPool.getInstance().release(rrdDb); } else { rrdDb.close(); } }
void addValue(double value) throws IOException { try { // request RRD database reference from the pool final RrdDb rrdDb = rrdPool.requestRrdDb(rrdFileName); synchronized (rrdDb) { try { // create sample with the current timestamp final Sample sample = rrdDb.createSample(); // test pour éviter l'erreur suivante au redéploiement par exemple: // org.jrobin.core.RrdException: // Bad sample timestamp x. Last update time was x, at least one second step is required if (sample.getTime() > rrdDb.getLastUpdateTime()) { // set value for load datasource sample.setValue(getDataSourceName(), value); // update database sample.update(); } } finally { // release RRD database reference rrdPool.release(rrdDb); } } } catch (final FileNotFoundException e) { if (e.getMessage() != null && e.getMessage().endsWith("[non existent]")) { // cf issue 255 LOG.debug( "A JRobin file was deleted and created again: " + new File(rrdFileName).getPath()); resetFile(); addValue(value); } } catch (final RrdException e) { if (e.getMessage() != null && e.getMessage().startsWith("Invalid file header")) { // le fichier RRD a été corrompu, par exemple en tuant le process java au milieu // d'un write, donc on efface le fichier corrompu et on le recrée pour corriger // le problème LOG.debug( "A JRobin file was found corrupted and was reset: " + new File(rrdFileName).getPath()); resetFile(); addValue(value); } throw createIOException(e); } }
static long deleteObsoleteJRobinFiles(String application) throws IOException { final Calendar nowMinusThreeMonthsAndADay = Calendar.getInstance(); nowMinusThreeMonthsAndADay.add(Calendar.DAY_OF_YEAR, -getObsoleteGraphsDays()); nowMinusThreeMonthsAndADay.add(Calendar.DAY_OF_YEAR, -1); final long timestamp = Util.getTimestamp(nowMinusThreeMonthsAndADay); final RrdDbPool rrdPool = getRrdDbPool(); final int counterRequestIdLength = new CounterRequest("", "").getId().length(); long diskUsage = 0; for (final File file : listRrdFiles(application)) { // on ne supprime que les fichiers rrd de requêtes (les autres sont peu nombreux) if (file.getName().length() > counterRequestIdLength && file.lastModified() < nowMinusThreeMonthsAndADay.getTimeInMillis()) { try { final RrdDb rrdDb = rrdPool.requestRrdDb(file.getPath()); final boolean obsolete = rrdDb.getLastUpdateTime() < timestamp; rrdPool.release(rrdDb); boolean deleted = false; if (obsolete) { deleted = file.delete(); } if (!deleted) { diskUsage += file.length(); } } catch (final IOException e) { continue; } catch (final RrdException e) { continue; } } else { diskUsage += file.length(); } } // on retourne true si tous les fichiers .rrd obsolètes ont été supprimés, false sinon return diskUsage; }
@Before public void setUp() throws RrdException, IOException { File file = new File("target/rrd/mo_calls.jrb"); if (file.exists()) { file.delete(); } File file2 = new File("target/rrd/mt_calls.jrb"); if (file2.exists()) { file2.delete(); } new File("target/rrd").mkdirs(); new File("target/reports").mkdirs(); long now = System.currentTimeMillis(); long end = now / MILLIS_PER_DAY * MILLIS_PER_DAY + (MILLIS_PER_HOUR * 4); long start = end - (MILLIS_PER_DAY * 7); m_startDate = new Date(start); System.out.println("startDate: " + m_startDate.getTime() / 1000); m_endDate = new Date(end); System.out.println("endDate: " + m_endDate.getTime() / 1000); RrdDef rrdDef = new RrdDef("target/rrd/mo_calls.jrb", (start / 1000) - 600000, 300); rrdDef.addDatasource("DS:mo_call_attempts:COUNTER:600:0:U"); rrdDef.addDatasource("DS:mo_call_completes:COUNTER:600:0:U"); rrdDef.addDatasource("DS:mo_mins_carried:COUNTER:600:0:U"); rrdDef.addDatasource("DS:mo_calls_active:GAUGE:600:0:U"); rrdDef.addArchive("RRA:AVERAGE:0.5:1:288"); RrdDef rrdDef2 = new RrdDef("target/rrd/mt_calls.jrb", (start / 1000) - 600000, 300); rrdDef2.addDatasource("DS:mt_call_attempts:COUNTER:600:0:U"); rrdDef2.addDatasource("DS:mt_call_completes:COUNTER:600:0:U"); rrdDef2.addDatasource("DS:mt_mins_carried:COUNTER:600:0:U"); rrdDef2.addDatasource("DS:mt_calls_active:GAUGE:600:0:U"); rrdDef2.addArchive("RRA:AVERAGE:0.5:1:288"); RrdDb rrd1 = new RrdDb(rrdDef); RrdDb rrd2 = new RrdDb(rrdDef2); Function bigSine = new Sin(start, 15, -10, MILLIS_PER_DAY); Function smallSine = new Sin(start, 7, 5, MILLIS_PER_DAY); Function moSuccessRate = new Cos(start, .5, .3, MILLIS_PER_DAY); Function mtSuccessRate = new Cos(start, .5, -.2, 2 * MILLIS_PER_DAY); Function moAttempts = new Counter(0, bigSine); Function moCompletes = new Counter(0, new Times(moSuccessRate, bigSine)); Function mtAttempts = new Counter(0, smallSine); Function mtCompletes = new Counter(0, new Times(mtSuccessRate, smallSine)); int count = 0; for (long timestamp = start - 300000; timestamp <= end; timestamp += 300000) { System.out.println("timestamp: " + new Date(timestamp)); Sample sample = rrd1.createSample(timestamp / 1000); double attemptsVal = moAttempts.evaluate(timestamp); double completesVal = moCompletes.evaluate(timestamp); System.out.println("Attempts: " + attemptsVal + " Completes " + completesVal); sample.setValue("mo_call_attempts", attemptsVal); sample.setValue("mo_call_completes", completesVal); sample.setValue("mo_mins_carried", 32 * count); sample.setValue("mo_calls_active", 2); sample.update(); Sample sample2 = rrd2.createSample(timestamp / 1000); sample2.setValue("mt_call_attempts", mtAttempts.evaluate(timestamp)); sample2.setValue("mt_call_completes", mtCompletes.evaluate(timestamp)); sample2.setValue("mt_mins_carried", 16 * count); sample2.setValue("mt_calls_active", 1); sample2.update(); count++; } rrd1.close(); rrd2.close(); }