#!/bin/sh
#
# Plugin to monitor Dallas 1-wire temperature sensors using digitemp
#
# Copyright (C) 2006 Dagfinn Ilmari Mannsaaker <ilmari@ilmari.org>
#
# 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

digitemp=${0##*/}
model=${digitemp##*_}
digitemprc=${digitemprc:-/etc/digitemp.conf}

if ! [ -x "`which $digitemp`" ]; then
    echo "$digitemp not found" >&2
    exit 1
fi

if [ "$1" = "config" ]; then
    echo "graph_title DigiTemp $model temperatures"
    echo 'graph_vlabel degrees C'
    echo 'graph_category sensors'
    echo "graph_info This graph shows the temperature read from $model 1-wire sensors"
    $digitemp -c "$digitemprc" -q -a -o '%s %R' | grep -v ^Found | while read sensor serial; do
	echo "sensor$serial.label sensor #$sensor"
	echo "sensor$serial.type GAUGE"
	echo "sensor$serial.info Temperature from sensor #$sensor"
	echo "sensor$serial.critical 30"
        echo "sensor$serial.warning 25"
   done
   exit 0
fi

$digitemp -c "$digitemprc" -q -a -o 'sensor%R.value %C'|grep -v ^Found
