目录:
- 运用场景
- 初始数据
- 绘制图表
[一]、运用场景
本人主要讲述JRobin中运用RPN表达式的功能绘制某一数据源指定时间段的流量图以及不同数据源不同时间段的合并流量图。
[二]、初始数据
生成测试RRD数据文件的代码CoreHandler.java :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
package com.micmiu.jrobin; import java.util.List; import org.jrobin.core.DsDef; import org.jrobin.core.DsTypes; import org.jrobin.core.RrdDb; import org.jrobin.core.RrdDef; import org.jrobin.core.Sample; import org.jrobin.core.Util; /** * * blog http://www.micmiu.com * * @author Michael * */ public class CoreHandler { /** * @param args */ public static void main(String[] args) { try { String rootPath = "D:/test/jrobin/"; String rrdName = rootPath + "demo_port1.rrd"; long startTime = Util.getTimestamp(2012, 3 - 1, 1); long endTime = Util.getTimestamp(2012, 4 - 1, 1); RrdDef rrdDef = CoreHandler.createRrdDef4Demo(rrdName, startTime); System.out.println("RrdDef Demo create"); rrdDef.exportXmlTemplate(rrdName + "_template.xml"); System.out.println("[RrdDef Template export to xml ]"); CoreHandler.initData4Demo(rrdDef, startTime, endTime); } catch (Exception e) { e.printStackTrace(); } } /** * 创建RRDDef */ public static RrdDef createRrdDef4Demo(String rrdName, long startTime) { try { RrdDef rrdDef = new RrdDef(rrdName, startTime - 1, 300); // DsTypes: GAUGE COUNTER DERIVE ABSOLUTE DsDef dsDef = new DsDef("flow", DsTypes.DT_COUNTER, 600, 0, Double.NaN); rrdDef.addDatasource(dsDef); rrdDef.addArchive("AVERAGE", 0.5, 1, 600); rrdDef.addArchive("AVERAGE", 0.5, 6, 700); rrdDef.addArchive("AVERAGE", 0.5, 24, 797); rrdDef.addArchive("AVERAGE", 0.5, 288, 775); rrdDef.addArchive("MAX", 0.5, 1, 600); rrdDef.addArchive("MAX", 0.5, 6, 700); rrdDef.addArchive("MAX", 0.5, 24, 797); rrdDef.addArchive("MAX", 0.5, 288, 775); System.out.println("RrdDef info =:" + rrdDef.dump()); return rrdDef; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 通过RrdDef创建RRD文件并初始化数据 */ public static void initData4Demo(RrdDef rrdDef, long startTime, long endTime) { try { RrdDb rrdDb = new RrdDb(rrdDef); // by this point, rrd file can be found on your disk // 模拟一些测试数据 // Math.sin(2 * Math.PI * (t / 86400.0)) * baseval; int baseval = 50; int dscount = rrdDef.getDsCount(); for (long t = startTime; t < endTime; t += 300) { Sample sample = rrdDb.createSample(t); double ranval = Math.random() * baseval; for (int i = 0; i < dscount; i++) { sample.setValue(i, baseval + ranval); } sample.update(); } System.out.println("[RrdDb init data success]"); System.out.println("[Rrd path]:" + rrdDef.getPath()); // rrdDb.dumpXml(rrdDef.getPath() + "_data.xml") rrdDb.exportXml(rrdDef.getPath() + "_data.xml"); // If your RRD files are updated rarely, open them only when // necessary and close them as soon as possible. rrdDb.close(); System.out.println("[RrdDb init demo data success]"); } catch (Exception e) { e.printStackTrace(); } } /** * 创建RRDDef */ public static RrdDef createRrdDef(String rrdName, long startTime, List<DsDef> dsDefList) { try { RrdDef rrdDef = new RrdDef(rrdName, startTime - 1, 300); for (DsDef dsDef : dsDefList) { rrdDef.addDatasource(dsDef); } rrdDef.addArchive("AVERAGE", 0.5, 1, 600); rrdDef.addArchive("AVERAGE", 0.5, 6, 700); rrdDef.addArchive("AVERAGE", 0.5, 24, 797); rrdDef.addArchive("AVERAGE", 0.5, 288, 775); rrdDef.addArchive("MAX", 0.5, 1, 600); rrdDef.addArchive("MAX", 0.5, 6, 700); rrdDef.addArchive("MAX", 0.5, 24, 797); rrdDef.addArchive("MAX", 0.5, 288, 775); return rrdDef; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 创建RRDDef */ public static RrdDef createFlowRrdDef(String rrdName, long startTime) { try { RrdDef rrdDef = new RrdDef(rrdName, startTime - 1, 300); // DsTypes: GAUGE COUNTER DERIVE ABSOLUTE DsDef dsDef = new DsDef("in", DsTypes.DT_COUNTER, 600, 0, Double.NaN); rrdDef.addDatasource(dsDef); rrdDef.addDatasource("out", DsTypes.DT_COUNTER, 600, 0, Double.NaN); rrdDef.addArchive("AVERAGE", 0.5, 1, 600); rrdDef.addArchive("AVERAGE", 0.5, 6, 700); rrdDef.addArchive("AVERAGE", 0.5, 24, 797); rrdDef.addArchive("AVERAGE", 0.5, 288, 775); rrdDef.addArchive("MAX", 0.5, 1, 600); rrdDef.addArchive("MAX", 0.5, 6, 700); rrdDef.addArchive("MAX", 0.5, 24, 797); rrdDef.addArchive("MAX", 0.5, 288, 775); return rrdDef; } catch (Exception e) { e.printStackTrace(); return null; } } } |
运用上述程序生成两个RRD测试数据文件:
- D:/test/jrobin/demo_port1.rrd
- D:/test/jrobin/demo_port2.rrd
[三]、绘制图表
绘制流量图的测试代码:GraphDemoHandler.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
package com.micmiu.jrobin; import java.awt.Color; import java.awt.Font; import java.awt.image.BufferedImage; import java.io.File; import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; import org.jrobin.core.Util; import org.jrobin.graph.RrdGraph; import org.jrobin.graph.RrdGraphDef; import org.jrobin.graph.RrdGraphDefTemplate; import org.xml.sax.InputSource; /** * blog: http://www.micmiu.com * * @author Michael * */ public class GraphDemoHandler { /** * @param args */ public static void main(String[] args) { try { String rootPath = "D:/test/jrobin/"; String rrdName = rootPath + "demo_port1.rrd"; String rrdName2 = rootPath + "demo_port2.rrd"; long startTime = Util.getTimestamp(2012, 3 - 1, 1); long endTime = Util.getTimestamp(2012, 4 - 1, 1); System.out.println("========== 基本的流量图 =========="); String imgFileName = rootPath + "jrobin_port1_1.png"; RrdGraphDef rgdef = GraphDemoHandler.graphDef4Demo(startTime, endTime, rrdName, imgFileName); GraphDemoHandler.graphByDef(rgdef); imgFileName = rootPath + "jrobin_port2_1.png"; RrdGraphDef rgdef2 = GraphDemoHandler.graphDef4Demo(startTime, endTime, rrdName2, imgFileName); GraphDemoHandler.graphByDef(rgdef2); System.out.println("========== 指定时间段的流量图 =========="); imgFileName = rootPath + "jrobin_port1_2.png"; String time1 = "1330531200";// 2012-03-01 00:00:00 String time2 = "1331827200";// 2012-03-16 00:00:00 String time3 = "1333209601";// 2012-04-01 00:00:01 RrdGraphDef rgdef3 = GraphDemoHandler.graphDef4Interval(startTime, endTime, rrdName, imgFileName, time1, time2); GraphDemoHandler.graphByDef(rgdef3); imgFileName = rootPath + "jrobin_port2_2.png"; RrdGraphDef rgdef4 = GraphDemoHandler.graphDef4Interval(startTime, endTime, rrdName2, imgFileName, time2, time3); GraphDemoHandler.graphByDef(rgdef4); System.out.println("========== 不同端口不同时间段的合并图 =========="); imgFileName = rootPath + "jrobin_port12_merge.png"; List<String[]> rrdparas = new ArrayList<String[]>(); rrdparas.add(new String[] { rrdName, time1, time2 }); rrdparas.add(new String[] { rrdName2, time2, time3 }); RrdGraphDef rgdef5 = GraphDemoHandler.graphDef4HorizontalMerge( startTime, endTime, imgFileName, rrdparas); GraphDemoHandler.graphByDef(rgdef5); } catch (Exception e) { e.printStackTrace(); } } private static RrdGraphDef cfgBaseGraphDef() { RrdGraphDef rgdef = new RrdGraphDef(); rgdef.setSignature("www.micmiu.com"); rgdef.setAntiAliasing(false); rgdef.setImageFormat("PNG"); rgdef.setSmallFont(new Font("Monospaced", Font.PLAIN, 11)); rgdef.setLargeFont(new Font("SansSerif", Font.BOLD, 14)); // rgdef rgdef.setTitle("JRobin Graph Demo for micmiu.com"); rgdef.setBase(1024); rgdef.setWidth(500); rgdef.setHeight(200); rgdef.setVerticalLabel("transfer speed [bps]"); return rgdef; } /** * 自定义画图模板 * * @param startTime * @param endTime * @param rrdName * @param imgFileName * @return */ public static RrdGraphDef graphDef4Demo(long startTime, long endTime, String rrdName, String imgFileName) { RrdGraphDef rgdef = null; try { System.out.println("[rrd graph by RrdGraphDef start...]"); rgdef = cfgBaseGraphDef(); rgdef.setFilename(imgFileName); rgdef.setStartTime(startTime); rgdef.setEndTime(endTime); rgdef.datasource("port", rrdName, "flow", "AVERAGE"); rgdef.datasource("line_data", "port,8,*"); rgdef.line("line_data", Color.BLUE, "test flow\\l"); // \\l->左对齐 \\c->中间对齐 \\r->右对齐 \\j->自适应 // \\s-> \\g->glue \\J-> rgdef.gprint("line_data", "MAX", "速率最大值=%.2f %sbps"); rgdef.gprint("line_data", "AVERAGE", "速率平均值=%.2f %sbps\\l"); rgdef.gprint("line_data", "TOTAL", "总流量=%.2f %sb\\l"); rgdef.comment("More info see : www.micmiu.com"); return rgdef; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 创建指定时间段的画图模板 * * @param startTime * @param endTime * @param rrdName * @param imgFileName * @return */ public static RrdGraphDef graphDef4Interval(long startTime, long endTime, String rrdName, String imgFileName, String time1, String time2) { RrdGraphDef rgdef = null; try { System.out.println("[rrd graph def for TimeInterval start...]"); rgdef = cfgBaseGraphDef(); rgdef.setFilename(imgFileName); rgdef.setStartTime(startTime); rgdef.setEndTime(endTime); rgdef.datasource("port", rrdName, "flow", "AVERAGE"); // rpn:TIME,starttime,GE,TIME,endtime,LT,*,dsname,UNKN,IF String rpnExp = "TIME,{0},GE,TIME,{1},LE,*,{2},UNKN,IF"; String rpn = MessageFormat.format(rpnExp, time1, time2, "port"); rgdef.datasource("port_data", rpn); rgdef.datasource("line_data", "port_data,8,*"); rgdef.line("line_data", Color.BLUE, "test flow\\l"); rgdef.gprint("line_data", "MAX", "速率最大值=%.2f %sbps"); rgdef.gprint("line_data", "AVERAGE", "速率平均值=%.2f %sbps\\l"); rgdef.gprint("line_data", "TOTAL", "总流量=%.2f %sb\\l"); rgdef.comment("More info see : www.micmiu.com"); return rgdef; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 多个端口指定时间段合并画图模板 * * @param startTime * @param endTime * @param rrdName * @param imgFileName * @return */ public static RrdGraphDef graphDef4HorizontalMerge(long startTime, long endTime, String imgFileName, List<String[]> rrdparas) { RrdGraphDef rgdef = null; try { System.out.println("[ RrdGraphDef for horizontal merge start...]"); rgdef = cfgBaseGraphDef(); rgdef.setFilename(imgFileName); rgdef.setStartTime(startTime); rgdef.setEndTime(endTime); // rpn:TIME,starttime,GE,TIME,endtime,LT,*,port_1,0,IF String rpnExp = "TIME,{0},GE,TIME,{1},LT,*,{2},0,IF"; String totalExp = "0"; for (int i = 0; i < rrdparas.size(); i++) { rgdef.datasource("port_" + i, rrdparas.get(i)[0], "flow", "AVERAGE"); String rpn = MessageFormat.format(rpnExp, rrdparas.get(i)[1], rrdparas.get(i)[2], "port_" + i); rgdef.datasource("port_" + i + "_data", rpn); rgdef.datasource("port_" + i + "_data_8", "port_" + i + "_data,8,*"); totalExp += ",port_" + i + "_data_8,+"; } rgdef.datasource("port_merge", totalExp); rgdef.line("port_merge", Color.BLUE, "test flow\\l"); rgdef.gprint("port_merge", "MAX", "速率最大值=%.2f %sbps"); rgdef.gprint("port_merge", "AVERAGE", "速率平均值=%.2f %sbps\\l"); rgdef.gprint("port_merge", "TOTAL", "总流量=%.2f %sb\\l"); rgdef.comment("More info see : www.micmiu.com"); return rgdef; } catch (Exception e) { e.printStackTrace(); return null; } } public static void graphByDef(RrdGraphDef rgdef) { try { RrdGraph graph = new RrdGraph(rgdef); System.out.println("[rrd graph info:]" + graph.getRrdGraphInfo().dump()); // 如果filename没有设置,只是在内存中,可以调用下面的方法再次生成图片文件 if ("-".equals(graph.getRrdGraphInfo().getFilename())) { createImgFile(graph, "jrobin-graph.jpg"); } System.out.println("[Jrobin graph by RrdGraphDef success.]"); } catch (Exception e) { e.printStackTrace(); } } /** * 根据定义的XML文件创建画图模板 * * @param rootPath * @param imgFileName */ public static RrdGraphDef createGraphDefByTemplate(long startTime, long endTime, String templateName, String rrdName, String imgFile) { try { System.out .println("[GraphDef create by xml template file start...]"); RrdGraphDefTemplate defTemplate = new RrdGraphDefTemplate( new InputSource(templateName)); // setVariable 设置XML template的变量 defTemplate.setVariable("startTime", startTime); defTemplate.setVariable("endTime", endTime); defTemplate.setVariable("img_file", imgFile); defTemplate.setVariable("rrd_file", rrdName); return defTemplate.getRrdGraphDef(); } catch (Exception e) { e.printStackTrace(); return null; } } /** * ImageIO 生成图片文件 */ public static void createImgFile(RrdGraph graph, String imgFileName) { try { BufferedImage image = new BufferedImage(graph.getRrdGraphInfo() .getWidth(), graph.getRrdGraphInfo().getHeight(), BufferedImage.TYPE_INT_RGB); graph.render(image.getGraphics()); File imgFile = new File(imgFileName); ImageIO.write(image, "PNG", imgFile); } catch (Exception e) { e.printStackTrace(); } } } |
运行日志如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
========== 基本的流量图 ========== [rrd graph by RrdGraphDef start...] [rrd graph info:]filename = "D:/test/jrobin/jrobin_port1_1.png" width = 603, height = 337 byteCount = 12963 imginfo = "null" No print lines found [Jrobin graph by RrdGraphDef success.] [rrd graph by RrdGraphDef start...] [rrd graph info:]filename = "D:/test/jrobin/jrobin_port2_1.png" width = 603, height = 337 byteCount = 13031 imginfo = "null" No print lines found [Jrobin graph by RrdGraphDef success.] ========== 指定时间段的流量图 ========== [rrd graph def for TimeInterval start...] [rrd graph info:]filename = "D:/test/jrobin/jrobin_port1_2.png" width = 603, height = 337 byteCount = 12022 imginfo = "null" No print lines found [Jrobin graph by RrdGraphDef success.] [rrd graph def for TimeInterval start...] [rrd graph info:]filename = "D:/test/jrobin/jrobin_port2_2.png" width = 603, height = 337 byteCount = 11848 imginfo = "null" No print lines found [Jrobin graph by RrdGraphDef success.] ========== 不同端口不同时间段的合并图 ========== [ RrdGraphDef for horizontal merge start...] [rrd graph info:]filename = "D:/test/jrobin/jrobin_port12_merge.png" width = 603, height = 337 byteCount = 13065 imginfo = "null" No print lines found [Jrobin graph by RrdGraphDef success.] |
生成的流量图如下:
端口1 指定时间段2012-03-01~2012-03-16的流量图:
端口2 指定时间段2012-03-16~2012-04-01的流量图:
端口1(2012-03-01~2012-03-16)端口2(2012-03-16~2012-04-01)的合并流量图:
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/enterprise-app/snmp/jrobin-graph-rpn/
0 条评论。