磁盘IO监控脚本

#!/usr/bin/perl -w
use strict;
use Regexp::Common;
use Getopt::Long;
my %values;
my $result;
my ($iostat,$data,$perfdata);
my ($warning,$critical,$disk);
$result = GetOptions("disk=s"      =>      \$disk,
                     "d=s"         =>      \$disk,
                     "warning=i"   =>      \$warning,
                     "w=i"         =>      \$warning,
                     "critical=i"  =>      \$critical,
                     "c=i"         =>      \$critical,
                     "help"        =>      \&help,
                     );
$warning ||= 75;
$critical ||= 80;
$disk ||= undef;
&main();
sub main(){
        &set_iostat();
        &get_data();
        if ( $values{'%util'} < $warning )
        {
            print "TEST_IO OK - " . "$data| $perfdata\n";
        }
        elsif ( $values{'%util'} >= $warning )
        {
            print "TEST_IO WARNING - " . "$data| $perfdata\n";
        }
        elsif ( $values{'%util'} >= $critical )
        {
            print "TEST_IO CRITICAL - " . "$data| $perfdata\n";
        }
        else #No clue what value is there!
        {
             print "UNKNOWN - Error in command output\n";
        }
}
sub set_iostat(){
        if (defined $disk){
                $iostat = "iostat " . "$disk";
        }else{
                $iostat = "iostat";
        }
}
sub get_data(){
        &set_iostat();
        my $output = `$iostat -d -x 1 3 |tail -n 3`;
        unless($output) {
            print "Error executing iostat command\n";
        }
        $output =~ /Device.*?/gs;
        foreach my $key (qw(rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm %util)){
                if($output =~ /\G.*?($RE{num}{real})/gs){
                        $data .= sprintf("%s %s ", $key, $1);
                }
        }
        %values = split ' ', $data;
        $data = '';
        foreach my $key(qw(r/s w/s avgqu-sz await svctm %util)){
            $data .= sprintf("%s %s ",$key,$values{$key});
            #排除(await、svctm、%util)的perfdata输出
            # if( $key eq 'r/s'|| $key eq 'w/s'|| $key eq 'avgqu-sz'){
                $perfdata .= $key . "=" . $values{$key} . " ";
            # }
        }
}
sub help{
    print "Usage: -d|--disk= -w|--warning= -c|--critical=\n";
}

#perl check_diskio.pl -d /dev/sda -w 75 -c 80

创新互联公司成立于2013年,先为阜平等服务建站,阜平等地企业,进行企业商务咨询服务。为阜平企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

TEST_IO OK - r/s 0.00 w/s 3.00 avgqu-sz 0.01 await 3.00 svctm 1.00 %util 0.30 | r/s=0.00 w/s=3.00 avgqu-sz=0.01 await=3.00 svctm=1.00 %util=0.30


分享文章:磁盘IO监控脚本
转载来源:http://myzitong.com/article/jdhhgc.html