3
0
mirror of https://github.com/rvalitov/zabbix-php-fpm.git synced 2023-11-05 03:30:27 +01:00

[fix] #35 analyze all pools

This commit is contained in:
Ramil Valitov 2020-06-29 14:32:31 +03:00
parent 27d55ad811
commit 9aae10855b
No known key found for this signature in database
GPG Key ID: 2AB186DABDFE1914

View File

@ -7,6 +7,7 @@ S_PS=$(type -P ps)
S_GREP=$(type -P grep) S_GREP=$(type -P grep)
S_AWK=$(type -P awk) S_AWK=$(type -P awk)
S_SORT=$(type -P sort) S_SORT=$(type -P sort)
S_UNIQ=$(type -P uniq)
S_HEAD=$(type -P head) S_HEAD=$(type -P head)
S_LSOF=$(type -P lsof) S_LSOF=$(type -P lsof)
S_JQ=$(type -P jq) S_JQ=$(type -P jq)
@ -33,6 +34,10 @@ if [[ ! -f $S_SORT ]]; then
${S_ECHO} "Utility 'sort' not found. Please, install it first." ${S_ECHO} "Utility 'sort' not found. Please, install it first."
exit 1 exit 1
fi fi
if [[ ! -f $S_UNIQ ]]; then
${S_ECHO} "Utility 'uniq' not found. Please, install it first."
exit 1
fi
if [[ ! -f $S_HEAD ]]; then if [[ ! -f $S_HEAD ]]; then
${S_ECHO} "Utility 'head' not found. Please, install it first." ${S_ECHO} "Utility 'head' not found. Please, install it first."
exit 1 exit 1
@ -216,14 +221,21 @@ fi
mapfile -t PS_LIST < <($S_PS ax | $S_GREP -F "php-fpm: pool " | $S_GREP -F -v "grep") mapfile -t PS_LIST < <($S_PS ax | $S_GREP -F "php-fpm: pool " | $S_GREP -F -v "grep")
# shellcheck disable=SC2016 # shellcheck disable=SC2016
POOL_LIST=$(${S_PRINTF} '%s\n' "${PS_LIST[@]}" | $S_AWK '{print $NF}' | $S_SORT -u) POOL_NAMES_LIST=$(${S_PRINTF} '%s\n' "${PS_LIST[@]}" | $S_AWK '{print $NF}' | $S_SORT -u)
POOL_FIRST=0 POOL_FIRST=0
#We store the resulting JSON data for Zabbix in the following var: #We store the resulting JSON data for Zabbix in the following var:
RESULT_DATA="{\"data\":[" RESULT_DATA="{\"data\":["
while IFS= read -r line; do while IFS= read -r line; do
# shellcheck disable=SC2016 # shellcheck disable=SC2016
POOL_PID=$(${S_PRINTF} '%s\n' "${PS_LIST[@]}" | $S_GREP -F -w "php-fpm: pool $line" | $S_HEAD -1 | $S_AWK '{print $1}') POOL_PID_LIST=$(${S_PRINTF} '%s\n' "${PS_LIST[@]}" | $S_GREP -F -w "php-fpm: pool $line" | $S_AWK '{print $1}')
POOL_PID_ARGS=""
while IFS= read -r POOL_PID; do
if [[ -n $POOL_PID ]]; then if [[ -n $POOL_PID ]]; then
POOL_PID_ARGS="$POOL_PID_ARGS -p $POOL_PID"
fi
done <<<"$POOL_PID_LIST"
if [[ -n $POOL_PID_ARGS ]]; then
#We search for socket or IP address and port #We search for socket or IP address and port
#Socket example: #Socket example:
#php-fpm7. 25897 root 9u unix 0x000000006509e31f 0t0 58381847 /run/php/php7.3-fpm.sock type=STREAM #php-fpm7. 25897 root 9u unix 0x000000006509e31f 0t0 58381847 /run/php/php7.3-fpm.sock type=STREAM
@ -236,14 +248,16 @@ while IFS= read -r line; do
#php-fpm7. 1203 www-data 8u IPv4 15070917 0t0 TCP localhost.localdomain:23054->localhost.localdomain:postgresql (ESTABLISHED) #php-fpm7. 1203 www-data 8u IPv4 15070917 0t0 TCP localhost.localdomain:23054->localhost.localdomain:postgresql (ESTABLISHED)
#More info at https://github.com/rvalitov/zabbix-php-fpm/issues/12 #More info at https://github.com/rvalitov/zabbix-php-fpm/issues/12
PrintDebug "Started analysis of pool $line, PID $POOL_PID" PrintDebug "Started analysis of pool $line, PID(s): $POOL_PID_ARGS"
#Extract only important information: #Extract only important information:
#Use -P to show port number instead of port name, see https://github.com/rvalitov/zabbix-php-fpm/issues/24 #Use -P to show port number instead of port name, see https://github.com/rvalitov/zabbix-php-fpm/issues/24
POOL_PARAMS_LIST=$($S_LSOF -P -p "$POOL_PID" 2>/dev/null | $S_GREP -w -e "unix" -e "TCP") #Sometimes different PHP-FPM versions may have the same names of pools, so we need to consider that.
# It's considered that a pair of pool name and socket must be unique.
#Sorting is required, because uniq needs it
POOL_PARAMS_LIST=$($S_LSOF -P $POOL_PID_ARGS 2>/dev/null | $S_GREP -w -e "unix" -e "TCP" | $S_SORT -u | $S_UNIQ -f8)
FOUND_POOL="" FOUND_POOL=""
while IFS= read -r pool; do while IFS= read -r pool; do
if [[ -n $pool ]]; then if [[ -n $pool ]]; then
if [[ -z $FOUND_POOL ]]; then
PrintDebug "Checking process: $pool" PrintDebug "Checking process: $pool"
# shellcheck disable=SC2016 # shellcheck disable=SC2016
POOL_TYPE=$(${S_ECHO} "${pool}" | $S_AWK '{print $5}') POOL_TYPE=$(${S_ECHO} "${pool}" | $S_AWK '{print $5}')
@ -259,6 +273,7 @@ while IFS= read -r line; do
if [[ ${POOL_STATUS} -gt 0 ]]; then if [[ ${POOL_STATUS} -gt 0 ]]; then
FOUND_POOL="1" FOUND_POOL="1"
PrintDebug "Success: socket $POOL_SOCKET returned valid status data" PrintDebug "Success: socket $POOL_SOCKET returned valid status data"
EncodeToJson "${line}" "${POOL_SOCKET}"
else else
PrintDebug "Error: socket $POOL_SOCKET didn't return valid data" PrintDebug "Error: socket $POOL_SOCKET didn't return valid data"
fi fi
@ -282,6 +297,7 @@ while IFS= read -r line; do
if [[ ${POOL_STATUS} -gt 0 ]]; then if [[ ${POOL_STATUS} -gt 0 ]]; then
FOUND_POOL="1" FOUND_POOL="1"
PrintDebug "Success: TCP connection $POOL_SOCKET returned valid status data" PrintDebug "Success: TCP connection $POOL_SOCKET returned valid status data"
EncodeToJson "${line}" "${POOL_SOCKET}"
else else
PrintDebug "Error: TCP connection $POOL_SOCKET didn't return valid data" PrintDebug "Error: TCP connection $POOL_SOCKET didn't return valid data"
fi fi
@ -297,23 +313,18 @@ while IFS= read -r line; do
else else
PrintDebug "Warning: pool type or socket is empty" PrintDebug "Warning: pool type or socket is empty"
fi fi
else
PrintDebug "Pool already found, skipping process: $pool"
fi
else else
PrintDebug "Error: failed to get process information. Probably insufficient privileges. Use sudo or run this script under root." PrintDebug "Error: failed to get process information. Probably insufficient privileges. Use sudo or run this script under root."
fi fi
done <<<"$POOL_PARAMS_LIST" done <<<"$POOL_PARAMS_LIST"
if [[ -n ${FOUND_POOL} ]]; then if [[ -z ${FOUND_POOL} ]]; then
EncodeToJson "${line}" "${POOL_SOCKET}"
else
PrintDebug "Error: failed to discover information for pool $line" PrintDebug "Error: failed to discover information for pool $line"
fi fi
else else
PrintDebug "Error: failed to find PID for pool $line" PrintDebug "Error: failed to find PID for pool $line"
fi fi
done <<<"$POOL_LIST" done <<<"$POOL_NAMES_LIST"
PrintDebug "Processing pools from old cache..." PrintDebug "Processing pools from old cache..."
for CACHE_ITEM in "${CACHE[@]}"; do for CACHE_ITEM in "${CACHE[@]}"; do