package org.opennms.netmgt.rrd; import java.io.ByteArrayInputStream; import java.io.File; import java.util.ArrayList; import java.util.List; import org.opennms.netmgt.config.DataCollectionConfigFactory; import org.opennms.netmgt.model.RrdRepository; /* * To use this example you must run it twice. First run using "jni" as parameter, and then * use "jrobin". You will get two files: /tmp/temp.rrd and /tmp/temp.jrb * * For jni you can see data executing "rrdtool dump /temp/temp.rrd" * For jrobin use "java -jar inspector-1.5.4.jar /tmp/temp.jrb" * * If you execute this two tests at, for example, 3:52, you will see data on RRD from 3:55. * This because rrd was created ad 3:52 and update must be on the future. I use multiples * of step as update timestamp to avoid normalization. * * The problem is that JROBIN data starts at 4:00 (for this example) instead of 3:55 like * jni file. So JNI is working fine with updates and JROBIN don't. * */ public class RrdTests { public static final String RRD_JROBIN = "org.opennms.rrd.strategyClass=org.opennms.netmgt.rrd.jrobin.JRobinRrdStrategy"; public static final String RRD_JNI = "org.opennms.rrd.strategyClass=org.opennms.netmgt.rrd.rrdtool.JniRrdStrategy"; private static String RRD_OWNER = "agalue"; private static String RRD_DIR = "/tmp"; private static String RRD_NAME = "temp"; public static File createRRD(String strategy) { try { // Initialize Factories RrdConfig.loadProperties(new ByteArrayInputStream(strategy.getBytes())); RrdUtils.initialize(); DataCollectionConfigFactory.init(); // Get RRD Repository RrdRepository r = DataCollectionConfigFactory.getInstance().getRrdRepository("default"); // Create RRD DataSources List ds = new ArrayList(); ds.add(new RrdDataSource("v1", "GAUGE", r.getHeartBeat(), "0", "U")); ds.add(new RrdDataSource("v2", "GAUGE", r.getHeartBeat(), "0", "U")); ds.add(new RrdDataSource("v3", "GAUGE", r.getHeartBeat(), "0", "U")); // Create RRD File File rrd = new File(RRD_DIR, RRD_NAME + RrdUtils.getExtension()); rrd.delete(); RrdUtils.createRRD(RRD_OWNER, RRD_DIR, RRD_NAME, r.getStep(), ds, r.getRraList()); // Put Data on RRD int samples = 20; int step = r.getStep() * 1000; long current = System.currentTimeMillis(); // Update will begin one step on the future. long time = current - current % step + step; for (int i = 0; i < samples; i++) { String v1 = Integer.toString(i+1); String v2 = Integer.toString((i+1)*2); String v3 = Integer.toString((i+1)*5); RrdUtils.updateRRD(RRD_OWNER, RRD_DIR, RRD_NAME, time, v1+":"+v2+":"+v3); time += step; } return rrd; } catch (Exception e) { System.err.println(e); return null; } } public static void main(String[] args) { if (args.length == 0) { System.out.println("Usage: RrdTests jni|jrobin"); System.exit(0); } String strategy = null; if (args[0].toLowerCase().equals("jni")) { strategy = RRD_JNI; } else if (args[0].toLowerCase().equals("jrobin")) { strategy = RRD_JROBIN; } else { System.err.println("ERROR: Bad argument. Use jrobin or jni"); } System.setProperty("opennms.home", "/opt/opennms"); File rrd = createRRD(strategy); if (rrd != null) { System.out.println(rrd + " was created successfully using " + strategy + " strategy!"); } else { System.err.println("Error creating RRD using " + strategy + " strategy"); } } }