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

use RRDs;

# define location of rrdtool databases
my $rrd = '/var/lib/rrd';
# define location of images
my $img = '/usr/local/apache/htdocs/rrd';

# process data for each specified HDD (add/delete as required)
#&ProcessHDD("hda", "primary master");
#&ProcessHDD("hdb", "primary slave");
#&ProcessHDD("hdc", "40GB Seagate");
&ProcessHDD("sg1", "500GB WD System RAID1");
&ProcessHDD("sg2", "500GB WD System RAID1");


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/local/sbin/hddtemp -n /dev/$_[0]`;
	my $temp=`/root/bin/hdd-temp /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", 300,
			"DS:temp:GAUGE:600:0:100",
			"RRA:AVERAGE:0.5:1:576",
			"RRA:AVERAGE:0.5:6:672",
			"RRA:AVERAGE:0.5:24:732",
			"RRA:AVERAGE:0.5:144:1460";
	}

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

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

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

	RRDs::graph "$img/hddtemp-$_[1].png",
		"--lazy",
		"-s -1$_[1]",
		"-t hdd temperature :: hard disk drives",
		"-h", "80", "-w", "600",
		"-a", "PNG",
		"-v degrees C",
		"--slope-mode",
		"DEF:sg1=$rrd/sg1.rrd:temp:AVERAGE",
		"DEF:sg2=$rrd/sg2.rrd:temp:AVERAGE",
		"LINE2:sg1#00FF00:500GB WD-1 System RAID1		",
		"GPRINT:sg1:MIN:  Min\\: %2.1lf",
		"GPRINT:sg1:MAX: Max\\: %2.1lf",
		"GPRINT:sg1:AVERAGE: Avg\\: %2.1lf",
		"GPRINT:sg1:LAST: Current\\: %2.lf degrees C\\n",
		"LINE2:sg2#CC00CC:500GB WD-2 System RAID1		",
		"GPRINT:sg2:MIN:  Min\\: %2.1lf",
		"GPRINT:sg2:MAX: Max\\: %2.1lf",
		"GPRINT:sg2:AVERAGE: Avg\\: %2.1lf",
		"GPRINT:sg2:LAST: Current\\: %2.lf degrees C\\n";
	if ($ERROR = RRDs::error) { print "$0: unable to generate $_[1] graph: $ERROR\n"; }
}
