From 591b32e499ddd042da3d87c6bcf54e0d1f7d613e Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Wed, 29 Jul 2020 04:16:44 +0300 Subject: [PATCH] Intermediate fix for timeout feature (#48) * [add] more asserts and checks * [fix] PHP sockets detection in tests * [fix] improved socket dir detection * [fix] improved service name detection, code simplification * [fix] socket pools check * [fix] mapfile -d * [add] local vars * [add] Travis CI folds * [add] color output --- tests/all.sh | 499 +++++++++++++++++++++-------- tests/missing.sh | 79 ++++- zabbix/zabbix_php_fpm_discovery.sh | 119 +++++-- 3 files changed, 524 insertions(+), 173 deletions(-) diff --git a/tests/all.sh b/tests/all.sh index ae5e638..907aa97 100644 --- a/tests/all.sh +++ b/tests/all.sh @@ -3,32 +3,133 @@ #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 + +# 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 -TEST_SOCKET="" + +# 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="" + +# ---------------------------------- +# 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 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" - echo "New UserParameter file:" + travis_fold_start "AddSleepToConfig" "ⓘ New UserParameter file" sudo cat "$PARAMS_FILE" - sudo service zabbix-agent restart + travis_fold_end + restartService "zabbix-agent" sleep 2 } @@ -39,6 +140,10 @@ 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() { @@ -54,11 +159,15 @@ function getEtcPHPDirectory() { for PHP_TEST_DIR in "${LIST_OF_DIRS[@]}"; do if [[ -d "$PHP_TEST_DIR" ]]; then PHP_ETC_DIR=$PHP_TEST_DIR - echo "$PHP_ETC_DIR" - return 0 + break fi done + if [[ -n "$PHP_ETC_DIR" ]]; then + echo "$PHP_ETC_DIR" + return 0 + fi + return 1 } @@ -73,14 +182,38 @@ function getRunPHPDirectory() { "/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 PHP_SOCKET_DIR="$RESULT_DIR/" - echo "$PHP_SOCKET_DIR" - return 0 + 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 } @@ -91,6 +224,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" @@ -109,16 +243,42 @@ copyPool() { 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: sudo find "$POOL_DIR" -name '*.conf' -type f -not -name 'www.conf' -exec rm -rf {} \; @@ -145,7 +305,7 @@ setupPool() { copyPool "$POOL_FILE" "$POOL_NAME" "$POOL_SOCKET" "ondemand" done - PHP_SERIAL_ID=$(find "$PHP_DIR" -maxdepth 1 -mindepth 1 -type d | sort | grep -n -F "$PHP_VERSION" | head -n1 | cut -d : -f 1) + 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_SERIAL_ID * $MAX_PORTS_COUNT + 1)/1" | bc) @@ -165,31 +325,51 @@ setupPool() { assertNull "Port $POOL_PORT is busy" "$PORT_IS_BUSY" copyPool "$POOL_FILE" "$POOL_NAME" "$POOL_SOCKET" "static" - echo "List of configured PHP$PHP_VERSION pools:" + travis_fold_start "list_PHP$PHP_VERSION" "ⓘ List of configured PHP$PHP_VERSION pools" sudo ls -l "$POOL_DIR" - sudo service "php${PHP_VERSION}-fpm" restart + 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 - echo "List of running PHP$PHP_VERSION pools:" - sudo systemctl -l status "php${PHP_VERSION}-fpm.service" + 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=$(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 - getRunPHPDirectory + 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") - sudo service "php${PHP_VERSION}-fpm" stop + 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" @@ -204,20 +384,23 @@ 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 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 ]" # We must start all the pools POOL_URL="/php-fpm-status" @@ -227,6 +410,7 @@ function startOndemandPoolsCache() { 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" @@ -248,18 +432,20 @@ function startOndemandPoolsCache() { 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" } @@ -270,31 +456,61 @@ getAnyPort() { echo "$PHP_PORT" } +function actionService() { + local SERVICE_NAME=$1 + local SERVICE_ACTION=$2 + local SERVICE_INFO + 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 +} + +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 sed -i "s#.* Timeout=.*#Timeout = $ZABBIX_TIMEOUT#" "/etc/zabbix/zabbix_agentd.conf" - sudo service zabbix-agent restart - 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 @@ -308,13 +524,14 @@ setUp() { tearDown() { restoreUserParameters sleep 2 - sudo service zabbix-agent restart + restartService "zabbix-agent" sleep 2 } testZabbixGetInstalled() { ZABBIX_GET=$(type -P zabbix_get) assertNotNull "Utility zabbix-get not installed" "$ZABBIX_GET" + printSuccess "${FUNCNAME[0]}" } testZabbixAgentVersion() { @@ -322,6 +539,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() { @@ -329,11 +547,13 @@ 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() { @@ -341,7 +561,8 @@ testStatusScriptSocket() { 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 $TEST_SOCKET: $DATA" "$IS_OK" - echo "Success test of $TEST_SOCKET" + printGreen "Success test of $TEST_SOCKET" + printSuccess "${FUNCNAME[0]}" } testStatusScriptPort() { @@ -352,14 +573,16 @@ testStatusScriptPort() { 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() { 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() { @@ -369,13 +592,15 @@ 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 -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() { @@ -384,18 +609,21 @@ testDiscoverScriptDebug() { PHP_COUNT=$(getNumberOfPHPVersions) if [[ $PHP_COUNT != "$NUMBER_OF_ERRORS" ]]; then ERRORS_LIST=$(echo "$DATA" | grep -F 'Error:') - echo "Errors list:" - echo "$ERRORS_LIST" - echo "Full output:" + 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() { @@ -403,14 +631,17 @@ testDiscoverScriptSleep() { 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) - echo "Success time checks: $CHECK_OK_COUNT" - echo "Stop time checks: $STOP_OK_COUNT" + 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 ]" + 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() { @@ -418,6 +649,7 @@ testZabbixDiscoverSleep() { AddSleepToConfig testZabbixDiscoverReturnsData + printSuccess "${FUNCNAME[0]}" } testDiscoverScriptRunDuration() { @@ -429,11 +661,12 @@ testDiscoverScriptRunDuration() { STOP_OK_COUNT=$(echo "$DATA" | grep -o -F "stop required" | wc -l) MAX_TIME=$(echo "$ZABBIX_TIMEOUT * 1000" | bc) - echo "Elapsed time $ELAPSED_TIME ms" - echo "Success time checks: $CHECK_OK_COUNT" - echo "Stop time checks: $STOP_OK_COUNT" + 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() { @@ -446,16 +679,18 @@ testZabbixDiscoverRunDuration() { ELAPSED_TIME=$(echo "($END_TIME - $START_TIME)/1000000" | bc) MAX_TIME=$(echo "$ZABBIX_TIMEOUT * 1000" | bc) - echo "Elapsed time $ELAPSED_TIME ms" + 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_SECOND" + assertNotEquals "Multiple discovery routines provide the same results: $DATA_FIRST" "$DATA_FIRST" "$DATA_SECOND" + printSuccess "${FUNCNAME[0]}" } testZabbixDiscoverDoubleRun() { @@ -465,150 +700,138 @@ testZabbixDiscoverDoubleRun() { 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_SECOND" + 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 - MAX_CHECKS=150 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 [[ "$DATA_OLD" == "$DATA" ]]; then + 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 - echo "Data old: $DATA_OLD" - echo "Data new: $DATA" + printYellow "Data old:" + printDebug "$DATA_OLD" + printYellow "Data new:" + printDebug "$DATA" return 1 fi discoverAllZabbix "$DATA" "$DATA_COUNT" + STATUS=$? + return $STATUS fi } -testZabbixDiscoverNumberOfStaticPools() { +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" "0" "$STATUS" + assertEquals "Failed to discover all data when checking pools '$POOL_TYPE'" "0" "$STATUS" - NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"static' | wc -l) + NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F "{\"{#POOLNAME}\":\"$POOL_TYPE" | wc -l) PHP_COUNT=$(getNumberOfPHPVersions) - POOLS_BY_DESIGN=$(echo "$PHP_COUNT * $MAX_POOLS" | bc) - assertEquals "Number of pools mismatch" "$POOLS_BY_DESIGN" "$NUMBER_OF_POOLS" + 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() { + local DATA + DATA=$(checkNumberOfPools "socket") + travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" + echo "$DATA" + travis_fold_end + printSuccess "${FUNCNAME[0]}" } testZabbixDiscoverNumberOfDynamicPools() { - DATA=$(discoverAllZabbix) - STATUS=$? - if [[ $STATUS -ne 0 ]]; then - echo "$DATA" - fi - assertEquals "Failed to discover all data" "0" "$STATUS" - - NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"dynamic' | wc -l) - PHP_COUNT=$(getNumberOfPHPVersions) - POOLS_BY_DESIGN=$(echo "$PHP_COUNT * $MAX_POOLS" | bc) - assertEquals "Number of pools mismatch" "$POOLS_BY_DESIGN" "$NUMBER_OF_POOLS" + local DATA + DATA=$(checkNumberOfPools "dynamic") + travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" + echo "$DATA" + travis_fold_end + printSuccess "${FUNCNAME[0]}" } testZabbixDiscoverNumberOfOndemandPoolsCold() { - DATA=$(discoverAllZabbix) - STATUS=$? - if [[ $STATUS -ne 0 ]]; then - echo "$DATA" - fi - assertEquals "Failed to discover all data" "0" "$STATUS" - - NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"ondemand' | wc -l) + local DATA #If the pools are not started then we have 0 here: - assertEquals "Number of pools mismatch" "0" "$NUMBER_OF_POOLS" + DATA=$(checkNumberOfPools "ondemand" 0) + travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" + echo "$DATA" + travis_fold_end + printSuccess "${FUNCNAME[0]}" } testZabbixDiscoverNumberOfOndemandPoolsHot() { - PHP_COUNT=$(getNumberOfPHPVersions) startOndemandPoolsCache - - DATA=$(discoverAllZabbix) - STATUS=$? - if [[ $STATUS -ne 0 ]]; then - echo "$DATA" - fi - assertEquals "Failed to discover all data" "0" "$STATUS" - - NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"ondemand' | wc -l) - PHP_COUNT=$(getNumberOfPHPVersions) - POOLS_BY_DESIGN=$(echo "$PHP_COUNT * $MAX_POOLS" | bc) - assertEquals "Number of pools mismatch" "$POOLS_BY_DESIGN" "$NUMBER_OF_POOLS" + local DATA + DATA=$(checkNumberOfPools "ondemand") + travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" + echo "$DATA" + travis_fold_end + printSuccess "${FUNCNAME[0]}" } testZabbixDiscoverNumberOfOndemandPoolsCache() { - PHP_COUNT=$(getNumberOfPHPVersions) startOndemandPoolsCache - DATA=$(discoverAllZabbix) - STATUS=$? - if [[ $STATUS -ne 0 ]]; then - echo "$DATA" - fi - assertEquals "Failed to discover all data (initial check)" "0" "$STATUS" - - NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"ondemand' | wc -l) - PHP_COUNT=$(getNumberOfPHPVersions) - POOLS_BY_DESIGN=$(echo "$PHP_COUNT * $MAX_POOLS" | bc) - assertEquals "Number of pools mismatch (initial check)" "$POOLS_BY_DESIGN" "$NUMBER_OF_POOLS" + 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" - DATA_CACHE=$(discoverAllZabbix) - STATUS=$? - if [[ $STATUS -ne 0 ]]; then - echo "$DATA" - fi - assertEquals "Failed to discover all data (final check)" "0" "$STATUS" + printAction "Full cache test..." + CACHED_DATA=$(checkNumberOfPools "ondemand") + travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" + echo "$CACHED_DATA" + travis_fold_end - NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"ondemand' | wc -l) - PHP_COUNT=$(getNumberOfPHPVersions) - POOLS_BY_DESIGN=$(echo "$PHP_COUNT * $MAX_POOLS" | bc) - assertEquals "Number of pools mismatch (final check)" "$POOLS_BY_DESIGN" "$NUMBER_OF_POOLS" - assertEquals "Data mismatch" "$DATA" "$DATA_CACHE" + assertEquals "Data mismatch" "$INITIAL_DATA" "$CACHED_DATA" + printSuccess "${FUNCNAME[0]}" } testZabbixDiscoverNumberOfIPPools() { - DATA=$(discoverAllZabbix) - STATUS=$? - if [[ $STATUS -ne 0 ]]; then - echo "$DATA" - fi - assertEquals "Failed to discover all data" "0" "$STATUS" - - NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"localhost",' | wc -l) PHP_COUNT=$(getNumberOfPHPVersions) - POOLS_BY_DESIGN="$PHP_COUNT" - assertEquals "Number of pools mismatch" "$POOLS_BY_DESIGN" "$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=$(discoverAllZabbix) - STATUS=$? - if [[ $STATUS -ne 0 ]]; then - echo "$DATA" - fi - assertEquals "Failed to discover all data" "0" "$STATUS" - - NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"port' | wc -l) - PHP_COUNT=$(getNumberOfPHPVersions) - POOLS_BY_DESIGN=$(echo "$PHP_COUNT * $MAX_POOLS" | bc) - assertEquals "Number of pools mismatch" "$POOLS_BY_DESIGN" "$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 @@ -619,26 +842,30 @@ testDiscoverScriptManyPools() { setupPools testDiscoverScriptReturnsData + printSuccess "${FUNCNAME[0]}" } testZabbixDiscoverManyPools() { testZabbixDiscoverReturnsData + printSuccess "${FUNCNAME[0]}" } testDiscoverScriptManyPoolsRunDuration() { MAX_RUNS=5 for ((c = 1; c <= MAX_RUNS; c++)); do - echo "Run #$c..." + printAction "Run #$c..." testDiscoverScriptRunDuration done + printSuccess "${FUNCNAME[0]}" } testZabbixDiscoverManyPoolsRunDuration() { MAX_RUNS=5 for ((c = 1; c <= MAX_RUNS; c++)); do - echo "Run #$c..." + printAction "Run #$c..." testZabbixDiscoverRunDuration done + printSuccess "${FUNCNAME[0]}" } # Load shUnit2. diff --git a/tests/missing.sh b/tests/missing.sh index 8c3bca7..0756ef3 100644 --- a/tests/missing.sh +++ b/tests/missing.sh @@ -3,13 +3,82 @@ #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() { - 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" @@ -17,19 +86,21 @@ oneTimeSetUp() { sudo chmod +x /etc/zabbix/zabbix_php_fpm_discovery.sh sudo chmod +x /etc/zabbix/zabbix_php_fpm_status.sh - echo "All done, starting tests..." + 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. diff --git a/zabbix/zabbix_php_fpm_discovery.sh b/zabbix/zabbix_php_fpm_discovery.sh index 82e07e3..bde2f0e 100644 --- a/zabbix/zabbix_php_fpm_discovery.sh +++ b/zabbix/zabbix_php_fpm_discovery.sh @@ -152,13 +152,15 @@ 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 + 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," @@ -174,9 +176,10 @@ function EncodeToJson() { # - pool socket # - pool type function UpdatePoolInCache() { - POOL_NAME=$1 - POOL_SOCKET=$2 - POOL_TYPE=$3 + 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" @@ -184,20 +187,32 @@ function UpdatePoolInCache() { fi for ITEM_INDEX in "${!CACHE[@]}"; do - CACHE_ITEM="${CACHE[$ITEM_INDEX]}" + 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 - mapfile -d $'\0' -t CACHE < <($S_PRINTF '%s\0' "${CACHE[@]}" | $S_GREP -Fwzv "$ITEM_NAME $ITEM_SOCKET") + 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 @@ -205,28 +220,42 @@ function UpdatePoolInCache() { # 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 - CACHE_ITEM="${CACHE[$ITEM_INDEX]}" + 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 - mapfile -d $'\0' -t CACHE < <($S_PRINTF '%s\0' "${CACHE[@]}" | $S_GREP -Fwzv "ITEM_NAME $ITEM_SOCKET") + 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() { - POOL_NAME=$1 - POOL_SOCKET=$2 + local POOL_NAME=$1 + local POOL_SOCKET=$2 if [[ -z $POOL_NAME ]] || [[ -z $POOL_SOCKET ]]; then PrintDebug "Error: Invalid arguments for IsInPendingList" @@ -246,8 +275,8 @@ function IsInPendingList() { # A new pool is added to the end of the list. # Function returns 1, if a pool was added, and 0 otherwise. function AddPoolToPendingList() { - POOL_NAME=$1 - POOL_SOCKET=$2 + local POOL_NAME=$1 + local POOL_SOCKET=$2 if [[ -z $POOL_NAME ]] || [[ -z $POOL_SOCKET ]]; then PrintDebug "Error: Invalid arguments for AddPoolToPendingList" @@ -255,7 +284,7 @@ function AddPoolToPendingList() { fi IsInPendingList "$POOL_NAME" "$POOL_SOCKET" - FOUND=$? + local FOUND=$? if [[ ${FOUND} == 1 ]]; then #Already in list, quit @@ -272,25 +301,31 @@ function AddPoolToPendingList() { # Removes a pool from pending list # Returns 1 if success, 0 otherwise function DeletePoolFromPendingList() { - POOL_NAME=$1 - POOL_SOCKET=$2 + 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 - IsInPendingList "$POOL_NAME" "$POOL_SOCKET" - FOUND=$? + 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 [[ ${FOUND} == 0 ]]; then + 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 - #Otherwise we remove this pool from the list - mapfile -d $'\0' -t PENDING_LIST < <($S_PRINTF '%s\0' "${PENDING_LIST[@]}" | $S_GREP -Fxzv "$POOL_NAME $POOL_SOCKET") + #Renumber the indexes + PENDING_LIST=("${PENDING_LIST[@]}") PrintDebug "Removed pool $POOL_NAME $POOL_SOCKET from pending list" return 1 } @@ -326,8 +361,11 @@ function SavePrintResults() { 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}" @@ -339,7 +377,10 @@ function SavePrintResults() { } 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 @@ -361,15 +402,16 @@ function CheckExecutionTime() { # 0 if the pool is invalid # 1 if the pool is OK function CheckPool() { - POOL_NAME=$1 - POOL_SOCKET=$2 + local POOL_NAME=$1 + local POOL_SOCKET=$2 if [[ -z ${POOL_NAME} ]] || [[ -z ${POOL_SOCKET} ]]; then PrintDebug "Error: Invalid arguments for CheckPool" return 0 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: @@ -378,6 +420,7 @@ function CheckPool() { 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" + 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" @@ -412,15 +455,16 @@ function sleepNow() { # Analysis of pool by name, scans the processes, and adds them to pending list for further checks function AnalyzePool() { - POOL_NAME=$1 + local POOL_NAME=$1 if [[ -z ${POOL_NAME} ]]; then PrintDebug "Invalid arguments for AnalyzePool" return 0 fi + local POOL_PID_LIST # shellcheck disable=SC2016 POOL_PID_LIST=$(${S_PRINTF} '%s\n' "${PS_LIST[@]}" | $S_GREP -F -w "php-fpm: pool $POOL_NAME" | $S_AWK '{print $1}') - POOL_PID_ARGS="" + 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" @@ -447,14 +491,17 @@ function AnalyzePool() { #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=$(echo "${pool}" | $S_AWK '{print $5}') + local POOL_SOCKET # shellcheck disable=SC2016 POOL_SOCKET=$(echo "${pool}" | $S_AWK '{print $9}') if [[ -n $POOL_TYPE ]] && [[ -n $POOL_SOCKET ]]; then @@ -469,10 +516,12 @@ function AnalyzePool() { 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=$(echo "${pool}" | $S_AWK '{print $8}') if [[ $CONNECTION_TYPE == "TCP" ]]; then #The connection must have state 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 @@ -510,10 +559,12 @@ function AnalyzePool() { # Prints list of pools in pending list function PrintPendingList() { - COUNTER=1 + 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 @@ -525,12 +576,15 @@ function PrintPendingList() { # Prints list of pools in cache function PrintCacheList() { - COUNTER=1 + 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 @@ -542,8 +596,8 @@ function PrintCacheList() { # Functions processes a pool by name: makes all required checks and adds it to cache, etc. function ProcessPool() { - POOL_NAME=$1 - POOL_SOCKET=$2 + local POOL_NAME=$1 + local POOL_SOCKET=$2 if [[ -z $POOL_NAME ]] || [[ -z $POOL_SOCKET ]]; then PrintDebug "Invalid arguments for ProcessPool" return 0 @@ -551,9 +605,8 @@ function ProcessPool() { PrintDebug "Processing pool $POOL_NAME $POOL_SOCKET" CheckPool "$POOL_NAME" "${POOL_SOCKET}" - POOL_STATUS=$? + local POOL_STATUS=$? if [[ ${POOL_STATUS} -gt 0 ]]; then - FOUND_POOL="1" PrintDebug "Success: socket $POOL_SOCKET returned valid status data" else PrintDebug "Error: socket $POOL_SOCKET didn't return valid data"