Menü schliessen
Created: November 30th 2022
Last updated: December 12th 2022
Categories: Linux
Author: LEXO

Send harddisk (and OwnCloud per-user) storage information via e-mail with via Cronjob and a Linux Shell Bash Script

Tags:  bash,  Cron,  Cronjob,  df,  Linux,  script,  shell,  storage
Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

Simple script to send regular and nicely formatted (text) e-mails about different harddisk storage capacities as well as OwnCloud per-user storage information

Use case description

A customer needed to be informed about his servers storage capacities on a regular basis. The script we wrote is very much focused on customers use case. It's has not been created with a lot of flexibility in mind. Although it might help you create your own script based on this.

How does it work?

  1. The script asks for some basic parameters first in order to be able to create an e-mail
  2. 2 variables are created which will contain the data from the df command
  3. The variables are being converted into arrays
  4. The mail body is created and the data from the arrays is used to create a proper view
  5. The mail body is sent via mailx to one or more recipients

Code

#!/bin/bash
set +v
set +x
## E-Mail settings
MAIL_SENDER="mail-sender-name" ##defines the user account which is used to send out e-mails. This particular user account does not need to have a shell assigned
MAIL_SUBJECT="LOCAL Server | EXTERNAL Server | Storage Information"
MAIL_RECIPIENTS="serveradmin@yourdomain.tld" ##send mail to multiple receipients by overgiving a space-seperated address list
MAIL_AGENT=`which mail`

## Get storage information from various systems
LOCAL_SERVER_STORAGE=($(df -h /dev/sda2))
EXTERNAL_SERVER_STORAGE=($(ssh admin@EXTERNAL_SERVER 'df -h /dev/sda3'))
declare -p LOCAL_SERVER_STORAGE &>/dev/null
declare -p EXTERNAL_SERVER_STORAGE &>/dev/null

## Get all OwnCloud user storage information and sort the directories according to their storage consumption in reverse order
OC_USR_STORAGE=( $(for i in $(find /home/ocdata -maxdepth 1 -type d |grep -E -i -v "(avatars|cache|\.locks)"); do du -smh "$i"; done |sort -hr) )

## Create mail body
MAIL_BODY="Please note the following storage information data taken on `date +%d.%B.%Y,%T`:"
MAIL_BODY="${MAIL_BODY}\n\n\n"
MAIL_BODY="${MAIL_BODY}--------------------- LOCAL_SERVER STORAGE INFORMATION ---------------------"
MAIL_BODY="${MAIL_BODY}\n\n"
MAIL_BODY="${MAIL_BODY}Storage Capacity:\t ${LOCAL_SERVER_STORAGE[8]}\n"
MAIL_BODY="${MAIL_BODY}Storage Used:\t\t   ${LOCAL_SERVER_STORAGE[9]}\n"
MAIL_BODY="${MAIL_BODY}Storage Used (%):\t ${LOCAL_SERVER_STORAGE[11]}\n"
MAIL_BODY="${MAIL_BODY}Storage Free:\t\t   ${LOCAL_SERVER_STORAGE[10]}\n"
MAIL_BODY="${MAIL_BODY}\n\n"
MAIL_BODY="${MAIL_BODY}\n\n"
MAIL_BODY="${MAIL_BODY}--------------------- EXTERNAL_SERVER STORAGE INFORMATION ---------------------------------"
MAIL_BODY="${MAIL_BODY}\n\n"
MAIL_BODY="${MAIL_BODY}Storage Capacity:\t ${EXTERNAL_SERVER_STORAGE[8]}\n"
MAIL_BODY="${MAIL_BODY}Storage Used:\t\t   ${EXTERNAL_SERVER_STORAGE[9]}\n"
MAIL_BODY="${MAIL_BODY}Storage Used (%):\t ${EXTERNAL_SERVER_STORAGE[11]}\n"
MAIL_BODY="${MAIL_BODY}Storage Free:\t\t   ${EXTERNAL_SERVER_STORAGE[10]}\n"
MAIL_BODY="${MAIL_BODY}\n\n"
MAIL_BODY="${MAIL_BODY}\n\n"
MAIL_BODY="${MAIL_BODY}--------------------- OWNCLOUD USER STORAGE INFORMATION ----------------------------"
MAIL_BODY="${MAIL_BODY}\n\n"

### List per-user OwnCloud storage information 
OC_USR_STORAGE_LENGTH=${#OC_USR_STORAGE[@]}
for (( STORAGECOUNTER=0; STORAGECOUNTER<${OC_USR_STORAGE_LENGTH}; STORAGECOUNTER++ ));
do
        case ${STORAGECOUNTER} in
                0)
                        MAIL_BODY="${MAIL_BODY}${OC_USR_STORAGE[i]}\t\tTotal storage used in all userfolders\n\n"
                        MAIL_BODY="${MAIL_BODY}Size\t\tUser folder\n"
                        MAIL_BODY="${MAIL_BODY}------------------------------------\n"
                        STORAGECOUNTER=$(( STORAGECOUNTER + 1 ))
                        continue
                ;;
                *)
                        ### Strip the "/home/ocdata/" information from the usernames in order to present a cleaner overview (customer does not need to know the real path)
                        CURR_PATH=${OC_USR_STORAGE[STORAGECOUNTER+1]/\/home\/ocdata\//}
                        MAIL_BODY="${MAIL_BODY}${OC_USR_STORAGE[${STORAGECOUNTER}]}\t\t${CURR_PATH}\n"
                        STORAGECOUNTER=$(( STORAGECOUNTER + 1 ))
                ;;
        esac
done

sudo -u ${MAIL_SENDER} echo -e ${MAIL_BODY} | sudo -u ${MAIL_SENDER} ${MAIL_AGENT} -s "${MAIL_SUBJECT}" ${MAIL_RECIPIENTS}

Screenshot of the received e-mail