1.脚本如下~
- #!/bin/bash
- #=========================================
- #File: check_vm.sh
- #Description:
- #Author: smileliuyb
- #Modify: smileliuyb
- #Creation Date: 2013-1-14
- #Last Modified: 2013-1-14
- #=========================================
- #======check-arg===============
- if [[ $4 != "" ]];then
- echo $2 $4 | awk '$1 !~ /^[0-9]*[0-9,M,m,G,g]$/ || $2 !~ /^[0-9]*[0-9,m,g,M,G]$/{print "User like ./check_vm.sh -w freememory[M,G] -c freememory[M,G]";exit 3}'
- if [ $? -eq 3 ];then
- exit 3
- fi
- if [[ $1 == '-w' ]] && [[ $3 == '-c' ]];then
- warning=`echo $2 | awk '$1 ~ /[0-9]$/{print $1};$1 ~ /[m,M]$/{print $1*1024};$1 ~ /[g,G]$/{print $1*1024*1024}'`
- critical=`echo $4 | awk '$1 ~ /[0-9]$/{print $1};$1 ~ /[m,M]$/{print $1*1024};$1 ~ /[g,G]$/{print $1*1024*1024}'`
- elif [[ $1 == '-c' ]] && [[ $3 == '-w' ]];then
- warning=`echo $4 | awk '$1 ~ /[0-9]$/{print $1};$1 ~ /[m,M]$/{print $1*1024};$1 ~ /[g,G]$/{print $1*1024*1024}'`
- critical=`echo $2 | awk '$1 ~ /[0-9]$/{print $1};$1 ~ /[m,M]$/{print $1*1024};$1 ~ /[g,G]$/{print $1*1024*1024}'`
- else
- echo "User like ./check_vm.sh -w freememory[M,G] -c freememory[M,G]"
- exit 3
- fi
- else
- echo "User like ./check_vm.sh -w freememory[M,G] -c free[M,G]"
- exit 3
- fi
- #====see-mem and comparison with arg========
- freemem=`free -m | cut -d " " -f17`
- if [ $(( freemem*1024 )) -le $critical ];then
- echo "Insufficient memory,Only"$freemem"M are free!"
- exit 2
- elif [ $(( freemem*1024 )) -le $warning ];then
- echo "Memory Warning,Only"$freemem"M are free!"
- exit 1
- else
- echo "Memory OK"
- exit 0
- fi