-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpd-performance-tuning.sh
101 lines (75 loc) · 3.01 KB
/
httpd-performance-tuning.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env bash
set -eo pipefail
# Set default maximum memory percentage to 65%
: "${MAX_MEM_PERCENT:=65}"
function main {
local APACHE_SERVER="httpd"
# Check if Apache is running
if systemctl is-active "$APACHE_SERVER" > /dev/null 2>&1; then
echo "Apache is running"
else
>&2 echo "Apache is not running. Exiting script"
exit 1
fi
# Get system information
local mem_total
mem_total=$(grep MemTotal /proc/meminfo | awk '{print $2}')
local cpu_cores
cpu_cores=$(grep -c ^processor /proc/cpuinfo)
echo "Analyzing performance before changes:"
# Measure CPU utilization
local before_cpu
before_cpu=$(mpstat | awk '$3 ~ /[0-9.]+/ { print 100 - $3 }')
echo "CPU utilization: $before_cpu%"
# Measure average response time
local before_response_time
before_response_time=$(tail -n 1000 /var/log/httpd/access_log | awk '{ total += $NF } END { print total/NR }')
echo "Average response time: $before_response_time ms"
echo ""
# Optimize kernel settings for Apache
# Increase maximum number of open files
local max_open_files
max_open_files=$(($mem_total / 8 + $cpu_cores * 1024))
sysctl -w fs.file-max="$max_open_files"
# Increase maximum number of connections
sysctl -w net.core.somaxconn=32768
# Increase maximum number of memory pages
sysctl -w vm.nr_hugepages=$((mem_total / 2048))
# Increase maximum number of threads
sysctl -w kernel.threads-max=$((cpu_cores * 64))
# Increase maximum number of processes
sysctl -w kernel.pid_max=4194304
# Increase maximum number of inotify watches
sysctl -w fs.inotify.max_user_watches=1048576
# Apply changes
sysctl -p
echo "Kernel settings optimized for Apache web server"
# Wait for changes to take effect
sleep 60
echo "Analyzing performance after changes:"
# Measure CPU utilization
local after_cpu
after_cpu=$(mpstat | awk '$3 ~ /[0-9.]+/ { print 100 - $3 }')
echo "CPU utilization: $after_cpu%"
# Measure average response time
local after_response_time
after_response_time=$(tail -n 1000 /var/log/httpd/access_log | awk '{ total += $NF } END { print total/NR }')
echo "Average response time: $after_response_time ms"
# Calculate change in CPU utilization
local cpu_change
cpu_change=$(echo "($after_cpu-$before_cpu)/$before_cpu*100" | bc -l)
echo "Change in CPU utilization: $cpu_change%"
# Calculate change in response time
local response_time_change
response_time_change=$(echo "($after_response_time-$before_response_time)/$before_response_time*100" | bc -l)
echo "Change in response time: $response_time_change%"
# Check memory usage
local MAX_MEM_PERCENT=65
local max_mem
max_mem=$((mem_total * MAX_MEM_PERCENT / 100))
local current_mem
current_mem=$(awk '/^MemAvailable:/{print $2}' /proc/meminfo)
if [ "$current_mem" -lt "$max_mem" ]; then
>&2 echo "Not enough memory available. Exiting script."
exit 1
fi