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

Fix ShellCheck errors and warnings (#25)

* [fix] comparison operators

* [fix] indents, globbing issue, and other shellcheck related fixes
This commit is contained in:
Ramil Valitov 2020-02-06 23:11:57 +01:00 committed by GitHub
parent b4e4e172f3
commit ccf91cbf17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 250 additions and 243 deletions

View File

@ -3,19 +3,19 @@
#https://github.com/rvalitov/zabbix-php-fpm #https://github.com/rvalitov/zabbix-php-fpm
#This script scans local machine for active PHP-FPM pools and returns them as a list in JSON format #This script scans local machine for active PHP-FPM pools and returns them as a list in JSON format
S_PS=`type -P ps` 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_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)
S_DIRNAME=`type -P dirname` S_DIRNAME=$(type -P dirname)
S_CAT=`type -P cat` S_CAT=$(type -P cat)
S_BASH=`type -P bash` S_BASH=$(type -P bash)
S_ECHO=`type -P echo` S_ECHO=$(type -P echo)
S_PRINTF=`type -P printf` S_PRINTF=$(type -P printf)
S_WHOAMI=`type -P whoami` S_WHOAMI=$(type -P whoami)
if [[ ! -f $S_PS ]]; then if [[ ! -f $S_PS ]]; then
${S_ECHO} "Utility 'ps' not found. Please, install it first." ${S_ECHO} "Utility 'ps' not found. Please, install it first."
@ -68,12 +68,12 @@ fi
STATUS_PATH="/php-fpm-status" STATUS_PATH="/php-fpm-status"
DEBUG_MODE="" DEBUG_MODE=""
ACTIVE_USER=`${S_WHOAMI}` ACTIVE_USER=$(${S_WHOAMI})
# Prints a string on screen. Works only if debug mode is enabled. # Prints a string on screen. Works only if debug mode is enabled.
function PrintDebug(){ function PrintDebug() {
if [[ ! -z $DEBUG_MODE ]] && [[ ! -z $1 ]]; then if [[ -n $DEBUG_MODE ]] && [[ -n $1 ]]; then
${S_ECHO} $1 ${S_ECHO} "$1"
fi fi
} }
@ -82,15 +82,15 @@ function PrintDebug(){
# - pool name # - pool name
# - pool socket # - pool socket
# Function returns 1 if all OK, and 0 otherwise. # Function returns 1 if all OK, and 0 otherwise.
function EncodeToJson(){ function EncodeToJson() {
POOL_NAME=$1 POOL_NAME=$1
POOL_SOCKET=$2 POOL_SOCKET=$2
if [[ -z ${POOL_NAME} ]] || [[ -z ${POOL_SOCKET} ]]; then if [[ -z ${POOL_NAME} ]] || [[ -z ${POOL_SOCKET} ]]; then
return 0 return 0
fi fi
JSON_POOL=`${S_ECHO} -n "$POOL_NAME" | ${S_JQ} -aR .` JSON_POOL=$(${S_ECHO} -n "$POOL_NAME" | ${S_JQ} -aR .)
JSON_SOCKET=`${S_ECHO} -n "$POOL_SOCKET" | ${S_JQ} -aR .` JSON_SOCKET=$(${S_ECHO} -n "$POOL_SOCKET" | ${S_JQ} -aR .)
if [[ ${POOL_FIRST} == 1 ]]; then if [[ ${POOL_FIRST} == 1 ]]; then
RESULT_DATA="$RESULT_DATA," RESULT_DATA="$RESULT_DATA,"
fi fi
@ -104,17 +104,18 @@ function EncodeToJson(){
# - pool name # - pool name
# - pool socket # - pool socket
# Function returns 1 if the pool is in cache, and 0 otherwise. # Function returns 1 if the pool is in cache, and 0 otherwise.
function IsInCache(){ function IsInCache() {
SEARCH_NAME=$1 SEARCH_NAME=$1
SEARCH_SOCKET=$2 SEARCH_SOCKET=$2
if [[ -z ${SEARCH_NAME} ]] || [[ -z ${SEARCH_SOCKET} ]]; then if [[ -z ${SEARCH_NAME} ]] || [[ -z ${SEARCH_SOCKET} ]]; then
return 0 return 0
fi fi
for CACHE_ITEM in "${NEW_CACHE[@]}" for CACHE_ITEM in "${NEW_CACHE[@]}"; do
do # shellcheck disable=SC2016
ITEM_NAME=`${S_ECHO} "$CACHE_ITEM" | ${S_AWK} '{print $1}'` ITEM_NAME=$(${S_ECHO} "$CACHE_ITEM" | ${S_AWK} '{print $1}')
ITEM_SOCKET=`${S_ECHO} "$CACHE_ITEM" | ${S_AWK} '{print $2}'` # shellcheck disable=SC2016
if [[ ${ITEM_NAME} == ${SEARCH_NAME} ]] && [[ ${ITEM_SOCKET} == ${SEARCH_SOCKET} ]]; then ITEM_SOCKET=$(${S_ECHO} "$CACHE_ITEM" | ${S_AWK} '{print $2}')
if [[ ${ITEM_NAME} == "${SEARCH_NAME}" ]] && [[ ${ITEM_SOCKET} == "${SEARCH_SOCKET}" ]]; then
return 1 return 1
fi fi
done done
@ -128,30 +129,30 @@ function IsInCache(){
# 1 if the pool is OK and is ondemand and is not in cache # 1 if the pool is OK and is ondemand and is not in cache
# 2 if the pool is OK and is ondemand and is in cache # 2 if the pool is OK and is ondemand and is in cache
# 3 if the pool is OK and is not ondemand and is not in cache # 3 if the pool is OK and is not ondemand and is not in cache
function ProcessPool(){ function ProcessPool() {
POOL_NAME=$1 POOL_NAME=$1
POOL_SOCKET=$2 POOL_SOCKET=$2
if [[ -z ${POOL_NAME} ]] || [[ -z ${POOL_SOCKET} ]]; then if [[ -z ${POOL_NAME} ]] || [[ -z ${POOL_SOCKET} ]]; then
return 0 return 0
fi fi
IsInCache ${POOL_NAME} ${POOL_SOCKET} IsInCache "${POOL_NAME}" "${POOL_SOCKET}"
FOUND=$? FOUND=$?
if [[ ${FOUND} == 1 ]]; then if [[ ${FOUND} == 1 ]]; then
return 2 return 2
fi fi
STATUS_JSON=`${S_BASH} ${STATUS_SCRIPT} ${POOL_SOCKET} ${STATUS_PATH}` STATUS_JSON=$(${S_BASH} "${STATUS_SCRIPT}" "${POOL_SOCKET}" ${STATUS_PATH})
EXIT_CODE=$? EXIT_CODE=$?
if [[ ${EXIT_CODE} == 0 ]]; then if [[ ${EXIT_CODE} == 0 ]]; then
# The exit code is OK, let's check the JSON data # The exit code is OK, let's check the JSON data
# JSON data example: # JSON data example:
# {"pool":"www2","process manager":"ondemand","start time":1578181845,"start since":117,"accepted conn":3,"listen queue":0,"max listen queue":0,"listen queue len":0,"idle processes":0,"active processes":1,"total processes":1,"max active processes":1,"max children reached":0,"slow requests":0} # {"pool":"www2","process manager":"ondemand","start time":1578181845,"start since":117,"accepted conn":3,"listen queue":0,"max listen queue":0,"listen queue len":0,"idle processes":0,"active processes":1,"total processes":1,"max active processes":1,"max children reached":0,"slow requests":0}
# We use basic regular expression here, i.e. we need to use \+ and not escape { and } # We use basic regular expression here, i.e. we need to use \+ and not escape { and }
if [[ ! -z `${S_ECHO} ${STATUS_JSON} | ${S_GREP} -G '^{.*\"pool\":\".\+\".*,\"process manager\":\".\+\".*}$'` ]]; then if [[ -n $(${S_ECHO} "${STATUS_JSON}" | ${S_GREP} -G '^{.*\"pool\":\".\+\".*,\"process manager\":\".\+\".*}$') ]]; then
PrintDebug "Status data for pool $POOL_NAME, socket $POOL_SOCKET, status path $STATUS_PATH is valid" PrintDebug "Status data for pool $POOL_NAME, socket $POOL_SOCKET, status path $STATUS_PATH is valid"
# Checking if we have ondemand pool # Checking if we have ondemand pool
if [[ ! -z `${S_ECHO} ${STATUS_JSON} | ${S_GREP} -F '"process manager":"ondemand"'` ]]; then if [[ -n $(${S_ECHO} "${STATUS_JSON}" | ${S_GREP} -F '"process manager":"ondemand"') ]]; then
PrintDebug "Detected pool's process manager is ondemand, it needs to be cached" PrintDebug "Detected pool's process manager is ondemand, it needs to be cached"
NEW_CACHE+=("$POOL_NAME $POOL_SOCKET") NEW_CACHE+=("$POOL_NAME $POOL_SOCKET")
return 1 return 1
@ -161,13 +162,13 @@ function ProcessPool(){
fi fi
PrintDebug "Failed to validate status data for pool $POOL_NAME, socket $POOL_SOCKET, status path $STATUS_PATH" PrintDebug "Failed to validate status data for pool $POOL_NAME, socket $POOL_SOCKET, status path $STATUS_PATH"
if [[ ! -z ${STATUS_JSON} ]]; then if [[ -n ${STATUS_JSON} ]]; then
PrintDebug "Status script returned: $STATUS_JSON" PrintDebug "Status script returned: $STATUS_JSON"
fi fi
return 0 return 0
fi fi
PrintDebug "Failed to get status for pool $POOL_NAME, socket $POOL_SOCKET, status path $STATUS_PATH" PrintDebug "Failed to get status for pool $POOL_NAME, socket $POOL_SOCKET, status path $STATUS_PATH"
if [[ ! -z ${STATUS_JSON} ]]; then if [[ -n ${STATUS_JSON} ]]; then
PrintDebug "Status script returned: $STATUS_JSON" PrintDebug "Status script returned: $STATUS_JSON"
fi fi
return 0 return 0
@ -187,7 +188,7 @@ done
PrintDebug "Current user is $ACTIVE_USER" PrintDebug "Current user is $ACTIVE_USER"
PrintDebug "Status path to be used: $STATUS_PATH" PrintDebug "Status path to be used: $STATUS_PATH"
LOCAL_DIR=`${S_DIRNAME} $0` LOCAL_DIR=$(${S_DIRNAME} "$0")
CACHE_FILE="$LOCAL_DIR/php_fpm.cache" CACHE_FILE="$LOCAL_DIR/php_fpm.cache"
STATUS_SCRIPT="$LOCAL_DIR/zabbix_php_fpm_status.sh" STATUS_SCRIPT="$LOCAL_DIR/zabbix_php_fpm_status.sh"
PrintDebug "Local directory is $LOCAL_DIR" PrintDebug "Local directory is $LOCAL_DIR"
@ -207,20 +208,21 @@ CACHE=()
NEW_CACHE=() NEW_CACHE=()
if [[ -r ${CACHE_FILE} ]]; then if [[ -r ${CACHE_FILE} ]]; then
PrintDebug "Reading cache file $CACHE_FILE..." PrintDebug "Reading cache file $CACHE_FILE..."
mapfile -t CACHE < <( ${S_CAT} ${CACHE_FILE} ) mapfile -t CACHE < <(${S_CAT} "${CACHE_FILE}")
else else
PrintDebug "Cache file $CACHE_FILE not found, skipping..." PrintDebug "Cache file $CACHE_FILE not found, skipping..."
fi 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")
POOL_LIST=`${S_PRINTF} '%s\n' "${PS_LIST[@]}" | $S_AWK '{print $NF}' | $S_SORT -u` # shellcheck disable=SC2016
POOL_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 while IFS= read -r line; do
do # 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=$(${S_PRINTF} '%s\n' "${PS_LIST[@]}" | $S_GREP -F -w "php-fpm: pool $line" | $S_HEAD -1 | $S_AWK '{print $1}')
if [[ ! -z $POOL_PID ]]; then if [[ -n $POOL_PID ]]; 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
@ -235,23 +237,24 @@ do
PrintDebug "Started analysis of pool $line, PID $POOL_PID" PrintDebug "Started analysis of pool $line, PID $POOL_PID"
#Extract only important information: #Extract only important information:
POOL_PARAMS_LIST=`$S_LSOF -p $POOL_PID 2>/dev/null | $S_GREP -w -e "unix" -e "TCP"` POOL_PARAMS_LIST=$($S_LSOF -p "$POOL_PID" 2>/dev/null | $S_GREP -w -e "unix" -e "TCP")
FOUND_POOL="" FOUND_POOL=""
while IFS= read -r pool while IFS= read -r pool; do
do if [[ -n $pool ]]; then
if [[ ! -z $pool ]]; then
if [[ -z $FOUND_POOL ]]; then if [[ -z $FOUND_POOL ]]; then
PrintDebug "Checking process: $pool" PrintDebug "Checking process: $pool"
POOL_TYPE=`${S_ECHO} "${pool}" | $S_AWK '{print $5}'` # shellcheck disable=SC2016
POOL_SOCKET=`${S_ECHO} "${pool}" | $S_AWK '{print $9}'` POOL_TYPE=$(${S_ECHO} "${pool}" | $S_AWK '{print $5}')
if [[ ! -z $POOL_TYPE ]] && [[ ! -z $POOL_SOCKET ]]; then # shellcheck disable=SC2016
POOL_SOCKET=$(${S_ECHO} "${pool}" | $S_AWK '{print $9}')
if [[ -n $POOL_TYPE ]] && [[ -n $POOL_SOCKET ]]; then
if [[ $POOL_TYPE == "unix" ]]; then if [[ $POOL_TYPE == "unix" ]]; then
#We have a socket here, test if it's actually a socket: #We have a socket here, test if it's actually a socket:
if [[ -S $POOL_SOCKET ]]; then if [[ -S $POOL_SOCKET ]]; then
PrintDebug "Found socket $POOL_SOCKET" PrintDebug "Found socket $POOL_SOCKET"
ProcessPool ${line} ${POOL_SOCKET} ProcessPool "${line}" "${POOL_SOCKET}"
POOL_STATUS=$? POOL_STATUS=$?
if [[ ${POOL_STATUS} > 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"
else else
@ -262,18 +265,19 @@ do
fi fi
elif [[ $POOL_TYPE == "IPv4" ]] || [[ $POOL_TYPE == "IPv6" ]]; then elif [[ $POOL_TYPE == "IPv4" ]] || [[ $POOL_TYPE == "IPv6" ]]; then
#We have a TCP connection here, check it: #We have a TCP connection here, check it:
CONNECTION_TYPE=`${S_ECHO} "${pool}" | $S_AWK '{print $8}'` # shellcheck disable=SC2016
CONNECTION_TYPE=$(${S_ECHO} "${pool}" | $S_AWK '{print $8}')
if [[ $CONNECTION_TYPE == "TCP" ]]; then if [[ $CONNECTION_TYPE == "TCP" ]]; then
#The connection must have state LISTEN: #The connection must have state LISTEN:
LISTEN=`${S_ECHO} ${pool} | $S_GREP -F -w "(LISTEN)"` LISTEN=$(${S_ECHO} "${pool}" | $S_GREP -F -w "(LISTEN)")
if [[ ! -z $LISTEN ]]; then if [[ -n $LISTEN ]]; then
#Check and replace * to localhost if it's found. Asterisk means that the PHP listens on #Check and replace * to localhost if it's found. Asterisk means that the PHP listens on
#all interfaces. #all interfaces.
POOL_SOCKET=`${S_ECHO} -n ${POOL_SOCKET/*:/localhost:}` POOL_SOCKET=$(${S_ECHO} -n "${POOL_SOCKET/*:/localhost:}")
PrintDebug "Found TCP connection $POOL_SOCKET" PrintDebug "Found TCP connection $POOL_SOCKET"
ProcessPool ${line} ${POOL_SOCKET} ProcessPool "${line}" "${POOL_SOCKET}"
POOL_STATUS=$? POOL_STATUS=$?
if [[ ${POOL_STATUS} > 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"
else else
@ -297,28 +301,29 @@ do
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 [[ ! -z ${FOUND_POOL} ]]; then if [[ -n ${FOUND_POOL} ]]; then
EncodeToJson ${line} ${POOL_SOCKET} EncodeToJson "${line}" "${POOL_SOCKET}"
else 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_LIST"
PrintDebug "Processing pools from old cache..." PrintDebug "Processing pools from old cache..."
for CACHE_ITEM in "${CACHE[@]}" for CACHE_ITEM in "${CACHE[@]}"; do
do # shellcheck disable=SC2016
ITEM_NAME=`${S_ECHO} "$CACHE_ITEM" | ${S_AWK} '{print $1}'` ITEM_NAME=$(${S_ECHO} "$CACHE_ITEM" | ${S_AWK} '{print $1}')
ITEM_SOCKET=`${S_ECHO} "$CACHE_ITEM" | ${S_AWK} '{print $2}'` # shellcheck disable=SC2016
ProcessPool ${ITEM_NAME} ${ITEM_SOCKET} ITEM_SOCKET=$(${S_ECHO} "$CACHE_ITEM" | ${S_AWK} '{print $2}')
ProcessPool "${ITEM_NAME}" "${ITEM_SOCKET}"
POOL_STATUS=$? POOL_STATUS=$?
if [[ ${POOL_STATUS} == "1" ]]; then if [[ ${POOL_STATUS} == "1" ]]; then
# This is a new pool and we must add it # This is a new pool and we must add it
EncodeToJson ${ITEM_NAME} ${ITEM_SOCKET} EncodeToJson "${ITEM_NAME}" "${ITEM_SOCKET}"
fi fi
done done
@ -328,8 +333,8 @@ if [[ -f ${CACHE_FILE} ]] && [[ ! -w ${CACHE_FILE} ]]; then
fi fi
PrintDebug "Saving new cache file $CACHE_FILE..." PrintDebug "Saving new cache file $CACHE_FILE..."
${S_PRINTF} "%s\n" "${NEW_CACHE[@]}" > ${CACHE_FILE} ${S_PRINTF} "%s\n" "${NEW_CACHE[@]}" >"${CACHE_FILE}"
RESULT_DATA="$RESULT_DATA]}" RESULT_DATA="$RESULT_DATA]}"
PrintDebug "Resulting JSON data for Zabbix:" PrintDebug "Resulting JSON data for Zabbix:"
${S_ECHO} -n $RESULT_DATA ${S_ECHO} -n "$RESULT_DATA"

View File

@ -3,9 +3,9 @@
#https://github.com/rvalitov/zabbix-php-fpm #https://github.com/rvalitov/zabbix-php-fpm
#Script gets status of PHP-FPM pool #Script gets status of PHP-FPM pool
S_FCGI=`type -P cgi-fcgi` S_FCGI=$(type -P cgi-fcgi)
S_GREP=`type -P grep` S_GREP=$(type -P grep)
S_ECHO=`type -P echo` S_ECHO=$(type -P echo)
if [[ ! -f $S_FCGI ]]; then if [[ ! -f $S_FCGI ]]; then
echo "Utility 'cgi-fcgi' not found. Please, install it first." echo "Utility 'cgi-fcgi' not found. Please, install it first."
@ -30,10 +30,12 @@ fi
POOL_URL=$1 POOL_URL=$1
POOL_PATH=$2 POOL_PATH=$2
#connecting to socket or address, https://easyengine.io/tutorials/php/directly-connect-php-fpm/ #connecting to socket or address, https://easyengine.io/tutorials/php/directly-connect-php-fpm/
PHP_STATUS=`SCRIPT_NAME=$POOL_PATH \ PHP_STATUS=$(
SCRIPT_FILENAME=$POOL_PATH \ SCRIPT_NAME=$POOL_PATH \
QUERY_STRING=json \ SCRIPT_FILENAME=$POOL_PATH \
REQUEST_METHOD=GET \ QUERY_STRING=json \
$S_FCGI -bind -connect $POOL_URL 2>/dev/null` REQUEST_METHOD=GET \
$S_FCGI -bind -connect "$POOL_URL" 2>/dev/null
)
$S_ECHO "$PHP_STATUS" | $S_GREP "{" $S_ECHO "$PHP_STATUS" | $S_GREP "{"
exit 0 exit 0