# -*- perl -*-
#
# $Revision: 1.3 $
package iostat;

require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(&print_usage &print_version &service_test);
use lib $main::runtimedir;
use csl_utils qw($TIMEOUT %ERRORS);
use strict;


sub print_usage ();
sub print_version ();
sub service_test (%);

sub print_usage () {

    print "
Usage:  Check iostat info for all partitions.
Options:
    [--verbose]
    --help
    --version
    --usage
";
    return $ERRORS{'OK'};
}

sub print_version () {
    my $version = '$Revision: 1.3 $';
    $version =~ s/^\$Revision: //;
    $version =~ s/ \$\s*$//;

    print "iostat.pm - $version\n";
    return $ERRORS{'OK'};
}

sub service_test (%) {
    my %options = @_;

    if ($options{"verbose"}) {
        foreach my $opt (sort keys(%options)) {
            if ($options{$opt}) {
                print "$opt - $options{$opt}\n";
            }
        }
    }

    my $output = "";
    my $status = "OK";

    my $command = "/usr/bin/iostat -d";

    open (IN, "$command |") or exitwarn("Could not run $command: $!\n");

    my $founddevice;

    print "OK: ";
    LINE: while (my $line = <IN>) {

        # Get rid of anything before the actual Device info.
        unless ($founddevice) {
            $founddevice++ if ($line =~ /^Device/);
            next LINE;
        }

        chomp $line;
        last LINE if ($line eq "");
        my ($drive, $tps, $rbls, $wbls, $rbl, $wbl) = split (/\s+/, $line);
        print "$drive: t:$tps, r/s:$rbls, w/s:$wbls, r:$rbl, w:$wbl# ";
    }
    close IN;

    print "\n";

    print "Exit status: $status\n" if $options{"verbose"};

    exit $ERRORS{$status};
}

sub exitwarn {
    my $message = shift;

    print "CRITICAL: $message";

    exit $ERRORS{CRITICAL};
}
