Bash Script to Monitor Disk Space Usage on Multiple Remote Linux Systems With eMail Alert

Some time ago, we had wrote Bash script to monitor disk space usage on a Linux system with an email alert.

That script works on a single machine, and you have to put the script on the corresponding machine.

If you want to set disk space usage alerts on multiple computers at the same time, that script does not help you.

So we have written this new shell script to achieve this.

To do so, you need a JUMP server (centralized server) that can communicate with any other computer without a password.

This means that password-less authentication must be set as a prerequisite.

When the prerequisite is complete, run the script on the JUMP server.

Finally add a cronjob to completely automate this process.

Three shell scripts are included in this article, and choose the one you like.

1) Bash Script-1: Bash Script to Check Disk Space Usage on Multiple Remote Linux Systems and Print Output on Terminal

This bash script checks the disk space usage on a given remote machine and print the output to the terminal if the system reaches the specified threshold.

In this example, we set the threshold limit to 80% for testing purpose and you can adjust this limit to suit your needs.

Also, replace your email id instead of us to receive this alert.

# vi /opt/scripts/disk-usage-multiple.sh

#!/bin/sh
output1=/tmp/disk-usage.out
output2=/tmp/disk-usage-1.out
echo "---------------------------------------------------------------------------" >> $output1
echo "HostName            Filesystem                Size Used Avail Use% Mounted on" >> $output1
echo "---------------------------------------------------------------------------" >> $output1
for server in `more /opt/scripts/servers.txt`
do
output=`ssh $server df -Ph | tail -n +2 | sed s/%//g | awk '{ if($5 > 80) print $0;}'`
echo "$server: $output" >> $output2
done
cat $output2 | grep G | column -t >> $output1
fscount=$(< "$output1" wc -l)
if [ $fscount -ge 4 ]; then
cat $output1 | grep G | column -t
else
echo "All are running under threshold"
fi
rm $output1 $output2

Run the script file once you have added the above script to a file.

# sh /opt/scripts/disk-usage-multiple.sh

You get an output like the one below.

------------------------------------------------------------------------------------------------
HostName           Filesystem                         Size  Used  Avail  Use%     Mounted on
------------------------------------------------------------------------------------------------
server01:         /dev/mapper/vg_root-lv_red          5.0G  4.3G  784M   85       /var/log/httpd
server02:         /dev/mapper/vg_root-lv_var          5.8G  4.5G  1.1G   81       /var
server03:         /dev/mapper/vg01-LogVol01           5.7G  4.5G  1003M  82       /usr
server04:         /dev/mapper/vg01-LogVol04           4.9G  3.9G  711M   85       /usr
server05:         /dev/mapper/vg_root-lv_u01          74G   56G   15G    80       /u01

2) Shell Script-2: Shell Script to Monitor Disk Space Usage on Multiple Remote Linux Systems With eMail Alerts

This shell script checks the disk space usage on a given remote machine and sends the output via a mail in a simple text once the system reaches the specified threshold.

# vi /opt/scripts/disk-usage-multiple-1.sh

#!/bin/sh
SUBJECT="Disk Usage Report on "`date`""
MESSAGE="/tmp/disk-usage.out"
MESSAGE1="/tmp/disk-usage-1.out"
TO="[email protected]"
echo "---------------------------------------------------------------------------------------------------" >> $MESSAGE1
echo "HostName            Filesystem                Size Used Avail Use% Mounted on" >> $MESSAGE1
echo "---------------------------------------------------------------------------------------------------" >> $MESSAGE1
for server in `more /opt/scripts/servers.txt`
do
output=`ssh $server df -Ph | tail -n +2 | sed s/%//g | awk '{ if($5 > 80) print $0;}'`
echo "$server: $output" >> $MESSAGE
done
cat $MESSAGE | grep G | column -t >> $MESSAGE1
fscount=$(< "$MESSAGE1" wc -l)
if [ $fscount -ge 4 ]; then
mail -s "$SUBJECT" "$TO" < $MESSAGE1
else
echo "All are running under threshold"
fi
rm $MESSAGE
rm $MESSAGE1

Run the script file once you have added the above script to a file.

# sh /opt/scripts/disk-usage-multiple-1.sh

You get an output like the one below.

------------------------------------------------------------------------------------------------
HostName           Filesystem                         Size  Used  Avail  Use%     Mounted on
------------------------------------------------------------------------------------------------
server01:         /dev/mapper/vg_root-lv_red          5.0G  4.3G  784M   85       /var/log/httpd
server02:         /dev/mapper/vg_root-lv_var          5.8G  4.5G  1.1G   81       /var
server03:         /dev/mapper/vg01-LogVol01           5.7G  4.5G  1003M  82       /usr
server04:         /dev/mapper/vg01-LogVol04           4.9G  3.9G  711M   85       /usr
server05:         /dev/mapper/vg_root-lv_u01          74G   56G   15G    80       /u01

Finally add a cronjob to automate this. It will run every 10 minutes.

# crontab -e

*/10 * * * * /bin/bash /opt/scripts/disk-usage-multiple-1.sh

3) Bash Script-3: Bash Script to Monitor Disk Space Usage on Multiple Remote Linux Systems With eMail Alerts

This shell script checks the disk space usage on a given remote machine and sends the output via the mail with a CSV file if the system reaches the specified threshold.

# vi /opt/scripts/disk-usage-multiple-2.sh

#!/bin/sh
MESSAGE="/tmp/disk-usage.out"
MESSAGE2="/tmp/disk-usage-1.csv"
echo "Server Name, Filesystem, Size, Used, Avail, Use%, Mounted on" > $MESSAGE2
for server in thvtstrhl7 thvrhel6
for server in `more /opt/scripts/servers-disk-usage.txt`
do
output1=`ssh $server df -Ph | tail -n +2 | sed s/%//g | awk '{ if($5 > 80) print $0;}'`
echo "$server $output1" >> $MESSAGE
done
cat $MESSAGE | grep G | column -t | while read output;
do
Sname=$(echo $output | awk '{print $1}')
Fsystem=$(echo $output | awk '{print $2}')
Size=$(echo $output | awk '{print $3}')
Used=$(echo $output | awk '{print $4}')
Avail=$(echo $output | awk '{print $5}')
Use=$(echo $output | awk '{print $6}')
Mnt=$(echo $output | awk '{print $7}')
echo "$Sname,$Fsystem,$Size,$Used,$Avail,$Use,$Mnt" >> $MESSAGE2
done
echo "Disk Usage Report for `date +"%B %Y"`" | mailx -s "Disk Usage Report on `date`" -a /tmp/disk-usage-1.csv [email protected]
rm $MESSAGE
rm $MESSAGE2

Run the script file once you have added the above script to a file.

# sh /opt/scripts/disk-usage-multiple-2.sh

You get an output like the one below.

Finally add a cronjob to automate this. It will run every 10 minutes.

# crontab -e

*/10 * * * * /bin/bash /opt/scripts/disk-usage-multiple-1.sh

Note: Because the script is scheduled to run once every 10 minutes, you will receive an email alert every 10 minutes.

If your system reaches a given limit after 18 minutes, you will receive an email alert on the second cycle, such as after 20 minutes (2nd 10 minute cycle).

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

3 Comments on “Bash Script to Monitor Disk Space Usage on Multiple Remote Linux Systems With eMail Alert”

Leave a Reply

Your email address will not be published. Required fields are marked *