Exemple #1
0
 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);
   }
 }
Exemple #2
0
  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;
  }