mirror of
https://github.com/rvalitov/zabbix-php-fpm.git
synced 2023-11-05 03:30:27 +01:00
Merge branch 'bug/duplicate-names' into feature/timeout
This commit is contained in:
commit
5d96c99765
117
.travis.yml
Normal file
117
.travis.yml
Normal file
@ -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
|
@ -1,6 +1,6 @@
|
|||||||
# PHP-FPM Zabbix Template with Auto Discovery and Multiple Pools
|
# 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)
|
![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
|
- CPU utilization
|
||||||
- Memory utilization
|
- Memory utilization
|
||||||
- Queue
|
- Queue
|
||||||
- Max children riched
|
- Max children reached
|
||||||
|
|
||||||
![Zabbix screens example](https://github.com/rvalitov/zabbix-php-fpm/wiki/media/zabbix-screens.jpg)
|
![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.
|
Can work with any version of ISPConfig as long as you have a valid PHP-FPM status page configuration there.
|
||||||
|
|
||||||
Tested with:
|
Tested with:
|
||||||
|
- [Travis CI](https://travis-ci.com/rvalitov/zabbix-php-fpm)
|
||||||
- PHP 7.2, 7.3, 7.4
|
- PHP 7.2, 7.3, 7.4
|
||||||
- Zabbix 4.0.4, 4.0.16, 4.0.20, 4.2.5, 4.4.4
|
- Zabbix 4.0.4, 4.0.16, 4.0.20, 4.2.5, 4.4.4
|
||||||
- Debian 9, 10
|
- Debian 9, 10
|
||||||
|
- Ubuntu 18
|
||||||
- CentOS 7
|
- CentOS 7
|
||||||
- ISPConfig v.3.1.14p2
|
- ISPConfig v.3.1.14p2
|
216
tests/all.sh
Normal file
216
tests/all.sh
Normal file
@ -0,0 +1,216 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
#Ramil Valitov ramilvalitov@gmail.com
|
||||||
|
#https://github.com/rvalitov/zabbix-php-fpm
|
||||||
|
#This script is used for testing
|
||||||
|
|
||||||
|
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
|
||||||
|
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
|
||||||
|
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"
|
||||||
|
|
||||||
|
#Create new socket pools
|
||||||
|
for ((c = 1; c <= MAX_POOLS; c++)); do
|
||||||
|
POOL_NAME="socket$c"
|
||||||
|
POOL_SOCKET="/run/php/php${PHP_VERSION}-fpm-${POOL_NAME}.sock"
|
||||||
|
copyPool "$POOL_FILE" "$POOL_NAME" "$POOL_SOCKET" "static"
|
||||||
|
done
|
||||||
|
|
||||||
|
#Create TCP port based pools
|
||||||
|
#Division on 1 is required to convert from float to integer
|
||||||
|
START_PORT=$(echo "($MIN_PORT + $PHP_VERSION * 100 + 1)/1" | bc)
|
||||||
|
for ((c = 1; c <= MAX_PORTS; c++)); do
|
||||||
|
POOL_NAME="port$c"
|
||||||
|
POOL_PORT=$(echo "($START_PORT + $c)/1" | bc)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
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 "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"
|
||||||
|
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:
|
||||||
|
setupPools
|
||||||
|
|
||||||
|
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"
|
||||||
|
echo "Success test of $PHP_POOL"
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
||||||
|
echo "Success test of $PHP_POOL"
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
||||||
|
echo "Success test of $PHP_POOL"
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
||||||
|
echo "Success test of $PHP_POOL"
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
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}":"port1",' | wc -l)
|
||||||
|
PHP_COUNT=$(getNumberOfPHPVersions)
|
||||||
|
assertEquals "Number of pools mismatch" "$PHP_COUNT" "$NUMBER_OF_POOLS"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Load shUnit2.
|
||||||
|
. shunit2
|
@ -1,2 +1,2 @@
|
|||||||
UserParameter=php-fpm.discover[*],/etc/zabbix/zabbix_php_fpm_discovery.sh $1
|
UserParameter=php-fpm.discover[*],sudo /etc/zabbix/zabbix_php_fpm_discovery.sh $1
|
||||||
UserParameter=php-fpm.status[*],/etc/zabbix/zabbix_php_fpm_status.sh $1 $2
|
UserParameter=php-fpm.status[*],sudo /etc/zabbix/zabbix_php_fpm_status.sh $1 $2
|
||||||
|
@ -45,6 +45,7 @@ S_PS=$(type -P ps)
|
|||||||
S_GREP=$(type -P grep)
|
S_GREP=$(type -P grep)
|
||||||
S_AWK=$(type -P awk)
|
S_AWK=$(type -P awk)
|
||||||
S_SORT=$(type -P sort)
|
S_SORT=$(type -P sort)
|
||||||
|
S_UNIQ=$(type -P uniq)
|
||||||
S_HEAD=$(type -P head)
|
S_HEAD=$(type -P head)
|
||||||
S_LSOF=$(type -P lsof)
|
S_LSOF=$(type -P lsof)
|
||||||
S_JQ=$(type -P jq)
|
S_JQ=$(type -P jq)
|
||||||
@ -71,6 +72,10 @@ if [[ ! -f $S_SORT ]]; then
|
|||||||
${S_ECHO} "Utility 'sort' not found. Please, install it first."
|
${S_ECHO} "Utility 'sort' not found. Please, install it first."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
if [[ ! -f $S_UNIQ ]]; then
|
||||||
|
${S_ECHO} "Utility 'uniq' not found. Please, install it first."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
if [[ ! -f $S_HEAD ]]; then
|
if [[ ! -f $S_HEAD ]]; then
|
||||||
${S_ECHO} "Utility 'head' not found. Please, install it first."
|
${S_ECHO} "Utility 'head' not found. Please, install it first."
|
||||||
exit 1
|
exit 1
|
||||||
@ -178,6 +183,7 @@ function ProcessPool() {
|
|||||||
POOL_NAME=$1
|
POOL_NAME=$1
|
||||||
POOL_SOCKET=$2
|
POOL_SOCKET=$2
|
||||||
if [[ -z ${POOL_NAME} ]] || [[ -z ${POOL_SOCKET} ]]; then
|
if [[ -z ${POOL_NAME} ]] || [[ -z ${POOL_SOCKET} ]]; then
|
||||||
|
PrintDebug "Invalid arguments for ProcessPool"
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -266,14 +272,21 @@ fi
|
|||||||
|
|
||||||
mapfile -t PS_LIST < <($S_PS ax | $S_GREP -F "php-fpm: pool " | $S_GREP -F -v "grep")
|
mapfile -t PS_LIST < <($S_PS ax | $S_GREP -F "php-fpm: pool " | $S_GREP -F -v "grep")
|
||||||
# shellcheck disable=SC2016
|
# 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
|
POOL_FIRST=0
|
||||||
#We store the resulting JSON data for Zabbix in the following var:
|
#We store the resulting JSON data for Zabbix in the following var:
|
||||||
RESULT_DATA="{\"data\":["
|
RESULT_DATA="{\"data\":["
|
||||||
while IFS= read -r line; do
|
while IFS= read -r line; do
|
||||||
# shellcheck disable=SC2016
|
# 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}')
|
POOL_PID_LIST=$(${S_PRINTF} '%s\n' "${PS_LIST[@]}" | $S_GREP -F -w "php-fpm: pool $line" | $S_AWK '{print $1}')
|
||||||
if [[ -n $POOL_PID ]]; then
|
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
|
#We search for socket or IP address and port
|
||||||
#Socket example:
|
#Socket example:
|
||||||
#php-fpm7. 25897 root 9u unix 0x000000006509e31f 0t0 58381847 /run/php/php7.3-fpm.sock type=STREAM
|
#php-fpm7. 25897 root 9u unix 0x000000006509e31f 0t0 58381847 /run/php/php7.3-fpm.sock type=STREAM
|
||||||
@ -286,84 +299,85 @@ while IFS= read -r line; do
|
|||||||
#php-fpm7. 1203 www-data 8u IPv4 15070917 0t0 TCP localhost.localdomain:23054->localhost.localdomain:postgresql (ESTABLISHED)
|
#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
|
#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:
|
#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 -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")
|
#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 -n -P $POOL_PID_ARGS 2>/dev/null | $S_GREP -w -e "unix" -e "TCP" | $S_SORT -u | $S_UNIQ -f8)
|
||||||
FOUND_POOL=""
|
FOUND_POOL=""
|
||||||
while IFS= read -r pool; do
|
while IFS= read -r pool; do
|
||||||
if [[ -n $pool ]]; then
|
if [[ -n $pool ]]; then
|
||||||
if [[ -z $FOUND_POOL ]]; then
|
PrintDebug "Checking process: $pool"
|
||||||
PrintDebug "Checking process: $pool"
|
# shellcheck disable=SC2016
|
||||||
# shellcheck disable=SC2016
|
POOL_TYPE=$(${S_ECHO} "${pool}" | $S_AWK '{print $5}')
|
||||||
POOL_TYPE=$(${S_ECHO} "${pool}" | $S_AWK '{print $5}')
|
# shellcheck disable=SC2016
|
||||||
# shellcheck disable=SC2016
|
POOL_SOCKET=$(${S_ECHO} "${pool}" | $S_AWK '{print $9}')
|
||||||
POOL_SOCKET=$(${S_ECHO} "${pool}" | $S_AWK '{print $9}')
|
if [[ -n $POOL_TYPE ]] && [[ -n $POOL_SOCKET ]]; then
|
||||||
if [[ -n $POOL_TYPE ]] && [[ -n $POOL_SOCKET ]]; then
|
if [[ $POOL_TYPE == "unix" ]]; then
|
||||||
if [[ $POOL_TYPE == "unix" ]]; then
|
#We have a socket here, test if it's actually a socket:
|
||||||
#We have a socket here, test if it's actually a socket:
|
if [[ -S $POOL_SOCKET ]]; then
|
||||||
if [[ -S $POOL_SOCKET ]]; then
|
PrintDebug "Found socket $POOL_SOCKET"
|
||||||
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.
|
||||||
|
PrintDebug "Found TCP connection $POOL_SOCKET"
|
||||||
|
POOL_SOCKET=${POOL_SOCKET/\*:/localhost:}
|
||||||
|
PrintDebug "Processed TCP connection $POOL_SOCKET"
|
||||||
ProcessPool "${line}" "${POOL_SOCKET}"
|
ProcessPool "${line}" "${POOL_SOCKET}"
|
||||||
POOL_STATUS=$?
|
POOL_STATUS=$?
|
||||||
if [[ ${POOL_STATUS} -gt 0 ]]; then
|
if [[ ${POOL_STATUS} -gt 0 ]]; then
|
||||||
FOUND_POOL="1"
|
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
|
else
|
||||||
PrintDebug "Error: socket $POOL_SOCKET didn't return valid data"
|
PrintDebug "Error: TCP connection $POOL_SOCKET didn't return valid data"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
PrintDebug "Error: specified socket $POOL_SOCKET is not valid"
|
PrintDebug "Warning: expected connection state must be LISTEN, but it was not detected"
|
||||||
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"
|
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
PrintDebug "Unsupported type $POOL_TYPE, skipping"
|
PrintDebug "Warning: expected connection type is TCP, but found $CONNECTION_TYPE"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
PrintDebug "Warning: pool type or socket is empty"
|
PrintDebug "Unsupported type $POOL_TYPE, skipping"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
PrintDebug "Pool already found, skipping process: $pool"
|
PrintDebug "Warning: pool type or socket is empty"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
PrintDebug "Error: failed to get process information. Probably insufficient privileges. Use sudo or run this script under root."
|
PrintDebug "Error: failed to get process information. Probably insufficient privileges. Use sudo or run this script under root."
|
||||||
fi
|
fi
|
||||||
done <<<"$POOL_PARAMS_LIST"
|
done <<<"$POOL_PARAMS_LIST"
|
||||||
|
|
||||||
if [[ -n ${FOUND_POOL} ]]; then
|
if [[ -z ${FOUND_POOL} ]]; then
|
||||||
EncodeToJson "${line}" "${POOL_SOCKET}"
|
|
||||||
else
|
|
||||||
PrintDebug "Error: failed to discover information for pool $line"
|
PrintDebug "Error: failed to discover information for pool $line"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
PrintDebug "Error: failed to find PID for pool $line"
|
PrintDebug "Error: failed to find PID for pool $line"
|
||||||
fi
|
fi
|
||||||
done <<<"$POOL_LIST"
|
done <<<"$POOL_NAMES_LIST"
|
||||||
|
|
||||||
PrintDebug "Processing pools from old cache..."
|
PrintDebug "Processing pools from old cache..."
|
||||||
for CACHE_ITEM in "${CACHE[@]}"; do
|
for CACHE_ITEM in "${CACHE[@]}"; do
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
S_FCGI=$(type -P cgi-fcgi)
|
S_FCGI=$(type -P cgi-fcgi)
|
||||||
S_GREP=$(type -P grep)
|
S_GREP=$(type -P grep)
|
||||||
S_ECHO=$(type -P echo)
|
|
||||||
|
|
||||||
if [[ ! -f $S_FCGI ]]; then
|
if [[ ! -f $S_FCGI ]]; then
|
||||||
echo "Utility 'cgi-fcgi' not found. Please, install it first."
|
echo "Utility 'cgi-fcgi' not found. Please, install it first."
|
||||||
@ -37,5 +36,5 @@ PHP_STATUS=$(
|
|||||||
REQUEST_METHOD=GET \
|
REQUEST_METHOD=GET \
|
||||||
$S_FCGI -bind -connect "$POOL_URL" 2>/dev/null
|
$S_FCGI -bind -connect "$POOL_URL" 2>/dev/null
|
||||||
)
|
)
|
||||||
$S_ECHO "$PHP_STATUS" | $S_GREP "{"
|
echo "$PHP_STATUS" | $S_GREP "{"
|
||||||
exit 0
|
exit 0
|
||||||
|
Loading…
Reference in New Issue
Block a user