From 5318b36d0a753df0acea26c498cf0a7c7e527061 Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 00:43:33 +0300 Subject: [PATCH 01/18] Feature/travis ci (#37) Supports Travis CI, fixes sudo problem --- .travis.yml | 117 +++++++++++++++++++++++++ README.md | 6 +- tests/all.sh | 140 ++++++++++++++++++++++++++++++ zabbix/userparameter_php_fpm.conf | 4 +- zabbix/zabbix_php_fpm_status.sh | 3 +- 5 files changed, 264 insertions(+), 6 deletions(-) create mode 100644 .travis.yml create mode 100644 tests/all.sh diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..08c7f48 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,117 @@ +--- +language: bash + +jobs: + include: + - os: linux + dist: bionic + name: "Zabbix 4.0 @ Ubuntu 18 bionic, PHP default" + arch: amd64 + addons: + apt: + sources: + - sourceline: 'deb http://repo.zabbix.com/zabbix/4.0/ubuntu bionic main' + key_url: "https://repo.zabbix.com/zabbix-official-repo.key" + packages: + - ca-certificates + - curl + - grep + - sed + - gawk + - lsof + - jq + - libfcgi-bin + - 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: "Zabbix 4.4 @ Ubuntu 18 bionic, PHP default" + arch: amd64 + addons: + apt: + sources: + - sourceline: 'deb http://repo.zabbix.com/zabbix/4.4/ubuntu bionic main' + key_url: "https://repo.zabbix.com/zabbix-official-repo.key" + packages: + - ca-certificates + - curl + - grep + - sed + - gawk + - lsof + - jq + - libfcgi-bin + - 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: "Zabbix 4.0 @ Ubuntu 18 bionic, PHP 7.0-7.4" + arch: amd64 + addons: + apt: + sources: + - sourceline: 'deb http://repo.zabbix.com/zabbix/4.4/ubuntu bionic main' + key_url: "https://repo.zabbix.com/zabbix-official-repo.key" + - sourceline: 'ppa:ondrej/php' + packages: + - ca-certificates + - curl + - grep + - sed + - gawk + - lsof + - jq + - libfcgi-bin + - unzip + - zabbix-agent + - zabbix-get + - php7.0-fpm + - php7.1-fpm + - php7.2-fpm + - php7.3-fpm + - php7.4-fpm + before_script: + - sudo curl -o /usr/local/bin/shunit2 https://raw.githubusercontent.com/kward/shunit2/master/shunit2 + - os: linux + dist: bionic + name: "Zabbix 4.4 @ Ubuntu 18 bionic, PHP 7.0-7.4" + arch: amd64 + addons: + apt: + sources: + - sourceline: 'deb http://repo.zabbix.com/zabbix/4.4/ubuntu bionic main' + key_url: "https://repo.zabbix.com/zabbix-official-repo.key" + - sourceline: 'ppa:ondrej/php' + packages: + - ca-certificates + - curl + - grep + - sed + - gawk + - lsof + - jq + - libfcgi-bin + - unzip + - zabbix-agent + - zabbix-get + - php7.0-fpm + - php7.1-fpm + - php7.2-fpm + - php7.3-fpm + - php7.4-fpm + before_script: + - sudo curl -o /usr/local/bin/shunit2 https://raw.githubusercontent.com/kward/shunit2/master/shunit2 + +script: bash tests/all.sh +notifications: + email: false +env: + - SH=bash diff --git a/README.md b/README.md index 9058150..013cf0d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # PHP-FPM Zabbix Template with Auto Discovery and Multiple Pools -![Zabbix versions](https://img.shields.io/badge/Zabbix_versions-4.4,_4.2,_4.0-green.svg?style=flat) ![PHP](https://img.shields.io/badge/PHP-5.3.3+-blue.svg?style=flat) ![PHP7](https://img.shields.io/badge/PHP7-supported-green.svg?style=flat) ![LLD](https://img.shields.io/badge/LLD-yes-green.svg?style=flat) ![ISPConfig](https://img.shields.io/badge/ISPConfig-supported-green.svg?style=flat) +![Zabbix versions](https://img.shields.io/badge/Zabbix_versions-4.4,_4.2,_4.0-green.svg?style=flat) ![PHP](https://img.shields.io/badge/PHP-5.3.3+-blue.svg?style=flat) ![PHP7](https://img.shields.io/badge/PHP7-supported-green.svg?style=flat) ![LLD](https://img.shields.io/badge/LLD-yes-green.svg?style=flat) ![ISPConfig](https://img.shields.io/badge/ISPConfig-supported-green.svg?style=flat) [![Build Status](https://travis-ci.com/rvalitov/zabbix-php-fpm.svg?branch=master)](https://travis-ci.com/rvalitov/zabbix-php-fpm) ![Banner](https://github.com/rvalitov/zabbix-php-fpm/wiki/media/repository-open-graph-template.png) @@ -112,7 +112,7 @@ Screens are based on the graphs above: - CPU utilization - Memory utilization - Queue -- Max children riched +- Max children reached ![Zabbix screens example](https://github.com/rvalitov/zabbix-php-fpm/wiki/media/zabbix-screens.jpg) @@ -127,8 +127,10 @@ Should work with any version of PHP-FPM (starting with PHP 5.3.3), Zabbix 4.0.x 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.2, 7.3, 7.4 - Zabbix 4.0.4, 4.0.16, 4.0.20, 4.2.5, 4.4.4 - Debian 9, 10 +- Ubuntu 18 - CentOS 7 - ISPConfig v.3.1.14p2 \ No newline at end of file diff --git a/tests/all.sh b/tests/all.sh new file mode 100644 index 0000000..d186c4a --- /dev/null +++ b/tests/all.sh @@ -0,0 +1,140 @@ +#!/bin/bash +#Ramil Valitov ramilvalitov@gmail.com +#https://github.com/rvalitov/zabbix-php-fpm +#This script is used for testing + +setupPool() { + POOL_FILE=$1 + POOL_DIR=$(dirname "${POOL_FILE}") + PHP_VERSION=$(echo "$POOL_DIR" | grep -oP "(\d\.\d)") + + #Add status path + echo 'pm.status_path = /php-fpm-status' | sudo tee -a "$POOL_FILE" + #Set pool manager + sudo sed -i 's#pm = dynamic#pm = static#' "$POOL_FILE" + + #Make copies and create new socket pools + MAX_POOLS=50 + for ((c = 1; c <= MAX_POOLS; c++)); do + POOL_NAME="socket$c" + NEW_POOL_FILE="$POOL_DIR/${POOL_NAME}.conf" + sudo cp "$POOL_FILE" "$NEW_POOL_FILE" + + sudo sed -i "s#listen =.*#listen = /run/php/php${PHP_VERSION}-fpm-${POOL_NAME}.sock#" "$NEW_POOL_FILE" + sudo sed -i "s#\[www\]#[$POOL_NAME]#" "$NEW_POOL_FILE" + done + + #Make copies and create HTTP pools + MAX_PORTS=50 + #Division on 1 is required to convert from float to integer + START_PORT=$(echo "(9000 + $PHP_VERSION * 100)/1" | bc) + for ((c = 1; c <= MAX_PORTS; c++)); do + POOL_NAME="http$c" + POOL_PORT=$(echo "($START_PORT + $c)/1" | bc) + NEW_POOL_FILE="$POOL_DIR/${POOL_NAME}.conf" + sudo cp "$POOL_FILE" "$NEW_POOL_FILE" + + sudo sed -i "s#listen =.*#listen = 127.0.0.1:$POOL_PORT#" "$NEW_POOL_FILE" + sudo sed -i "s#\[www\]#[$POOL_NAME]#" "$NEW_POOL_FILE" + done + + sudo service "php${PHP_VERSION}-fpm" restart +} + +getAnySocket() { + #Get any socket of PHP-FPM: + PHP_FIRST=$(find /etc/php/ -name 'www.conf' -type f | head -n1) + assertNotNull "Failed to get PHP conf" "$PHP_FIRST" + PHP_VERSION=$(echo "$PHP_FIRST" | grep -oP "(\d\.\d)") + assertNotNull "Failed to get PHP version" "$PHP_VERSION" + PHP_POOL=$(find /run/php/ -name "php${PHP_VERSION}*.sock" -type s | 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" + echo "$PHP_PORT" +} + +oneTimeSetUp() { + 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" + sudo cp "$TRAVIS_BUILD_DIR/zabbix/userparameter_php_fpm.conf" "$(find /etc/zabbix/ -name 'zabbix_agentd*.d' -type d | head -n1)" + sudo chmod +x /etc/zabbix/zabbix_php_fpm_discovery.sh + sudo chmod +x /etc/zabbix/zabbix_php_fpm_status.sh + + #Configure Zabbix: + echo 'zabbix ALL=NOPASSWD: /etc/zabbix/zabbix_php_fpm_discovery.sh,/etc/zabbix/zabbix_php_fpm_status.sh' | sudo EDITOR='tee -a' visudo + sudo service zabbix-agent restart + + echo "Setup PHP-FPM..." + + #Setup PHP-FPM pools: + PHP_LIST=$(find /etc/php/ -name 'www.conf' -type f) + while IFS= read -r pool; do + if [[ -n $pool ]]; then + setupPool "$pool" + fi + done <<<"$PHP_LIST" + + echo "All done, starting tests..." +} + +testZabbixGetInstalled() { + ZABBIX_GET=$(type -P zabbix_get) + assertNotNull "Utility zabbix-get not installed" "$ZABBIX_GET" +} + +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() { + PHP_POOL=$(getAnySocket) + + #Make the test: + DATA=$(sudo bash "/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" +} + +testStatusScriptPort() { + PHP_PORT=$(getAnyPort) + PHP_POOL="127.0.0.1:$PHP_PORT" + + #Make the test: + DATA=$(sudo bash "/etc/zabbix/zabbix_php_fpm_status.sh" "$PHP_POOL" "/php-fpm-status") + IS_OK=$(echo "$DATA" | grep -F '{"pool":"') + assertNotNull "Failed to get status from pool $PHP_POOL: $DATA" "$IS_OK" +} + +testZabbixStatusSocket() { + PHP_POOL=$(getAnySocket) + + 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" +} + +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" +} + +testDiscoverScriptReturnsData() { + DATA=$(sudo bash "/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" +} + +# Load shUnit2. +. shunit2 diff --git a/zabbix/userparameter_php_fpm.conf b/zabbix/userparameter_php_fpm.conf index e8a6dc3..24aa8c4 100644 --- a/zabbix/userparameter_php_fpm.conf +++ b/zabbix/userparameter_php_fpm.conf @@ -1,2 +1,2 @@ -UserParameter=php-fpm.discover[*],/etc/zabbix/zabbix_php_fpm_discovery.sh $1 -UserParameter=php-fpm.status[*],/etc/zabbix/zabbix_php_fpm_status.sh $1 $2 +UserParameter=php-fpm.discover[*],sudo /etc/zabbix/zabbix_php_fpm_discovery.sh $1 +UserParameter=php-fpm.status[*],sudo /etc/zabbix/zabbix_php_fpm_status.sh $1 $2 diff --git a/zabbix/zabbix_php_fpm_status.sh b/zabbix/zabbix_php_fpm_status.sh index 5600714..65b7249 100644 --- a/zabbix/zabbix_php_fpm_status.sh +++ b/zabbix/zabbix_php_fpm_status.sh @@ -5,7 +5,6 @@ S_FCGI=$(type -P cgi-fcgi) S_GREP=$(type -P grep) -S_ECHO=$(type -P echo) if [[ ! -f $S_FCGI ]]; then echo "Utility 'cgi-fcgi' not found. Please, install it first." @@ -37,5 +36,5 @@ PHP_STATUS=$( REQUEST_METHOD=GET \ $S_FCGI -bind -connect "$POOL_URL" 2>/dev/null ) -$S_ECHO "$PHP_STATUS" | $S_GREP "{" +echo "$PHP_STATUS" | $S_GREP "{" exit 0 From 930e8f98d15fea9498b795ad38ebe0cd5be7bef3 Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 01:18:44 +0300 Subject: [PATCH 02/18] [add] zabbix discover test --- tests/all.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/all.sh b/tests/all.sh index d186c4a..9a04191 100644 --- a/tests/all.sh +++ b/tests/all.sh @@ -136,5 +136,11 @@ testDiscoverScriptReturnsData() { assertNotNull "Discover script failed: $DATA" "$IS_OK" } +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" +} + # Load shUnit2. . shunit2 From 13e2e3e6e31a531ec0d0ff7c0ea96fbcf3165b32 Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 01:37:20 +0300 Subject: [PATCH 03/18] decrease number of pools --- tests/all.sh | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/tests/all.sh b/tests/all.sh index 9a04191..53fd65e 100644 --- a/tests/all.sh +++ b/tests/all.sh @@ -3,18 +3,23 @@ #https://github.com/rvalitov/zabbix-php-fpm #This script is used for testing +MAX_POOLS=3 +MAX_PORTS=3 + setupPool() { POOL_FILE=$1 POOL_DIR=$(dirname "${POOL_FILE}") PHP_VERSION=$(echo "$POOL_DIR" | grep -oP "(\d\.\d)") + #Delete all active pools except www.conf: + find "$POOL_DIR" -name '*.conf' -type f -not -name 'www.conf' -exec rm -rf {} \; + #Add status path - echo 'pm.status_path = /php-fpm-status' | sudo tee -a "$POOL_FILE" + sudo sed -i 's#;pm.status_path.*#pm.status_path = /php-fpm-status#' "$POOL_FILE" #Set pool manager sudo sed -i 's#pm = dynamic#pm = static#' "$POOL_FILE" #Make copies and create new socket pools - MAX_POOLS=50 for ((c = 1; c <= MAX_POOLS; c++)); do POOL_NAME="socket$c" NEW_POOL_FILE="$POOL_DIR/${POOL_NAME}.conf" @@ -25,7 +30,6 @@ setupPool() { done #Make copies and create HTTP pools - MAX_PORTS=50 #Division on 1 is required to convert from float to integer START_PORT=$(echo "(9000 + $PHP_VERSION * 100)/1" | bc) for ((c = 1; c <= MAX_PORTS; c++)); do @@ -41,6 +45,15 @@ setupPool() { sudo service "php${PHP_VERSION}-fpm" restart } +setupPools() { + PHP_LIST=$(find /etc/php/ -name 'www.conf' -type f) + while IFS= read -r pool; do + if [[ -n $pool ]]; then + setupPool "$pool" + fi + done <<<"$PHP_LIST" +} + getAnySocket() { #Get any socket of PHP-FPM: PHP_FIRST=$(find /etc/php/ -name 'www.conf' -type f | head -n1) @@ -74,12 +87,7 @@ oneTimeSetUp() { echo "Setup PHP-FPM..." #Setup PHP-FPM pools: - PHP_LIST=$(find /etc/php/ -name 'www.conf' -type f) - while IFS= read -r pool; do - if [[ -n $pool ]]; then - setupPool "$pool" - fi - done <<<"$PHP_LIST" + setupPools echo "All done, starting tests..." } From 45dbf7de0b1978533a9eddf70e51464eb2ce2148 Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 01:44:32 +0300 Subject: [PATCH 04/18] [add] similar pool names test --- tests/all.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/all.sh b/tests/all.sh index 53fd65e..fb22e12 100644 --- a/tests/all.sh +++ b/tests/all.sh @@ -54,6 +54,11 @@ setupPools() { done <<<"$PHP_LIST" } +getNumberOfPHPVersions() { + PHP_COUNT=$(find /etc/php/ -name 'www.conf' -type f | wc -l) + echo "$PHP_COUNT" +} + getAnySocket() { #Get any socket of PHP-FPM: PHP_FIRST=$(find /etc/php/ -name 'www.conf' -type f | head -n1) @@ -150,5 +155,21 @@ testZabbixDiscoverReturnsData() { assertNotNull "Discover script failed: $DATA" "$IS_OK" } +testZabbixDiscoverSimilarSocketPoolNames() { + DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"]) + NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"socket1",' | wc -l) + PHP_COUNT=$(getNumberOfPHPVersions) + POOLS=$(echo "($PHP_COUNT * $MAX_POOLS)/1" | bc) + assertEquals "Found $NUMBER_OF_POOLS pools instead of $POOLS" "$NUMBER_OF_POOLS" "$POOLS" +} + +testZabbixDiscoverSimilarHttpPoolNames() { + DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"]) + NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"http1",' | wc -l) + PHP_COUNT=$(getNumberOfPHPVersions) + POOLS=$(echo "($PHP_COUNT * $MAX_POOLS)/1" | bc) + assertEquals "Found $NUMBER_OF_POOLS pools instead of $POOLS" "$NUMBER_OF_POOLS" "$POOLS" +} + # Load shUnit2. . shunit2 From 9cb128461d6c80aed690a1be0629ab0686e9fa1a Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 01:49:57 +0300 Subject: [PATCH 05/18] [fix] pools check --- tests/all.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/all.sh b/tests/all.sh index fb22e12..4f4ec8f 100644 --- a/tests/all.sh +++ b/tests/all.sh @@ -159,16 +159,14 @@ testZabbixDiscoverSimilarSocketPoolNames() { DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"]) NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"socket1",' | wc -l) PHP_COUNT=$(getNumberOfPHPVersions) - POOLS=$(echo "($PHP_COUNT * $MAX_POOLS)/1" | bc) - assertEquals "Found $NUMBER_OF_POOLS pools instead of $POOLS" "$NUMBER_OF_POOLS" "$POOLS" + assertEquals "Number of pools mismatch" "$PHP_COUNT" "$NUMBER_OF_POOLS" } testZabbixDiscoverSimilarHttpPoolNames() { DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"]) NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"http1",' | wc -l) PHP_COUNT=$(getNumberOfPHPVersions) - POOLS=$(echo "($PHP_COUNT * $MAX_POOLS)/1" | bc) - assertEquals "Found $NUMBER_OF_POOLS pools instead of $POOLS" "$NUMBER_OF_POOLS" "$POOLS" + assertEquals "Number of pools mismatch" "$PHP_COUNT" "$NUMBER_OF_POOLS" } # Load shUnit2. From 4784d7828049241e0bd2cd4ec2903d83e112643e Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 02:16:36 +0300 Subject: [PATCH 06/18] [add] discovery debug errors --- tests/all.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/all.sh b/tests/all.sh index 4f4ec8f..b291922 100644 --- a/tests/all.sh +++ b/tests/all.sh @@ -149,6 +149,13 @@ testDiscoverScriptReturnsData() { assertNotNull "Discover script failed: $DATA" "$IS_OK" } +testDiscoverScriptDebug() { + DATA=$(sudo bash "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "/php-fpm-status") + ERRORS_LIST=$(echo "$DATA" | grep -F 'Error:') + assertNull "Discover script errors: $ERRORS_LIST" "$ERRORS_LIST" + +} + 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}"') From 21e4630738a13463d29e71cdd4bcb3619c4b5a8b Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 02:37:13 +0300 Subject: [PATCH 07/18] [add] extra output --- zabbix/zabbix_php_fpm_discovery.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/zabbix/zabbix_php_fpm_discovery.sh b/zabbix/zabbix_php_fpm_discovery.sh index 8f18bc8..53d763e 100644 --- a/zabbix/zabbix_php_fpm_discovery.sh +++ b/zabbix/zabbix_php_fpm_discovery.sh @@ -133,6 +133,7 @@ function ProcessPool() { POOL_NAME=$1 POOL_SOCKET=$2 if [[ -z ${POOL_NAME} ]] || [[ -z ${POOL_SOCKET} ]]; then + PrintDebug "Invalid arguments for ProcessPool" return 0 fi From 8106d57ebcd0217e26fa6f08f40182fa90d551e7 Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 02:43:29 +0300 Subject: [PATCH 08/18] [add] full output --- tests/all.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/all.sh b/tests/all.sh index b291922..c9e5e2e 100644 --- a/tests/all.sh +++ b/tests/all.sh @@ -152,7 +152,7 @@ testDiscoverScriptReturnsData() { testDiscoverScriptDebug() { DATA=$(sudo bash "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "/php-fpm-status") ERRORS_LIST=$(echo "$DATA" | grep -F 'Error:') - assertNull "Discover script errors: $ERRORS_LIST" "$ERRORS_LIST" + assertNull "Discover script errors: $DATA" "$ERRORS_LIST" } From d8eb7a3de5423c0c29bcc4030675fc7be82ff68a Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 02:52:57 +0300 Subject: [PATCH 09/18] [add] json --- zabbix/zabbix_php_fpm_discovery.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/zabbix/zabbix_php_fpm_discovery.sh b/zabbix/zabbix_php_fpm_discovery.sh index 53d763e..35c4322 100644 --- a/zabbix/zabbix_php_fpm_discovery.sh +++ b/zabbix/zabbix_php_fpm_discovery.sh @@ -163,6 +163,7 @@ function ProcessPool() { fi PrintDebug "Failed to validate status data for pool $POOL_NAME, socket $POOL_SOCKET, status path $STATUS_PATH" + PrintDebug "Status script returned: $STATUS_JSON" if [[ -n ${STATUS_JSON} ]]; then PrintDebug "Status script returned: $STATUS_JSON" fi From e2264c575a9b1acfd60ebaf2191aeb6ad2ab4433 Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 03:02:55 +0300 Subject: [PATCH 10/18] [del] localhost --- tests/all.sh | 2 +- zabbix/zabbix_php_fpm_discovery.sh | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/all.sh b/tests/all.sh index c9e5e2e..3a29aa5 100644 --- a/tests/all.sh +++ b/tests/all.sh @@ -38,7 +38,7 @@ setupPool() { NEW_POOL_FILE="$POOL_DIR/${POOL_NAME}.conf" sudo cp "$POOL_FILE" "$NEW_POOL_FILE" - sudo sed -i "s#listen =.*#listen = 127.0.0.1:$POOL_PORT#" "$NEW_POOL_FILE" + sudo sed -i "s#listen =.*#listen = $POOL_PORT#" "$NEW_POOL_FILE" sudo sed -i "s#\[www\]#[$POOL_NAME]#" "$NEW_POOL_FILE" done diff --git a/zabbix/zabbix_php_fpm_discovery.sh b/zabbix/zabbix_php_fpm_discovery.sh index 35c4322..53d763e 100644 --- a/zabbix/zabbix_php_fpm_discovery.sh +++ b/zabbix/zabbix_php_fpm_discovery.sh @@ -163,7 +163,6 @@ function ProcessPool() { fi PrintDebug "Failed to validate status data for pool $POOL_NAME, socket $POOL_SOCKET, status path $STATUS_PATH" - PrintDebug "Status script returned: $STATUS_JSON" if [[ -n ${STATUS_JSON} ]]; then PrintDebug "Status script returned: $STATUS_JSON" fi From 27d55ad8119196a8bd59621421b57e9523baa24a Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 12:42:42 +0300 Subject: [PATCH 11/18] [add] tests for localhost --- tests/all.sh | 67 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/tests/all.sh b/tests/all.sh index 3a29aa5..e4842d8 100644 --- a/tests/all.sh +++ b/tests/all.sh @@ -5,6 +5,28 @@ MAX_POOLS=3 MAX_PORTS=3 +MIN_PORT=9000 + +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)") + + 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" +} setupPool() { POOL_FILE=$1 @@ -19,29 +41,28 @@ setupPool() { #Set pool manager sudo sed -i 's#pm = dynamic#pm = static#' "$POOL_FILE" - #Make copies and create new socket pools + #Create new socket pools for ((c = 1; c <= MAX_POOLS; c++)); do POOL_NAME="socket$c" - NEW_POOL_FILE="$POOL_DIR/${POOL_NAME}.conf" - sudo cp "$POOL_FILE" "$NEW_POOL_FILE" - - sudo sed -i "s#listen =.*#listen = /run/php/php${PHP_VERSION}-fpm-${POOL_NAME}.sock#" "$NEW_POOL_FILE" - sudo sed -i "s#\[www\]#[$POOL_NAME]#" "$NEW_POOL_FILE" + POOL_SOCKET="/run/php/php${PHP_VERSION}-fpm-${POOL_NAME}.sock" + copyPool "$POOL_FILE" "$POOL_NAME" "$POOL_SOCKET" "static" done - #Make copies and create HTTP pools + #Create TCP port based pools #Division on 1 is required to convert from float to integer - START_PORT=$(echo "(9000 + $PHP_VERSION * 100)/1" | bc) + START_PORT=$(echo "($MIN_PORT + $PHP_VERSION * 100 + 1)/1" | bc) for ((c = 1; c <= MAX_PORTS; c++)); do - POOL_NAME="http$c" + POOL_NAME="port$c" POOL_PORT=$(echo "($START_PORT + $c)/1" | bc) - NEW_POOL_FILE="$POOL_DIR/${POOL_NAME}.conf" - sudo cp "$POOL_FILE" "$NEW_POOL_FILE" - - sudo sed -i "s#listen =.*#listen = $POOL_PORT#" "$NEW_POOL_FILE" - sudo sed -i "s#\[www\]#[$POOL_NAME]#" "$NEW_POOL_FILE" + copyPool "$POOL_FILE" "$POOL_NAME" "$POOL_PORT" "static" done + #Create TCP IPv4 localhost pool + POOL_NAME="localhost" + POOL_PORT=$(echo "($MIN_PORT + $PHP_VERSION * 100)/1" | bc) + POOL_SOCKET="127.0.0.1:$POOL_PORT" + copyPool "$POOL_FILE" "$POOL_NAME" "$POOL_SOCKET" "static" + sudo service "php${PHP_VERSION}-fpm" restart } @@ -114,6 +135,7 @@ testStatusScriptSocket() { DATA=$(sudo bash "/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" } testStatusScriptPort() { @@ -124,6 +146,7 @@ testStatusScriptPort() { DATA=$(sudo bash "/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" } testZabbixStatusSocket() { @@ -132,6 +155,7 @@ testZabbixStatusSocket() { 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" } testZabbixStatusPort() { @@ -141,6 +165,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":"') assertNotNull "Failed to get status from pool $PHP_POOL: $DATA" "$IS_OK" + echo "Success test of $PHP_POOL" } testDiscoverScriptReturnsData() { @@ -152,8 +177,7 @@ testDiscoverScriptReturnsData() { testDiscoverScriptDebug() { DATA=$(sudo bash "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "/php-fpm-status") ERRORS_LIST=$(echo "$DATA" | grep -F 'Error:') - assertNull "Discover script errors: $DATA" "$ERRORS_LIST" - + assertNull "Discover script errors: $ERRORS_LIST" "$ERRORS_LIST" } testZabbixDiscoverReturnsData() { @@ -162,14 +186,21 @@ testZabbixDiscoverReturnsData() { assertNotNull "Discover script failed: $DATA" "$IS_OK" } -testZabbixDiscoverSimilarSocketPoolNames() { +testZabbixDiscoverNumberOfSocketPools() { DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"]) NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"socket1",' | wc -l) PHP_COUNT=$(getNumberOfPHPVersions) assertEquals "Number of pools mismatch" "$PHP_COUNT" "$NUMBER_OF_POOLS" } -testZabbixDiscoverSimilarHttpPoolNames() { +testZabbixDiscoverNumberOfIPPools() { + DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"]) + NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"localhost",' | wc -l) + PHP_COUNT=$(getNumberOfPHPVersions) + assertEquals "Number of pools mismatch" "$PHP_COUNT" "$NUMBER_OF_POOLS" +} + +testZabbixDiscoverNumberOfPortPools() { DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"]) NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"http1",' | wc -l) PHP_COUNT=$(getNumberOfPHPVersions) From 9aae10855b4e17c4976593fc4c79aacc1eda6c6c Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 14:32:31 +0300 Subject: [PATCH 12/18] [fix] #35 analyze all pools --- zabbix/zabbix_php_fpm_discovery.sh | 115 ++++++++++++++++------------- 1 file changed, 63 insertions(+), 52 deletions(-) diff --git a/zabbix/zabbix_php_fpm_discovery.sh b/zabbix/zabbix_php_fpm_discovery.sh index 53d763e..2c145bf 100644 --- a/zabbix/zabbix_php_fpm_discovery.sh +++ b/zabbix/zabbix_php_fpm_discovery.sh @@ -7,6 +7,7 @@ S_PS=$(type -P ps) S_GREP=$(type -P grep) S_AWK=$(type -P awk) S_SORT=$(type -P sort) +S_UNIQ=$(type -P uniq) S_HEAD=$(type -P head) S_LSOF=$(type -P lsof) S_JQ=$(type -P jq) @@ -33,6 +34,10 @@ if [[ ! -f $S_SORT ]]; then ${S_ECHO} "Utility 'sort' not found. Please, install it first." exit 1 fi +if [[ ! -f $S_UNIQ ]]; then + ${S_ECHO} "Utility 'uniq' not found. Please, install it first." + exit 1 +fi if [[ ! -f $S_HEAD ]]; then ${S_ECHO} "Utility 'head' not found. Please, install it first." exit 1 @@ -216,14 +221,21 @@ fi mapfile -t PS_LIST < <($S_PS ax | $S_GREP -F "php-fpm: pool " | $S_GREP -F -v "grep") # shellcheck disable=SC2016 -POOL_LIST=$(${S_PRINTF} '%s\n' "${PS_LIST[@]}" | $S_AWK '{print $NF}' | $S_SORT -u) +POOL_NAMES_LIST=$(${S_PRINTF} '%s\n' "${PS_LIST[@]}" | $S_AWK '{print $NF}' | $S_SORT -u) POOL_FIRST=0 #We store the resulting JSON data for Zabbix in the following var: RESULT_DATA="{\"data\":[" while IFS= read -r line; do # shellcheck disable=SC2016 - POOL_PID=$(${S_PRINTF} '%s\n' "${PS_LIST[@]}" | $S_GREP -F -w "php-fpm: pool $line" | $S_HEAD -1 | $S_AWK '{print $1}') - if [[ -n $POOL_PID ]]; then + POOL_PID_LIST=$(${S_PRINTF} '%s\n' "${PS_LIST[@]}" | $S_GREP -F -w "php-fpm: pool $line" | $S_AWK '{print $1}') + POOL_PID_ARGS="" + while IFS= read -r POOL_PID; do + if [[ -n $POOL_PID ]]; then + POOL_PID_ARGS="$POOL_PID_ARGS -p $POOL_PID" + fi + done <<<"$POOL_PID_LIST" + + if [[ -n $POOL_PID_ARGS ]]; then #We search for socket or IP address and port #Socket example: #php-fpm7. 25897 root 9u unix 0x000000006509e31f 0t0 58381847 /run/php/php7.3-fpm.sock type=STREAM @@ -236,84 +248,83 @@ while IFS= read -r line; do #php-fpm7. 1203 www-data 8u IPv4 15070917 0t0 TCP localhost.localdomain:23054->localhost.localdomain:postgresql (ESTABLISHED) #More info at https://github.com/rvalitov/zabbix-php-fpm/issues/12 - PrintDebug "Started analysis of pool $line, PID $POOL_PID" + PrintDebug "Started analysis of pool $line, PID(s): $POOL_PID_ARGS" #Extract only important information: #Use -P to show port number instead of port name, see https://github.com/rvalitov/zabbix-php-fpm/issues/24 - POOL_PARAMS_LIST=$($S_LSOF -P -p "$POOL_PID" 2>/dev/null | $S_GREP -w -e "unix" -e "TCP") + #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 + POOL_PARAMS_LIST=$($S_LSOF -P $POOL_PID_ARGS 2>/dev/null | $S_GREP -w -e "unix" -e "TCP" | $S_SORT -u | $S_UNIQ -f8) FOUND_POOL="" while IFS= read -r pool; do if [[ -n $pool ]]; then - if [[ -z $FOUND_POOL ]]; then - PrintDebug "Checking process: $pool" - # shellcheck disable=SC2016 - POOL_TYPE=$(${S_ECHO} "${pool}" | $S_AWK '{print $5}') - # shellcheck disable=SC2016 - POOL_SOCKET=$(${S_ECHO} "${pool}" | $S_AWK '{print $9}') - if [[ -n $POOL_TYPE ]] && [[ -n $POOL_SOCKET ]]; then - if [[ $POOL_TYPE == "unix" ]]; then - #We have a socket here, test if it's actually a socket: - if [[ -S $POOL_SOCKET ]]; then - PrintDebug "Found socket $POOL_SOCKET" + PrintDebug "Checking process: $pool" + # shellcheck disable=SC2016 + POOL_TYPE=$(${S_ECHO} "${pool}" | $S_AWK '{print $5}') + # shellcheck disable=SC2016 + POOL_SOCKET=$(${S_ECHO} "${pool}" | $S_AWK '{print $9}') + if [[ -n $POOL_TYPE ]] && [[ -n $POOL_SOCKET ]]; then + if [[ $POOL_TYPE == "unix" ]]; then + #We have a socket here, test if it's actually a socket: + if [[ -S $POOL_SOCKET ]]; then + PrintDebug "Found socket $POOL_SOCKET" + ProcessPool "${line}" "${POOL_SOCKET}" + POOL_STATUS=$? + if [[ ${POOL_STATUS} -gt 0 ]]; then + FOUND_POOL="1" + PrintDebug "Success: socket $POOL_SOCKET returned valid status data" + EncodeToJson "${line}" "${POOL_SOCKET}" + else + PrintDebug "Error: socket $POOL_SOCKET didn't return valid data" + fi + else + PrintDebug "Error: specified socket $POOL_SOCKET is not valid" + fi + elif [[ $POOL_TYPE == "IPv4" ]] || [[ $POOL_TYPE == "IPv6" ]]; then + #We have a TCP connection here, check it: + # shellcheck disable=SC2016 + CONNECTION_TYPE=$(${S_ECHO} "${pool}" | $S_AWK '{print $8}') + if [[ $CONNECTION_TYPE == "TCP" ]]; then + #The connection must have state LISTEN: + LISTEN=$(${S_ECHO} "${pool}" | $S_GREP -F -w "(LISTEN)") + if [[ -n $LISTEN ]]; then + #Check and replace * to localhost if it's found. Asterisk means that the PHP listens on + #all interfaces. + POOL_SOCKET=$(${S_ECHO} -n "${POOL_SOCKET/*:/localhost:}") + PrintDebug "Found TCP connection $POOL_SOCKET" ProcessPool "${line}" "${POOL_SOCKET}" POOL_STATUS=$? if [[ ${POOL_STATUS} -gt 0 ]]; then FOUND_POOL="1" - PrintDebug "Success: socket $POOL_SOCKET returned valid status data" + PrintDebug "Success: TCP connection $POOL_SOCKET returned valid status data" + EncodeToJson "${line}" "${POOL_SOCKET}" else - PrintDebug "Error: socket $POOL_SOCKET didn't return valid data" + PrintDebug "Error: TCP connection $POOL_SOCKET didn't return valid data" fi else - PrintDebug "Error: specified socket $POOL_SOCKET is not valid" - fi - elif [[ $POOL_TYPE == "IPv4" ]] || [[ $POOL_TYPE == "IPv6" ]]; then - #We have a TCP connection here, check it: - # shellcheck disable=SC2016 - CONNECTION_TYPE=$(${S_ECHO} "${pool}" | $S_AWK '{print $8}') - if [[ $CONNECTION_TYPE == "TCP" ]]; then - #The connection must have state LISTEN: - LISTEN=$(${S_ECHO} "${pool}" | $S_GREP -F -w "(LISTEN)") - if [[ -n $LISTEN ]]; then - #Check and replace * to localhost if it's found. Asterisk means that the PHP listens on - #all interfaces. - POOL_SOCKET=$(${S_ECHO} -n "${POOL_SOCKET/*:/localhost:}") - PrintDebug "Found TCP connection $POOL_SOCKET" - ProcessPool "${line}" "${POOL_SOCKET}" - POOL_STATUS=$? - if [[ ${POOL_STATUS} -gt 0 ]]; then - FOUND_POOL="1" - PrintDebug "Success: TCP connection $POOL_SOCKET returned valid status data" - else - PrintDebug "Error: TCP connection $POOL_SOCKET didn't return valid data" - fi - else - PrintDebug "Warning: expected connection state must be LISTEN, but it was not detected" - fi - else - PrintDebug "Warning: expected connection type is TCP, but found $CONNECTION_TYPE" + PrintDebug "Warning: expected connection state must be LISTEN, but it was not detected" fi else - PrintDebug "Unsupported type $POOL_TYPE, skipping" + PrintDebug "Warning: expected connection type is TCP, but found $CONNECTION_TYPE" fi else - PrintDebug "Warning: pool type or socket is empty" + PrintDebug "Unsupported type $POOL_TYPE, skipping" fi else - PrintDebug "Pool already found, skipping process: $pool" + PrintDebug "Warning: pool type or socket is empty" fi else PrintDebug "Error: failed to get process information. Probably insufficient privileges. Use sudo or run this script under root." fi done <<<"$POOL_PARAMS_LIST" - if [[ -n ${FOUND_POOL} ]]; then - EncodeToJson "${line}" "${POOL_SOCKET}" - else + if [[ -z ${FOUND_POOL} ]]; then PrintDebug "Error: failed to discover information for pool $line" fi else PrintDebug "Error: failed to find PID for pool $line" fi -done <<<"$POOL_LIST" +done <<<"$POOL_NAMES_LIST" PrintDebug "Processing pools from old cache..." for CACHE_ITEM in "${CACHE[@]}"; do From 388eef5b61d5be5296a6dfebb5b3af54339f8091 Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 14:58:32 +0300 Subject: [PATCH 13/18] [fix] port pools test --- tests/all.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/all.sh b/tests/all.sh index e4842d8..91e8853 100644 --- a/tests/all.sh +++ b/tests/all.sh @@ -202,7 +202,7 @@ testZabbixDiscoverNumberOfIPPools() { testZabbixDiscoverNumberOfPortPools() { DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"]) - NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"http1",' | wc -l) + NUMBER_OF_POOLS=$(echo "$DATA" | grep -o -F '{"{#POOLNAME}":"port1",' | wc -l) PHP_COUNT=$(getNumberOfPHPVersions) assertEquals "Number of pools mismatch" "$PHP_COUNT" "$NUMBER_OF_POOLS" } From 94f3af4599f9415f5a764c2443da820d927a5f64 Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 15:28:33 +0300 Subject: [PATCH 14/18] [add] show host info --- tests/all.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/all.sh b/tests/all.sh index 91e8853..f468518 100644 --- a/tests/all.sh +++ b/tests/all.sh @@ -98,6 +98,11 @@ getAnyPort() { } oneTimeSetUp() { + echo "Host info:" + nslookup localhost + sudo ifconfig + sudo cat /etc/hosts + echo "Copying Zabbix files..." #Install files: sudo cp "$TRAVIS_BUILD_DIR/zabbix/zabbix_php_fpm_discovery.sh" "/etc/zabbix" From 6ac9292c0ae093bdfe549c6b4459c137a161c3b7 Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 15:40:20 +0300 Subject: [PATCH 15/18] [fix] localhost name resolution --- zabbix/zabbix_php_fpm_discovery.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zabbix/zabbix_php_fpm_discovery.sh b/zabbix/zabbix_php_fpm_discovery.sh index 2c145bf..b2806a5 100644 --- a/zabbix/zabbix_php_fpm_discovery.sh +++ b/zabbix/zabbix_php_fpm_discovery.sh @@ -251,10 +251,11 @@ while IFS= read -r line; do PrintDebug "Started analysis of pool $line, PID(s): $POOL_PID_ARGS" #Extract only important information: #Use -P to show port number instead of port name, see https://github.com/rvalitov/zabbix-php-fpm/issues/24 + #Use -n flag to show IP address and not convert it to domain name (like localhost) #Sometimes different PHP-FPM versions may have the same names of pools, so we need to consider that. # It's considered that a pair of pool name and socket must be unique. #Sorting is required, because uniq needs it - POOL_PARAMS_LIST=$($S_LSOF -P $POOL_PID_ARGS 2>/dev/null | $S_GREP -w -e "unix" -e "TCP" | $S_SORT -u | $S_UNIQ -f8) + 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="" while IFS= read -r pool; do if [[ -n $pool ]]; then From 24153d7d496f569e8eec5a8acafd8956538dcce2 Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 15:44:33 +0300 Subject: [PATCH 16/18] display full log --- tests/all.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/all.sh b/tests/all.sh index f468518..8bc9a7c 100644 --- a/tests/all.sh +++ b/tests/all.sh @@ -182,7 +182,7 @@ testDiscoverScriptReturnsData() { testDiscoverScriptDebug() { DATA=$(sudo bash "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "/php-fpm-status") ERRORS_LIST=$(echo "$DATA" | grep -F 'Error:') - assertNull "Discover script errors: $ERRORS_LIST" "$ERRORS_LIST" + assertNull "Discover script errors: $DATA" "$ERRORS_LIST" } testZabbixDiscoverReturnsData() { From 8d642e70cbf11c3ed6845318e3c78e1d9c3c177c Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 15:53:22 +0300 Subject: [PATCH 17/18] [add] info for IP processing --- zabbix/zabbix_php_fpm_discovery.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zabbix/zabbix_php_fpm_discovery.sh b/zabbix/zabbix_php_fpm_discovery.sh index b2806a5..6dcddb5 100644 --- a/zabbix/zabbix_php_fpm_discovery.sh +++ b/zabbix/zabbix_php_fpm_discovery.sh @@ -291,8 +291,9 @@ while IFS= read -r line; do if [[ -n $LISTEN ]]; then #Check and replace * to localhost if it's found. Asterisk means that the PHP listens on #all interfaces. - POOL_SOCKET=$(${S_ECHO} -n "${POOL_SOCKET/*:/localhost:}") PrintDebug "Found TCP connection $POOL_SOCKET" + POOL_SOCKET=${POOL_SOCKET//*:/localhost:} + PrintDebug "Processed TCP connection $POOL_SOCKET" ProcessPool "${line}" "${POOL_SOCKET}" POOL_STATUS=$? if [[ ${POOL_STATUS} -gt 0 ]]; then From 54db0b85bf9569e2cd10ac292844cb40c4e576c5 Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 16:02:20 +0300 Subject: [PATCH 18/18] [fix] IP processing --- zabbix/zabbix_php_fpm_discovery.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zabbix/zabbix_php_fpm_discovery.sh b/zabbix/zabbix_php_fpm_discovery.sh index 6dcddb5..b322a61 100644 --- a/zabbix/zabbix_php_fpm_discovery.sh +++ b/zabbix/zabbix_php_fpm_discovery.sh @@ -292,7 +292,7 @@ while IFS= read -r line; do #Check and replace * to localhost if it's found. Asterisk means that the PHP listens on #all interfaces. PrintDebug "Found TCP connection $POOL_SOCKET" - POOL_SOCKET=${POOL_SOCKET//*:/localhost:} + POOL_SOCKET=${POOL_SOCKET/\*:/localhost:} PrintDebug "Processed TCP connection $POOL_SOCKET" ProcessPool "${line}" "${POOL_SOCKET}" POOL_STATUS=$?