Skip to content

Commit

Permalink
Merge pull request #32 from renatomefi/feature/configurable-status-path
Browse files Browse the repository at this point in the history
Allow to configure FPM healthcheck status page path
  • Loading branch information
renatomefi committed Apr 5, 2020
2 parents 9d3510e + 4101ab6 commit 008eb1a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Or with `verbose` to see php-fpm status output:

```console
$ php-fpm-healthcheck -v
Trying to connect to php-fpm via: localhost:9000
Trying to connect to php-fpm via: localhost:9000/status
php-fpm status output:
pool: www
process manager: dynamic
Expand Down Expand Up @@ -140,6 +140,21 @@ $ echo $?
0
```

### Alternative status page path

_Since v0.5.0_

While the default status page path is `/status`, you can replace it in your php-fpm configuration, in order to change
also in the script in you can specify `FCGI_STATUS_PATH` env var within your connection uri:

```console
$ FCGI_STATUS_PATH=/custom-status-path php-fpm-healthcheck -v
Trying to connect to php-fpm via: localhost:9000/custom-status-path
...
$ echo $?
0
```

## Kubernetes example

More and more people are looking for health checks on kubernetes for php-fpm, here is an example of livenessProbe and readinessProbe:
Expand Down
15 changes: 8 additions & 7 deletions php-fpm-healthcheck
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ set -eu

OPTIND=1 # Reset getopt in case it has been used previously in the shell

# FastCGI variables
export REQUEST_METHOD="GET"
export SCRIPT_NAME="/status"
export SCRIPT_FILENAME="/status"
FCGI_CONNECT_DEFAULT="localhost:9000"

# Required software
FCGI_CMD_PATH=$(command -v cgi-fcgi) || { >&2 echo "Make sure fcgi is installed (i.e. apk add --no-cache fcgi). Aborting."; exit 4; }
command -v sed 1> /dev/null || { >&2 echo "Make sure sed is installed (i.e. apk add --no-cache busybox). Aborting."; exit 4; }
Expand All @@ -58,7 +52,7 @@ command -v grep 1> /dev/null || { >&2 echo "Make sure grep is installed (i.e. ap
# Get status from fastcgi connection
# $1 - cgi-fcgi connect argument
get_fpm_status() {
if test "$VERBOSE" = 1; then printf "Trying to connect to php-fpm via: %s\\n" "$1"; fi;
if test "$VERBOSE" = 1; then printf "Trying to connect to php-fpm via: %s%s\\n" "$1" "$SCRIPT_NAME"; fi;

# Since I cannot use pipefail I'll just split these in two commands
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)
Expand Down Expand Up @@ -120,6 +114,13 @@ fi;

eval set -- "$GETOPT"

# FastCGI variables
FCGI_CONNECT_DEFAULT="localhost:9000"
FCGI_STATUS_PATH_DEFAULT="/status"

export REQUEST_METHOD="GET"
export SCRIPT_NAME="${FCGI_STATUS_PATH:-$FCGI_STATUS_PATH_DEFAULT}"
export SCRIPT_FILENAME="${FCGI_STATUS_PATH:-$FCGI_STATUS_PATH_DEFAULT}"
FCGI_CONNECT="${FCGI_CONNECT:-$FCGI_CONNECT_DEFAULT}"

VERBOSE=0
Expand Down
2 changes: 1 addition & 1 deletion test/docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set -eEuo pipefail
function cleanup {
docker stop "$CONTAINER" 1> /dev/null
echo "container $CONTAINER: stopped"
docker rmi -f "$DOCKER_TAG_TEMPORARY"
docker rmi -f "$DOCKER_TAG_TEMPORARY"
}
trap cleanup EXIT

Expand Down
4 changes: 3 additions & 1 deletion test/testinfra/test_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def test_valid_with_non_integer_value_exits_properly(host):

@pytest.mark.php_fpm
def test_all_available_options_at_once(host):
cmd = host.run("php-fpm-healthcheck --accepted-conn=1000 --listen-queue=1000 --max-listen-queue=1000 --listen-queue-len=1000 --idle-processes=1000 --active-processes=1000 --total-processes=1000 --max-active-processes=1000 --max-children-reached=1000 --slow-requests=1000")
cmd = host.run("php-fpm-healthcheck --accepted-conn=1000 --listen-queue=1000 --max-listen-queue=1000 "
"--listen-queue-len=1000 --idle-processes=1000 --active-processes=1000 --total-processes=1000 "
"--max-active-processes=1000 --max-children-reached=1000 --slow-requests=1000")
assert cmd.rc == 0

@pytest.mark.php_fpm
Expand Down
15 changes: 15 additions & 0 deletions test/testinfra/test_fpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ def test_fpm_on_socket_with_huge_env(host, setup_fpm_to_default_fixture):
assert "status output:" in cmd.stdout
assert "pool:" in cmd.stdout

@pytest.mark.php_fpm
def test_default_status_page_path(host, setup_fpm_to_default_fixture):
cmd = host.run("php-fpm-healthcheck -v")
assert cmd.rc == 0
assert "Trying to connect to php-fpm via: localhost:9000/status" in cmd.stdout

@pytest.mark.php_fpm
def test_exit_when_fpm_is_invalid_path(host, setup_fpm_to_default_fixture):
cmd = host.run("FCGI_STATUS_PATH=/invalid php-fpm-healthcheck -v")
assert cmd.rc == 8
assert "Trying to connect to php-fpm via: localhost:9000/invalid" in cmd.stdout
assert "File not found." in cmd.stdout
assert "php-fpm status page non reachable" in cmd.stderr

@pytest.mark.alpine
def test_exit_when_fpm_is_not_reachable_apk(host, setup_fpm_to_default_fixture):
cmd = host.run("FCGI_CONNECT=localhost:9001 php-fpm-healthcheck -v")
Expand All @@ -70,3 +84,4 @@ def test_exit_when_fpm_is_invalid_host_apt(host, setup_fpm_to_default_fixture):
cmd = host.run("FCGI_CONNECT=abc php-fpm-healthcheck -v")
assert cmd.rc == 2
assert "Trying to connect to php-fpm via: abc" in cmd.stdout

0 comments on commit 008eb1a

Please sign in to comment.