File: //bigscoots/wpo/mail/quotachk.sh
#!/bin/bash
# Define thresholds and email address
THRESHOLDS=(75 85 95)
# Function to get the current date
get_date() {
echo $(date '+%Y-%m-%d')
}
# Function to check if an email has been sent today for a specific threshold
email_sent_today() {
local user=$1
local threshold=$2
local filename="/root/mailqcheck/email_sent_${user}_${threshold}.log"
if [[ -e $filename ]]; then
local date_sent=$(cat $filename)
[[ $date_sent == $(get_date) ]]
else
return 1
fi
}
# Function to send an email and log the sent date
send_email() {
local user=$1
local threshold=$2
local domain=$3
local disk_usage=$4
local filename="/root/mailqcheck/email_sent_${user}_${threshold}.log"
curl -s "https://api-dev.bigscoots.com/hook/send-email-notification?hash=1a18fefe16e4ab6ae1967a2ad980f396&percentage=$threshold&username=$user"
if [ "$threshold" -ge 95 ]
then
bash /bigscoots/general/slack.sh "#wpo-customer-alerts" ":mailbox:WPO EMAIL: Sending email for $user at $threshold% usage"
fi
echo $(get_date) > $filename
}
# Iterate over accounts
/usr/sbin/whmapi1 listaccts --output=json | jq -r '.data.acct[] | "\(.user) \(.diskused) \(.disklimit) \(.domain)"' | while read line; do
user=$(echo $line | awk '{print $1}');
disk_used=$(echo $line | awk '{print $2}' | sed 's/M//g');
disk_limit=$(echo $line | awk '{print $3}' | sed 's/M//g');
domain=$(echo $line | awk '{print $4}');
# Calculate disk usage percentage
if (( $(echo "$disk_limit > 0.01" | bc -l) )) && [[ ! "$disk_limit" =~ [^0-9.] ]]; then
disk_usage=$(awk "BEGIN {printf(\"%.2f\", (${disk_used}/${disk_limit})*100)}")
#echo "User: $user, Domain: $domain, Disk Usage: $disk_usage%"
else
#echo "User: $user, Domain: $domain, Disk Usage: N/A"
continue
fi
# Check thresholds and send email if necessary
for threshold in "${THRESHOLDS[@]}"; do
if (( $(echo "$disk_usage >= $threshold" | bc -l) )); then
if ! email_sent_today $user $threshold; then
send_email $user $threshold $domain $disk_usage
fi
fi
done
done