#!/bin/sh
#
# $OpenBSD: sensors_,v 1.1.1.1 2009/11/17 11:11:21 sthen Exp $
#
# Script to monitor OpenBSD sensors
# (c) 2007 Michael Knudsen <mk@openbsd.org>
#
# Invoke as "volt", "temp" or "fan" based on what is being monitored.

# Parameters understood:
#
# 	config   (required)
# 	autoconf (optional - used by munin-config)

# Magic markers (optional - used by munin-config and installation
# scripts):
#
#%# family=auto
#%# capabilities=autoconf suggest

mode=$(basename $0 | sed 's/sensors_//')

if [ "$1" = "autoconf" ]; then
	if [ "$(uname -s)" = "OpenBSD" ]; then
		echo yes
		exit 0
	else
		echo no
		exit 1
	fi
fi

if [ "$1" = "suggest" ]; then
    sysctl hw.sensors | awk '
/.temp/ { temp=1; }
/.fan/  { fan=1; }
/.volt/    { volt=1; }
END {
    if (temp) {
        print "temp";
    }
    if (fan) {
        print "fan";
    }
    if (volt) {
        print "volt";
    }
}'
    exit 0
fi

if [ "$mode" != "volt" -a "$mode" != "temp" -a "$mode" != "fan" ]; then
	# error:  invoke as "temp" "volt" or "fan"
	exit 1
fi

if [ "$1" = "config" ]; then

	echo 'graph_args --base 1000'

	if [ "$mode" = "temp" ]; then
		echo 'graph_title Temperature sensor values'
		echo 'graph_vlabel degC'
	elif [ "$mode" = "volt" ]; then
		echo 'graph_title Voltage sensor values'
		echo 'graph_vlabel V'
	elif [ "$mode" = "fan" ]; then
		echo 'graph_title Fan speed sensor values'
		echo 'graph_vlabel RPM'
	fi
	echo 'graph_category system'

	sysctl hw.sensors | cut -b 12- | fgrep $mode | while read s; do
		name=$(echo $s | cut -d= -f1 | sed 's/\./_/g')
		echo "$name.label $name"
	done
	exit 0
fi

sysctl hw.sensors | cut -b 12- | fgrep $mode | while read s; do
	name=$(echo $s | cut -d= -f1 | sed 's/\./_/g')
	value=$(echo $s | cut -d= -f2 | cut -d' ' -f1)
	echo "$name.value  $value"
done

