-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools-php-fpm-calculator
334 lines (303 loc) · 13.5 KB
/
tools-php-fpm-calculator
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#! /bin/bash
function print_logo ()
{
echo
echo -e "\e[38;5;81m ( ) ( ( ( * "
echo -e "\e[38;5;51m\e[1m )\ ) ( /( )\ ) )\ ) )\ ) ( \` \e[33m\e[1m ( "
echo -e "\e[38;5;45m(()/( )\())(()/( (()/( (()/( )\))( \e[38;5;214m ) )\ "
echo -e "\e[38;5;33m /())(()\ /()) /()) /())(()()\ \e[38;5;208m \` ) ( /( ((_) "
echo -e "\e[38;5;27m()) _(()()) ())|()) (()(() \e[38;5;124m/(/( )(_)) _ "
echo -e "\e[0m\e[38;5;21m| _ \ | || || _ \ _ | |_ | _ \ | \/ |\e[38;5;88m\e[1m(()\ \e[38;5;88m(()\e[0m\e[38;5;9m | | "
echo -ne "\e[38;5;21m| / | _ || /|_|| _| | / | |\/| |\e[38;5;9m| ' \\"
echo -e "\e[38;5;88m\e[1m)\e[0m\e[38;5;9m/ _\` || | "
echo -e "\e[38;5;21m|| |||||| || || || ||\e[38;5;9m| ._/ \,||_| "
for i in {17..21} {21..17} {17..21} {21..17} {17..21} {21..17} {17..21} {21..17} {17..17} ; do echo -en "\e[38;5;${i}m=\e[0m" ; done
echo -en "\e[38;5;9m |_| "
for i in {17..21} {21..21} {21..21} {21..21} {20..16} ; do echo -en "\e[38;5;${i}m=\e[0m" ; done
echo -e "\e[0m"
}
phpfpm_installed=1
fpm_type=''
function check_php-fpm_installed_and_processname ()
{
php-fpm -v 1> /dev/null 2>&1
if [ $? == 0 ]; then
phpfpm_installed=1
fpm_type=`php-fpm -i 2>&1 | grep "SERVER\[\"_\"\]" | cut -d\/ -f4`
fi
php5.6-fpm -v 1> /dev/null 2>&1
if [ $? == 0 ]; then
phpfpm_installed=1
fpm_type=`php5.6-fpm -i 2>&1 | grep "SERVER\[\"_\"\]" | cut -d\/ -f4`
fi
php-fpm7.2 -v 1> /dev/null 2>&1
if [ $? == 0 ]; then
phpfpm_installed=1
fpm_type=`php-fpm7.2 -i 2>&1 | grep "SERVER\['_'\]" | cut -d\/ -f4`
fi
if [ $phpfpm_installed == 0 ]; then # Exit if PHP-FPM is not installed
echo -e "\e[31m!!! PHP-FPM not detected. Exiting. !!!\e[0m"
echo
exit 1
fi
}
function check_php-fpm_running ()
{
ps aux | grep "php-fpm" | grep -v grep | grep -v "php-fpmpal" 1> /dev/null 2>&1
if [ $? == 1 ]; then
echo -e "\e[31m!!! PHP-FPM is installed but not running. PHP-FPM should be started before running this script. Exiting. !!!\e[0m"
echo
exit 1
fi
}
function check_bc_install ()
{
bc -v 1> /dev/null 2>&1
if [ $? != 0 ]; then
echo -e "\e[31m\"bc\" is not installed. This script depends on it. Exiting. \e[0m"
echo
exit 1
fi
}
function variable_initialisation ()
{
httpd_root=`apache2ctl -V | awk -F "\"" '/HTTPD_ROOT/ {print $2}'`
httpd_config_file=`apache2ctl -V | awk -F "\"" '/SERVER_CONFIG_FILE/ {print $2}'`
httpd_root_and_config_file=$httpd_root"/"$httpd_config_file
httpd_main_include=`awk -v var="$httpd_root" '/^Include/ {print var "/" $2}' $httpd_root_and_config_file`
IFS=$'\n' list_of_apache_includes=($(ls $httpd_main_include))
IFS=$'\n' list_of_pools=($(ps aux | grep "php-fpm" | grep -v ^root | grep -v grep | awk -F "pool " '{print $2}' | sort | uniq | sed -e 's/ //g'))
total_server_memory=`cat /proc/meminfo | grep MemTotal | awk '{print $2}'`
total_phpfpm_mem_usage=0
total_phpfpm_mem_usage_largest_process=0
total_phpfpm_mem_usage_average_process=0
let no_of_pools=(${#list_of_pools[@]}-1)
IFS=$'\n' list_of_includes=($(grep -i ^include `ps aux | egrep "php.*master" | grep -v grep | cut -d\( -f2 | cut -d\) -f1` | cut -d= -f2))
let no_of_includes=(${#list_of_includes[@]}-1)
function match_pool_to_config_file ()
{
for ((p=0; p<=no_of_pools; p++))
do
for ((i=0; i<=no_of_includes; i++ ))
do
grep "\[${list_of_pools[$p]}\]" ${list_of_includes[$i]} > /dev/null
if [ $? == 0 ]; then
pool_config_file[$p]="${list_of_includes[$i]}"
fi
done
done
}
match_pool_to_config_file
}
function display_pool_information ()
{
echo -en "\e[38;5;22m=\e[38;5;28m=\e[38;5;34m=\e[38;5;40m=\e[38;5;46m="
echo -en "\e[32m\e[1m ICS TOOLS \e[0m"
echo -e "\e[38;5;46m=\e[38;5;40m=\e[38;5;34m=\e[38;5;28m=\e[38;5;22m=\e[0m"
echo
echo -en "\e[38;5;22m=\e[38;5;28m=\e[38;5;34m=\e[38;5;40m=\e[38;5;46m="
echo -en "\e[32m\e[1m List of PHP-FPM pools \e[0m"
echo -e "\e[38;5;46m=\e[38;5;40m=\e[38;5;34m=\e[38;5;28m=\e[38;5;22m=\e[0m"
### For each pool
for ((i=0; i<=no_of_pools; i++))
do
echo -en "\t\e[36m\e[1m--- "
echo -en ${list_of_pools[$i]}
echo -e " ---\e[0m"
IFS=$'\n' list_of_pids=($(ps aux | grep "php-fpm" | grep -v ^root | grep -v grep | grep pool | awk '{print $2, $13}' | grep " ${list_of_pools[$i]}$" | awk '{print $1}'))
total_pool_mem_usage[$i]=0
for a in "${list_of_pids[@]}"
do
let total_pool_mem_usage[$i]+=`pmap -d $a | grep "writeable/private" | awk '{ print $4 }' | sed -e s/K//`
done
echo -en "\t\tTotal memory used: \e[1m"
echo -en `echo "scale =2; ${total_pool_mem_usage[$i]}" / 1024 | bc`; echo -en " MB\e[0m\t"
let total_phpfpm_mem_usage+=${total_pool_mem_usage[$i]}
echo -n "Processes: "
echo -en "\e[1m${#list_of_pids[@]}\e[0m"
echo -en " \e[2m(max_children: "
current_max_children_value=`grep "^pm.max_children" ${pool_config_file[$i]} | cut -d= -f2 | sed -e 's/ //g' | tail -1`
echo -en "$current_max_children_value)\e[0m\t"
echo -en "Ave. process: \e[1m"
pool_ave_process_size[$i]=`echo "${total_pool_mem_usage[$i]} / ${#list_of_pids[@]}" | bc`
echo -en `echo "scale =2; ${pool_ave_process_size[$i]}" / 1024 | bc`; echo -e " MB\e[0m"
potential_mem_usage=`echo "$current_max_children_value * ${pool_ave_process_size[$i]}" | bc`
let total_phpfpm_mem_usage_average_process+=$potential_mem_usage
done
}
function display_server_memory_information ()
{
echo
echo -en "\e[38;5;22m=\e[38;5;28m=\e[38;5;34m=\e[38;5;40m=\e[38;5;46m="
echo -en "\e[32m\e[1m Server memory usage statistics \e[0m"
echo -e "\e[38;5;46m=\e[38;5;40m=\e[38;5;34m=\e[38;5;28m=\e[38;5;22m=\e[0m"
echo -ne "\tTotal server memory: "
echo -ne "\t\t\t\t\e[1m"
echo -ne `echo "scale =2; $total_server_memory" / 1024 | bc`; echo -e " MB\e[0m"
echo -en "\tMemory available to assign to PHP-FPM pools: "
rhel7_check=0
if [ -f /etc/redhat-release ]; then
rhel7_check=`cat /etc/redhat-release | awk -F "release" '{print $2}' | awk '{print $1}' | cut -d. -f1` > /dev/null
fi
ubuntu1604_check=0
if [ -f /etc/lsb-release ]; then
ubuntu1604_check=`grep DISTRIB_ID /etc/lsb-release | cut -d= -f2`
if [ $ubuntu1604_check == 'Ubuntu' ]; then
ubuntu1604_check=`grep DISTRIB_RELEASE /etc/lsb-release | cut -d= -f2`
if [ $ubuntu1604_check == '16.04' ]; then
ubuntu1604_check=1
else
ubuntu1604_check=0
fi
else
ubuntu1604_check=0
fi
fi
ubuntu1804_check=0
if [ -f /etc/lsb-release ]; then
ubuntu1804_check=`grep DISTRIB_ID /etc/lsb-release | cut -d= -f2`
if [ $ubuntu1804_check == 'Ubuntu' ]; then
ubuntu1804_check=`grep DISTRIB_RELEASE /etc/lsb-release | cut -d= -f2`
if [ $ubuntu1804_check == '18.04' ]; then
ubuntu1804_check=1
else
ubuntu1804_check=0
fi
else
ubuntu1804_check=0
fi
fi
if [ $rhel7_check == '7' ]; then
total_phpfpm_allowed_memory=$(echo "`free -k | awk '/Mem/ {print $7}'` + $total_phpfpm_mem_usage" | bc)
elif [ $ubuntu1604_check == '1' ]; then
total_phpfpm_allowed_memory=$(echo "`free -k | awk '/Mem/ {print $7}'` + $total_phpfpm_mem_usage" | bc)
elif [ $ubuntu1804_check == '1' ]; then
total_phpfpm_allowed_memory=$(echo "`free -k | awk '/Mem/ {print $7}'` + $total_phpfpm_mem_usage" | bc)
else
total_phpfpm_allowed_memory=$(echo "`free -k | awk '/buffers\/cache/ {print $4}'` + $total_phpfpm_mem_usage" | bc)
fi
echo -ne "\t\e[1m"
echo -ne `echo "scale =2; $total_phpfpm_allowed_memory" / 1024 | bc`; echo -e " MB\e[0m"
echo -e "\t\t\e[2m(total free memory + PHP-FPM's current memory usage)\e[0m"
echo
echo -en "\tPotential PHP-FPM memory usage: "
echo -en "\t\t\e[1m"
echo -ne `echo "scale =2; $total_phpfpm_mem_usage_average_process" / 1024 | bc`; echo -en " MB\e[0m "
echo -ne "\e[33m (`echo "scale=2; $total_phpfpm_mem_usage_average_process*100/$total_phpfpm_allowed_memory" | bc `"
echo -n "%)"
if [ $total_phpfpm_mem_usage_average_process -gt $total_phpfpm_allowed_memory ]; then
echo -e " \e[31m!!! THIS IS LARGER THAN THE ASSIGNED MEMORY USAGE !!!\e[0m"
else
echo -e " \e[32m...GOOD :-)\e[0m"
fi
echo -e "\t\t\e[2m(based on current ave. process and max_children)\e[0m"
echo
}
function succinct_pool_recommendations ()
{
echo -en "\e[38;5;22m=\e[38;5;28m=\e[38;5;34m=\e[38;5;40m=\e[38;5;46m="
echo -en "\e[32m\e[1m Recommendations per pool \e[0m"
echo -e "\e[38;5;46m=\e[38;5;40m=\e[38;5;34m=\e[38;5;28m=\e[38;5;22m=\e[0m"
for ((i=0; i<=no_of_pools; i++))
do
pool_perc_mem_use[$i]=`echo "scale=2; ${total_pool_mem_usage[$i]}*100/$total_phpfpm_mem_usage" | bc`
pool_allowed_mem_use[$i]=`echo "$total_phpfpm_allowed_memory * ${pool_perc_mem_use[$i]} / 100" | bc`
pool_allowed_max_children[$i]=`echo "${pool_allowed_mem_use[$i]} / ${pool_ave_process_size[$i]}" | bc`
current_max_children_value=`grep "^pm.max_children" ${pool_config_file[$i]} | cut -d= -f2 | sed -e 's/ //g' | tail -1`
echo -e "\t\e[36m\e[1m--- ${list_of_pools[$i]} ---\e[0m"
echo -ne "\t\tCurrent usage: \e[1m${pool_perc_mem_use[$i]}%\e[0m"
echo -ne "\tRecommended max_children: \e[1m${pool_allowed_max_children[$i]}\e[0m"
echo -ne "\e[2m (current: $current_max_children_value)\e[0m"
echo -ne "\tConfig file: ${pool_config_file[$i]}"
echo
done
echo
}
function detailed_pool_recommendations ()
{
echo -en "\e[38;5;22m=\e[38;5;28m=\e[38;5;34m=\e[38;5;40m=\e[38;5;46m="
echo -en "\e[32m\e[1m Recommendations per pool \e[0m"
echo -e "\e[38;5;46m=\e[38;5;40m=\e[38;5;34m=\e[38;5;28m=\e[38;5;22m=\e[0m"
for ((i=0; i<=no_of_pools; i++))
do
pool_perc_mem_use[$i]=`echo "scale=2; ${total_pool_mem_usage[$i]}*100/$total_phpfpm_mem_usage" | bc`
pool_allowed_mem_use[$i]=`echo "$total_phpfpm_allowed_memory * ${pool_perc_mem_use[$i]} / 100" | bc`
pool_allowed_max_children[$i]=`echo "${pool_allowed_mem_use[$i]} / ${pool_ave_process_size[$i]}" | bc`
current_max_children_value=`grep "^pm.max_children" ${pool_config_file[$i]} | cut -d= -f2 | sed -e 's/ //g' | tail -1`
echo -e "\t\e[36m\e[1m--- ${list_of_pools[$i]} ---\e[0m"
echo -ne "\t\tCurrent usage: \e[1m${pool_perc_mem_use[$i]}%\e[0m"
echo -ne "\tRecommended max_children: \e[1m${pool_allowed_max_children[$i]}\e[0m"
echo -ne "\e[2m (current: $current_max_children_value)\e[0m"
echo -ne "\tConfig file: ${pool_config_file[$i]}"
echo
echo -ne "\t\tMemory usage: \e[1m"
echo -ne `echo "scale =2; ${total_pool_mem_usage[$i]}" / 1024 | bc`; echo -ne " MB\e[0m"
echo -ne "\tAllowed usage: \e[1m"
echo -ne `echo "scale =2; ${pool_allowed_mem_use[$i]}" / 1024 | bc`; echo -ne " MB\e[0m"
echo
done
echo
}
function logfile_observations ()
{
echo -en "\e[38;5;22m=\e[38;5;28m=\e[38;5;34m=\e[38;5;40m=\e[38;5;46m="
echo -en "\e[32m\e[1m Observations from the pool logfiles \e[0m"
echo -e "\e[38;5;46m=\e[38;5;40m=\e[38;5;34m=\e[38;5;28m=\e[38;5;22m=\e[0m"
main_phpfpm_config_file=`ps aux | grep "php-fpm: master process" | grep -v grep | awk -F 'process' '{print $2}' | sed -e 's/[() ]//g'`
error_log_location=`grep "^error_log" $main_phpfpm_config_file | awk '{print $3}'`
errors_in_logs=`zgrep "server reached pm.max_children" $error_log_location* | wc -l` > /dev/null
if [ $errors_in_logs != 0 ]; then
echo "\tFrom the PHP-FPM error logfiles ($error_log_location):"
zgrep "\tserver reached pm.max_children" $error_log_location* | awk '{print $5, $10}' | sed -e 's/[(),]//g' | sed -e 's/\]//g' | sort | uniq -c | awk '{print " - pool \033[36m" $2 "\033[0m had reached its max_children value of " $3 " on " $1 " occasion(s)"}'
echo
echo "\tFor these pools you may want to compare the recommended max_children value to this information, and decide whether the recommended value would be high enough to prevent max_children from being hit in future."
else
echo -e "\tNo other recommendations/observations at this stage."
fi
}
function general_script_info ()
{
echo
echo -en "\e[38;5;22m=\e[38;5;28m=\e[38;5;34m=\e[38;5;40m=\e[38;5;46m="
echo -en "\e[32m\e[1m General information about this script's usage \e[0m"
echo -e "\e[38;5;46m=\e[38;5;40m=\e[38;5;34m=\e[38;5;28m=\e[38;5;22m=\e[0m"
echo -e "\tNote: It is not ideal to run PHP-FPMpal shortly after restarting PHP-FPM or your webservices. This is because PHP-FPMpal makes recommendations based on the average pool process size, and if PHP-FPM was restarted a short while ago then the likelihood is high that there won't have been many requests made to the sites since the restart, and metrics will be skewed and not show a normalised average."
echo -e "\tIt is also worth noting that if you've recently restarted any services that normally use up a large amount of memory then you probably want to wait a while before running PHP-FPMpal (e.g. if MySQL normally uses 50% of memory, but you've just restarted it then it may only use 10% of memory right now, thus the recommendations will be very skewed)."
echo
}
function debug_print_variables()
{
echo "phpfpm_installed : $phpfpm_installed"
echo "fpm_type : $fpm_type"
echo "total_server_memory : $total_server_memory"
}
function main_verbose ()
{
check_php-fpm_installed_and_processname
check_php-fpm_running
check_bc_install
print_logo
variable_initialisation
display_pool_information
display_server_memory_information
detailed_pool_recommendations
logfile_observations
general_script_info
}
function main_succinct ()
{
check_php-fpm_installed_and_processname
check_php-fpm_running
check_bc_install
variable_initialisation
display_pool_information
display_server_memory_information
succinct_pool_recommendations
}
if [ $# != 0 ]; then
if [ $1 == "-d" ]; then
main_verbose
fi
else
main_succinct
fi