nagios 监控内存使用情况

  1. 监控本机内存
    cd /usr/lib64/nagios/plugins
    touch check_mem.sh

    #!/bin/bash
    
    if [ "$1" = "-w" ] && [ "$2" -gt "0" ] && [ "$3" = "-c" ] && [ "$4" -gt "0" ]; then
    
            memTotal_b=`free -b |grep Mem |awk '{print $2}'`
            memFree_b=`free -b |grep Mem |awk '{print $4}'`
            memBuffer_b=`free -b |grep Mem |awk '{print $6}'`
            memCache_b=`free -b |grep Mem |awk '{print $7}'`
    
            memTotal_m=`free -m |grep Mem |awk '{print $2}'`
            memFree_m=`free -m |grep Mem |awk '{print $4}'`
            memBuffer_m=`free -m |grep Mem |awk '{print $6}'`
            memCache_m=`free -m |grep Mem |awk '{print $7}'`
    
            memUsed_b=$(($memTotal_b-$memFree_b-$memBuffer_b-$memCache_b))
            memUsed_m=$(($memTotal_m-$memFree_m-$memBuffer_m-$memCache_m))
    
            memUsedPrc=$((($memUsed_b*100)/$memTotal_b))
    
    
            if [ "$memUsedPrc" -ge "$4" ]; then
                    echo "Memory: CRITICAL Total: $memTotal_m MB - Used: $memUsed_m MB - $memUsedPrc% used!|TOTAL=$memTotal_b;;;; USED=$memUsed_b;;;; CACHE=$memCache_b;;;; BUFFER=$memBuffer_b;;;;"
                    $(exit 2)
            elif [ "$memUsedPrc" -ge "$2" ]; then
                    echo "Memory: WARNING Total: $memTotal_m MB - Used: $memUsed_m MB - $memUsedPrc% used!|TOTAL=$memTotal_b;;;; USED=$memUsed_b;;;; CACHE=$memCache_b;;;; BUFFER=$memBuffer_b;;;;"
                    $(exit 1)
            else
                    echo "Memory: OK Total: $memTotal_m MB - Used: $memUsed_m MB - $memUsedPrc% used|TOTAL=$memTotal_b;;;; USED=$memUsed_b;;;; CACHE=$memCache_b;;;; BUFFER=$memBuffer_b;;;;"
                    $(exit 0)
            fi
    
    else
            echo "check_mem v1.1"
            echo ""
            echo "Usage:"
            echo "check_mem.sh -w <warnlevel> -c <critlevel>"
            echo ""
            echo "warnlevel and critlevel is percentage value without %"
            echo ""
            echo "Copyright (C) 2012 Lukasz Gogolin (lukasz.gogolin@gmail.com)"
            exit
    fi
    check_mem.sh Code

    chmod +x check_mem.sh
    测试check_mem脚本是否能正常使用
    ./check_mem.sh -w 80 -c 90
    修改commands.cfg配置
    vi /etc/nagios/objects/commands.cfg

    define command{
            command_name        check_mem
            command_line        $USER1$/check_mem.sh -w $ARG1$ -c $ARG2$
            }
    commands.cfg Code

    vi /etc/nagios/objects/localhost.cfg

    define service{
            use                             local-service,srv-pnp
            host_name                       localhost
            service_description             check_mem
            check_command                   check_mem!80!90
            }
    localhost.cfg Code
  2. 监控被监控机内存使用情况
    拷贝check_mem文件到被监控机的/usr/lib64/nagios/plugins目录下
    scp check_mem root@serverip:/usr/lib64/nagios/plugins/
    修改nrpe.cfg
    vi /usr/local/nagios/etc/nrpe.cfg
    增加一行:command[check_mem]=/usr/lib64/nagios/plugins/check_mem.sh -w 80 -c 90  
    回到nagios监控机修改服务项
    vi /etc/nagios/objects/services.cfg
    增加如下监控代码
    define service{
            use                     generic-service
            host_name               Nagios_Centos_Client1
            service_description     Memery Monitoring
            check_command           check_nrpe!check_mem
            }
    Check_mem Code

    nagios -v nagios.cfg
    重启nagios即可

posted @ 2015-06-18 13:19  noops  阅读(347)  评论(0编辑  收藏  举报