80 lines
3.2 KiB
Bash
80 lines
3.2 KiB
Bash
|
|
#!/usr/bin/env bash
|
||
|
|
# Hot-adjust Apache MPM Event settings and graceful reload.
|
||
|
|
# Usage: tune-mpm.sh [--max-workers N] [--server-limit N] [--start-servers N]
|
||
|
|
# [--min-spare-threads N] [--max-spare-threads N]
|
||
|
|
# [--max-connections-per-child N]
|
||
|
|
|
||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
# Read current values from the config as defaults
|
||
|
|
CONFIG_FILE="/etc/httpd/conf.d/mpm-tuning.conf"
|
||
|
|
|
||
|
|
if [ ! -f "$CONFIG_FILE" ]; then
|
||
|
|
echo "Error: $CONFIG_FILE not found. Run detect-memory.sh first."
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Parse current values from config
|
||
|
|
current_start=$(grep -oP 'StartServers\s+\K\d+' "$CONFIG_FILE" 2>/dev/null || echo "1")
|
||
|
|
current_min_spare=$(grep -oP 'MinSpareThreads\s+\K\d+' "$CONFIG_FILE" 2>/dev/null || echo "5")
|
||
|
|
current_max_spare=$(grep -oP 'MaxSpareThreads\s+\K\d+' "$CONFIG_FILE" 2>/dev/null || echo "15")
|
||
|
|
current_max_workers=$(grep -oP 'MaxRequestWorkers\s+\K\d+' "$CONFIG_FILE" 2>/dev/null || echo "50")
|
||
|
|
current_server_limit=$(grep -oP 'ServerLimit\s+\K\d+' "$CONFIG_FILE" 2>/dev/null || echo "2")
|
||
|
|
current_max_conn=$(grep -oP 'MaxConnectionsPerChild\s+\K\d+' "$CONFIG_FILE" 2>/dev/null || echo "3000")
|
||
|
|
|
||
|
|
# Parse arguments
|
||
|
|
START_SERVERS=$current_start
|
||
|
|
MIN_SPARE_THREADS=$current_min_spare
|
||
|
|
MAX_SPARE_THREADS=$current_max_spare
|
||
|
|
MAX_REQUEST_WORKERS=$current_max_workers
|
||
|
|
SERVER_LIMIT=$current_server_limit
|
||
|
|
MAX_CONNECTIONS_PER_CHILD=$current_max_conn
|
||
|
|
|
||
|
|
while [[ $# -gt 0 ]]; do
|
||
|
|
case $1 in
|
||
|
|
--max-workers) MAX_REQUEST_WORKERS="$2"; shift 2 ;;
|
||
|
|
--server-limit) SERVER_LIMIT="$2"; shift 2 ;;
|
||
|
|
--start-servers) START_SERVERS="$2"; shift 2 ;;
|
||
|
|
--min-spare-threads) MIN_SPARE_THREADS="$2"; shift 2 ;;
|
||
|
|
--max-spare-threads) MAX_SPARE_THREADS="$2"; shift 2 ;;
|
||
|
|
--max-connections-per-child) MAX_CONNECTIONS_PER_CHILD="$2"; shift 2 ;;
|
||
|
|
--help|-h)
|
||
|
|
echo "Usage: $0 [--max-workers N] [--server-limit N] [--start-servers N]"
|
||
|
|
echo " [--min-spare-threads N] [--max-spare-threads N]"
|
||
|
|
echo " [--max-connections-per-child N]"
|
||
|
|
echo ""
|
||
|
|
echo "Current values:"
|
||
|
|
echo " StartServers: $current_start"
|
||
|
|
echo " MinSpareThreads: $current_min_spare"
|
||
|
|
echo " MaxSpareThreads: $current_max_spare"
|
||
|
|
echo " MaxRequestWorkers: $current_max_workers"
|
||
|
|
echo " ServerLimit: $current_server_limit"
|
||
|
|
echo " MaxConnectionsPerChild: $current_max_conn"
|
||
|
|
exit 0
|
||
|
|
;;
|
||
|
|
*) echo "Unknown option: $1"; exit 1 ;;
|
||
|
|
esac
|
||
|
|
done
|
||
|
|
|
||
|
|
# Write updated config
|
||
|
|
cat <<EOF > "$CONFIG_FILE"
|
||
|
|
<IfModule mpm_event_module>
|
||
|
|
StartServers ${START_SERVERS}
|
||
|
|
MinSpareThreads ${MIN_SPARE_THREADS}
|
||
|
|
MaxSpareThreads ${MAX_SPARE_THREADS}
|
||
|
|
ThreadLimit 64
|
||
|
|
ThreadsPerChild 25
|
||
|
|
MaxRequestWorkers ${MAX_REQUEST_WORKERS}
|
||
|
|
ServerLimit ${SERVER_LIMIT}
|
||
|
|
MaxConnectionsPerChild ${MAX_CONNECTIONS_PER_CHILD}
|
||
|
|
</IfModule>
|
||
|
|
EOF
|
||
|
|
|
||
|
|
echo "MPM config updated:"
|
||
|
|
echo " StartServers=$START_SERVERS ServerLimit=$SERVER_LIMIT MaxRequestWorkers=$MAX_REQUEST_WORKERS"
|
||
|
|
echo " MinSpareThreads=$MIN_SPARE_THREADS MaxSpareThreads=$MAX_SPARE_THREADS MaxConnectionsPerChild=$MAX_CONNECTIONS_PER_CHILD"
|
||
|
|
|
||
|
|
# Graceful reload
|
||
|
|
/usr/sbin/httpd -k graceful
|
||
|
|
echo "Apache graceful reload triggered."
|