HEX
Server: nginx/1.29.3
System: Linux 11979.bigscoots-wpo.com 6.8.0-88-generic #89-Ubuntu SMP PREEMPT_DYNAMIC Sat Oct 11 01:02:46 UTC 2025 x86_64
User: nginx (1068)
PHP: 7.4.33
Disabled: exec,system,passthru,shell_exec,proc_open,proc_close,popen,show_source,cmd# Do not modify this line # 1684243876
Upload Files
File: //bigscoots/wpo_diskspacecheck.sh
#!/bin/bash

source /bigscoots/includes/common.sh

if [[ $# -eq 0 ]]
then
    echo >&2 "Requires arguments:"
    echo >&2 "domain.com"
    echo >&2 "ALL"
    exit 1
fi

if [[ $1 == "ALL" ]]; then
    unset domain
else
    domain=$1
fi

# Global variable for the base path
BASE_PATH="/home/nginx/domains/"

# Function to calculate disk usage
calculate_wp_disk_usage() {
    local domain=$1
    local full_path="${BASE_PATH}${domain}"
    du -sh "$full_path" 2>/dev/null | awk '{print $1}' | sed -r 's/([0-9]+)([a-zA-Z])/\1 \2/g'
}

# Function to calculate WordPress database disk usage
calculate_wp_db_usage() {
    local domain=$1

    if [[ -z "$domain" || "$domain" == "ALL" ]]; then
        du -sh --exclude 'ib_logfile*' --exclude 'ibdata*' /var/lib/mysql/ 2>/dev/null | tail -1 | awk '{print $1}' | sed -r 's/([0-9]+)([a-zA-Z])/\1 \2/g'
        return
    fi

    local wpinstall="${BASE_PATH}${domain}/public"

    if [[ -d $wpinstall ]]; then
        local db_name=$(wpcli config get DB_NAME --path=$wpinstall 2>/dev/null)
        du -sh "/var/lib/mysql/${db_name}" 2>/dev/null | awk '{print $1}' | sed -r 's/([0-9]+)([a-zA-Z])/\1 \2/g'
    else
        echo "0 G"
    fi
}

# Function to convert all disk sizes to a common unit (bytes)
convert_to_bytes() {
    local size=$(echo $1 | cut -d. -f1)
    local unit=$2

    case "$unit" in
        "G")
            echo $((size * 1024 * 1024 * 1024))
            ;;
        "M")
            echo $((size * 1024 * 1024))
            ;;
        "K")
            echo $((size * 1024))
            ;;
        *)
            echo $size
            ;;
    esac
}

# Function to calculate the total disk usage
calculate_wp_total_usage() {
    local file_size_bytes=$(convert_to_bytes $fdusize $fduunit)
    local db_size_bytes=$(convert_to_bytes $ddusize $dduunit)

    local total_size_bytes=$((file_size_bytes + db_size_bytes))

    # Determine the appropriate unit
    if ((total_size_bytes >= 1024**3)); then  # GB
        echo "$((total_size_bytes / 1024**3)) G"
    elif ((total_size_bytes >= 1024**2)); then  # MB
        echo "$((total_size_bytes / 1024**2)) M"
    elif ((total_size_bytes >= 1024)); then  # KB
        echo "$((total_size_bytes / 1024)) K"
    else  # Bytes
        echo "${total_size_bytes} B"
    fi
}

# Function to print disk usage information in JSON format
print_json_output() {
    echo "[{\"name\": \"File Disk Usage\",\"size\": \"$fdusize\",\"value\": \"$fduunit\"},{\"name\": \"Database Disk Usage\",\"size\": \"$ddusize\",\"value\": \"$dduunit\"},{\"name\": \"Total Disk Usage\",\"size\": \"$tdusize\",\"value\": \"$tduunit\"}]"
}

read fdusize fduunit <<< $(calculate_wp_disk_usage "$domain")
read ddusize dduunit <<< $(calculate_wp_db_usage "$domain")
read tdusize tduunit <<< $(calculate_wp_total_usage)
print_json_output