In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to monitor the utilization of disk partitions in shell programming practice. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
I do not know if you have ever encountered the disk partition is full, resulting in new data can not be written, resulting in a problem with the website. I have encountered this situation several times before, and my emergency solution at that time was to delete the log file of the website and delete the binlog log of MySQL.
Of course, we do not want this kind of problem to happen, if the website is always unstable or can not be opened, it will give users a sense of unreliability. Therefore, for the use of disk partitions, we need to monitor in real time, and when the default value is exceeded, send an email or text message to the administrator to strangle the error into the cradle.
It is easy to write this script, and the main point is to get the utilization of the disk partition. We know that the df command lists the space usage of each partition:
# df Filesystem 1K-blocks Used Available Use% Mounted ondevtmpfs 3986648 0 3986648 / devtmpfs 3999984 0 3999984 / dev/shmtmpfs 3999984 528 3999456.1 / runtmpfs 3999984 039999840 / sys/fs/cgroup/dev/vda1 4191,756 17576208 2435554842 / / dev/vdb1 52403200 476572 519266281 / data/dev/vdb2 104806400 29255772 75550628 28% / wwwtmpfs 799996 07999996% / run/user/0/dev/vdb3 52402180 398576 520036041% / mnt
We can see the space usage of each partition. Then interface the grep command and the awk command to filter out other information, leaving only the usage information of each partition.
We just want to get partition information that starts with / dev/
# df | grep ^ / dev/dev/vda1 41931756 17576264 24355492 42% / / dev/vdb1 52403200 476572 519266281% / data/dev/vdb2 104806400 29255980 75550420 28% / www/dev/vdb3 52402180 398576 52003604 1% / mnt
Then continue to use the awk command to filter unwanted information
# df | grep ^ / dev | awk'{print $1 ":" $5}'/ dev/vda1:42%/dev/vdb1:1%/dev/vdb2:28%/dev/vdb3:1%
After getting the utilization rate of each partition, the following is to cycle through it and judge the usage of each partition in turn.
The completed code is given below:
#! / bin/bash# monitors the space usage of the partition Alarm export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:~/binexport LANG=en# partition space utilization threshold USE_LIM=70while ((1)) do disks=$ (df | grep ^ / dev | awk'{print $1 ":" $5}') for disk in $disks do used=$ (echo $disk | cut-d:-f2 | cut-dwells%'- F1) if the default value is exceeded If ["$used"-ge "$USE_LIM"] Then # calls the alarm script, which directly replaces echo "$(echo $disk | cut-d:-F1) is used $used%" fi done sleep 10done with echo
Note: generally speaking, the language family used on our servers is English, especially when writing shell scripts, it is recommended to add this line.
Export LANG=en
Because we want to do real-time monitoring, a dead loop is used in the script, indicating that the script will not stop executing the loop all the time. Of course, the speed of execution should also be limited, otherwise it would not be good to take up too much server resources. So sleep 10 is used here, which means monitoring every 10 seconds.
Another thing to note here is to filter out the percent sign. Here we use the cut command to filter out%, and then compare the pure numeric partition usage with the default value.
On shell programming practice how to monitor the utilization of disk partitions is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.