1 /*****************************************************************************
2 * RRDLIB .NET Binding Test
3 *****************************************************************************
4 * Created 2010/06/29 by Chris Larsen
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 ****************************************************************************/
14 using System.Collections;
15 using System.Runtime.InteropServices;
18 namespace dnrrd_binding_test
20 class rrd_binding_test
22 private static string path = "";
24 static void Main(string[] args)
26 Console.WriteLine("----- Starting Tests -----");
27 Console.WriteLine("RRDLib Version: " + rrd.Version());
42 Console.WriteLine("\n!!!!!! Finished !!!!!!!");
43 string inp = Console.ReadLine();
46 static void Test_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)));
56 Console.WriteLine("Error: " + rrd.Get_Error());
58 Console.WriteLine("Test create: Successful!");
60 static void Test_Get_Info()
62 Console.WriteLine("Try getting info...");
63 rrd_info_t? info = rrd.Info(path + "test_a.rrd");
66 Console.WriteLine("Error: " + rrd.Get_Error());
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)
75 case rrd_info_type_t.RD_I_STR:
76 Console.WriteLine("\"" + Marshal.PtrToStringAnsi(((rrd_info_t)info).value.u_str) + "\"");
78 case rrd_info_type_t.RD_I_INT:
79 Console.WriteLine(((rrd_info_t)info).value.u_int);
81 case rrd_info_type_t.RD_I_CNT:
82 Console.WriteLine(((rrd_info_t)info).value.u_cnt);
84 case rrd_info_type_t.RD_I_VAL:
85 Console.WriteLine(((rrd_info_t)info).value.u_val);
87 case rrd_info_type_t.RD_I_BLO:
88 Console.WriteLine(" ** BLOB ** ");
91 Console.WriteLine("**** Unknown type! ****");
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));
100 Console.WriteLine("Test Info: Successful!");
101 //Console.WriteLine("Printing information...");
102 //Info_Print(((rrd_info_t)info));
104 static void Test_Update()
106 Console.WriteLine("Updating RRD...");
107 ArrayList al = new ArrayList();
109 // set to false if you want to use random values
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");
130 UInt32 ts = 920804700;
131 for (int i = 0; i < 15; i++)
133 al.Add(ts.ToString() + ":" + rrd.Random());
137 int ret = rrd.Update(path + "test_a.rrd", null, (string[])al.ToArray(typeof(string)));
139 Console.WriteLine("Error: " + rrd.Get_Error());
141 Console.WriteLine("Test update: Successful!");
143 static void Test_Fetch()
146 Console.WriteLine("Attempting Fetch...");
147 ArrayList al = new ArrayList();
149 al.Add(path + "test_a.rrd");
155 IntPtr data = new IntPtr();
156 string[] rrds = new string[0];
161 int ret = rrd.Fetch((string[])al.ToArray(typeof(string)), ref start, ref end,
162 ref step, ref dscnt, ref rrds, ref data);
166 for (Int32 ti = start + (Int32)step; ti <= end; ti += (Int32)step)
168 Console.Write(ti + ": ");
169 for (Int32 i = 0; i < (Int32)dscnt; i++)
171 Console.Write(((double)Marshal.PtrToStructure(data, typeof(double))).ToString(" 0.0000000000e+00"));
172 data = new IntPtr(data.ToInt64() + sizeof(double));
174 Console.Write(Environment.NewLine);
179 Console.WriteLine("Error: " + rrd.Get_Error());
181 Console.WriteLine("Test fetch: Successful!");
183 static void Test_Graph()
185 Console.WriteLine("Creating graph...");
186 ArrayList al = new ArrayList();
188 al.Add(path + "graph_simple.png");
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)));
197 Console.WriteLine("Error: " + rrd.Get_Error());
199 Console.WriteLine("Test graph: Successful!");
201 static void Test_Graph_Math()
203 Console.WriteLine("Creating graph...");
204 ArrayList al = new ArrayList();
206 al.Add(path + "graph_math.png");
211 al.Add("--vertical-label");
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)));
218 Console.WriteLine("Error: " + rrd.Get_Error());
220 Console.WriteLine("Test graph: Successful!");
222 static void Test_Graph_Math2()
224 Console.WriteLine("Creating graph...");
225 ArrayList al = new ArrayList();
227 al.Add(path + "graph_math2.png");
232 al.Add("--vertical-label");
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)));
243 Console.WriteLine("Error: " + rrd.Get_Error());
245 Console.WriteLine("Test graph: Successful!");
247 static void Test_Graph_Math3()
249 Console.WriteLine("Creating graph...");
250 ArrayList al = new ArrayList();
252 al.Add(path + "graph_math3.png");
257 al.Add("--vertical-label");
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)));
271 Console.WriteLine("Error: " + rrd.Get_Error());
273 Console.WriteLine("Test graph: Successful!");
275 static void Test_First_Last()
277 Console.WriteLine("Testing values...");
278 Console.WriteLine("First Value: " + rrd.First(path + "test_a.rrd", 0));
279 string err = rrd.Get_Error();
281 Console.WriteLine("Error: " + err);
282 Console.WriteLine("Last Value: " + rrd.Last(path + "test_a.rrd", 0));
283 err = rrd.Get_Error();
285 Console.WriteLine("Error: " + err);
287 Int32 last_update = 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!");
295 static void Test_Dump()
297 Console.WriteLine("Dumping RRD...");
298 int ret = rrd.Dump(path + "test_a.rrd", path + "test_a.xml");
300 Console.WriteLine("Error: " + rrd.Get_Error());
302 Console.WriteLine("Test Dump: Successful!");
304 static void Test_Xport()
306 Console.WriteLine("Exporting RRD...");
307 ArrayList al = new ArrayList();
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];
321 int ret = rrd.Xport((string[])al.ToArray(typeof(string)), ref start, ref end,
322 ref step, ref col_cnt, ref legends, ref data);
326 for (Int32 ti = start + (Int32)step; ti <= end; ti += (Int32)step)
328 Console.Write(ti + ": ");
329 for (Int32 i = 0; i < (Int32)col_cnt; i++)
331 Console.Write(((double)Marshal.PtrToStructure(data, typeof(double))).ToString(" 0.0000000000e+00"));
332 data = new IntPtr(data.ToInt64() + sizeof(double));
334 Console.Write(Environment.NewLine);
338 Console.WriteLine("Error: " + rrd.Get_Error());
340 Console.WriteLine("Test xport: Successful!");
342 static void Test_Restore()
344 Console.WriteLine("Restoring RRD...");
345 ArrayList al = new ArrayList();
347 al.Add(path + "test_a.xml");
348 al.Add(path + "restored_a.rrd");
349 int ret = rrd.Restore((string[])al.ToArray(typeof(string)));
351 Console.WriteLine("Error: " + rrd.Get_Error());
353 Console.WriteLine("Test restore: Successful!");
355 static void Test_Tune()
357 Console.WriteLine("Tuning RRD...");
358 ArrayList al = new ArrayList();
360 al.Add(path + "restored_a.rrd");
363 int ret = rrd.Tune((string[])al.ToArray(typeof(string)));
365 Console.WriteLine("Error: " + rrd.Get_Error());
367 Console.WriteLine("Test tune: Successful!");