Perl自定义开发Nagios检测磁盘所有分区使用率插件(NRPE端使用)

功能说明:

脚本已经用在实践环境中,放到nrpe远程被监控端,用来实时监控远程主机所有分区,动态增加的分区也会自动加到监控范围。

当多个分区同时出现Critical和Warning状态时,将在Nagios中优先显示Critical状态,同时输出所有分区使用情况报告。

Perl自定义开发Nagios检测磁盘所有分区使用率插件(NRPE端使用)_第1张图片

监控内容包括:

分区总size;

可用分区size;

已经使用的使用率%;

开发语言:perl

使用说明:

在nrpe.cfg文件中添加下面一行

command[check_disk]=/opt/nrpe/libexec/lionel_disk.pl -w 90 -c 95

保存,重启nrpe使服务生效。

-w参数表示warning的阀值,90表示分区使用率达到90%时警告;-c参数表示critical的阀值,95表示使用率达到95%时报警。举例如下:

# ./lionel_disk.pl -w 90 -c 95

/dev/sda2 WARNING: Total is 9.7G MB, FreeDisk is 5.5G, UseDisk% is 42%
/dev/sda5 WARNING: Total is 8.7G MB, FreeDisk is 8.1G, UseDisk% is 2%
/dev/sda1 WARNING: Total is 99M MB, FreeDisk is 82M, UseDisk% is 13%
/dev/sdb1 CRITICAL: Total is 89G MB, FreeDisk is 33G, UseDisk% is 62%

其他细节:本脚本提供检测参数的功能,参数给出错误会提示Usage,warning的参数值一定要比 critical的值小,因为已用的使用率越多报警级别越高。

脚本内容如下,供大家参考:

脚本名称是【lionel_disk.pl 】

#!/usr/bin/perl
# author:shenxiaoran
# date:2014-1-14

use strict;

use lib "/opt/nrpe/libexec";
use utils qw(%ERRORS &print_revision &support &usage);

my ($opt_w,$opt_c) = ($ARGV[1],$ARGV[3]);
my $name_partation;
my $total_disk;
my $use_disk;
my $free_disk;
my $rate_use_disk;
my $status = 'OK';
my $flag = 0;

if ( !defined $ARGV[1] or !defined $ARGV[3] ) {
        print "Usage: $0 -w num -c num \n";
        print "Example: lionel_disk.pl -w 90 -c 95 \n";
}
else {
        check_disk();

}

sub check_disk() {
if ( $opt_w > $opt_c ) {
        print "Error: -w num > -c num \n";
        print "-w: means already use 90% \n";
        print "-c: means already use 95% \n";
        print "Usage: $0 -w num -c num \n";
        print "Example: lionel_disk.pl -w 90 -c 95 \n";
        exit;
}
else {
        my @data ;
        system "df -h > /tmp/df.tmp";
        open (FILE,"</tmp/df.tmp") || die ("Can't open /tmp/df.tmp $!");
        my $i = 0;
        while (<FILE>) {
                if (/^\/dev\/*./) {
                        $data[$i] = $_;
                        $i++;
                }
        }
        close FILE;
        foreach (@data) {
                my @partation = split(/\s+/,$_);
                $name_partation = $partation[0];
                $total_disk = $partation[1];
                $use_disk = $partation[2];
                $free_disk = $partation[3];
                $rate_use_disk = abs($partation[4]);

                if ( $rate_use_disk >= $opt_c ) {
                        $status = 'CRITICAL';
                }
                elsif (( $opt_w <= $rate_use_disk ) and ( $rate_use_disk < $opt_c )) {
                        $status = 'WARNING';
                }
                print "$name_partation $status: Total is $total_disk MB, FreeDisk is $free_disk, UseDisk% is $rate_use_disk%\n";
                if ($status =~ 'CRITICAL') {
                        $flag = 1
                }
        }
        if ( $flag == 1 ) {
                exit "$ERRORS{CRITICAL}"
        }
        else {
                exit "$ERRORS{$status}";
        }
}
}
脚本结束

你可能感兴趣的:(Perl自定义开发Nagios检测磁盘所有分区使用率插件(NRPE端使用))