#!/bin/sh

# snmp plugin for Wiesemann und Thies WebThermometer
# (C) Michael Renner <michael.renner@gmx.de>
# 10.07.2007
# Version 0.2

# to find out which values your device use type
# snmpwalk -v1 -c public <IP> 1.3.6.1.4.1.5040

COMMUNITY="public"
MIB=/usr/share/snmp/mibs/an2graph_mib_125.mib
graph_period="second"
SNMPCLIENT=`basename $0 | sed 's/^snmp_//g' | cut -d "_" -f1`
SNMPGET="/usr/bin/snmpget -m $MIB -v1 -c $COMMUNITY $SNMPCLIENT"
BROADCAST="255.255.255.255"

if [ "$1" = "autoconf" ]; then
	echo yes
	exit 0
fi

if [ "$1" = "suggest" ]; then
	# This part does not work well
	# the pinf command must support broadcasts
	# echo  | nc -u -w 1 192.168.5.0 8513
	#
	# 1.) find all network devices
	# 2.) find out if it reacts to a UDP package at port 8513
	# 3.) find out if it is a W & T WebGraph
	IPS=`ping -b -c2  $BROADCAST | grep icmp 2> /dev/null | sed s/.*from//g | cut -f1 -d":"`
	for IP in $IPS ; do
		unset RETURN
		RETURN=`echo "-" | nc -u -w 1 $IP 8513`
		RETURN=foo
		if [ "$RETURN" ] ; then
			/usr/bin/snmpget -m $MIB -v1 -c $COMMUNITY $IP WebGraph-2xThermometer-MIB::wtWebioAn2GraphSensors.0 > /dev/null 2>&1
			RC=$?
			RC=0
			if [ ! "$RC" != "0" ] ; then
				FILE=`basename $0`
				DIR=`dirname $0`
				HOST_NAME=`host $IP | sed s/.*pointer//\ `
				HOST_NAME=`echo $HOST_NAME | sed -e 's/\.$//'`
				LINKDIR="/etc/munin/plugins/"
				LINKFILE=`echo $FILE | sed s/__/_$HOST_NAME_/`
				echo file $FILE dir $DIR hostname $HOST_NAME linkdir $LINKDIR	linkfile $LINKFILE
				#echo "ln -s $DIR/$FILE $LINKNAME"
			fi

			unset SNMPCLIENT
		fi
	done
	exit

fi

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

	# some fix values
        echo "host_name $SNMPCLIENT"
        echo 'graph_category Other'
        echo 'graph_args --base 1000 -l 0'

	# some variables, fetched from the device
	GRAPH_TITLE=`$SNMPGET WebGraph-2xThermometer-MIB::wtWebioAn2GraphDeviceName.0 | sed s/.*STRING:// | sed s/\"//g`
	GRAPH_INFO=`$SNMPGET WebGraph-2xThermometer-MIB::wtWebioAn2GraphDeviceText.0 | sed s/.*STRING:// | sed s/\"//g`
	GRAPH_VLABEL=`$SNMPGET WebGraph-2xThermometer-MIB::wtWebioAn2GraphDeviceLocation.0 | sed s/.*STRING:// | sed s/\"//g`
	SENSOR_1_LABEL=`$SNMPGET WebGraph-2xThermometer-MIB::wtWebioAn2GraphPortName.1 | sed s/.*STRING:// | sed s/\"//g`
	SENSOR_2_LABEL=`$SNMPGET WebGraph-2xThermometer-MIB::wtWebioAn2GraphPortName.2 | sed s/.*STRING:// | sed s/\"//g`
	SENSOR_1_CRITICAL=`$SNMPGET WebGraph-2xThermometer-MIB::wtWebioAn2GraphAlarmMax.1 | sed s/.*STRING:// | sed s/\"//g`
	SENSOR_2_CRITICAL=`$SNMPGET WebGraph-2xThermometer-MIB::wtWebioAn2GraphAlarmMax.2 | sed s/.*STRING:// | sed s/\"//g`

	# echo the result to munin
	echo "graph_title $GRAPH_TITLE" 
        echo "graph_info $GRAPH_INFO"
        echo "graph_vlabel $GRAPH_VLABEL"
        echo "Sensor_1.label $SENSOR_1_LABEL"
	echo "Sensor_2.label $SENSOR_2_LABEL"
	echo "Sensor_1.critical $SENSOR_1_CRITICAL"
	echo "Sensor_2.critical $SENSOR_2_CRITICAL"
        echo 'Sensor_1.min 0'
        echo 'Sensor_2.min 0'
        echo 'Sensor_1.graph  yes'
        echo 'Sensor_2.graph yes'
else

	Sensor_1=`$SNMPGET WebGraph-2xThermometer-MIB::wtWebioAn2GraphTempValue.1 | sed s/.*STRING:// | sed s/\"//g | sed s/,/./`
	Sensor_2=`$SNMPGET WebGraph-2xThermometer-MIB::wtWebioAn2GraphTempValue.2 | sed s/.*STRING:// | sed s/\"//g | sed s/,/./`
	echo Sensor_1.value $Sensor_1
	echo Sensor_2.value $Sensor_2
fi
