【问题】由于使用sar命令监控系统资源使用,有可能存在系统没有安装sar的情况
解决方案:
#yum -y install sysstat
初次执行的时候会存在问题 需要建立一个存放记录的文件【当天日期】sar -o 16
在被监控端也需要配置【略】
【注意】需要加入crontab 每天生成记录cpu命令的文件
#crontab -e 记得检查crontab任务是否启动
1 0 * * * /usr/lib64/sa/sa1
2、内存监控
vi check_mem.sh
#!/bin/bash
#DESC: OS mem check
#Author:James
function help {
echo -e "\n\tThis plugin shows the % of used MEM, using free (whichever is available)\n\n\t$0:\n\t\t-c <integer>\tIf the % of used MEM is above <integer>, returns CRITICAL state\n\t\t-w <integer>\tIf the % of used MEM is below CRITICAL and above <integer>, returns WARNING state\n"
exit -1
}
while getopts "w:c:h" OPT; do
case $OPT in
"w") warning=$OPTARG;;
"c") critical=$OPTARG;;
"h") help;;
esac
done
set `free|head -2|tail -1`
MEMTOTAL=$2
MEMUSED=$3
MEMFREE=$4
MEMBUFFERS=$6
MEMCACHED=$7
REALMEMUSED=`echo $MEMUSED - $MEMBUFFERS - $MEMCACHED | bc`
USEPCT=`echo "scale=3; $REALMEMUSED / $MEMTOTAL * 100" |bc -l`
REALMEMUSEDmb=`echo "($REALMEMUSED)/1024" | bc`
MEMTOTALMB=`echo "($MEMTOTAL)/1024"|bc`
if [ `echo "$USEPCT > $critical" |bc` == 1 ];then
echo "MEM CRITICAL - Memory usage = ${USEPCT}%,MEMTOTAL=${MEMTOTALMB}MB,RealUsed=${REALMEMUSEDmb}MB |Used=${USEPCT}%;$warning;$critical"
exit 2
elif [ `echo "$USEPCT > $warning" |bc` == 1 ];then
echo "MEM WARNING - Memory usage = ${USEPCT}%,MEMTOTAL=${MEMTOTALMB}MB,RealUsed=${REALMEMUSEDmb}MB |Used=${USEPCT}%;$warning;$critical"
exit 1
elif [ `echo "$USEPCT < $warning" |bc` == 1 ];then
echo "MEM OK - Memory usage = ${USEPCT}%,MEMTOTAL=${MEMTOTALMB}MB,RealUsed=${REALMEMUSEDmb}MB|Used=${USEPCT}%;$warning;$critical"
exit 0
else
echo "MEM ERROR - Unable to determine memory usage"
exit 3
fi
echo "Unable to determine memory usage."
exit 3
3、LVS监控
vi check_lvs.sh
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 #!/bin/bash
USAGE_Method="$(basename $0)[-h|--hostname] <Free ip or hostname> [-w|--warning] <Free integer> [-c|--critical] <Free integer>"
USAGE_Value="warning value must be small than critical value: `basename $0` $*"
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
if [ $# -lt 4 ];then
echo "Usage:$USAGE_Method"
fi
while [ $# -gt 0 ];
do
case "$1" in
-w|--warning)
shift
warning=$1
;;
-c|--critical)
shift
critical=$1
;;
esac
shift
done
if [[ $warning == $critical || $warning -gt $critical ]];then
#echo $warning
#echo $critical
echo "$USAGE_Value"
echo "Usage: $USAGE_Method"
exit 0
fi
ACT_COUNT=0
Inactive_count=0
stat1=`sudo ipvsadm | grep http | grep Route|wc -l`
if [ $stat1 -ne 0 ];then
for NUM in `sudo ipvsadm | grep http | grep Route | awk '{print $5}'`
do
ACT_COUNT=$(($ACT_COUNT+ $NUM))
done
for NUM in `sudo ipvsadm | grep http | grep Route | awk '{print $6}'`
do
Inactive_count=$(($Inactive_count+ $NUM))
done
else
echo " stat1:$stat1, lvs critical,lvs is down now."
exit 3
fi
4、MYSQL监控
在需要监控的mysql数据库上建一个专门给Nagios使用的库