#!@@GOODSH@@
# -*- sh -*-
#
# Original script (NetBSD) by he
# 
# Adapted to FreeBSD 6.2 / PC-BSD 1.4 by Pierre Bauduin (pierre@baudu.in)
# Version 0.0.2
# October 24 2007
#
# Plugin for watching io-bound traffic (in bytes) on disks.
#
# Parameters:
#
# 	config   (required)
# 	autoconf (optional - used by munin-config)
#
# Magick markers (optional - used by munin-config and som installation
# scripts):
#%# family=auto
#%# capabilities=autoconf

# autoconf

if [ "$1" = "autoconf" ]; then
   # Is the iostat executable there at least ?
   if [ -x /usr/sbin/iostat ]; then
       if iostat -x > /dev/null 2>&1; then
           echo yes
           exit 0
       else
           echo "no (iostat too old, does not yet support -x option)"
           exit 0
       fi
   else
       echo "no (no /usr/sbin/iostat executable)"
       exit 0
   fi
fi

# config
if [ "$1" = "config" ]; then
    echo 'graph_title IOstat by bytes'
    echo 'graph_args --base 1024 -l 0'
    echo 'graph_vlabel MB per ${graph_period} read+written'
    echo 'graph_category disk'
    echo 'graph_info This graph shows the I/O to and from block devices'
    # We don't give a XXXX about device or extended
    drives=`/usr/sbin/iostat -I -x | 
		awk '/^device/ { next; } // { print $1; }' | 
		awk '/extended/ { next; } // { print $1; }'`
    echo -n 'graph_order'
    for d in $drives; do
       echo -n ' '${d}'_read '${d}'_write'
    done
    echo
    for d in $drives; do
       echo "${d}_read.label ${d}"
       echo "${d}_read.type DERIVE"
       echo "${d}_read.max 2000"
       echo "${d}_read.min 0"
       echo "${d}_read.graph no"
       echo "${d}_write.label ${d}"
       echo "${d}_write.info I/O on device ${d}"
       echo "${d}_write.type DERIVE"
       echo "${d}_write.max 2000"
       echo "${d}_write.min 0"
       echo "${d}_write.negative ${d}_read"
    done
exit 0
fi

# This is the main part of the script, basically it calls
# /usr/sbin/iostat and processes its output

    # We don't give a XXXX about device or extended
    # On NetBSD the kilobyte read and kilobyte write are columns 5 and 9
    # On FreeBSD the kilobyte read and kilobyte write are columns 4 and 5
    /usr/sbin/iostat -I -x |
		awk '/^device/ { next; } //'  | 
		awk '/extended/ { next; } //' |
		awk ' {
    			print $1 "_read.value " int($4);
    			print $1 "_write.value " int($5);}'
