In this blog post, we will guide you through the process of creating a shell script that will monitor server disk space and send email alerts using the Simple Mail Transfer Protocol (SMTP). We will make use of the “sendEmail” package, which ensures reliable email delivery, unlike when using the “sendmail” package. By the end of this tutorial, you will have a robust monitoring system in place that will notify you via email when your server’s disk space is running low.
Shell Code
#!/bin/bash
# Get current disk usage percentage
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
# Set the disk space threshold for triggering the email alert
THRESHOLD=70
# SMTP server configuration
SERVER="mail.server.com:587"
USERNAME="your_smtp_username"
PASSWORD="your_smtp_password"
FROM="[email protected]"
TO="[email protected]"
SUBJ="WebServer Disk Space Low!"
MESSAGE="WebServer root partition remaining free space is critically low. Used: $CURRENT%"
CHARSET="utf-8"
# Check if disk usage exceeds the threshold
if [ "$CURRENT" -gt "$THRESHOLD" ]; then
sendemail -f $FROM -t $TO -u $SUBJ -s $SERVER -xu $USERNAME -xp $PASSWORD -V -m $MESSAGE -o message-charset=$CHARSET
fi
Creating the Shell Script
Let’s start by creating a shell script file named disk-monitor.sh
and granting it execution
permissions. Follow the steps below:
- Open your terminal and navigate to your home directory by entering
cd ~
. - Create the shell script file by running the command
nano disk-monitor.sh
. This will open the Nano text editor. - Copy and paste the provided shell code into the
disk-monitor.sh
file. - Update the SMTP server configuration by replacing
mail.server.com:587
with your actual mail server host, portyour_smtp_username
with your actual SMTP username andyour_smtp_password
with your actual SMTP password. - Update the
FROM
variable with the email address you want to use as the sender. - Update the
TO
variable with the recipient’s email address where you want to receive the alerts. - Save the file and exit the Nano editor.
Granting Execution Permissions
Next, we will grant execution permissions to the disk-monitor.sh
script. Follow these steps:
- In the terminal, enter the following command:
sudo chmod +x disk-monitor.sh
. - You may be prompted to enter your password. Type it in (you won’t see the characters), and press Enter.
Installing sendEmail (if not already installed)
If the sendEmail
package is not installed on your system, you can install it by following these steps:
- In the terminal, enter the command:
sudo apt-get install sendemail
. - You may be prompted to enter your password. Type it in (you won’t see the characters), and press Enter.
- Wait for the installation to complete.
Adding the Script to the Cron Job
To schedule the script to run daily using the cron job, follow the steps below:
- In the terminal, enter the command:
crontab -e
. This will open the cron table in your default text
editor. - If prompted to choose an editor, select your preferred one.
- At the bottom of the file, add
@daily ~/disk-monitor.sh
to run thedisk-monitor.sh
script daily. - Save the file and exit the text editor.
Congratulations! You have created a shell script named disk-monitor.sh
that monitors your server’s disk space and sends email alerts if the usage exceeds the specified threshold. By granting execution permissions and adding the script to the daily cron job, you ensure that the monitoring process runs automatically and reliably.
With this implementation, you can proactively manage disk space issues and prevent potential disruptions to your server’s performance. Emails sent via the SMTP protocol using the “sendEmail” package guarantee reliable delivery, giving you peace of mind that critical notifications will reach their intended recipients.
Feel free to customize the script and adjust the threshold or email settings to fit your specific requirements. Or just add this to your CloudPanel install.
Discover more from Techbreeze IT Solutions
Subscribe to get the latest posts sent to your email.