User quota low notification

Hello, any advice to create a script and send a notification email that the user quota is running low?

Hello @fra,

You could use something like this as a base.

Note: Keep in mind that I don’t check for errors nor send the email, but you should have all info (user, quota limit, used quota, percentage of used quota, user email) to implement those features.

#!/usr/bin/env bash
source /etc/hestiacp/hestia.conf
source "$HESTIA/func/main.sh"
warning_percentage="90"

for i in $("$BIN"/v-list-users json | jq -r 'to_entries[] | "\(.key)|\(.value | .DISK_QUOTA)|\(.value | .CONTACT)"' | grep -v unlimited); do
        if [[ -z "$i" ]]; then
                echo "I didn't found user with quotas"
                exit
        fi
        user="$(awk -F '|' '{print $1}' <<<"$i")"
        email="$(awk -F '|' '{print $3}' <<<"$i")"
        result_quota="$(quota -w -u "$user" | tail -n1)"
        blocks_used="$(awk '{print $2}' <<<"$result_quota" | grep -oE '^[0-9]{1,}')"
        blocks_used_mb="$((blocks_used / 1024))"
        blocks_quota="$(awk '{print $3}' <<<"$result_quota")"
        blocks_quota_mb="$((blocks_quota / 1024))"
        used_percentage="$((blocks_used * 100 / blocks_quota))"
        echo "User $user is using ${used_percentage}% of quota: ${blocks_used_mb} MB of ${blocks_quota_mb} MB"
        if [[ "$used_percentage" -gt "$warning_percentage" ]]; then
                echo "Send email to $email to inform that used space is above ${warning_percentage}%"
        else
                echo "There is no need to send an email to $email, the used space is below ${warning_percentage}%"
        fi
        echo ""
done

I hope it helps.

Cheers,
sahsanu

Great it works, thanks, nice script

I changed the result_quota line with -w, --no-wrap Do not wrap the line if the device name is too long

result_quota="$(quota -uw "$user" | tail -n1)"

else in some case can produce more lines than expected and parsing get wrong in blocks_used

I share my script that create a txt file with all users out of quota, a file ready to be sent (to admin) with sendmail with this command

sendmail [email protected] <hestia-quota-email.txt
#!/usr/bin/env bash
source /etc/hestiacp/hestia.conf
source "$HESTIA/func/main.sh"

warning_percentage="90"
body_file="hestia-quota-email.txt"
server="$(hostname)"
subject="Quota exceeded for server $server"

rm $body_file 2> /dev/null

echo "Subject: $subject" >> $body_file
echo "Server $server" >> $body_file
echo "" >> $body_file

for i in $("$BIN"/v-list-users json | jq -r 'to_entries[] | "\(.key)|\(.value | .DISK_QUOTA)|\(.value | .CONTACT)"' | grep -v unlimited); do
        if [[ -z "$i" ]]; then
            echo "I didn't found user with quotas"
            exit
        fi
        user="$(awk -F '|' '{print $1}' <<<"$i")"
        email="$(awk -F '|' '{print $3}' <<<"$i")"
        result_quota="$(quota -uw "$user" | tail -n1)"
        blocks_used="$(awk '{print $2}' <<<"$result_quota")"
        blocks_used_mb="$((blocks_used / 1024))"
        blocks_quota="$(awk '{print $3}' <<<"$result_quota")"
        blocks_quota_mb="$((blocks_quota / 1024))"
        used_percentage="$((blocks_used * 100 / blocks_quota))"

        if [[ "$used_percentage" -gt "$warning_percentage" ]]; then
            echo "User $user" >> $body_file
            echo "Send email to $email to inform that used space is above ${warning_percentage}%" >> $body_file
            echo "User $user is using ${used_percentage}% of quota: ${blocks_used_mb} MB of ${blocks_quota_mb} MB" >> $body_file
            echo "" >> $body_file
        # else
            # echo "There is no need to send an email to $email, the used space is below ${warning_percentage}%"
        fi
        # echo ""
done
1 Like