From 5318b36d0a753df0acea26c498cf0a7c7e527061 Mon Sep 17 00:00:00 2001 From: Ramil Valitov Date: Mon, 29 Jun 2020 00:43:33 +0300 Subject: [PATCH] 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