標籤:shell指令碼 監控 系統 郵件
此指令碼可以做一個定時的檢測,超出設定的值,即往郵箱發送警告
指令碼用到bc,sendmail,163郵箱,
yum install bc
#!/bin/bash#System Monitoring Scriptwhile [ 1 ]do#本機需開啟postfix或sendmail服務。#警示郵件地址設定[email protected][email protected]#設定指令碼運行間隔時間。單位(秒)。RUNTIME=900#記憶體使用量率監控設定,單位 (%)MEMTHRE=80#流量監控設定#要監控的網卡ETHX=eth0#填寫大於多少MB的時候發送警報,單位(MB)INFLOWTHREMB=50OUTFLOWTHREMB=50#填寫大於多少KB的時候發送警報,單位(KB)INFLOWTHREKB=8000OUTFLOWTHREKB=8000#TCP串連狀態數量監控設定#填寫最大串連的個數TIME_WAIT=4000FIN_WAIT1=500FIN_WAIT2=200ESTABLISHED=4000SYN_RECV=100CLOSE_WAIT=100CLOSING=1000LAST_ACK=3000#CPU使用率監控設定#填寫cpu使用率大於多少發送警示,單位(%)CPUTHRE=60#硬碟使用大小設定#填寫硬碟佔用率,單位(%)ROOT=80VAR=100USR=100BOOT=80#偵錯模式開關。(YES/NO)DBUG=NO# 監 控 腳 本 執 行 內 容################################################################################time=`date +"%Y-%m-%d %H:%M:%S"`#記憶體監控部分NULL=/dev/nullMEM=`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}‘`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 ] ; thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time MEM記憶體警告,當前記憶體佔用率為$MEM %,大於$MEMTHRE %.EOFfiMB=`expr $MEMB \> $MEMTHRE`if [ $MB -eq 1 ] ; thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time MEMB記憶體警告,當前記憶體佔用率為$MEMB %,大於$MEMTHRE %EOFfi#流量監控部分FLOWA=/tmp/.flowifconfig $ETHX |grep "RX byte" |awk ‘{print $2" "$6}‘ |awk -Fbytes: ‘{print "INPUT "$2"OUTPUT "$3}‘\ > $FLOWAINPUTA=`cat $FLOWA |awk ‘{print $2}‘`OUTPUTA=`cat $FLOWA |awk ‘{print $4}‘`sleep 1ifconfig $ETHX |grep "RX byte" |awk ‘{print $2" "$6}‘ |awk -Fbytes: ‘{print "INPUT "$2"OUTPUT "$3}‘\ > $FLOWAINPUTB=`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/.inputMBOUTMBF=/tmp/.outputMBecho `echo "scale=4;$INPUTMBA/1024"|bc`MB > $INMBFecho `echo "scale=4;$OUTPUTMBA/1024"|bc`MB > $OUTMBFINMB=`cat $INMBF |awk ‘{print $1}‘`OUTMB=`cat $OUTMBF |awk ‘{print $1}‘`if [ $INPUTMBA -gt 1024 ] ; thenif [ $INMB -gt $INFLOWTHREMB ] ;thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time 流量警告,當前流量異常,請登入伺服器查看。當前速率$INMB MB/秒,大於$INFLOWTHREMB MB/秒。EOFfiif [ $OUTMB -gt $OUTFLOWTHREMB ] ;thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time 流量警告,當前流量異常,請登入伺服器查看。當前速率$OUTMB MB/秒 大於$OUTFLOWTHREMB MB/秒。EOFfielseINKBF=/tmp/.inputKBOUTKBF=/tmp/.outputKBecho $INPUTMBA KB > $INKBFecho $OUTPUTMBA KB > $OUTKBFINKB=`cat $INKBF |awk ‘{print $1}‘`OUTKB=`cat $OUTKBF |awk ‘{print $1}‘`if [ $INKB -gt $INFLOWTHREKB ] ; thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time 流量警告,當前流量異常,請登入伺服器查看。$INKB KB/秒 大於$INFLOWTHREKB KB/秒。EOFfiif [ $OUTKB -gt $OUTFLOWTHREKB ] ;thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time 流量警告,當前流量異常,請登入伺服器查看。當前速率$OUTKB KB/秒大於$INFLOWTHREKB KB/秒。EOFfifi#串連數tcpfile=/tmp/.tcpnetstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}‘ >$tcpfilegrep TIME_WAIT $tcpfile > $NULLif [ $? -eq 1 ] ; thenecho "TIME_WAIT 0 " >> $tcpfilefigrep FIN_WAIT1 $tcpfile > $NULLif [ $? -eq 1 ] ; thenecho "FIN_WAIT1 0 " >> $tcpfilefigrep FIN_WAIT2 $tcpfile > $NULLif [ $? -eq 1 ] ; thenecho "FIN_WAIT2 0 " >> $tcpfilefigrep CLOSE_WAIT $tcpfile > $NULLif [ $? -eq 1 ] ; thenecho "CLOSE_WAIT 0 " >> $tcpfilefigrep LAST_ACK $tcpfile > $NULLif [ $? -eq 1 ] ; thenecho "LAST_ACK 0 " >> $tcpfilefigrep SYN_RECV $tcpfile > $NULLif [ $? -eq 1 ] ; thenecho "SYN_RECV 0 " >> $tcpfilefigrep CLOSING $tcpfile > $NULLif [ $? -eq 1 ] ; thenecho "CLOSING 0 " >> $tcpfilefigrep ESTABLISHED $tcpfile > $NULLif [ $? -eq 1 ] ; thenecho "ESTABLISHED 0 " >> $tcpfilefiTIME_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 ] ; thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time 串連數警告,當前ESTABLISHED串連數異常,請登入伺服器查看。當前串連數為$ESTABLISHEDV個,大於$ESTABLISHED個EOFfiif [ $SYN_RECVV -gt $SYN_RECV ] ; thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time 串連數警告,當前SYN_RECV串連數異常,請登入伺服器查看。當前串連數為$SYN_RECVV個,大於$SYN_REC個。EOFfiif [ $CLOSE_WAITV -gt $CLOSE_WAIT ] ; thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time 串連數警告,當前CLOSE_WAIT串連數異常,請登入伺服器查看。當前串連數為$CLOSE_WAITV個,大於$CLOSE_WAIT個。EOFfiif [ $CLOSINGV -gt $CLOSING ] ; thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time 串連數警告,當前CLOSING串連數異常,請登入伺服器查看。當前串連數為$CLOSINGV個,大於$CLOSING個。EOFfiif [ $LAST_ACKV -gt $LAST_ACK ] ; thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time 串連數警告,當前LAST_ACK串連數異常,請登入伺服器查看。當前串連數為$LAST_ACKV個,大於$LAST_ACK個。EOFfiif [ $TIME_WAITV -gt $TIME_WAIT ] ; thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time 串連數警告,當前TIME_WAIT串連數異常,請登入伺服器查看。當前串連數為$TIME_WAITV個,大於$TIME_WAIT個。EOFfiif [ $FIN_WAIT1V -gt $FIN_WAIT1 ] ; thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time 串連數警告,當前FIN_WAIT1串連數異常,請登入伺服器查看。當前串連數為$FIN_WAIT1V個,大於$FIN_WAIT1個。EOFfiif [ $FIN_WAIT2V -gt $FIN_WAIT2 ] ; thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time 串連數警告,當前FIN_WAIT2串連數異常,請登入伺服器查看。當前串連數為$FIN_WAIT2V個,大於$FIN_WAIT2個。EOFfiDISKF=/tmp/.diskdf -h > $DISKFgrep var $DISKF > $NULLif [ $? -eq 1 ] ; thenecho "/dev/sda1 20G 1.6G 17G 0% /var" >> $DISKFfigrep usr $DISKF > $NULLif [ $? -eq 1 ] ; thenecho "/dev/sda1 20G 1.6G 17G 0% /usr" >> $DISKFfigrep boot $DISKF > $NULLif [ $? -eq 1 ] ; thenecho "/dev/sda1 20G 1.6G 17G 0% /boot" >> $DISKFfiBOOTV=`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 > $NULLif [ $? -eq 0 ] ;thenROOTV=`cat $DISKF | sed -n ‘3p‘ |awk ‘{print $4}‘|awk -F% ‘{print $1}‘`elseROOTV=`cat $DISKF | sed -n ‘2p‘|awk ‘{print $5}‘|awk -F% ‘{print $1}‘`fiif [ $ROOTV -gt $ROOT ] ; thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time 磁碟使用警告,您監控的 / 分區已經大於你設定的數值$ROOT %,詳情登陸系統查看,目前使用率為$ROOTV %.EOFfiif [ $VARV -gt $VAR ] ; thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time 磁碟使用警告,您監控的 /var 分區已經大於你設定的數值$VAR %,詳情登陸系統查看,目前使用率為$VARV %.EOFfiif [ $BOOTV -gt $BOOT ] ; thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time 磁碟使用警告,您監控的 /boot 分區已經大於你設定的數值 $BOOT %,詳情登陸系統查看,目前使用率為$BOOTV %.EOFfiif [ $USRV -gt $USR ] ; thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time 磁碟使用警告,您監控的 /usr 分區已經大於你設定的數值$USR %,詳情登陸系統查看,目前使用率為$USRV %.EOFfiCPURATE=`top -b -n 1 |grep Cpu | awk ‘{print $2}‘ |awk -F. ‘{print $1}‘`CB=`expr $CPURATE \> $CPUTHRE`if [ $CB -eq 1 ] ; thensendmail -t <<EOFfrom: $MAILFROMto:$MAILTOsubject:67warning$time 使用警告,您監控的CPU使用率,已經超過您設定的限額$CPUTHRE %,當前CPU使用率為$CPURATE .EOFfiDBUGS=YESif [ "$DBUGS" == "$DBUG" ] ; thenecho " "‘== 內 存 ==‘ ;echo \ ;echo " "當前程式佔用記憶體為 $MEMB % ,總佔用記憶體為 $MEM % ; echo \ ;echo " "‘== 流 量 ==‘; echo \ ;YA=`wc -l $INMBF > /dev/null 2>&1 ; echo $?`if [ $YA -eq 1 ] ; thenecho 0 > $INMBFfiIN=`expr $INMB \> $INFLOWTHREMB >$NULL; echo $? ` 2> $NULLif [ $IN -eq 1 ] ; thenecho " "當前輸入資料流量為 $INMB MB/秒 .;echo " "當前輸入資料流量為 $INKB KB/秒 .;fiYB=`wc -l $OUTMBF > /dev/null 2>&1 ; echo $?`if [ $YB -eq 1 ] ; thenecho 0 > $OUTMBFfiOUT=`expr $OUTMB \> $OUTFLOWTHREMB >$NULL ; echo $?` 2> $NULLif [ $OUT -eq 1 ] ; thenecho " "當前輸出資料流量為 $OUTMB MB/秒。 ;echo " "當前輸出資料流量為 $OUTKB KB/秒。 ; echo \ ;fiecho " "‘== 串連數 ==‘ ; 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 %.exitfisleep $RUNTIMEdone
指令碼42行,先改為DBUG=YES,運行一次,如果沒問題,出現下面提示
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/27/23/wKiom1NwjyWSATs0AAJ7tAelj9o671.jpg" title="QQ20140512170248.jpg" alt="wKiom1NwjyWSATs0AAJ7tAelj9o671.jpg" />
再把DBUG=NO改回來
附,郵件通知
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/27/23/wKioL1Nwj0PBXIq_AABwHhEFA7k537.jpg" title="QQ20140512170358.jpg" alt="wKioL1Nwj0PBXIq_AABwHhEFA7k537.jpg" />
參考http://yangji.blog.51cto.com/1155906/929250,有改動
本文出自 “一顆石頭 BBOTTE的部落格” 部落格,請務必保留此出處http://bbotte.blog.51cto.com/6205307/1409983