This script can do a timed detection, beyond the set value, that is, to send a warning to the mailbox
The script uses the bc,sendmail,163 mailbox,
Yum Install BC
#!/bin/bash#system monitoring Scriptwhile [1]do# the machine needs to open postfix or sendmail services. #报警邮件地址设置 [email protected][email protected] #设置脚本运行间隔时间. Unit (seconds). runtime=900# Memory usage monitoring settings, units (%) memthre=80# traffic Monitoring settings # to monitor the network card ethx=eth0# fill in more than a few MB when the alarm is sent, in megabytes inflowthremb= 50outflowthremb=50# fill in the number of KB when the alert is sent, in kilobytes (KB) INFLOWTHREKB=8000OUTFLOWTHREKB=8000#TCP connection state Quantity Monitoring settings # Fill in the maximum number of connections time_wait= 4000FIN_WAIT1=500FIN_WAIT2=200ESTABLISHED=4000SYN_RECV=100CLOSE_WAIT=100CLOSING=1000LAST_ACK=3000#CPU Usage Monitoring Settings # Fill in the CPU usage is greater than how many send alarm, unit (%) cputhre=60# HDD Use size setting # Fill in the hard drive occupancy rate, unit (%) root=80var=100usr=100boot=80# debug mode switch. (yes/no) dbug=no# monitoring script execution content ############################################################################### #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 |aw K ' {print $2,$4+$6+$7} ' |awk ' {print $1,$1-$2} ' | awk ' {print $2/$1*100} ' memuse= ' Free-m|grep ' buffers/cache ' |awk ' {print $} ' 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 memory Warning, current memory usage is $mem%, greater than $memthre%. eoffimb= ' expr $MEMB \> $MEMTHRE ' If [$MB-eq 1]; Thensendmail-t <<eoffrom: $MAILFROMto: $MAILTOsubject: 67warning$time MEMB memory Warning, current memory usage is $MEMB%, greater than $memthre% eoffi# Traffic Monitoring Section flowa=/tmp/.flowifconfig $ETHX |grep "RX byte" |awk ' {print $ "" $6} ' |awk-fbytes: ' {print "INPUT" $ "OUTPUT "$ $} ' \ > $FLOWAINPUTA = ' cat $FLOWA |awk ' {print $} ' outputa= ' cat $FLOWA |awk ' {print $4} ' sleep 1ifconfig $ETHX |grep ' RX byte "|awk ' {print $" "$6} ' |awk-fbytes: ' {print" INPUT "$" OUTPUT "$ $" \ > $FLOWAINPUTB = ' cat $FLOWA |awk ' {print $ ' 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 '} ' ' outmb= ' cat $OUTMBF |awk ' {print '} ' if [$INPUTMBA-gt 1024]; thenif [$INMB-gt $INFLOWTHREMB]; thensendmail-t <<eoffrom: $MAILFROMto: $MAILTOsubject: 67warning$time Traffic Warning, Current traffic is abnormal, please log in to the server to view. Current Rate $INMB MB/s, greater than $inflowthremb MB/s. Eoffiif [$OUTMB-gt $OUTFLOWTHREMB]; thensendmail-t <<eoffrom: $MAILFROMto: $MAILTOsubject: 67warning$time Traffic Warning, Current traffic is abnormal, please log in to the server to view. Current Rate $OUTMB MB/s greater than $outflowthremb MB/s. Eoffielseinkbf=/tmp/.inputkboutkbf=/tmp/.outputkbecho $INPUTMBA KB > $INKBFecho $OUTPUTMBA KB > $OUTKBFINKB = ' cat $INKBF |awk ' {print $} ' outkb= ' cat $OUTKBF |awk ' {print '} ' if [$INKB-gt $INFLOWTHREKB]; Thensendmail-t <<eoffrom: $MAILFROMto: $MAILTOsubject: 67warning$time traffic Warning, current traffic is abnormal, please log in to the server to view. $INKB kb/seconds is greater than $inflowthrekb kb/seconds. Eoffiif [$OUTKB-gt $OUTFLOWTHREKB]; thensendmail-t <<eoffrom: $MAILFROMto: $MAILTOsubject: 67warning$time Traffic Warning, Current traffic is abnormal, please log in to the server to view. Current rate $outkb kb/seconds greater than $inflowthrekb kb/seconds。 eoffifi# number of Connections 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 $} ' fin_wait1v= ' grep fin_wait1 $tcpfile | awk ' {print $} ' fin_wait2v= ' grep fin_wAIT2 $tcpfile | awk ' {print $} ' establishedv= ' grep established $tcpfile | awk ' {print $} ' syn_recvv= ' grep syn_recv $tcpfile | awk ' {print $} ' closingv= ' grep CLOSING $tcpfile | awk ' {print $} ' close_waitv= ' grep close_wait $tcpfile | awk ' {print $} ' last_ackv= ' grep last_ack $tcpfile | awk ' {print $} ' if [$ESTABLISHEDV-gt $ESTABLISHED]; Thensendmail-t <<eoffrom: $MAILFROMto: $MAILTOsubject: 67warning$time Number of connections warning, the current number of established connections is abnormal, please log on to the server to view. The current number of connections is $ESTABLISHEDV, greater than $established eoffiif [$SYN _recvv-gt $SYN _RECV]; Thensendmail-t <<eoffrom: $MAILFROMto: $MAILTOsubject: 67warning$time Number of connections warning, the current number of SYN_RECV connections is abnormal, please log on to the server to view. The current number of connections is $SYN_RECVV, greater than $syn_rec. Eoffiif [$CLOSE _waitv-gt $CLOSE _wait]; Thensendmail-t <<eoffrom: $MAILFROMto: $MAILTOsubject: 67warning$time Number of connections warning, the current number of close_wait connections is abnormal, please log on to the server to view. The current number of connections is $CLOSE_WAITV, greater than $close_wait. Eoffiif [$CLOSINGV-gt $CLOSING]; Thensendmail-t <<eoffrom: $MAILFROMto: $MAILTOsubject: 67warning$time Number of connections warning, the current number of closing connections is abnormal, please log on to the server to view. The current number of connections is$CLOSINGV, more than $closing. Eoffiif [$LAST _ackv-gt $LAST _ack]; Thensendmail-t <<eoffrom: $MAILFROMto: $MAILTOsubject: 67warning$time Number of connections warning, the current number of last_ack connections is abnormal, please log on to the server to view. The current number of connections is $LAST_ACKV, greater than $last_ack. Eoffiif [$TIME _waitv-gt $TIME _wait]; Thensendmail-t <<eoffrom: $MAILFROMto: $MAILTOsubject: 67warning$time Number of connections warning, the current number of time_wait connections is abnormal, please log on to the server to view. The current number of connections is $TIME_WAITV, greater than $time_wait. Eoffiif [$FIN _wait1v-gt $FIN _wait1]; Thensendmail-t <<eoffrom: $MAILFROMto: $MAILTOsubject: 67warning$time Number of connections warning, the current number of fin_wait1 connections is abnormal, please log on to the server to view. The current number of connections is $FIN_WAIT1V, greater than $fin_wait1. Eoffiif [$FIN _wait2v-gt $FIN _wait2]; Thensendmail-t <<eoffrom: $MAILFROMto: $MAILTOsubject: 67warning$time Number of connections warning, the current number of fin_wait2 connections is abnormal, please log on to the server to view. The current number of connections is $FIN_WAIT2V, greater than $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 $} ' |awk-f% ' {print $} ' varv= ' Cat $DISKF | grep var | awk ' {print $} ' |awk-f% ' {print $} ' usrv= ' Cat $DISKF | grep usr | awk ' {print $} ' |awk-f% ' {print $} ' grep VolGroup $DISKF > $NULLif [$-eq 0]; thenrootv= ' Cat $DISKF | Sed-n ' 3p ' |awk ' {print $4} ' |awk-f% ' {print \ {} ' elserootv= ' Cat $DISKF | Sed-n ' 2p ' |awk ' {print $ |awk-f% ' {print $} ' fiif [$ROOTV-gt $ROOT]; Thensendmail-t <<eoffrom: $MAILFROMto: $MAILTOsubject: 67warning$time disk Use warning, you monitor/partition is greater than the value you set $root%, details login system view , the current usage rate is $ROOTV%. Eoffiif [$VARV-gt $VAR]; Thensendmail-t <<eoffrom: $MAILFROMto: $MAILTOsubject: 67warning$time disk Use warning, you monitor the/var partition is greater than the value you set $var%, Detailed login system view, the current usage rate is $varv%. Eoffiif [$BOOTV-gt $BOOT]; Thensendmail-t <<eoffrom: $MAILFROMto: $MAILTOsubject: 67warning$time disk usage warning, you have monitored the/boot partition larger than the value you set $BOOT%, Detailed login system view, the current usage rate is $BOOTV%. Eoffiif [$USRV-GT $USR]; Thensendmail-t <<eoffrom: $MAILFROMto: $MAILTOsubject: 67warning$time disk Use warning, you monitor the/USR partition is greater than the value you set $usr%, Detailed login system view, the current usage rate is $usrv%. Eofficpurate= ' Top-b-N 1 |grep Cpu | awk ' {print $} ' |awk-f. ' {print $} ' cb= ' expr $CPURATE \> $CPUTHRE ' If [$CB-eq 1]; Thensendmail-t <<eoffrom: $MAILFROMto: $MAILTOsubject: 67warning$time use warning, the CPU usage you monitor has exceeded the limit you set $cputhre%, The current CPU utilization is $cpurate. Eoffidbugs=yesif ["$DBUGS" = = "$DBUG"]; Thenecho "' = = memory = = '; Echo \ echo" "The current program occupies a memory of $MEMB%, the total memory consumption is $MEM%; echo \ echo "' = = = Flow = = '; 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 "" Current input traffic is $INMB MB/s.; echo "" Current input traffic is $INKB kb/s.; 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 "" Current output is $OUTMB MB/s. echo "" Current output flow is $OUTKB kb/second.; echo \; Fiecho "" ' = = = Number of connections = = '; echo \ echo "Current Time_wait" "The number of connections is $TIME _WAITV. echo "Current Fin_wait1" "Number of connections is $FIN _wait1v. echo "Current Fin_wait2" "Number of connections is $FIN _wait2v. echo "Current Close_wait" "Number of connections is $CLOSE _WAITV. echo "Current established" "Number of connections is $ESTABLISHEDV. echo "Current Syn_recv" "Number of connections is $SYN _RECVV. echo "Current Last_ackv" "Number of connections is $LAST _ackv. echo "Current Closing" "The number of connections is $CLOSINGV.; echo \ echo "' = = CPU Usage = = '; echo \ echo "Current CPU process usage is $USERATE.; echo \ echo "" ' = = disk Usage = = '; echo \ Echo "Current"/"Partition," "Utilization is $ROOTV%.echo" "Current/var partition, usage is $VARV%.echo" "Current/boot partition, usage is $BOOTV%.echo" "Current/U SR partition with a usage rate of $USRV%.exitfisleep $RUNTIMEdone
Script 42 line, first change to Dbug=yes, run once, if no problem, appear the following prompt
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/27/23/wKiom1NwjyWSATs0AAJ7tAelj9o671.jpg "title=" Qq20140512170248.jpg "alt=" Wkiom1nwjywsats0aaj7taelj9o671.jpg "/>
and change the Dbug=no back.
Attached, email notification
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/27/23/wKioL1Nwj0PBXIq_AABwHhEFA7k537.jpg "title=" Qq20140512170358.jpg "alt=" Wkiol1nwj0pbxiq_aabwhhefa7k537.jpg "/>
Reference http://yangji.blog.51cto.com/1155906/929250, there are changes
This article is from "a Stone Bbotte blog" blog, please be sure to keep this source http://bbotte.blog.51cto.com/6205307/1409983