home Tutorial Monitor Server Disk Space and Send Email Alerts with Shell Script

Monitor Server Disk Space and Send Email Alerts with Shell Script

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:

  1. Open your terminal and navigate to your home directory by entering cd ~.
  2. Create the shell script file by running the command nano disk-monitor.sh. This will open the Nano text editor.
  3. Copy and paste the provided shell code into the disk-monitor.sh file.
  4. Update the SMTP server configuration by replacing mail.server.com:587 with your actual mail server host, port  your_smtp_username with your actual SMTP username and your_smtp_password with your actual SMTP password.
  5. Update the FROM variable with the email address you want to use as the sender.
  6. Update the TO variable with the recipient’s email address where you want to receive the alerts.
  7. 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:

  1. In the terminal, enter the following command: sudo chmod +x disk-monitor.sh.
  2. 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:

  1. In the terminal, enter the command: sudo apt-get install sendemail.
  2. You may be prompted to enter your password. Type it in (you won’t see the characters), and press Enter.
  3. 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:

  1. In the terminal, enter the command: crontab -e. This will open the cron table in your default text
    editor.
  2. If prompted to choose an editor, select your preferred one.
  3. At the bottom of the file, add @daily ~/disk-monitor.sh to run the disk-monitor.sh script daily.
  4. 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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Techbreeze IT Solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading