Search in sources :

Example 1 with RrdDb

use of org.jrobin.core.RrdDb in project jmxtrans by jmxtrans.

the class RRDWriter method createOrOpenDatabase.

/**
	 * If the database file doesn't exist, it'll get created, otherwise, it'll
	 * be returned in r/w mode.
	 */
protected RrdDb createOrOpenDatabase() throws Exception {
    RrdDb result;
    if (!this.outputFile.exists()) {
        FileUtils.forceMkdir(this.outputFile.getParentFile());
        RrdDefTemplate t = new RrdDefTemplate(this.templateFile);
        t.setVariable("database", this.outputFile.getCanonicalPath());
        RrdDef def = t.getRrdDef();
        result = new RrdDb(def);
    } else {
        result = new RrdDb(this.outputFile.getCanonicalPath());
    }
    return result;
}
Also used : RrdDefTemplate(org.jrobin.core.RrdDefTemplate) RrdDef(org.jrobin.core.RrdDef) RrdDb(org.jrobin.core.RrdDb)

Example 2 with RrdDb

use of org.jrobin.core.RrdDb in project jmxtrans by jmxtrans.

the class RRDWriter method internalWrite.

@Override
public void internalWrite(Server server, Query query, ImmutableList<Result> results) throws Exception {
    RrdDb db = null;
    try {
        db = createOrOpenDatabase();
        Sample sample = db.createSample();
        List<String> dsNames = Arrays.asList(db.getDsNames());
        // keys from the result values
        for (Result res : results) {
            for (Entry<String, Object> entry : res.getValues().entrySet()) {
                if (dsNames.contains(entry.getKey()) && isNumeric(entry.getValue())) {
                    sample.setValue(entry.getKey(), Double.valueOf(entry.getValue().toString()));
                }
            }
        }
        sample.update();
    } finally {
        if (db != null) {
            db.close();
        }
    }
}
Also used : Sample(org.jrobin.core.Sample) RrdDb(org.jrobin.core.RrdDb) Result(com.googlecode.jmxtrans.model.Result)

Example 3 with RrdDb

use of org.jrobin.core.RrdDb in project opennms by OpenNMS.

the class JmxRrdMigratorOfflineTest method testUpgradeMultiMetric.

/**
     * Test upgrade (multi-metric JRBs, i.e. storeByGroup=true).
     *
     * @throws Exception the exception
     */
@Test
public void testUpgradeMultiMetric() throws Exception {
    FileUtils.copyDirectory(new File("src/test/resources/rrd2"), new File("target/home/rrd"));
    File config = new File("target/home/etc/opennms.properties");
    Properties p = new Properties();
    p.load(new FileReader(config));
    p.setProperty("org.opennms.rrd.storeByGroup", "true");
    p.store(new FileWriter(config), null);
    executeMigrator();
    File jrbFile = new File("target/home/rrd/1/opennms-jvm/java_lang_type_MemoryPool_name_Survivor_Space.jrb");
    Assert.assertTrue(jrbFile.exists());
    RrdDb jrb = new RrdDb(jrbFile, true);
    String[] dataSources = jrb.getDsNames();
    jrb.close();
    Properties dsProp = new Properties();
    dsProp.load(new FileReader("target/home/rrd/1/opennms-jvm/ds.properties"));
    Properties meta = new Properties();
    meta.load(new FileReader("target/home/rrd/1/opennms-jvm/java_lang_type_MemoryPool_name_Survivor_Space.meta"));
    for (String ds : dataSources) {
        Assert.assertFalse(ds.contains("."));
        Assert.assertEquals("java_lang_type_MemoryPool_name_Survivor_Space", dsProp.getProperty(ds));
        Assert.assertEquals(ds, meta.getProperty("JMX_java.lang:type=MemoryPool.name.Survivor Space." + ds));
    }
}
Also used : FileWriter(java.io.FileWriter) FileReader(java.io.FileReader) RrdDb(org.jrobin.core.RrdDb) Properties(java.util.Properties) File(java.io.File) Test(org.junit.Test)

Example 4 with RrdDb

use of org.jrobin.core.RrdDb in project opennms by OpenNMS.

the class AbstractVTDXmlCollectorTest method validateJrb.

/**
     * Validates a JRB.
     * <p>It assumes storeByGroup=true</p>
     *
     * @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()));
    }
}
Also used : Datasource(org.jrobin.core.Datasource) RrdDb(org.jrobin.core.RrdDb)

Example 5 with RrdDb

use of org.jrobin.core.RrdDb in project opennms by OpenNMS.

the class NMS7963IT method testHttpCollection.

/**
     * Test HTTP Data Collection with XPath
     *
     * @throws Exception the exception
     */
@Test
@JUnitHttpServer(port = 10342, https = false, webapps = { @Webapp(context = "/junit", path = "src/test/resources/test-webapp") })
public void testHttpCollection() throws Exception {
    File configFile = new File("src/test/resources/http-datacollection-config.xml");
    XmlDataCollectionConfig config = JaxbUtils.unmarshal(XmlDataCollectionConfig.class, configFile);
    XmlDataCollection collection = config.getDataCollectionByName("NMS-7963");
    RrdRepository repository = createRrdRepository(collection.getXmlRrd());
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("collection", "NMS-7963");
    DefaultXmlCollectionHandler collector = new DefaultXmlCollectionHandler();
    collector.setRrdRepository(repository);
    collector.setServiceName("HTTP");
    CollectionSet collectionSet = XmlCollectorTestUtils.doCollect(m_nodeDao, collector, m_collectionAgent, collection, parameters);
    Assert.assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
    ServiceParameters serviceParams = new ServiceParameters(new HashMap<String, Object>());
    CollectionSetVisitor persister = m_persisterFactory.createGroupPersister(serviceParams, repository, false, false);
    collectionSet.visit(persister);
    RrdDb jrb = new RrdDb(new File(getSnmpRoot(), "1/xml-retrv-wipo-data.jrb"));
    Assert.assertNotNull(jrb);
    Assert.assertEquals(1, jrb.getDsCount());
    Datasource ds = jrb.getDatasource("xml-wipo-paco");
    Assert.assertNotNull(ds);
    Assert.assertEquals(new Double(903), Double.valueOf(ds.getLastValue()));
}
Also used : Datasource(org.jrobin.core.Datasource) XmlDataCollectionConfig(org.opennms.protocols.xml.config.XmlDataCollectionConfig) HashMap(java.util.HashMap) CollectionSetVisitor(org.opennms.netmgt.collection.api.CollectionSetVisitor) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) XmlDataCollection(org.opennms.protocols.xml.config.XmlDataCollection) RrdDb(org.jrobin.core.RrdDb) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) File(java.io.File) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Aggregations

RrdDb (org.jrobin.core.RrdDb)47 Test (org.junit.Test)26 File (java.io.File)20 Datasource (org.jrobin.core.Datasource)8 HashMap (java.util.HashMap)7 Sample (org.jrobin.core.Sample)7 CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)7 RrdDef (org.jrobin.core.RrdDef)6 CollectionSetVisitor (org.opennms.netmgt.collection.api.CollectionSetVisitor)6 ServiceParameters (org.opennms.netmgt.collection.api.ServiceParameters)6 JUnitHttpServer (org.opennms.core.test.http.annotations.JUnitHttpServer)5 RrdRepository (org.opennms.netmgt.rrd.RrdRepository)5 XmlDataCollection (org.opennms.protocols.xml.config.XmlDataCollection)5 XmlDataCollectionConfig (org.opennms.protocols.xml.config.XmlDataCollectionConfig)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)3 FetchData (org.jrobin.core.FetchData)3 RrdException (org.jrobin.core.RrdException)3 FileReader (java.io.FileReader)2 FileWriter (java.io.FileWriter)2