Saturday, April 28, 2007

rrdtool for graphing hdd temp





this was a real prick of a thing to install. The ubuntu package did not work. Had to install by hand. must remember the make install_perl command.......


Somewhere in this list of commands lies the answer that made it work. Next time I install it I'll sort this out...

wget http://oss.oetiker.ch/rrdtool/pub/rrdtool-1.2.19.tar.gz
tar -xvf rrdtool-1.2.19.tar.gz
cd rrdtool-1.2.19
cat doc/rrdbuild.txt
BUILD_DIR=/home/sgoggin/build/rrdtool-1.2.19
210 sudo BUILD_DIR=/tmp/rrdbuild
211 INSTALL_DIR=/usr/local/rrdtool-1.2.12
212 BUILD_DIR=/tmp/rrdbuild
213 sudo mkdir /tmp/rrdbuild
222 wget http://oss.oetiker.ch/rrdtool/pub/libs/libart_lgpl-2.3.17.tar
226 wget http://oss.oetiker.ch/rrdtool/pub/libs/libart_lgpl-2.3.17.tar.gz
243 cd rrdtool-1.2.19
275 cd rrdtool-1.2.19
283 sudo ln -sf /usr/local/rrdtool-1.2.12/bin/rrdtool /usr/bin/rrdtool
284 sudo ln -sf /usr/local/rrdtool-1.2.12/bin/rrdupdate /usr/bin/rrdupdate
285 sudo ln -sf /usr/local/rrdtool-1.2.12/bin/rrdcgi /usr/bin/rrdcgi
286 sudo mkdir /var/lib/rrd
287 sudo mkdir /var/www/html/rrdtool
290 sudo mkdir /var/www/html/rrdtool
292 /usr/bin/rrdtool
307 sudo ln -sf /usr/local/rrdtool-1.2.12/bin/rrdtool /usr/bin/rrdtool
308 sudo ln -sf /usr/local/rrdtool-1.2.19/bin/rrdtool /usr/bin/rrdtool
309 sudo ln -sf /usr/local/rrdtool-1.2.19/bin/rrdupdate /usr/bin/rrdupdate
310 sudo ln -sf /usr/local/rrdtool-1.2.19/bin/rrdcgi /usr/bin/rrdcgi
316 rrdtool
330 ls /usr/local/rrdtool-1.2.19/lib/perl/5.8.8/i486-linux-gnu-thread-multi/
331 cat /usr/local/rrdtool-1.2.19/lib/perl/5.8.8/i486-linux-gnu-thread-multi/auto/
332 ls /usr/local/rrdtool-1.2.19/lib/perl/5.8.8/i486-linux-gnu-thread-multi/auto
340 sudo ln -s /usr/local/rrdtool-1.2.19/lib/perl/5.8.8/i486-linux-gnu-thread-multi/RRDs.pm .
342 sudo ln -s /usr/local/rrdtool-1.2.19/lib/perl/5.8.8/i486-linux-gnu-thread-multi/* .

Script to create the initial db from my hddtemp file

#!/usr/bin/perl
#
# copyright Martin Pot 2003
# http://martybugs.net/linux/hddtemp.cgi
#
# rrd_hddtemp.pl

use lib qw(/usr/local/rrdtool-1.2.19/lib/perl);
use RRDs;



# define location of rrdtool databases
my $rrd = '/var/lib/rrd';
# define location of images
my $img = '/var/www/html/rrdtool';

# process data for each specified HDD (add/delete as required)

&ProcessHDD("sda", "IDE Array d1");
&ProcessHDD("sdb", "IDE Array d2");
&ProcessHDD("sdf", "SATA Array d1");
&ProcessHDD("sdg", "SATA Array d2");


sub ProcessHDD
{
# process HDD
# inputs: $_[0]: hdd (ie, hda, etc)
# $_[1]: hdd description

# Open the hddtemps
open (hddtemps,"/var/log/hddtemps") || die("Could not open file!");

# Read data into array
@hddtempdata = ;

# if rrdtool database doesn't exist, create it
if (! -e "$rrd/$_[0].rrd")
{
print "creating rrd database for /dev/$_[0]...\n";
RRDs::create "$rrd/$_[0].rrd",
"-s 600",
"-b 1175694002",
"DS:temp:GAUGE:660:0:100",
"RRA:AVERAGE:0.5:1:1008",
"RRA:AVERAGE:0.5:3:1344",
"RRA:AVERAGE:0.5:12:2190",
"RRA:AVERAGE:0.5:72:1460";
}


# Loop through the file
foreach $hddtempline (@hddtempdata)
{
# Print out data
# Read the date
$sampledate = substr ($hddtempline,0,20);
# Convert to epoch
my $epochdate=`/bin/date --date='$sampledate' +%s`;
$epochdate =~ s/[\n ]//g;

# Read the temp
$sampletemp = substr ($hddtempline,21,2);
# print out insert data
print " Inserting $sampletemp at time $epochdate\n";
$checkdate = `/bin/date --date='$sampledate'`;
print " $sampledate --> $checkdate ";
# Insert Data into db

# insert value into rrd
RRDs::update "$rrd/$_[0].rrd",
"-t", "temp",
"$epochdate:$sampletemp";

my $ERR=RRDs::error;
die "ERROR while updating mydemo.rrd: $ERR\n" if $ERR;


}

# # create graphs
&CreateGraph($_[0], "day", $_[1]);
&CreateGraph($_[0], "week", $_[1]);
&CreateGraph($_[0], "month", $_[1]);
&CreateGraph($_[0], "year", $_[1]);
}

sub CreateGraph
{
# creates graph
# inputs: $_[0]: hdd name (ie, hda, etc)
# $_[1]: interval (ie, day, week, month, year)
# $_[2]: hdd description

RRDs::graph "$img/$_[0]-$_[1].png",
"--lazy",
"-s -1$_[1]",
"-t hdd temperature :: $_[2] (/dev/$_[0])",
"-h", "80", "-w", "600",
"-a", "PNG",
"-v degrees C",
"DEF:temp=$rrd/$_[0].rrd:temp:AVERAGE",
"LINE2:temp#0000FF:$_[2] (/dev/$_[0])",
"GPRINT:temp:MIN: Min\\: %2.lf",
"GPRINT:temp:MAX: Max\\: %2.lf",
"GPRINT:temp:AVERAGE: Avg\\: %4.1lf",
"GPRINT:temp:LAST: Current\\: %2.lf degrees C\\n";
if ($ERROR = RRDs::error) { print "$0: unable to generate $_[0] graph: $ERROR\n"; }
}

Script to create the graphs
#!/usr/bin/perl
#
# copyright Martin Pot 2003
# http://martybugs.net/linux/hddtemp.cgi
#
# rrd_hddtemp.pl

use lib qw(/usr/local/rrdtool-1.2.19/lib/perl);
use RRDs;

# define location of rrdtool databases
my $rrd = '/var/lib/rrd';
# define location of images
my $img = '/var/www/html/rrdtool';

# process data for each specified HDD (add/delete as required)
&importHDD("sda", "IDE Array a");

sub importHDD
{
# process HDD
# inputs: $_[0]: hdd (ie, hda, etc)
# $_[1]: hdd description


# # create graphs
&CreateGraph($_[0], "day", $_[1]);
&CreateGraph($_[0], "week", $_[1]);
&CreateGraph($_[0], "month", $_[1]);
&CreateGraph($_[0], "year", $_[1]);
}

sub CreateGraph
{
# creates graph
# inputs: $_[0]: hdd name (ie, hda, etc)
# $_[1]: interval (ie, day, week, month, year)
# $_[2]: hdd description

RRDs::graph "$img/$_[0]-$_[1]_comparison.png",
"--lazy",
"-s -1$_[1]",
"-t hdd temperature :: $_[2] (/dev/$_[0])",
"-h", "180", "-w", "900",
"-a", "PNG",
"-v degrees C",
"DEF:temp1=$rrd/sda.rrd:temp:AVERAGE",
"DEF:temp2=$rrd/sdb.rrd:temp:AVERAGE",
"DEF:temp3=$rrd/sdg.rrd:temp:AVERAGE",
"DEF:temp4=$rrd/sdf.rrd:temp:AVERAGE",
"LINE1:temp1#FF00FF:sda",
"LINE1:temp2#00FF00:sdb",
"LINE1:temp3#666600:sdf",
"LINE1:temp4#00AAAA:sdg",
"GPRINT:temp1:MIN: Min\\: %2.lf",
"GPRINT:temp1:MAX: Max\\: %2.lf",
"GPRINT:temp1:AVERAGE: Avg\\: %4.1lf",
"GPRINT:temp1:LAST: Current\\: %2.lf degrees C\\n";
if ($ERROR = RRDs::error) { print "$0: unable to generate $_[0] graph: $ERROR\n"; }
}




Script to do the samples

The script lives in the 10 minute crontab directory
sgoggin@Orangutang:/etc/cron.10min$ cat rddhddtemp
#!/usr/bin/perl
#
# copyright Martin Pot 2003
# http://martybugs.net/linux/hddtemp.cgi
#
# rrd_hddtemp.pl

use lib qw(/usr/local/rrdtool-1.2.19/lib/perl);
use RRDs;

# define location of rrdtool databases
my $rrd = '/var/lib/rrd';
# define location of images
my $img = '/var/www/html/rrdtool';

# process data for each specified HDD (add/delete as required)
&ProcessHDD("sda", "IDE Array d1");
&ProcessHDD("sdb", "IDE Array d2");
&ProcessHDD("sdf", "SATA Array d1");
&ProcessHDD("sdg", "SATA Array d2");

sub ProcessHDD
{
# process HDD
# inputs: $_[0]: hdd (ie, hda, etc)
# $_[1]: hdd description

# get hdd temp for master drive on secondary IDE channel
my $temp=`/usr/sbin/hddtemp -n /dev/$_[0]`;
# remove eol chars and white space
$temp =~ s/[\n ]//g;

print "$_[1] (/dev/$_[0]) temp: $temp degrees C\n";

# if rrdtool database doesn't exist, create it
if (! -e "$rrd/$_[0].rrd")
{
print "creating rrd database for /dev/$_[0]...\n";
RRDs::create "$rrd/$_[0].rrd",
"-s 660",
"DS:temp:GAUGE:600:0:100",
"RRA:AVERAGE:0.5:1:1008",
"RRA:AVERAGE:0.5:3:1344",
"RRA:AVERAGE:0.5:12:2190",
"RRA:AVERAGE:0.5:72:1460";
}

# insert value into rrd
RRDs::update "$rrd/$_[0].rrd",
"-t", "temp",
"N:$temp";

my $ERR=RRDs::error;
die "ERROR while updating /dev/$_[0]...: $ERR\n" if $ERR;


# Update Syslog
$xyz=`/usr/bin/logger -t rddhddtemp.pl hdd temp for $_[0] is $temp`;

}

sub CreateGraph
{
# creates graph
# inputs: $_[0]: hdd name (ie, hda, etc)
# $_[1]: interval (ie, day, week, month, year)
# $_[2]: hdd description

RRDs::graph "$img/$_[0]-$_[1].png",
"--lazy",
"-s -1$_[1]",
"-t hdd temperature :: $_[2] (/dev/$_[0])",
"-h", "80", "-w", "600",
"-a", "PNG",
"-v degrees C",
"DEF:temp=$rrd/$_[0].rrd:temp:AVERAGE",
"LINE2:temp#0000FF:$_[2] (/dev/$_[0])",
"GPRINT:temp:MIN: Min\\: %2.lf",
"GPRINT:temp:MAX: Max\\: %2.lf",
"GPRINT:temp:AVERAGE: Avg\\: %4.1lf",
"GPRINT:temp:LAST: Current\\: %2.lf degrees C\\n";
if ($ERROR = RRDs::error) { print "$0: unable to generate $_[0] graph: $ERROR\n"; }
}

sgoggin@Orangutang:/etc/cron.10min$




Gotcha's


I sample data every 10 mins so a sample interval of 600 secs sounds right . BUT if the cron job is even 3 seconds late then 50% of the time the sample is lost. So if you set it to 660 seconds it is OK.

if (! -e "$rrd/$_[0].rrd")
{
print "creating rrd database for /dev/$_[0]...\n";
RRDs::create "$rrd/$_[0].rrd",
"-s 660",
"-b 1175694002",
"DS:temp:GAUGE:660:0:100",
"RRA:AVERAGE:0.5:1:1008",
"RRA:AVERAGE:0.5:3:1344",
"RRA:AVERAGE:0.5:12:2190",
"RRA:AVERAGE:0.5:72:1460";
}

No comments: