File: //proc/self/root/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