#!/bin/sh
#
# $OpenBSD: sensors_,v 1.4 2014/10/30 08:48:14 kirby Exp $
#
# Script to monitor OpenBSD sensors
# (c) 2007 Michael Knudsen <mk@openbsd.org>
#
# Invoke as "volt", "temp", "fan" or "humidity" 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; }
/.humidity/ { humidity=1; }
END {
    if (temp) {
        print "temp";
    }
    if (fan) {
        print "fan";
    }
    if (volt) {
        print "volt";
    }
    if (humidity) {
	print "humidity";
    }
}'
    exit 0
fi

if [ "$mode" != "volt" -a "$mode" != "temp" -a "$mode" != "fan" -a "$mode" != "humidity" ]; then
	# error:  invoke as "temp" "volt" "fan" or "humidity"
	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_scale no'
                echo 'graph_printf %4.0lf'
		echo 'graph_vlabel RPM'
	elif [ "$mode" = "humidity" ]; then
		echo 'graph_title Relative Humidity sensor values (in %)'
		echo 'graph_args --upper-limit 100 -l 0'
		echo 'graph_vlabel %'
		echo 'graph_scale no'
	fi
	echo 'graph_category sensors'

	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 | sed -e 's,\%$,,g')
	echo "$name.value  $value"
done

