In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use Linux Shell script to monitor disk usage and send email". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Method 1: Linux Shell script to monitor disk space utilization and send email
You can use the following shell script to monitor disk space usage in the Linux system.
When the system reaches the given threshold limit, it sends an email. In this example, we set the threshold to 60% for testing purposes, and you can change this limit to meet your needs.
If more than one file system reaches a given threshold, it will send multiple messages because the script uses loops.
Also, replace your email address to get this warning.
# vi / opt/script/disk-usage-alert.sh #! / bin/shdf-Ph | grep-vE'^ Filesystem | tmpfs | cdrom' | awk'{print $5LLI}'| while read output;do echo $output used=$ (echo $output | awk'{print $1}'| sed s hash% pare g) partition=$ (echo $output | awk'{print $2}') if [$used-ge 60] Then echo "The partition\" $partition\ "on $(hostname) has used $used% at $(date)" | mail-s "Disk Space Alert: $used% Used On $(hostname)" [email protected] fidone
Output: I got the following two email warnings.
The partition "/ dev/mapper/vg_2g-lv_home" on 2g.CentOS7 has used 85% at Mon Apr 29 06:16:14 IST 2019 The partition "/ dev/mapper/vg_2g-lv_root" on 2g.CentOS7 has used 67% at Mon Apr 29 06:16:14 IST 2019
Finally, a cronjob is added to complete it automatically. It runs every 10 minutes.
# crontab-eSplash 10 * / bin/bash / opt/script/disk-usage-alert.sh method 2: Linux Shell script to monitor disk space utilization and send email
Instead, you can use the following shell script. We made a few changes compared to the script above.
# vi / opt/script/disk-usage-alert-1.sh #! / bin/shdf-Ph | grep-vE'^ Filesystem | tmpfs | cdrom' | awk'{print $5Line1}'| while read output;do max=60% echo $output used=$ (echo $output | awk'{print $1}') partition=$ (echo $output | awk'{print $2}') if [${used%?}-ge ${max%?}] Then echo "The partition\" $partition\ "on $(hostname) has used $used at $(date)" | mail-s "Disk Space Alert: $used Used On $(hostname)" [email protected] fidone
Output: I got the following two email warnings.
The partition "/ dev/mapper/vg_2g-lv_home" on 2g.CentOS7 has used 85% at Mon Apr 29 06:16:14 IST 2019 The partition "/ dev/mapper/vg_2g-lv_root" on 2g.CentOS7 has used 67% at Mon Apr 29 06:16:14 IST 2019
Finally, a cronjob is added to complete it automatically. It runs every 10 minutes.
# crontab-eBay opt/script/disk-usage-alert-1.sh 10 * / bin/bash / opt/script/disk-usage-alert-1.sh method 3: Linux Shell script to monitor disk space utilization and send email
I prefer this method. Because it works so magically that you only get one email about everything.
This is quite simple and straightforward.
* / 10 * df-Ph | sed Disk Space Alert On% hostname g | awk'{if ($5 > 60) print $0;}'| mail-s "Disk Space Alert On $(hostname)" [email protected]
Output: I got an email about all warnings.
Filesystem Size Used Avail Use Mounted on/dev/mapper/vg_2g-lv_root 10G 6.7G 3.4G 67 / / dev/mapper/vg_2g-lv_home 5.0G 4.3G 784M 85 / home method 4: Linux Shell script to monitor disk space usage of a partition and send email # vi / opt/script/disk-usage-alert-2.sh #! / bin/bashused=$ (df-Ph | grep'/ dev/mapper/vg_2g-lv_dbs' | awk {'print $5'}) max=80%if [${used%?}-ge ${max%?}] Thenecho "The Mount Point" / DB "on $(hostname) has used $used at $(date)" | mail-s "Disk space alert on $(hostname): $used used" [email protected] fi
Output: I got the following email warning.
The partition / dev/mapper/vg_2g-lv_dbs on 2g.CentOS6 has used 82% at Mon Apr 29 06:16:14 IST 2019
Finally, a cronjob is added to automate the work. It will run every 10 minutes.
# crontab-eBay bin/bash 10 * / bin/bash / opt/script/disk-usage-alert-2.sh
Note: you will receive an email warning in 10 minutes because the script is scheduled to run every 10 minutes (but not exactly 10 minutes, depending on the time).
For example, this example. If your system reaches the limit at 8:25, you will receive an email warning in 5 minutes. I hope it's clear now.
This is the end of "how to use Linux Shell script to monitor disk usage and send email". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.