A shell script that can monitor system resources

Source: Internet
Author: User

I just wrote a script to monitor system resources. You can study it.
You can change the value of the front edge to the value you want to monitor,
The variables before the execution of the monitoring script set the monitoring information and modify the information as needed.
[Root @ AR log] # cat monitor #! /Bin/bash
# System Monitoring Script
While [1]
Do
# The postfix or sendmail service must be enabled on the local machine.
# Alarm Email Address Settings
MAILFROM = monitor@zhaoyun.com
MAILTO = zhaoyun006@163.com
# Set the script running interval. Unit: seconds ).
RUNTIME = 1
# Memory usage monitoring settings, in %)
MEMTHRE = 90
# Traffic monitoring settings
# Nic to be monitored
ETHX = eth0
# Send an alarm when the value is greater than MB, in MB)
INFLOWTHREMB = 50
OUTFLOWTHREMB = 50
# Send an alarm when the value is greater than KB, in KB)
INFLOWTHREKB = 500
OUTFLOWTHREKB = 500
# TCP connection status count monitoring settings
# Enter the maximum number of connections
TIME_WAIT = 10
FIN_WAIT1 = 20
FIN_WAIT2 = 20
ESTABLISHED = 20
SYN_RECV = 10
CLOSE_WAIT = 10
CLOSING = 10
LAST_ACK = 10
# CPU usage monitoring settings
# Specify the number of alarms sent when the cpu usage is greater than %)
CPUTHRE = 5
# Hard disk usage size settings
# Fill in the hard disk usage, in %)
ROOT = 80
VAR = 100.
Usr= 100
BOOT = 80
# Debug mode switch. YES/NO)
DBUG = NO # internal capacity of the supervision script
######################################## ########################################
Time = 'date + "% Y-% m-% d % H: % M: % S "'
# Memory monitoring
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 <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
 
$ Time MEM memory warning. The current memory usage is $ MEM %, greater than $ MEMTHRE %.
EOF
FiMB = 'expr $ MEMB \> $ memthre'
If [$ MB-eq 1]; then
Sendmail-t <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
 
$ Time MEMB memory warning, current memory usage is $ MEMB %, greater than $ MEMTHRE %
EOF
Fi
# Traffic Monitoring
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
Sendmail-t <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
$ Time traffic warning. The current traffic is abnormal. Please log on to the server to view it. Current Rate $ inmb mb/second, greater than $ inflowthremb mb/second.
EOF
Fi
If [$ OUTMB-gt $ OUTFLOWTHREMB]; then
Sendmail-t <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
$ Time traffic warning. The current traffic is abnormal. Please log on to the server to view it. The current rate $ outmb mb/second is greater than $ outflowthremb mb/second.
EOF
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
Sendmail-t <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
$ Time traffic warning. The current traffic is abnormal. Please log on to the server to view it. $ Inkb kb/second is greater than $ INFLOWTHREKB/second.
EOF
Fi
If [$ OUTKB-gt $ OUTFLOWTHREKB]; then
Sendmail-t <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
$ Time traffic warning. The current traffic is abnormal. Please log on to the server to view it. The current rate $ outkb kb/second is greater than $ INFLOWTHREKB/second.
EOF
Fi
Fi # Number of connections
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
Sendmail-t <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
$ Time connection count warning. The current number of ESTABLISHED connections is abnormal. log on to the server and check the number. The current number of connections is $ ESTABLISHEDV, which is greater than $ ESTABLISHED.
EOF
Fi
If [$ SYN_RECVV-gt $ SYN_RECV]; then
Sendmail-t <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
$ Time connection count warning. The current number of SYN_RECV connections is abnormal. log on to the server and check the number. The current number of connections is $ SYN_RECVV, which is greater than $ SYN_REC.
EOF
Fi
If [$ CLOSE_WAITV-gt $ CLOSE_WAIT]; then
Sendmail-t <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
$ Time connection count warning. The current number of CLOSE_WAIT connections is abnormal. log on to the server and check the number. The current number of connections is $ CLOSE_WAITV, which is greater than $ CLOSE_WAIT.
EOF
Fi
If [$ CLOSINGV-gt $ CLOSING]; then
Sendmail-t <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
$ Time connection count warning. The current number of CLOSING connections is abnormal. log on to the server to view the number. The number of current connections is $ CLOSINGV, which is greater than $ CLOSING.
EOF
Fi
If [$ LAST_ACKV-gt $ LAST_ACK]; then
Sendmail-t <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
$ Time connection count warning. The current number of LAST_ACK connections is abnormal. log on to the server and check the number. The current number of connections is $ LAST_ACKV, which is greater than $ LAST_ACK.
EOF
Fi
If [$ TIME_WAITV-gt $ TIME_WAIT]; then
Sendmail-t <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
$ Time connection count warning. The current number of TIME_WAIT connections is abnormal. log on to the server and check the number. The current number of connections is $ TIME_WAITV, which is greater than $ TIME_WAIT.
EOF
Fi
If [$ FIN_WAIT1V-gt $ FIN_WAIT1]; then
Sendmail-t <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
$ Time connection count warning. The current number of FIN_WAIT1 connections is abnormal. Please log on to the server to view it. The current number of connections is $ FIN_WAIT1V, and the number is greater than $ FIN_WAIT1.
EOF
Fi
If [$ FIN_WAIT2V-gt $ FIN_WAIT2]; then
Sendmail-t <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
$ Time connection count warning. The current number of FIN_WAIT2 connections is abnormal. Please log on to the server to view it. The current number of connections is $ FIN_WAIT2V, and the number is greater than $ fin_wait2.
EOF
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 '3' | 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 <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
$ Time disk usage warning: the monitored/partition is greater than the value you set $ ROOT %. log on to the system for details. The current usage is $ ROOTV %.
EOF
Fi
If [$ VARV-gt $ VAR]; then
Sendmail-t <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
$ Time disk usage warning. The/var partition you monitor is greater than the value you set $ VAR %. For details, log on to the system and check that the current usage is $ VARV %.
EOF
Fi
If [$ BOOTV-gt $ BOOT]; then
Sendmail-t <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
$ Time disk usage warning. The/boot partition you monitor is greater than the value you set $ BOOT %. For details, log on to the system and check that the current usage is $ BOOTV %.
EOF
Fi
If [$ USRV-gt $ USR]; then
Sendmail-t <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
$ Time disk usage warning. The/usr partition you monitor is already greater than the value you set $ USR %. log on to the system for details. The current usage is $ USRV %.
EOF
Fi
# CPU
TotalA = 'cat/proc/stat | sed-n '1p' | awk' {print $2 + $3 + $4 + $5 + $6 + $7 + $8 }''
IDLEA = 'cat/proc/stat | sed-n '1p' | awk' {print $5 }''
Sleep 2
TotalB = 'cat/proc/stat | sed-n '1p' | awk' {print $2 + $3 + $4 + $5 + $6 + $7 + $8 }''
IDLEB = 'cat/proc/stat | sed-n '1p' | awk' {print $5 }''
Total = 'echo "$ TotalB-$ TotalA" | bc'
Idle = 'echo "$ IDLEB-$ IDLEA" | bc'
USAGE = 'echo "$ Idle/$ Total * 100" | bc-l'
RATE = 'echo "100-$ USAGE" | bc-l'
USERATE = 'echo "scale = 2; $ RATE/1" | bc' %
CA = 'expr $ USERATE \> $ cputhre'
If [$ CA-eq 1]; then
Sendmail-t <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
$ Time CPU usage warning: the CPU usage you have monitored has exceeded your set quota $ CPUTHRE %. The current CPU usage is $ USERATE.
EOF
Fi
CPURATE = 'top-B-n 1 | grep Cpu | awk '{print $5}' | awk-F % id' {print 100-$1 }''
CB = 'expr $ CPURATE \> $ cputhre'
If [$ CB-eq 1]; then
Sendmail-t <EOF
From: $ MAILFROM
To: $ MAILTO
Subject: warning
$ Time CPU usage warning: the CPU usage you have monitored has exceeded your set quota $ CPUTHRE %. The current CPU usage is $ CPURATE.
EOF
Fi
DBUGS = YES
If ["$ DBUGS" = "$ DBUG"]; then
Echo "" '& memory &'; echo \;
Echo "" the memory occupied by the current program is $ MEMB %, and the total memory occupied is $ MEM %; echo \;
Echo "" '& streaming volume &'; 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 "" the current input traffic is $ inmb mb/second .;
Echo "" the current input traffic is $ inkb kb/second .;
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 "" the current output traffic is $ outmb mb/second. ;
Echo "" the current output traffic is $ outkb kb/second. ; Echo \;
Fi
Echo "" '& connections &'; echo \;
The number of echo "" Current TIME_WAIT "connections is $ TIME_WAITV.
The number of echo "" Current FIN_WAIT1 "" connections is $ FIN_WAIT1V.
The number of echo "" Current FIN_WAIT2 "" connections is $ FIN_WAIT2V.
The number of echo "" Current CLOSE_WAIT "connections is $ CLOSE_WAITV.
The number of echo "" current ESTABLISHED "connections is $ ESTABLISHEDV.
The number of echo "Current SYN_RECV" "connections is $ SYN_RECVV.
The number of echo "" Current LAST_ACKV "connections is $ LAST_ACKV.
The number of echo "" Current CLOSING "connections is $ CLOSINGV .; Echo \;
Echo "" '& CPU usage &'; echo \;
Echo "" Current CPU process usage is $ USERATE.; echo \;
Echo "" '& disk usage &'; echo \;
Echo "" current "/" "partition," "usage is $ ROOTV %.
Echo "" current/var partition, usage: $ VARV %.
Echo "" current/boot partition, usage: $ BOOTV %.
Echo "" current/usr partition, usage: $ USRV %.
Exit
Fi
Sleep $ RUNTIME
Done
Running method, which runs in the background. [Root @ AR log] #. /monitor & stop method [root @ AR log] # ps-aux | grep monitor | grep-v grep | awk '{print $2}' | xargs kill-9 mail effect 650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131227/0R15A4c-0.jpg "/> 650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131227/0R1562F8-1.jpg "/> 650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131227/0R1563627-2.jpg "/> 650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131227/0R1561303-3.jpg "/>

650) this. width = 650; "border =" 0 "alt =" "src =" ../attachment/201107/101437588 .jpg"/>
After DBUG is enabled, it takes effect when DBUG = YES is set. 650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131227/0R15B453-4.jpg "/>

This article is from the "Technical Exchange" blog, please be sure to keep this source http://zhaoyun.blog.51cto.com/2090116/626083

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.