Tools:linux下shell监控脚本,超过阈值邮件告警

install.sh

#!/bin/sh
if [ $# -ne 1 ]; then
    echo "########################################################"
    echo "# 用法:sh install_monitor.sh 邮箱地址;                #"
    echo "# 如:sh install_monitor.sh chenjy@wangsu.com          #"
    echo "# 若邮箱地址多个用英文逗号分隔                         #"
    echo "########################################################"
    exit 1
fi
#安装sendmail
/usr/bin/chattr -i /etc/shadow /etc/passwd /etc/gshadow /etc/group /etc/group-
mkdir /var/spool/clientmqueue;chmod 777 /var/spool/clientmqueue

for i in `seq 1 3`
do
    echo $i
    rpm -q --quiet sendmail
    if [ $? == 1 ]; then
        yum install -y sendmail
        if [ $? == 1 ]; then
            echo "######ERROR!sendmail install fail!######"
            echo "######Please try [yum install -y sendmail]######"
            exit
        fi
    fi
    rpm -q --quiet sendmail
    if [ $? == 0 ]; then
        service sendmail restart
        if [ $? == 1 ]; then
            echo "######ERROR!sendmail start fail!######"
            echo "######Please try [service sendmail restart]######"
            exit
        fi
        chkconfig --add sendmail
        break
    fi
    done
#下载监控脚本及启动后台执行
for i in `seq 1 3`
do
    echo $i
    if [ -f wsmonitor.sh ];then
        echo "wsmonitor.sh in current dir"
        nohup sh wsmonitor.sh $1 > wsmonitor.log &
        echo "######install success#####"
        exit
    fi
    if [ ! -f wsmontor.sh ]; then
        wget http://10.8.225.126/wsmonitor/wsmonitor.sh
    fi
done
echo "##### ERROR! download script fail! #####"
echo "##### Please try [wget http://10.8.225.126/wsmonitor/wsmonitor.sh] #####"
View Code

wsmonitor.sh

#!/bin/bash
#System Monitoring Script
if [ $# -ne 1 ]; then
    echo "########################################################"
    echo "# 用法:sh wsmonitor.sh 邮箱地址;                      #"
    echo "# 如:sh wsmonitor.sh chenjy@wangsu.com                #"
    echo "# 若邮箱地址多个用英文逗号分隔                         #"
    echo "########################################################"
    exit 1
fi
while [ 1 ]
do
    #配置多个收件人用英文逗号隔开
    #MAILTO=chenjy@wangsu.com,qinjy@wangsu.com
    #MAILTO=chenjy@wangsu.com
    MAILTO=$1
    #####1、设置脚本运行间隔时间。单位(秒)。
    RUNTIME=3600
    #####2、内存使用率监控设置,单位 (%)
    MEMTHRE=60
    #####3、流量监控设置
    #要监控的网卡
    ETHX=eth0
    #填写大于多少MB的时候发送警报,单位(MB)
    INFLOWTHREMB=1024
    OUTFLOWTHREMB=1024
    #填写大于多少KB的时候发送警报,单位(KB)
    INFLOWTHREKB=8000
    OUTFLOWTHREKB=8000
    #########################
    #####4、TCP连接状态数量监控设置
    #填写最大连接的个数
    TIME_WAIT=4000
    FIN_WAIT1=500
    FIN_WAIT2=200
    ESTABLISHED=4000
    SYN_RECV=100
    CLOSE_WAIT=100
    CLOSING=1000
    LAST_ACK=3000
    #####5、CPU使用率监控设置
    #填写cpu使用率大于多少发送报警,单位(%)
    CPUTHRE=60
    #####6、硬盘使用大小设置
    #填写硬盘占用率,单位(%)
    ROOT=80
    VAR=100
    USR=100
    BOOT=80
############################################################
################!以下脚本内容不需要修改!!################
############################################################
    #获取本机IP
    SERVER_IP=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
    #调试模式开关。(YES/NO)
    #DBUG=NO
    #定义发送邮件的内容
    MAILSUBJECT="WARNING_[$SERVER_IP]_"
    warn_type_mem="MEM"
    warn_type_flow="FLOW"
    warn_type_conn="CONNECTIONS"
    warn_type_disk="DISK"
    warn_type_cpu="CPU"
    function do_sendmail()
    {
    sendmail -t <<EOF
to:$MAILTO
subject:$MAILSUBJECT$1
Content-Type: text/html;charset=utf-8
<html>
<body>
$time $1,$2
</body></html>

EOF
    }
#[资源类型]$1
#[内容]$2
#[报告时间]$time
    function set_mailcontemt()
    {
    if [ "$1" == "mem" ];then
        warn_content="当前内存占用率为$2%,大于$3%,详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "flow_mb_in" ];then
        echo $2 $3
        warn_content="当前流量异常,当前速率$2(MB/秒),大于监控阈值$3(MB/秒).详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "flow_mb_out" ];then
        warn_content="当前流量异常,当前速率$2MB/秒,大于监控阈值$3MB/秒.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "flow_kb_in" ];then
        warn_content="当前流量异常,当前速率$2(KB/秒),大于监控阈值$3(KB/秒).详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "flow_kb_out" ];then
        warn_content="当前流量异常,当前速率$2(KB/秒),大于监控阈值$3(KB/秒).详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "conn_established" ];then
        warn_content="当前ESTABLISHED连接数异常,当前连接数为$2个,大于监控阈值$3个.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "conn_syn_recv" ];then        
        warn_content="当前SYN_RECV连接数异常,当前连接数为$2个,大于监控阈值$3个.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "conn_close_wait" ];then
        warn_content="当前CLOSE_WAIT连接数异常,当前连接数为$2个,大于监控阈值$3个.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "conn_close_closing" ];then    
        warn_content="当前CLOSING连接数异常,当前连接数为$2个,大于监控阈值$3个.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "conn_last_ack" ];then    
        warn_content="当前LAST_ACK连接数异常,当前连接数为$2个,大于监控阈值$3个.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "conn_time_wait" ];then    
        warn_content="当前TIME_WAIT连接数异常,当前连接数为$2个,大于监控阈值$3个.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "conn_fin_wait1" ];then    
        warn_content="当前FIN_WAIT1连接数异常,当前连接数为$2个,大于监控阈值$3个.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "conn_fin_wait2" ];then    
        warn_content="当前FIN_WAIT2连接数异常,当前连接数为$2个,大于监控阈值$3个.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "disk_root" ];then
        warn_content="当前[/]分区磁盘空间异常,当前使用率为$2%,大于监控阈值$3%.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "disk_var" ];then
        warn_content="当前[/var]分区磁盘空间异常,当前使用率为$2%,大于监控阈值$3%.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "disk_boot" ];then
        warn_content="当前[/boot]分区磁盘空间异常,当前使用率为$2%,大于监控阈值$3%.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "disk_usr" ];then
        warn_content="当前[/usr]分区磁盘空间异常,当前使用率为$2%,大于监控阈值$3%.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "cpu" ];then
        warn_content="当前CPU使用率异常,当前使用率为$2%,大于监控阈值$3%.详情登陆系统查看[$SERVER_IP]."
    fi    
    echo $warn_content
    }
    
    # 监 控 脚 本 执 行 内 容
    ################################################################################
    time=`date +"%Y-%m-%d %H:%M:%S"`
    #内存监控部分
    NULL=/dev/null
    #对于操作系统来说,buffers/caches是已经被使用的,对于Mem:这一行
#    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}'`
    #对于应用程序来说,buffers/caches是未被使用的,对应-/+ buffers/cache那一行
    memuse=`free -m|grep "buffers/cache"|awk '{print $3}'`
    memtotal=`free -m|grep "Mem"|awk '{print $2}'`
    memory=`echo "$memuse/$memtotal*100"|bc -l|cut -d. -f1`
    MA=`expr $memory > $MEMTHRE `
    #判断是否超过阈值
    if [ $MA -eq 1 ] ; then
        warn_content=`set_mailcontemt mem $MEM $MEMTHRE`
        do_sendmail $warn_type_mem $warn_content 
    fi
    MB=`expr $MEMB > $MEMTHRE`
    if [ $MB -eq 1 ] ; then
        warn_content=`set_mailcontemt mem $MEM $MEMTHRE`
        do_sendmail $warn_type_mem $warn_content 
    fi
    #流量监控部分
    FLOWA=/tmp/.flow
    ifconfig $ETHX |grep "RX byte" |awk '{print $2" "$6}' |awk -Fbytes: '{print "INPUT "$2"OUTPUT "$3}' > $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
            warn_content=`set_mailcontemt flow_mb_in $INMB $INFLOWTHREMB`
            do_sendmail $warn_type_flow $warn_content 
        fi
        if [ $OUTMB -gt $OUTFLOWTHREMB ] ;then
            warn_content=`set_mailcontemt flow_mb_out $OUTMB $OUTFLOWTHREMB`
            do_sendmail $warn_type_flow $warn_content 
        fi
    else
        INKBF=/tmp/.inputKB
        OUTKBF=/tmp/.outputKB
        echo $INPUTMBA KB > $INKBF
        echo $OUTPUTMBA KB > $OUTKBF
        INKB=`cat $INKBF |awk '{print $1}'`
        OUTKB=`cat $OUTKBF |awk '{print $1}'`
        if [ $INKB -gt $INFLOWTHREKB ] ; then
            warn_content=`set_mailcontemt flow_kb_in $INKB $INFLOWTHREKB`
            do_sendmail $warn_type_flow $warn_content 
        fi
        if [ $OUTKB -gt $OUTFLOWTHREKB ] ;then
            warn_content=`set_mailcontemt flow_kb_out $OUTKB $OUTFLOWTHREKB`
            do_sendmail $warn_type_flow $warn_content
        fi
    fi
    #连接数
    tcpfile=/tmp/.tcp
    netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' >$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
        warn_content=`set_mailcontemt conn_established $ESTABLISHEDV $ESTABLISHED`
        do_sendmail $warn_type_conn $warn_content 
    fi
    if [ $SYN_RECVV -gt $SYN_RECV ] ; then
        warn_content=`set_mailcontemt conn_syn_recv $SYN_RECVV $SYN_RECV`
        do_sendmail $warn_type_conn $warn_content 
    fi
    if [ $CLOSE_WAITV -gt $CLOSE_WAIT ] ; then
        warn_content=`set_mailcontemt conn_close_wait $CLOSE_WAITV $CLOSE_WAIT`
        do_sendmail $warn_type_conn $warn_content 
    fi
    if [ $CLOSINGV -gt $CLOSING ] ; then
        warn_content=`set_mailcontemt conn_close_closing $CLOSINGV $CLOSING`
        do_sendmail $warn_type_conn $warn_content 
    fi
    if [ $LAST_ACKV -gt $LAST_ACK ] ; then
        warn_content=`set_mailcontemt conn_last_ack $LAST_ACKV $LAST_ACK`
        do_sendmail $warn_type_conn $warn_content 
    fi
    if [ $TIME_WAITV -gt $TIME_WAIT ] ; then
        warn_content=`set_mailcontemt conn_time_wait $TIME_WAITV $TIME_WAIT`
        do_sendmail $warn_type_conn $warn_content 
    fi
    if [ $FIN_WAIT1V -gt $FIN_WAIT1 ] ; then
        warn_content=`set_mailcontemt conn_fin_wait1 $FIN_WAIT1V $FIN_WAIT1`
        do_sendmail $warn_type_conn $warn_content 
    fi
    if [ $FIN_WAIT2V -gt $FIN_WAIT2 ] ; then    
        warn_content=`set_mailcontemt conn_fin_wait2 $FIN_WAIT2V $FIN_WAIT2`
        do_sendmail $warn_type_conn $warn_content 
    fi
    DISKF=/tmp/.disk
    df -h > $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
        warn_content=`set_mailcontemt disk_root $ROOTV $ROOT`
        do_sendmail $warn_type_disk $warn_content 
    fi
    if [ $VARV -gt $VAR ] ; then
        warn_content=`set_mailcontemt disk_var $VARV $VAR`
        do_sendmail $warn_type_disk $warn_content 
    fi
    if [ $BOOTV -gt $BOOT ] ; then
        warn_content=`set_mailcontemt disk_boot $BOOTV $BOOT`
        do_sendmail $warn_type_disk $warn_content 
    fi
    if [ $USRV -gt $USR ] ; then
        warn_content=`set_mailcontemt disk_usr $USRV $USR`
        do_sendmail $warn_type_disk $warn_content 
    fi
    CPURATE=`top -b -n 1 |grep Cpu | awk '{print $2}' |awk -F. '{print $1}'`
    CB=`expr $CPURATE > $CPUTHRE`
    if [ $CB -eq 1 ] ; then
        warn_content=`set_mailcontemt cpu $CPUTHRE $CPURATE`
        do_sendmail $warn_type_cpu $warn_content 
    fi
    DBUGS=YES
    if [ "$DBUGS" == "$DBUG" ] ; then
        echo " "'== 内 存 ==' ;echo  ;
        echo " "当前程序占用内存为 $MEMB % ,总占用内存为 $MEM % ; echo  ;
        echo " "'== 流 量 =='; echo  ;
        YA=`wc -l $INMBF > /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
View Code

wsmonitor_update.sh

#!/bin/bash
#System Monitoring Script
if [ $# -ne 1 ]; then
    echo "########################################################"
    echo "# 用法:sh wsmonitor.sh 邮箱地址;                      #"
    echo "# 如:sh wsmonitor.sh chenjy@wangsu.com                #"
    echo "# 若邮箱地址多个用英文逗号分隔                         #"
    echo "########################################################"
    exit 1
fi
while [ 1 ]
do
    #配置多个收件人用英文逗号隔开
    #MAILTO=chenjy@wangsu.com,qinjy@wangsu.com
    #MAILTO=chenjy@wangsu.com
    MAILTO=$1
    #####1、设置脚本运行间隔时间。单位(秒)。
    TIME_INTERVAL=3600
    #####2、内存使用率监控设置,单位 (%)
    MEM_RATE=60
    #####3、流量监控设置
    #要监控的网卡
    ETH_NAME=eth0
    #大于多少MB的时候发送警报,单位(MB)
    INFLOW_MB=1024
    OUTFLOW_MB=1024
    #大于多少KB的时候发送警报,单位(KB)
    INFLOW_KB=8000
    OUTFLOW_KB=8000
    #########################
    #####4、TCP连接状态数量监控设置
    #填写最大连接的个数
    TIME_WAIT=4000
    FIN_WAIT1=500
    FIN_WAIT2=200
    ESTABLISHED=4000
    SYN_RECV=100
    CLOSE_WAIT=100
    CLOSING=1000
    LAST_ACK=3000
    #####5、CPU使用率监控设置
    #填写cpu使用率大于多少发送报警,单位(%)
    CPU_RATE=60
    #####6、硬盘使用大小设置
    #填写硬盘占用率,单位(%)
    ROOT=80
    VAR=100
    USR=100
    BOOT=80
############################################################
################!以下脚本内容不需要修改!!################
############################################################
    #获取本机IP
    SERVER_IP=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
    #调试模式开关。(YES/NO)
    #DBUG=NO
    #定义发送邮件的内容
    MAILSUBJECT="WARNING_[$SERVER_IP]_"
    warn_type_mem="MEM"
    warn_type_flow="FLOW"
    warn_type_conn="CONNECTIONS"
    warn_type_disk="DISK"
    warn_type_cpu="CPU"
    function do_sendmail()
    {
    sendmail -t <<EOF
to:$MAILTO
subject:$MAILSUBJECT$1
Content-Type: text/html;charset=utf-8
<html>
<body>
$time $1,$2
</body></html>

EOF
    }
#[资源类型]$1
#[内容]$2
#[报告时间]$time
    function set_mailcontemt()
    {
    if [ "$1" == "mem" ];then
        warn_content="当前内存占用率为$2%,大于$3%,详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "flow_mb_in" ];then
        echo $2 $3
        warn_content="当前流量异常,当前速率$2(MB/秒),大于监控阈值$3(MB/秒).详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "flow_mb_out" ];then
        warn_content="当前流量异常,当前速率$2MB/秒,大于监控阈值$3MB/秒.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "flow_kb_in" ];then
        warn_content="当前流量异常,当前速率$2(KB/秒),大于监控阈值$3(KB/秒).详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "flow_kb_out" ];then
        warn_content="当前流量异常,当前速率$2(KB/秒),大于监控阈值$3(KB/秒).详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "conn_established" ];then
        warn_content="当前ESTABLISHED连接数异常,当前连接数为$2个,大于监控阈值$3个.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "conn_syn_recv" ];then        
        warn_content="当前SYN_RECV连接数异常,当前连接数为$2个,大于监控阈值$3个.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "conn_close_wait" ];then
        warn_content="当前CLOSE_WAIT连接数异常,当前连接数为$2个,大于监控阈值$3个.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "conn_close_closing" ];then    
        warn_content="当前CLOSING连接数异常,当前连接数为$2个,大于监控阈值$3个.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "conn_last_ack" ];then    
        warn_content="当前LAST_ACK连接数异常,当前连接数为$2个,大于监控阈值$3个.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "conn_time_wait" ];then    
        warn_content="当前TIME_WAIT连接数异常,当前连接数为$2个,大于监控阈值$3个.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "conn_fin_wait1" ];then    
        warn_content="当前FIN_WAIT1连接数异常,当前连接数为$2个,大于监控阈值$3个.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "conn_fin_wait2" ];then    
        warn_content="当前FIN_WAIT2连接数异常,当前连接数为$2个,大于监控阈值$3个.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "disk_root" ];then
        warn_content="当前[/]分区磁盘空间异常,当前使用率为$2%,大于监控阈值$3%.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "disk_var" ];then
        warn_content="当前[/var]分区磁盘空间异常,当前使用率为$2%,大于监控阈值$3%.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "disk_boot" ];then
        warn_content="当前[/boot]分区磁盘空间异常,当前使用率为$2%,大于监控阈值$3%.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "disk_usr" ];then
        warn_content="当前[/usr]分区磁盘空间异常,当前使用率为$2%,大于监控阈值$3%.详情登陆系统查看[$SERVER_IP]."
    elif [ $1 == "cpu" ];then
        warn_content="当前CPU使用率异常,当前使用率为$2%,大于监控阈值$3%.详情登陆系统查看[$SERVER_IP]."
    fi    
    echo $warn_content
    }
    
    # 监 控 脚 本 执 行 内 容
    ################################################################################
    time=`date +"%Y-%m-%d %H:%M:%S"`
    #内存监控部分
    NULL=/dev/null
    #对于操作系统来说,buffers/caches是已经被使用的,对于Mem:这一行
#    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}'`
    #对于应用程序来说,buffers/caches是未被使用的,对应-/+ buffers/cache那一行
    memuse=`free -m|grep "buffers/cache"|awk '{print $3}'`
    memtotal=`free -m|grep "Mem"|awk '{print $2}'`
    memory=`echo "$memuse/$memtotal*100"|bc -l|cut -d. -f1`
    MA=`expr $memory > $MEM_RATE `
    #判断是否超过阈值
    if [ $MA -eq 1 ] ; then
        warn_content=`set_mailcontemt mem $MEM $MEM_RATE`
        do_sendmail $warn_type_mem $warn_content 
    fi
    MB=`expr $MEMB > $MEM_RATE`
    if [ $MB -eq 1 ] ; then
        warn_content=`set_mailcontemt mem $MEM $MEM_RATE`
        do_sendmail $warn_type_mem $warn_content 
    fi
    
    sleep $TIME_INTERVAL
done
View Code

wsmonitor_keepalive.sh

#!/bin/sh
script_path=`pwd`
wsmonitor_process=`ps -ef|grep "sh wsmonitor.sh"|grep -v grep|wc -l`
if [ $wsmonitor_process==0 ]; then
    echo "$script_path/wsmonitor.sh $1 is not running!"
    nohup sh $script_path/wsmonitor.sh $1 &
fi
View Code
每天努力一点,每天学习一点。 Keep Moving...
原文地址:https://www.cnblogs.com/channy14/p/12982565.html