#!/usr/bin/env bash # # This code will check the storage size every 5 am # then check to see if storage < thrush-hold, if so then # it will send an send an email to users in this email list at 5 am. # # # # Crontab: 0 5 * * * /home/flo/chkdisk.sh ############################################################################# # mount location disk="/mnt/disk1" # disk to monitor # check percentage #currentusage=$(df -h | grep ${disk} | awk {'print $5'}) # get disk usage from monitored disk #maxusage="90%" # max 90% disk usage # check actual size currentusage=$(df -h | grep ${disk} | awk {'print $4'}) # get disk usage from monitored disk maxusage="100G" # max 100G disk usage # mail users #mail="" # mail to sent alert to mail="" # mail to sent alert to function maxExceeded() { # tell that mail is being sent echo "Max usage (${maxusage}) exceeded. Your disk usage is it at ${currentusage}. Trying to send mail-alert..." # check if the mail program exist type mail > /dev/null 2>&1 || { echo >&2 "Mail does not exist. Install it and run script again. Aborting script..."; exit; } # if the mail program exist we continue to this and send the alert mailbody="Max usage (${maxusage}) exceeded. Your disk (${disk}) usage is at ${currentusage}." echo ${mailbody} | mailx -s "Disk alert!" "${mail}" echo "Mail was sent to ${mail}" > /dev/null } function noIssues() { echo "No problems. Disk (${disk}) usage at ${currentusage}. Max is set to ${maxusage}." > /dev/null echo 1 > /home/flo/data } function main() { # this other data file is needed to keep tabs of if storage # has already been sent so it wont send it again # this data file needs 1 or 0 in it, initially. # manually, stick a 1 in this file called data first. the rest of the code should care of the rest. # echo 1 > /home/flo/data # this is still convuluted, cant quite see if this is actually functioning. # filename="/home/flo/data" # send no more than one emails line=$(head -n 1 $filename) # 0 no; 1 yes # check if a valid disk is chosen if [ ${currentusage} ]; then # check if current disk usage is less than or equal to max usage. if [ ${currentusage%?} -le ${maxusage%?} ]; then # Need a way to control the number of emails going out if [ ${line} -eq 1 ]; then # if it is less than or equal to max usage maxExceeded echo 0 > /home/flo/data else # no need to send echo 1 > /home/flo/data fi else # if it is ok we do nothing noIssues fi else # if the disk is not valid, print valid disks on system echo "Set a valid disk, and run script again. Your disks:" df -h fi } # init # main