Linux practice (11) ---- shell monitoring server performance --- comprehensive application
I have learned some monitoring scripts and practical functional scripts. This chapter is a comprehensive article that combines the content I learned to monitor server performance.
The following functions are implemented:
To continuously observe the basic running status of the server on a daily basis and provide easy-to-read and centralized log record data, you need to configure the task plan based on the shell script, periodically records the CPU load, memory, swap space, disk usage, and other information of servers in different time periods.
1. Record the basic running status of the server every 15 minutes, including CPU load, memory and swap space, and disk space information.
2. Recent users
3. Record the current time information (in the format of YYYY-mm-dd HH: MM: SS, output all information to the file/data/shell/running. log)
4. Back up running at every night. log File; Compressed backup, save to the/data/shell directory; Backup File Name Reference: runing.log-YYYYmmdd.tar.bz2; backup completed the deletion of the day's record file (running. log)
Command Script learning to obtain CPU load information
function GetSysCPU { CpuIdle=`vmstat 1 5 |sed -n '3,$p'|awk '{x = x + $15} END {print x/5}' |awk -F. '{print $1}'` CpuNum=`echo "100-$CpuIdle" | bc` echo $CpuNum } Vmstat is used to display virtual memory information. delay: interval of refreshing. If this parameter is not specified, only one result is displayed.
Count: Number of refreshes. If you do not specify the number of refreshes, but specify the interval, the number of refreshes is infinite.
Vmstat 1 5 indicates refresh every 1 second, take 5 times sed-n '3, $ p'-n indicates to print only the selected row, the address is a number, it indicates the row number; "$" indicates the last line. '3, $ 'indicates the first line to the last line, and p indicates the output. Awk '{x = x + $15} END {print x/5}' indicates adding the 15th columns of each row and dividing it by 5 to calculate the average awk-F. '{print $1. to separate 1st columns, that is, to get an integer for the average.
CpuIdle = 'vmstat 1 5 | sed-n' 3, $ P' | awk '{x = x + $15} END {print x/5}' | awk-F. '{print $1 }''
Indicates calculating the average CPU idle time in five seconds in vmstat.
CpuNum = 'echo "100-$ CpuIdle" | bc'
Use 100 minus the average percentage of idle time to get the CPU usage.
That is, the CPU load information we requested.
Obtain memory information
function GetSysMem { Mem=`free -m | grep Mem | awk '{print $4}'` echo $Mem } Use the free command to view memory usage in linux: (MB)
# Free-m
Total used free shared buffers cached
Mem: 3952 3414 538 0 168 484
-/+ Buffers/cache: 2760 1191
Swap: 8191 86 8105
Free-m | grep Mem | awk '{print $4}
Grep Mem: Select rows containing Mem
Awk '{print $4}' is separated by blank spaces and the fourth column is taken. Here, free is used to obtain the remaining memory.
Obtain information about a swap space
function GetSysSwap{ swap_free=`free -m | grep Swap | awk '{print $4}'` echo $swap_free } Free-m | grep Swap | awk '{print $4} obtains the remaining Swap partition free size.
Grep Swap: Select rows containing Swap
Awk '{print $4}' is separated by blank spaces and the fourth column is taken. Here, free is used to obtain the remaining Swap.
Obtain disk space information
function GetSysSpace{ SPACE=`df| grep -v Use | gawk '{x = x + $5} END {print x}' | sed 's/%//'` echo $SPACE }
Df | grep-v Use | gawk '{x = x + $5} END {print x}' | sed's/% //'
Df is a command to query disk usage
Grep-v Use removes the line containing the Use string
Sed's/% //'s is the replacement character, which indicates matching the string containing the % sign to get the value before the % sign (that is, the value of the s position)
Gawk '{x = x + $5} END {print x}' are separated by a blank space. Then, the last value is obtained by adding the 5th columns of each row.
That is, the total usage percentage of the disk space.
Obtain Recent user information
Use the last command
Get current time
YYYY-mm-dd HH: MM: SS format
date +"%Y-%m-%d %H:%M:%S"
Delete original backup file
time=`date +"%Y%m%d"` tar cvf - /data/shell/running.log > runing.log-${time}.tar.bz2 rm running.log Create a new running. log in the/data/shell directory
Then run the command
Decompress available
tar -xvf runing.log-20160229.tar.bz2
Comprehensive script
We have learned all the commands for monitoring server performance in related commands.
Now let's combine them.
Combined
CheckSys. sh writes all monitoring information to the running. log file.
Backup. sh is responsible for backing up and deleting the original file
CheckSys. sh
function GetSysCPU { CpuIdle=`vmstat 1 5 |sed -n '3,$p'|awk '{x = x + $15} END {print x/5}' |awk -F. '{print $1}'` CpuNum=`echo "100-$CpuIdle" | bc` echo $CpuNum } echo CPU have used `GetSysCPU`% >> running.logfunction GetSysMem { Mem=`free -m | grep Mem | awk '{print $4}'` echo $Mem } echo free Mem is `GetSysMem`M >> running.logfunction GetSysSwap{ swap_free=`free -m | grep Swap | awk '{print $4}'` echo $swap_free } echo free Swap is `GetSysSwap` M >> running.logfunction GetSysSpace{ SPACE=`df| grep -v Use | gawk '{x = x + $5} END {print x}' | sed 's/%//'` echo $SPACE } echo Space have used `GetSysSpace`% >> running.loglast >> running.logdate +"%Y-%m-%d %H:%M:%S" >> running.log
Write the script after vim checkSys. sh
Run
Chmod + x/data/shell/checkSys. sh
./CheckSys. sh
Result:
Backup. sh
time=`date +"%Y%m%d"` tar cvf - /data/shell/running.log > runing.log-${time}.tar.bz2 rm running.log
Write the script after vim backup. sh.
Run
Chmod + x/data/shell/backup. sh
./Backup. sh
The backup is successful and the original file is deleted.
Timed running script
Now both scripts can run normally.
We set checkSys to run every 15 minutes.
Set backup. sh to run once every day at 00:00:00
crontab -e
Go to the timed running configuration
Add the following scheduled run plan.
*/15 * * * * /data/shell/checkSys.sh 0 0 * * * /data/shell/backup.sh
Here we have completed the whole example of monitoring server performance.