#!/bin/sh
#
# Plugin to monitor inode-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

fs() {
	local escaped_mntpt=`echo "$*" | awk '{ print $NF }' | sed 's|/|\\\\/|g'`
	awk "/^[^ ]* $escaped_mntpt / { print \$3 }" /proc/mounts | grep -v rootfs
}

print_values() {
	df -P -l -i -x none -x unknown | sed 1d | grep -v "//" | awk '$5 ~ /[0-9]%/ {print}' | while read i; do
		if [ "`fs $i`" = "reiserfs" ] ; then continue ; fi
		name=`echo $i | sed 's/[\/.-]/_/g'| awk '{ print $1 ".value " }'`
		echo -n "$name "
		echo $i | awk '{ print $5 }' | cut -f1 -d%
	done
}

if [ "$1" = "autoconf" ]; then
	if [ "`print_values`" = "" ] ; then
		echo no
	else
		echo yes
	fi
	exit 0
fi

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

	echo 'graph_title Inode usage (in %)'
	echo 'graph_args --upper-limit 100 -l 0'
	echo 'graph_vlabel %'
	echo 'graph_category disk'
	echo 'graph_info This graph shows the inode usage for the partitions of types that use inodes.'
	df -P -l -i -x unknown  -x none -T| sed 1d | grep -v "//" | sort | awk '$6 ~ /[0-9]%/ {print}' | awk "
		\$2 !~ /reiserfs/ {
                        dir=\$7
			name=\$1; gsub(/[\/.-]/, \"_\", name);
                        if (length(dir) <= $MAXLABEL)
                                print name \".label \" dir
                        else
                                printf (\"%s.label ...%s\n\", name, substr (dir, length(dir)-$MAXLABEL+4, $MAXLABEL-3))
			print name \".info \" \$7 \" (\" \$2 \") -> \" \$1;
			print name \".warning 92\"
			print name \".critical 98\" 
                }"
	exit 0
fi

print_values
