Om jag tar bort alla*.rrd filer och sen kör scriptet nedan så kommer det att komma in temp värden i graferna som det finns givare "kopplade" till.
Men om en sensor inte är ansluten så stoppas det in ett noll värde sen blir det som det ska, en följd av "nan".
Här kommer scriptet:
Kod: Markera allt
#!/usr/bin/perl
use lib qw(/usr/local/rrdtool-1.2.27/lib/perl);
use RRDs;
use HTTP::Date;
my ($date, $time) = split(" ", HTTP::Date::time2iso());
my ($hour, $min) = split(":", $time);
# define location of rrdtool databases
my $rrd = '/home/huskii/rrddata';
# define location of images
my $img = '/var/www/html/graphs/';
# process data for each devices (add/delete as required)
&ProcessSensor("10.FC0736010800", "Test, vid skrivbord");
&ProcessSensor("10.3AD85F010800", "Ute");
&ProcessSensor("10.DCF135010800", "Inne, test");
&ProcessSensor("10.982860010800", "Servern");
&ProcessSensor("10.462736010800", "Akvariet");
&ProcessSensor("10.92B694010800", "Elementet");
&ProcessSensor("10.7D0760010800", "Hallen");
#&ProcessSensor("your device id here", "temp sensor namn");
#&ProcessSensor("your device id here", "temp sensor namn");
sub ProcessSensor
{
# process sensor
# inputs: $_[0]: sensor number (ie, 0/1/2/etc)
# $_[1]: sensor description
# get temperature from sensor
my $temp = `grep . /mnt/1wire/$_[0]/temperature | sed -e s/" "//g`;
sleep(1); # test delay
# remove eol chars
chomp($temp);
if ($temp == "")
{
$temp="NAN";
print "sensor $_[0] NO VALUE\n";
}
else
{
print "sensor $_[0]: $temp Grader Celsius\n";
}
# if rrdtool database doesn't exist, create it
if (! -e "$rrd/owfs-$_[0].rrd")
{
print "creating rrd database for temp sensor $_[0]...\n";
RRDs::create "$rrd/owfs-$_[0].rrd",
"-s 300",
"DS:temp:GAUGE:600:U:U",
"RRA:AVERAGE:0.5:1:210240",
"RRA:MAX:0.5:1:210240",
"RRA:MIN:0.5:1:210240",
}
if ($ERROR = RRDs::error) { print "$0: failed to create $_[0] database file: $ERROR\n"; }
# check for error code from temp sensor
if ($temp == "85")
{
print "failed to read value from sensor $_[0] ERROR Code 85\n";
$temp="NAN";
}
elsif ($temp == "")
{
print "failed to read value from sensor $_[0] no value\n";
$temp="NAN";
}
else
{
# insert values into rrd
RRDs::update "$rrd/owfs-$_[0].rrd",
"-t", "temp",
"N:$temp";
if ($ERROR = RRDs::error) { print "$0: failed to insert $_[0] data into rrd: $ERROR\n"; }
}
# create graphs for current sensor
&CreateGraph($_[0], "day", $_[1]);
&CreateGraph($_[0], "week", $_[1]);
&CreateGraph($_[0], "month", $_[1]);
&CreateGraph($_[0], "year", $_[1]);
}
sub CreateGraph
{
# creates graph
# inputs: $_[0]: sensor number (ie, 0/1/2/etc)
# $_[1]: interval (ie, day, week, month, year)
# $_[2]: sensor description
RRDs::graph "$img/temp-$_[0]-$_[1].png",
"-s -1$_[1]",
"-t $_[2]",
"--lazy",
"-h", "200", "-w", "600",
"-a", "PNG",
"-v Grader Celsius (\260C)",
"-X0",
"--slope-mode",
"--watermark= HuskiBoy | Generated: $date kl: $hour:$min",
"DEF:temp=$rrd/owfs-$_[0].rrd:temp:AVERAGE",
"DEF:temp_min=$rrd/owfs-$_[0].rrd:temp:MIN",
"DEF:temp_max=$rrd/owfs-$_[0].rrd:temp:MAX",
"COMMENT:\t\t\t\tNu Medel Max Min\\n",
"HRULE:0#000000",
"LINE2:temp#0000FF:\t\t\t",
"GPRINT:temp:LAST:%6.2lf\260C",
"GPRINT:temp:AVERAGE:%6.2lf\260C",
"GPRINT:temp:MAX:%6.2lf\260C",
"GPRINT:temp:MIN:%6.2lf\260C\\n";
if ($ERROR = RRDs::error) { print "$0: unable to generate sensor $_[0] $_[1] graph: $ERROR\n"; }
}
Hur kan jag testa om givaren inte finns och isåfall sätta variabeln $temp till "NAN"?