#!/bin/bash

: <<EOF

=head1 NAME

df_abs - Plugin to monitor absolute disk usage

=head1 CONFIGURATION

The following configuration parameters are used by this plugin

 [df_abs]
  env.exclude     - space separated list of file system types to exclude
  env.exclude_re  - space separated regex list of devices or mount points to exlcude
  env.warning     - Warning percentage
  env.critical    - Critical percentage
  env.total       - Enable/Disable graph total [on|off]
  env.dfopts      - Override commandline options for calling "df"

This plugin will monitor local filesystems by default.  To monitor
network filesystems, set "env.dfopts", and omit the "-l" option.

=head2 DEFAULT CONFIGURATION

 [df_abs]
  env.exclude     iso9660
  env.exclude_re  ^/dev/sdc
  env.warning     92
  env.critical    98
  env.total       on
  env.dfopts      -P -l

=head2 EXAMPLE CONFIGURATION

To monitor remote filesystems, omit the "-l" option from the
"env.dfopts" defaults.

  [df_abs]
    env.dfopts -P

=head1 LICENSE

GPLv2

=head1 MAGIC MARKERS

 #%# family=manual
 #%# capabilities=autoconf

=cut

EOF

. $MUNIN_LIBDIR/plugins/plugin.sh

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

# Append $mnt in case $dev is not a real block device
get_fieldname() {
        dev="$1"
        mnt="$2"
        case "$dev" in
                /* )
                        clean_fieldname $dev
                        ;;
                * )
                        clean_fieldname "$dev $mnt"
                        ;;
        esac
}

total=${total:-on}

exclude=${exclude:-iso9660}
exclude=$(echo $exclude | sed -r -e 's/ +/ -x /g' -e 's/^/-x /')

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

        echo 'graph_title Filesystem usage (in bytes)'
        echo 'graph_args --base 1024 --lower-limit 0'
        echo 'graph_vlabel bytes'
        echo 'graph_category disk'
        if [ "$total" = "on" ]; then
                echo 'graph_total Total'
        fi
        df $dfopts $exclude | sed 1d | grep -v "//" |
        while read dev size used avail cap mnt; do
                name="$(get_fieldname $dev $mnt)"
                echo "$name.label $mnt"
                echo "$name.cdef $name,1024,*"
                warn=$(get_warning $name)
                crit=$(get_critical $name)
                if [ -n "$warn" ]; then
                        echo "$name.warning $((size * $warn / 100))"
                fi
                if [ -n "$crit" ]; then
                        echo "$name.critical $((size * $crit / 100))"
                fi
        done
        exit 0
		for re in $exclude_re
		do
			if [[ "$dev" =~ $re ]] || [[ "$mnt" =~ $re ]]
			then
				continue 2
			fi
		done
fi

df $dfopts $exclude | sed 1d | grep -v "//" |
	while read dev size used avail cap mnt; do
		for re in $exclude_re
		do
			if [[ "$dev" =~ $re ]] || [[ "$mnt" =~ $re ]]
			then
				continue 2
			fi
		done
		echo "$(get_fieldname $dev $mnt).value $used"
done
