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/db/cnf_manager.sh
#!/bin/bash

# 1. Robust RAM Detection via cgroups
get_cgroup_mem() {
    local mem_limit
    # Try cgroup v2 first (standard on newer systems)
    if [ -f "/sys/fs/cgroup/memory.max" ]; then
        mem_limit=$(cat /sys/fs/cgroup/memory.max)
    # Fallback to cgroup v1
    elif [ -f "/sys/fs/cgroup/memory/memory.limit_in_bytes" ]; then
        mem_limit=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes)
    fi

    # Check if we got a valid number and it's not "max" (which means unlimited)
    if [[ "$mem_limit" =~ ^[0-9]+$ ]]; then
        # Convert bytes to MiB
        echo $((mem_limit / 1024 / 1024))
    else
        # If cgroup limit is "max" or missing, fallback to free -m as a last resort
        free -m | awk '/^Mem:/{print $2}'
    fi
}

TOTAL_RAM=$(get_cgroup_mem)

# 2. Exit if the server is a "Large/Dedicated" instance (over 13GB)
if [ "$TOTAL_RAM" -gt 13000 ]; then
    echo "Memory exceeds 12GB Business Plan. Skipping to avoid impacting large customer."
    exit 0
fi

# 3. Define logic for Starters, Pros, and Business
if [ "$TOTAL_RAM" -le 6500 ]; then
    PLAN="Starter"
    POOL="1536M"
    CONN=100
    TMP=32M
    QCACHE=16M
elif [ "$TOTAL_RAM" -le 9200 ]; then
    PLAN="Professional"
    POOL="3G"
    CONN=150
    TMP=64M
    QCACHE=32M
else
    PLAN="Business"
    POOL="5G"
    CONN=200
    TMP=128M
    QCACHE=64M
fi

# 4. Locate Config
CONF_FILE="/etc/my.cnf"
[ ! -f "$CONF_FILE" ] && CONF_FILE="/etc/my.cnf.d/my.cnf"

# --- NEW LOGIC: Check if changes are actually needed ---
NEEDS_UPDATE=false

check_val() {
    local key=$1
    local target=$2
    # Pull current value from config, stripping whitespace and equal signs
    local current=$(grep -E "^${key}[[:space:]]*=" "$CONF_FILE" | awk -F'=' '{print $2}' | tr -d '[:space:]')
    
    if [ "$current" != "$target" ]; then
        NEEDS_UPDATE=true
    fi
}

check_val "innodb_buffer_pool_size" "$POOL"
check_val "max_connections" "$CONN"
check_val "tmp_table_size" "$TMP"
check_val "max_heap_table_size" "$TMP"
check_val "query_cache_size" "$QCACHE"

if [ "$NEEDS_UPDATE" = false ]; then
    echo "Configuration for $PLAN plan is already optimal. No changes needed."
    exit 0
fi
# --- END NEW LOGIC ---

echo "Changes detected. Applying $PLAN template (Pool: $POOL)..."
cp "$CONF_FILE" "${CONF_FILE}.bak"

# 5. Apply Settings
sed -i -E "s/^(innodb_buffer_pool_size)[[:space:]]*=[[:space:]]*.*/\1 = $POOL/" "$CONF_FILE"
sed -i -E "s/^(max_connections)[[:space:]]*=[[:space:]]*.*/\1 = $CONN/" "$CONF_FILE"
sed -i -E "s/^(tmp_table_size)[[:space:]]*=[[:space:]]*.*/\1 = $TMP/" "$CONF_FILE"
sed -i -E "s/^(max_heap_table_size)[[:space:]]*=[[:space:]]*.*/\1 = $TMP/" "$CONF_FILE"
sed -i -E "s/^(query_cache_size)[[:space:]]*=[[:space:]]*.*/\1 = $QCACHE/" "$CONF_FILE"

# Clean up massive defaults
sed -i -E "s/^(key_buffer_size)[[:space:]]*=[[:space:]]*.*/\1 = 32M/" "$CONF_FILE"
sed -i -E "s/^(table_open_cache)[[:space:]]*=[[:space:]]*.*/\1 = 2000/" "$CONF_FILE"
sed -i -E "s/^(table_definition_cache)[[:space:]]*=[[:space:]]*.*/\1 = 2000/" "$CONF_FILE"
sed -i -E "s/^(thread_cache_size)[[:space:]]*=[[:space:]]*.*/\1 = 32/" "$CONF_FILE"

# 6. Restart MariaDB
systemctl restart mariadb
echo "Success: $PLAN settings applied and MariaDB restarted."