#! /usr/bin/perl -w

use strict;
use RRDs;

sub create_rrd_db ($$;);

my $db = "/var/log/monitoring/loadavg/loadavg.rrd";
my $step = 15;

&create_rrd_db($db, $step);
my $error = RRDs::error;
die "Could not create RRD db file $db: $error\n" if $error;

my $now = time;

# Start gathering stats on a 30 second boundary

select(undef, undef, undef, 30 - $now % 30);

while (1)
{
    my $one;
    my $five;
    my $fifteen;
    my $running;
    my $tasks;

    open(LOADAVG, "/proc/loadavg");

    while (<LOADAVG>)
    {
	chomp;

	($one, $five, $fifteen, $running, undef) = split(/\s+/);
    }

    close LOADAVG;

    ($running, $tasks) = split(/\//, $running);

    my $update = "N:${one}:${five}:${fifteen}:${running}:${tasks}";

    RRDs::update( $db, $update);
    my $error = RRDs::error;
    die "Problem updating $db. $error\n" if $error;

    select(undef, undef, undef, $step);
}

###########################################################################

sub create_rrd_db ($$;)
{
    my $db = shift;
    my $step = shift;

    return 1 if -e $db;

    # Start time will be the default (now - 10 seconds)
    # Step time will be $step seconds.
    # All values will be GAUGE.
    # Heartbeat interval will expire after 2 * $step seconds.
    #
    # The last values will be stored.
    # Actual values will be stored for 36 hours
    # 30 minute averages will be stored for 10 days
    # 2 hour averages will be stored for 5 weeks
    # 24 hour averages will be stored for 78 weeks (1.5 years)

    my $heartbeat = $step * 2;

    my $second = 1;
    my $minute = 60 * $second;
    my $hour = 60 * $minute;
    my $day = 24 * $hour;
    my $week = 7 * $day;

    my $daily_interval = $step;			# 1 sample interval
    my $daily_period = 36 * $hour;		# 36 hours
    my $daily_samples = int($daily_interval / $step);
    my $daily_rows = int($daily_period / $daily_interval); 

    my $weekly_interval = 30 * $minute;		# 30 minutes
    my $weekly_period = 10 * $day;		# 10 days
    my $weekly_samples = int($weekly_interval / $step);
    my $weekly_rows = int($weekly_period / $weekly_interval);

    my $monthly_interval = 2 * $hour;		# 2 hours
    my $monthly_period = 5 * $week;		# 5 weeks
    my $monthly_samples = int($monthly_interval / $step);
    my $monthly_rows = int($monthly_period / $monthly_interval);

    my $yearly_interval = 1 * $day;		# 24 hours
    my $yearly_period = 78 * $week;		# 78 weeks
    my $yearly_samples = int($yearly_interval / $step);
    my $yearly_rows = int($yearly_period / $yearly_interval);

    return RRDs::create(
	    $db,
	    "--step", $step,

	    "DS:one:GAUGE:$heartbeat:0:U",
	    "DS:five:GAUGE:$heartbeat:0:U",
	    "DS:fifteen:GAUGE:$heartbeat:0:U",
	    "DS:running:GAUGE:$heartbeat:0:U",
	    "DS:tasks:GAUGE:$heartbeat:0:U",

	    "RRA:LAST:0.5:1:1",
	    "RRA:AVERAGE:0.5:$daily_samples:$daily_rows",
	    "RRA:AVERAGE:0.5:$weekly_samples:$weekly_rows",
	    "RRA:AVERAGE:0.5:$monthly_samples:$monthly_rows",
	    "RRA:AVERAGE:0.5:$yearly_samples:$yearly_rows",
	);
}
