#!/usr/bin/perl

#
# Version 0.0.2 - Jan/2009
# Changes: added device verification
#
# by Thiago Varela - thiago@iplenix.com

# Version 0.1 - Nov/2011
# by Ruediger Oertel ro@suse.de
# Changes:
#     - rewrite in perl, no need for external grep, awk, bc
#     - add output for iowait
#     - implement using device mapper names, e.g. $vg-$lv

use strict;
use Getopt::Std;

$Getopt::Std::STANDARD_HELP_VERSION = "true";

my $iostat = `which iostat 2>/dev/null`;
chomp($iostat);

my $progname = $0;


# call it VERSION_MESSAGE so that getopts uses it automatically
sub VERSION_MESSAGE {
    print "$progname: version 0.1, Nov/2011\n";
}

# call it HELP_MESSAGE so that getopts uses it automatically
sub HELP_MESSAGE {
    print "\n\tThis plugin shows the I/O usage of the specified disk, using the iostat external program.\n";
    print "\tIt prints three statistics: Transactions per second (tps), Kilobytes per second\n";
    print "tread from the disk (KB_read/s) and and written to the disk (KB_written/s)\n\n";
    print "$progname:\n\t-d <disk>\t\tDevice to be checked (without the full path, eg. sda)\n";
    print "\t\t\t\t(also accepted are device mapper names)\n";
    print "\t-c <tps>,<read>,<wrtn>\tSets the CRITICAL level for tps, KB_read/s and KB_written/s, respectively\n";
    print "\t-w <tps>,<read>,<wrtn>\tSets the WARNING level for tps, KB_read/s and KB_written/s, respectively\n";
    print "\t-C <percent>\t Sets the CRITICAL level for iowait\n";
    print "\t-W <percent>\t Sets the WARNING level for iowait\n";
    print "\t\t\t(if no level is set for iowait, no warning is set for this value)\n";
    exit 1;
}

unless ($iostat && -f $iostat) {
    warn "ERROR: You must have iostat installed in order to run this plugin\n";
    exit 1;
}

# Getting parameters:
my %opts;
getopts('d:w:c:W:C:hv', \%opts);

my $disk = $opts{'d'};
my $warning = $opts{'w'};
my $critical = $opts{'c'};
my $warning_iowait = $opts{'W'};
my $critical_iowait = $opts{'C'};

VERSION_MESSAGE() if $opts{'v'};
HELP_MESSAGE() if $opts{'h'};

# Adjusting the three warn and crit levels:
my ($crit_tps,$crit_read,$crit_written) = split(',',$critical);
my ($warn_tps,$warn_read,$warn_written) = split(',',$warning);

# Checking parameters:
if (! -b "/dev/$disk") {
    if (-b "/dev/mapper/$disk") {
        my @f = stat("/dev/mapper/$disk");
	$f[6] %= 256;
	$disk = "dm-$f[6]";
    } else {
	warn "ERROR: Device incorrectly specified\n";
	HELP_MESSAGE();
    }
}

unless ($warn_tps && $warn_read && $warn_written && $crit_tps && $crit_read && $crit_written) {
    warn "ERROR: You must specify all warning and critical levels\n";
    HELP_MESSAGE();
}

if ($warn_tps > $crit_tps || $warn_read > $crit_read || $warn_written > $crit_written) {
    warn "ERROR: critical levels must be highter than warning levels\n";
    HELP_MESSAGE();
}

if ($warning_iowait && $critical_iowait && $warning_iowait > $critical_iowait) {
    warn "ERROR: critical iowait level must be higher that warning level\n";
    HELP_MESSAGE();
}

my ($tps,$kbread,$kbwritten,$iowait);
my $seen_usage = 0;
my $seen_disk = 0;

# Doing the actual check:
open (IOSTAT,"-|","$iostat -k $disk 2 2");
while (<IOSTAT>) {
    chomp();
    if (/^[0-9\.\ \t]+$/) {
	$seen_usage++;
	next if $seen_usage < 2;
	my (@stats) = split ('\s+', $_);
        $iowait = $stats[4];
        next;
    }
    if (/^$disk /) {
	$seen_disk++;
	next if $seen_disk < 2;
	my (@stats) = split ('\s+', $_);
        ($tps,$kbread,$kbwritten) = @stats[1,2,3];
	last;
    }
}
close (IOSTAT);    

my $msg = "OK";
my $status = 0;

# Comparing the result and setting the correct level:
if ($tps >= $warn_tps || $kbread >= $warn_read || $kbwritten >= $warn_written) {
   $msg = "WARNING";
   $status = 1;
}

if ($warning_iowait && $iowait >= $warning_iowait) {
   $msg = "WARNING";
   $status = 1;
}

if ($tps >= $crit_tps || $kbread >= $crit_read || $kbwritten >= $crit_written) {
   $msg = "CRITICAL";
   $status = 2;
}

if ($critical_iowait && $iowait >= $critical_iowait) {
   $msg = "CRITICAL";
   $status = 2;
}


# Printing the results:
print "$msg - I/O stats tps=$tps KB_read/s=$kbread KB_written/s=$kbwritten iowait=$iowait | 'tps'=$tps; 'KB_read/s'=$kbread; 'KB_written/s'=$kbwritten; 'iowait'=$iowait\n";

# Bye!
exit $status;
