#!/usr/bin/perl
#
# Munin Plugin to monitor Dallas 1-wire temperature sensors using digitemp
#
# Completely changed the existing digitemp Plugin to be able to cooperate
# with the IPCop digitemp addon.
#
# Copyright (C) 2007 Olaf (weizen_42) Westrik
#
# For support post / read in http://www.ipcop-forum.de
#
# 
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 dated June, 1991.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA.
#
#
# Parameters:
#
#       config   (required)
#       autoconf (optional - only used by munin-config)
#
# Usage:
#
# Symlink as the name of the digitemp program to use, i.e. digitemp_DS2490
# 
# Enviroment variables
#
#       digitemprc     config file to use. default /etc/digitemp.conf
#                      This must be generated with digitemp_<model> -i
#
# Magic markers (optional - used by munin-config and some installation
# scripts):
#%# family=manual

require "/var/ipcop/general-functions.pl";

my $model;
my $digitemp;
my %digitemp_settings = ();
my @tmpfile = ();
my %values = ();
my $i;
my $line;
my $retries;

$model = `basename $0 | sed 's/^digitemp_//g'` ;
chomp $model;
die "No model defined" if ( $model eq "" );

$digitemp = "/usr/local/bin/digitemp_$model";
die "digitemp_$model not found" unless ( -e $digitemp );

die "Digitemp addon settings not found" unless ( -e "/var/ipcop/digitemp/settings" );
&General::readhash("${General::swroot}/digitemp/settings", \%digitemp_settings);


if ( defined $ARGV[0] and $ARGV[0] eq "config" )
{
  print "graph_title DigiTemp $model temperatures\n";
  print "graph_vlabel degrees C\n";
  print "graph_category sensors\n";
  print "graph_info This graph shows the temperature read from $model 1-wire sensors\n";

  for($i = 0; $i < $digitemp_settings{'SENSORS'}; $i++)
  {
    my $SNR = $digitemp_settings{"SENSOR$i"};
    print "sensor$SNR.label ". $digitemp_settings{"LABEL-$SNR"} ."\n";
    print "sensor$SNR.type GAUGE\n";
    print "sensor$SNR.info Temperature from sensor #$i\n";
    print "sensor$SNR.critical 30\n";
    print "sensor$SNR.warning 25\n";
  }
  exit 0;
}


for ($retries = 0; $retries < 5; $retries++)
{
  if ( open(FILE, "/var/log/digitemp.log") )
  {
    @tmpfile = <FILE>;
    close (FILE);
    last;
  }
  sleep 1;
}

die "Digitemp addon settings not found" if ( $retries >= 5 );


foreach $line ( @tmpfile )
{
  chomp($line);               # remove newline
  my @temp = split(/\,/,$line,4);
  if ( $temp[0] eq '' ) { next; }

  $values{$temp[1]}{'T'}   = $temp[2];
}


for($i = 0; $i < $digitemp_settings{'SENSORS'}; $i++)
{
  my $SNR = $digitemp_settings{"SENSOR$i"};

  print "sensor$SNR.value ". $values{$SNR}{'T'} ."\n";
}
