监控系统资源的shell脚本

#!/bin/bash
#System Monitoring Script
while [ 1 ]
do
#本机需开启postfix或sendmail服务。
#报警邮件地址设置
[email protected]
[email protected]
#设置脚本运行间隔时间。单位(秒)。
RUNTIME=1
#内存使用率监控设置,单位 (%)
MEMTHRE=90
#流量监控设置
#要监控的网卡
ETHX=eth0
#填写大于多少MB的时候发送警报,单位(MB)
INFLOWTHREMB=50
OUTFLOWTHREMB=50
#填写大于多少KB的时候发送警报,单位(KB)
INFLOWTHREKB=500
OUTFLOWTHREKB=500
#TCP连接状态数量监控设置
#填写最大连接的个数
TIME_WAIT=10
FIN_WAIT1=20
FIN_WAIT2=20
ESTABLISHED=20
SYN_RECV=10
CLOSE_WAIT=10
CLOSING=10
LAST_ACK=10
#CPU使用率监控设置
#填写cpu使用率大于多少发送报警,单位(%)
CPUTHRE=5
#硬盘使用大小设置
#填写硬盘占用率,单位(%)
ROOT=80
VAR=100
USR=100
BOOT=80
#调试模式开关。(YES/NO)
DBUG=NO
# 监 控 脚 本 执 行 内 容
################################################################################
time=`date +”%Y-%m-%d %H:%M:%S”`
#内存监控部分
NULL=/dev/null
MEM=`free -m |grep Mem |awk ‘{print $3/$2*100}’`
MEMB=`free -m |grep Mem |awk ‘{print $2,$4+$6+$7}’|awk ‘{print $1,$1-$2}’| awk ‘{print $2/$1*100}’`
#echo $MEM
#echo $MEMB
MA=`expr $MEM > $MEMTHRE `
if [ $MA -eq 1 ] ; then
sendmail -t < $MEMTHRE`
if [ $MB -eq 1 ] ; then
sendmail -t < $FLOWA
INPUTA=`cat $FLOWA |awk ‘{print $2}’`
OUTPUTA=`cat $FLOWA |awk ‘{print $4}’`
sleep 1
ifconfig $ETHX |grep “RX byte” |awk ‘{print $2″ “$6}’ |awk -Fbytes: ‘{print “INPUT “$2″OUTPUT “$3}’ > $FLOWA
INPUTB=`cat $FLOWA |awk ‘{print $2}’`
OUTPUTB=`cat $FLOWA |awk ‘{print $4}’`
INPUTC=`echo “$INPUTB-$INPUTA” | bc`
OUTPUTC=`echo “$OUTPUTB-$OUTPUTA”| bc`
INPUTMBA=`echo “$INPUTC/1024″|bc`
OUTPUTMBA=`echo “$OUTPUTC/1024″|bc`
INMBF=/tmp/.inputMB
OUTMBF=/tmp/.outputMB
echo `echo “scale=4;$INPUTMBA/1024″|bc`MB > $INMBF
echo `echo “scale=4;$OUTPUTMBA/1024″|bc`MB > $OUTMBF
INMB=`cat $INMBF |awk ‘{print $1}’`
OUTMB=`cat $OUTMBF |awk ‘{print $1}’`
if [ $INPUTMBA -gt 1024 ] ; then
if [ $INMB -gt $INFLOWTHREMB ] ;then
sendmail -t < $INKBF
echo $OUTPUTMBA KB > $OUTKBF
INKB=`cat $INKBF |awk ‘{print $1}’`
OUTKB=`cat $OUTKBF |awk ‘{print $1}’`
if [ $INKB -gt $INFLOWTHREKB ] ; then
sendmail -t <$tcpfile
grep TIME_WAIT $tcpfile > $NULL
if [ $? -eq 1 ] ; then
echo “TIME_WAIT 0 ” >> $tcpfile
fi
grep FIN_WAIT1 $tcpfile > $NULL
if [ $? -eq 1 ] ; then
echo “FIN_WAIT1 0 ” >> $tcpfile
fi
grep FIN_WAIT2 $tcpfile > $NULL
if [ $? -eq 1 ] ; then
echo “FIN_WAIT2 0 ” >> $tcpfile
fi
grep CLOSE_WAIT $tcpfile > $NULL
if [ $? -eq 1 ] ; then
echo “CLOSE_WAIT 0 ” >> $tcpfile
fi
grep LAST_ACK $tcpfile > $NULL
if [ $? -eq 1 ] ; then
echo “LAST_ACK 0 ” >> $tcpfile
fi
grep SYN_RECV $tcpfile > $NULL
if [ $? -eq 1 ] ; then
echo “SYN_RECV 0 ” >> $tcpfile
fi
grep CLOSING $tcpfile > $NULL
if [ $? -eq 1 ] ; then
echo “CLOSING 0 ” >> $tcpfile
fi
grep ESTABLISHED $tcpfile > $NULL
if [ $? -eq 1 ] ; then
echo “ESTABLISHED 0 ” >> $tcpfile
fi

TIME_WAITV=`grep TIME_WAIT $tcpfile | awk ‘{print $2}’`
FIN_WAIT1V=`grep FIN_WAIT1 $tcpfile | awk ‘{print $2}’`
FIN_WAIT2V=`grep FIN_WAIT2 $tcpfile | awk ‘{print $2}’`
ESTABLISHEDV=`grep ESTABLISHED $tcpfile | awk ‘{print $2}’`
SYN_RECVV=`grep SYN_RECV $tcpfile | awk ‘{print $2}’`
CLOSINGV=`grep CLOSING $tcpfile | awk ‘{print $2}’`
CLOSE_WAITV=`grep CLOSE_WAIT $tcpfile | awk ‘{print $2}’`
LAST_ACKV=`grep LAST_ACK $tcpfile | awk ‘{print $2}’`
if [ $ESTABLISHEDV -gt $ESTABLISHED ] ; then
sendmail -t < $DISKF
grep var $DISKF > $NULL
if [ $? -eq 1 ] ; then
echo “/dev/sda1 20G 1.6G 17G 0% /var” >> $DISKF
fi
grep usr $DISKF > $NULL
if [ $? -eq 1 ] ; then
echo “/dev/sda1 20G 1.6G 17G 0% /usr” >> $DISKF
fi
grep boot $DISKF > $NULL
if [ $? -eq 1 ] ; then
echo “/dev/sda1 20G 1.6G 17G 0% /boot” >> $DISKF
fi
BOOTV=`cat $DISKF | grep boot | awk ‘{print $5}’|awk -F% ‘{print $1}’`
VARV=`cat $DISKF | grep var | awk ‘{print $5}’|awk -F% ‘{print $1}’`
USRV=`cat $DISKF | grep usr | awk ‘{print $5}’|awk -F% ‘{print $1}’`
grep VolGroup $DISKF > $NULL
if [ $? -eq 0 ] ;then
ROOTV=`cat $DISKF | sed -n ‘3p’ |awk ‘{print $4}’|awk -F% ‘{print $1}’`
else
ROOTV=`cat $DISKF | sed -n ‘2p’|awk ‘{print $5}’|awk -F% ‘{print $1}’`
fi
if [ $ROOTV -gt $ROOT ] ; then
sendmail -t < $CPUTHRE`
if [ $CA -eq 1 ] ; then
sendmail -t < $CPUTHRE`
if [ $CB -eq 1 ] ; then
sendmail -t < /dev/null 2>&1 ; echo $?`
if [ $YA -eq 1 ] ; then
echo 0 > $INMBF
fi
IN=`expr $INMB > $INFLOWTHREMB >$NULL; echo $? ` 2> $NULL
if [ $IN -eq 1 ] ; then
echo ” “当前输入流量为 $INMB MB/秒 .;
echo ” “当前输入流量为 $INKB KB/秒 .;
fi
YB=`wc -l $OUTMBF > /dev/null 2>&1 ; echo $?`
if [ $YB -eq 1 ] ; then
echo 0 > $OUTMBF
fi
OUT=`expr $OUTMB > $OUTFLOWTHREMB >$NULL ; echo $?` 2> $NULL
if [ $OUT -eq 1 ] ; then
echo ” “当前输出流量为 $OUTMB MB/秒。 ;
echo ” “当前输出流量为 $OUTKB KB/秒。 ; echo ;
fi
echo ” “‘& 连接数 &’ ; echo ;
echo ” “当前TIME_WAIT” ” 连接数为 $TIME_WAITV 个。
echo ” “当前FIN_WAIT1″ ” 连接数为 $FIN_WAIT1V 个。
echo ” “当前FIN_WAIT2″ ” 连接数为 $FIN_WAIT2V 个。
echo ” “当前CLOSE_WAIT” ” 连接数为 $CLOSE_WAITV 个。
echo ” “当前ESTABLISHED”” 连接数为 $ESTABLISHEDV 个。
echo ” “当前SYN_RECV” ” 连接数为 $SYN_RECVV 个。
echo ” “当前LAST_ACKV” ” 连接数为 $LAST_ACKV 个。
echo ” “当前CLOSING” ” 连接数为 $CLOSINGV 个。; echo ;
echo ” “‘& CPU使用率 &’ ; echo ;
echo ” “当前CPU 进程使用率为 $USERATE . ;echo ;
echo ” “‘& 磁盘使用率 &’ ; echo ;
echo ” “当前” “/” “分区,” “使用率为 $ROOTV %.
echo ” “当前/var 分区, 使用率为 $VARV %.
echo ” “当前/boot分区, 使用率为 $BOOTV %.
echo ” “当前/usr 分区, 使用率为 $USRV %.
exit
fi
sleep $RUNTIME
done

运行方法,在后台运行。
[root@AR log]# ./monitor &
停止方法
[root@AR log]# ps -aux |grep monitor | grep -v grep | awk ‘{print $2}’ | xargs kill -9