#!@@PERL@@ -w
# -*- perl -*-

=head1 NAME

iostat - Plugin for watching output from iostat on SunOS/Solaris.
This plugin is based on io counters rather than calculated averages.

=head1 APPLICABLE SYSTEMS

SunOS/Solaris that have /bin/iostat.

=head1 CONFIGURATION

None needed or possible at this time.

=head1 INTERPRETATION

To be written...

=head1 MAGIC MARKERS

  #%# family=auto
  #%# capabilities=autoconf

=head1 VERSION

  $Id $

=head1 AUTHORS

Nicolai Langfeldt (janl@linpro.no) 17/10/2003.  Based on work done by
Andreas Dahl (andread@linpro.no)

=head1 LICENSE

GPLv2

=cut

use strict;

use constant IOSTATBIN => "/bin/iostat";

# -I    Report the counts in each interval, rather than  rates
#       (where applicable).
# -x    For each disk, report extended disk  statistics.   The
#       output is in tabular form.
# -r    Emit data in a comma-separated format.
use constant IOSTATPRM => "-Ixr";

if ($ARGV[0] && $ARGV[0] eq "autoconf"){
    if (-x IOSTATBIN) {
	print "yes\n";
    } else {
	print 'no (no ',IOSTATBIN," executable)\n";
    }
    exit 0;
}

if ($ARGV[0] && $ARGV[0] eq "config"){
    print "graph_title I/O throughput\n";
    print "graph_vlabel bytes/second\n";

    print "diskw.label Disk write\n";
    print "diskw.type DERIVE\n";
    print "diskw.min 0\n";

    print "diskr.label Disk read\n";
    print "diskr.type DERIVE\n";
    print "diskr.min 0\n";

    print "tapew.label Tape write\n";
    print "tapew.type DERIVE\n";
    print "tapew.min 0\n";

    print "taper.label Tape read\n";
    print "taper.type DERIVE\n";
    print "taper.min 0\n";

    print "nfsw.label NFS write\n";
    print "nfsw.type DERIVE\n";
    print "nfsw.min 0\n";

    print "nfsr.label NFS read\n";
    print "nfsr.type DERIVE\n";
    print "nfsr.min 0\n";
    exit 0;
}

open(IOSTAT, IOSTATBIN." ".IOSTATPRM."|") || 
    die ("Couldn't run iostat $!\n");

my($dev,$nri,$nwi,$bri,$bwi);
my(%nri,%nwi,%bri,%bwi);

while(<IOSTAT>){
    # Fields: device,r/i,w/i,kr/i,kw/i,...
    # print;
    ($dev,$nri,$nwi,$bri,$bwi,undef) = split(',',$_,6);
    # print "$dev $nri $nwi $bri $bwi\n";
    if ($dev =~ /^sd/) {
	$dev = 'disk';
    } elsif ($dev =~ /^st/) {
	$dev = 'tape';
    } elsif ($dev =~ /^nfs/) {
	$dev = 'nfs';
    } else {
	# We ignore fd = floppy, and lines of headers and such
	next;
    }
    $nri{$dev} += $nri;
    $nwi{$dev} += $nwi;
    $bri{$dev} += ($bri*1024); # The header says kr/i, and we want bytes
    $bwi{$dev} += ($bwi*1024); # Ditto.
}

close(IOSTAT);

foreach $dev ('disk','tape','nfs') {
    print $dev,"w.value ", (defined($bwi{$dev}) ? $bwi{$dev} : "U"), "\n";
    print $dev,"r.value ", (defined($bri{$dev}) ? $bri{$dev} : "U"), "\n";
}

