#!/bin/bash

LIMITS=/var/lib/munin/limits
STF=/var/spool/statusd/munin.status
MAXTRY=5

if [ -f $LIMITS ]; then

	#STATUS=`grep -ie '\(warning\|critical\|error\)[^;]*$' $LIMITS |grep -vie ';state[^;]*$' |sed -e 's/^[^;]*;\([^;.]*\)[^;]*;/\1;/'`
	STATUS=`grep -ie '\(critical\|error\)[^;]*$' $LIMITS |grep -vie ';state[^;]*$' |sed -e 's/^[^;]*;\([^;.]*\)[^;]*;/\1;/'`

	if [ -n "$STATUS" ]; then
		if [ -f $STF ]; then
			TRY=`cat $STF`
			if [ $TRY -gt $MAXTRY ]; then
				echo $STATUS
				exit 1
			else
				let "TRY++"
				echo "$TRY" > $STF
				echo 'OK'
				exit 0
			fi
		else
			echo '1' > $STF
			echo 'OK'
			exit 0
		fi
	else
		if [ -f $STF ]; then
			rm $STF
		fi
		echo 'OK'
		exit 0
	fi

else
	echo 'WARNING: munin limits file not found'
	exit 1
fi
