To ensure that the server disk occupancy of the test environments that the team is responsible for is not too high during major holidays, resulting in frequent alarms. We require server disk occupancy to be checked before major holidays. If the occupancy is found to be too high, human intervention is required to clean it up accordingly.
I. Inspection requirements
Check the occupancy of each partition, and if any of the following conditions are met, human intervention is required to judge or handle the situation:
(1) Disk Utilization > 90%
(2) Disk usage > 80% and remaining space < 30G
(3) Disk usage > 70% and remaining space < 50G
II. Solutions
Use a shell script to count and determine disk occupancy and output an exception message if there is an exception.
The script code is as follows:
#!/bin/bash
RED='\033[0;31m'
NC='\033[0m'
if [[ $1 == "detail" ]]
then
df -BG
else
IS_NORMAL=1
while read line
do
if [[ ${line} == Filesystem* ]]; then
continue
fi
filesystem=$(echo ${line} | awk '{print $1}')
use_rate=$(echo ${line} | awk '{print $5}' | sed 's/%//g')
avail_space=$(echo ${line} | awk '{print $4}' | sed 's/G//g')
mounted_on=$(echo ${line} | awk '{print $6}')
if [[ ${use_rate} -gt 90 ]] || [[ ${use_rate} -gt 80 && ${avail_space} -lt 30 ]] || [[ ${use_rate} -gt 70 && ${avail_space} -lt 50 ]]; then
echo -e "${RED}WARN: Filesystem ${filesystem} mounted on ${mounted_on} has problem: use rate is ${use_rate}%, available space is ${avail_space}G.${NC}"
IS_NORMAL=0
fi
done < <(df -BG)
if [[ ${IS_NORMAL} -eq 1 ]]; then
echo "INFO: Disk space usage is normal."
fi
fi
Key Code Description:
df -BG : command is used to report the disk space usage of the file system, the -BG option means displaying in G bytes, the meaning of B is: use SIZE-byte blocks
Suggested script save path:/data/sh/general/disk_usage_check.sh
Initialize the script execution command:mkdir -p /data/sh/general/;touch /data/sh/general/disk_usage_check.sh;chmod +x /data/sh/general/disk_usage_check.sh;vim /data/sh/general/disk_usage_check.sh
III. Script usage
(1) According to the inspection requirements, determine whether there is excessive occupation of disk space occupation
Execute the script:/data/sh/general/disk_usage_check.sh
(2) If necessary, you can further view the disk occupancy information of each partition.
Execute the script:/data/sh/general/disk_usage_check.sh detail
Use the example to see the results in two cases:
(1) Check normal
[root@localhost ~]# /data/sh/general/disk_usage_check.sh
INFO: Disk space usage is normal.
(2) Check for anomalies that require human intervention in judgment and treatment
[root@novalocal general]# /data/sh/general/disk_usage_check.sh
WARN: Filesystem /dev/vdb mounted on /data has problem: use rate is 76%, available space is 47G.
[root@novalocal general]# /data/sh/general/disk_usage_check.sh detail
Filesystem 1G-blocks Used Available Use% Mounted on
/dev/mapper/centos-root 49G 12G 38G 23% /
devtmpfs 8G 0G 8G 0% /dev
tmpfs 8G 1G 8G 1% /dev/shm
tmpfs 8G 1G 7G 11% /run
tmpfs 8G 0G 8G 0% /sys/fs/cgroup
/dev/vdb 197G 142G 47G 76% /data
/dev/vda1 1G 1G 1G 20% /boot
tmpfs 2G 0G 2G 0% /run/user/0
The treatment can be found in the next section
IV. Occupancy positioning and solutions
1. View the size of each document or folder in the directory and output it in descending order
[root@f2 data]# du -sh * | sort -hr
27G tomcat
5.1G did-generator
4.1G register
2.5G turbine-web
1.4G rbmq-productor
1.1G consul
600M backup
544M test-backup
527M deploy
Command Parsing:
du: Short for "disk usage", this command is used to estimate the amount of space a file or directory occupies on a disk.
-s: This option tells the du command to display only the total size, not the size of each subdirectory or file.
-h: This option tells du to display the size in an easy-to-read format (e.g., KB, MB, GB, etc. are automatically selected).
-r: This option tells the sort command to sort the results in descending order (the default is ascending).
2. Why can't I free up space by deleting occupied files?
In Linux, when you delete a file, if the file is still in use by a process (i.e., there is an open file descriptor pointing to the file), the disk space for the file is not immediately freed. This is because in Linux, the deletion of a file actually removes the association between the file name and the inode, not the inode itself. Only after all file descriptors associated with the inode have been closed will the inode be deleted and the corresponding disk space freed.
If you delete a file that is still being used by a process, you can free up disk space by restarting the process or rebooting the system to ensure that all file descriptors are closed.
The class file can be viewed using the lsof command
(1) Command to view deleted but unreleased files:lsof | grep '(deleted)'
(2) View the deleted but unreleased files that take up the most space:lsof | grep '(deleted)' | sort -n -r -k 7,7 | head -n 10
, command parsing:
-n: Sort numerically. By default, the sort command sorts as a string, but the -n option causes the sort command to sort numerically.
-r: Sort in reverse order. By default, the sort command sorts in ascending order, but the -r option causes the sort command to sort in descending order.
-k 7,7: Specifies the fields to sort. By default, the sort command sorts on the entire line, but the -k option allows the sort command to sort on only the specified fields. In this case, -k 7,7 means only the seventh field is used as the basis for sorting.
V. Problems encountered
1. When executing in pipeline mode, the value of a variable cannot be updated.
IS_NORMAL=1
df -BG | while read line
do
IS_NORMAL=0
done
echo ${IS_NORMAL}
With the above code, the final output IS_NORMAL value is always 1 and cannot be modified to 0. Reason:
In bash scripts, the pipe character | creates a sub-shell to execute the commands to the right of the pipe character. In this example, the while read line loop is executed in a sub-shell. Therefore, changes to the variable IS_NORMAL inside the loop are made in the sub-shell and do not affect the variables in the main shell.
To solve this problem, you can use process substitution and execute the while read line loop as the main process. The modified code is as follows:
IS_NORMAL=1
while read line
do
IS_NORMAL=0
done < <(df -BG)
echo ${IS_NORMAL}
VI. Supplementary notes
1. Difference between du and df
du
cap (a poem)df
are both Linux commands for checking disk space usage, but they are used differently and display different information.
(1) du
Command:du
Short for "disk usage", the main purpose of this command is to estimate the amount of space a file or directory occupies on a disk. It will recursively scan the directory and then calculate the size of each subdirectory.
Example: du -sh /home
This command displays the total size of the /home directory.-s
parameter indicates that only totals are displayed.-h
parameter indicates the size to display in an easy-to-read format (e.g., K, M, G).
(2)df
Command:df
Short for "disk filesystem", the main purpose of this command is to display disk usage. It displays disk space usage for all mounted file systems, including total space, used space, remaining space, and percentage used.
Example: df -h
This command displays disk space usage for all mounted file systems in an easy-to-read format.-h
parameter indicates the size to display in an easy-to-read format (e.g., K, M, G).
Overall.du
cap (a poem)df
The main difference between thedu
is used to view the size of a file or directory, and thedf
is used to view disk usage.