监控 linux 内存free

 linuxlogo
被监控端操作

注释:
脚本中有两种默认.
按照剩余内存
$check_file -w 2548 -c 2048 -b 1
按照剩余内存百分比
$check_file -w 25 -c 20 -b 0
$check_file -w 25 -c 20
1 在nagios-plugins-nrpe安装目录(如/usr/local/nagios/libexec/)添加插件check_mem_free.sh
check_name="check_mem_free"

nagios_etcdir="/usr/local/nagios/etc"
nagios_libdir="/usr/local/nagios/libexec"
check_file="$nagios_libdir/$check_name.sh"
cd $nagios_libdir
rz -bey
2.赋权
chown nagios.nagios $nagios_dir/$check_file
chmod +x  $nagios_dir/$check_file
3.  在nrpe中增加command(监控项目)
echo "command[$check_name]=$nagios_dir/$check_file -w 2597152 -c 2097152 -b 1" >> $nagios_etcdir/nrpe.cfg
4.验证
tail $nagios_etcdir/nrpe.cfg
5. 重启nagios-plugins-nrp
bash /usr/local/nagios/bin/restart
脚本:
2014年11月23日
#!/bin/sh
# Filename: check_mem_free.sh
function help {
 echo -e "\nStatus (Analysis of the)\
n\n\t$0:\n\t\t-c <integer>\tIf the % of Unknown is above <integer>, returns CRITICAL state\n\t\t-w <integer>\tIf the % of Unknown is below CRITICAL and above <integer>, returns WARNING state\n\t\t-b <number>\tPatter\n\t\t 0 >>>>>>Percentage\n\t\t 1 >>>>>>Numerical"
 exit -3
}
calc=/tmp/memcalc
# Getting parameters:
while getopts "w:c:b:h" OPT; do
 case $OPT in
  "w") warning=$OPTARG;;
  "c") critical=$OPTARG;;
  "b") Pattern=$OPTARG;;
  "h") help;;
 esac
done
# Checking parameters:
( [ "$warning" == "" ] || [ "$critical" == "" ] ) && echo "ERROR: You must specify warning and critical levels" && help
[[ "$critical" -ge "$warning"    ]] && echo "ERROR: critical level must be highter than warning level" && help
function Percentage_def() {
    echo "5" > $calc # decimal accuracy
    echo "k" >> $calc # commit
    echo "100" >> $calc # multiply
    echo "$free" >> $calc # division integer
    echo "$total" >> $calc # division integer
    echo "/" >> $calc # division sign
    echo "*" >> $calc # multiplication sign
    echo "p" >> $calc # print
    percent=`/usr/bin/dc $calc|/bin/sed 's/^\./0./'|/usr/bin/tr "." " "|/usr/bin/gawk {'print $1'}`
    if [[ $percent -lt $critical ]]; then
        results="CRITICAL - $free MB ($percent%) Free Memory"
        status=2
    else if [[ $percent -lt $warning ]]; then
            results="WARNING - $free MB ($percent%) Free Memory"
            status=1
         else
            results="OK - $free MB ($percent%) Free Memory"
            status=0
         fi
    fi
}
function Numerical_def() {
    percent=$free
    if [[ $percent -lt $critical ]]; then
        results="CRITICAL - $free MB"
        status=2
    else if [[ $percent -lt $warning ]]; then
            results="WARNING - $free MB"
            status=1
         else
            results="OK - $free MB"
            status=0
         fi
    fi
}
function free_def() {
    # Total memory available
    total=`free -m | head -2 |tail -1 |gawk '{print $2}'`
    # Total memory used
    used=`free -m | head -2 |tail -1 |gawk '{print $3}'`
    # Calc total minus used
    free=`free -m | head -2 |tail -1 |gawk '{print $2-$3}'`
    if [[ -z $Pattern ]]; then
        Percentage_def
    else
        case $Pattern in
          '0') Percentage_def;;
          '1') Numerical_def;;
          *) help;;
        esac
    fi
}
free_def
# Printing the results:
echo "$results| 'free'=$percent;$warning;$critical;"
# Bye!
exit $status


发表评论

登录 后发表评论.