2020-06-28 23:43:33 +02:00
#!/bin/bash
#Ramil Valitov ramilvalitov@gmail.com
#https://github.com/rvalitov/zabbix-php-fpm
#This script is used for testing
2020-06-29 00:37:20 +02:00
MAX_POOLS = 3
MAX_PORTS = 3
2020-07-07 21:00:55 +02:00
MIN_PORT = 49001
MAX_PORTS_COUNT = 100
2020-07-06 23:50:36 +02:00
TEST_SOCKET = ""
2020-07-07 14:57:20 +02:00
ONDEMAND_TIMEOUT = 60
2020-07-07 22:20:54 +02:00
ZABBIX_TIMEOUT = 20
2020-07-21 14:59:42 +02:00
PHP_SOCKET_DIR = ""
PHP_ETC_DIR = ""
2020-06-29 11:42:42 +02:00
2020-07-07 14:03:28 +02:00
function getUserParameters( ) {
2020-07-21 15:14:15 +02:00
sudo find /etc/zabbix/ -name 'userparameter_php_fpm.conf' -type f 2>/dev/null | sort | head -n1
2020-07-07 14:03:28 +02:00
}
function restoreUserParameters( ) {
PARAMS_FILE = $( getUserParameters)
sudo rm -f $PARAMS_FILE
2020-07-21 15:14:15 +02:00
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) "
2020-07-07 14:03:28 +02:00
}
2020-07-07 14:17:58 +02:00
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:"
sudo cat " $PARAMS_FILE "
sudo service zabbix-agent restart
sleep 2
}
2020-07-21 05:26:29 +02:00
function getPHPVersion( ) {
TEST_STRING = $1
PHP_VERSION = $( echo " $TEST_STRING " | grep -oP "(\d\.\d)" )
if [ [ -z " $PHP_VERSION " ] ] ; then
PHP_VERSION = $( echo " $TEST_STRING " | grep -oP "php(\d)" | grep -oP "(\d)" )
fi
echo " $PHP_VERSION "
}
function getEtcPHPDirectory( ) {
2020-07-21 14:59:42 +02:00
if [ [ -n " $PHP_ETC_DIR " ] ] ; then
echo " $PHP_ETC_DIR "
return 0
fi
2020-07-21 05:26:29 +02:00
LIST_OF_DIRS = (
"/etc/php/"
"/etc/php5/"
)
for PHP_TEST_DIR in " ${ LIST_OF_DIRS [@] } " ; do
if [ [ -d " $PHP_TEST_DIR " ] ] ; then
2020-07-21 14:59:42 +02:00
PHP_ETC_DIR = $PHP_TEST_DIR
echo " $PHP_ETC_DIR "
2020-07-21 05:26:29 +02:00
return 0
fi
done
return 1
}
function getRunPHPDirectory( ) {
2020-07-21 14:59:42 +02:00
if [ [ -n " $PHP_SOCKET_DIR " ] ] ; then
echo " $PHP_SOCKET_DIR "
return 0
fi
2020-07-21 05:26:29 +02:00
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)
if [ [ -d " $RESULT_DIR " ] ] ; then
2020-07-21 14:59:42 +02:00
PHP_SOCKET_DIR = " $RESULT_DIR / "
echo " $PHP_SOCKET_DIR "
2020-07-21 05:26:29 +02:00
return 0
fi
done
return 1
}
2020-06-29 11:42:42 +02:00
copyPool( ) {
ORIGINAL_FILE = $1
POOL_NAME = $2
POOL_SOCKET = $3
POOL_TYPE = $4
POOL_DIR = $( dirname " ${ ORIGINAL_FILE } " )
2020-07-21 05:26:29 +02:00
PHP_VERSION = $( getPHPVersion " $POOL_DIR " )
2020-06-29 11:42:42 +02:00
NEW_POOL_FILE = " $POOL_DIR / ${ POOL_NAME } .conf "
sudo cp " $ORIGINAL_FILE " " $NEW_POOL_FILE "
#Add status path
sudo sed -i 's#;pm.status_path.*#pm.status_path = /php-fpm-status#' " $NEW_POOL_FILE "
#Set pool manager
sudo sed -i " s#pm = dynamic#pm = $POOL_TYPE # " " $NEW_POOL_FILE "
#Socket
sudo sed -i " s#listen =.*#listen = $POOL_SOCKET # " " $NEW_POOL_FILE "
#Pool name
sudo sed -i " s#\[www\]#[ $POOL_NAME ]# " " $NEW_POOL_FILE "
2020-07-07 11:13:33 +02:00
if [ [ $POOL_TYPE = = "ondemand" ] ] ; then
2020-07-07 14:57:20 +02:00
sudo sed -i " s#;pm.process_idle_timeout.*#pm.process_idle_timeout = ${ ONDEMAND_TIMEOUT } s# " " $NEW_POOL_FILE "
2020-07-07 11:13:33 +02:00
fi
2020-06-29 11:42:42 +02:00
}
2020-06-29 00:37:20 +02:00
2020-06-28 23:43:33 +02:00
setupPool( ) {
POOL_FILE = $1
POOL_DIR = $( dirname " ${ POOL_FILE } " )
2020-07-21 05:26:29 +02:00
PHP_VERSION = $( getPHPVersion " $POOL_DIR " )
PHP_RUN_DIR = $( getRunPHPDirectory)
EXIT_CODE = $?
assertEquals "Failed to find PHP run directory" "0" " $EXIT_CODE "
2020-06-28 23:43:33 +02:00
2020-07-21 15:14:15 +02:00
PHP_DIR = $( getEtcPHPDirectory)
2020-06-29 00:37:20 +02:00
#Delete all active pools except www.conf:
2020-06-29 19:20:17 +02:00
sudo find " $POOL_DIR " -name '*.conf' -type f -not -name 'www.conf' -exec rm -rf { } \;
2020-06-29 00:37:20 +02:00
2020-06-29 11:42:42 +02:00
#Create new socket pools
2020-06-28 23:43:33 +02:00
for ( ( c = 1; c <= MAX_POOLS; c++) ) ; do
POOL_NAME = " socket $c "
2020-07-21 05:26:29 +02:00
POOL_SOCKET = " ${ PHP_RUN_DIR } php ${ PHP_VERSION } -fpm- ${ POOL_NAME } .sock "
2020-06-29 11:42:42 +02:00
copyPool " $POOL_FILE " " $POOL_NAME " " $POOL_SOCKET " "static"
2020-07-06 23:50:36 +02:00
if [ [ -z $TEST_SOCKET ] ] ; then
TEST_SOCKET = " $POOL_SOCKET "
fi
2020-06-28 23:43:33 +02:00
done
2020-07-06 22:08:12 +02:00
for ( ( c = 1; c <= MAX_POOLS; c++) ) ; do
POOL_NAME = " dynamic $c "
2020-07-21 15:14:15 +02:00
POOL_SOCKET = " ${ PHP_RUN_DIR } php ${ PHP_VERSION } -fpm- ${ POOL_NAME } .sock "
2020-07-06 22:08:12 +02:00
copyPool " $POOL_FILE " " $POOL_NAME " " $POOL_SOCKET " "dynamic"
done
for ( ( c = 1; c <= MAX_POOLS; c++) ) ; do
POOL_NAME = " ondemand $c "
2020-07-21 15:14:15 +02:00
POOL_SOCKET = " ${ PHP_RUN_DIR } php ${ PHP_VERSION } -fpm- ${ POOL_NAME } .sock "
2020-07-06 22:08:12 +02:00
copyPool " $POOL_FILE " " $POOL_NAME " " $POOL_SOCKET " "ondemand"
done
2020-07-21 15:14:15 +02:00
PHP_SERIAL_ID = $( find " $PHP_DIR " -maxdepth 1 -mindepth 1 -type d | sort | grep -n -F " $PHP_VERSION " | head -n1 | cut -d : -f 1)
2020-06-29 11:42:42 +02:00
#Create TCP port based pools
2020-06-28 23:43:33 +02:00
#Division on 1 is required to convert from float to integer
2020-07-07 21:00:55 +02:00
START_PORT = $( echo " ( $MIN_PORT + $PHP_SERIAL_ID * $MAX_PORTS_COUNT + 1)/1 " | bc)
2020-06-28 23:43:33 +02:00
for ( ( c = 1; c <= MAX_PORTS; c++) ) ; do
2020-06-29 11:42:42 +02:00
POOL_NAME = " port $c "
2020-06-28 23:43:33 +02:00
POOL_PORT = $( echo " ( $START_PORT + $c )/1 " | bc)
2020-07-07 21:40:51 +02:00
PORT_IS_BUSY = $( sudo lsof -i:" $POOL_PORT " )
2020-07-07 21:00:55 +02:00
assertNull " Port $POOL_PORT is busy " " $PORT_IS_BUSY "
2020-06-29 11:42:42 +02:00
copyPool " $POOL_FILE " " $POOL_NAME " " $POOL_PORT " "static"
2020-06-28 23:43:33 +02:00
done
2020-06-29 11:42:42 +02:00
#Create TCP IPv4 localhost pool
POOL_NAME = "localhost"
2020-07-07 21:00:55 +02:00
POOL_PORT = $( echo " ( $MIN_PORT + $PHP_SERIAL_ID * $MAX_PORTS_COUNT )/1 " | bc)
2020-06-29 11:42:42 +02:00
POOL_SOCKET = " 127.0.0.1: $POOL_PORT "
2020-07-07 21:40:51 +02:00
PORT_IS_BUSY = $( sudo lsof -i:" $POOL_PORT " )
2020-07-07 21:00:55 +02:00
assertNull " Port $POOL_PORT is busy " " $PORT_IS_BUSY "
2020-06-29 11:42:42 +02:00
copyPool " $POOL_FILE " " $POOL_NAME " " $POOL_SOCKET " "static"
2020-07-07 12:02:55 +02:00
echo " List of configured PHP $PHP_VERSION pools: "
2020-07-07 11:24:07 +02:00
sudo ls -l " $POOL_DIR "
2020-06-28 23:43:33 +02:00
sudo service " php ${ PHP_VERSION } -fpm " restart
2020-07-07 11:24:07 +02:00
sleep 3
2020-07-07 12:02:55 +02:00
echo " List of running PHP $PHP_VERSION pools: "
2020-07-07 05:40:23 +02:00
sudo systemctl -l status " php ${ PHP_VERSION } -fpm.service "
2020-07-07 11:24:07 +02:00
sleep 2
2020-06-28 23:43:33 +02:00
}
2020-06-29 00:37:20 +02:00
setupPools( ) {
2020-07-21 05:26:29 +02:00
PHP_DIR = $( getEtcPHPDirectory)
EXIT_CODE = $?
assertEquals "Failed to find PHP directory" "0" " $EXIT_CODE "
2020-07-21 05:41:40 +02:00
PHP_LIST = $( sudo find " $PHP_DIR " -name 'www.conf' -type f)
2020-07-07 20:17:21 +02:00
2020-07-21 14:59:42 +02:00
#Call to detect and cache PHP run directory, we need to call it before we stop all PHP-FPM
getRunPHPDirectory
2020-07-07 20:17:21 +02:00
#First we need to stop all PHP-FPM
while IFS = read -r pool; do
if [ [ -n $pool ] ] ; then
POOL_DIR = $( dirname " $pool " )
2020-07-21 05:41:40 +02:00
PHP_VERSION = $( getPHPVersion " $POOL_DIR " )
2020-07-07 20:17:21 +02:00
sudo service " php ${ PHP_VERSION } -fpm " stop
fi
done <<< " $PHP_LIST "
#Now we reconfigure them and restart
2020-06-29 00:37:20 +02:00
while IFS = read -r pool; do
if [ [ -n $pool ] ] ; then
setupPool " $pool "
fi
done <<< " $PHP_LIST "
}
2020-06-29 00:44:32 +02:00
getNumberOfPHPVersions( ) {
2020-07-21 05:26:29 +02:00
PHP_DIR = $( getEtcPHPDirectory)
EXIT_CODE = $?
assertEquals "Failed to find PHP directory" "0" " $EXIT_CODE "
PHP_COUNT = $( find " $PHP_DIR " -name 'www.conf' -type f | wc -l)
2020-06-29 00:44:32 +02:00
echo " $PHP_COUNT "
}
2020-07-07 14:57:20 +02:00
function startOndemandPoolsCache( ) {
2020-07-21 05:41:40 +02:00
PHP_DIR = $( getEtcPHPDirectory)
EXIT_CODE = $?
assertEquals "Failed to find PHP directory" "0" " $EXIT_CODE "
PHP_RUN_DIR = $( getRunPHPDirectory)
EXIT_CODE = $?
assertEquals "Failed to find PHP run directory" "0" " $EXIT_CODE "
2020-07-07 14:57:20 +02:00
# We must start all the pools
POOL_URL = "/php-fpm-status"
2020-07-21 05:41:40 +02:00
PHP_LIST = $( sudo find " $PHP_DIR " -name 'www.conf' -type f)
2020-07-07 14:57:20 +02:00
while IFS = read -r pool; do
if [ [ -n $pool ] ] ; then
POOL_DIR = $( dirname " $pool " )
2020-07-21 05:41:40 +02:00
PHP_VERSION = $( getPHPVersion " $POOL_DIR " )
2020-07-07 14:57:20 +02:00
for ( ( c = 1; c <= MAX_POOLS; c++) ) ; do
POOL_NAME = " ondemand $c "
2020-07-21 05:41:40 +02:00
POOL_SOCKET = " ${ PHP_RUN_DIR } php ${ PHP_VERSION } -fpm- ${ POOL_NAME } .sock "
2020-07-07 14:57:20 +02:00
PHP_STATUS = $(
SCRIPT_NAME = $POOL_URL \
2020-07-21 05:41:40 +02:00
SCRIPT_FILENAME = $POOL_URL \
QUERY_STRING = json \
REQUEST_METHOD = GET \
sudo cgi-fcgi -bind -connect " $POOL_SOCKET " 2>/dev/null
2020-07-07 14:57:20 +02:00
)
assertNotNull " Failed to connect to $POOL_SOCKET " " $PHP_STATUS "
done
fi
done <<< " $PHP_LIST "
}
2020-06-28 23:43:33 +02:00
getAnySocket( ) {
2020-07-21 05:26:29 +02:00
PHP_DIR = $( getEtcPHPDirectory)
EXIT_CODE = $?
assertEquals "Failed to find PHP directory" "0" " $EXIT_CODE "
PHP_RUN_DIR = $( getRunPHPDirectory)
EXIT_CODE = $?
assertEquals "Failed to find PHP run directory" "0" " $EXIT_CODE "
2020-06-28 23:43:33 +02:00
#Get any socket of PHP-FPM:
2020-07-21 05:26:29 +02:00
PHP_FIRST = $( find " $PHP_DIR " -name 'www.conf' -type f | sort | head -n1)
2020-06-28 23:43:33 +02:00
assertNotNull "Failed to get PHP conf" " $PHP_FIRST "
2020-07-21 05:26:29 +02:00
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)
2020-06-28 23:43:33 +02:00
assertNotNull " Failed to get PHP ${ PHP_VERSION } socket " " $PHP_POOL "
echo " $PHP_POOL "
}
getAnyPort( ) {
PHP_PORT = $( sudo netstat -tulpn | grep -F "LISTEN" | grep -F "php-fpm" | head -n1 | awk '{print $4}' | rev | cut -d: -f1 | rev)
assertNotNull "Failed to get PHP port" " $PHP_PORT "
echo " $PHP_PORT "
}
oneTimeSetUp( ) {
2020-07-03 15:48:27 +02:00
echo " Started job $TRAVIS_JOB_NAME "
2020-06-29 14:28:33 +02:00
echo "Host info:"
nslookup localhost
sudo ifconfig
sudo cat /etc/hosts
2020-06-28 23:43:33 +02:00
echo "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"
2020-07-21 05:26:29 +02:00
sudo cp " $TRAVIS_BUILD_DIR /zabbix/userparameter_php_fpm.conf " " $( find /etc/zabbix/ -name 'zabbix_agentd*.d' -type d | sort | head -n1) "
2020-06-28 23:43:33 +02:00
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
2020-07-07 15:09:55 +02:00
sudo sed -i " s#.* Timeout=.*#Timeout = $ZABBIX_TIMEOUT # " "/etc/zabbix/zabbix_agentd.conf"
2020-06-28 23:43:33 +02:00
sudo service zabbix-agent restart
echo "Setup PHP-FPM..."
#Setup PHP-FPM pools:
2020-06-29 00:37:20 +02:00
setupPools
2020-06-28 23:43:33 +02:00
echo "All done, starting tests..."
}
2020-07-07 11:29:21 +02:00
#Called before every test
setUp( ) {
#Delete all cache files
2020-07-07 12:02:55 +02:00
sudo rm -f "/etc/zabbix/php_fpm_results.cache"
sudo rm -f "/etc/zabbix/php_fpm_pending.cache"
2020-07-07 11:29:21 +02:00
}
#Called after every test
2020-07-07 11:04:29 +02:00
tearDown( ) {
restoreUserParameters
2020-07-07 11:13:33 +02:00
sleep 2
2020-07-07 11:04:29 +02:00
sudo service zabbix-agent restart
2020-07-07 11:13:33 +02:00
sleep 2
2020-07-07 11:04:29 +02:00
}
2020-06-28 23:43:33 +02:00
testZabbixGetInstalled( ) {
ZABBIX_GET = $( type -P zabbix_get)
assertNotNull "Utility zabbix-get not installed" " $ZABBIX_GET "
}
2020-07-03 15:48:27 +02:00
testZabbixAgentVersion( ) {
#Example: 4.4
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 "
}
testZabbixGetVersion( ) {
#Example: 4.4
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 "
}
2020-06-28 23:43:33 +02:00
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 "
}
testStatusScriptSocket( ) {
2020-07-07 11:20:27 +02:00
assertNotNull "Test socket is not defined" " $TEST_SOCKET "
2020-07-06 23:50:36 +02:00
DATA = $( sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_status.sh" " $TEST_SOCKET " "/php-fpm-status" )
2020-06-28 23:43:33 +02:00
IS_OK = $( echo " $DATA " | grep -F '{"pool":"' )
2020-07-07 11:20:27 +02:00
assertNotNull " Failed to get status from pool $TEST_SOCKET : $DATA " " $IS_OK "
echo " Success test of $TEST_SOCKET "
2020-06-28 23:43:33 +02:00
}
testStatusScriptPort( ) {
PHP_PORT = $( getAnyPort)
PHP_POOL = " 127.0.0.1: $PHP_PORT "
#Make the test:
2020-07-06 23:29:01 +02:00
DATA = $( sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_status.sh" " $PHP_POOL " "/php-fpm-status" )
2020-06-28 23:43:33 +02:00
IS_OK = $( echo " $DATA " | grep -F '{"pool":"' )
assertNotNull " Failed to get status from pool $PHP_POOL : $DATA " " $IS_OK "
2020-06-29 11:42:42 +02:00
echo " Success test of $PHP_POOL "
2020-06-28 23:43:33 +02:00
}
testZabbixStatusSocket( ) {
2020-07-06 23:50:36 +02:00
DATA = $( zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.status[ " $TEST_SOCKET " ,"/php-fpm-status" ] )
2020-06-28 23:43:33 +02:00
IS_OK = $( echo " $DATA " | grep -F '{"pool":"' )
assertNotNull " Failed to get status from pool $PHP_POOL : $DATA " " $IS_OK "
2020-06-29 11:42:42 +02:00
echo " Success test of $PHP_POOL "
2020-06-28 23:43:33 +02:00
}
testZabbixStatusPort( ) {
PHP_PORT = $( getAnyPort)
PHP_POOL = " 127.0.0.1: $PHP_PORT "
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 "
2020-06-29 11:42:42 +02:00
echo " Success test of $PHP_POOL "
2020-06-28 23:43:33 +02:00
}
testDiscoverScriptReturnsData( ) {
2020-07-06 23:29:01 +02:00
DATA = $( sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "/php-fpm-status" )
2020-06-28 23:43:33 +02:00
IS_OK = $( echo " $DATA " | grep -F '{"data":[{"{#POOLNAME}"' )
assertNotNull " Discover script failed: $DATA " " $IS_OK "
}
2020-06-29 01:16:36 +02:00
testDiscoverScriptDebug( ) {
2020-07-07 00:31:17 +02:00
DATA = $( sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "nosleep" "/php-fpm-status" )
2020-07-06 23:56:13 +02:00
NUMBER_OF_ERRORS = $( echo " $DATA " | grep -o -F 'Error:' | wc -l)
PHP_COUNT = $( getNumberOfPHPVersions)
2020-07-07 04:25:12 +02:00
if [ [ $PHP_COUNT != " $NUMBER_OF_ERRORS " ] ] ; then
2020-07-07 00:13:01 +02:00
ERRORS_LIST = $( echo " $DATA " | grep -F 'Error:' )
echo "Errors list:"
echo " $ERRORS_LIST "
echo "Full output:"
echo " $DATA "
fi
assertEquals "Discover script errors mismatch" " $PHP_COUNT " " $NUMBER_OF_ERRORS "
2020-06-29 01:16:36 +02:00
}
2020-06-29 00:18:44 +02:00
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 "
}
2020-07-07 04:55:00 +02:00
testDiscoverScriptSleep( ) {
DATA = $( sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "sleep" "/php-fpm-status" )
2020-07-07 05:09:52 +02:00
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)
2020-07-07 04:55:00 +02:00
2020-07-07 05:09:52 +02:00
echo " Success time checks: $CHECK_OK_COUNT "
echo " Stop time checks: $STOP_OK_COUNT "
2020-07-07 05:40:23 +02:00
2020-07-07 05:48:20 +02:00
if [ [ $CHECK_OK_COUNT -lt 1 ] ] || [ [ $STOP_OK_COUNT -lt 1 ] ] ; then
2020-07-07 05:40:23 +02:00
echo " $DATA "
fi
assertTrue "No success time checks detected" " [ $CHECK_OK_COUNT -gt 0 ] "
assertTrue "No success stop checks detected" " [ $STOP_OK_COUNT -gt 0 ] "
2020-07-07 04:55:00 +02:00
}
2020-07-07 11:20:27 +02:00
testZabbixDiscoverSleep( ) {
#Add sleep
AddSleepToConfig
2020-07-07 11:04:29 +02:00
testZabbixDiscoverReturnsData
}
2020-07-07 14:17:58 +02:00
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)
2020-07-07 14:57:20 +02:00
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)
2020-07-07 15:22:16 +02:00
MAX_TIME = $( echo " $ZABBIX_TIMEOUT * 1000 " | bc)
2020-07-07 14:17:58 +02:00
echo " Elapsed time $ELAPSED_TIME ms "
2020-07-07 14:57:20 +02:00
echo " Success time checks: $CHECK_OK_COUNT "
echo " Stop time checks: $STOP_OK_COUNT "
2020-07-07 14:17:58 +02:00
2020-07-07 15:22:16 +02:00
assertTrue "The script worked for too long" " [ $ELAPSED_TIME -lt $MAX_TIME ] "
2020-07-07 14:17:58 +02:00
}
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)
2020-07-07 15:22:16 +02:00
MAX_TIME = $( echo " $ZABBIX_TIMEOUT * 1000 " | bc)
2020-07-07 14:17:58 +02:00
echo " Elapsed time $ELAPSED_TIME ms "
2020-07-07 15:22:16 +02:00
assertTrue "The script worked for too long" " [ $ELAPSED_TIME -lt $MAX_TIME ] "
2020-07-07 14:17:58 +02:00
}
2020-07-07 04:55:00 +02:00
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 "
}
2020-07-07 11:04:29 +02:00
testZabbixDiscoverDoubleRun( ) {
#Add sleep
2020-07-07 11:20:27 +02:00
AddSleepToConfig
2020-07-07 11:04:29 +02:00
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 "
}
2020-07-07 04:55:00 +02:00
function discoverAllZabbix( ) {
2020-07-07 04:25:12 +02:00
DATA_OLD = $1
DATA_COUNT = $2
2020-07-07 15:09:55 +02:00
MAX_CHECKS = 150
2020-07-07 00:31:17 +02:00
2020-07-07 04:25:12 +02:00
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" ] )
2020-07-07 04:30:24 +02:00
if [ [ " $DATA_OLD " = = " $DATA " ] ] ; then
2020-07-07 04:25:12 +02:00
echo " $DATA "
return 0
else
DATA_COUNT = $( echo " $DATA_COUNT + 1 " | bc)
2020-07-07 14:03:28 +02:00
if [ [ $DATA_COUNT -gt $MAX_CHECKS ] ] ; then
2020-07-07 04:37:40 +02:00
echo " Data old: $DATA_OLD "
echo " Data new: $DATA "
2020-07-07 04:25:12 +02:00
return 1
fi
2020-07-07 04:55:00 +02:00
discoverAllZabbix " $DATA " " $DATA_COUNT "
2020-07-07 04:25:12 +02:00
fi
}
testZabbixDiscoverNumberOfStaticPools( ) {
2020-07-07 04:55:00 +02:00
DATA = $( discoverAllZabbix)
2020-07-07 04:25:12 +02:00
STATUS = $?
2020-07-07 04:30:24 +02:00
if [ [ $STATUS -ne 0 ] ] ; then
echo " $DATA "
fi
2020-07-07 04:25:12 +02:00
assertEquals "Failed to discover all data" "0" " $STATUS "
2020-07-07 00:31:17 +02:00
2020-07-06 22:08:12 +02:00
NUMBER_OF_POOLS = $( echo " $DATA " | grep -o -F '{"{#POOLNAME}":"static' | wc -l)
PHP_COUNT = $( getNumberOfPHPVersions)
2020-07-06 22:12:47 +02:00
POOLS_BY_DESIGN = $( echo " $PHP_COUNT * $MAX_POOLS " | bc)
assertEquals "Number of pools mismatch" " $POOLS_BY_DESIGN " " $NUMBER_OF_POOLS "
2020-07-06 22:08:12 +02:00
}
testZabbixDiscoverNumberOfDynamicPools( ) {
2020-07-07 04:55:00 +02:00
DATA = $( discoverAllZabbix)
2020-07-07 04:25:12 +02:00
STATUS = $?
2020-07-07 04:30:24 +02:00
if [ [ $STATUS -ne 0 ] ] ; then
echo " $DATA "
fi
2020-07-07 04:25:12 +02:00
assertEquals "Failed to discover all data" "0" " $STATUS "
2020-07-07 00:31:17 +02:00
2020-07-06 22:08:12 +02:00
NUMBER_OF_POOLS = $( echo " $DATA " | grep -o -F '{"{#POOLNAME}":"dynamic' | wc -l)
PHP_COUNT = $( getNumberOfPHPVersions)
2020-07-06 22:12:47 +02:00
POOLS_BY_DESIGN = $( echo " $PHP_COUNT * $MAX_POOLS " | bc)
assertEquals "Number of pools mismatch" " $POOLS_BY_DESIGN " " $NUMBER_OF_POOLS "
2020-07-06 22:08:12 +02:00
}
2020-07-06 23:50:36 +02:00
testZabbixDiscoverNumberOfOndemandPoolsCold( ) {
2020-07-07 04:55:00 +02:00
DATA = $( discoverAllZabbix)
2020-07-07 04:25:12 +02:00
STATUS = $?
2020-07-07 04:30:24 +02:00
if [ [ $STATUS -ne 0 ] ] ; then
echo " $DATA "
fi
2020-07-07 04:25:12 +02:00
assertEquals "Failed to discover all data" "0" " $STATUS "
2020-07-07 00:31:17 +02:00
2020-07-06 23:50:36 +02:00
NUMBER_OF_POOLS = $( echo " $DATA " | grep -o -F '{"{#POOLNAME}":"ondemand' | wc -l)
#If the pools are not started then we have 0 here:
assertEquals "Number of pools mismatch" "0" " $NUMBER_OF_POOLS "
}
testZabbixDiscoverNumberOfOndemandPoolsHot( ) {
2020-07-07 00:05:11 +02:00
PHP_COUNT = $( getNumberOfPHPVersions)
2020-07-07 14:57:20 +02:00
startOndemandPoolsCache
2020-07-07 00:05:11 +02:00
2020-07-07 14:57:20 +02:00
DATA = $( discoverAllZabbix)
STATUS = $?
if [ [ $STATUS -ne 0 ] ] ; then
echo " $DATA "
fi
assertEquals "Failed to discover all data" "0" " $STATUS "
2020-07-07 00:05:11 +02:00
2020-07-07 14:57:20 +02:00
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 "
}
2020-07-07 00:05:11 +02:00
2020-07-07 14:57:20 +02:00
testZabbixDiscoverNumberOfOndemandPoolsCache( ) {
PHP_COUNT = $( getNumberOfPHPVersions)
startOndemandPoolsCache
2020-07-06 23:50:36 +02:00
2020-07-07 04:55:00 +02:00
DATA = $( discoverAllZabbix)
2020-07-07 04:30:24 +02:00
STATUS = $?
if [ [ $STATUS -ne 0 ] ] ; then
echo " $DATA "
fi
2020-07-07 14:57:20 +02:00
assertEquals "Failed to discover all data (initial check)" "0" " $STATUS "
2020-07-07 00:31:17 +02:00
2020-07-06 22:08:12 +02:00
NUMBER_OF_POOLS = $( echo " $DATA " | grep -o -F '{"{#POOLNAME}":"ondemand' | wc -l)
2020-06-29 00:44:32 +02:00
PHP_COUNT = $( getNumberOfPHPVersions)
2020-07-06 22:12:47 +02:00
POOLS_BY_DESIGN = $( echo " $PHP_COUNT * $MAX_POOLS " | bc)
2020-07-07 14:57:20 +02:00
assertEquals "Number of pools mismatch (initial check)" " $POOLS_BY_DESIGN " " $NUMBER_OF_POOLS "
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 "
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 "
2020-06-29 00:44:32 +02:00
}
2020-06-29 11:42:42 +02:00
testZabbixDiscoverNumberOfIPPools( ) {
2020-07-07 04:55:00 +02:00
DATA = $( discoverAllZabbix)
2020-07-07 04:25:12 +02:00
STATUS = $?
2020-07-07 04:30:24 +02:00
if [ [ $STATUS -ne 0 ] ] ; then
echo " $DATA "
fi
2020-07-07 04:25:12 +02:00
assertEquals "Failed to discover all data" "0" " $STATUS "
2020-07-07 00:31:17 +02:00
2020-06-29 11:42:42 +02:00
NUMBER_OF_POOLS = $( echo " $DATA " | grep -o -F '{"{#POOLNAME}":"localhost",' | wc -l)
PHP_COUNT = $( getNumberOfPHPVersions)
2020-07-06 22:24:53 +02:00
POOLS_BY_DESIGN = " $PHP_COUNT "
2020-07-06 22:12:47 +02:00
assertEquals "Number of pools mismatch" " $POOLS_BY_DESIGN " " $NUMBER_OF_POOLS "
2020-06-29 11:42:42 +02:00
}
testZabbixDiscoverNumberOfPortPools( ) {
2020-07-07 04:55:00 +02:00
DATA = $( discoverAllZabbix)
2020-07-07 04:25:12 +02:00
STATUS = $?
2020-07-07 04:30:24 +02:00
if [ [ $STATUS -ne 0 ] ] ; then
echo " $DATA "
fi
2020-07-07 04:25:12 +02:00
assertEquals "Failed to discover all data" "0" " $STATUS "
2020-07-07 00:31:17 +02:00
2020-07-06 22:12:47 +02:00
NUMBER_OF_POOLS = $( echo " $DATA " | grep -o -F '{"{#POOLNAME}":"port' | wc -l)
2020-06-29 00:44:32 +02:00
PHP_COUNT = $( getNumberOfPHPVersions)
2020-07-06 22:12:47 +02:00
POOLS_BY_DESIGN = $( echo " $PHP_COUNT * $MAX_POOLS " | bc)
assertEquals "Number of pools mismatch" " $POOLS_BY_DESIGN " " $NUMBER_OF_POOLS "
2020-06-29 00:44:32 +02:00
}
2020-07-05 23:18:23 +02:00
#This test should be last in Zabbix tests
2020-07-07 11:04:29 +02:00
testDiscoverScriptManyPools( ) {
2020-06-29 18:06:35 +02:00
#Create lots of pools
2020-07-07 04:55:00 +02:00
MAX_POOLS = 20
MAX_PORTS = 20
2020-06-29 18:06:35 +02:00
setupPools
2020-07-07 05:48:20 +02:00
testDiscoverScriptReturnsData
}
2020-07-07 11:04:29 +02:00
testZabbixDiscoverManyPools( ) {
2020-06-29 18:06:35 +02:00
testZabbixDiscoverReturnsData
}
2020-07-07 14:29:59 +02:00
testDiscoverScriptManyPoolsRunDuration( ) {
2020-07-07 18:46:50 +02:00
MAX_RUNS = 5
for ( ( c = 1; c <= MAX_RUNS; c++) ) ; do
echo " Run # $c ... "
testDiscoverScriptRunDuration
done
2020-07-07 14:29:59 +02:00
}
testZabbixDiscoverManyPoolsRunDuration( ) {
2020-07-07 18:46:50 +02:00
MAX_RUNS = 5
for ( ( c = 1; c <= MAX_RUNS; c++) ) ; do
echo " Run # $c ... "
testZabbixDiscoverRunDuration
done
2020-07-07 14:29:59 +02:00
}
2020-06-28 23:43:33 +02:00
# Load shUnit2.
. shunit2