#!/bin/sh
#
# Script to monitor disk usage.
#
# Parameters understood:
#
# 	config   (required)
# 	autoconf (optional - used by munin-config)
#
# Magic markers (optional - used by munin-config and installation
# scripts):
#
#%# family=auto
#%# capabilities=autoconf

MAXLABEL=20

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

clean_name() {
    echo $1 $7 $2 | sed 's/[\/.-]/_/g'| awk "{ 
	if (\$3 == \"tmpfs\")
		n=\$1\$2
	else
		n=\$1
	print n
	}"
}


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

	echo 'graph_title Filesystem usage (in %)'
	echo 'graph_args --upper-limit 100 -l 0'
	echo 'graph_vlabel %'
	echo 'graph_category disk'
	echo 'graph_info This graph shows disk usage on the machine.'
	df -T -P -l -x none -x unknown -x udf -x iso9660 -x romfs -x ramfs | sed 1d | grep -v "//" | sort | while read i; do
		name=`clean_name $i`
		echo -n "$name.label "
		echo $i | awk "{ 
			dir=\$7
			if (length(dir) <= $MAXLABEL)
				print dir
			else
				printf (\"...%s\n\", substr (dir, length(dir)-$MAXLABEL+4, $MAXLABEL-3)) 
			print \"$name.info \" \$7 \" (\" \$2 \") -> \" \$1;
		}"
		echo "$name.warning 92"
		echo "$name.critical 98"
	done
	exit 0
fi

df -T -P -l -x none -x unknown -x udf -x iso9660 -x romfs -x ramfs | sed 1d | grep -v "//" | while read i; do
	name=`clean_name $i`
	echo -n "$name.value "
	echo $i | awk '{ print $6 }' | cut -f1 -d%
done
