Search in sources :

Example 6 with RrdGraphDef

use of org.jrobin.graph.RrdGraphDef in project opennms by OpenNMS.

the class JRobinRrdStrategyTest method testGprintWithNewlines.

@Test
public void testGprintWithNewlines() throws Exception {
    long end = System.currentTimeMillis();
    long start = end - (24 * 60 * 60 * 1000);
    String[] command = new String[] { "--start=" + start, "--end=" + end, "CDEF:a=1", "GPRINT:a:AVERAGE:\"%8.2lf\\n\"" };
    String[] command2 = new String[] { "--start=" + start, "--end=" + end, "CDEF:a=1", "CDEF:b=1", "GPRINT:a:AVERAGE:\"%8.2lf\\n\"", "GPRINT:b:AVERAGE:\"%8.2lf\\n\"" };
    RrdGraphDef graphDef = ((JRobinRrdStrategy) m_strategy).createGraphDef(new File(""), command);
    RrdGraph graph = new RrdGraph(graphDef);
    assertNotNull("graph object", graph);
    int firstHeight = graph.getRrdGraphInfo().getHeight();
    RrdGraphDef graphDef2 = ((JRobinRrdStrategy) m_strategy).createGraphDef(new File(""), command2);
    RrdGraph graph2 = new RrdGraph(graphDef2);
    assertNotNull("second graph object", graph2);
    int secondHeight = graph2.getRrdGraphInfo().getHeight();
    assertFalse("first graph height " + firstHeight + " and second graph height " + secondHeight + " should not be equal... there should be another line with a newline in the second one making it taller", firstHeight == secondHeight);
}
Also used : RrdGraphDef(org.jrobin.graph.RrdGraphDef) RrdGraph(org.jrobin.graph.RrdGraph) File(java.io.File) Test(org.junit.Test)

Example 7 with RrdGraphDef

use of org.jrobin.graph.RrdGraphDef in project opennms by OpenNMS.

the class JRobinRrdStrategy method createGraphDef.

/**
 * <p>createGraphDef</p>
 *
 * @param workDir a {@link java.io.File} object.
 * @param commandArray an array of {@link java.lang.String} objects.
 * @return a {@link org.jrobin.graph.RrdGraphDef} object.
 * @throws org.jrobin.core.RrdException if any.
 */
protected RrdGraphDef createGraphDef(final File workDir, final String[] inputArray) throws RrdException {
    RrdGraphDef graphDef = new RrdGraphDef();
    graphDef.setImageFormat("PNG");
    long start = 0;
    long end = 0;
    int height = 100;
    int width = 400;
    double lowerLimit = Double.NaN;
    double upperLimit = Double.NaN;
    boolean rigid = false;
    Map<String, List<String>> defs = new LinkedHashMap<String, List<String>>();
    // Map<String,List<String>> cdefs = new HashMap<String,List<String>>();
    final String[] commandArray;
    if (inputArray[0].contains("rrdtool") && inputArray[1].equals("graph") && inputArray[2].equals("-")) {
        commandArray = Arrays.copyOfRange(inputArray, 3, inputArray.length);
    } else {
        commandArray = inputArray;
    }
    LOG.debug("command array = {}", Arrays.toString(commandArray));
    for (int i = 0; i < commandArray.length; i++) {
        String arg = commandArray[i];
        if (arg.startsWith("--start=")) {
            start = Long.parseLong(arg.substring("--start=".length()));
            LOG.debug("JRobin start time: {}", start);
        } else if (arg.equals("--start")) {
            if (i + 1 < commandArray.length) {
                start = Long.parseLong(commandArray[++i]);
                LOG.debug("JRobin start time: {}", start);
            } else {
                throw new IllegalArgumentException("--start must be followed by a start time");
            }
        } else if (arg.startsWith("--end=")) {
            end = Long.parseLong(arg.substring("--end=".length()));
            LOG.debug("JRobin end time: {}", end);
        } else if (arg.equals("--end")) {
            if (i + 1 < commandArray.length) {
                end = Long.parseLong(commandArray[++i]);
                LOG.debug("JRobin end time: {}", end);
            } else {
                throw new IllegalArgumentException("--end must be followed by an end time");
            }
        } else if (arg.startsWith("--title=")) {
            String[] title = tokenize(arg, "=", true);
            graphDef.setTitle(title[1]);
        } else if (arg.equals("--title")) {
            if (i + 1 < commandArray.length) {
                graphDef.setTitle(commandArray[++i]);
            } else {
                throw new IllegalArgumentException("--title must be followed by a title");
            }
        } else if (arg.startsWith("--color=")) {
            String[] color = tokenize(arg, "=", true);
            parseGraphColor(graphDef, color[1]);
        } else if (arg.equals("--color") || arg.equals("-c")) {
            if (i + 1 < commandArray.length) {
                parseGraphColor(graphDef, commandArray[++i]);
            } else {
                throw new IllegalArgumentException("--color must be followed by a color");
            }
        } else if (arg.startsWith("--vertical-label=")) {
            String[] label = tokenize(arg, "=", true);
            graphDef.setVerticalLabel(label[1]);
        } else if (arg.equals("--vertical-label")) {
            if (i + 1 < commandArray.length) {
                graphDef.setVerticalLabel(commandArray[++i]);
            } else {
                throw new IllegalArgumentException("--vertical-label must be followed by a label");
            }
        } else if (arg.startsWith("--height=")) {
            String[] argParm = tokenize(arg, "=", true);
            height = Integer.parseInt(argParm[1]);
            LOG.debug("JRobin height: {}", height);
        } else if (arg.equals("--height")) {
            if (i + 1 < commandArray.length) {
                height = Integer.parseInt(commandArray[++i]);
                LOG.debug("JRobin height: {}", height);
            } else {
                throw new IllegalArgumentException("--height must be followed by a number");
            }
        } else if (arg.startsWith("--width=")) {
            String[] argParm = tokenize(arg, "=", true);
            width = Integer.parseInt(argParm[1]);
            LOG.debug("JRobin width: {}", width);
        } else if (arg.equals("--width")) {
            if (i + 1 < commandArray.length) {
                width = Integer.parseInt(commandArray[++i]);
                LOG.debug("JRobin width: {}", width);
            } else {
                throw new IllegalArgumentException("--width must be followed by a number");
            }
        } else if (arg.startsWith("--units-exponent=")) {
            String[] argParm = tokenize(arg, "=", true);
            int exponent = Integer.parseInt(argParm[1]);
            LOG.debug("JRobin units exponent: {}", exponent);
            graphDef.setUnitsExponent(exponent);
        } else if (arg.equals("--units-exponent")) {
            if (i + 1 < commandArray.length) {
                int exponent = Integer.parseInt(commandArray[++i]);
                LOG.debug("JRobin units exponent: {}", exponent);
                graphDef.setUnitsExponent(exponent);
            } else {
                throw new IllegalArgumentException("--units-exponent must be followed by a number");
            }
        } else if (arg.startsWith("--lower-limit=")) {
            String[] argParm = tokenize(arg, "=", true);
            lowerLimit = Double.parseDouble(argParm[1]);
            LOG.debug("JRobin lower limit: {}", lowerLimit);
        } else if (arg.equals("--lower-limit")) {
            if (i + 1 < commandArray.length) {
                lowerLimit = Double.parseDouble(commandArray[++i]);
                LOG.debug("JRobin lower limit: {}", lowerLimit);
            } else {
                throw new IllegalArgumentException("--lower-limit must be followed by a number");
            }
        } else if (arg.startsWith("--upper-limit=")) {
            String[] argParm = tokenize(arg, "=", true);
            upperLimit = Double.parseDouble(argParm[1]);
            LOG.debug("JRobin upper limit: {}", upperLimit);
        } else if (arg.equals("--upper-limit")) {
            if (i + 1 < commandArray.length) {
                upperLimit = Double.parseDouble(commandArray[++i]);
                LOG.debug("JRobin upper limit: {}", upperLimit);
            } else {
                throw new IllegalArgumentException("--upper-limit must be followed by a number");
            }
        } else if (arg.startsWith("--base=")) {
            String[] argParm = tokenize(arg, "=", true);
            graphDef.setBase(Double.parseDouble(argParm[1]));
        } else if (arg.equals("--base")) {
            if (i + 1 < commandArray.length) {
                graphDef.setBase(Double.parseDouble(commandArray[++i]));
            } else {
                throw new IllegalArgumentException("--base must be followed by a number");
            }
        } else if (arg.startsWith("--font=")) {
            String[] argParm = tokenize(arg, "=", true);
            processRrdFontArgument(graphDef, argParm[1]);
        } else if (arg.equals("--font")) {
            if (i + 1 < commandArray.length) {
                processRrdFontArgument(graphDef, commandArray[++i]);
            } else {
                throw new IllegalArgumentException("--font must be followed by an argument");
            }
        } else if (arg.startsWith("--imgformat=")) {
            String[] argParm = tokenize(arg, "=", true);
            graphDef.setImageFormat(argParm[1]);
        } else if (arg.equals("--imgformat")) {
            if (i + 1 < commandArray.length) {
                graphDef.setImageFormat(commandArray[++i]);
            } else {
                throw new IllegalArgumentException("--imgformat must be followed by an argument");
            }
        } else if (arg.equals("--rigid")) {
            rigid = true;
        } else if (arg.startsWith("DEF:")) {
            String definition = arg.substring("DEF:".length());
            String[] def = splitDef(definition);
            String[] ds = def[0].split("=");
            // LOG.debug("ds = {}", Arrays.toString(ds));
            // Removing double quotes because of NMS-6331 and changes on RrdFileConstants
            final String replaced = ds[1].replaceAll("\\\\(.)", "$1").replaceAll("\"", "");
            // LOG.debug("replaced = {}", replaced);
            final File dsFile;
            File rawPathFile = new File(replaced);
            if (rawPathFile.isAbsolute()) {
                dsFile = rawPathFile;
            } else {
                dsFile = new File(workDir, replaced);
            }
            // LOG.debug("dsFile = {}, ds[1] = {}", dsFile, ds[1]);
            final String absolutePath = (File.separatorChar == '\\') ? dsFile.getAbsolutePath().replace("\\", "\\\\") : dsFile.getAbsolutePath();
            // LOG.debug("absolutePath = {}", absolutePath);
            graphDef.datasource(ds[0], absolutePath, def[1], def[2]);
            List<String> defBits = new ArrayList<String>();
            defBits.add(absolutePath);
            defBits.add(def[1]);
            defBits.add(def[2]);
            defs.put(ds[0], defBits);
        } else if (arg.startsWith("CDEF:")) {
            String definition = arg.substring("CDEF:".length());
            String[] cdef = tokenize(definition, "=", true);
            graphDef.datasource(cdef[0], cdef[1]);
            List<String> cdefBits = new ArrayList<String>();
            cdefBits.add(cdef[1]);
            defs.put(cdef[0], cdefBits);
        } else if (arg.startsWith("VDEF:")) {
            String definition = arg.substring("VDEF:".length());
            String[] vdef = tokenize(definition, "=", true);
            String[] expressionTokens = tokenize(vdef[1], ",", false);
            addVdefDs(graphDef, vdef[0], expressionTokens, start, end, defs);
        } else if (arg.startsWith("LINE1:")) {
            String definition = arg.substring("LINE1:".length());
            String[] line1 = tokenize(definition, ":", true);
            String[] color = tokenize(line1[0], "#", true);
            graphDef.line(color[0], getColorOrInvisible(color, 1), (line1.length > 1 ? line1[1] : ""));
        } else if (arg.startsWith("LINE2:")) {
            String definition = arg.substring("LINE2:".length());
            String[] line2 = tokenize(definition, ":", true);
            String[] color = tokenize(line2[0], "#", true);
            graphDef.line(color[0], getColorOrInvisible(color, 1), (line2.length > 1 ? line2[1] : ""), 2);
        } else if (arg.startsWith("LINE3:")) {
            String definition = arg.substring("LINE3:".length());
            String[] line3 = tokenize(definition, ":", true);
            String[] color = tokenize(line3[0], "#", true);
            graphDef.line(color[0], getColorOrInvisible(color, 1), (line3.length > 1 ? line3[1] : ""), 3);
        } else if (arg.startsWith("GPRINT:")) {
            String definition = arg.substring("GPRINT:".length());
            String[] gprint = tokenize(definition, ":", true);
            String format = gprint[2];
            // format = format.replaceAll("%(\\d*\\.\\d*)lf", "@$1");
            // format = format.replaceAll("%s", "@s");
            // format = format.replaceAll("%%", "%");
            // LOG.debug("gprint: oldformat = {} newformat = {}", gprint[2], format);
            format = format.replaceAll("\\n", "\\\\l");
            graphDef.gprint(gprint[0], gprint[1], format);
        } else if (arg.startsWith("PRINT:")) {
            String definition = arg.substring("PRINT:".length());
            String[] print = tokenize(definition, ":", true);
            String format = print[2];
            // format = format.replaceAll("%(\\d*\\.\\d*)lf", "@$1");
            // format = format.replaceAll("%s", "@s");
            // format = format.replaceAll("%%", "%");
            // LOG.debug("gprint: oldformat = {} newformat = {}", print[2], format);
            format = format.replaceAll("\\n", "\\\\l");
            graphDef.print(print[0], print[1], format);
        } else if (arg.startsWith("COMMENT:")) {
            String[] comments = tokenize(arg, ":", true);
            String format = comments[1].replaceAll("\\n", "\\\\l");
            graphDef.comment(format);
        } else if (arg.startsWith("AREA:")) {
            String definition = arg.substring("AREA:".length());
            String[] area = tokenize(definition, ":", true);
            String[] color = tokenize(area[0], "#", true);
            if (area.length > 1) {
                graphDef.area(color[0], getColorOrInvisible(color, 1), area[1]);
            } else {
                graphDef.area(color[0], getColorOrInvisible(color, 1));
            }
        } else if (arg.startsWith("STACK:")) {
            String definition = arg.substring("STACK:".length());
            String[] stack = tokenize(definition, ":", true);
            String[] color = tokenize(stack[0], "#", true);
            graphDef.stack(color[0], getColor(color[1]), (stack.length > 1 ? stack[1] : ""));
        } else if (arg.startsWith("HRULE:")) {
            String definition = arg.substring("HRULE:".length());
            String[] hrule = tokenize(definition, ":", true);
            String[] color = tokenize(hrule[0], "#", true);
            Double value = Double.valueOf(color[0]);
            graphDef.hrule(value, getColor(color[1]), (hrule.length > 1 ? hrule[1] : ""));
        } else if (arg.endsWith("/rrdtool") || arg.equals("graph") || arg.equals("-")) {
        // ignore, this is just a leftover from the rrdtool-specific options
        } else if (arg.trim().isEmpty()) {
        // ignore empty whitespace arguments
        } else {
            LOG.warn("JRobin: Unrecognized graph argument: {}", arg);
        }
    }
    graphDef.setTimeSpan(start, end);
    graphDef.setMinValue(lowerLimit);
    graphDef.setMaxValue(upperLimit);
    graphDef.setRigid(rigid);
    graphDef.setHeight(height);
    graphDef.setWidth(width);
    // graphDef.setSmallFont(new Font("Monospaced", Font.PLAIN, 10));
    // graphDef.setLargeFont(new Font("Monospaced", Font.PLAIN, 12));
    LOG.debug("JRobin Finished tokenizing checking: start time: {}, end time: {}", start, end);
    LOG.debug("large font = {}, small font = {}", graphDef.getLargeFont(), graphDef.getSmallFont());
    return graphDef;
}
Also used : ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RrdGraphDef(org.jrobin.graph.RrdGraphDef) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) File(java.io.File)

Aggregations

RrdGraphDef (org.jrobin.graph.RrdGraphDef)7 RrdGraph (org.jrobin.graph.RrdGraph)6 File (java.io.File)5 Test (org.junit.Test)4 RrdGraphInfo (org.jrobin.graph.RrdGraphInfo)2 Font (java.awt.Font)1 Graphics (java.awt.Graphics)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 ImageOutputStream (javax.imageio.stream.ImageOutputStream)1 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)1 RouterContext (net.i2p.router.RouterContext)1 RrdDb (org.jrobin.core.RrdDb)1