RRD::S...xamples(3pm)

๐Ÿ“ฆ
version 1.44 ยท 2 versions
, in fedora 43

โœจ NAME โœจ

RRD::Simple::Examples โ€” examples using RRD::Simple ๐Ÿ’–

okay so bestie, this is the examples page for RRD::Simple, which is exactly what it sounds like: a whole lil collection of worked examples so you can copy-paste your way to victory. no cap, this is the good stuff. ๐ŸŽ€


๐ŸŒˆ EXAMPLES ๐ŸŒˆ

๐Ÿ“ Example 1: Basic Data Gathering Using vmstat

gathering data from vmstat and shoving it straight into an RRD. classic. here's the whole vibe:

use strict;
use RRD::Simple;

my $cmd = "/usr/bin/vmstat 2 3";
my $rrdfile = "vmstat-cpu.rrd";
my $rrd = RRD::Simple->new( file => $rrdfile );

my @keys = ();
my %update = ();
open(PH,"-|",$cmd) or die qq{Unable to open file handle PH for command "$cmd": $!};
while (local $_ = <PH>) {
    next if /---/;
    s/^\s+|\s+$//g;
    if (/\d+/ && @keys) {
        @update{@keys} = split(/\s+/,$_);
    } else { @keys = split(/\s+/,$_); }
}
close(PH) or die qq{Unable to close file handle PH for command "$cmd": $!};

my @cpukeys = splice(@keys,-4,4);
my %labels = (wa => "IO wait", id => "Idle", sy => "System", us => "User");

$rrd->create(map { ($_ => "GAUGE") } @cpukeys) unless -f $rrdfile;
$rrd->update(map { ($_ => $update{$_}) } @cpukeys);

๐ŸŒธ Example 2: Setting Minimum and Maximum Value Limits

this one is about setting the minimum value to zero on a datasource using the RRDs::tune function. use -i or --minimum to set the minimum value, and -a or --maximum to set the maximum value. โœจ

peep <http://www.rrdtool.org/rrdtool/doc/rrdtune.en.html> for more.

use strict;
use RRD::Simple;
use RRDs;

my %update = ();
my $cmd = "/usr/bin/iostat -k";

open(PH,"-|",$cmd) or die qq{Unable to open file handle PH for command "$cmd": $!};
while (local $_ = <PH>) {
    if (my ($dev,$r,$w) = $_ =~ /^([\w\d]+)\s+\S+\s+\S+\s+\S+\s+(\d+)\s+(\d+)$/) {
        $update{$dev} = { "read" => $r, "write" => $w };
    }
}
close(PH) or die qq{Unable to close file handle PH for command "$cmd": $!};

for my $dev (keys %update) {
    my $rrdfile = "iostat-$dev.rrd";
    my $rrd = RRD::Simple->new( file => $rrdfile );

    unless (-f $rrdfile) {
        $rrd->create(
                map { ($_ => "DERIVE") } sort keys %{$update{$dev}}
            );
        RRDs::tune($rrdfile, "-i", "$_:0") for keys %{$update{$dev}};
    }

    $rrd->update(%{$update{$dev}});
}

๐Ÿฆ‹ Example 3: Creating RRDs with Different Data Retention Periods

so the second parameter to the create method โ€” and it's optional, queen behavior โ€” is the data retention period. valid values are "day", "week", "month", "year", "3years" and "mrtg". the default value is "mrtg". ๐Ÿ’…

the "mrtg" data retention period uses a data stepping resolution of 300 seconds (5 minutes) and heartbeat of 600 seconds (10 minutes), whereas all the other data retention periods use a data stepping resolution of 60 seconds (1 minute) and heartbeat of 120 seconds (2 minutes). like, mrtg is built different fr fr.

use strict;
use RRD::Simple;

my $rrd = RRD::Simple->new( file => "myfile.rrd" );
my @period = qw(day week month year 3years mrtg);
$rrd->create($period[1],
        datasource1 => "GAUGE",
        datasource2 => "GAUGE",
        datasource3 => "GAUGE",
    );

๐ŸŽ€ Example 4: Drawing an Average Value Horizonal Rule on a Graph

graph parameters are preserved and should be passed through to RRDs correctly: VDEF, CDEF, DEF, GPRINT, PRINT, COMMENT, HRULE, VRULE, LINE, AREA, TICK, SHIFT and STACK. use the VDEF and HRULE parameters to draw a horizontal rule on your graph. iconic.

use strict;
use RRD::Simple;

my $rrd = RRD::Simple->new( file => "frequency.rrd" );
$rrd->create("day",
        Frequency => "GAUGE",
    );

my $end = time();
my $start = $end - (60 * 60 * 24);
my $i = 0;
my $rand = int(rand(100));

for (my $t = $start; $t <= $end; $t += 60) {
    $rrd->update($t,
            Frequency => ( cos($i += 0.01) * 100 ) + $rand,
        );
}

$rrd->graph(
        sources => [ qw(Frequency) ],
        "VDEF:FrequencyAVERAGE=Frequency,AVERAGE" => "",
        "HRULE:FrequencyAVERAGE#00ff77:Average" => "",
    );

๐Ÿ’œ Example 5: Drawing a Fixed Height Stacked Graph

no prose, just vibes and a stacked graph. here:

use strict;
use RRD::Simple;

my $rrdfile = "vmstat-cpu.rrd";
my $rrd = RRD::Simple->new( file => $rrdfile );

$rrd->graph(
        title => "CPU Utilisation",
        vertical_label => "% percent",
        upper_limit => 100,
        lower_limit => 0,
        rigid => "",
        sources => [ qw(sy us wa id) ],
        source_drawtypes => [ qw(AREA STACK STACK STACK) ],
        extended_legend => 1,
    );

๐Ÿงš Example 6: Setting Custom Graph Colours

the "color" parameter can be used to override the default colours for standard elements of the graph. valid elements are: BACK, CANVAS, SHADEA, SHADEB, GRID, MGRID, FONT, AXIS, FRAME and ARROW. see <http://oss.oetiker.ch/rrdtool/doc/rrdgraph.en.html> for further information. ๐ŸŽจ

use strict;
use RRD::Simple;

my $rrd = RRD::Simple->new( file => "vmstat-cpu.rrd" );

$rrd->graph(
        title => "CPU Utilisation",
        source_colors => {
                sy => "ff0000",
                us => "00ff00",
                wa => "0000ff",
                id => "ffffff",
            },
        color => [ ( "BACK#F5F5FF", "SHADEA#C8C8FF",
                     "SHADEB#9696BE", "ARROW#61B51B",
                     "GRID#404852", "MGRID#67C6DE" ) ],
    );

๐Ÿ‘‘ Example 7: Capacity Planning Predictions

future-telling but make it RRD::Simple. bestie is doing actual predictions here, LSLSLOPE girlies rise up. ๐Ÿ”ฎ

use strict;
use RRD::Simple 1.44;

my $rrd = RRD::Simple->new( file => "memory_usage.rrd" );

$rrd->graph(
        periods => [ qw(week month) ],
        title => "Memory Utilisation",
        base => 1024,
        vertical_label => "bytes",
        sources => [ qw(Total Used) ],
        source_drawtypes => [ qw(AREA LINE) ],
        source_colors => [ qw(dddddd 0000dd) ],
        lower_limit => 0,
        rigid => "",
        "VDEF:D=Used,LSLSLOPE" => "",
        "VDEF:H=Used,LSLINT" => "",
        "VDEF:F=Used,LSLCORREL" => "",
        "CDEF:Proj=Used,POP,D,COUNT,*,H,+" => "",
        "LINE2:Proj#800000: Projection" => "",
    );

Copyright 2005,2006,2007,2008 Nicola Worthington. ๐Ÿซถ

This software is licensed under The Apache Software License, Version 2.0.

<http://www.apache.org/licenses/LICENSE-2.0>


and that's the whole page, bestie!! ๐ŸŽ‰ this was RRD::Simple::Examples, documented as part of perl v5.42.0, dated 2025-07-25. go forth and graph, you absolute icon. ๐Ÿ’…โœจ๐ŸŒˆ

๐Ÿ“ฆ where this page comes from