首先看下内存信息吧

 

[root@localhost ~]# free -m

             total       used       free     shared    buffers     cached

Mem:           249        235         14          0          3         96

-/+ buffers/cache:        134        114

Swap:         2047         43       2003

 
一句话思路,取可用内存值(free)
free -m | awk 'NR==3{print $4}'   简单的取第三行第四列的值
 
#!/bin/bash
a=`free -m | awk 'NR==3{print $4}'`  
        if [ $a -ge 100 ]; then   大于等于100
        echo $a
        exit 0
elif [ $a -lt 100 ] && [ $a -gt 50 ];then     大于50小于100
        echo $a 
        exit 1
        else
        echo $a
        exit 2
fi
 

根据情况取值比如mem_free + cached,也可写成根据百分比判断

保存自定义插件到nagios下的插件目录 /usr/local/nagios/libexec/check_mem

配置/usr/local/nagios/etc/objects/localhost.cfg

 

define host {

        host_name       shenmeh

        alias           server1

        address         192.168.1.129

        check_command   check-host-alive

        notification_options    d,r

        check_interval          1

        max_check_attempts      2

        contact_groups          admins

        notification_interval   1

        notification_period     24x7

}

 

 

define service {

 

        host_name               shenmeh

        service_description     server1_apache_mem

        check_period            24x7

        normal_check_interval   2

        retry_check_interval    1

        max_check_attempts      2

        notification_period     24x7

        notification_options    w,u,c,r

        check_command   check_mem

}

配置/usr/local/nagios/etc/objects/commands.cfg
 
# 'check_mem' command definition
 
define command{
 
        command_name    check_mem
 
        command_line    $USER1$/check_mem
 
        }