How to implement a shell script that can monitor system resources
This article mainly shows you "how to achieve a shell script that can monitor system resources". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn "how to implement a shell script that can monitor system resources".
[root@AR log] # cat monitor
#! / bin/bash
# System Monitoring Script
While [1]
Do
# postfix or sendmail service needs to be enabled on this machine.
# alarm email address setting
MAILFROM=monitor@zhaoyun.com
MAILTO=zhaoyun006@163.com
# set the interval between script runs. Unit (seconds).
RUNTIME=1
# memory usage monitoring settings (%)
MEMTHRE=90
# Traffic Monitoring Settings
# Network card to be monitored
ETHX=eth0
# enter the number of MB greater than the number to send an alarm, unit (MB)
INFLOWTHREMB=50
OUTFLOWTHREMB=50
# enter the number of KB greater than the number to send an alarm, unit (KB)
INFLOWTHREKB=500
OUTFLOWTHREKB=500
# TCP connection status quantity 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
# enter the number of alarms sent when the cpu utilization rate is greater than (%)
CPUTHRE=5
# setting the size of hard disk usage
# fill in the occupancy rate of hard disk (%)
ROOT=80
VAR=100
USR=100
BOOT=80
# Debug mode switch. (YES/NO)
DBUG=NO
# Supervision and control script to hold the capacity of the line
#
Time= `date + "% Y-%m-%d% H:%M:%S" `
# memory Monitoring Section
NULL=/dev/null
MEM= `free-m | grep Mem | awk'{print $3max 2cm 100}'`
MEMB= `free-m | grep Mem | awk'{print $2 camera 4 minutes 6 minutes 7}'| awk'{print $1 minus 1 million 2}'| awk'{print $2 Universe 100}'`
# echo $MEM
# echo $MEMB
MA= `expr $MEM\ > $MEMTHRE `
If [$MA-eq 1]; then
Sendmail-t $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 > $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