Your IP : 216.73.216.21


Current Path : /cmd/
Upload File :
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

https://nalibali.org/post-sitemap.xml 2026-11-20T09:00:00+00:00 https://nalibali.org/page-sitemap.xml 2026-11-18T09:24:11+00:00 https://nalibali.org/stories-sitemap.xml 2026-05-05T06:48:38+00:00 https://nalibali.org/stories-sitemap2.xml 2026-05-05T06:48:38+00:00 https://nalibali.org/audio_stories-sitemap.xml 2026-03-27T06:53:13+00:00 https://nalibali.org/story_cards-sitemap.xml 2025-06-06T10:27:48+00:00 https://nalibali.org/story_seeds-sitemap.xml 2025-05-19T07:35:43+00:00 https://nalibali.org/rhymes-sitemap.xml 2025-06-17T07:43:10+00:00 https://nalibali.org/activity_sheets-sitemap.xml 2025-05-16T09:16:43+00:00 https://nalibali.org/printables-sitemap.xml 2025-05-16T09:28:40+00:00 https://nalibali.org/posters_bookmarks-sitemap.xml 2025-05-16T10:20:22+00:00 https://nalibali.org/team_member-sitemap.xml 2025-05-28T09:40:43+00:00 https://nalibali.org/video_stories-sitemap.xml 2025-06-23T08:48:28+00:00 https://nalibali.org/guides-sitemap.xml 2025-06-17T09:38:41+00:00 https://nalibali.org/story_supplements-sitemap.xml 2025-09-16T12:22:20+00:00 https://nalibali.org/vacancies-sitemap.xml 2026-09-17T07:05:01+00:00 https://nalibali.org/category-sitemap.xml 2026-11-20T09:00:00+00:00 https://nalibali.org/post_tag-sitemap.xml 2026-11-06T00:00:00+00:00 https://nalibali.org/test-sitemap.xml 2025-06-13T09:36:28+00:00 https://nalibali.org/story_categories-sitemap.xml 2026-05-05T06:48:38+00:00 https://nalibali.org/activity_sheets_categories-sitemap.xml 2025-05-16T09:16:43+00:00 https://nalibali.org/printables_categories-sitemap.xml 2025-05-16T09:28:40+00:00 https://nalibali.org/posters_bookmarks_categories-sitemap.xml 2025-05-16T10:20:22+00:00 https://nalibali.org/single_audio-sitemap.xml 2026-03-27T06:53:13+00:00 https://nalibali.org/rhyme_categories-sitemap.xml 2025-06-17T07:43:10+00:00 https://nalibali.org/guide_categories-sitemap.xml 2025-06-17T09:38:41+00:00 https://nalibali.org/author-sitemap.xml 2026-01-22T07:26:07+00:00