From 5e0d616ad05c095ef10794ef952b570cf3e8bee8 Mon Sep 17 00:00:00 2001 From: Christopher Date: Wed, 25 Oct 2023 23:32:04 -0500 Subject: [PATCH] Calculate the PHP Pool memory usage --- calculate-php-pool/README.md | 5 ++++ calculate-php-pool/run.sh | 55 ++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 calculate-php-pool/README.md create mode 100644 calculate-php-pool/run.sh diff --git a/calculate-php-pool/README.md b/calculate-php-pool/README.md new file mode 100644 index 0000000..3a011fd --- /dev/null +++ b/calculate-php-pool/README.md @@ -0,0 +1,5 @@ +# Run command + +```bash +bash -c "$(wget -qLO - https://raw.githubusercontent.com/bigbeartechworld/big-bear-scripts/master/calculate-php-pool/run.sh)" +``` diff --git a/calculate-php-pool/run.sh b/calculate-php-pool/run.sh new file mode 100644 index 0000000..036ca8e --- /dev/null +++ b/calculate-php-pool/run.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +# Step 1: Find running PHP-FPM processes +php_fpm_versions=$(ps -eo comm | grep php-fpm | grep -v grep | sort | uniq) + +if [[ -z "$php_fpm_versions" ]]; then + echo "No PHP-FPM processes found." + exit 1 +fi + +# Step 2: Allow user to select a version if multiple are found +array=($php_fpm_versions) +if [ ${#array[@]} -gt 1 ]; then + echo "Multiple PHP-FPM versions detected. Please select one:" + select version in "${array[@]}"; do + if [[ " ${array[*]} " =~ " ${version} " ]]; then + php_fpm_version=$version + break + else + echo "Invalid selection. Please try again." + fi + done +else + php_fpm_version=${array[0]} +fi + +echo "Selected PHP-FPM version: $php_fpm_version" + +# Step 3: Calculate average memory usage +avg_memory=$(ps --no-headers -o rss -C $php_fpm_version | awk '{sum+=$1; ++n} END {print sum/n/1024}') + +echo "Average Memory Usage per process (MB): $avg_memory" + +# Extract PHP version number from the selected PHP-FPM version +php_version=$(echo "$php_fpm_version" | sed -E 's/[^0-9]*([0-9]+\.[0-9]+).*/\1/') + +# Step 4: Read PHP-FPM pool configuration (adjust the path to your php-fpm pool config) +pool_config="/etc/php/$php_version/fpm/pool.d/www.conf" # Example path, change as needed +if [[ -f "$pool_config" ]]; then + max_children=$(grep "^pm.max_children" $pool_config | cut -d "=" -f2 | xargs) +else + echo "Pool configuration file not found." + exit 1 +fi + +echo "Max children set in pool: $max_children" + +# Calculate total memory requirement and display in MB or GB +total_memory=$(echo "$avg_memory * $max_children" | bc) +if (( $(echo "$total_memory < 1024" | bc -l) )); then + echo "Total estimated memory required for all PHP-FPM processes: $total_memory MB" +else + total_memory_gb=$(echo "$total_memory / 1024" | bc -l) + printf "Total estimated memory required for all PHP-FPM processes: %.2f GB\n" $total_memory_gb +fi