diff --git a/.travis.yml b/.travis.yml index 8fea9bf..45d9206 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,54 @@ language: bash jobs: include: + - os: linux + dist: trusty + name: "Zabbix 4.0 @ Ubuntu 14 trusty, PHP default" + arch: amd64 + addons: + apt: + sources: + - sourceline: 'deb http://repo.zabbix.com/zabbix/4.0/ubuntu trusty main' + key_url: "https://repo.zabbix.com/zabbix-official-repo.key" + packages: + - ca-certificates + - curl + - grep + - sed + - gawk + - lsof + - jq + - libfcgi0ldbl + - unzip + - zabbix-agent + - zabbix-get + - php5-fpm + before_script: + - sudo curl -o /usr/local/bin/shunit2 https://raw.githubusercontent.com/kward/shunit2/master/shunit2 + - os: linux + dist: xenial + name: "Zabbix 4.0 @ Ubuntu 16 xenial, PHP default" + arch: amd64 + addons: + apt: + sources: + - sourceline: 'deb http://repo.zabbix.com/zabbix/4.0/ubuntu xenial main' + key_url: "https://repo.zabbix.com/zabbix-official-repo.key" + packages: + - ca-certificates + - curl + - grep + - sed + - gawk + - lsof + - jq + - libfcgi0ldbl + - unzip + - zabbix-agent + - zabbix-get + - php-fpm + before_script: + - sudo curl -o /usr/local/bin/shunit2 https://raw.githubusercontent.com/kward/shunit2/master/shunit2 - os: linux dist: bionic name: "Missing packages test" diff --git a/README.md b/README.md index 1cee9bb..8e99473 100644 --- a/README.md +++ b/README.md @@ -123,14 +123,25 @@ Please refer to [Wiki](https://github.com/rvalitov/zabbix-php-fpm/wiki/Installat Please refer to [Wiki](https://github.com/rvalitov/zabbix-php-fpm/wiki/Testing-and-Troubleshooting). # Compatibility -Should work with any version of PHP-FPM (starting with PHP 5.3.3), Zabbix 4.0.x and later. -Can work with any version of ISPConfig as long as you have a valid PHP-FPM status page configuration there. -Tested with: -- [Travis CI](https://travis-ci.com/rvalitov/zabbix-php-fpm) -- PHP 7.4, 7.3, 7.2, 7.1, 7.0 -- Zabbix 5.0.1, 4.4.4, 4.2.5, 4.0.20, 4.0.16, 4.0.4 -- Debian 10, 9 -- Ubuntu 18 -- CentOS 7 -- ISPConfig v.3.1.14p2 \ No newline at end of file +### System requirements + +- **PHP**. Should work with any version of PHP-FPM (starting with PHP 5.3.3) +- **Zabbix** 4.0.x and later. +- **ISPConfig**. Can work with any version of ISPConfig as long as you have a valid PHP-FPM status page configuration there. +- Minimal `bash` version 4. +- **OS** + - **Debian** 8 Jessie or newer + - **Ubuntu** 20 Focal, 18 Bionic, 16 Xenial, 14 Trusty. Other versions are supported if they are present in the [Zabbix repository](http://repo.zabbix.com/zabbix/4.0/ubuntu/dists/). + - **CentOS** 6 or newer + - **RHEL** 5 or newer + +### Tested with: + +- [**Travis CI**](https://travis-ci.com/rvalitov/zabbix-php-fpm) +- **PHP** 7.4, 7.3, 7.2, 7.1, 7.0 +- **Zabbix** 5.0.1, 4.4.4, 4.2.5, 4.0.20, 4.0.16, 4.0.4 +- **Debian** 10, 9 +- **Ubuntu** 18 Bionic, 16 Xenial, 14 Trusty +- **CentOS** 7 +- **ISPConfig** v.3.1.14p2 diff --git a/tests/all.sh b/tests/all.sh index dbdc595..b8d4979 100644 --- a/tests/all.sh +++ b/tests/all.sh @@ -30,13 +30,53 @@ function AddSleepToConfig() { sleep 2 } +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() { + 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 + fi + done + + return 1 +} + +function getRunPHPDirectory() { + 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 + echo "$RESULT_DIR" + return 0 + fi + done + + return 1 +} + copyPool() { ORIGINAL_FILE=$1 POOL_NAME=$2 POOL_SOCKET=$3 POOL_TYPE=$4 POOL_DIR=$(dirname "${ORIGINAL_FILE}") - PHP_VERSION=$(echo "$POOL_DIR" | grep -oP "(\d\.\d)") + PHP_VERSION=$(getPHPVersion "$POOL_DIR") NEW_POOL_FILE="$POOL_DIR/${POOL_NAME}.conf" sudo cp "$ORIGINAL_FILE" "$NEW_POOL_FILE" @@ -58,15 +98,19 @@ copyPool() { setupPool() { POOL_FILE=$1 POOL_DIR=$(dirname "${POOL_FILE}") - PHP_VERSION=$(echo "$POOL_DIR" | grep -oP "(\d\.\d)") + PHP_VERSION=$(getPHPVersion "$POOL_DIR") + + PHP_RUN_DIR=$(getRunPHPDirectory) + EXIT_CODE=$? + assertEquals "Failed to find PHP run directory" "0" "$EXIT_CODE" #Delete all active pools except www.conf: 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="static$c" - POOL_SOCKET="/run/php/php${PHP_VERSION}-fpm-${POOL_NAME}.sock" + 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" @@ -116,13 +160,16 @@ setupPool() { } setupPools() { - PHP_LIST=$(sudo find /etc/php/ -name 'www.conf' -type f) + PHP_DIR=$(getEtcPHPDirectory) + EXIT_CODE=$? + assertEquals "Failed to find PHP directory" "0" "$EXIT_CODE" + PHP_LIST=$(sudo find "$PHP_DIR" -name 'www.conf' -type f) #First we need to stop all PHP-FPM while IFS= read -r pool; do if [[ -n $pool ]]; then POOL_DIR=$(dirname "$pool") - PHP_VERSION=$(echo "$POOL_DIR" | grep -oP "(\d\.\d)") + PHP_VERSION=$(getPHPVersion "$POOL_DIR") sudo service "php${PHP_VERSION}-fpm" stop fi done <<<"$PHP_LIST" @@ -136,30 +183,42 @@ setupPools() { } getNumberOfPHPVersions() { - PHP_COUNT=$(sudo find /etc/php/ -name 'www.conf' -type f | wc -l) + 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) echo "$PHP_COUNT" } function startOndemandPoolsCache() { + 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" + # We must start all the pools POOL_URL="/php-fpm-status" - PHP_LIST=$(sudo find /etc/php/ -name 'www.conf' -type f) + 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=$(echo "$POOL_DIR" | grep -oP "(\d\.\d)") + PHP_VERSION=$(getPHPVersion "$POOL_DIR") for ((c = 1; c <= MAX_POOLS; c++)); do POOL_NAME="ondemand$c" - POOL_SOCKET="/run/php/php${PHP_VERSION}-fpm-${POOL_NAME}.sock" + 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 + 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 @@ -167,6 +226,25 @@ function startOndemandPoolsCache() { done <<<"$PHP_LIST" } +getAnySocket() { + 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" + + #Get any socket of PHP-FPM: + PHP_FIRST=$(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 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" @@ -183,7 +261,7 @@ oneTimeSetUp() { #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" "$(sudo find /etc/zabbix/ -name 'zabbix_agentd*.d' -type d | head -n1)" + sudo cp "$TRAVIS_BUILD_DIR/zabbix/userparameter_php_fpm.conf" "$(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 diff --git a/zabbix/zabbix_php_fpm_discovery.sh b/zabbix/zabbix_php_fpm_discovery.sh index e92cbfa..82e07e3 100644 --- a/zabbix/zabbix_php_fpm_discovery.sh +++ b/zabbix/zabbix_php_fpm_discovery.sh @@ -113,6 +113,11 @@ if [[ ! -x $S_FCGI ]]; then exit 1 fi +if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then + ${S_ECHO} "This script requires bash version 4.x or newer. Older version detected." + exit 1 +fi + #Local directory LOCAL_DIR=$(${S_DIRNAME} "$0")