c907e75c6dbded37aad082e34dfcc81507f654ea
1 /*****************************************************************************
2 * RRDLIB .NET Binding Test
3 *****************************************************************************
4 * Created 2010/06/29 by Chris Larsen
5 *
6 * This project tests the .NET binding library by creating an rrd, inserting
7 * data, fetching data, creating graphs, dumping and exporting the data to
8 * XML, then restoring from an XML file. The examples follow the tutorial
9 * written by Alex van den Bogaerdt found at
10 * http://oss.oetiker.ch/rrdtool/tut/rrdtutorial.en.html
11 ****************************************************************************/
13 using System;
14 using System.Collections;
15 using System.Runtime.InteropServices;
16 using dnrrdlib;
18 namespace dnrrd_binding_test
19 {
20 class rrd_binding_test
21 {
22 private static string path = "";
24 static void Main(string[] args)
25 {
26 Console.WriteLine("----- Starting Tests -----");
27 Console.WriteLine("RRDLib Version: " + rrd.Version());
29 Test_Create();
30 Test_Get_Info();
31 Test_Update();
32 Test_Fetch();
33 Test_Graph();
34 Test_Graph_Math();
35 Test_Graph_Math2();
36 Test_Graph_Math3();
37 Test_First_Last();
38 Test_Dump();
39 Test_Xport();
40 Test_Restore();
41 Test_Tune();
42 Console.WriteLine("\n!!!!!! Finished !!!!!!!");
43 string inp = Console.ReadLine();
44 }
46 static void Test_Create()
47 {
48 // create
49 ArrayList al = new ArrayList();
50 al.Add("DS:speed:COUNTER:600:U:U");
51 al.Add("RRA:AVERAGE:0.5:1:24");
52 al.Add("RRA:AVERAGE:0.5:6:10");
54 int ret = rrd.Create(path + "test_a.rrd", 300, 920804400, (string[])al.ToArray(typeof(string)));
55 if (ret < 0)
56 Console.WriteLine("Error: " + rrd.Get_Error());
57 else
58 Console.WriteLine("Test create: Successful!");
59 }
60 static void Test_Get_Info()
61 {
62 Console.WriteLine("Try getting info...");
63 rrd_info_t? info = rrd.Info(path + "test_a.rrd");
64 if (info == null)
65 {
66 Console.WriteLine("Error: " + rrd.Get_Error());
67 return;
68 }
69 while (info != null)
70 {
71 //info = (rrd_info_t)Marshal.PtrToStructure(info.next, typeof(rrd_info_t));
72 Console.Write(((rrd_info_t)info).key + ": ");
73 switch (((rrd_info_t)info).type)
74 {
75 case rrd_info_type_t.RD_I_STR:
76 Console.WriteLine("\"" + Marshal.PtrToStringAnsi(((rrd_info_t)info).value.u_str) + "\"");
77 break;
78 case rrd_info_type_t.RD_I_INT:
79 Console.WriteLine(((rrd_info_t)info).value.u_int);
80 break;
81 case rrd_info_type_t.RD_I_CNT:
82 Console.WriteLine(((rrd_info_t)info).value.u_cnt);
83 break;
84 case rrd_info_type_t.RD_I_VAL:
85 Console.WriteLine(((rrd_info_t)info).value.u_val);
86 break;
87 case rrd_info_type_t.RD_I_BLO:
88 Console.WriteLine(" ** BLOB ** ");
89 break;
90 default:
91 Console.WriteLine("**** Unknown type! ****");
92 break;
93 }
95 if (((rrd_info_t)info).next != IntPtr.Zero && (int)((rrd_info_t)info).next > 0)
96 info = (rrd_info_t)Marshal.PtrToStructure(((rrd_info_t)info).next, typeof(rrd_info_t));
97 else
98 info = null;
99 }
100 Console.WriteLine("Test Info: Successful!");
101 //Console.WriteLine("Printing information...");
102 //Info_Print(((rrd_info_t)info));
103 }
104 static void Test_Update()
105 {
106 Console.WriteLine("Updating RRD...");
107 ArrayList al = new ArrayList();
109 // set to false if you want to use random values
110 if (true)
111 {
112 al.Add("920804700:12345");
113 al.Add("920805000:12357");
114 al.Add("920805300:12363");
115 al.Add("920805600:12363");
116 al.Add("920805900:12363");
117 al.Add("920806200:12373");
118 al.Add("920806500:12383");
119 al.Add("920806800:12393");
120 al.Add("920807100:12399");
121 al.Add("920807400:12405");
122 al.Add("920807700:12411");
123 al.Add("920808000:12415");
124 al.Add("920808300:12420");
125 al.Add("920808600:12422");
126 al.Add("920808900:12423");
127 }
128 else
129 {
130 UInt32 ts = 920804700;
131 for (int i = 0; i < 15; i++)
132 {
133 al.Add(ts.ToString() + ":" + rrd.Random());
134 ts += 300;
135 }
136 }
137 int ret = rrd.Update(path + "test_a.rrd", null, (string[])al.ToArray(typeof(string)));
138 if (ret < 0)
139 Console.WriteLine("Error: " + rrd.Get_Error());
140 else
141 Console.WriteLine("Test update: Successful!");
142 }
143 static void Test_Fetch()
144 {
145 // FETCH
146 Console.WriteLine("Attempting Fetch...");
147 ArrayList al = new ArrayList();
148 al.Add("fetch");
149 al.Add(path + "test_a.rrd");
150 al.Add("AVERAGE");
151 al.Add("--start");
152 al.Add("920804400");
153 al.Add("--end");
154 al.Add("920809200");
155 IntPtr data = new IntPtr();
156 string[] rrds = new string[0];
157 Int32 start = 0;
158 Int32 end = 0;
159 UInt32 step = 0;
160 UInt32 dscnt = 0;
161 int ret = rrd.Fetch((string[])al.ToArray(typeof(string)), ref start, ref end,
162 ref step, ref dscnt, ref rrds, ref data);
164 if (end > start)
165 {
166 for (Int32 ti = start + (Int32)step; ti <= end; ti += (Int32)step)
167 {
168 Console.Write(ti + ": ");
169 for (Int32 i = 0; i < (Int32)dscnt; i++)
170 {
171 Console.Write(((double)Marshal.PtrToStructure(data, typeof(double))).ToString(" 0.0000000000e+00"));
172 data = new IntPtr(data.ToInt64() + sizeof(double));
173 }
174 Console.Write(Environment.NewLine);
175 }
176 }
178 if (ret < 0)
179 Console.WriteLine("Error: " + rrd.Get_Error());
180 else
181 Console.WriteLine("Test fetch: Successful!");
182 }
183 static void Test_Graph()
184 {
185 Console.WriteLine("Creating graph...");
186 ArrayList al = new ArrayList();
187 al.Add("graph");
188 al.Add(path + "graph_simple.png");
189 al.Add("--start");
190 al.Add("920804400");
191 al.Add("--end");
192 al.Add("920808000");
193 al.Add("DEF:myspeed=" + path.Replace(":", "\\:") + "test_a.rrd:speed:AVERAGE");
194 al.Add("LINE2:myspeed#00004D");
195 int ret = rrd.Graph((string[])al.ToArray(typeof(string)));
196 if (ret < 0)
197 Console.WriteLine("Error: " + rrd.Get_Error());
198 else
199 Console.WriteLine("Test graph: Successful!");
200 }
201 static void Test_Graph_Math()
202 {
203 Console.WriteLine("Creating graph...");
204 ArrayList al = new ArrayList();
205 al.Add("graph");
206 al.Add(path + "graph_math.png");
207 al.Add("--start");
208 al.Add("920804400");
209 al.Add("--end");
210 al.Add("920808000");
211 al.Add("--vertical-label");
212 al.Add("m/s");
213 al.Add("DEF:myspeed=" + path.Replace(":", "\\:") + "test_a.rrd:speed:AVERAGE");
214 al.Add("CDEF:realspeed=myspeed,1000,*");
215 al.Add("LINE2:realspeed#00004D");
216 int ret = rrd.Graph((string[])al.ToArray(typeof(string)));
217 if (ret < 0)
218 Console.WriteLine("Error: " + rrd.Get_Error());
219 else
220 Console.WriteLine("Test graph: Successful!");
221 }
222 static void Test_Graph_Math2()
223 {
224 Console.WriteLine("Creating graph...");
225 ArrayList al = new ArrayList();
226 al.Add("graph");
227 al.Add(path + "graph_math2.png");
228 al.Add("--start");
229 al.Add("920804400");
230 al.Add("--end");
231 al.Add("920808000");
232 al.Add("--vertical-label");
233 al.Add("m/s");
234 al.Add("DEF:myspeed=" + path.Replace(":", "\\:") + "test_a.rrd:speed:AVERAGE");
235 al.Add("CDEF:kmh=myspeed,3600,*");
236 al.Add("CDEF:fast=kmh,100,GT,kmh,0,IF");
237 al.Add("CDEF:good=kmh,100,GT,0,kmh,IF");
238 al.Add("HRULE:100#0000FF:\"Maximum allowed\"");
239 al.Add("AREA:good#00FF00:\"Good speed\"");
240 al.Add("AREA:fast#FF0000:\"Too fast\"");
241 int ret = rrd.Graph((string[])al.ToArray(typeof(string)));
242 if (ret < 0)
243 Console.WriteLine("Error: " + rrd.Get_Error());
244 else
245 Console.WriteLine("Test graph: Successful!");
246 }
247 static void Test_Graph_Math3()
248 {
249 Console.WriteLine("Creating graph...");
250 ArrayList al = new ArrayList();
251 al.Add("graph");
252 al.Add(path + "graph_math3.png");
253 al.Add("--start");
254 al.Add("920804400");
255 al.Add("--end");
256 al.Add("920808000");
257 al.Add("--vertical-label");
258 al.Add("m/s");
259 al.Add("DEF:myspeed=" + path.Replace(":", "\\:") + "test_a.rrd:speed:AVERAGE");
260 al.Add("CDEF:nonans=myspeed,UN,0,myspeed,IF");
261 al.Add("CDEF:kmh=nonans,3600,*");
262 al.Add("CDEF:fast=kmh,100,GT,100,0,IF");
263 al.Add("CDEF:over=kmh,100,GT,kmh,100,-,0,IF");
264 al.Add("CDEF:good=kmh,100,GT,0,kmh,IF");
265 al.Add("HRULE:100#0000FF:\"Maximum allowed\"");
266 al.Add("AREA:good#00FF00:\"Good speed\"");
267 al.Add("AREA:fast#550000:\"Too fast\"");
268 al.Add("STACK:over#FF0000:\"Over speed\"");
269 int ret = rrd.Graph((string[])al.ToArray(typeof(string)));
270 if (ret < 0)
271 Console.WriteLine("Error: " + rrd.Get_Error());
272 else
273 Console.WriteLine("Test graph: Successful!");
274 }
275 static void Test_First_Last()
276 {
277 Console.WriteLine("Testing values...");
278 Console.WriteLine("First Value: " + rrd.First(path + "test_a.rrd", 0));
279 string err = rrd.Get_Error();
280 if (err.Length > 1)
281 Console.WriteLine("Error: " + err);
282 Console.WriteLine("Last Value: " + rrd.Last(path + "test_a.rrd", 0));
283 err = rrd.Get_Error();
284 if (err.Length > 1)
285 Console.WriteLine("Error: " + err);
287 Int32 last_update = 0;
288 UInt32 ds_count = 0;
289 string[] ds_names = new string[0];
290 string[] last_ds = new string[0];
291 rrd.Last_Update(path + "test_a.rrd", ref last_update, ref ds_count, ref ds_names, ref last_ds);
292 Console.WriteLine("Last Update: " + last_update);
293 Console.WriteLine("Value testing successful!");
294 }
295 static void Test_Dump()
296 {
297 Console.WriteLine("Dumping RRD...");
298 int ret = rrd.Dump(path + "test_a.rrd", path + "test_a.xml");
299 if (ret < 0)
300 Console.WriteLine("Error: " + rrd.Get_Error());
301 else
302 Console.WriteLine("Test Dump: Successful!");
303 }
304 static void Test_Xport()
305 {
306 Console.WriteLine("Exporting RRD...");
307 ArrayList al = new ArrayList();
308 al.Add("xport");
309 al.Add("--start");
310 al.Add("920804400");
311 al.Add("--end");
312 al.Add("920808000");
313 al.Add("DEF:myspeed=" + path.Replace(":", "\\:") + "test_a.rrd:speed:AVERAGE");
314 al.Add("XPORT:myspeed:\"MySpeed\"");
315 IntPtr data = new IntPtr();
316 string[] legends = new string[0];
317 Int32 start = 0;
318 Int32 end = 0;
319 UInt32 step = 0;
320 UInt32 col_cnt = 0;
321 int ret = rrd.Xport((string[])al.ToArray(typeof(string)), ref start, ref end,
322 ref step, ref col_cnt, ref legends, ref data);
324 if (end > start)
325 {
326 for (Int32 ti = start + (Int32)step; ti <= end; ti += (Int32)step)
327 {
328 Console.Write(ti + ": ");
329 for (Int32 i = 0; i < (Int32)col_cnt; i++)
330 {
331 Console.Write(((double)Marshal.PtrToStructure(data, typeof(double))).ToString(" 0.0000000000e+00"));
332 data = new IntPtr(data.ToInt64() + sizeof(double));
333 }
334 Console.Write(Environment.NewLine);
335 }
336 }
337 if (ret < 0)
338 Console.WriteLine("Error: " + rrd.Get_Error());
339 else
340 Console.WriteLine("Test xport: Successful!");
341 }
342 static void Test_Restore()
343 {
344 Console.WriteLine("Restoring RRD...");
345 ArrayList al = new ArrayList();
346 al.Add("restore");
347 al.Add(path + "test_a.xml");
348 al.Add(path + "restored_a.rrd");
349 int ret = rrd.Restore((string[])al.ToArray(typeof(string)));
350 if (ret < 0)
351 Console.WriteLine("Error: " + rrd.Get_Error());
352 else
353 Console.WriteLine("Test restore: Successful!");
354 }
355 static void Test_Tune()
356 {
357 Console.WriteLine("Tuning RRD...");
358 ArrayList al = new ArrayList();
359 al.Add("tune");
360 al.Add(path + "restored_a.rrd");
361 al.Add("-h");
362 al.Add("speed:650");
363 int ret = rrd.Tune((string[])al.ToArray(typeof(string)));
364 if (ret < 0)
365 Console.WriteLine("Error: " + rrd.Get_Error());
366 else
367 Console.WriteLine("Test tune: Successful!");
368 }
369 }
370 }