#!/bin/sh
# 
# plugin to monitor a nvidia graphic card temperature(s)
# dynamically shows the sensor name (gpu, board...)
# written by Dju
#
# Requirements :
#  - a nvidia graphic card
#  - a working nvclock binary
#  - shell commands: grep, awk, sed, cut
#  - root rights to execute nvclock. add in plugin-conf.d/munin-node
#    [nvclock]
#    user root
#
# Parameters : autoconf and config
#
# Version: 1.1
#
#%# family=auto
#%# capabilities=autoconf

NVCLOCK=`which nvclock`
if [ ! -z "$NVCLOCK" -a -f $NVCLOCK -a -x $NVCLOCK ]; then
   NVCLOCK_OK=1
   temps=$($NVCLOCK -i | grep 'temperature:' | awk '{print $1,$3}' | sed 's/ /@/g' | sed 's/C$//g')
else
   NVCLOCK_OK=0
fi

if [ "$1" = "autoconf" ]; then
	if [ $NVCLOCK_OK -eq 1 ]; then
		echo yes
		exit 0
	else
		echo no
		exit 1
	fi
fi

if [ "$1" = "config" ]; then
	echo 'graph_title Nvidia temperatures'
	echo 'graph_args --base 1000 -l 0'
	echo 'graph_vlabel temp in °C'
	echo 'graph_category sensors'
	if [ $NVCLOCK_OK -eq 1 ]; then
		for temp in $temps; do
			label=$(echo $temp | cut -d@ -f1)
			echo "${label}.label ${label}"
		done
	fi
	exit 0
fi

if [ $NVCLOCK_OK -eq 1 ]; then
	for temp in $temps; do
		label=$(echo $temp | cut -d@ -f1)
		value=$(echo $temp | cut -d@ -f2)
		echo "${label}.value ${value}"
	done
fi
