#!/usr/bin/perl
#
# Modified from Martin Pot's 2003 script at
# http://martybugs.net/linux/hddtemp.cgi
# 
# Graphs motherboard temperatures using mbmon
# rrd_mbtemp.pl

use RRDs;

# define location of rrdtool databases
my $rrd = '/var/lib/rrd';
# define location of images
my $img = '/data/srv/httpd/htdocs/rrd';

# process data for each temperature sensor (add/delete as required)
# run mbmon -r -c 1 to see what temps your mobo supports, then make
# an educated guess about which component corresponds to which temp
# Note TEMP0 = T 1, TEMP1 = T 2, TEMP2 = T 3
&ProcessHDD("mbtemp", "T 1", "Motherboard");
&ProcessHDD("cputemp", "T 2", "CPU");
&ProcessHDD("ambtemp", "T 3", "Case ambient");

sub ProcessHDD
{
# process HDD
# inputs: $_[0]: component being measured (used for graph filenames)
#          $_[1]: code (mbmon code)
#         $_[2]: description

        # get temp value from mbmon
        my $temp=`/usr/bin/mbmon -c 1 -$_[1]`;
        # remove eol chars and white space
        $temp =~ s/[\n ]//g;
        
        print "$_[2] temp: $temp degrees C\n";

        # if rrdtool database doesn't exist, create it
        if (! -e "$rrd/$_[0].rrd")
        {
                print "creating rrd database for $_[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", $_[2]);
        &CreateGraph($_[0], "week", $_[2]);
        &CreateGraph($_[0], "month", $_[2]);
        &CreateGraph($_[0], "year", $_[2]);
}

sub CreateGraph
{
# creates graph
# inputs: $_[0]: component being measured (used for graph filenames)
#         $_[1]: interval (ie, day, week, month, year)
#         $_[2]: description

        RRDs::graph "$img/$_[0]-$_[1].png",
                "--lazy",
                "-s -1$_[1]",
                "--color", "SHADEA#EAE9EE",
                "--color", "SHADEB#EAE9EE",
                "--color", "BACK#EAE9EE",
                "-t $_[2] Temperature - $_[1]",
                "-h", "100", "-w", "600",
                "-a", "PNG",
                "-v degrees C",
                "DEF:temp=$rrd/$_[0].rrd:temp:AVERAGE",

		# This CDEF section defines a variable for every 5 degree increment of temperature 
		# between 5 and 130 degrees. Undefined/unknown variables are set to zero.
		"CDEF:tempz=temp,130,LT,temp,130,IF",
		"CDEF:tempzNoUnk=temp,UN,0,tempz,IF",
		"CDEF:tempy=temp,125,LT,temp,125,IF",
		"CDEF:tempyNoUnk=temp,UN,0,tempy,IF",
		"CDEF:tempx=temp,120,LT,temp,120,IF",
		"CDEF:tempxNoUnk=temp,UN,0,tempx,IF",
		"CDEF:tempw=temp,115,LT,temp,115,IF",
		"CDEF:tempwNoUnk=temp,UN,0,tempw,IF",
		"CDEF:tempv=temp,110,LT,temp,110,IF",
		"CDEF:tempvNoUnk=temp,UN,0,tempv,IF",
		"CDEF:tempu=temp,105,LT,temp,105,IF",
		"CDEF:tempuNoUnk=temp,UN,0,tempu,IF",
		"CDEF:tempt=temp,100,LT,temp,100,IF",
		"CDEF:temptNoUnk=temp,UN,0,tempt,IF",
		"CDEF:temps=temp,95,LT,temp,95,IF",
		"CDEF:tempsNoUnk=temp,UN,0,temps,IF",
		"CDEF:tempr=temp,90,LT,temp,90,IF",
		"CDEF:temprNoUnk=temp,UN,0,tempr,IF",
		"CDEF:tempq=temp,85,LT,temp,85,IF",
		"CDEF:tempqNoUnk=temp,UN,0,tempq,IF",
		"CDEF:tempp=temp,80,LT,temp,80,IF",
		"CDEF:temppNoUnk=temp,UN,0,tempp,IF",
		"CDEF:tempo=temp,75,LT,temp,75,IF",
		"CDEF:tempoNoUnk=temp,UN,0,tempo,IF",
		"CDEF:tempn=temp,70,LT,temp,70,IF",
		"CDEF:tempnNoUnk=temp,UN,0,tempn,IF",
		"CDEF:tempm=temp,65,LT,temp,65,IF",
		"CDEF:tempmNoUnk=temp,UN,0,tempm,IF",
		"CDEF:templ=temp,60,LT,temp,60,IF",
		"CDEF:templNoUnk=temp,UN,0,templ,IF",
		"CDEF:tempk=temp,55,LT,temp,55,IF",
		"CDEF:tempkNoUnk=temp,UN,0,tempk,IF",
		"CDEF:tempj=temp,50,LT,temp,50,IF",
		"CDEF:tempjNoUnk=temp,UN,0,tempj,IF",
		"CDEF:tempi=temp,45,LT,temp,45,IF",
		"CDEF:tempiNoUnk=temp,UN,0,tempi,IF",
		"CDEF:temph=temp,40,LT,temp,40,IF",
		"CDEF:temphNoUnk=temp,UN,0,temph,IF",
		"CDEF:tempg=temp,35,LT,temp,35,IF",
		"CDEF:tempgNoUnk=temp,UN,0,tempg,IF",
		"CDEF:tempf=temp,30,LT,temp,30,IF",
		"CDEF:tempfNoUnk=temp,UN,0,tempf,IF",
		"CDEF:tempe=temp,25,LT,temp,25,IF",
		"CDEF:tempeNoUnk=temp,UN,0,tempe,IF",
		"CDEF:tempd=temp,20,LT,temp,20,IF",
		"CDEF:tempdNoUnk=temp,UN,0,tempd,IF",
		"CDEF:tempc=temp,15,LT,temp,15,IF",
		"CDEF:tempcNoUnk=temp,UN,0,tempc,IF",
		"CDEF:tempb=temp,10,LT,temp,10,IF",
		"CDEF:tempbNoUnk=temp,UN,0,tempb,IF",
		"CDEF:tempa=temp,5,LT,temp,5,IF",
		"CDEF:tempaNoUnk=temp,UN,0,tempa,IF",

		# This section defines a color gradient for displaying the temperature data
		"AREA:tempzNoUnk#ff0000",
		"AREA:tempyNoUnk#ff0000",
		"AREA:tempxNoUnk#ff0000",
		"AREA:tempwNoUnk#ff0000",
		"AREA:tempvNoUnk#ff1b00",
		"AREA:tempuNoUnk#ff4100",
		"AREA:temptNoUnk#ff6600",
		"AREA:tempsNoUnk#ff8e00",
		"AREA:temprNoUnk#ffb500",
		"AREA:tempqNoUnk#ffdb00",
		"AREA:temppNoUnk#fdff00",
		"AREA:tempoNoUnk#d7ff00",
		"AREA:tempnNoUnk#b0ff00",
		"AREA:tempmNoUnk#8aff00",
		"AREA:templNoUnk#65ff00",
		"AREA:tempkNoUnk#3eff00",
		"AREA:tempjNoUnk#17ff00",
		"AREA:tempiNoUnk#00ff10",
		"AREA:temphNoUnk#00ff36",
		"AREA:tempgNoUnk#00ff5c",
		"AREA:tempfNoUnk#00ff83",
		"AREA:tempeNoUnk#00ffa8",
		"AREA:tempdNoUnk#00ffd0",
		"AREA:tempcNoUnk#00fff4",
		"AREA:tempbNoUnk#00e4ff",
		"AREA:tempaNoUnk#00beff",

                # Line along the top of the gradient graph
		"LINE2:temp#0000FF:$_[2] ($_[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"; }
}

