Skip to content

Instantly share code, notes, and snippets.

@nckltcha
Created March 24, 2012 14:41
Show Gist options
  • Select an option

  • Save nckltcha/2183677 to your computer and use it in GitHub Desktop.

Select an option

Save nckltcha/2183677 to your computer and use it in GitHub Desktop.
RRD Temperature Graph - DiskStation
#!/usr/bin/perl
#
# copyright Martin Pot 2003
# http://martybugs.net/linux/hddtemp.cgi
#
# Modified by myredhotcar for DiskStation DS212j
#
#
#
# rrd_hddtemp.pl
#use RRDs;
# define location for rrdtool databases
my $rrd = '/opt/bin/rrd';
# define location for graph images
my $img = '/volume1/Web';
# process data for each specified HDD (add/delete as required)
&ProcessHDD("sda", "Disk 1");
# &ProcessHDD("xxx", "Disk2");
sub ProcessHDD
{
# process HDD
# inputs: $_[0]: hdd (ie, hda, etc)
# $_[1]: hdd description
# get hdd temp for master drive on secondary IDE channel
my $txt=`smartctl -A -d sat /dev/$_[0] | grep "194 Temperature_Celsius" | awk '{print $10}'`;
$txt =~ s/[\n ]//g; # remove eol chars and white space
$re1='.*?'; # Non-greedy match on filler
$re2='\\d+'; # Uninteresting: int
$re3='.*?'; # Non-greedy match on filler
$re4='\\d+'; # Uninteresting: int
$re5='.*?'; # Non-greedy match on filler
$re6='\\d+'; # Uninteresting: int
$re7='.*?'; # Non-greedy match on filler
$re8='(\\d+)'; # Integer Number 1
$re=$re1.$re2.$re3.$re4.$re5.$re6.$re7.$re8;
if ($txt =~ m/$re/is)
{
$temp=$1;
# Debug purposes only
# print "($temp) \n";
}
# Debug purposes only
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";
`rrdtool create "$rrd/$_[0].rrd" --step 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`;
# Debug purposes for running in bash shell
# rrdtool create "$rrd/$_[0].rrd" --step 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
`rrdtool 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]);
}
sub CreateGraph
{
# creates graph
# inputs: $_[0]: hdd name (ie, hda, etc)
# $_[1]: interval (ie, day, week, month, year)
# $_[2]: hdd description
`rrdtool graph "$img/$_[0]-$_[1].png" -S "$_[1]" -t "hdd temperature" -h240 -w600 -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";`
# Debug purposes for running in bash shell
# rrdtool graph /volume1/Web/sda-day.png -S 300 -t "hdd temperature" -h240 -w600 -a PNG -v "degrees C" DEF:temp="/opt/bin/rrdtool/sda.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"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment