Perl开发自定义Nagios监视内存插件(nrpe端使用)

功能说明:脚本已经用在实践环境中,放到nrpe远程被监控端,用来实时监控远程主机内存。

监控内容包括:

free剩余大小;

总内存大小;

free剩余内存所占的百分比;

开发语言:perl

使用说明:提供三个参数,第一个是Memory表示针对内存进行监控,以后还提供对swap等其他的参数;第二个参数表示warning的阀值,20表示剩余内存小于等于20%时警告;第三个参数表示critical的阀值,10表示剩余内存不足10%时报警。举例如下:

# ./lionel_memory.pl Memory 20 10
Memory CRITICAL: FreeMem is 152488 KB, TotalMem is 3107192 KB, FreeMem% is 4% KB

其他细节:本脚本提供检测参数的功能,参数给出错误会提示Usage,warning的参数值一定要比critical的值大因为剩余内存越小警报级别才越高。最后,对剩余内存的算法我不是简单的使用free命令获取,而是收集的/proc/meminfo的值,并且考虑了Buffers与Cached两个值,反映真实的内存使用情况。还有,swap空间的使用并不代表物理内存完全耗尽,一般使用swap空间时物理内存还会剩余100多M的空间,但百分比肯定都不足10%了。

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

脚本名称是【lionel_memory.pl 】
#!/usr/bin/perl
# author:shenxiaoran

use strict;

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

my ($opt_w,$opt_c) = ($ARGV[1],$ARGV[2]);
my $free_memory;
my $memfree;
my $buffers;
my $cached;
my $total_memory;
my $rate_free_memory;

if ( !defined $ARGV[0] or !defined $ARGV[1] or !defined $ARGV[2] ) {
        print "Usage: $0 {Memory} -w num -c num \n";
        print "Example: lionel_memory.pl Memory 20 10 \n";
}
else {
        free_memory($ARGV[0]);

}

sub free_memory() {
# MemTotal|MemFree|Buffers|Cached|SwapCached|Active|Inactive|SwapTotal|SwapFree
        if ( $opt_w < $opt_c ) {
                print "Error: -w num > -c num \n";
                print "Usage: $0 {Memory} -w num -c num \n";
                print "Example: lionel_memory.pl Memory 20 10 \n";
                exit;
        }
        if ( $_[0] eq "Memory" ) {
                open(PROCESS, "cat /proc/meminfo|");
                    foreach (<PROCESS>) {
                        if ($_ =~ /(MemFree.*\s)(.*[0-9])( kB)/) {
                            $memfree = $2;
                        }
                        if ($_ =~ /(Buffers.*\s)(.*[0-9])( kB)/) {
                            $buffers = $2;
                        }
                        if ($_ =~ /(^Cached.*\s)(.*[0-9])( kB)/) {
                            $cached = $2;
                        }
                        if ($_ =~ /(MemTotal.*\s)(.*[0-9])( kB)/) {
                            $total_memory = $2;
                        }
                    }
                    $free_memory = $memfree + $buffers + $cached;
                    $rate_free_memory = int($free_memory / $total_memory * 100);
                close(PROCESS);

                my $status = 'OK';
                if ( $rate_free_memory < $opt_c ) {
                $status = 'CRITICAL';
                }
                elsif ( $rate_free_memory < $opt_w ) {
                $status = 'WARNING';
                }
                print "$ARGV[0] $status: FreeMem is $free_memory KB, TotalMem is $total_memory KB, FreeMem% is ";
                printf "%0.0f%% KB\n","$rate_free_memory";
                exit $ERRORS{$status};
        }
        else {
        print "Usage: $0 {Memory} -w num -c num \n";
                print "Example: lionel_memory.pl Memory 20 10 \n";
        }
}

脚本结束

状态截图:



转载于:https://my.oschina.net/lionel45/blog/151557

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值