1RRD::Simple::Examples(3U)ser Contributed Perl DocumentatiRoRnD::Simple::Examples(3)
2
3
4
6 RRD::Simple::Examples - Examples using RRD::Simple
7
9 Example 1: Basic Data Gathering Using vmstat
10
11 use strict;
12 use RRD::Simple;
13
14 my $cmd = "/usr/bin/vmstat 2 3";
15 my $rrdfile = "vmstat-cpu.rrd";
16 my $rrd = RRD::Simple->new( file => $rrdfile );
17
18 my @keys = ();
19 my %update = ();
20 open(PH,"-⎪",$cmd) or die qq{Unable to open file handle PH for command "$cmd": $!};
21 while (local $_ = <PH>) {
22 next if /---/;
23 s/^\s+⎪\s+$//g;
24 if (/\d+/ && @keys) {
25 @update{@keys} = split(/\s+/,$_);
26 } else { @keys = split(/\s+/,$_); }
27 }
28 close(PH) or die qq{Unable to close file handle PH for command "$cmd": $!};
29
30 my @cpukeys = splice(@keys,-4,4);
31 my %labels = (wa => "IO wait", id => "Idle", sy => "System", us => "User");
32
33 $rrd->create(map { ($_ => "GAUGE") } @cpukeys) unless -f $rrdfile;
34 $rrd->update(map { ($_ => $update{$_}) } @cpukeys);
35
36 Example 2: Setting Minimum and Maximum Value Limits
37
38 This example shows how to set the minimum value to zero on a datasource
39 using the RRDs::tune function. Use "-i" or "--minimum" to set the mini‐
40 mum value, and "-a" or "--maximum" to set the maximum value.
41
42 See <http://www.rrdtool.org/rrdtool/doc/rrdtune.en.html>.
43
44 use strict;
45 use RRD::Simple;
46 use RRDs;
47
48 my %update = ();
49 my $cmd = "/usr/bin/iostat -k";
50
51 open(PH,"-⎪",$cmd) or die qq{Unable to open file handle PH for command "$cmd": $!};
52 while (local $_ = <PH>) {
53 if (my ($dev,$r,$w) = $_ =~ /^([\w\d]+)\s+\S+\s+\S+\s+\S+\s+(\d+)\s+(\d+)$/) {
54 $update{$dev} = { "read" => $r, "write" => $w };
55 }
56 }
57 close(PH) or die qq{Unable to close file handle PH for command "$cmd": $!};
58
59 for my $dev (keys %update) {
60 my $rrdfile = "iostat-$dev.rrd";
61 my $rrd = RRD::Simple->new( file => $rrdfile );
62
63 unless (-f $rrdfile) {
64 $rrd->create(
65 map { ($_ => "DERIVE") } sort keys %{$update{$dev}}
66 );
67 RRDs::tune($rrdfile, "-i", "$_:0") for keys %{$update{$dev}};
68 }
69
70 $rrd->update(%{$update{$dev}});
71 }
72
73 Example 3: Creating RRDs with Different Data Retention Periods
74
75 The second (optional) parameter to the create method is the data reten‐
76 tion period. Valid values are "day", "week", "month", "year", "3years"
77 and "mrtg". The default value is "mrtg".
78
79 The "mrtg" data retention period uses a data stepping resolution of 300
80 seconds (5 minutes) and heartbeat of 600 seconds (10 minutes), whereas
81 all the other data retention periods use a data stepping resolution of
82 60 seconds (1 minute) and heartbeat of 120 seconds (2 minutes).
83
84 use strict;
85 use RRD::Simple;
86
87 my $rrd = RRD::Simple->new( file => "myfile.rrd" );
88 my @period = qw(day week month year 3years mrtg);
89 $rrd->create($period[1],
90 datasource1 => "GAUGE",
91 datasource2 => "GAUGE",
92 datasource3 => "GAUGE",
93 );
94
95 Example 4: Drawing an Average Value Horizonal Rule on a Graph
96
97 Graph parameters are preserved and should be passed through to RRDs
98 correctly: VDEF, CDEF, DEF, GPRINT, PRINT, COMMENT, HRULE, VRULE, LINE,
99 AREA, TICK, SHIFT and STACK. Use the HRULE parameter to draw a horizon‐
100 tal rule on your graph.
101
102 use strict;
103 use RRD::Simple;
104
105 my $rrd = RRD::Simple->new( file => "frequency.rrd" );
106 $rrd->create("day",
107 Frequency => "GAUGE",
108 );
109
110 my $end = time();
111 my $start = $end - (60 * 60 * 24);
112 my $i = 0;
113 my $rand = int(rand(100));
114
115 for (my $t = $start; $t <= $end; $t += 60) {
116 $rrd->update($t,
117 Frequency => ( cos($i += 0.01) * 100 ) + $rand,
118 );
119 }
120
121 $rrd->graph(
122 sources => [ qw(Frequency) ],
123 "HRULE:FrequencyAVERAGE#00ff77:Average" => "",
124 );
125
126 Example 5: Drawing a Fixed Height Stacked Graph
127
128 use strict;
129 use RRD::Simple;
130
131 my $rrdfile = "vmstat-cpu.rrd";
132 my $rrd = RRD::Simple->new( file => $rrdfile );
133
134 $rrd->graph(
135 title => "CPU Utilisation",
136 vertical_label => "% percent",
137 upper_limit => 100,
138 lower_limit => 0,
139 rigid => "",
140 sources => [ qw(sy us wa id) ],
141 source_drawtypes => [ qw(AREA STACK STACK STACK) ],
142 extended_legend => 1,
143 );
144
145 Example 6: Setting Custom Graph Colours
146
147 The "color" parameter can be used to override the default colours for
148 standard elements of the graph. Valid elements are: BACK, CANVAS,
149 SHADEA, SHADEB, GRID, MGRID, FONT, AXIS, FRAME and ARROW. See
150 <http://oss.oetiker.ch/rrdtool/doc/rrdgraph.en.html> for further infor‐
151 mation.
152
153 use strict;
154 use RRD::Simple;
155
156 my $rrd = RRD::Simple->new( file => "vmstat-cpu.rrd" );
157
158 $rrd->graph(
159 title => "CPU Utilisation",
160 source_colors => {
161 sy => "ff0000",
162 us => "00ff00",
163 wa => "0000ff",
164 id => "ffffff",
165 },
166 color => [ ( "BACK#F5F5FF", "SHADEA#C8C8FF",
167 "SHADEB#9696BE", "ARROW#61B51B",
168 "GRID#404852", "MGRID#67C6DE" ) ],
169 );
170
172 Copyright 2005,2006,2007 Nicola Worthington.
173
174 This software is licensed under The Apache Software License, Version
175 2.0.
176
177 <http://www.apache.org/licenses/LICENSE-2.0>
178
179
180
181perl v5.8.8 2007-03-22 RRD::Simple::Examples(3)