最近接触一个流量统计和网管的系统,对相关信息和技术做了一些了解和学习,在此记录,以供自己日后回顾,同时也方便给刚接触的TX们一些参考。
MRTG(Multi Router Traffic Grapher,MRTG),相信接触过网络管理的人,应该不会对它陌生的。MRTG是比较早的技术了,后来在此基础上发展了RRD技术,相比MRTG而言更加灵活方便。
JRobin(基于LGPL授权的开源技术)就是RRD的一个Java实现的版本。JRobin支持在RRD(Round Robin Database)上的所有标准操作:CREATE, UPDATE, FETCH, LAST, DUMP, XPORT和GRAPH。JRobin的API适合于那些熟悉RRDTool的概念与逻辑,但更喜欢使用纯Java实现的人。如果你提供相同的数据给RRDTool与JRobin,你将会得到相同的结果与图形。
ps:JRobin的官网:
http://oldwww.jrobin.org/
http://www.jrobin.org/index.php/Main_Page
先从Core API 开始了解学习如何定义RRD模板、如何创建RRD文件、如何获取RrdDb、如何将RRD文件和XML之间转换以及如何从RRD文件里读取数据等基础内容,下面是学习时写的一些测试代码:TestCoreRrd.java
1 | package com.snmp.jrobin; |
3 | import org.jrobin.core.DsDef; |
4 | import org.jrobin.core.DsTypes; |
5 | import org.jrobin.core.FetchData; |
6 | import org.jrobin.core.FetchRequest; |
7 | import org.jrobin.core.RrdDb; |
8 | import org.jrobin.core.RrdDef; |
9 | import org.jrobin.core.Sample; |
10 | import org.jrobin.core.Util; |
16 | public class TestCoreRrd { |
20 | public static void main(String[] args) { |
21 | // 2010-10-01:1285862400L 2010-11-01:1288540800L |
22 | long startTime = Util.getTimestamp( 2010 , 10 - 1 , 1 ); |
23 | long endTime = Util.getTimestamp( 2010 , 11 - 1 , 1 ); |
25 | TestCoreRrd test = new TestCoreRrd(); |
26 | String rootPath = "d:/test/jrobin/" ; |
27 | String rrdName = "demo_flow.rrd" ; |
29 | RrdDef rrdDef = test.createRrdDef(rootPath, rrdName, startTime); |
31 | test.createRRDInitData(startTime, endTime, rootPath, rrdName, rrdDef); |
33 | test.getRrdDbMethod(rootPath); |
35 | test.fetchRrdData(rootPath, rrdName); |
41 | private RrdDef createRrdDef(String rootPath, String rrdName, long startTime) { |
44 | String rrdPath = rootPath + rrdName; |
45 | RrdDef rrdDef = new RrdDef(rrdPath, startTime - 1 , 300 ); |
46 | // DsTypes: GAUGE COUNTER DERIVE ABSOLUTE |
47 | DsDef dsDef = new DsDef( "input" , DsTypes.DT_COUNTER, 600 , 0 , |
49 | rrdDef.addDatasource(dsDef); |
51 | rrdDef.addDatasource( "output" , DsTypes.DT_COUNTER, 600 , 0 , |
54 | rrdDef.addArchive( "AVERAGE" , 0.5 , 1 , 600 ); |
55 | rrdDef.addArchive( "AVERAGE" , 0.5 , 6 , 700 ); |
56 | rrdDef.addArchive( "AVERAGE" , 0.5 , 24 , 797 ); |
57 | rrdDef.addArchive( "AVERAGE" , 0.5 , 288 , 775 ); |
58 | rrdDef.addArchive( "MAX" , 0.5 , 1 , 600 ); |
59 | rrdDef.addArchive( "MAX" , 0.5 , 6 , 700 ); |
60 | rrdDef.addArchive( "MAX" , 0.5 , 24 , 797 ); |
61 | rrdDef.addArchive( "MAX" , 0.5 , 288 , 775 ); |
63 | // RRD file definition is completed |
65 | rrdDef.exportXmlTemplate(rootPath + rrdName + "_template.xml" ); |
66 | System.out.println( "[RrdDef Template export xml success]" ); |
69 | } catch (Exception e) { |
76 | * 通过RrdDef创建RRD文件并初始化数据 |
78 | private void createRRDInitData( long startTime, long endTime, |
79 | String rootPath, String rrdName, RrdDef rrdDef) { |
82 | RrdDb rrdDb = new RrdDb(rrdDef); |
83 | // / by this point, rrd file can be found on your disk |
86 | //Math.sin(2 * Math.PI * (t / 86400.0)) * baseval; |
88 | for ( long t = startTime; t < endTime; t += 300 ) { |
89 | Sample sample = rrdDb.createSample(t); |
90 | double tmpval = Math.random() * baseval; |
91 | double tmpval2 = Math.random() * baseval; |
92 | sample.setValue( "input" , tmpval + 50 ); |
93 | sample.setValue( "output" , tmpval2 + 50 ); |
96 | System.out.println( "[RrdDb init data success]" ); |
97 | System.out.println( "[Rrd path]:" + rrdDef.getPath()); |
99 | // rrdDb.dumpXml(rootPath + rrdName + "_rrd.xml") |
100 | rrdDb.exportXml(rootPath + rrdName + ".xml" ); |
102 | // If your RRD files are updated rarely, open them only when |
103 | // necessary and close them as soon as possible. |
106 | System.out.println( "[RrdDb export xml success]" ); |
107 | } catch (Exception e) { |
114 | * 除根据RrdDef以外获取RrdDb的其他方法 |
116 | private void getRrdDbMethod(String rootPath) { |
120 | String rrdFullPath = rootPath + "demo_flow.rrd" ; |
121 | RrdDb rrdDb = new RrdDb(rrdFullPath); |
122 | System.out.println( "[info:]" + rrdDb.getInfo() + "[path:]" |
127 | rrdDb = new RrdDb(rootPath + "copy.rrd" , rootPath |
128 | + "demo_flow_rrd.xml" ); |
129 | System.out.println( "[info:]" + rrdDb.getInfo() + "[path:]" |
132 | } catch (Exception e) { |
140 | private void fetchRrdData(String rootPath, String rrdName) { |
143 | RrdDb rrd = new RrdDb(rootPath + rrdName); |
145 | // create fetch request using the database reference |
146 | FetchRequest request = rrd.createFetchRequest( "AVERAGE" , Util |
147 | .getTimestamp( 2010 , 10 - 1 , 1 ), Util.getTimestamp( 2010 , |
150 | System.out.println( "[requet dump:]" + request.dump()); |
152 | // filter the datasources you really need |
153 | // String[] filterDataSource = { "input", "output" }; |
154 | // request.setFilter(filterDataSource); |
156 | // if you want only the "input" datasource use: |
157 | // request.setFilter("input"); |
159 | // execute the request |
160 | FetchData fetchData = request.fetchData(); |
161 | int columnCount = fetchData.getColumnCount(); |
162 | int rowCount = fetchData.getRowCount(); |
163 | long [] timestamps = fetchData.getTimestamps(); |
164 | System.out.println( "[data column count:]" + columnCount); |
165 | System.out.println( "[data row count:]" + rowCount); |
167 | // System.out.println("[fetch data dump:]" + fetchData.dump()); |
169 | double [][] values = fetchData.getValues(); |
170 | StringBuffer buffer = new StringBuffer( "" ); |
171 | for ( int row = 0 ; row < rowCount; row++) { |
172 | buffer.append(timestamps[row]); |
174 | for ( int dsIndex = 0 ; dsIndex < columnCount; dsIndex++) { |
175 | buffer.append(Util.formatDouble(values[dsIndex][row])); |
180 | System.out.println( "[fetch data display :]\n" + buffer); |
181 | } catch (Exception e) { |
RRD定义的模板导出XML:
2 | < path >d:/test/jrobin/demo_flow.rrd</ path > |
4 | < start >1285862399</ start > |
8 | < heartbeat >600</ heartbeat > |
9 | < min >+0.0000000000E00</ min > |
15 | < heartbeat >600</ heartbeat > |
16 | < min >+0.0000000000E00</ min > |
21 | < xff >+5.0000000000E-01</ xff > |
27 | < xff >+5.0000000000E-01</ xff > |
33 | < xff >+5.0000000000E-01</ xff > |
39 | < xff >+5.0000000000E-01</ xff > |
45 | < xff >+5.0000000000E-01</ xff > |
51 | < xff >+5.0000000000E-01</ xff > |
57 | < xff >+5.0000000000E-01</ xff > |
63 | < xff >+5.0000000000E-01</ xff > |
RRD文件转换成XML文件的片段:
2 | <!-- JRobin, version 0.1 --> |
3 | < version >0001</ version > |
6 | <!-- Sun Oct 31 23:55:00 CST 2010 --> |
7 | < lastupdate >1288540500</ lastupdate > |
11 | < minimal_heartbeat >600</ minimal_heartbeat > |
12 | < min >+0.0000000000E00</ min > |
15 | < last_ds >+6.9973544356E01</ last_ds > |
16 | < value >+0.0000000000E00</ value > |
17 | < unknown_sec >0</ unknown_sec > |
22 | < minimal_heartbeat >600</ minimal_heartbeat > |
23 | < min >+0.0000000000E00</ min > |
26 | < last_ds >+9.4423065918E01</ last_ds > |
27 | < value >+0.0000000000E00</ value > |
28 | < unknown_sec >0</ unknown_sec > |
33 | < pdp_per_row >1</ pdp_per_row > |
34 | < xff >+5.0000000000E-01</ xff > |
38 | < unknown_datapoints >0</ unknown_datapoints > |
42 | < unknown_datapoints >0</ unknown_datapoints > |
46 | <!-- Fri Oct 29 22:00:00 CST 2010 / 1288360800 --> |
48 | < v >+1.4316557626E07</ v > |
49 | < v >+1.2619069495E-01</ v > |
51 | <!-- Fri Oct 29 22:05:00 CST 2010 / 1288361100 --> |
53 | < v >+1.4063324411E-02</ v > |
54 | < v >+1.4316557534E07</ v > |
57 | ........................... |
61 | ............................... |
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。