|
| 1 | +#!/bin/sh |
| 2 | +# vim: set filetype=sh : |
| 3 | + |
| 4 | +# Author: <Renato Mefi [email protected]> https://github.com/renatomefi |
| 5 | +# The original code lives in https://github.com/renatomefi/php-fpm-healthcheck |
| 6 | +# |
| 7 | +# A POSIX compliant shell script to healthcheck PHP fpm status, can be used only for pinging the status page |
| 8 | +# or check for specific metrics |
| 9 | +# |
| 10 | +# i.e.: ./php-fpm-healthcheck --verbose --active-processes=6 |
| 11 | +# The script will fail in case the 'active processes' is bigger than 6. |
| 12 | +# |
| 13 | +# You can combine multiple options as well, the first one to fail will fail the healthcheck |
| 14 | +# i.e.: ./php-fpm-healthcheck --listen-queue-len=10 --active-processes=6 |
| 15 | +# |
| 16 | +# Ping mode (exit 0 if php-fpm returned data): ./php-fpm-healthcheck |
| 17 | +# |
| 18 | +# Ping mode with data (outputs php-fpm status text): ./php-fpm-healthcheck -v |
| 19 | +# |
| 20 | +# Exit status codes: |
| 21 | +# 2,9,111 - Couldn't connect to PHP fpm, is it running? |
| 22 | +# 8 - Couldn't reach PHP fpm status page, have you configured it with `pm.status_path = /status`? |
| 23 | +# 1 - A healthcheck condition has failed |
| 24 | +# 3 - Invalid option given |
| 25 | +# 4 - One or more required softwares are missing |
| 26 | +# |
| 27 | +# Available options: |
| 28 | +# -v|--verbose |
| 29 | +# |
| 30 | +# Metric options, fails in case the CURRENT VALUE is bigger than the GIVEN VALUE |
| 31 | +# --accepted-conn=n |
| 32 | +# --listen-queue=n |
| 33 | +# --max-listen-queue=n |
| 34 | +# --idle-processes=n |
| 35 | +# --active-processes=n |
| 36 | +# --total-processes=n |
| 37 | +# --max-active-processes=n |
| 38 | +# --max-children-reached=n |
| 39 | +# --slow-requests=n |
| 40 | +# |
| 41 | + |
| 42 | +set -eu |
| 43 | + |
| 44 | +OPTIND=1 # Reset getopt in case it has been used previously in the shell |
| 45 | + |
| 46 | +# Required software |
| 47 | +FCGI_CMD_PATH=$(command -v cgi-fcgi) || { >&2 echo "Make sure fcgi is installed (i.e. apk add --no-cache fcgi). Aborting."; exit 4; } |
| 48 | +command -v sed 1> /dev/null || { >&2 echo "Make sure sed is installed (i.e. apk add --no-cache busybox). Aborting."; exit 4; } |
| 49 | +command -v tail 1> /dev/null || { >&2 echo "Make sure tail is installed (i.e. apk add --no-cache busybox). Aborting."; exit 4; } |
| 50 | +command -v grep 1> /dev/null || { >&2 echo "Make sure grep is installed (i.e. apk add --no-cache grep). Aborting."; exit 4; } |
| 51 | + |
| 52 | +# Get status from fastcgi connection |
| 53 | +# $1 - cgi-fcgi connect argument |
| 54 | +get_fpm_status() { |
| 55 | + if test "$VERBOSE" = 1; then printf "Trying to connect to php-fpm via: %s%s\\n" "$1" "$SCRIPT_NAME"; fi; |
| 56 | + |
| 57 | + # Since I cannot use pipefail I'll just split these in two commands |
| 58 | + FPM_STATUS=$(env -i REQUEST_METHOD="$REQUEST_METHOD" SCRIPT_NAME="$SCRIPT_NAME" SCRIPT_FILENAME="$SCRIPT_FILENAME" "$FCGI_CMD_PATH" -bind -connect "$1" 2> /dev/null) |
| 59 | + FPM_STATUS=$(echo "$FPM_STATUS" | tail -n +5) |
| 60 | + |
| 61 | + if test "$VERBOSE" = 1; then printf "php-fpm status output:\\n%s\\n" "$FPM_STATUS"; fi; |
| 62 | + |
| 63 | + if test "$FPM_STATUS" = "File not found."; then |
| 64 | + >&2 printf "php-fpm status page non reachable\\n"; |
| 65 | + exit 8; |
| 66 | + fi; |
| 67 | +} |
| 68 | + |
| 69 | +# $1 - fpm option |
| 70 | +# $2 - expected value threshold |
| 71 | +check_fpm_health_by() { |
| 72 | + OPTION=$(echo "$1" | sed 's/--//g; s/-/ /g;') |
| 73 | + VALUE_EXPECTED="$2"; |
| 74 | + VALUE_ACTUAL=$(echo "$FPM_STATUS" | grep "^$OPTION:" | cut -d: -f2 | sed 's/ //g') |
| 75 | + |
| 76 | + if test "$VERBOSE" = 1; then printf "'%s' value '%s' and expected is less than '%s'\\n" "$OPTION" "$VALUE_ACTUAL" "$VALUE_EXPECTED"; fi; |
| 77 | + |
| 78 | + if test "$VALUE_ACTUAL" -gt "$VALUE_EXPECTED"; then |
| 79 | + >&2 printf "'%s' value '%s' is greater than expected '%s'\\n" "$OPTION" "$VALUE_ACTUAL" "$VALUE_EXPECTED"; |
| 80 | + exit 1; |
| 81 | + fi; |
| 82 | +} |
| 83 | + |
| 84 | +PARAM_AMOUNT=0 |
| 85 | + |
| 86 | +# $1 - fpm option |
| 87 | +# $2 - expected value threshold |
| 88 | +check_later() { |
| 89 | + # The POSIX sh way to check if it's an integer, also the output is supressed since it's polution |
| 90 | + if ! test "$2" -eq "$2" 2> /dev/null; then |
| 91 | + >&2 printf "'%s' option value must be an integer, '%s' given\\n" "$1" "$2"; exit 3; |
| 92 | + fi |
| 93 | + |
| 94 | + PARAM_AMOUNT=$(( PARAM_AMOUNT + 1 )) |
| 95 | + |
| 96 | + eval "PARAM_TO_CHECK$PARAM_AMOUNT=$1" |
| 97 | + eval "VALUE_TO_CHECK$PARAM_AMOUNT=$2" |
| 98 | +} |
| 99 | + |
| 100 | +# From the PARAM_TO_CHECK/VALUE_TO_CHECK magic variables, do all the checks |
| 101 | +check_fpm_health() { |
| 102 | + j=1 |
| 103 | + while [ $j -le $PARAM_AMOUNT ]; do |
| 104 | + eval "CURRENT_PARAM=\$PARAM_TO_CHECK$j" |
| 105 | + eval "CURRENT_VALUE=\$VALUE_TO_CHECK$j" |
| 106 | + check_fpm_health_by "$CURRENT_PARAM" "$CURRENT_VALUE" |
| 107 | + j=$(( j + 1 )) |
| 108 | + done |
| 109 | +} |
| 110 | + |
| 111 | +if ! GETOPT=$(getopt -o v --long verbose,accepted-conn:,listen-queue:,max-listen-queue:,listen-queue-len:,idle-processes:,active-processes:,total-processes:,max-active-processes:,max-children-reached:,slow-requests: -n 'php-fpm-healthcheck' -- "$@"); then |
| 112 | + >&2 echo "Invalid options, terminating." ; exit 3 |
| 113 | +fi; |
| 114 | + |
| 115 | +eval set -- "$GETOPT" |
| 116 | + |
| 117 | +# FastCGI variables |
| 118 | +FCGI_CONNECT_DEFAULT="localhost:9000" |
| 119 | +FCGI_STATUS_PATH_DEFAULT="/status" |
| 120 | + |
| 121 | +export REQUEST_METHOD="GET" |
| 122 | +export SCRIPT_NAME="${FCGI_STATUS_PATH:-$FCGI_STATUS_PATH_DEFAULT}" |
| 123 | +export SCRIPT_FILENAME="${FCGI_STATUS_PATH:-$FCGI_STATUS_PATH_DEFAULT}" |
| 124 | +FCGI_CONNECT="${FCGI_CONNECT:-$FCGI_CONNECT_DEFAULT}" |
| 125 | + |
| 126 | +VERBOSE=0 |
| 127 | + |
| 128 | +while test "$1"; do |
| 129 | + case "$1" in |
| 130 | + -v|--verbose ) VERBOSE=1; shift ;; |
| 131 | + --) shift ; break ;; |
| 132 | + * ) check_later "$1" "$2"; shift 2 ;; |
| 133 | + esac |
| 134 | +done |
| 135 | + |
| 136 | +FPM_STATUS=false |
| 137 | + |
| 138 | +get_fpm_status "$FCGI_CONNECT" |
| 139 | +check_fpm_health |
0 commit comments