Click here to Skip to main content
6,593,923 members and growing! (12,503 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate License: The GNU General Public License (GPL)

"C# Hooks For RRDtool"

By Mike Corley

C# (.Net and Mono) library provider for RRDtool
C# (C# 1.0, C# 2.0, C# 3.0), .NET (.NET 2.0, .NET 3.0, .NET 3.5), Visual Studio (VS2008), Dev
Posted:22 Aug 2008
Views:20,545
Bookmarked:13 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
20 votes for this article.
Popularity: 5.59 Rating: 4.30 out of 5
1 vote, 5.0%
1
1 vote, 5.0%
2

3
4 votes, 20.0%
4
14 votes, 70.0%
5
rrdtool-logo-light.png

Introduction

NHawk is an initiative to provide a thin, complete RRDtool provider for the .Net and Mono framework. The project goal is targeted at providing a C# interface to native RRDtool facilities through appropriate .Net / Mono object model semantics with the intent of retaining an intuitive syntax that fits naturally into the .Net / Mono paradigm and closely resembles that of native RRDtool. In doing so, NHawk seeks to provide a powerful time series analysis toolkit library that fills a big void in the .Net and Mono framework. It is our hope to provide a toolkit that promotes high productively, robustness, and expressiveness; enabling proficient C# developers to build code interchangeably (and intuitively) with native RRDtool script. It is a NHawk goal to promote productive use by allowing clients to refer solely to native RRDtool documentation resources, and thereby carry a very short learning curve (in most cases if the developer is familiar with both RRDtool and C#). The example code snippets that follow will hopefully illustrate the basic concepts. Please make particular note of syntactic similarities, and pay special attention to the section highlighting serialization, deserialization, and deepcloning, as these comprise powerful architectural utility which include support for web service oriented applications as well as for generation of, and interoperation with existing (native) RRDtool scripts.

Background

The examples that follow are taken from an expanded tutorial which has been included along with the version 1.0 release of the source code and binaries.

Please note: that the example material that follows is derived from a tutorial created initially by: Alex van den Bogaerdt <alex@ergens.op.het.net> which can be viewed at: http://oss.oetiker.ch/rrdtool/tut/rrdtutorial.en.html

RRDtool is written by Tobias Oetiker <tobi@oetiker.ch> http://oss.oetiker.ch/rrdtool/index.en.html

*** See the end of this document for contact info. and information

pertaining to development and status of the current code revision ***

Using the code

RRDtool Syntax for defining and creating RRDs

rrdtool create test.rrd --start 920804400
DS:speed:COUNTER:600:U:U 
RRA:AVERAGE:0.5:1:24
RRA:AVERAGE:0.5:6:10 

NHawk (C#) Syntax for defining and creating RRDs

RRD rrd1 = new RRD("test.rrd", 920804400);
rrd1.addDS(new DS("speed",  DS.TYPE.COUNTER, 600, DS.U, DS.U));
rrd1.addRRA(new RRA(RRA.CF.AVERAGE, 0.5, 1, 24));
rrd1.addRRA(new RRA(RRA.CF.AVERAGE, 0.5, 6, 10));
rrd1.create(true);

RRDtool Update Syntax

rrdtool update test.rrd 920804700:12345

NHawk (C#) Update Syntax

NHawk provides a means to update an RRD by packing expected DS (data source) arguments into an array of type object or string. One of the following C# method overloads should be used.

Note: the version with type object is explictly casted (internally) to string as the update command is serialized and sent to rrdtool for processing. The intent is to save you from having to explictly cast (whenever possible). When not possible you’ll have use the string version. Aside from that, both methods are equivalent.

rrd1.update(920804700, new object[] { 12345 });
rrd1.update(920804700, new string[] { “12345” });
string[] args = new string[1];
args[0] = “12345”;
rrd1.update(920804700, args);

The (current) serialized update string can be retrieved by calling: “rrd1.CurrentUpdateStr” as in the example below:

Console.WriteLine("{0}", rrd1.CurrentUpdateStr);

Resulting in the following console output…

rrdtool update test.rrd 920804700:12345

Note: “CurrentUpdateStr” should be called after each call to:

“rrd1.update(…)”, because serialization is done there.

RRDtool Graph Syntax

 rrdtool graph speed4.png                           
--start 920804400 --end 920808000               
--vertical-label km/h                           
DEF:myspeed=test.rrd:speed:AVERAGE              
"CDEF:kmh=myspeed,3600,*"                       
CDEF:fast=kmh,100,GT,100,0,IF                   
CDEF:over=kmh,100,GT,kmh,100,-,0,IF             
CDEF:good=kmh,100,GT,0,kmh,IF                   
HRULE:100#0000FF:"Maximum allowed"              
AREA:good#00FF00:"Good speed"                   
AREA:fast#550000:"Too fast"                     
STACK:over#FF0000:"Over speed"

NHawk(C#) Syntax Graph Syntax

GRAPH gr4 = new GRAPH("speed4.png", "920804400", "920808000");
gr4.yaxislabel = "km/h";
gr4.addDEF(new DEF("myspeed", rrd1, "speed", RRA.CF.AVERAGE));
gr4.addCDEF(new CDEF("kmh", "myspeed,3600,*"));
gr4.addCDEF(new CDEF("fast", "kmh,100,GT,kmh,0,IF"));
gr4.addCDEF(new CDEF("over", "kmh,100,GT,kmh,100,-,0,IF"));
gr4.addCDEF(new CDEF("good", "kmh,100,GT,0,kmh,IF"));
gr4.addGELEM(new HRULE("100", Color.Blue, "Maximum allowed"));
gr4.addGELEM(new AREA("good", Color.Green, "Good speed"));
gr4.addGELEM(new AREA("fast", Color.Brown, "Too fast"));
gr4.addGELEM(new AREA("over", Color.Red, "Over speed",true));
gr4.graph();

speed4.png

Note: the NHAWK code line:
gr4.addGELEM(new AREA("over", Color.Red, "Over speed",true));
is semantically equivalent to:
STACK:over#FF0000:"Over speed"

NHAWK is using the newer syntactic constructs, as the STACK
construct as a direct graphing element is deprecated as of RRDTool 1.3

Architectural Features: Serialization and Deserialization and Deepcloning

All NHAWK constructs are built as composite structures, in which a contract states that each construct must be able to serialize, deserialize, and deepclone itself (see the UML architecture diagram available with the source download). In other words, a higher level construct (such as the GRAPH class) would serialize itself by asking all of its composed constructs to serialize themselves. This is a powerful feature in that it opens up a whole new level of functionality. This includes the generation and interoperation with (native) RRDTool script, as well as ease of use with service orientated architectures such as web service applications. Below we illustrate with an example for the GRAPH class.

GRAPH gr5 = new GRAPH(gr4.ToString(), ".\\"); 
Console.WriteLine("{0}", gr5.ToString());

The above code snippet performs the following operations
1) graph object g4 is serialized and passed to the GRAPH class
deserialization (promotion) constructor.

2) graph object g5 is instantiated by deserialing the serialized
(string) form of object g4.

3) object g5 is serialized and written to the console as shown
below…

graph speed4.png --start 920804400 --end 920808000
--vertical-label "km/h"
DEF:myspeed=test.rrd:speed:AVERAGE
CDEF:kmh=myspeed,3600,*
CDEF:fast=kmh,100,GT,kmh,0,IF
CDEF:over=kmh,100,GT,kmh,100,-,0,IF
CDEF:good=kmh,100,GT,0,kmh,IF
HRULE:100#0000FF:"Maximum allowed"
AREA:good#008000:"Good speed"
AREA:fast#A52A2A:"Too fast"
AREA:over#FF0000:"Over speed"

DeepCloning and why NHawk has it

GRAPH gr6 = GRAPH.DeepClone(gr5);
Console.WriteLine("\n{0}", gr6.ToString());

Above object gr6 is a (new memory) copy of gr5. Essentially, NHawk is really just a whole load of parsing, and therefore every aggregated class member is either a string or a value type. Value types are deep copied by default, and although string is a reference type, NHawk uses temporary StringBuilders to return newly constructed (new memory) string objects. This could prove useful in many circumstances as it provides for limited preservation of object identity in a object model (shallow reference) which works on premise of not having object identity. Finally, just for completeness the below code snippet illustrates the usual shallow copy.

GRAPH gr7 = gr6;

Current code base progress: revision: 1.0

DEF (Graph Definitions) == 100%
CDEF (Graph Calc Definitions) == 100%

RRD (Defining, creating, and loading existing Round Robin Database files).
DS (data source) == 100%
RRA (round robin archives) == 100%

GRAPHING ELEMENTS
LINE == 100%
AREA == 100%
VRULE == 100%
HRULE == 100%
SHIFT == 100%

GRAPH == 90% (approx. needs VDEF, PRINT Elements)

Current TODO List (for features I plan to implement next).
Timeline: 2-4 weeks. Note: once the list below is completed, tested and added what’s already be done, then the vast majority of the major rrdtool features will be complete. This is a very do-able project.
-- VDEF
-- PRINT Elements
-- RRDFetch

Compilation instructions and library usage

-- See the user manual included with the source download.

Getting Started

For a quick start, see the tutorial (included with the source) located in: "NHawk\tutorials\rrdtutorial.en.html"
The source code can be compiled and executed from folder: "NHawk\tutorials\rrdtutorial1_code"

RRDtool can be downloaded from : http://oss.oetiker.ch/rrdtool/pub/
A good site for Windows and Netware Binaries: http://www.gknw.net/mirror/rrdtool/
(latest == rrdtool-1.2.28-bin-w32.zip)


Author Contact Info.

Michael Corley
mwcorley79@gmail.com

History

22 August 08 -- Posted revision 1.0

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPL)

About the Author

Mike Corley


Member
8+ years experience developing software in C/C++ and .Net. Education background includes computer science and computer engineering. Currently completing a MSCE thesis at Syracuse University. Past times include riding the Harley, watching Football, exercise, and too much else to list. Feel free to email me with questions/comments: mwcorley79@gmail.com
Occupation: Software Developer (Senior)
Company: Oasis Systems, Inc.
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 35 (Total in Forum: 35) (Refresh)FirstPrevNext
GeneralIssues using NHawk under Ubuntu and OpenSuse Pinmemberccdr5:44 10 Sep '09  
GeneralRe: Issues using NHawk under Ubuntu and OpenSuse PinmemberMike Corley6:34 10 Sep '09  
AnswerHiding the command prompts PinmemberCylindricalMark1:10 8 Sep '09  
GeneralRe: Hiding the command prompts PinmemberMike Corley4:02 8 Sep '09  
GeneralRe: Hiding the command prompts PinmemberCylindricalMark5:22 8 Sep '09  
Generalcouple of newby questions Pinmembertbhanson7:22 3 Jun '09  
GeneralRe: couple of newby questions PinmemberMike Corley8:28 3 Jun '09  
GeneralRe: couple of newby questions Pinmembertbhanson5:11 4 Jun '09  
GeneralRe: couple of newby questions PinmemberMike Corley8:24 4 Jun '09  
GeneralRe: couple of newby questions Pinmembertbhanson2:18 5 Jun '09  
GeneralRe: couple of newby questions PinmemberMike Corley8:17 5 Jun '09  
GeneralRe: couple of newby questions Pinmembertbhanson4:42 8 Jun '09  
GeneralRe: couple of newby questions Pinmembertbhanson4:53 8 Jun '09  
GeneralRe: couple of newby questions Pinmembertbhanson5:32 8 Jun '09  
GeneralRe: couple of newby questions PinmemberMike Corley9:22 8 Jun '09  
GeneralRe: couple of newby questions PinmemberMike Corley9:31 8 Jun '09  
GeneralRe: couple of newby questions Pinmembertbhanson4:56 10 Jun '09  
QuestionIs RRDFetch implemented? PinmemberMarcel Pletosu5:56 22 Apr '09  
AnswerRe: Is RRDFetch implemented? PinmemberMike Corley10:43 22 Apr '09  
GeneralError running tutorial (Win7 / VS2008 / RRDTool 1.2.30) Pinmemberamg19765:33 1 Apr '09  
GeneralRe: Error running tutorial (Win7 / VS2008 / RRDTool 1.2.30) PinmemberMike Corley13:12 1 Apr '09  
GeneralRe: Error running tutorial (Win7 / VS2008 / RRDTool 1.2.30) Pinmemberamg19762:04 2 Apr '09  
QuestionMilliseconds PinmemberDeveloper1286:27 24 Mar '09  
AnswerRe: Milliseconds PinmemberMike Corley18:15 30 Mar '09  
QuestionCould not load RRD file: test.rrd version: 0003 PinmemberMiZaFin22:10 10 Feb '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Aug 2008
Editor: Sean Ewington
Copyright 2008 by Mike Corley
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project