1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>rrd-beginners</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:root@localhost" />
8 </head>
10 <body style="background-color: white">
12 <p><a name="__index__"></a></p>
13 <!-- INDEX BEGIN -->
14 <!--
16 <ul>
18 <li><a href="#name">NAME</a></li>
19 <li><a href="#synopsis">SYNOPSIS</a></li>
20 <li><a href="#description">DESCRIPTION</a></li>
21 <ul>
23 <li><a href="#what_makes_rrdtool_so_special">What makes RRDtool so special?</a></li>
24 <li><a href="#understanding_by_an_example">Understanding by an example</a></li>
25 <li><a href="#graphical_magic">Graphical Magic</a></li>
26 <li><a href="#wrapping_rrdtool_within_shell_perl_script">Wrapping RRDtool within Shell/Perl script</a></li>
27 <ul>
29 <li><a href="#shell_script__collects_data__updates_database_">Shell script (collects data, updates database)</a></li>
30 <li><a href="#perl_script__retrieves_data_from_database_and_generates_graphs_and_statistics_">Perl script (retrieves data from database and generates graphs and statistics)</a></li>
31 </ul>
33 </ul>
35 <li><a href="#author">AUTHOR</a></li>
36 </ul>
37 -->
38 <!-- INDEX END -->
40 <p>
41 </p>
42 <h1><a name="name">NAME</a></h1>
43 <p>rrd-beginners - RRDtool Beginners' Guide</p>
44 <p>
45 </p>
46 <hr />
47 <h1><a name="synopsis">SYNOPSIS</a></h1>
48 <p>Helping new RRDtool users to understand the basics of RRDtool</p>
49 <p>
50 </p>
51 <hr />
52 <h1><a name="description">DESCRIPTION</a></h1>
53 <p>This manual is an attempt to assist beginners in understanding the concepts
54 of RRDtool. It sheds a light on differences between RRDtool and other
55 databases. With help of an example, it explains the structure of RRDtool
56 database. This is followed by an overview of the ``graph'' feature of RRDtool.
57 At the end, it has sample scripts that illustrate the
58 usage/wrapping of RRDtool within Shell or Perl scripts.</p>
59 <p>
60 </p>
61 <h2><a name="what_makes_rrdtool_so_special">What makes RRDtool so special?</a></h2>
62 <p>RRDtool is GNU licensed software developed by Tobias Oetiker, a system
63 manager at the Swiss Federal Institute of Technology. Though it is a
64 database, there are distinct differences between RRDtool databases and other
65 databases as listed below:</p>
66 <ul>
67 <li>
68 <p>RRDtool stores data; that makes it a back-end tool. The RRDtool command set
69 allows the creation of graphs; that makes it a front-end tool as well. Other
70 databases just store data and can not create graphs.</p>
71 </li>
72 <li>
73 <p>In case of linear databases, new data gets appended at the bottom of
74 the database table. Thus its size keeps on increasing, whereas the size of
75 an RRDtool database is determined at creation time. Imagine an RRDtool
76 database as the perimeter of a circle. Data is added along the
77 perimeter. When new data reaches the starting point, it overwrites
78 existing data. This way, the size of an RRDtool database always
79 remains constant. The name ``Round Robin'' stems from this behavior.</p>
80 </li>
81 <li>
82 <p>Other databases store the values as supplied. RRDtool can be configured to
83 calculate the rate of change from the previous to the current value and
84 store this information instead.</p>
85 </li>
86 <li>
87 <p>Other databases get updated when values are supplied. The RRDtool database
88 is structured in such a way that it needs data at predefined time
89 intervals. If it does not get a new value during the interval, it stores an
90 UNKNOWN value for that interval. So, when using the RRDtool database, it is
91 imperative to use scripts that run at regular intervals to ensure a constant
92 data flow to update the RRDtool database.</p>
93 </li>
94 </ul>
95 <p>RRDtool is designed to store time series of data. With every data
96 update, an associated time stamp is stored. Time is always expressed
97 in seconds passed since epoch (01-01-1970). RRDtool can be installed
98 on Unix as well as Windows. It comes with a command set to carry out
99 various operations on RRD databases. This command set can be accessed
100 from the command line, as well as from Shell or Perl scripts. The
101 scripts act as wrappers for accessing data stored in RRDtool
102 databases.</p>
103 <p>
104 </p>
105 <h2><a name="understanding_by_an_example">Understanding by an example</a></h2>
106 <p>The structure of an RRD database is different than other linear databases.
107 Other databases define tables with columns, and many other parameters. These
108 definitions sometimes are very complex, especially in large databases.
109 RRDtool databases are primarily used for monitoring purposes and
110 hence are very simple in structure. The parameters
111 that need to be defined are variables that hold values and archives of those
112 values. Being time sensitive, a couple of time related parameters are also
113 defined. Because of its structure, the definition of an RRDtool database also
114 includes a provision to specify specific actions to take in the absence of
115 update values. Data Source (DS), heartbeat, Date Source Type (DST), Round
116 Robin Archive (RRA), and Consolidation Function (CF) are some of the
117 terminologies related to RRDtool databases.</p>
118 <p>The structure of a database and the terminology associated with it can be
119 best explained with an example.</p>
120 <pre>
121 rrdtool create target.rrd \
122 --start 1023654125 \
123 --step 300 \
124 DS:mem:GAUGE:600:0:671744 \
125 RRA:AVERAGE:0.5:12:24 \
126 RRA:AVERAGE:0.5:288:31</pre>
127 <p>This example creates a database named <em>target.rrd</em>. Start time
128 (1'023'654'125) is specified in total number of seconds since epoch
129 (time in seconds since 01-01-1970). While updating the database, the
130 update time is also specified. This update time MUST be large (later)
131 then start time and MUST be in seconds since epoch.</p>
132 <p>The step of 300 seconds indicates that database expects new values every
133 300 seconds. The wrapper script should be scheduled to run every <strong>step</strong>
134 seconds so that it updates the database every <strong>step</strong> seconds.</p>
135 <p>DS (Data Source) is the actual variable which relates to the parameter on
136 the device that is monitored. Its syntax is</p>
137 <pre>
138 DS:variable_name:DST:heartbeat:min:max</pre>
139 <p><strong>DS</strong> is a key word. <code>variable_name</code> is a name under which the parameter is
140 saved in the database. There can be as many DSs in a database as needed. After
141 every step interval, a new value of DS is supplied to update the database.
142 This value is also called Primary Data Point <strong>(PDP)</strong>. In our example
143 mentioned above, a new PDP is generated every 300 seconds.</p>
144 <p>Note, that if you do NOT supply new datapoints exactly every 300 seconds,
145 this is not a problem, RRDtool will interpolate the data accordingly.</p>
146 <p><strong>DST</strong> (Data Source Type) defines the type of the DS. It can be
147 COUNTER, DERIVE, ABSOLUTE, GAUGE. A DS declared as COUNTER will save
148 the rate of change of the value over a step period. This assumes that
149 the value is always increasing (the difference between the current and
150 the previous value is greater than 0). Traffic counters on a router
151 are an ideal candidate for using COUNTER as DST. DERIVE is the same as
152 COUNTER, but it allows negative values as well. If you want to see the
153 rate of <em>change</em> in free diskspace on your server, then you might
154 want to use the DERIVE data type. ABSOLUTE also saves the rate of
155 change, but it assumes that the previous value is set to 0. The
156 difference between the current and the previous value is always equal
157 to the current value. Thus it just stores the current value divided by
158 the step interval (300 seconds in our example). GAUGE does not save
159 the rate of change. It saves the actual value itself. There are no
160 divisions or calculations. Memory consumption in a server is a typical
161 example of gauge. The difference between the different types DSTs can be
162 explained better with the following example:</p>
163 <pre>
164 Values = 300, 600, 900, 1200
165 Step = 300 seconds
166 COUNTER DS = 1, 1, 1, 1
167 DERIVE DS = 1, 1, 1, 1
168 ABSOLUTE DS = 1, 2, 3, 4
169 GAUGE DS = 300, 600, 900, 1200</pre>
170 <p>The next parameter is <strong>heartbeat</strong>. In our example, heartbeat is 600
171 seconds. If the database does not get a new PDP within 300 seconds, it
172 will wait for another 300 seconds (total 600 seconds). If it doesn't
173 receive any PDP within 600 seconds, it will save an UNKNOWN value into
174 the database. This UNKNOWN value is a special feature of RRDtool - it
175 is much better than to assume a missing value was 0 (zero) or any
176 other number which might also be a valid data value. For example, the
177 traffic flow counter on a router keeps increasing. Lets say, a value
178 is missed for an interval and 0 is stored instead of UNKNOWN. Now when
179 the next value becomes available, it will calculate the difference
180 between the current value and the previous value (0) which is not
181 correct. So, inserting the value UNKNOWN makes much more sense here.</p>
182 <p>The next two parameters are the minimum and maximum value,
183 respectively. If the variable to be stored has predictable maximum and
184 minimum values, this should be specified here. Any update value
185 falling out of this range will be stored as UNKNOWN.</p>
186 <p>The next line declares a round robin archive (RRA). The syntax for
187 declaring an RRA is</p>
188 <pre>
189 RRA:CF:xff:step:rows</pre>
190 <p>RRA is the keyword to declare RRAs. The consolidation function (CF)
191 can be AVERAGE, MINIMUM, MAXIMUM, and LAST. The concept of the
192 consolidated data point (CDP) comes into the picture here. A CDP is
193 CFed (averaged, maximum/minimum value or last value) from <em>step</em>
194 number of PDPs. This RRA will hold <em>rows</em> CDPs.</p>
195 <p>Lets have a look at the example above. For the first RRA, 12 (steps)
196 PDPs (DS variables) are AVERAGEed (CF) to form one CDP. 24 (rows) of
197 theses CDPs are archived. Each PDP occurs at 300 seconds. 12 PDPs
198 represent 12 times 300 seconds which is 1 hour. It means 1 CDP (which
199 is equal to 12 PDPs) represents data worth 1 hour. 24 such CDPs
200 represent 1 day (1 hour times 24 CDPs). This means, this RRA is an
201 archive for one day. After 24 CDPs, CDP number 25 will replace the 1st
202 CDP. The second RRA saves 31 CDPs; each CPD represents an AVERAGE
203 value for a day (288 PDPs, each covering 300 seconds = 24
204 hours). Therefore this RRA is an archive for one month. A single
205 database can have many RRAs. If there are multiple DSs, each
206 individual RRA will save data for all the DSs in the database. For
207 example, if a database has 3 DSs and daily, weekly, monthly, and
208 yearly RRAs are declared, then each RRA will hold data from all 3 data
209 sources.</p>
210 <p>
211 </p>
212 <h2><a name="graphical_magic">Graphical Magic</a></h2>
213 <p>Another important feature of RRDtool is its ability to create
214 graphs. The ``graph'' command uses the ``fetch'' command internally to
215 retrieve values from the database. With the retrieved values it draws
216 graphs as defined by the parameters supplied on the command line. A
217 single graph can show different DS (Data Sources) from a database. It
218 is also possible to show the values from more than one database in a
219 single graph. Often, it is necessary to perform some math on the
220 values retrieved from the database before plotting them. For example,
221 in SNMP replies, memory consumption values are usually specified in
222 KBytes and traffic flow on interfaces is specified in Bytes. Graphs
223 for these values will be more meaningful if values are represented in
224 MBytes and mbps. The RRDtool graph command allows to define such
225 conversions. Apart from mathematical calculations, it is also possible
226 to perform logical operations such as greater than, less than, and
227 if/then/else. If a database contains more than one RRA archive, then a
228 question may arise - how does RRDtool decide which RRA archive to use
229 for retrieving the values? RRDtool looks at several things when making
230 its choice. First it makes sure that the RRA covers as much of the
231 graphing time frame as possible. Second it looks at the resolution of
232 the RRA compared to the resolution of the graph. It tries to find one
233 which has the same or higher better resolution. With the ``-r'' option
234 you can force RRDtool to assume a different resolution than the one
235 calculated from the pixel width of the graph.</p>
236 <p>Values of different variables can be presented in 5 different shapes
237 in a graph - AREA, LINE1, LINE2, LINE3, and STACK. AREA is represented
238 by a solid colored area with values as the boundary of this
239 area. LINE1/2/3 (increasing width) are just plain lines representing
240 the values. STACK is also an area but it is ``stack''ed on top AREA or
241 LINE1/2/3. Another important thing to note is that variables are
242 plotted in the order they are defined in the graph command. Therefore
243 care must be taken to define STACK only after defining AREA/LINE. It
244 is also possible to put formatted comments within the graph. Detailed
245 instructions can be found in the graph manual.</p>
246 <p>
247 </p>
248 <h2><a name="wrapping_rrdtool_within_shell_perl_script">Wrapping RRDtool within Shell/Perl script</a></h2>
249 <p>After understanding RRDtool it is now a time to actually use RRDtool
250 in scripts. Tasks involved in network management are data collection,
251 data storage, and data retrieval. In the following example, the
252 previously created target.rrd database is used. Data collection and
253 data storage is done using Shell scripts. Data retrieval and report
254 generation is done using Perl scripts. These scripts are shown below:</p>
255 <p>
256 </p>
257 <h3><a name="shell_script__collects_data__updates_database_">Shell script (collects data, updates database)</a></h3>
258 <pre>
259 #!/bin/sh
260 a=0
261 while [ "$a" == 0 ]; do
262 snmpwalk -c public 192.168.1.250 hrSWRunPerfMem > snmp_reply
263 total_mem=`awk 'BEGIN {tot_mem=0}
264 { if ($NF == "KBytes")
265 {tot_mem=tot_mem+$(NF-1)}
266 }
267 END {print tot_mem}' snmp_reply`
268 # I can use N as a replacement for the current time
269 rrdtool update target.rrd N:$total_mem
270 # sleep until the next 300 seconds are full
271 perl -e 'sleep 300 - time % 300'
272 done # end of while loop</pre>
273 <p>
274 </p>
275 <h3><a name="perl_script__retrieves_data_from_database_and_generates_graphs_and_statistics_">Perl script (retrieves data from database and generates graphs and statistics)</a></h3>
276 <pre>
277 #!/usr/bin/perl -w
278 # This script fetches data from target.rrd, creates a graph of memory
279 # consumption on the target (Dual P3 Processor 1 GHz, 656 MB RAM)</pre>
280 <pre>
281 # call the RRD perl module
282 use lib qw( /usr/local/rrdtool-1.0.41/lib/perl ../lib/perl );
283 use RRDs;
284 my $cur_time = time(); # set current time
285 my $end_time = $cur_time - 86400; # set end time to 24 hours ago
286 my $start_time = $end_time - 2592000; # set start 30 days in the past</pre>
287 <pre>
288 # fetch average values from the RRD database between start and end time
289 my ($start,$step,$ds_names,$data) =
290 RRDs::fetch("target.rrd", "AVERAGE",
291 "-r", "600", "-s", "$start_time", "-e", "$end_time");
292 # save fetched values in a 2-dimensional array
293 my $rows = 0;
294 my $columns = 0;
295 my $time_variable = $start;
296 foreach $line (@$data) {
297 $vals[$rows][$columns] = $time_variable;
298 $time_variable = $time_variable + $step;
299 foreach $val (@$line) {
300 $vals[$rows][++$columns] = $val;}
301 $rows++;
302 $columns = 0;
303 }
304 my $tot_time = 0;
305 my $count = 0;
306 # save the values from the 2-dimensional into a 1-dimensional array
307 for $i ( 0 .. $#vals ) {
308 $tot_mem[$count] = $vals[$i][1];
309 $count++;
310 }
311 my $tot_mem_sum = 0;
312 # calculate the total of all values
313 for $i ( 0 .. ($count-1) ) {
314 $tot_mem_sum = $tot_mem_sum + $tot_mem[$i];
315 }
316 # calculate the average of the array
317 my $tot_mem_ave = $tot_mem_sum/($count);
318 # create the graph
319 RRDs::graph ("/images/mem_$count.png", \
320 "--title= Memory Usage", \
321 "--vertical-label=Memory Consumption (MB)", \
322 "--start=$start_time", \
323 "--end=$end_time", \
324 "--color=BACK#CCCCCC", \
325 "--color=CANVAS#CCFFFF", \
326 "--color=SHADEB#9999CC", \
327 "--height=125", \
328 "--upper-limit=656", \
329 "--lower-limit=0", \
330 "--rigid", \
331 "--base=1024", \
332 "DEF:tot_mem=target.rrd:mem:AVERAGE", \
333 "CDEF:tot_mem_cor=tot_mem,0,671744,LIMIT,UN,0,tot_mem,IF,1024,/",\
334 "CDEF:machine_mem=tot_mem,656,+,tot_mem,-",\
335 "COMMENT:Memory Consumption between $start_time",\
336 "COMMENT: and $end_time ",\
337 "HRULE:656#000000:Maximum Available Memory - 656 MB",\
338 "AREA:machine_mem#CCFFFF:Memory Unused", \
339 "AREA:tot_mem_cor#6699CC:Total memory consumed in MB");
340 my $err=RRDs::error;
341 if ($err) {print "problem generating the graph: $err\n";}
342 # print the output
343 print "Average memory consumption is ";
344 printf "%5.2f",$tot_mem_ave/1024;
345 print " MB. Graphical representation can be found at /images/mem_$count.png.";</pre>
346 <p>
347 </p>
348 <hr />
349 <h1><a name="author">AUTHOR</a></h1>
350 <p>Ketan Patel <<a href="mailto:k2pattu@yahoo.com">k2pattu@yahoo.com</a>></p>
352 </body>
354 </html>