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