| Current Path : /cmd/ |
| Current File : //cmd/zweb-quota-scan |
#!/usr/bin/bash
debug=0
send=1
base='/srv/web'
perc_trigger=5
help_desk='help@zootzone.com'
kb_to_gb() {
local kb=$1
local gb=$(echo "scale=2; $kb/1024/1024" | bc)
echo $gb | sed 's/^\./0./' # prefix 0
}
cd $base
host="$(hostname).$(dnsdomainname)"
if ! echo "$host" | grep -qiE '^([a-z0-9]{1})([a-z0-9.-]{1,})([a-z]{2,15})$'; then
echo "invalid host $host"
exit 1
fi
for user in w*; do
# missing user
if ! id $user &>/dev/null; then
if [ $debug -eq 1 ]; then
echo "$user: user missing"
fi
continue
fi
# not served by nginx
if [ ! -f /etc/nginx/hosts/$user ]; then
continue
fi
quota_output=$(quota $user | tr -s ' ' | sed 's/^[ \t]//g' | grep '^/')
if [ "$quota_output" == '' ]; then
if [ $debug -eq 1 ]; then
echo "$user: no quota set"
fi
continue
fi
used=$(echo "$quota_output" | cut -d ' ' -f2 | tr -d '*')
quota=$(echo "$quota_output" | cut -d ' ' -f3)
# calculate % quota used and round to an integer
perc_now=$(
echo "scale=2; ($used/$quota)*100" \
| bc \
| awk '{printf("%d\n",$1 + 0.5)}'
)
cd $base/$user
# possibly override change with user's config
user_perc_trigger='etc/notify/quota_percentage_change'
if [ -f $user_perc_trigger ]; then
if grep -qE '^[1-9]?[0-9]$' $user_perc_trigger && [ "$(wc -l < $user_perc_trigger)" -eq 1 ]; then
perc_trigger=$(head -n 1 $user_perc_trigger)
if [ $debug -eq 1 ]; then
echo "$user: perc_trigger = $perc_trigger"
fi
fi
fi
# create and change to the sys dir
mkdir -p sys
cd sys
first_run=0
perc_diff=0
if [ -f .quota_last_perc ]; then
perc_last=$(head -n 1 .quota_last_perc)
echo $perc_now > .quota_last_perc
perc_diff=$((perc_now-perc_last))
if echo $perc_diff | grep -q ^-; then
sign='-'
else
sign='+'
fi
perc_diff=$(echo $perc_diff | tr -d '-')
else
first_run=1
echo $perc_now > .quota_last_perc
fi
used_gb=$(kb_to_gb $used)
quota_gb=$(kb_to_gb $quota)
echo "$used_gb/$quota_gb GB (${perc_now}%)" > storage
if [ $debug -eq 1 ]; then
echo "$user: $used_gb/$quota_gb GB (${perc_now}%)"
fi
notify='none'
if [ $perc_now -ge 100 ]; then
if [ -f .quota_alert_100 ]; then
continue
else
notify='URGENT'
touch .quota_alert_100
fi
else
rm -f .quota_alert_100
if [ $perc_diff -ge $perc_trigger ]; then
notify='CHANGE'
elif [ $first_run -eq 1 ]; then
notify='REPORT'
else
continue
fi
fi
cd $base/$user/etc
if [ $debug -eq 1 ]; then
recipients="$help_desk"
elif [ -f notify/webmasters ]; then
if [ $first_run -eq 1 ]; then
recipients="$help_desk"
else
recipients=''
fi
while read recipient; do
if echo "$recipient" | grep -q '@'; then
recipients="$recipients $recipient"
fi
done < notify/webmasters
if ! echo "$recipients" | grep -q '@'; then
recipients="$help_desk"
fi
else
recipients="$help_desk"
fi
# remove leading, repeated and trailing whitespace
recipients=$(echo "$recipients" | sed 's/^[ \t]\+//g;s/\t\+/ /g;s/ \+/ /g;s/[ \t]\+$//g')
header_to=$(echo "$recipients" | sed 's/ /, /g')
if [ -f nginx/hosts ]; then
sites=$(cat nginx/hosts)
else
sites='unknown'
fi
if [ $send -eq 0 ]; then
echo "recipients: $header_to"
echo "subject: $notify [$user] web disk quota @ ${perc_now}%"
sleep 1
continue
fi
cat << MESSAGE | /cmd/qmail-inject -f noreply@$host $recipients
From: noreply@$host
Reply-To: $help_desk
To: $header_to
Subject: $notify [$user] web disk quota @ ${perc_now}%
Date: $(date -R)
This is a system generated message.
Disk quota report for web hosting account $user:
$used_gb/$quota_gb GB (${perc_now}%)
The account hosts the following sites:
$sites
Recipients of this message:
$(echo "$recipients" | tr " " "\n")
Notifications are sent when the:
* account is created
* account quota alert is reset
* quota changes by at least 5%
* quota reaches 100%
Why changes may occur:
* the server creates log files of activity
* the website's code adds/removes files
* users add/remove content
Possible ways to reduce quota use:
* remove files not used by the website
* remove old backup archives
* reduce media file size
* remove old log files
The website may not function correctly if the quota reaches 100%
If you're unable to reduce the account's quota use, consider requesting an upgrade:
https://zootzone.com/prices
If you require assistance, reply to this message.
--
help@zootzone.com
MESSAGE
done