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
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 ****************************************************************************/
16 using System.Collections;
17 using System.Runtime.InteropServices;
20 namespace dnrrd_binding_test
22 class rrd_binding_test
24 private static string path = "";
26 static void Main(string[] args)
28 Console.WriteLine("----- Starting Tests -----");
29 Console.WriteLine("RRDLib Version: " + rrd.Version());
44 Console.WriteLine("\n!!!!!! Finished !!!!!!!");
45 string inp = Console.ReadLine();
48 static void Test_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)));
58 Console.WriteLine("Error: " + rrd.Get_Error());
60 Console.WriteLine("Test create: Successful!");
62 static void Test_Get_Info()
64 Console.WriteLine("Try getting info...");
65 rrd_info_t? info = rrd.Info(path + "test_a.rrd");
68 Console.WriteLine("Error: " + rrd.Get_Error());
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)
77 case rrd_info_type_t.RD_I_STR:
78 Console.WriteLine("\"" + Marshal.PtrToStringAnsi(((rrd_info_t)info).value.u_str) + "\"");
80 case rrd_info_type_t.RD_I_INT:
81 Console.WriteLine(((rrd_info_t)info).value.u_int);
83 case rrd_info_type_t.RD_I_CNT:
84 Console.WriteLine(((rrd_info_t)info).value.u_cnt);
86 case rrd_info_type_t.RD_I_VAL:
87 Console.WriteLine(((rrd_info_t)info).value.u_val);
89 case rrd_info_type_t.RD_I_BLO:
90 Console.WriteLine(" ** BLOB ** ");
93 Console.WriteLine("**** Unknown type! ****");
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));
102 Console.WriteLine("Test Info: Successful!");
103 //Console.WriteLine("Printing information...");
104 //Info_Print(((rrd_info_t)info));
106 static void Test_Update()
108 Console.WriteLine("Updating RRD...");
109 ArrayList al = new ArrayList();
111 // set to false if you want to use random values
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");
132 UInt32 ts = 920804700;
133 for (int i = 0; i < 15; i++)
135 al.Add(ts.ToString() + ":" + rrd.Random());
139 int ret = rrd.Update(path + "test_a.rrd", null, (string[])al.ToArray(typeof(string)));
141 Console.WriteLine("Error: " + rrd.Get_Error());
143 Console.WriteLine("Test update: Successful!");
145 static void Test_Fetch()
148 Console.WriteLine("Attempting Fetch...");
149 ArrayList al = new ArrayList();
151 al.Add(path + "test_a.rrd");
157 IntPtr data = new IntPtr();
158 string[] rrds = new string[0];
163 int ret = rrd.Fetch((string[])al.ToArray(typeof(string)), ref start, ref end,
164 ref step, ref dscnt, ref rrds, ref data);
168 for (Int32 ti = start + (Int32)step; ti <= end; ti += (Int32)step)
170 Console.Write(ti + ": ");
171 for (Int32 i = 0; i < (Int32)dscnt; i++)
173 Console.Write(((double)Marshal.PtrToStructure(data, typeof(double))).ToString(" 0.0000000000e+00"));
174 data = new IntPtr(data.ToInt64() + sizeof(double));
176 Console.Write(Environment.NewLine);
181 Console.WriteLine("Error: " + rrd.Get_Error());
183 Console.WriteLine("Test fetch: Successful!");
185 static void Test_Graph()
187 Console.WriteLine("Creating graph...");
188 ArrayList al = new ArrayList();
190 al.Add(path + "graph_simple.png");
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)));
199 Console.WriteLine("Error: " + rrd.Get_Error());
201 Console.WriteLine("Test graph: Successful!");
203 static void Test_Graph_Math()
205 Console.WriteLine("Creating graph...");
206 ArrayList al = new ArrayList();
208 al.Add(path + "graph_math.png");
213 al.Add("--vertical-label");
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)));
220 Console.WriteLine("Error: " + rrd.Get_Error());
222 Console.WriteLine("Test graph: Successful!");
224 static void Test_Graph_Math2()
226 Console.WriteLine("Creating graph...");
227 ArrayList al = new ArrayList();
229 al.Add(path + "graph_math2.png");
234 al.Add("--vertical-label");
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)));
245 Console.WriteLine("Error: " + rrd.Get_Error());
247 Console.WriteLine("Test graph: Successful!");
249 static void Test_Graph_Math3()
251 Console.WriteLine("Creating graph...");
252 ArrayList al = new ArrayList();
254 al.Add(path + "graph_math3.png");
259 al.Add("--vertical-label");
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)));
273 Console.WriteLine("Error: " + rrd.Get_Error());
275 Console.WriteLine("Test graph: Successful!");
277 static void Test_First_Last()
279 Console.WriteLine("Testing values...");
280 Console.WriteLine("First Value: " + rrd.First(path + "test_a.rrd", 0));
281 string err = rrd.Get_Error();
283 Console.WriteLine("Error: " + err);
284 Console.WriteLine("Last Value: " + rrd.Last(path + "test_a.rrd", 0));
285 err = rrd.Get_Error();
287 Console.WriteLine("Error: " + err);
289 Int32 last_update = 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!");
297 static void Test_Dump()
299 Console.WriteLine("Dumping RRD...");
300 int ret = rrd.Dump(path + "test_a.rrd", path + "test_a.xml");
302 Console.WriteLine("Error: " + rrd.Get_Error());
304 Console.WriteLine("Test Dump: Successful!");
306 static void Test_Xport()
308 Console.WriteLine("Exporting RRD...");
309 ArrayList al = new ArrayList();
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];
323 int ret = rrd.Xport((string[])al.ToArray(typeof(string)), ref start, ref end,
324 ref step, ref col_cnt, ref legends, ref data);
328 for (Int32 ti = start + (Int32)step; ti <= end; ti += (Int32)step)
330 Console.Write(ti + ": ");
331 for (Int32 i = 0; i < (Int32)col_cnt; i++)
333 Console.Write(((double)Marshal.PtrToStructure(data, typeof(double))).ToString(" 0.0000000000e+00"));
334 data = new IntPtr(data.ToInt64() + sizeof(double));
336 Console.Write(Environment.NewLine);
340 Console.WriteLine("Error: " + rrd.Get_Error());
342 Console.WriteLine("Test xport: Successful!");
344 static void Test_Restore()
346 Console.WriteLine("Restoring RRD...");
347 ArrayList al = new ArrayList();
349 al.Add(path + "test_a.xml");
350 al.Add(path + "restored_a.rrd");
351 int ret = rrd.Restore((string[])al.ToArray(typeof(string)));
353 Console.WriteLine("Error: " + rrd.Get_Error());
355 Console.WriteLine("Test restore: Successful!");
357 static void Test_Tune()
359 Console.WriteLine("Tuning RRD...");
360 ArrayList al = new ArrayList();
362 al.Add(path + "restored_a.rrd");
365 int ret = rrd.Tune((string[])al.ToArray(typeof(string)));
367 Console.WriteLine("Error: " + rrd.Get_Error());
369 Console.WriteLine("Test tune: Successful!");