2019-08-12 14:22:49 +02:00
|
|
|
#!/bin/bash
|
|
|
|
#Ramil Valitov ramilvalitov@gmail.com
|
|
|
|
#https://github.com/rvalitov/zabbix-php-fpm
|
2019-12-19 12:02:16 +01:00
|
|
|
#This script scans local machine for active PHP-FPM pools and returns them as a list in JSON format
|
2019-08-12 14:22:49 +02:00
|
|
|
|
2019-11-04 22:45:20 +01:00
|
|
|
S_PS=`type -P ps`
|
|
|
|
S_GREP=`type -P grep`
|
|
|
|
S_AWK=`type -P awk`
|
|
|
|
S_SORT=`type -P sort`
|
|
|
|
S_HEAD=`type -P head`
|
|
|
|
S_LSOF=`type -P lsof`
|
|
|
|
S_JQ=`type -P jq`
|
2019-08-12 14:22:49 +02:00
|
|
|
|
|
|
|
if [[ ! -f $S_PS ]]; then
|
|
|
|
echo "Utility 'ps' not found. Please, install it first."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [[ ! -f $S_GREP ]]; then
|
|
|
|
echo "Utility 'grep' not found. Please, install it first."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [[ ! -f $S_AWK ]]; then
|
|
|
|
echo "Utility 'awk' not found. Please, install it first."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [[ ! -f $S_SORT ]]; then
|
|
|
|
echo "Utility 'sort' not found. Please, install it first."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [[ ! -f $S_HEAD ]]; then
|
|
|
|
echo "Utility 'head' not found. Please, install it first."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [[ ! -f $S_LSOF ]]; then
|
|
|
|
echo "Utility 'lsof' not found. Please, install it first."
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-08-13 10:32:59 +02:00
|
|
|
if [[ ! -f $S_JQ ]]; then
|
|
|
|
echo "Utility 'jq' not found. Please, install it first."
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-08-12 14:22:49 +02:00
|
|
|
|
2019-12-19 12:02:16 +01:00
|
|
|
DEBUG_MODE=""
|
|
|
|
if [[ ! -z $1 ]] && [[ $1 == "debug" ]]; then
|
|
|
|
DEBUG_MODE="1"
|
|
|
|
echo "Debug mode enabled"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Prints a string on screen. Works only if debug mode is enabled.
|
|
|
|
function PrintDebug(){
|
|
|
|
if [[ ! -z $DEBUG_MODE ]] && [[ ! -z $1 ]]; then
|
|
|
|
echo $1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
mapfile -t PS_LIST < <( $S_PS ax | $S_GREP -F "php-fpm: pool " | $S_GREP -F -v "grep" )
|
2019-08-12 14:22:49 +02:00
|
|
|
POOL_LIST=`printf '%s\n' "${PS_LIST[@]}" | $S_AWK '{print $NF}' | $S_SORT -u`
|
|
|
|
POOL_FIRST=0
|
2019-12-19 12:02:16 +01:00
|
|
|
#We store the resulting JSON data for Zabbix in the following var:
|
|
|
|
RESULT_DATA="{\"data\":["
|
2019-08-12 14:22:49 +02:00
|
|
|
while IFS= read -r line
|
|
|
|
do
|
2019-12-19 12:02:16 +01:00
|
|
|
POOL_PID=`printf '%s\n' "${PS_LIST[@]}" | $S_GREP -F -w "php-fpm: pool $line" | $S_HEAD -1 | $S_AWK '{print $1}'`
|
2019-08-12 14:22:49 +02:00
|
|
|
if [[ ! -z $POOL_PID ]]; then
|
2019-11-26 17:07:54 +01:00
|
|
|
#We search for socket or IP address and port
|
|
|
|
#Socket example:
|
|
|
|
#php-fpm7. 25897 root 9u unix 0x000000006509e31f 0t0 58381847 /run/php/php7.3-fpm.sock type=STREAM
|
|
|
|
#IP example:
|
2019-12-19 12:02:16 +01:00
|
|
|
#php-fpm7. 1110 default 0u IPv4 15760 0t0 TCP localhost:8002 (LISTEN)
|
|
|
|
|
|
|
|
#Check all matching processes, because we may face a redirect (or a symlink?), examples:
|
|
|
|
#php-fpm7. 1203 www-data 5u unix 0x000000006509e31f 0t0 15068771 type=STREAM
|
|
|
|
#php-fpm7. 6086 www-data 11u IPv6 21771 0t0 TCP *:9000 (LISTEN)
|
|
|
|
#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
|
|
|
|
|
|
|
|
PrintDebug "Started analysis of pool $line, PID $POOL_PID"
|
|
|
|
#Extract only important information:
|
|
|
|
POOL_PARAMS_LIST=`$S_LSOF -p $POOL_PID 2>/dev/null | $S_GREP -w -e "unix" -e "TCP"`
|
|
|
|
FOUND_POOL=""
|
|
|
|
while IFS= read -r pool
|
|
|
|
do
|
|
|
|
if [[ ! -z $pool ]]; then
|
|
|
|
if [[ -z $FOUND_POOL ]]; then
|
|
|
|
PrintDebug "Checking process: $pool"
|
|
|
|
POOL_TYPE=`echo "${pool}" | $S_AWK '{print $5}'`
|
|
|
|
POOL_SOCKET=`echo "${pool}" | $S_AWK '{print $9}'`
|
|
|
|
if [[ ! -z $POOL_TYPE ]] && [[ ! -z $POOL_SOCKET ]]; then
|
|
|
|
if [[ $POOL_TYPE == "unix" ]]; then
|
|
|
|
#We have a socket here, test if it's actually a socket:
|
|
|
|
if [[ -S $POOL_SOCKET ]]; then
|
|
|
|
FOUND_POOL="1"
|
|
|
|
PrintDebug "Success: found socket $POOL_SOCKET"
|
|
|
|
else
|
|
|
|
PrintDebug "Error: specified socket $POOL_SOCKET is not valid"
|
|
|
|
fi
|
|
|
|
elif [[ $POOL_TYPE == "IPv4" ]] || [[ $POOL_TYPE == "IPv6" ]]; then
|
|
|
|
#We have a TCP connection here, check it:
|
|
|
|
CONNECTION_TYPE=`echo "${pool}" | $S_AWK '{print $8}'`
|
|
|
|
if [[ $CONNECTION_TYPE == "TCP" ]]; then
|
|
|
|
#The connection must have state LISTEN:
|
|
|
|
LISTEN=`echo ${pool} | $S_GREP -F -w "(LISTEN)"`
|
|
|
|
if [[ ! -z $LISTEN ]]; then
|
|
|
|
#Check and replace * to localhost if it's found. Asterisk means that the PHP listens on
|
|
|
|
#all interfaces.
|
|
|
|
POOL_SOCKET=`echo -n ${POOL_SOCKET/*:/localhost:}`
|
|
|
|
FOUND_POOL="1"
|
|
|
|
PrintDebug "Success: found TCP connection $POOL_SOCKET"
|
|
|
|
else
|
|
|
|
PrintDebug "Warning: expected connection state must be LISTEN, but it was not detected"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
PrintDebug "Warning: expected connection type is TCP, but found $CONNECTION_TYPE"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
PrintDebug "Unsupported type $POOL_TYPE, skipping"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
PrintDebug "Warning: pool type or socket is empty"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
PrintDebug "Pool already found, skipping process: $pool"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
PrintDebug "Error: failed to get process information. Probably insufficient privileges. Use sudo or run this script under root."
|
|
|
|
fi
|
|
|
|
done <<< "$POOL_PARAMS_LIST"
|
|
|
|
|
|
|
|
if [[ ! -z $FOUND_POOL ]]; then
|
|
|
|
JSON_POOL=`echo -n "$line" | $S_JQ -aR .`
|
|
|
|
JSON_SOCKET=`echo -n "$POOL_SOCKET" | $S_JQ -aR .`
|
2019-10-01 11:02:49 +02:00
|
|
|
if [[ $POOL_FIRST == 1 ]]; then
|
2019-12-19 12:02:16 +01:00
|
|
|
RESULT_DATA="$RESULT_DATA,"
|
2019-10-01 11:02:49 +02:00
|
|
|
fi
|
2019-12-19 12:02:16 +01:00
|
|
|
RESULT_DATA="$RESULT_DATA{\"{#POOLNAME}\":$JSON_POOL,\"{#POOLSOCKET}\":$JSON_SOCKET}"
|
2019-08-13 10:32:59 +02:00
|
|
|
POOL_FIRST=1
|
2019-12-19 12:02:16 +01:00
|
|
|
else
|
|
|
|
PrintDebug "Error: failed to discover information for pool $line"
|
2019-08-12 14:22:49 +02:00
|
|
|
fi
|
2019-12-19 12:02:16 +01:00
|
|
|
else
|
|
|
|
PrintDebug "Error: failed to find PID for pool $line"
|
2019-08-12 14:22:49 +02:00
|
|
|
fi
|
|
|
|
done <<< "$POOL_LIST"
|
2019-12-19 12:02:16 +01:00
|
|
|
RESULT_DATA="$RESULT_DATA]}"
|
|
|
|
PrintDebug "Resulting JSON data for Zabbix:"
|
|
|
|
echo -n $RESULT_DATA
|