Package org.rrd4j.core

Examples of org.rrd4j.core.RrdDb


    rrdDef.setStartTime(System.currentTimeMillis());
    rrdDef.addDatasource("speed", DsType.COUNTER, heartbeat, Double.NaN, Double.NaN);
    rrdDef.addArchive(ConsolFun.AVERAGE, 0.5, 1, 24);
    rrdDef.addArchive(ConsolFun.AVERAGE, 0.5, 6, 10);
    // RrdBackendFactory factory = RrdBackendFactory.getFactory("MEMORY");
    rrdDb = new RrdDb(rrdDef);
    sample = rrdDb.createSample();
  }
View Full Code Here


    File file = FileTools.newAkteraFile("/var/aktera/jvm-memory/" + name + ".rrd");
    String fileName = file.getAbsolutePath();

    if (file.exists())
    {
      rrdDb = new RrdDb(fileName);
    }
    else
    {
      RrdDef rrdDef = new RrdDef(fileName, startTimestamp - 1, AkteraJvmMemoryManager.DATA_CAPTURE_INTERVAL);

      rrdDef.addDatasource("Init", DsType.GAUGE, max, 0, Double.NaN);
      rrdDef.addDatasource("Used", DsType.GAUGE, max, 0, Double.NaN);
      rrdDef.addDatasource("Committed", DsType.GAUGE, max, 0, Double.NaN);
      rrdDef.addDatasource("Max", DsType.GAUGE, max, 0, Double.NaN);
      rrdDef.addArchive(AVERAGE, 0.5, 1, 1440);
      rrdDef.addArchive(AVERAGE, 0.5, 30, 336);
      rrdDef.addArchive(AVERAGE, 0.5, 60, 744);
      rrdDef.addArchive(AVERAGE, 0.5, 1440, 336);
      rrdDef.addArchive(MIN, 0.5, 1, 1440);
      rrdDef.addArchive(MIN, 0.5, 30, 336);
      rrdDef.addArchive(MIN, 0.5, 60, 744);
      rrdDef.addArchive(MIN, 0.5, 1440, 336);
      rrdDef.addArchive(MAX, 0.5, 1, 1440);
      rrdDef.addArchive(MAX, 0.5, 30, 336);
      rrdDef.addArchive(MAX, 0.5, 60, 744);
      rrdDef.addArchive(MAX, 0.5, 1440, 336);
      rrdDb = new RrdDb(rrdDef);
    }

    probe = rrdDb.createSample();
    rrdDb.close();
  }
View Full Code Here

  public void startMeasurand(String name) throws IOException
  {
    File file = FileTools.newAkteraFile("/var/aktera/jvm-memory/" + name + ".rrd");
    String fileName = file.getAbsolutePath();

    rrdDb = new RrdDb(fileName);
    probe = rrdDb.createSample();
  }
View Full Code Here

   * @{inheritDoc}
   */
  public synchronized void store(final Item item, final String alias) {
    final String name = alias==null ? item.getName() : alias;
    ConsolFun function = getConsolidationFunction(item);
    RrdDb db = getDB(name, function);
    if(db!=null) {
      long now = System.currentTimeMillis()/1000;
      if(function!=ConsolFun.AVERAGE) {
        try {
          // we store the last value again, so that the value change in the database is not interpolated, but
          // happens right at this spot
          if(now - 1 > db.getLastUpdateTime()) {
            // only do it if there is not already a value
            double lastValue = db.getLastDatasourceValue(DATASOURCE_STATE);
            if(!Double.isNaN(lastValue)) {
              Sample sample = db.createSample();
                    sample.setTime(now - 1);
                    sample.setValue(DATASOURCE_STATE, lastValue);
                    sample.update();
                        logger.debug("Stored '{}' with state '{}' in rrd4j database", name, mapToState(lastValue, item.getName()));
            }
          }
        } catch (IOException e) {
          logger.debug("Error re-storing last value: {}", e.getMessage());
        }
      }
      try {
        Sample sample = db.createSample();
              sample.setTime(now);
             
              DecimalType state = (DecimalType) item.getStateAs(DecimalType.class);
              if (state!=null) {
                    double value = state.toBigDecimal().doubleValue();
                    sample.setValue(DATASOURCE_STATE, value);
                    sample.update();
                    logger.debug("Stored '{}' with state '{}' in rrd4j database", name, item.getState());
              }
      } catch (IllegalArgumentException e) {
        if(e.getMessage().contains("at least one second step is required")) {

          // we try to store the value one second later
          TimerTask task = new TimerTask() {
            public void run() {
              store(item, name);
            }
          };
          Timer timer = timers.get(name);
          if(timer!=null) {
            timer.cancel();
            timers.remove(name);
          }
          timer = new Timer();
          timers.put(name, timer);
          timer.schedule(task, 1000);
        } else {
          logger.warn("Could not persist '{}' to rrd4j database: {}", new String[] { name, e.getMessage() });
        }
      } catch (Exception e) {
        logger.warn("Could not persist '{}' to rrd4j database: {}", new String[] { name, e.getMessage() });
      }
            try {
        db.close();
      } catch (IOException e) {
        logger.debug("Error closing rrd4j database: {}", e.getMessage());
      }
    }
  }
View Full Code Here

 
  @Override
  public Iterable<HistoricItem> query(FilterCriteria filter) {
    String itemName = filter.getItemName();
    ConsolFun consolidationFunction = getConsolidationFunction(itemName);
    RrdDb db = getDB(itemName, consolidationFunction);
    if(db!=null) {
      long start = 0L;
      long end = filter.getEndDate()==null ? System.currentTimeMillis()/1000 - 1 : filter.getEndDate().getTime()/1000;

      try {
        if(filter.getBeginDate()==null) {
          // as rrd goes back for years and gets more and more inaccurate, we only support descending order and a single return value
          // if there is no begin date is given - this case is required specifically for the historicState() query, which we
          // want to support
          if(filter.getOrdering()==Ordering.DESCENDING && filter.getPageSize()==1 && filter.getPageNumber()==0) {
            if(filter.getEndDate()==null) {
              // we are asked only for the most recent value!
              double lastValue = db.getLastDatasourceValue(DATASOURCE_STATE);
              if(!Double.isNaN(lastValue)) {
                HistoricItem rrd4jItem = new RRD4jItem(itemName, mapToState(lastValue, itemName), new Date(db.getLastArchiveUpdateTime() * 1000));
                return Collections.singletonList(rrd4jItem);
              } else {
                return Collections.emptyList();
              }
            } else {
              start = end;
            }
          } else {
            throw new UnsupportedOperationException("rrd4j does not allow querys without a begin date, " +
                "unless order is decending and a single value is requested");
          }
        } else {
          start = filter.getBeginDate().getTime()/1000;
        }
        FetchRequest request = db.createFetchRequest(consolidationFunction, start, end, 1);

        List<HistoricItem> items = new ArrayList<HistoricItem>();
        FetchData result = request.fetchData();
        long ts = result.getFirstTimestamp();
        long step = result.getRowCount() > 1 ? result.getStep() : 0;
View Full Code Here

    }
    return Collections.emptyList();
  }

  protected synchronized RrdDb getDB(String alias, ConsolFun function) {
    RrdDb db = null;
        File file = new File(DB_FOLDER + File.separator + alias + ".rrd");
      try {
            if (file.exists()) {
              // recreate the RrdDb instance from the file
              db = new RrdDb(file.getAbsolutePath());
            } else {
              File folder = new File(DB_FOLDER);
              if(!folder.exists()) {
                folder.mkdir();
              }
              // create a new database file
                db = new RrdDb(getRrdDef(function, file));
            }
    } catch (IOException e) {
      logger.error("Could not create rrd4j database file '{}': {}", new String[] { file.getAbsolutePath(), e.getMessage() });
    } catch(RejectedExecutionException e) {
      // this happens if the system is shut down
View Full Code Here

    String responseType = MediaTypeHelper.getResponseMediaType(headers
        .getAcceptableMediaTypes());
   
    if (responseType != null) {
      try {
        RrdDb rrdDb = new RrdDb("./etc/rrd4j/" + rrdName);
        long[] times = Util.getTimestamps(start, end);
        FetchRequest fetchRequest = rrdDb.createFetchRequest(
            ConsolFun.valueOf(ds), times[0], times[1], res);
        FetchData fetchData = fetchRequest.fetchData();
        StringBuilder buffer = new StringBuilder();
        long[] timestamps = fetchData.getTimestamps();
        double[][] values = fetchData.getValues();
        buffer.append("[");
        for (int row = 0; row < fetchData.getRowCount(); row++) {
          // change to microseconds
          buffer.append("[" + (timestamps[row]*1000) + ",");
          buffer.append("[");
          ArrayList<String> data = new ArrayList<String>();
          for (int dsIndex = 0; dsIndex < fetchData.getColumnCount(); dsIndex++) {
            data.add(dsIndex,RrdResource.formatDouble(values[dsIndex][row],"null",true));
          }
          buffer.append(StringUtils.join(data, ","));
          buffer.append("]],");
        }
        buffer.deleteCharAt(buffer.length()-1);
        buffer.append("]");
        rrdDb.close();
        return Response.ok(buffer.toString(),responseType).build();
      } catch (IOException e) {
        logger.error(e.getLocalizedMessage());
      }
      return Response.serverError().build();
View Full Code Here

                    rrdDef.addArchive( ConsolFun.AVERAGE, 0.2, 15000,
                            STEPS_PER_ARCHIVE * 5 );

                    // INSTANTIATE

                    INSTANCE = new RrdDb( rrdDef );

                }
                else
                {
                    INSTANCE = new RrdDb( getDbFilePath() );
                }
            }
            catch ( IOException e )
            {
                throw new RuntimeException(
View Full Code Here

TOP

Related Classes of org.rrd4j.core.RrdDb

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.