Check & Email Hard Disk Utilization in Zimbra


Recently, one of our customers requested us to provide them hard disk utilization report every day. They would like to monitor the disk utilization. We may send them report manually by collecting disk utilization from the server. However, it is a troublesome job to provide them the report every day. In order to overcome this situation, we have written a script.

Deployment Infrastructure:
OS: Ubuntu 18.04 LTS
Zimbra Version: 8.8.15 Open Source Edition

Step 1: Write the script and save it as hddcapacity.sh.
root@mail:/srv# vim hddcapacity.sh
#!/bin/bash
# Hard disk capacity monitoring script
clear
yes | rm /tmp/hdd.txt
# Check hard disk capacity
CURRENT=$(df -h | grep / | awk '{ print $5}' | sed 's/%//g' | sort -g | tail -n1)
THRESHOLD=50

if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
SOURCE="from:report@domain.com";
DESTINATION="to:adminx@domain.com";
COPY="cc:bolcorp@domain.com";
SERVER=`hostname -f`;
CAPACITY=`df -h`;
SUBJECT="Subject: [NOTICE]: $SERVER HDD capacity has been reached by $CURRENT%"
BODY="
Dear Concern,
The server $SERVER is currently occupied $CURRENT% of storage.
Please avail below details.
$SERVER
*****************
$CAPACITY
Thank you.
This is system generated message. Please do not reply.
"
SENDMAIL=$(ionice -c3 find /opt/zimbra/ -type f -iname sendmail)
echo "$SOURCE" >> /tmp/hdd.txt
echo "$DESTINATION" >> /tmp/hdd.txt
echo "$COPY" >> /tmp/hdd.txt
echo "$SUBJECT" >> /tmp/hdd.txt
echo "$BODY" >> /tmp/hdd.txt
cat /tmp/hdd.txt | $SENDMAIL -t
fi
echo "The capacity of HDD is under $THRESHOLD%"

Step 2: Provide execute permission to the file.
root@mail:/srv# chmod +x /srv/hddcapacity.sh

Step 3: Create a file named hdd.txt.
root@mail:/srv# touch /tmp/hdd.txt

Step 4: Execute the file.
root@mail:/srv# sh /srv/hddcapacity.sh

Now, you will get an email to the defined email addresses in the script. Still we could not be able to automate the report email. To automate the report email, you can add a cron job.

Step 5: Add an entry in crontab.
root@mail:~# crontab –e
00 09 * * * sh /srv/hddcapacity.sh

This script shall run automatically on 09:00 AM every day.


Note: Please adjust the THRESHOLD value according to your requirement.

Comments

Popular posts from this blog

Install a Comodo/Sectigo Domain Validation SSL certificate in Zimbra

Adding DKIM record (1024 bit) in Zimbra

Recipient Limit of a Message in Zimbra