Project # 2 - Monitoring Disk Space with Email Alert
Shell Script for Monitoring Disk Space and Send Email Alert
In this project we'll be creating a powerful shell script for monitoring free disk space with email alert functionality. In today's data-driven world, managing disk space efficiently is crucial for maintaining system stability and preventing unexpected downtime. By automating the monitoring process and implementing email alerts, we can stay proactive in addressing potential storage issues before they escalate.
Some Important Commands:
We'll be using pipes in the commands. I'll show you each additional command in pipe one by one.
df:
df
is used to list all the disk spaces in the file system.
grep:
grep
is used to list the content only that we want to show. This matches the pattern and list the content that we want. You'll have to select one of the disk that you want to monitor. In my case I'll be monitoringsda3
. grep command is written asgrep "[pattern]"
.
awk:
awk
command is used for different purposes. In our case we'll be usingawk
to extract a specific column. We'll be extracting the 5th column that shows the disk usage. This command will be written asawk '{print $5}'
. This command will only extract the 5th column.
tr:
tr
command is used for translation. We'll be usingtr
command with-d
option. This will delete a specified occurrences of the specified string or character. We'll be removing the % sign and for this we'll usetr -d %
. This will delete the character%
.
ALL COMMANDS TOGETHER:
We'll be using
df | grep "sda3" | awk '{print $5}' | tr -d %
command in our shell script.
Script:
#!/bin/bash
FU=$(df | grep "sda3" | awk '{print $5}' | tr -d %)
TO="[email]"
if [[ $FU -ge 70 ]]
then
echo "Warning, Disk Space is Low: $FU %" | mail -s "DISK SPACE ALERT!" $TO
else
echo "All Good." | mail -s "DISK SPACE ALERT!" $TO
fi
Explanation:
#!/bin/bash
: This is the shebang line. It is used to tell the operating system which shell to use for executing the script.FU=$(df | grep "sda3" | awk '{print $5}' | tr -d %)
:FU
is a variable which will store the disk space usage.TO="[email]"
:TO
is a variable that will store the email address of the account where the email will be sent.if [[ $FU -ge 70 ]]
: There is a condition that if the disk space usage is greater than or equal to 70 then:echo "Warning, Disk Space is Low: $FU %" | mail -s "DISK SPACE ALERT!" $TO
: An email will be sent to the email address stored in the variableTO
.mail -s
is used to give the subject to the email and the subject will beDISK SPACE ALERT!
. Andecho
will print the body of the email that will beWarning, Disk Space is Low: [Disk Space Usage]%
. This is done using Postfix in linux. You'll have to install and configure postfix. If you want to understand postfix Click Here. else:echo "All Good." | mail -s "DISK SPACE ALERT!" $TO
: An email will be sent to the email address stored in the variableTO
. Subject of the mail will beDISK SPACE ALERT!
. Andecho
will print the body of the email that will beAll Good.
.
Running the Script:
Creation, Changing Permissions, and Execution of the script:
-
The email: