diff --git a/tests/all.sh b/tests/all.sh index db58c11..c6ba7cb 100644 --- a/tests/all.sh +++ b/tests/all.sh @@ -142,6 +142,19 @@ function AddSleepToConfig() { sleep 2 } +function StartTimer() { + START_TIME=$(date +%s%N) +} + +function printElapsedTime() { + local END_TIME + local ELAPSED_TIME + + END_TIME=$(date +%s%N) + ELAPSED_TIME=$(echo "($END_TIME - $START_TIME)/1000000" | bc) + printYellow "Elapsed time $ELAPSED_TIME ms" +} + function getPHPVersion() { TEST_STRING=$1 PHP_VERSION=$(echo "$TEST_STRING" | grep -oP "(\d\.\d)") @@ -530,6 +543,7 @@ setUp() { if [[ -d "$CACHE_DIRECTORY" ]]; then sudo find "$CACHE_DIRECTORY" -type f -exec rm '{}' \; fi + StartTimer } #Called after every test @@ -566,6 +580,7 @@ testNonRootUserPrivilegesDiscovery() { #Run the script under non root user DATA=$(sudo -u zabbix "/etc/zabbix/zabbix_php_fpm_discovery.sh") IS_OK=$(echo "$DATA" | grep -F 'Insufficient privileges') + printElapsedTime assertNotNull "The discovery script must not work for non root user" "$IS_OK" printSuccess "${FUNCNAME[0]}" } @@ -575,6 +590,7 @@ testNonRootUserPrivilegesStatus() { assertNotNull "Test socket is not defined" "$TEST_SOCKET" DATA=$(sudo -u zabbix "/etc/zabbix/zabbix_php_fpm_status.sh" "$TEST_SOCKET" "/php-fpm-status") IS_OK=$(echo "$DATA" | grep -F 'Insufficient privileges') + printElapsedTime assertNotNull "The status script must not work for non root user" "$IS_OK" printSuccess "${FUNCNAME[0]}" } @@ -589,6 +605,7 @@ testStatusScriptSocket() { 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":"') + printElapsedTime assertNotNull "Failed to get status from pool $TEST_SOCKET: $DATA" "$IS_OK" printGreen "Success test of $TEST_SOCKET" printSuccess "${FUNCNAME[0]}" @@ -601,6 +618,7 @@ testStatusScriptPort() { #Make the test: DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_status.sh" "$PHP_POOL" "/php-fpm-status") IS_OK=$(echo "$DATA" | grep -F '{"pool":"') + printElapsedTime assertNotNull "Failed to get status from pool $PHP_POOL: $DATA" "$IS_OK" printGreen "Success test of $PHP_POOL" printSuccess "${FUNCNAME[0]}" @@ -609,6 +627,7 @@ testStatusScriptPort() { 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":"') + printElapsedTime assertNotNull "Failed to get status from pool $PHP_POOL: $DATA" "$IS_OK" printGreen "Success test of $PHP_POOL" printSuccess "${FUNCNAME[0]}" @@ -620,6 +639,7 @@ 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":"') + printElapsedTime assertNotNull "Failed to get status from pool $PHP_POOL: $DATA" "$IS_OK" printGreen "Success test of $PHP_POOL" printSuccess "${FUNCNAME[0]}" @@ -628,6 +648,7 @@ testZabbixStatusPort() { testDiscoverScriptReturnsData() { DATA=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "/php-fpm-status") IS_OK=$(echo "$DATA" | grep -F '{"data":[{"{#POOLNAME}"') + printElapsedTime assertNotNull "Discover script failed: $DATA" "$IS_OK" printSuccess "${FUNCNAME[0]}" } @@ -644,6 +665,7 @@ testDiscoverScriptDebug() { echo "$DATA" travis_fold_end fi + printElapsedTime assertEquals "Discover script errors mismatch" "$PHP_COUNT" "$NUMBER_OF_ERRORS" printSuccess "${FUNCNAME[0]}" } @@ -651,6 +673,7 @@ testDiscoverScriptDebug() { 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}"') + printElapsedTime assertNotNull "Discover script failed: $DATA" "$IS_OK" printSuccess "${FUNCNAME[0]}" } @@ -660,6 +683,7 @@ 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) + printElapsedTime printYellow "Success time checks: $CHECK_OK_COUNT" printYellow "Stop time checks: $STOP_OK_COUNT" @@ -682,15 +706,12 @@ testZabbixDiscoverSleep() { } 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" + printElapsedTime printYellow "Success time checks: $CHECK_OK_COUNT" printYellow "Stop time checks: $STOP_OK_COUNT" @@ -702,13 +723,10 @@ 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" + printElapsedTime assertTrue "The script worked for too long" "[ $ELAPSED_TIME -lt $MAX_TIME ]" printSuccess "${FUNCNAME[0]}" @@ -718,6 +736,7 @@ 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") + printElapsedTime assertNotEquals "Multiple discovery routines provide the same results: $DATA_FIRST" "$DATA_FIRST" "$DATA_SECOND" printSuccess "${FUNCNAME[0]}" } @@ -729,6 +748,7 @@ 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"]) + printElapsedTime assertNotEquals "Multiple discovery routines provide the same results: $DATA_FIRST" "$DATA_FIRST" "$DATA_SECOND" printSuccess "${FUNCNAME[0]}" } @@ -790,6 +810,7 @@ testZabbixDiscoverNumberOfSocketPools() { travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" echo "$DATA" travis_fold_end + printElapsedTime printSuccess "${FUNCNAME[0]}" } @@ -799,6 +820,7 @@ testZabbixDiscoverNumberOfDynamicPools() { travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" echo "$DATA" travis_fold_end + printElapsedTime printSuccess "${FUNCNAME[0]}" } @@ -809,6 +831,7 @@ testZabbixDiscoverNumberOfOndemandPoolsCold() { travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" echo "$DATA" travis_fold_end + printElapsedTime printSuccess "${FUNCNAME[0]}" } @@ -819,6 +842,7 @@ testZabbixDiscoverNumberOfOndemandPoolsHot() { travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" echo "$DATA" travis_fold_end + printElapsedTime printSuccess "${FUNCNAME[0]}" } @@ -840,6 +864,7 @@ testZabbixDiscoverNumberOfOndemandPoolsCache() { echo "$CACHED_DATA" travis_fold_end + printElapsedTime assertEquals "Data mismatch" "$INITIAL_DATA" "$CACHED_DATA" printSuccess "${FUNCNAME[0]}" } @@ -851,6 +876,7 @@ testZabbixDiscoverNumberOfIPPools() { travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" echo "$DATA" travis_fold_end + printElapsedTime printSuccess "${FUNCNAME[0]}" } @@ -860,6 +886,7 @@ testZabbixDiscoverNumberOfPortPools() { travis_fold_start "${FUNCNAME[0]}" "ⓘ Zabbix response" echo "$DATA" travis_fold_end + printElapsedTime printSuccess "${FUNCNAME[0]}" } @@ -885,6 +912,7 @@ testDiscoverScriptManyPoolsRunDuration() { printAction "Run #$c..." testDiscoverScriptRunDuration done + printElapsedTime printSuccess "${FUNCNAME[0]}" } @@ -894,6 +922,7 @@ testZabbixDiscoverManyPoolsRunDuration() { printAction "Run #$c..." testZabbixDiscoverRunDuration done + printElapsedTime printSuccess "${FUNCNAME[0]}" } diff --git a/zabbix/zabbix_php_fpm_discovery.sh b/zabbix/zabbix_php_fpm_discovery.sh index a987055..178c7da 100644 --- a/zabbix/zabbix_php_fpm_discovery.sh +++ b/zabbix/zabbix_php_fpm_discovery.sh @@ -38,6 +38,13 @@ CACHE_DIRECTORY="$CACHE_ROOT/$CACHE_DIR_NAME" #Maximum number of tasks allowed to be run in parallel MAX_PARALLEL_TASKS=10 +# Separator that is used internally in array. +# This is only a single character that should not be distinct and not in a list of characters allowed in: +# PHP-FPM pool name +# domain name +# IP address, port, and colon (:) +ARRAY_SEPARATOR=";" + #Checking all the required executables S_PS=$(type -P ps) S_GREP=$(type -P grep) @@ -234,15 +241,15 @@ function UpdatePoolInCache() { local ITEM_NAME # shellcheck disable=SC2016 - ITEM_NAME=$(echo "$CACHE_ITEM" | $S_AWK -F ";" '{print $1}') + ITEM_NAME=$(echo "$CACHE_ITEM" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $1}') local ITEM_SOCKET # shellcheck disable=SC2016 - ITEM_SOCKET=$(echo "$CACHE_ITEM" | $S_AWK -F ";" '{print $2}') + ITEM_SOCKET=$(echo "$CACHE_ITEM" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $2}') local ITEM_POOL_TYPE # shellcheck disable=SC2016 - ITEM_POOL_TYPE=$(echo "$CACHE_ITEM" | $S_AWK -F ";" '{print $3}') + ITEM_POOL_TYPE=$(echo "$CACHE_ITEM" | $S_AWK -F "$ARRAY_SEPARATOR" '{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..." @@ -257,7 +264,7 @@ function UpdatePoolInCache() { CACHE=("${CACHE[@]}") fi - CACHE+=("$POOL_NAME;$POOL_SOCKET;$POOL_TYPE") + CACHE+=("$POOL_NAME$ARRAY_SEPARATOR$POOL_SOCKET$ARRAY_SEPARATOR$POOL_TYPE") PrintDebug "Added pool $POOL_NAME $POOL_SOCKET to cache list" return 0 } @@ -271,15 +278,15 @@ function UpdateCacheList() { local ITEM_NAME # shellcheck disable=SC2016 - ITEM_NAME=$(echo "$CACHE_ITEM" | $S_AWK -F ";" '{print $1}') + ITEM_NAME=$(echo "$CACHE_ITEM" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $1}') local ITEM_SOCKET # shellcheck disable=SC2016 - ITEM_SOCKET=$(echo "$CACHE_ITEM" | $S_AWK -F ";" '{print $2}') + ITEM_SOCKET=$(echo "$CACHE_ITEM" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $2}') local ITEM_POOL_TYPE # shellcheck disable=SC2016 - ITEM_POOL_TYPE=$(echo "$CACHE_ITEM" | $S_AWK -F ";" '{print $3}') + ITEM_POOL_TYPE=$(echo "$CACHE_ITEM" | $S_AWK -F "$ARRAY_SEPARATOR" '{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..." @@ -337,7 +344,7 @@ function AddPoolToPendingList() { fi #Otherwise add this pool to the end of the list - PENDING_LIST+=("$POOL_NAME;$POOL_SOCKET") + PENDING_LIST+=("$POOL_NAME$ARRAY_SEPARATOR$POOL_SOCKET") PrintDebug "Added pool $POOL_NAME $POOL_SOCKET to pending list" return 1 } @@ -356,7 +363,7 @@ function DeletePoolFromPendingList() { for ITEM_INDEX in "${!PENDING_LIST[@]}"; do local PENDING_ITEM="${PENDING_LIST[$ITEM_INDEX]}" - if [[ "$PENDING_ITEM" == "$POOL_NAME $POOL_SOCKET" ]]; then + if [[ "$PENDING_ITEM" == "$POOL_NAME$ARRAY_SEPARATOR$POOL_SOCKET" ]]; then unset "PENDING_LIST[$ITEM_INDEX]" UNSET_USED="1" fi @@ -410,11 +417,11 @@ function SavePrintResults() { for CACHE_ITEM in "${CACHE[@]}"; do local ITEM_NAME # shellcheck disable=SC2016 - ITEM_NAME=$(echo "$CACHE_ITEM" | $S_AWK -F ";" '{print $1}') + ITEM_NAME=$(echo "$CACHE_ITEM" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $1}') local ITEM_SOCKET # shellcheck disable=SC2016 - ITEM_SOCKET=$(echo "$CACHE_ITEM" | $S_AWK -F ";" '{print $2}') + ITEM_SOCKET=$(echo "$CACHE_ITEM" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $2}') EncodeToJson "$ITEM_NAME" "$ITEM_SOCKET" done @@ -621,10 +628,10 @@ function PrintPendingList() { for POOL_ITEM in "${PENDING_LIST[@]}"; do local POOL_NAME # shellcheck disable=SC2016 - POOL_NAME=$(echo "$POOL_ITEM" | $S_AWK -F ";" '{print $1}') + POOL_NAME=$(echo "$POOL_ITEM" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $1}') local POOL_SOCKET # shellcheck disable=SC2016 - POOL_SOCKET=$(echo "$POOL_ITEM" | $S_AWK -F ";" '{print $2}') + POOL_SOCKET=$(echo "$POOL_ITEM" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $2}') if [[ -n "$POOL_NAME" ]] && [[ -n "$POOL_SOCKET" ]]; then PrintDebug "#$COUNTER $POOL_NAME $POOL_SOCKET" COUNTER=$(echo "$COUNTER + 1" | $S_BC) @@ -638,13 +645,13 @@ function PrintCacheList() { for POOL_ITEM in "${CACHE[@]}"; do local POOL_NAME # shellcheck disable=SC2016 - POOL_NAME=$(echo "$POOL_ITEM" | $S_AWK -F ";" '{print $1}') + POOL_NAME=$(echo "$POOL_ITEM" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $1}') local POOL_SOCKET # shellcheck disable=SC2016 - POOL_SOCKET=$(echo "$POOL_ITEM" | $S_AWK -F ";" '{print $2}') + POOL_SOCKET=$(echo "$POOL_ITEM" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $2}') local PROCESS_MANAGER # shellcheck disable=SC2016 - PROCESS_MANAGER=$(echo "$POOL_ITEM" | $S_AWK -F ";" '{print $3}') + PROCESS_MANAGER=$(echo "$POOL_ITEM" | $S_AWK -F "$ARRAY_SEPARATOR" '{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) @@ -798,16 +805,16 @@ LAST_PENDING_ITEM=${PENDING_LIST[${#PENDING_LIST[@]} - 1]} for POOL_ITEM in "${PENDING_LIST[@]}"; do # shellcheck disable=SC2016 - POOL_NAME=$(echo "$POOL_ITEM" | $S_AWK -F ";" '{print $1}') + POOL_NAME=$(echo "$POOL_ITEM" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $1}') # shellcheck disable=SC2016 - POOL_SOCKET=$(echo "$POOL_ITEM" | $S_AWK -F ";" '{print $2}') + POOL_SOCKET=$(echo "$POOL_ITEM" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $2}') if [[ -n "$POOL_NAME" ]] && [[ -n "$POOL_SOCKET" ]]; then PARALLEL_TASKS=$((PARALLEL_TASKS + 1)) if [[ $PARALLEL_TASKS -le $MAX_PARALLEL_TASKS ]]; then PrintDebug "Starting processing task for pool $POOL_NAME $POOL_SOCKET, subprocess #$PARALLEL_TASKS..." ProcessPool "$POOL_NAME" "$POOL_SOCKET" & TASK_PID=$! - TASK_LIST+=("$POOL_NAME;$POOL_SOCKET;$TASK_PID") + TASK_LIST+=("$POOL_NAME$ARRAY_SEPARATOR$POOL_SOCKET$ARRAY_SEPARATOR$TASK_PID") fi if [[ $PARALLEL_TASKS -gt $MAX_PARALLEL_TASKS ]] || [[ $LAST_PENDING_ITEM == "$POOL_ITEM" ]]; then @@ -819,11 +826,11 @@ for POOL_ITEM in "${PENDING_LIST[@]}"; do fi for TASK_LINE in "${TASK_LIST[@]}"; do # shellcheck disable=SC2016 - POOL_NAME=$(echo "$TASK_LINE" | $S_AWK -F ";" '{print $1}') + POOL_NAME=$(echo "$TASK_LINE" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $1}') # shellcheck disable=SC2016 - POOL_SOCKET=$(echo "$TASK_LINE" | $S_AWK -F ";" '{print $2}') + POOL_SOCKET=$(echo "$TASK_LINE" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $2}') # shellcheck disable=SC2016 - TASK_PID=$(echo "$TASK_LINE" | $S_AWK -F ";" '{print $3}') + TASK_PID=$(echo "$TASK_LINE" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $3}') wait $TASK_PID EXIT_CODE=$? PrintDebug "Finished parallel task PID $TASK_PID for pool \"$POOL_NAME\" at $POOL_SOCKET"