Search in sources :

Example 11 with RrdDb

use of org.rrd4j.core.RrdDb in project ddf by codice.

the class RrdJmxCollectorTest method testRrdFileCreationForDeriveDataSource.

@Test
public void testRrdFileCreationForDeriveDataSource() throws Exception {
    // Set sample rate to 1 sec (default is 60 seconds) so that unit test runs quickly
    String mbeanAttributeName = "Uptime";
    String metricName = name.getMethodName();
    int sampleRate = 1;
    createJmxCollector(mbeanAttributeName, metricName, RrdJmxCollector.DERIVE_DATA_SOURCE_TYPE, sampleRate);
    String rrdFilename = jmxCollector.getRrdPath();
    assertThat(rrdFilename, is(TEST_DIR + metricName + RrdJmxCollector.RRD_FILENAME_SUFFIX));
    rrdDb = new RrdDb(rrdFilename);
    assertThat(rrdDb, not(nullValue()));
    assertThat(rrdDb.isClosed(), is(false));
    Header header = rrdDb.getHeader();
    assertThat(header, not(nullValue()));
    assertThat(header.getStep(), is((long) sampleRate));
    assertThat(rrdDb.getDsCount(), is(1));
    Datasource dataSource = rrdDb.getDatasource(dataSourceName);
    assertThat(dataSource, not(nullValue()));
    DsType dataSourceType = dataSource.getType();
    assertThat(dataSourceType, is(DsType.DERIVE));
    assertThat(rrdDb.getArcCount(), is(8));
    Archive archive = rrdDb.getArchive(ConsolFun.AVERAGE, 1);
    assertThat(archive, not(nullValue()));
    assertThat(archive.getRows(), is(60));
    archive = rrdDb.getArchive(ConsolFun.AVERAGE, 15);
    assertThat(archive, not(nullValue()));
    assertThat(archive.getRows(), is(RrdJmxCollector.ONE_YEAR_IN_15_MINUTE_STEPS));
    archive = rrdDb.getArchive(ConsolFun.TOTAL, 1);
    assertThat(archive, not(nullValue()));
    assertThat(archive.getRows(), is(60));
    archive = rrdDb.getArchive(ConsolFun.TOTAL, 15);
    assertThat(archive, not(nullValue()));
    assertThat(archive.getRows(), is(RrdJmxCollector.ONE_YEAR_IN_15_MINUTE_STEPS));
// LOGGER.debug(rrdDb.dump());
}
Also used : Datasource(org.rrd4j.core.Datasource) Archive(org.rrd4j.core.Archive) Header(org.rrd4j.core.Header) RrdDb(org.rrd4j.core.RrdDb) DsType(org.rrd4j.DsType) Test(org.junit.Test)

Example 12 with RrdDb

use of org.rrd4j.core.RrdDb in project ddf by codice.

the class SampleDataGenerator method main.

public static void main(String[] args) {
    if (args.length == 1) {
        try {
            String installLoc = args[0];
            File metricsDir = new File(installLoc, "/data/metrics");
            File[] files = metricsDir.listFiles();
            if (files != null) {
                for (File metricsFile : files) {
                    String metricsFileName = metricsFile.getName();
                    if (!metricsFileName.endsWith(".rrd")) {
                        continue;
                    }
                    RrdDb oldDb = new RrdDb(metricsFile.getAbsolutePath());
                    if (oldDb.getDsCount() > 1) {
                        continue;
                    }
                    DsType dsType = oldDb.getDatasource(0).getType();
                    String newDb = "target/" + metricsFileName;
                    long startTime = new DateTime().minusYears(1).getMillis();
                    int sampleSize = (int) ((new DateTime().getMillis() - startTime) / (60 * 1000));
                    new RrdMetricsRetrieverTest.RrdFileBuilder().rrdFileName(newDb).dsType(dsType).numSamples(sampleSize).numRows(sampleSize).startTime(startTime).build();
                    FileUtils.copyFile(new File(newDb), metricsFile);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    exit(0);
}
Also used : RrdDb(org.rrd4j.core.RrdDb) File(java.io.File) DateTime(org.joda.time.DateTime) DsType(org.rrd4j.DsType)

Example 13 with RrdDb

use of org.rrd4j.core.RrdDb in project scheduling by ow2-proactive.

the class RMRestTest method createRrdDb.

private RrdDb createRrdDb() throws IOException {
    final long start = (System.currentTimeMillis() - 10000) / 1000;
    final long end = System.currentTimeMillis() / 1000;
    RrdDef rrdDef = new RrdDef("testDB", start - 1, 300);
    for (String dataSource : RMRest.dataSources) {
        rrdDef.addDatasource(dataSource, DsType.GAUGE, 600, 0, Double.NaN);
    }
    rrdDef.addArchive(ConsolFun.AVERAGE, 0.5, 1, 150);
    RrdDb rrdDb = new RrdDb(rrdDef, org.rrd4j.core.RrdBackendFactory.getFactory("MEMORY"));
    Sample sample = rrdDb.createSample();
    long time = start;
    while (time <= end + 172800L) {
        sample.setTime(time);
        for (String dataSource : RMRest.dataSources) {
            sample.setValue(dataSource, 1.042);
        }
        sample.update();
        time += new Random().nextDouble() * 300 + 1;
    }
    return rrdDb;
}
Also used : RrdDef(org.rrd4j.core.RrdDef) Random(java.util.Random) Sample(org.rrd4j.core.Sample) RrdDb(org.rrd4j.core.RrdDb) Matchers.anyString(org.mockito.Matchers.anyString)

Example 14 with RrdDb

use of org.rrd4j.core.RrdDb in project scheduling by ow2-proactive.

the class RMRest method getStatHistory.

/**
 * Return the statistic history contained in the RM's RRD database,
 * without redundancy, in a friendly JSON format.
 *
 * The following sources will be queried from the RRD DB :<pre>
 * 	{ "BusyNodesCount",
 *    "FreeNodesCount",
 *    "DownNodesCount",
 *    "AvailableNodesCount",
 *    "AverageActivity" }</pre>
 *
 * @param sessionId a valid session
 * @param range a String of 5 chars, one for each stat history source, indicating the time range to fetch
 *      for each source. Each char can be:<ul>
 *            <li>'a' 1 minute
 *            <li>'m' 10 minutes
 *            <li>'h' 1 hour
 *            <li>'H' 8 hours
 *            <li>'d' 1 day
 *            <li>'w' 1 week
 *            <li>'M' 1 month
 *            <li>'y' 1 year</ul>
 * @return a JSON object containing a key for each source
 * @throws InstanceNotFoundException
 * @throws IntrospectionException
 * @throws ReflectionException
 * @throws IOException
 * @throws MalformedObjectNameException
 * @throws NullPointerException
 * @throws InterruptedException
 * @throws NotConnectedException
 */
@Override
@GET
@GZIP
@Path("stathistory")
@Produces("application/json")
public String getStatHistory(@HeaderParam("sessionid") String sessionId, @QueryParam("range") String range) throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException, MalformedObjectNameException, NullPointerException, InterruptedException, NotConnectedException {
    RMProxyUserInterface rm = checkAccess(sessionId);
    // to make it recognizable by StatHistoryCaching
    if (range.length() > dataSources.length) {
        range = range.substring(0, dataSources.length);
    }
    // complete range if too short
    while (range.length() < dataSources.length) {
        range += 'a';
    }
    StatHistoryCacheEntry cache = StatHistoryCaching.getInstance().getEntry(range);
    // found unexpired cache entry matching the parameters: return it immediately
    if (cache != null) {
        return cache.getValue();
    }
    long l1 = System.currentTimeMillis();
    ObjectName on = new ObjectName(RMJMXBeans.RUNTIMEDATA_MBEAN_NAME);
    AttributeList attrs = rm.getMBeanAttributes(on, new String[] { "StatisticHistory" });
    Attribute attr = (Attribute) attrs.get(0);
    // content of the RRD4J database backing file
    byte[] rrd4j = (byte[]) attr.getValue();
    File rrd4jDb = File.createTempFile("database", "rr4dj");
    rrd4jDb.deleteOnExit();
    try (OutputStream out = new FileOutputStream(rrd4jDb)) {
        out.write(rrd4j);
    }
    // create RRD4J DB, should be identical to the one held by the RM
    RrdDb db = new RrdDb(rrd4jDb.getAbsolutePath(), true);
    long timeEnd = db.getLastUpdateTime();
    // force float separator for JSON parsing
    DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.US);
    otherSymbols.setDecimalSeparator('.');
    // formatting will greatly reduce response size
    DecimalFormat formatter = new DecimalFormat("###.###", otherSymbols);
    // construct the JSON response directly in a String
    StringBuilder result = new StringBuilder();
    result.append("{");
    for (int i = 0; i < dataSources.length; i++) {
        String dataSource = dataSources[i];
        char zone = range.charAt(i);
        long timeStart;
        switch(zone) {
            default:
            case // 1 minute
            'a':
                timeStart = timeEnd - 60;
                break;
            case // 10 minute
            'm':
                timeStart = timeEnd - 60 * 10;
                break;
            case // 1 hours
            'h':
                timeStart = timeEnd - 60 * 60;
                break;
            case // 8 hours
            'H':
                timeStart = timeEnd - 60 * 60 * 8;
                break;
            case // 1 day
            'd':
                timeStart = timeEnd - 60 * 60 * 24;
                break;
            case // 1 week
            'w':
                timeStart = timeEnd - 60 * 60 * 24 * 7;
                break;
            case // 1 month
            'M':
                timeStart = timeEnd - 60 * 60 * 24 * 28;
                break;
            case // 1 year
            'y':
                timeStart = timeEnd - 60 * 60 * 24 * 365;
                break;
        }
        FetchRequest req = db.createFetchRequest(ConsolFun.AVERAGE, timeStart, timeEnd);
        req.setFilter(dataSource);
        FetchData fetchData = req.fetchData();
        result.append("\"").append(dataSource).append("\":[");
        double[] values = fetchData.getValues(dataSource);
        for (int j = 0; j < values.length; j++) {
            if (Double.compare(Double.NaN, values[j]) == 0) {
                result.append("null");
            } else {
                result.append(formatter.format(values[j]));
            }
            if (j < values.length - 1)
                result.append(',');
        }
        result.append(']');
        if (i < dataSources.length - 1)
            result.append(',');
    }
    result.append("}");
    db.close();
    rrd4jDb.delete();
    String ret = result.toString();
    StatHistoryCaching.getInstance().addEntry(range, l1, ret);
    return ret;
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DecimalFormat(java.text.DecimalFormat) FetchData(org.rrd4j.core.FetchData) ObjectName(javax.management.ObjectName) StatHistoryCacheEntry(org.ow2.proactive_grid_cloud_portal.common.StatHistoryCaching.StatHistoryCacheEntry) FileOutputStream(java.io.FileOutputStream) RMProxyUserInterface(org.ow2.proactive.resourcemanager.common.util.RMProxyUserInterface) FetchRequest(org.rrd4j.core.FetchRequest) RrdDb(org.rrd4j.core.RrdDb) File(java.io.File) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 15 with RrdDb

use of org.rrd4j.core.RrdDb in project scheduling by ow2-proactive.

the class SigarProcesses method getAttributesHistory.

@Override
public String getAttributesHistory(String objectName, String[] attrs, String range) throws IOException {
    RrdDb db = new RrdDb(statBaseName, true);
    long timeEnd = db.getLastUpdateTime();
    // force float separator for JSON parsing
    DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.US);
    otherSymbols.setDecimalSeparator('.');
    // formatting will greatly reduce response size
    DecimalFormat formatter = new DecimalFormat("###.###", otherSymbols);
    // construct the JSON response directly in a String
    StringBuilder result = new StringBuilder();
    result.append("{");
    for (int i = 0; i < attrs.length; i++) {
        String dataSource = RRDSigarDataStore.toDataStoreName(attrs[i] + "-" + objectName);
        char zone = range.charAt(0);
        long timeStart;
        switch(zone) {
            default:
            case // 1 minute
            'a':
                timeStart = timeEnd - 60;
                break;
            case // 10 minute
            'm':
                timeStart = timeEnd - 60 * 10;
                break;
            case // 1 hours
            'h':
                timeStart = timeEnd - 60 * 60;
                break;
            case // 8 hours
            'H':
                timeStart = timeEnd - 60 * 60 * 8;
                break;
            case // 1 day
            'd':
                timeStart = timeEnd - 60 * 60 * 24;
                break;
            case // 1 week
            'w':
                timeStart = timeEnd - 60 * 60 * 24 * 7;
                break;
            case // 1 month
            'M':
                timeStart = timeEnd - 60 * 60 * 24 * 28;
                break;
            case // 1 year
            'y':
                timeStart = timeEnd - 60 * 60 * 24 * 365;
                break;
        }
        FetchRequest req = db.createFetchRequest(ConsolFun.AVERAGE, timeStart, timeEnd);
        req.setFilter(dataSource);
        FetchData fetchData = req.fetchData();
        result.append("\"").append(dataSource).append("\":[");
        double[] values = fetchData.getValues(dataSource);
        for (int j = 0; j < values.length - 1; j++) {
            if (Double.compare(Double.NaN, values[j]) == 0) {
                result.append("null");
            } else {
                result.append(formatter.format(values[j]));
            }
            if (j < values.length - 2)
                result.append(',');
        }
        result.append(']');
        if (i < attrs.length - 1)
            result.append(',');
    }
    result.append("}");
    db.close();
    return result.toString();
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) FetchRequest(org.rrd4j.core.FetchRequest) RrdDb(org.rrd4j.core.RrdDb) FetchData(org.rrd4j.core.FetchData)

Aggregations

RrdDb (org.rrd4j.core.RrdDb)18 IOException (java.io.IOException)6 File (java.io.File)5 DsType (org.rrd4j.DsType)5 FetchData (org.rrd4j.core.FetchData)5 FetchRequest (org.rrd4j.core.FetchRequest)5 Test (org.junit.Test)4 ConsolFun (org.rrd4j.ConsolFun)3 Datasource (org.rrd4j.core.Datasource)3 Header (org.rrd4j.core.Header)3 Sample (org.rrd4j.core.Sample)3 MetricsGraphException (ddf.metrics.reporting.internal.MetricsGraphException)2 DecimalFormat (java.text.DecimalFormat)2 DecimalFormatSymbols (java.text.DecimalFormatSymbols)2 ArrayList (java.util.ArrayList)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 ObjectName (javax.management.ObjectName)2 Archive (org.rrd4j.core.Archive)2 RrdDef (org.rrd4j.core.RrdDef)2 Color (java.awt.Color)1