jrobinrrdstrategy.java
来自「opennms得相关源码 请大家看看」· Java 代码 · 共 602 行 · 第 1/2 页
JAVA
602 行
} if (debugTokens) log.debug("tokenize: reached end of string. completing token " + currToken); tokenList.add(currToken.toString()); return (String[]) tokenList.toArray(new String[tokenList.size()]); } /** * This constructs a graphDef by parsing the rrdtool style command and using * the values to create the JRobin graphDef. It does not understand the 'AT * style' time arguments however. Also there may be some rrdtool parameters * that it does not understand. These will be ignored. The graphDef will be * used to construct an RrdGraph and a PNG image will be created. An input * stream returning the bytes of the PNG image is returned. */ public InputStream createGraph(String command, File workDir) throws IOException, org.opennms.netmgt.rrd.RrdException { Category log = ThreadCategory.getInstance(getClass()); try { InputStream tempIn = null; String[] commandArray = tokenize(command, " \t", false); RrdGraphDef graphDef = new RrdGraphDef(); long start = 0; long end = 0; int height = 100; int width = 400; double lowerLimit = Double.NaN; double upperLimit = Double.NaN; boolean rigid = false; 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: " + start); } 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: "+height); } else if (arg.equals("--width")) { if (i + 1 < commandArray.length) { width = Integer.parseInt(commandArray[++i]); log.debug("JRobin width: "+height); } 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 upp limit: "+lowerLimit); } else if (arg.equals("--upper-limit")) { if (i + 1 < commandArray.length) { upperLimit = Double.parseDouble(commandArray[++i]); log.debug("JRobin upper limit: "+lowerLimit); } else { throw new IllegalArgumentException("--upper-limit must be followed by a number"); } } else if (arg.equals("--rigid")) { rigid = true; } else if (arg.startsWith("DEF:")) { String definition = arg.substring("DEF:".length()); String[] def = tokenize(definition, ":", true); String[] ds = tokenize(def[0], "=", true); File dsFile = new File(workDir, ds[1]); graphDef.datasource(ds[0], dsFile.getAbsolutePath(), def[1], def[2]); } else if (arg.startsWith("CDEF:")) { String definition = arg.substring("CDEF:".length()); String[] cdef = tokenize(definition, "=", true); graphDef.datasource(cdef[0], cdef[1]); } 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], getColor(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], getColor(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], getColor(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 = " + gprint[2] + " newformat = " + format); graphDef.gprint(gprint[0], gprint[1], format); } else if (arg.startsWith("COMMENT:")) { String comments[] = tokenize(arg, ":", true); graphDef.comment(comments[1]); } else if (arg.startsWith("AREA:")) { String definition = arg.substring("AREA:".length()); String area[] = tokenize(definition, ":", true); String[] color = tokenize(area[0], "#", true); graphDef.area(color[0], getColor(color[1]), (area.length > 1 ? area[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 { log.warn("JRobin: Unrecognized graph argument: " + arg); } } graphDef.setTimePeriod(start, end); graphDef.setGridRange(lowerLimit, upperLimit, rigid); graphDef.setDefaultFont(new Font("Monospaced", Font.PLAIN, 10)); graphDef.setTitleFont(new Font("Monospaced", Font.PLAIN, 12)); log.debug("JRobin Finished tokenizing checking: start time: " + start + "; end time: " + end); RrdGraph graph = new RrdGraph(graphDef, false); byte[] bytes = graph.getPNGBytes(width, height); tempIn = new ByteArrayInputStream(bytes); return tempIn; } catch (Exception e) { log.error("JRobin:exception occurred creating graph", e); throw new org.opennms.netmgt.rrd.RrdException("An exception occurred creating the graph.", e); } } /** * @param colorArg Should have the form COLORTAG#RRGGBB * @see http://www.jrobin.org/support/man/rrdgraph.html */ private void parseGraphColor(RrdGraphDef graphDef, String colorArg) throws IllegalArgumentException { // Parse for format COLORTAG#RRGGBB String[] colorArgParts = tokenize(colorArg, "#", false); if (colorArgParts.length != 2) throw new IllegalArgumentException ("--color must be followed by value with format " + "COLORTAG#RRGGBB"); String colorTag = colorArgParts[0].toUpperCase(); String colorHex = colorArgParts[1].toUpperCase(); // validate hex color input is actually an RGB hex color value if (colorHex.length() != 6) throw new IllegalArgumentException ("--color must be followed by value with format " + "COLORTAG#RRGGBB"); // this might throw NumberFormatException, but whoever wrote // createGraph didn't seem to care, so I guess I don't care either. // It'll get wrapped in an RrdException anyway. Color color = getColor(colorHex); // These are the documented RRD color tags if (colorTag.equals("BACK")) { graphDef.setBackColor(color); } else if (colorTag.equals("CANVAS")) { graphDef.setCanvasColor(color); } else if (colorTag.equals("SHADEA")) { graphDef.setImageBorder(color, 1); } else if (colorTag.equals("SHADEB")) { Category log = ThreadCategory.getInstance(getClass()); log.debug("parseGraphColor: JRobin does not support SHADEB"); } else if (colorTag.equals("GRID")) { graphDef.setMinorGridColor(color); } else if (colorTag.equals("MGRID")) { graphDef.setMajorGridColor(color); } else if (colorTag.equals("FONT")) { graphDef.setDefaultFontColor(color); graphDef.setTitleFontColor(color); } else if (colorTag.equals("FRAME")) { graphDef.setFrameColor(color); graphDef.setAxisColor(color); } else if (colorTag.equals("ARROW")) { graphDef.setArrowColor(color); } else { throw new IllegalArgumentException ("Unknown color tag " + colorTag); } } /** * This implementation does not track and stats. */ public String getStats() { return ""; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?