1RRD::Simple::Examples(3U)ser Contributed Perl DocumentatiRoRnD::Simple::Examples(3)
2
3
4

NAME

6       RRD::Simple::Examples - Examples using RRD::Simple
7

EXAMPLES

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