#!/bin/sh
#
# Plugin to monitor various environment sensors provided by envstat(8)
# on NetBSD
#
# Requirements:
# 	- envsys(4) driver configured and supported hardware present
# 	- envstat(8) program present
#
# Parameters supported:
#
# 	config
# 	autoconf
# 	suggest
#
# $Log: sensors_.in,v $
# Revision 1.2  2006/07/21 23:28:37  abs
# Use PKG_SYSCONFSUBDIR so PKG_SYSCONFBASE can be set
# Use REPLACE_PERL on all plugins
# Adjust sensors_ to work with NetBSD 3
# Bump PKGREVISION
#
# Revision 1.1.1.1  2006/06/04 20:53:57  he
# Import the client version of the Munin system monitoring/graphing
# tool -- project homepage is at http://munin.sourceforge.net/
#
# This package has added support for NetBSD, via a number of new plugin
# scripts where specific steps needs to be taken to collect information.
#
# I also modified the ntp_ plugin script to make it possible to not
# plot the NTP poll delay, leaving just jitter and offset, which IMO
# produces a more telling graph.
#
#
#
#%# family=auto
#%# capabilities="autoconf suggest"

if [ "$1" = "autoconf" ]; then
    if [ -x /usr/sbin/envstat ]; then
	if /usr/sbin/envstat -r >/dev/null 2>&1; then
	    echo yes
	    exit 0
	else
	    echo no '(no sensors available via envstat(8))'
	    exit 1
	fi
    else
	echo no '(/usr/sbin/envstat not executable)'
	exit 1
    fi
fi
	

if [ "$1" = "suggest" ]; then
    /usr/sbin/envstat -r | awk '
/degC$/ { temp=1; }
/RPM$/  { fans=1; }
/V$/    { volt=1; }
END {
    if (temp) {
	print "temp";
    }
    if (fans) {
	print "fans";
    }
    if (volt) {
	print "volt";
    }
}'
    exit 0
fi

envstat_config()
{
    /usr/sbin/envstat -r | awk -v "lookfor=$1" -F: '
BEGIN { p=0 }
match($0, lookfor "$") {
    l=$1
    gsub("[^A-Za-z0-9\\-]", "", $1);
    print tolower(lookfor) "_" tolower($1) ".label " l;
    p=1;
}
END {
    if (!p) {
	exit 1;
    }
}'
    if [ $? != 0 ]; then
	echo "No $1 sensors found" >&2
	exit 1
    fi
}

envstat_value()
{
    /usr/sbin/envstat -r | awk -v "lookfor=$1" -F: '
match($0, lookfor "$") {
    gsub("[^A-Za-z0-9\\-]", "", $1);
    sub(" *", "", $2);
    sub(" .*", "", $2);
    print tolower(lookfor) "_" tolower($1) ".value " $2;
}
'
}

if [ "$1" = "config" ]; then
    case $0 in
	*sensors_temp)
            echo 'graph_title Temperatures'
	    echo 'graph_vtitle deg Celcius'
	    echo 'graph_args --base 1000 -l 0'
	    envstat_config degC
	    ;;
	*sensors_fans)
            echo 'graph_title Fans'
	    echo 'graph_vtitle RPM'
	    echo 'graph_args --base 1000 -l 0'
	    envstat_config RPM
	    ;;
	*sensors_volt)
            echo 'graph_title Voltages'
	    echo 'graph_vtitle Volt'
	    echo 'graph_args --base 1000 --logarithmic'
	    envstat_config V
	    ;;
    esac
    echo 'graph_category sensors'

    exit 0
fi


case $0 in
    *sensors_temp)
	envstat_value degC
	;;
    *sensors_fans)
	envstat_value RPM
	;;
    *sensors_volt)
	envstat_value V
	;;
esac
