#!/bin/sh

LOGDIR="/var/log/munin/"
LOGBASE=$LOGDIR"munin-"

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

	echo 'graph_category system'
	echo 'graph_title Munin'
	echo 'graph_args --base 1000 --lower-limit 0 --rigid'
	echo 'graph_vlabel Temps de traitement (s)'
	echo 'update.label update'
	echo 'update.draw AREA'
	echo 'limits.label limits'
	echo 'limits.draw STACK'
	echo 'graph.label graph'
	echo 'graph.draw STACK'
	echo 'html.label html'
	echo 'html.draw STACK'
	echo 'nagios.label nagios'
	echo 'nagios.draw STACK'
	echo 'total.label Total'
	echo 'max.label Maximum'
	echo 'total.warning 0:300'
	echo 'update.critical 0:240'
	exit 0
fi

total=0

for log in update limits graph html nagios
do
  echo -n $log".value "
  time=$(grep -E -i "Starting|finished" $LOGBASE$log".log" | grep -B1 "finished" | tail -n 2 | awk '{print "date -d \""$1" "$2" "$3"\" +%s"}' | sh | xargs | awk '{if($2) {print $2"-"$1} else {print "0"}}' | bc -l) || 0
  echo $time
  total=$(echo $time"+"$total | bc -l)
done

echo "total.value "$total

# Le temps de rafraîchissement de munin est de 5 minutes
echo "max.value 300"
