diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..94f480d --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index dc66c28..528f726 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,29 @@ language: bash jobs: include: + - os: linux + dist: bionic + name: "Missing packages test" + arch: amd64 + addons: + apt: + sources: + - sourceline: 'deb http://repo.zabbix.com/zabbix/4.0/ubuntu bionic main' + key_url: "https://repo.zabbix.com/zabbix-official-repo.key" + packages: + - bc + - curl + - grep + - sed + - gawk + - zabbix-agent + before_script: + - sudo curl -o /usr/local/bin/shunit2 https://raw.githubusercontent.com/kward/shunit2/master/shunit2 + - sudo apt-get -y remove jq + - sudo apt-get -y remove libfcgi-bin libfcgi0ldbl + - sudo apt autoremove + - jq --version + script: bash tests/missing.sh - os: linux dist: trusty name: "Zabbix 4.0 @ Ubuntu 14 trusty, PHP default" @@ -61,6 +84,7 @@ jobs: - sourceline: 'deb http://repo.zabbix.com/zabbix/4.0/ubuntu bionic main' key_url: "https://repo.zabbix.com/zabbix-official-repo.key" packages: + - bc - ca-certificates - curl - grep @@ -85,6 +109,7 @@ jobs: - sourceline: 'deb http://repo.zabbix.com/zabbix/4.4/ubuntu bionic main' key_url: "https://repo.zabbix.com/zabbix-official-repo.key" packages: + - bc - ca-certificates - curl - grep @@ -109,6 +134,7 @@ jobs: - sourceline: 'deb http://repo.zabbix.com/zabbix/5.0/ubuntu bionic main' key_url: "https://repo.zabbix.com/zabbix-official-repo.key" packages: + - bc - ca-certificates - curl - grep @@ -134,6 +160,7 @@ jobs: key_url: "https://repo.zabbix.com/zabbix-official-repo.key" - sourceline: 'ppa:ondrej/php' packages: + - bc - ca-certificates - curl - grep @@ -163,6 +190,7 @@ jobs: key_url: "https://repo.zabbix.com/zabbix-official-repo.key" - sourceline: 'ppa:ondrej/php' packages: + - bc - ca-certificates - curl - grep @@ -192,6 +220,7 @@ jobs: key_url: "https://repo.zabbix.com/zabbix-official-repo.key" - sourceline: 'ppa:ondrej/php' packages: + - bc - ca-certificates - curl - grep diff --git a/tests/all.sh b/tests/all.sh index 7b99cf0..1c1bf93 100644 --- a/tests/all.sh +++ b/tests/all.sh @@ -3,9 +3,144 @@ #https://github.com/rvalitov/zabbix-php-fpm #This script is used for testing +################################### START OF CONFIGURATION CONSTANTS + +# Number of pools, created for each ondemand, static and dynamic sockets. MAX_POOLS=3 + +# Number of port based pools created for each PHP version MAX_PORTS=3 -MIN_PORT=9000 + +# Starting port number for port based PHP pools +MIN_PORT=49001 + +# Maximum number of ports per PHP version, this value is used to define the available port range. +MAX_PORTS_COUNT=100 + +# Timeout in seconds that we put in the option "pm.process_idle_timeout" of configuration of ondemand PHP pools. +ONDEMAND_TIMEOUT=60 + +# Timeout in seconds that we put in the configuration of Zabbix agent +ZABBIX_TIMEOUT=20 + +# Maximum iterations to perform during sequential scans of pools, when the operation is time-consuming and requires +# multiple calls to the discovery script. +# This value should be big enough to be able to get information about all pools in the system. +# It allows to exit from indefinite check loops. +MAX_CHECKS=150 + +################################### END OF CONFIGURATION CONSTANTS + +# A random socket used for tests, this variable is defined when PHP pools are created +TEST_SOCKET="" + +# The directory where the PHP socket files are located, for example, /var/run or /run. +# This variable is used as cache, because it may be impossible to detect it when we start and stop the PHP-FPM. +# Don't use this variable directly. Use function getRunPHPDirectory +PHP_SOCKET_DIR="" + +# The directory where the PHP configuration files are located, for example, /etc/php or /etc/php5. +# This variable is used as cache. So, don't use this variable directly. Use function getEtcPHPDirectory +PHP_ETC_DIR="" + +# List of all services in the system. +# This variable is used as cache. So, don't use this variable directly. Use function getPHPServiceName +LIST_OF_SERVICES="" + +# Used for section folding in Travis CI +SECTION_UNIQUE_ID="" + +#Parent directory where all cache files are located in the OS +CACHE_ROOT="/var/cache" + +#Name of the private directory to store the cache files +CACHE_DIR_NAME="zabbix-php-fpm" + +#Full path to directory to store cache files +CACHE_DIRECTORY="$CACHE_ROOT/$CACHE_DIR_NAME" + +# ---------------------------------- +# Colors +# ---------------------------------- +NOCOLOR='\033[0m' +RED='\033[0;31m' +GREEN='\033[0;32m' +ORANGE='\033[0;33m' +BLUE='\033[0;34m' +PURPLE='\033[0;35m' +CYAN='\033[0;36m' +LIGHTGRAY='\033[0;37m' +DARKGRAY='\033[1;30m' +LIGHTRED='\033[1;31m' +LIGHTGREEN='\033[1;32m' +YELLOW='\033[1;33m' +LIGHTBLUE='\033[1;34m' +LIGHTPURPLE='\033[1;35m' +LIGHTCYAN='\033[1;36m' +WHITE='\033[1;37m' + +function printYellow() { + local info=$1 + echo -e "${YELLOW}$info${NOCOLOR}" +} + +function printRed() { + local info=$1 + echo -e "${RED}$info${NOCOLOR}" +} + +function printGreen() { + local info=$1 + echo -e "${LIGHTGREEN}$info${NOCOLOR}" +} + +function printSuccess() { + local name=$1 + printGreen "✓ OK: test '$name' passed" +} + +function printDebug() { + local info=$1 + echo -e "${DARKGRAY}$info${NOCOLOR}" +} + +function printAction() { + local info=$1 + echo -e "${LIGHTBLUE}$info${NOCOLOR}" +} + +function travis_fold_start() { + local name=$1 + local info=$2 + local CURRENT_TIMING + CURRENT_TIMING=$(date +%s%3N) + SECTION_UNIQUE_ID="$name.$CURRENT_TIMING" + echo -e "travis_fold:start:${SECTION_UNIQUE_ID}\033[33;1m${info}\033[0m" +} + +function travis_fold_end() { + echo -e "\ntravis_fold:end:${SECTION_UNIQUE_ID}\r" +} + +function getUserParameters() { + sudo find /etc/zabbix/ -name 'userparameter_php_fpm.conf' -type f 2>/dev/null | sort | head -n1 +} + +function restoreUserParameters() { + PARAMS_FILE=$(getUserParameters) + sudo rm -f "$PARAMS_FILE" + sudo cp "$TRAVIS_BUILD_DIR/zabbix/userparameter_php_fpm.conf" "$(sudo find /etc/zabbix/ -name 'zabbix_agentd*.d' -type d 2>/dev/null | sort | head -n1)" +} + +function AddSleepToConfig() { + PARAMS_FILE=$(getUserParameters) + sudo sed -i 's#.*zabbix_php_fpm_discovery.*#UserParameter=php-fpm.discover[*],sudo /etc/zabbix/zabbix_php_fpm_discovery.sh sleep $1#' "$PARAMS_FILE" + travis_fold_start "AddSleepToConfig" "ⓘ New UserParameter file" + sudo cat "$PARAMS_FILE" + travis_fold_end + restartService "zabbix-agent" + sleep 2 +} function getPHPVersion() { TEST_STRING=$1 @@ -14,36 +149,80 @@ function getPHPVersion() { PHP_VERSION=$(echo "$TEST_STRING" | grep -oP "php(\d)" | grep -oP "(\d)") fi echo "$PHP_VERSION" + if [[ -z "$PHP_VERSION" ]]; then + return 1 + fi + return 0 } function getEtcPHPDirectory() { + if [[ -n "$PHP_ETC_DIR" ]]; then + echo "$PHP_ETC_DIR" + return 0 + fi + LIST_OF_DIRS=( "/etc/php/" "/etc/php5/" ) for PHP_TEST_DIR in "${LIST_OF_DIRS[@]}"; do if [[ -d "$PHP_TEST_DIR" ]]; then - echo "$PHP_TEST_DIR" - return 0 + PHP_ETC_DIR=$PHP_TEST_DIR + break fi done + if [[ -n "$PHP_ETC_DIR" ]]; then + echo "$PHP_ETC_DIR" + return 0 + fi + return 1 } function getRunPHPDirectory() { + if [[ -n "$PHP_SOCKET_DIR" ]]; then + echo "$PHP_SOCKET_DIR" + return 0 + fi + LIST_OF_DIRS=( "/run/" "/var/run/" ) for PHP_TEST_DIR in "${LIST_OF_DIRS[@]}"; do - RESULT_DIR=$(find "$PHP_TEST_DIR" -name 'php*-fpm.sock' -type s -exec dirname {} \; 2>/dev/null | sort | head -n1) + RESULT_DIR=$(sudo find "$PHP_TEST_DIR" -name 'php*-fpm.sock' -type s -exec dirname {} \; 2>/dev/null | sort | head -n1) if [[ -d "$RESULT_DIR" ]]; then - echo "$RESULT_DIR" - return 0 + PHP_SOCKET_DIR="$RESULT_DIR/" + break fi done + if [[ -z "$PHP_SOCKET_DIR" ]]; then + #Try to parse the location from default config + PHP_DIR=$(getEtcPHPDirectory) + EXIT_CODE=$? + assertEquals "Failed to find PHP configuration directory" "0" "$EXIT_CODE" + assertTrue "PHP configuration directory '$PHP_DIR' is not a directory" "[ -d $PHP_DIR ]" + + DEFAULT_CONF=$(sudo find "$PHP_DIR" -name "www.conf" -type f | uniq | head -n1) + assertTrue "Failed to find default www.conf file inside '$PHP_DIR'" "[ -n $DEFAULT_CONF ]" + + DEFAULT_SOCKET=$(sudo grep -Po 'listen = (.+)' "$DEFAULT_CONF" | cut -d '=' -f2 | sed -e 's/^[ \t]*//') + assertTrue "Failed to extract socket information from '$DEFAULT_CONF'" "[ -n $DEFAULT_SOCKET ]" + + RESULT_DIR=$(dirname "$DEFAULT_SOCKET") + assertTrue "Directory '$RESULT_DIR' does not exist" "[ -d $RESULT_DIR ]" + if [[ -d "$RESULT_DIR" ]]; then + PHP_SOCKET_DIR="$RESULT_DIR/" + fi + fi + + if [[ -n "$PHP_SOCKET_DIR" ]]; then + echo "$PHP_SOCKET_DIR" + return 0 + fi + return 1 } @@ -54,6 +233,7 @@ copyPool() { POOL_TYPE=$4 POOL_DIR=$(dirname "${ORIGINAL_FILE}") PHP_VERSION=$(getPHPVersion "$POOL_DIR") + assertNotNull "Failed to detect PHP version from string '$POOL_DIR'" "$PHP_VERSION" NEW_POOL_FILE="$POOL_DIR/${POOL_NAME}.conf" sudo cp "$ORIGINAL_FILE" "$NEW_POOL_FILE" @@ -66,56 +246,143 @@ copyPool() { sudo sed -i "s#listen =.*#listen = $POOL_SOCKET#" "$NEW_POOL_FILE" #Pool name sudo sed -i "s#\[www\]#[$POOL_NAME]#" "$NEW_POOL_FILE" + + if [[ $POOL_TYPE == "ondemand" ]]; then + sudo sed -i "s#;pm.process_idle_timeout.*#pm.process_idle_timeout = ${ONDEMAND_TIMEOUT}s#" "$NEW_POOL_FILE" + fi +} + +getPHPServiceName() { + PHP_VERSION=$1 + if [[ -z "$LIST_OF_SERVICES" ]]; then + LIST_OF_SERVICES=$(sudo service --status-all 2>/dev/null | sort) + fi + + LIST_OF_NAMES=( + "php${PHP_VERSION}-fpm" + "php-fpm" + ) + + for SERVICE_NAME in "${LIST_OF_NAMES[@]}"; do + RESULT=$(echo "$LIST_OF_SERVICES" | grep -F "$SERVICE_NAME") + if [[ -n "$RESULT" ]]; then + echo "$SERVICE_NAME" + return 0 + fi + done + return 1 } setupPool() { POOL_FILE=$1 POOL_DIR=$(dirname "${POOL_FILE}") PHP_VERSION=$(getPHPVersion "$POOL_DIR") + assertNotNull "Failed to detect PHP version from string '$POOL_DIR'" "$PHP_VERSION" PHP_RUN_DIR=$(getRunPHPDirectory) EXIT_CODE=$? assertEquals "Failed to find PHP run directory" "0" "$EXIT_CODE" + assertTrue "PHP run directory '$PHP_RUN_DIR' is not a directory" "[ -d $PHP_RUN_DIR ]" + + PHP_DIR=$(getEtcPHPDirectory) + EXIT_CODE=$? + assertEquals "Failed to find PHP configuration directory" "0" "$EXIT_CODE" + assertTrue "PHP configuration directory '$PHP_DIR' is not a directory" "[ -d $PHP_DIR ]" #Delete all active pools except www.conf: - find "$POOL_DIR" -name '*.conf' -type f -not -name 'www.conf' -exec rm -rf {} \; - - #Add status path - sudo sed -i 's#;pm.status_path.*#pm.status_path = /php-fpm-status#' "$POOL_FILE" - #Set pool manager - sudo sed -i 's#pm = dynamic#pm = static#' "$POOL_FILE" + sudo find "$POOL_DIR" -name '*.conf' -type f -not -name 'www.conf' -exec rm -rf {} \; #Create new socket pools for ((c = 1; c <= MAX_POOLS; c++)); do POOL_NAME="socket$c" POOL_SOCKET="${PHP_RUN_DIR}php${PHP_VERSION}-fpm-${POOL_NAME}.sock" copyPool "$POOL_FILE" "$POOL_NAME" "$POOL_SOCKET" "static" + if [[ -z $TEST_SOCKET ]]; then + TEST_SOCKET="$POOL_SOCKET" + fi done + for ((c = 1; c <= MAX_POOLS; c++)); do + POOL_NAME="dynamic$c" + POOL_SOCKET="${PHP_RUN_DIR}php${PHP_VERSION}-fpm-${POOL_NAME}.sock" + copyPool "$POOL_FILE" "$POOL_NAME" "$POOL_SOCKET" "dynamic" + done + + for ((c = 1; c <= MAX_POOLS; c++)); do + POOL_NAME="ondemand$c" + POOL_SOCKET="${PHP_RUN_DIR}php${PHP_VERSION}-fpm-${POOL_NAME}.sock" + copyPool "$POOL_FILE" "$POOL_NAME" "$POOL_SOCKET" "ondemand" + done + + PHP_SERIAL_ID=$(sudo find "$PHP_DIR" -maxdepth 1 -mindepth 1 -type d | sort | grep -n -F "$PHP_VERSION" | head -n1 | cut -d : -f 1) #Create TCP port based pools #Division on 1 is required to convert from float to integer - START_PORT=$(echo "($MIN_PORT + $PHP_VERSION * 100 + 1)/1" | bc) + START_PORT=$(echo "($MIN_PORT + $PHP_SERIAL_ID * $MAX_PORTS_COUNT + 1)/1" | bc) for ((c = 1; c <= MAX_PORTS; c++)); do POOL_NAME="port$c" POOL_PORT=$(echo "($START_PORT + $c)/1" | bc) + PORT_IS_BUSY=$(sudo lsof -i:"$POOL_PORT") + assertNull "Port $POOL_PORT is busy" "$PORT_IS_BUSY" copyPool "$POOL_FILE" "$POOL_NAME" "$POOL_PORT" "static" done #Create TCP IPv4 localhost pool POOL_NAME="localhost" - POOL_PORT=$(echo "($MIN_PORT + $PHP_VERSION * 100)/1" | bc) + POOL_PORT=$(echo "($MIN_PORT + $PHP_SERIAL_ID * $MAX_PORTS_COUNT)/1" | bc) POOL_SOCKET="127.0.0.1:$POOL_PORT" + PORT_IS_BUSY=$(sudo lsof -i:"$POOL_PORT") + assertNull "Port $POOL_PORT is busy" "$PORT_IS_BUSY" copyPool "$POOL_FILE" "$POOL_NAME" "$POOL_SOCKET" "static" - sudo service "php${PHP_VERSION}-fpm" restart + travis_fold_start "list_PHP$PHP_VERSION" "ⓘ List of configured PHP$PHP_VERSION pools" + sudo ls -l "$POOL_DIR" + travis_fold_end + + SERVICE_NAME=$(getPHPServiceName "$PHP_VERSION") + assertNotNull "Failed to detect service name for PHP${PHP_VERSION}" "$SERVICE_NAME" + printAction "Restarting service $SERVICE_NAME..." + restartService "$SERVICE_NAME" + sleep 3 + + travis_fold_start "running_PHP$PHP_VERSION" "ⓘ List of running PHP$PHP_VERSION pools" + E_SYSTEM_CONTROL=$(type -P systemctl) + if [[ -x "$E_SYSTEM_CONTROL" ]]; then + sudo systemctl -l status "$SERVICE_NAME.service" + else + sudo initctl list | grep -F "$SERVICE_NAME" + fi + travis_fold_end + sleep 2 } setupPools() { PHP_DIR=$(getEtcPHPDirectory) EXIT_CODE=$? - assertEquals "Failed to find PHP directory" "0" "$EXIT_CODE" + assertEquals "Failed to find PHP configuration directory" "0" "$EXIT_CODE" + assertTrue "PHP configuration directory '$PHP_DIR' is not a directory" "[ -d $PHP_DIR ]" - PHP_LIST=$(find "$PHP_DIR" -name 'www.conf' -type f) + PHP_LIST=$(sudo find "$PHP_DIR" -name 'www.conf' -type f) + + #Call to detect and cache PHP run directory, we need to call it before we stop all PHP-FPM + PHP_RUN_DIR=$(getRunPHPDirectory) + EXIT_CODE=$? + assertEquals "Failed to find PHP run directory" "0" "$EXIT_CODE" + assertTrue "PHP run directory '$PHP_RUN_DIR' is not a directory" "[ -d $PHP_RUN_DIR ]" + + #First we need to stop all PHP-FPM + while IFS= read -r pool; do + if [[ -n $pool ]]; then + POOL_DIR=$(dirname "$pool") + PHP_VERSION=$(getPHPVersion "$POOL_DIR") + assertNotNull "Failed to detect PHP version from string '$POOL_DIR'" "$PHP_VERSION" + SERVICE_NAME=$(getPHPServiceName "$PHP_VERSION") + assertNotNull "Failed to detect service name for PHP${PHP_VERSION}" "$SERVICE_NAME" + printAction "Stopping service $SERVICE_NAME..." + stopService "$SERVICE_NAME" + fi + done <<<"$PHP_LIST" + + #Now we reconfigure them and restart while IFS= read -r pool; do if [[ -n $pool ]]; then setupPool "$pool" @@ -126,27 +393,68 @@ setupPools() { getNumberOfPHPVersions() { PHP_DIR=$(getEtcPHPDirectory) EXIT_CODE=$? - assertEquals "Failed to find PHP directory" "0" "$EXIT_CODE" + assertEquals "Failed to find PHP configuration directory" "0" "$EXIT_CODE" + assertTrue "PHP configuration directory '$PHP_DIR' is not a directory" "[ -d $PHP_DIR ]" - PHP_COUNT=$(find "$PHP_DIR" -name 'www.conf' -type f | wc -l) + PHP_COUNT=$(sudo find "$PHP_DIR" -name 'www.conf' -type f | wc -l) echo "$PHP_COUNT" } +function startOndemandPoolsCache() { + PHP_DIR=$(getEtcPHPDirectory) + EXIT_CODE=$? + assertEquals "Failed to find PHP configuration directory" "0" "$EXIT_CODE" + assertTrue "PHP configuration directory '$PHP_DIR' is not a directory" "[ -d $PHP_DIR ]" + + PHP_RUN_DIR=$(getRunPHPDirectory) + EXIT_CODE=$? + assertEquals "Failed to find PHP run directory" "0" "$EXIT_CODE" + assertTrue "PHP run directory '$PHP_RUN_DIR' is not a directory" "[ -d $PHP_RUN_DIR ]" + + # We must start all the pools + POOL_URL="/php-fpm-status" + + PHP_LIST=$(sudo find "$PHP_DIR" -name 'www.conf' -type f) + while IFS= read -r pool; do + if [[ -n $pool ]]; then + POOL_DIR=$(dirname "$pool") + PHP_VERSION=$(getPHPVersion "$POOL_DIR") + assertNotNull "Failed to detect PHP version from string '$POOL_DIR'" "$PHP_VERSION" + + for ((c = 1; c <= MAX_POOLS; c++)); do + POOL_NAME="ondemand$c" + POOL_SOCKET="${PHP_RUN_DIR}php${PHP_VERSION}-fpm-${POOL_NAME}.sock" + + PHP_STATUS=$( + SCRIPT_NAME=$POOL_URL \ + SCRIPT_FILENAME=$POOL_URL \ + QUERY_STRING=json \ + REQUEST_METHOD=GET \ + sudo cgi-fcgi -bind -connect "$POOL_SOCKET" 2>/dev/null + ) + assertNotNull "Failed to connect to $POOL_SOCKET" "$PHP_STATUS" + done + fi + done <<<"$PHP_LIST" +} + getAnySocket() { PHP_DIR=$(getEtcPHPDirectory) EXIT_CODE=$? - assertEquals "Failed to find PHP directory" "0" "$EXIT_CODE" + assertEquals "Failed to find PHP configuration directory" "0" "$EXIT_CODE" + assertTrue "PHP configuration directory '$PHP_DIR' is not a directory" "[ -d $PHP_DIR ]" PHP_RUN_DIR=$(getRunPHPDirectory) EXIT_CODE=$? assertEquals "Failed to find PHP run directory" "0" "$EXIT_CODE" + assertTrue "PHP run directory '$PHP_RUN_DIR' is not a directory" "[ -d $PHP_RUN_DIR ]" #Get any socket of PHP-FPM: - PHP_FIRST=$(find "$PHP_DIR" -name 'www.conf' -type f | sort | head -n1) + PHP_FIRST=$(sudo find "$PHP_DIR" -name 'www.conf' -type f | sort | head -n1) assertNotNull "Failed to get PHP conf" "$PHP_FIRST" PHP_VERSION=$(getPHPVersion "$PHP_FIRST") - assertNotNull "Failed to get PHP version for $PHP_FIRST" "$PHP_VERSION" - PHP_POOL=$(find "$PHP_RUN_DIR" -name "php${PHP_VERSION}*.sock" -type s 2>/dev/null | sort | head -n1) + assertNotNull "Failed to detect PHP version from string '$PHP_FIRST'" "$PHP_VERSION" + PHP_POOL=$(sudo find "$PHP_RUN_DIR" -name "php${PHP_VERSION}*.sock" -type s 2>/dev/null | sort | head -n1) assertNotNull "Failed to get PHP${PHP_VERSION} socket" "$PHP_POOL" echo "$PHP_POOL" } @@ -157,35 +465,85 @@ getAnyPort() { echo "$PHP_PORT" } +function actionService() { + local SERVICE_NAME=$1 + local SERVICE_ACTION=$2 + local SERVICE_INFO + sleep 3 + SERVICE_INFO=$(sudo service "$SERVICE_NAME" $SERVICE_ACTION) + STATUS=$? + if [[ "$STATUS" -ne 0 ]]; then + printRed "Failed to $SERVICE_ACTION service '$SERVICE_NAME':" + echo "$SERVICE_INFO" + fi + sleep 3 +} + +function restartService() { + local SERVICE_NAME=$1 + actionService "$SERVICE_NAME" "restart" +} + +function stopService() { + local SERVICE_NAME=$1 + actionService "$SERVICE_NAME" "stop" +} + oneTimeSetUp() { - echo "Started job $TRAVIS_JOB_NAME" - echo "Host info:" + printAction "Started job $TRAVIS_JOB_NAME" + + travis_fold_start "host_info" "ⓘ Host information" nslookup localhost sudo ifconfig sudo cat /etc/hosts - echo "Copying Zabbix files..." + travis_fold_end + + printAction "Copying Zabbix files..." #Install files: sudo cp "$TRAVIS_BUILD_DIR/zabbix/zabbix_php_fpm_discovery.sh" "/etc/zabbix" sudo cp "$TRAVIS_BUILD_DIR/zabbix/zabbix_php_fpm_status.sh" "/etc/zabbix" - sudo cp "$TRAVIS_BUILD_DIR/zabbix/userparameter_php_fpm.conf" "$(find /etc/zabbix/ -name 'zabbix_agentd*.d' -type d | sort | head -n1)" + sudo cp "$TRAVIS_BUILD_DIR/zabbix/userparameter_php_fpm.conf" "$(sudo find /etc/zabbix/ -name 'zabbix_agentd*.d' -type d | sort | head -n1)" sudo chmod +x /etc/zabbix/zabbix_php_fpm_discovery.sh sudo chmod +x /etc/zabbix/zabbix_php_fpm_status.sh #Configure Zabbix: echo 'zabbix ALL=NOPASSWD: /etc/zabbix/zabbix_php_fpm_discovery.sh,/etc/zabbix/zabbix_php_fpm_status.sh' | sudo EDITOR='tee -a' visudo - sudo service zabbix-agent restart + sudo sed -i "s#.* Timeout=.*#Timeout = $ZABBIX_TIMEOUT#" "/etc/zabbix/zabbix_agentd.conf" - echo "Setup PHP-FPM..." + travis_fold_start "zabbix_agent" "ⓘ Zabbix agent configuration" + sudo cat "/etc/zabbix/zabbix_agentd.conf" + travis_fold_end + + restartService "zabbix-agent" + + printAction "Setup PHP-FPM..." #Setup PHP-FPM pools: setupPools - echo "All done, starting tests..." + printAction "All done, starting tests..." +} + +#Called before every test +setUp() { + #Delete all cache files + if [[ -d "$CACHE_DIRECTORY" ]]; then + sudo find "$CACHE_DIRECTORY" -type f -exec rm '{}' \; + fi +} + +#Called after every test +tearDown() { + restoreUserParameters + sleep 2 + restartService "zabbix-agent" + sleep 2 } testZabbixGetInstalled() { ZABBIX_GET=$(type -P zabbix_get) assertNotNull "Utility zabbix-get not installed" "$ZABBIX_GET" + printSuccess "${FUNCNAME[0]}" } testZabbixAgentVersion() { @@ -193,6 +551,7 @@ testZabbixAgentVersion() { REQUESTED_VERSION=$(echo "$TRAVIS_JOB_NAME" | grep -i -F "zabbix" | head -n1 | cut -d "@" -f1 | cut -d " " -f2) INSTALLED_VERSION=$(zabbix_agentd -V | grep -F "zabbix" | head -n1 | rev | cut -d " " -f1 | rev | cut -d "." -f1,2) assertSame "Requested version $REQUESTED_VERSION and installed version $INSTALLED_VERSION of Zabbix agent do not match" "$REQUESTED_VERSION" "$INSTALLED_VERSION" + printSuccess "${FUNCNAME[0]}" } testZabbixGetVersion() { @@ -200,21 +559,22 @@ testZabbixGetVersion() { REQUESTED_VERSION=$(echo "$TRAVIS_JOB_NAME" | grep -i -F "zabbix" | head -n1 | cut -d "@" -f1 | cut -d " " -f2) INSTALLED_VERSION=$(zabbix_get -V | grep -F "zabbix" | head -n1 | rev | cut -d " " -f1 | rev | cut -d "." -f1,2) assertSame "Requested version $REQUESTED_VERSION and installed version $INSTALLED_VERSION of zabbix_get do not match" "$REQUESTED_VERSION" "$INSTALLED_VERSION" + printSuccess "${FUNCNAME[0]}" } testPHPIsRunning() { IS_OK=$(sudo ps ax | grep -F "php-fpm: pool " | grep -F -v "grep" | head -n1) assertNotNull "No running PHP-FPM instances found" "$IS_OK" + printSuccess "${FUNCNAME[0]}" } testStatusScriptSocket() { - PHP_POOL=$(getAnySocket) - - #Make the test: - DATA=$(sudo bash "/etc/zabbix/zabbix_php_fpm_status.sh" "$PHP_POOL" "/php-fpm-status") + assertNotNull "Test socket is not defined" "$TEST_SOCKET" + DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_status.sh" "$TEST_SOCKET" "/php-fpm-status") IS_OK=$(echo "$DATA" | grep -F '{"pool":"') - assertNotNull "Failed to get status from pool $PHP_POOL: $DATA" "$IS_OK" - echo "Success test of $PHP_POOL" + assertNotNull "Failed to get status from pool $TEST_SOCKET: $DATA" "$IS_OK" + printGreen "Success test of $TEST_SOCKET" + printSuccess "${FUNCNAME[0]}" } testStatusScriptPort() { @@ -222,19 +582,19 @@ testStatusScriptPort() { PHP_POOL="127.0.0.1:$PHP_PORT" #Make the test: - DATA=$(sudo bash "/etc/zabbix/zabbix_php_fpm_status.sh" "$PHP_POOL" "/php-fpm-status") + DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_status.sh" "$PHP_POOL" "/php-fpm-status") IS_OK=$(echo "$DATA" | grep -F '{"pool":"') assertNotNull "Failed to get status from pool $PHP_POOL: $DATA" "$IS_OK" - echo "Success test of $PHP_POOL" + printGreen "Success test of $PHP_POOL" + printSuccess "${FUNCNAME[0]}" } testZabbixStatusSocket() { - PHP_POOL=$(getAnySocket) - - DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.status["$PHP_POOL","/php-fpm-status"]) + DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.status["$TEST_SOCKET","/php-fpm-status"]) IS_OK=$(echo "$DATA" | grep -F '{"pool":"') assertNotNull "Failed to get status from pool $PHP_POOL: $DATA" "$IS_OK" - echo "Success test of $PHP_POOL" + printGreen "Success test of $PHP_POOL" + printSuccess "${FUNCNAME[0]}" } testZabbixStatusPort() { @@ -244,46 +604,280 @@ testZabbixStatusPort() { DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.status["$PHP_POOL","/php-fpm-status"]) IS_OK=$(echo "$DATA" | grep -F '{"pool":"') assertNotNull "Failed to get status from pool $PHP_POOL: $DATA" "$IS_OK" - echo "Success test of $PHP_POOL" + printGreen "Success test of $PHP_POOL" + printSuccess "${FUNCNAME[0]}" } testDiscoverScriptReturnsData() { - DATA=$(sudo bash "/etc/zabbix/zabbix_php_fpm_discovery.sh" "/php-fpm-status") + DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "/php-fpm-status") IS_OK=$(echo "$DATA" | grep -F '{"data":[{"{#POOLNAME}"') assertNotNull "Discover script failed: $DATA" "$IS_OK" + printSuccess "${FUNCNAME[0]}" } testDiscoverScriptDebug() { - DATA=$(sudo bash "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "/php-fpm-status") - ERRORS_LIST=$(echo "$DATA" | grep -F 'Error:') - assertNull "Discover script errors: $DATA" "$ERRORS_LIST" + DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "nosleep" "/php-fpm-status") + NUMBER_OF_ERRORS=$(echo "$DATA" | grep -o -F 'Error:' | wc -l) + PHP_COUNT=$(getNumberOfPHPVersions) + if [[ $PHP_COUNT != "$NUMBER_OF_ERRORS" ]]; then + ERRORS_LIST=$(echo "$DATA" | grep -F 'Error:') + printYellow "Errors list:" + printYellow "$ERRORS_LIST" + travis_fold_start "testDiscoverScriptDebug_full" "ⓘ Full output" + echo "$DATA" + travis_fold_end + fi + assertEquals "Discover script errors mismatch" "$PHP_COUNT" "$NUMBER_OF_ERRORS" + printSuccess "${FUNCNAME[0]}" } testZabbixDiscoverReturnsData() { DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"]) IS_OK=$(echo "$DATA" | grep -F '{"data":[{"{#POOLNAME}"') assertNotNull "Discover script failed: $DATA" "$IS_OK" + printSuccess "${FUNCNAME[0]}" +} + +testDiscoverScriptSleep() { + DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "sleep" "/php-fpm-status") + CHECK_OK_COUNT=$(echo "$DATA" | grep -o -F "execution time OK" | wc -l) + STOP_OK_COUNT=$(echo "$DATA" | grep -o -F "stop required" | wc -l) + + printYellow "Success time checks: $CHECK_OK_COUNT" + printYellow "Stop time checks: $STOP_OK_COUNT" + + if [[ $CHECK_OK_COUNT -lt 1 ]] || [[ $STOP_OK_COUNT -lt 1 ]]; then + travis_fold_start "ScriptSleep" "ⓘ Zabbix response" + echo "$DATA" + travis_fold_end + fi + assertTrue "No success time checks detected" "[ $CHECK_OK_COUNT -gt 0 ] || [ $STOP_OK_COUNT -eq 1 ]" + assertTrue "No success stop checks detected" "[ $STOP_OK_COUNT -gt 0 ]" + printSuccess "${FUNCNAME[0]}" +} + +testZabbixDiscoverSleep() { + #Add sleep + AddSleepToConfig + + testZabbixDiscoverReturnsData + printSuccess "${FUNCNAME[0]}" +} + +testDiscoverScriptRunDuration() { + START_TIME=$(date +%s%N) + DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "sleep" "/php-fpm-status") + END_TIME=$(date +%s%N) + ELAPSED_TIME=$(echo "($END_TIME - $START_TIME)/1000000" | bc) + CHECK_OK_COUNT=$(echo "$DATA" | grep -o -F "execution time OK" | wc -l) + STOP_OK_COUNT=$(echo "$DATA" | grep -o -F "stop required" | wc -l) + MAX_TIME=$(echo "$ZABBIX_TIMEOUT * 1000" | bc) + + printYellow "Elapsed time $ELAPSED_TIME ms" + printYellow "Success time checks: $CHECK_OK_COUNT" + printYellow "Stop time checks: $STOP_OK_COUNT" + + assertTrue "The script worked for too long" "[ $ELAPSED_TIME -lt $MAX_TIME ]" + printSuccess "${FUNCNAME[0]}" +} + +testZabbixDiscoverRunDuration() { + #Add sleep + AddSleepToConfig + + START_TIME=$(date +%s%N) + DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"]) + END_TIME=$(date +%s%N) + ELAPSED_TIME=$(echo "($END_TIME - $START_TIME)/1000000" | bc) + MAX_TIME=$(echo "$ZABBIX_TIMEOUT * 1000" | bc) + + printYellow "Elapsed time $ELAPSED_TIME ms" + + assertTrue "The script worked for too long" "[ $ELAPSED_TIME -lt $MAX_TIME ]" + printSuccess "${FUNCNAME[0]}" +} + +testDiscoverScriptDoubleRun() { + DATA_FIRST=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "sleep" "/php-fpm-status") + DATA_SECOND=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "sleep" "/php-fpm-status") + + assertNotEquals "Multiple discovery routines provide the same results: $DATA_FIRST" "$DATA_FIRST" "$DATA_SECOND" + printSuccess "${FUNCNAME[0]}" +} + +testZabbixDiscoverDoubleRun() { + #Add sleep + AddSleepToConfig + + DATA_FIRST=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"]) + DATA_SECOND=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"]) + + assertNotEquals "Multiple discovery routines provide the same results: $DATA_FIRST" "$DATA_FIRST" "$DATA_SECOND" + printSuccess "${FUNCNAME[0]}" +} + +function discoverAllZabbix() { + DATA_OLD=$1 + DATA_COUNT=$2 + + if [[ -z $DATA_COUNT ]]; then + DATA_COUNT=0 + fi + + DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"]) + if [[ -n "$DATA" ]] && [[ -n "$DATA_OLD" ]] && [[ "$DATA_OLD" == "$DATA" ]]; then + echo "$DATA" + return 0 + else + DATA_COUNT=$(echo "$DATA_COUNT + 1" | bc) + if [[ $DATA_COUNT -gt $MAX_CHECKS ]]; then + printYellow "Data old:" + printDebug "$DATA_OLD" + printYellow "Data new:" + printDebug "$DATA" + return 1 + fi + discoverAllZabbix "$DATA" "$DATA_COUNT" + STATUS=$? + return $STATUS + fi +} + +checkNumberOfPools() { + POOL_TYPE=$1 + CHECK_COUNT=$2 + + DATA=$(discoverAllZabbix) + STATUS=$? + if [[ $STATUS -ne 0 ]]; then + echo "$DATA" + return 1 + fi + assertEquals "Failed to discover all data when checking pools '$POOL_TYPE'" "0" "$STATUS" + + NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F "{\"{#POOLNAME}\":\"$POOL_TYPE" | wc -l) + PHP_COUNT=$(getNumberOfPHPVersions) + if [[ -n "$CHECK_COUNT" ]] && [[ "$CHECK_COUNT" -ge 0 ]]; then + POOLS_BY_DESIGN="$CHECK_COUNT" + else + POOLS_BY_DESIGN=$(echo "$PHP_COUNT * $MAX_POOLS" | bc) + fi + assertEquals "Number of '$POOL_TYPE' pools mismatch" "$POOLS_BY_DESIGN" "$NUMBER_OF_POOLS" + echo "$DATA" + return 0 } testZabbixDiscoverNumberOfSocketPools() { - DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"]) - NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"socket1",' | wc -l) - PHP_COUNT=$(getNumberOfPHPVersions) - assertEquals "Number of pools mismatch" "$PHP_COUNT" "$NUMBER_OF_POOLS" + local DATA + DATA=$(checkNumberOfPools "socket") + travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" + echo "$DATA" + travis_fold_end + printSuccess "${FUNCNAME[0]}" +} + +testZabbixDiscoverNumberOfDynamicPools() { + local DATA + DATA=$(checkNumberOfPools "dynamic") + travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" + echo "$DATA" + travis_fold_end + printSuccess "${FUNCNAME[0]}" +} + +testZabbixDiscoverNumberOfOndemandPoolsCold() { + local DATA + #If the pools are not started then we have 0 here: + DATA=$(checkNumberOfPools "ondemand" 0) + travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" + echo "$DATA" + travis_fold_end + printSuccess "${FUNCNAME[0]}" +} + +testZabbixDiscoverNumberOfOndemandPoolsHot() { + startOndemandPoolsCache + local DATA + DATA=$(checkNumberOfPools "ondemand") + travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" + echo "$DATA" + travis_fold_end + printSuccess "${FUNCNAME[0]}" +} + +testZabbixDiscoverNumberOfOndemandPoolsCache() { + startOndemandPoolsCache + + printAction "Empty cache test..." + INITIAL_DATA=$(checkNumberOfPools "ondemand") + travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" + echo "$INITIAL_DATA" + travis_fold_end + + WAIT_TIMEOUT=$(echo "$ONDEMAND_TIMEOUT * 2" | bc) + sleep "$WAIT_TIMEOUT" + + printAction "Full cache test..." + CACHED_DATA=$(checkNumberOfPools "ondemand") + travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" + echo "$CACHED_DATA" + travis_fold_end + + assertEquals "Data mismatch" "$INITIAL_DATA" "$CACHED_DATA" + printSuccess "${FUNCNAME[0]}" } testZabbixDiscoverNumberOfIPPools() { - DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"]) - NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"localhost",' | wc -l) PHP_COUNT=$(getNumberOfPHPVersions) - assertEquals "Number of pools mismatch" "$PHP_COUNT" "$NUMBER_OF_POOLS" + local DATA + DATA=$(checkNumberOfPools "localhost" "$PHP_COUNT") + travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" + echo "$DATA" + travis_fold_end + printSuccess "${FUNCNAME[0]}" } testZabbixDiscoverNumberOfPortPools() { - DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"]) - NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"port1",' | wc -l) - PHP_COUNT=$(getNumberOfPHPVersions) - assertEquals "Number of pools mismatch" "$PHP_COUNT" "$NUMBER_OF_POOLS" + local DATA + DATA=$(checkNumberOfPools "port") + travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" + echo "$DATA" + travis_fold_end + printSuccess "${FUNCNAME[0]}" +} + +#This test should be last in Zabbix tests +testDiscoverScriptManyPools() { + #Create lots of pools + MAX_POOLS=20 + MAX_PORTS=20 + setupPools + + testDiscoverScriptReturnsData + printSuccess "${FUNCNAME[0]}" +} + +testZabbixDiscoverManyPools() { + testZabbixDiscoverReturnsData + printSuccess "${FUNCNAME[0]}" +} + +testDiscoverScriptManyPoolsRunDuration() { + MAX_RUNS=5 + for ((c = 1; c <= MAX_RUNS; c++)); do + printAction "Run #$c..." + testDiscoverScriptRunDuration + done + printSuccess "${FUNCNAME[0]}" +} + +testZabbixDiscoverManyPoolsRunDuration() { + MAX_RUNS=5 + for ((c = 1; c <= MAX_RUNS; c++)); do + printAction "Run #$c..." + testZabbixDiscoverRunDuration + done + printSuccess "${FUNCNAME[0]}" } # Load shUnit2. diff --git a/tests/missing.sh b/tests/missing.sh new file mode 100644 index 0000000..0756ef3 --- /dev/null +++ b/tests/missing.sh @@ -0,0 +1,107 @@ +#!/bin/bash +#Ramil Valitov ramilvalitov@gmail.com +#https://github.com/rvalitov/zabbix-php-fpm +#This script is used for testing + +# Used for section folding in Travis CI +SECTION_UNIQUE_ID="" + +# ---------------------------------- +# Colors +# ---------------------------------- +NOCOLOR='\033[0m' +RED='\033[0;31m' +GREEN='\033[0;32m' +ORANGE='\033[0;33m' +BLUE='\033[0;34m' +PURPLE='\033[0;35m' +CYAN='\033[0;36m' +LIGHTGRAY='\033[0;37m' +DARKGRAY='\033[1;30m' +LIGHTRED='\033[1;31m' +LIGHTGREEN='\033[1;32m' +YELLOW='\033[1;33m' +LIGHTBLUE='\033[1;34m' +LIGHTPURPLE='\033[1;35m' +LIGHTCYAN='\033[1;36m' +WHITE='\033[1;37m' + +function printYellow() { + local info=$1 + echo -e "${YELLOW}$info${NOCOLOR}" +} + +function printRed() { + local info=$1 + echo -e "${RED}$info${NOCOLOR}" +} + +function printGreen() { + local info=$1 + echo -e "${LIGHTGREEN}$info${NOCOLOR}" +} + +function printSuccess() { + local name=$1 + printGreen "✓ OK: test '$name' passed" +} + +function printDebug() { + local info=$1 + echo -e "${DARKGRAY}$info${NOCOLOR}" +} + +function printAction() { + local info=$1 + echo -e "${LIGHTBLUE}$info${NOCOLOR}" +} + +function travis_fold_start() { + local name=$1 + local info=$2 + local CURRENT_TIMING + CURRENT_TIMING=$(date +%s%3N) + SECTION_UNIQUE_ID="$name.$CURRENT_TIMING" + echo -e "travis_fold:start:${SECTION_UNIQUE_ID}\033[33;1m${info}\033[0m" +} + +function travis_fold_end() { + echo -e "\ntravis_fold:end:${SECTION_UNIQUE_ID}\r" +} + +oneTimeSetUp() { + printAction "Started job $TRAVIS_JOB_NAME" + + travis_fold_start "host_info" "ⓘ Host information" + nslookup localhost + sudo ifconfig + sudo cat /etc/hosts + travis_fold_end + + printAction "Copying Zabbix files..." + #Install files: + sudo cp "$TRAVIS_BUILD_DIR/zabbix/zabbix_php_fpm_discovery.sh" "/etc/zabbix" + sudo cp "$TRAVIS_BUILD_DIR/zabbix/zabbix_php_fpm_status.sh" "/etc/zabbix" + sudo cp "$TRAVIS_BUILD_DIR/zabbix/userparameter_php_fpm.conf" "$(find /etc/zabbix/ -name 'zabbix_agentd*.d' -type d | head -n1)" + sudo chmod +x /etc/zabbix/zabbix_php_fpm_discovery.sh + sudo chmod +x /etc/zabbix/zabbix_php_fpm_status.sh + + printAction "All done, starting tests..." +} + +testMissingPackagesDiscoveryScript() { + DATA=$(sudo bash "/etc/zabbix/zabbix_php_fpm_discovery.sh" "/php-fpm-status") + IS_OK=$(echo "$DATA" | grep -F ' not found.') + assertNotNull "Discovery script didn't report error on missing utilities $DATA" "$IS_OK" + printSuccess "${FUNCNAME[0]}" +} + +testMissingPackagesStatusScript() { + DATA=$(sudo bash "/etc/zabbix/zabbix_php_fpm_status.sh" "localhost:9000" "/php-fpm-status") + IS_OK=$(echo "$DATA" | grep -F ' not found.') + assertNotNull "Status script didn't report error on missing utilities $DATA" "$IS_OK" + printSuccess "${FUNCNAME[0]}" +} + +# Load shUnit2. +. shunit2 diff --git a/zabbix/zabbix_php_fpm_discovery.sh b/zabbix/zabbix_php_fpm_discovery.sh index d975fb8..476fe15 100644 --- a/zabbix/zabbix_php_fpm_discovery.sh +++ b/zabbix/zabbix_php_fpm_discovery.sh @@ -3,6 +3,38 @@ #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 parameter is used to limit the execution time of this script. +# Zabbix allows us to use a script that runs no more than 3 seconds by default. This option can be adjusted in settings: +# see Timeout option https://www.zabbix.com/forum/zabbix-help/1284-server-agentd-timeout-parameter-in-config +# So, we need to stop and save our state in case we need more time to run. +# This parameter sets the maximum number of milliseconds that the script is allowed to run. +# After this duration is reached, the script will stop running and save its state. +# So, the actual execution time will be slightly more than this parameter. +# We put value equivalent to 1.5 seconds here. +MAX_EXECUTION_TIME="1500" + +#Status path used in calls to PHP-FPM +STATUS_PATH="/php-fpm-status" + +#Debug mode is disabled by default +DEBUG_MODE="" + +#Use sleep for testing timeouts, disabled by default. Can be used for testing & debugging +USE_SLEEP_TIMEOUT="" + +#Sleep timeout in seconds +SLEEP_TIMEOUT="0.5" + +#Parent directory where all cache files are located in the OS +CACHE_ROOT="/var/cache" + +#Name of the private directory to store the cache files +CACHE_DIR_NAME="zabbix-php-fpm" + +#Full path to directory to store cache files +CACHE_DIRECTORY="$CACHE_ROOT/$CACHE_DIR_NAME" + +#Checking all the required executables S_PS=$(type -P ps) S_GREP=$(type -P grep) S_AWK=$(type -P awk) @@ -14,60 +46,79 @@ S_JQ=$(type -P jq) S_DIRNAME=$(type -P dirname) S_CAT=$(type -P cat) S_BASH=$(type -P bash) -S_ECHO=$(type -P echo) S_PRINTF=$(type -P printf) S_WHOAMI=$(type -P whoami) +S_DATE=$(type -P date) +S_BC=$(type -P bc) +S_SLEEP=$(type -P sleep) +S_FCGI=$(type -P cgi-fcgi) -if [[ ! -f $S_PS ]]; then - ${S_ECHO} "Utility 'ps' not found. Please, install it first." +if [[ ! -x $S_PS ]]; then + echo "Utility 'ps' not found. Please, install it first." exit 1 fi -if [[ ! -f $S_GREP ]]; then - ${S_ECHO} "Utility 'grep' not found. Please, install it first." +if [[ ! -x $S_GREP ]]; then + echo "Utility 'grep' not found. Please, install it first." exit 1 fi -if [[ ! -f $S_AWK ]]; then - ${S_ECHO} "Utility 'awk' not found. Please, install it first." +if [[ ! -x $S_AWK ]]; then + echo "Utility 'awk' not found. Please, install it first." exit 1 fi -if [[ ! -f $S_SORT ]]; then - ${S_ECHO} "Utility 'sort' not found. Please, install it first." +if [[ ! -x $S_SORT ]]; then + echo "Utility 'sort' not found. Please, install it first." exit 1 fi -if [[ ! -f $S_UNIQ ]]; then - ${S_ECHO} "Utility 'uniq' not found. Please, install it first." +if [[ ! -x $S_UNIQ ]]; then + echo "Utility 'uniq' not found. Please, install it first." exit 1 fi -if [[ ! -f $S_HEAD ]]; then - ${S_ECHO} "Utility 'head' not found. Please, install it first." +if [[ ! -x $S_HEAD ]]; then + echo "Utility 'head' not found. Please, install it first." exit 1 fi -if [[ ! -f $S_LSOF ]]; then - ${S_ECHO} "Utility 'lsof' not found. Please, install it first." +if [[ ! -x $S_LSOF ]]; then + echo "Utility 'lsof' not found. Please, install it first." exit 1 fi -if [[ ! -f $S_JQ ]]; then - ${S_ECHO} "Utility 'jq' not found. Please, install it first." +if [[ ! -x $S_JQ ]]; then + echo "Utility 'jq' not found. Please, install it first." exit 1 fi -if [[ ! -f ${S_DIRNAME} ]]; then - ${S_ECHO} "Utility 'dirname' not found. Please, install it first." +if [[ ! -x ${S_DIRNAME} ]]; then + echo "Utility 'dirname' not found. Please, install it first." exit 1 fi -if [[ ! -f ${S_CAT} ]]; then - ${S_ECHO} "Utility 'cat' not found. Please, install it first." +if [[ ! -x ${S_CAT} ]]; then + echo "Utility 'cat' not found. Please, install it first." exit 1 fi -if [[ ! -f ${S_BASH} ]]; then - ${S_ECHO} "Utility 'bash' not found. Please, install it first." +if [[ ! -x ${S_BASH} ]]; then + echo "Utility 'bash' not found. Please, install it first." exit 1 fi -if [[ ! -f ${S_PRINTF} ]]; then - ${S_ECHO} "Utility 'printf' not found. Please, install it first." +if [[ ! -x ${S_PRINTF} ]]; then + echo "Utility 'printf' not found. Please, install it first." exit 1 fi -if [[ ! -f ${S_WHOAMI} ]]; then - ${S_ECHO} "Utility 'whoami' not found. Please, install it first." +if [[ ! -x ${S_WHOAMI} ]]; then + echo "Utility 'whoami' not found. Please, install it first." + exit 1 +fi +if [[ ! -x ${S_DATE} ]]; then + echo "Utility 'date' not found. Please, install it first." + exit 1 +fi +if [[ ! -x $S_BC ]]; then + echo "Utility 'bc' not found. Please, install it first." + exit 1 +fi +if [[ ! -x $S_SLEEP ]]; then + echo "Utility 'sleep' not found. Please, install it first." + exit 1 +fi +if [[ ! -x $S_FCGI ]]; then + echo "Utility 'cgi-fcgi' not found. Please, install it first." exit 1 fi @@ -76,14 +127,55 @@ if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then exit 1 fi -STATUS_PATH="/php-fpm-status" -DEBUG_MODE="" +if [[ ! -d "$CACHE_ROOT" ]]; then + ${S_ECHO} "The OS cache directory '$CACHE_ROOT' not found in the system." + exit 1 +fi + +function createCacheDirectory() { + if [[ ! -d "$CACHE_DIRECTORY" ]]; then + mkdir "$CACHE_DIRECTORY" + fi + if [[ ! -d "$CACHE_DIRECTORY" ]]; then + return 1 + fi + + chmod 700 "$CACHE_DIRECTORY" + return 0 +} + +createCacheDirectory +EXIT_CODE=$? +if [[ ${EXIT_CODE} -ne 0 ]]; then + ${S_ECHO} "Failed to create cache directory '$CACHE_DIRECTORY'." + exit 1 +fi + +#Local directory +LOCAL_DIR=$(${S_DIRNAME} "$0") + +#Cache file for pending pools, used to store execution state +#File format: +# +PENDING_FILE="$CACHE_DIRECTORY/php_fpm_pending.cache" + +#Cache file with list of active pools, used to store execution state +#File format: +# +RESULTS_CACHE_FILE="$CACHE_DIRECTORY/php_fpm_results.cache" + +#Path to status script, another script of this bundle +STATUS_SCRIPT="$LOCAL_DIR/zabbix_php_fpm_status.sh" + +#Start time of the script +START_TIME=$($S_DATE +%s%N) + ACTIVE_USER=$(${S_WHOAMI}) # Prints a string on screen. Works only if debug mode is enabled. function PrintDebug() { if [[ -n $DEBUG_MODE ]] && [[ -n $1 ]]; then - ${S_ECHO} "$1" + echo "$1" fi } @@ -93,14 +185,16 @@ function PrintDebug() { # - pool socket # Function returns 1 if all OK, and 0 otherwise. function EncodeToJson() { - POOL_NAME=$1 - POOL_SOCKET=$2 + local POOL_NAME=$1 + local POOL_SOCKET=$2 if [[ -z ${POOL_NAME} ]] || [[ -z ${POOL_SOCKET} ]]; then return 0 fi - JSON_POOL=$(${S_ECHO} -n "$POOL_NAME" | ${S_JQ} -aR .) - JSON_SOCKET=$(${S_ECHO} -n "$POOL_SOCKET" | ${S_JQ} -aR .) + local JSON_POOL + JSON_POOL=$(echo -n "$POOL_NAME" | ${S_JQ} -aR .) + local JSON_SOCKET + JSON_SOCKET=$(echo -n "$POOL_SOCKET" | ${S_JQ} -aR .) if [[ ${POOL_FIRST} == 1 ]]; then RESULT_DATA="$RESULT_DATA," fi @@ -109,67 +203,268 @@ function EncodeToJson() { return 1 } -# Checks if selected pool is in cache. +# Updates information about the pool in cache. # Input arguments: # - pool name # - pool socket -# Function returns 1 if the pool is in cache, and 0 otherwise. -function IsInCache() { - SEARCH_NAME=$1 - SEARCH_SOCKET=$2 - if [[ -z ${SEARCH_NAME} ]] || [[ -z ${SEARCH_SOCKET} ]]; then +# - pool type +function UpdatePoolInCache() { + local POOL_NAME=$1 + local POOL_SOCKET=$2 + local POOL_TYPE=$3 + local UNSET_USED="" + + if [[ -z $POOL_NAME ]] || [[ -z $POOL_SOCKET ]] || [[ -z $POOL_TYPE ]]; then + PrintDebug "Error: Invalid arguments for UpdatePoolInCache" return 0 fi - for CACHE_ITEM in "${NEW_CACHE[@]}"; do + + for ITEM_INDEX in "${!CACHE[@]}"; do + local CACHE_ITEM="${CACHE[$ITEM_INDEX]}" + + local ITEM_NAME # shellcheck disable=SC2016 - ITEM_NAME=$(${S_ECHO} "$CACHE_ITEM" | ${S_AWK} '{print $1}') + ITEM_NAME=$(echo "$CACHE_ITEM" | ${S_AWK} '{print $1}') + + local ITEM_SOCKET # shellcheck disable=SC2016 - ITEM_SOCKET=$(${S_ECHO} "$CACHE_ITEM" | ${S_AWK} '{print $2}') - if [[ ${ITEM_NAME} == "${SEARCH_NAME}" ]] && [[ ${ITEM_SOCKET} == "${SEARCH_SOCKET}" ]]; then + ITEM_SOCKET=$(echo "$CACHE_ITEM" | ${S_AWK} '{print $2}') + + local ITEM_POOL_TYPE + # shellcheck disable=SC2016 + ITEM_POOL_TYPE=$(echo "$CACHE_ITEM" | ${S_AWK} '{print $3}') + if [[ $ITEM_NAME == "$POOL_NAME" && $ITEM_SOCKET == "$POOL_SOCKET" ]] || [[ -z $ITEM_POOL_TYPE ]]; then + PrintDebug "Pool $POOL_NAME $POOL_SOCKET is in cache, deleting..." + #Deleting the pool first + unset "CACHE[$ITEM_INDEX]" + UNSET_USED="1" + fi + done + + if [[ -n "$UNSET_USED" ]]; then + #Renumber the indexes + CACHE=("${CACHE[@]}") + fi + + CACHE+=("$POOL_NAME $POOL_SOCKET $POOL_TYPE") + PrintDebug "Added pool $POOL_NAME $POOL_SOCKET to cache list" + return 0 +} + +# Removes pools from cache that are currently inactive and are missing in pending list +function UpdateCacheList() { + local UNSET_USED="" + + for ITEM_INDEX in "${!CACHE[@]}"; do + local CACHE_ITEM="${CACHE[$ITEM_INDEX]}" + + local ITEM_NAME + # shellcheck disable=SC2016 + ITEM_NAME=$(echo "$CACHE_ITEM" | ${S_AWK} '{print $1}') + + local ITEM_SOCKET + # shellcheck disable=SC2016 + ITEM_SOCKET=$(echo "$CACHE_ITEM" | ${S_AWK} '{print $2}') + + local ITEM_POOL_TYPE + # shellcheck disable=SC2016 + ITEM_POOL_TYPE=$(echo "$CACHE_ITEM" | ${S_AWK} '{print $3}') + + if [[ $ITEM_NAME == "$POOL_NAME" && $ITEM_SOCKET == "$POOL_SOCKET" ]] || [[ -z $ITEM_POOL_TYPE ]]; then + PrintDebug "Pool $POOL_NAME $POOL_SOCKET is in cache, deleting..." + #Deleting the pool first + unset "CACHE[$ITEM_INDEX]" + UNSET_USED="1" + fi + done + + if [[ -n "$UNSET_USED" ]]; then + #Renumber the indexes + CACHE=("${CACHE[@]}") + fi +} + +# Checks if selected pool is in pending list +# Function returns 1 if pool is in list, and 0 otherwise +function IsInPendingList() { + local POOL_NAME=$1 + local POOL_SOCKET=$2 + + if [[ -z $POOL_NAME ]] || [[ -z $POOL_SOCKET ]]; then + PrintDebug "Error: Invalid arguments for IsInPendingList" + return 0 + fi + + for ITEM in "${PENDING_LIST[@]}"; do + if [[ "$ITEM" == "$POOL_NAME $POOL_SOCKET" ]]; then return 1 fi done return 0 } +# Adds a pool to the pending list +# The pool is added only if it's not already in the list. +# A new pool is added to the end of the list. +# Function returns 1, if a pool was added, and 0 otherwise. +function AddPoolToPendingList() { + local POOL_NAME=$1 + local POOL_SOCKET=$2 + + if [[ -z $POOL_NAME ]] || [[ -z $POOL_SOCKET ]]; then + PrintDebug "Error: Invalid arguments for AddPoolToPendingList" + return 0 + fi + + IsInPendingList "$POOL_NAME" "$POOL_SOCKET" + local FOUND=$? + + if [[ ${FOUND} == 1 ]]; then + #Already in list, quit + PrintDebug "Pool $POOL_NAME $POOL_SOCKET is already in pending list" + return 0 + fi + + #Otherwise add this pool to the end of the list + PENDING_LIST+=("$POOL_NAME $POOL_SOCKET") + PrintDebug "Added pool $POOL_NAME $POOL_SOCKET to pending list" + return 1 +} + +# Removes a pool from pending list +# Returns 1 if success, 0 otherwise +function DeletePoolFromPendingList() { + local POOL_NAME=$1 + local POOL_SOCKET=$2 + local UNSET_USED="" + + if [[ -z $POOL_NAME ]] || [[ -z $POOL_SOCKET ]]; then + PrintDebug "Error: Invalid arguments for DeletePoolFromPendingList" + return 0 + fi + + for ITEM_INDEX in "${!PENDING_LIST[@]}"; do + local PENDING_ITEM="${PENDING_LIST[$ITEM_INDEX]}" + if [[ "$PENDING_ITEM" == "$POOL_NAME $POOL_SOCKET" ]]; then + unset "PENDING_LIST[$ITEM_INDEX]" + UNSET_USED="1" + fi + done + + if [[ -z "$UNSET_USED" ]]; then + #Not in list, quit + PrintDebug "Error: Pool $POOL_NAME $POOL_SOCKET is already missing in pending list" + return 0 + fi + + #Renumber the indexes + PENDING_LIST=("${PENDING_LIST[@]}") + PrintDebug "Removed pool $POOL_NAME $POOL_SOCKET from pending list" + return 1 +} + +function SavePrintResults() { + #Checking and creating cache directory just in case: + createCacheDirectory + + #Saving pending list: + if [[ -f $PENDING_FILE ]] && [[ ! -w $PENDING_FILE ]]; then + echo "Error: write permission is not granted to user $ACTIVE_USER for cache file $PENDING_FILE" + exit 1 + fi + + PrintDebug "Saving pending pools list to file $PENDING_FILE..." + ${S_PRINTF} "%s\n" "${PENDING_LIST[@]}" >"$PENDING_FILE" + + #We must sort the cache list + readarray -t CACHE < <(for a in "${CACHE[@]}"; do echo "$a"; done | $S_SORT) + + if [[ -n $DEBUG_MODE ]]; then + PrintDebug "List of pools to be saved to cache pools file:" + PrintCacheList + fi + + if [[ -f $RESULTS_CACHE_FILE ]] && [[ ! -w $RESULTS_CACHE_FILE ]]; then + echo "Error: write permission is not granted to user $ACTIVE_USER for cache file $RESULTS_CACHE_FILE" + exit 1 + fi + + PrintDebug "Saving cache file to file $RESULTS_CACHE_FILE..." + ${S_PRINTF} "%s\n" "${CACHE[@]}" >"$RESULTS_CACHE_FILE" + + POOL_FIRST=0 + #We store the resulting JSON data for Zabbix in the following var: + RESULT_DATA="{\"data\":[" + + for CACHE_ITEM in "${CACHE[@]}"; do + local ITEM_NAME + # shellcheck disable=SC2016 + ITEM_NAME=$(echo "$CACHE_ITEM" | ${S_AWK} '{print $1}') + + local ITEM_SOCKET + # shellcheck disable=SC2016 + ITEM_SOCKET=$(echo "$CACHE_ITEM" | ${S_AWK} '{print $2}') + EncodeToJson "${ITEM_NAME}" "${ITEM_SOCKET}" + done + + RESULT_DATA="$RESULT_DATA]}" + PrintDebug "Resulting JSON data for Zabbix:" + echo -n "$RESULT_DATA" +} + +function CheckExecutionTime() { + local CURRENT_TIME + CURRENT_TIME=$($S_DATE +%s%N) + + local ELAPSED_TIME + ELAPSED_TIME=$(echo "($CURRENT_TIME - $START_TIME)/1000000" | $S_BC) + if [[ $ELAPSED_TIME -lt $MAX_EXECUTION_TIME ]]; then + #All good, we can continue + PrintDebug "Check execution time OK, elapsed $ELAPSED_TIME ms" + return 1 + fi + + #We need to save our state and exit + PrintDebug "Check execution time: stop required, elapsed $ELAPSED_TIME ms" + + SavePrintResults + + exit 0 +} + # Validates the specified pool by getting its status and working with cache. # Pass two arguments: pool name and pool socket # Function returns: # 0 if the pool is invalid -# 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 -# 3 if the pool is OK and is not ondemand and is not in cache -function ProcessPool() { - POOL_NAME=$1 - POOL_SOCKET=$2 +# 1 if the pool is OK +function CheckPool() { + local POOL_NAME=$1 + local POOL_SOCKET=$2 if [[ -z ${POOL_NAME} ]] || [[ -z ${POOL_SOCKET} ]]; then - PrintDebug "Invalid arguments for ProcessPool" + PrintDebug "Error: Invalid arguments for CheckPool" return 0 fi - IsInCache "${POOL_NAME}" "${POOL_SOCKET}" - FOUND=$? - if [[ ${FOUND} == 1 ]]; then - return 2 - fi - + local STATUS_JSON STATUS_JSON=$(${S_BASH} "${STATUS_SCRIPT}" "${POOL_SOCKET}" ${STATUS_PATH}) - EXIT_CODE=$? + local EXIT_CODE=$? if [[ ${EXIT_CODE} == 0 ]]; then # The exit code is OK, let's check the JSON data # 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} # We use basic regular expression here, i.e. we need to use \+ and not escape { and } - if [[ -n $(${S_ECHO} "${STATUS_JSON}" | ${S_GREP} -G '^{.*\"pool\":\".\+\".*,\"process manager\":\".\+\".*}$') ]]; then + if [[ -n $(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" - # Checking if we have ondemand pool - 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" - NEW_CACHE+=("$POOL_NAME $POOL_SOCKET") + + local PROCESS_MANAGER + PROCESS_MANAGER=$(echo "$STATUS_JSON" | $S_GREP -oP '"process manager":"\K([a-z]+)') + if [[ -n $PROCESS_MANAGER ]]; then + PrintDebug "Detected pool's process manager is $PROCESS_MANAGER" + UpdatePoolInCache "$POOL_NAME" "$POOL_SOCKET" "$PROCESS_MANAGER" return 1 + else + PrintDebug "Error: Failed to detect process manager of the pool" fi - PrintDebug "Detected pool's process manager is NOT ondemand, it will not be cached" - return 3 fi PrintDebug "Failed to validate status data for pool $POOL_NAME, socket $POOL_SOCKET, status path $STATUS_PATH" @@ -185,55 +480,27 @@ function ProcessPool() { return 0 } -for ARG in "$@"; do - if [[ ${ARG} == "debug" ]]; then - DEBUG_MODE="1" - ${S_ECHO} "Debug mode enabled" - elif [[ ${ARG} == /* ]]; then - STATUS_PATH=${ARG} - PrintDebug "Argument $ARG is interpreted as status path" - else - PrintDebug "Argument $ARG is unknown and skipped" +#Sleeps for a specified predefined amount of time. Works only if "sleep mode" is enabled. +function sleepNow() { + if [[ -n $USE_SLEEP_TIMEOUT ]]; then + PrintDebug "Debug: Sleep for $SLEEP_TIMEOUT sec" + $S_SLEEP "$SLEEP_TIMEOUT" + CheckExecutionTime fi -done -PrintDebug "Current user is $ACTIVE_USER" -PrintDebug "Status path to be used: $STATUS_PATH" +} -LOCAL_DIR=$(${S_DIRNAME} "$0") -CACHE_FILE="$LOCAL_DIR/php_fpm.cache" -STATUS_SCRIPT="$LOCAL_DIR/zabbix_php_fpm_status.sh" -PrintDebug "Local directory is $LOCAL_DIR" -if [[ ! -f ${STATUS_SCRIPT} ]]; then - ${S_ECHO} "Helper script $STATUS_SCRIPT not found" - exit 1 -fi -if [[ ! -r ${STATUS_SCRIPT} ]]; then - ${S_ECHO} "Helper script $STATUS_SCRIPT is not readable" - exit 1 -fi -PrintDebug "Helper script $STATUS_SCRIPT is reachable" +# Analysis of pool by name, scans the processes, and adds them to pending list for further checks +function AnalyzePool() { + local POOL_NAME=$1 + if [[ -z ${POOL_NAME} ]]; then + PrintDebug "Invalid arguments for AnalyzePool" + return 0 + fi -# Loading cached data for ondemand pools. -# The cache file consists of lines, each line contains pool name, then space, then socket (or TCP info) -CACHE=() -NEW_CACHE=() -if [[ -r ${CACHE_FILE} ]]; then - PrintDebug "Reading cache file $CACHE_FILE..." - mapfile -t CACHE < <(${S_CAT} "${CACHE_FILE}") -else - PrintDebug "Cache file $CACHE_FILE not found, skipping..." -fi - -mapfile -t PS_LIST < <($S_PS ax | $S_GREP -F "php-fpm: pool " | $S_GREP -F -v "grep") -# shellcheck disable=SC2016 -POOL_NAMES_LIST=$(${S_PRINTF} '%s\n' "${PS_LIST[@]}" | $S_AWK '{print $NF}' | $S_SORT -u) -POOL_FIRST=0 -#We store the resulting JSON data for Zabbix in the following var: -RESULT_DATA="{\"data\":[" -while IFS= read -r line; do + local POOL_PID_LIST # shellcheck disable=SC2016 - POOL_PID_LIST=$(${S_PRINTF} '%s\n' "${PS_LIST[@]}" | $S_GREP -F -w "php-fpm: pool $line" | $S_AWK '{print $1}') - POOL_PID_ARGS="" + POOL_PID_LIST=$(${S_PRINTF} '%s\n' "${PS_LIST[@]}" | $S_GREP -F -w "php-fpm: pool $POOL_NAME" | $S_AWK '{print $1}') + local POOL_PID_ARGS="" while IFS= read -r POOL_PID; do if [[ -n $POOL_PID ]]; then POOL_PID_ARGS="$POOL_PID_ARGS -p $POOL_PID" @@ -253,61 +520,52 @@ while IFS= read -r line; do #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(s): $POOL_PID_ARGS" + PrintDebug "Started analysis of pool $POOL_NAME, PID(s): $POOL_PID_ARGS" #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 -n flag to show IP address and not convert it to domain name (like localhost) #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 + local POOL_PARAMS_LIST + # shellcheck disable=SC2086 POOL_PARAMS_LIST=$($S_LSOF -n -P $POOL_PID_ARGS 2>/dev/null | $S_GREP -w -e "unix" -e "TCP" | $S_SORT -u | $S_UNIQ -f8) - FOUND_POOL="" + local FOUND_POOL="" while IFS= read -r pool; do if [[ -n $pool ]]; then PrintDebug "Checking process: $pool" + local POOL_TYPE # shellcheck disable=SC2016 - POOL_TYPE=$(${S_ECHO} "${pool}" | $S_AWK '{print $5}') + POOL_TYPE=$(echo "${pool}" | $S_AWK '{print $5}') + local POOL_SOCKET # shellcheck disable=SC2016 - POOL_SOCKET=$(${S_ECHO} "${pool}" | $S_AWK '{print $9}') + POOL_SOCKET=$(echo "${pool}" | $S_AWK '{print $9}') if [[ -n $POOL_TYPE ]] && [[ -n $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 "Found socket $POOL_SOCKET" - ProcessPool "${line}" "${POOL_SOCKET}" - POOL_STATUS=$? - if [[ ${POOL_STATUS} -gt 0 ]]; then - FOUND_POOL="1" - PrintDebug "Success: socket $POOL_SOCKET returned valid status data" - EncodeToJson "${line}" "${POOL_SOCKET}" - else - PrintDebug "Error: socket $POOL_SOCKET didn't return valid data" - fi + AddPoolToPendingList "$POOL_NAME" "$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: + local CONNECTION_TYPE # shellcheck disable=SC2016 - CONNECTION_TYPE=$(${S_ECHO} "${pool}" | $S_AWK '{print $8}') + CONNECTION_TYPE=$(echo "${pool}" | $S_AWK '{print $8}') if [[ $CONNECTION_TYPE == "TCP" ]]; then #The connection must have state LISTEN: - LISTEN=$(${S_ECHO} "${pool}" | $S_GREP -F -w "(LISTEN)") + local LISTEN + LISTEN=$(echo "${pool}" | $S_GREP -F -w "(LISTEN)") if [[ -n $LISTEN ]]; then #Check and replace * to localhost if it's found. Asterisk means that the PHP listens on #all interfaces. + FOUND_POOL="1" PrintDebug "Found TCP connection $POOL_SOCKET" POOL_SOCKET=${POOL_SOCKET/\*:/localhost:} - PrintDebug "Processed TCP connection $POOL_SOCKET" - ProcessPool "${line}" "${POOL_SOCKET}" - POOL_STATUS=$? - if [[ ${POOL_STATUS} -gt 0 ]]; then - FOUND_POOL="1" - PrintDebug "Success: TCP connection $POOL_SOCKET returned valid status data" - EncodeToJson "${line}" "${POOL_SOCKET}" - else - PrintDebug "Error: TCP connection $POOL_SOCKET didn't return valid data" - fi + AddPoolToPendingList "$POOL_NAME" "$POOL_SOCKET" else PrintDebug "Warning: expected connection state must be LISTEN, but it was not detected" fi @@ -326,35 +584,164 @@ while IFS= read -r line; do done <<<"$POOL_PARAMS_LIST" if [[ -z ${FOUND_POOL} ]]; then - PrintDebug "Error: failed to discover information for pool $line" + PrintDebug "Error: failed to discover information for pool $POOL_NAME" fi else - PrintDebug "Error: failed to find PID for pool $line" + PrintDebug "Error: failed to find PID for pool $POOL_NAME" fi + + return 1 +} + +# Prints list of pools in pending list +function PrintPendingList() { + local COUNTER=1 + for POOL_ITEM in "${PENDING_LIST[@]}"; do + local POOL_NAME + # shellcheck disable=SC2016 + POOL_NAME=$(echo "$POOL_ITEM" | $S_AWK '{print $1}') + local POOL_SOCKET + # shellcheck disable=SC2016 + POOL_SOCKET=$(echo "$POOL_ITEM" | $S_AWK '{print $2}') + if [[ -n "$POOL_NAME" ]] && [[ -n "$POOL_SOCKET" ]]; then + PrintDebug "#$COUNTER $POOL_NAME $POOL_SOCKET" + COUNTER=$(echo "$COUNTER + 1" | $S_BC) + fi + done +} + +# Prints list of pools in cache +function PrintCacheList() { + local COUNTER=1 + for POOL_ITEM in "${CACHE[@]}"; do + local POOL_NAME + # shellcheck disable=SC2016 + POOL_NAME=$(echo "$POOL_ITEM" | $S_AWK '{print $1}') + local POOL_SOCKET + # shellcheck disable=SC2016 + POOL_SOCKET=$(echo "$POOL_ITEM" | $S_AWK '{print $2}') + local PROCESS_MANAGER + # shellcheck disable=SC2016 + PROCESS_MANAGER=$(echo "$POOL_ITEM" | $S_AWK '{print $3}') + if [[ -n "$POOL_NAME" ]] && [[ -n "$POOL_SOCKET" ]] && [[ -n "$PROCESS_MANAGER" ]]; then + PrintDebug "#$COUNTER $POOL_NAME $POOL_SOCKET $PROCESS_MANAGER" + COUNTER=$(echo "$COUNTER + 1" | $S_BC) + fi + done +} + +# Functions processes a pool by name: makes all required checks and adds it to cache, etc. +function ProcessPool() { + local POOL_NAME=$1 + local POOL_SOCKET=$2 + if [[ -z $POOL_NAME ]] || [[ -z $POOL_SOCKET ]]; then + PrintDebug "Invalid arguments for ProcessPool" + return 0 + fi + + PrintDebug "Processing pool $POOL_NAME $POOL_SOCKET" + CheckPool "$POOL_NAME" "${POOL_SOCKET}" + local POOL_STATUS=$? + if [[ ${POOL_STATUS} -gt 0 ]]; then + PrintDebug "Success: socket $POOL_SOCKET returned valid status data" + else + PrintDebug "Error: socket $POOL_SOCKET didn't return valid data" + fi + + DeletePoolFromPendingList "$POOL_NAME" "$POOL_SOCKET" + return 1 +} + +for ARG in "$@"; do + if [[ ${ARG} == "debug" ]]; then + DEBUG_MODE="1" + echo "Debug mode enabled" + elif [[ ${ARG} == "sleep" ]]; then + USE_SLEEP_TIMEOUT="1" + echo "Debug: Sleep timeout enabled" + elif [[ ${ARG} == "nosleep" ]]; then + MAX_EXECUTION_TIME="10000000" + echo "Debug: Timeout checks disabled" + elif [[ ${ARG} == /* ]]; then + STATUS_PATH=${ARG} + PrintDebug "Argument $ARG is interpreted as status path" + else + PrintDebug "Argument $ARG is unknown and skipped" + fi +done +PrintDebug "Current user is $ACTIVE_USER" +PrintDebug "Status path to be used: $STATUS_PATH" + +PrintDebug "Local directory is $LOCAL_DIR" +if [[ ! -f ${STATUS_SCRIPT} ]]; then + echo "Helper script $STATUS_SCRIPT not found" + exit 1 +fi +if [[ ! -r ${STATUS_SCRIPT} ]]; then + echo "Helper script $STATUS_SCRIPT is not readable" + exit 1 +fi +PrintDebug "Helper script $STATUS_SCRIPT is reachable" + +# Loading cached data for pools. +CACHE=() +if [[ -r $RESULTS_CACHE_FILE ]]; then + PrintDebug "Reading cache file of pools $RESULTS_CACHE_FILE..." + mapfile -t CACHE < <(${S_CAT} "$RESULTS_CACHE_FILE") +else + PrintDebug "Cache file of pools $RESULTS_CACHE_FILE not found, skipping..." +fi + +if [[ -n $DEBUG_MODE ]]; then + PrintDebug "List of pools loaded from cache pools file:" + PrintCacheList +fi + +#Loading pending tasks +PENDING_LIST=() +if [[ -r $PENDING_FILE ]]; then + PrintDebug "Reading file of pending pools $PENDING_FILE..." + mapfile -t PENDING_LIST < <($S_CAT "$PENDING_FILE") +else + PrintDebug "List of pending pools $PENDING_FILE not found, skipping..." +fi + +if [[ -n $DEBUG_MODE ]]; then + PrintDebug "List of pools loaded from pending pools file:" + PrintPendingList +fi + +mapfile -t PS_LIST < <($S_PS ax | $S_GREP -F "php-fpm: pool " | $S_GREP -F -v "grep") +# shellcheck disable=SC2016 +POOL_NAMES_LIST=$(${S_PRINTF} '%s\n' "${PS_LIST[@]}" | $S_AWK '{print $NF}' | $S_SORT -u) + +#Update pending list with pools that are active and running +while IFS= read -r POOL_NAME; do + AnalyzePool "$POOL_NAME" done <<<"$POOL_NAMES_LIST" -PrintDebug "Processing pools from old cache..." -for CACHE_ITEM in "${CACHE[@]}"; do +if [[ -n $DEBUG_MODE ]]; then + PrintDebug "Pending list generated:" + PrintPendingList +fi + +#Process pending list +PrintDebug "Processing pools" + +for POOL_ITEM in "${PENDING_LIST[@]}"; do # shellcheck disable=SC2016 - ITEM_NAME=$(${S_ECHO} "$CACHE_ITEM" | ${S_AWK} '{print $1}') + POOL_NAME=$(echo "$POOL_ITEM" | $S_AWK '{print $1}') # shellcheck disable=SC2016 - ITEM_SOCKET=$(${S_ECHO} "$CACHE_ITEM" | ${S_AWK} '{print $2}') - ProcessPool "${ITEM_NAME}" "${ITEM_SOCKET}" - POOL_STATUS=$? - if [[ ${POOL_STATUS} == "1" ]]; then - # This is a new pool and we must add it - EncodeToJson "${ITEM_NAME}" "${ITEM_SOCKET}" + POOL_SOCKET=$(echo "$POOL_ITEM" | $S_AWK '{print $2}') + if [[ -n "$POOL_NAME" ]] && [[ -n "$POOL_SOCKET" ]]; then + ProcessPool "$POOL_NAME" "$POOL_SOCKET" + + #Confirm that we run not too much time + CheckExecutionTime + + #Used for debugging: + sleepNow fi done -if [[ -f ${CACHE_FILE} ]] && [[ ! -w ${CACHE_FILE} ]]; then - ${S_ECHO} "Error: write permission is not granted to user $ACTIVE_USER for cache file $CACHE_FILE" - exit 1 -fi - -PrintDebug "Saving new cache file $CACHE_FILE..." -${S_PRINTF} "%s\n" "${NEW_CACHE[@]}" >"${CACHE_FILE}" - -RESULT_DATA="$RESULT_DATA]}" -PrintDebug "Resulting JSON data for Zabbix:" -${S_ECHO} -n "$RESULT_DATA" +SavePrintResults diff --git a/zabbix/zabbix_php_fpm_status.sh b/zabbix/zabbix_php_fpm_status.sh index 65b7249..f9a6b09 100644 --- a/zabbix/zabbix_php_fpm_status.sh +++ b/zabbix/zabbix_php_fpm_status.sh @@ -6,12 +6,12 @@ S_FCGI=$(type -P cgi-fcgi) S_GREP=$(type -P grep) -if [[ ! -f $S_FCGI ]]; then +if [[ ! -x $S_FCGI ]]; then echo "Utility 'cgi-fcgi' not found. Please, install it first." exit 1 fi -if [[ ! -f $S_GREP ]]; then +if [[ ! -x $S_GREP ]]; then echo "Utility 'grep' not found. Please, install it first." exit 1 fi