Skip to content

Commit d6795dc

Browse files
Use cgi response code as error check point
1 parent 6b560ef commit d6795dc

File tree

4 files changed

+57
-29
lines changed

4 files changed

+57
-29
lines changed

php-fpm-healthcheck

+17-9
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
# Ping mode with data (outputs php-fpm status text): ./php-fpm-healthcheck -v
1919
#
2020
# 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`?
2321
# 1 - A healthcheck condition has failed
22+
# 2 - Couldn't connect to PHP fpm, is it running?
2423
# 3 - Invalid option given
2524
# 4 - One or more required softwares are missing
25+
# 8 - Couldn't reach PHP fpm status page, have you configured it with `pm.status_path = /status`?
26+
# 9 - Couldn't connect to PHP fpm, is it running?
27+
# 10 - Invalid PHP fpm status page response
28+
# 111 - Couldn't connect to PHP fpm, is it running?
2629
#
2730
# Available options:
2831
# -v|--verbose
@@ -52,16 +55,21 @@ command -v grep 1> /dev/null || { >&2 echo "Make sure grep is installed (i.e. ap
5255
# Get status from fastcgi connection
5356
# $1 - cgi-fcgi connect argument
5457
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+
if test "$VERBOSE" = 1; then printf "Trying to connect to PHP-FPM via: %s%s\\n" "$1" "$SCRIPT_NAME"; fi;
59+
5860
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 +5)
61+
RESPONSE_CODE=$(echo "$FPM_STATUS" | sed -n '/Status:/p' | sed -e 's/[^0-9]//g')
6062

61-
if test "$VERBOSE" = 1; then printf "php-fpm status output:\\n%s\\n" "$FPM_STATUS"; fi;
63+
# If missing status code, assume 200
64+
if ! test "$RESPONSE_CODE" -eq "$RESPONSE_CODE" 2> /dev/null; then
65+
RESPONSE_CODE=200
66+
fi
6267

63-
if test "$FPM_STATUS" = "File not found."; then
64-
>&2 printf "php-fpm status page non reachable\\n";
68+
if test "$RESPONSE_CODE" -ge 200 -a "$RESPONSE_CODE" -lt 400; then
69+
if test "$VERBOSE" = 1; then printf "PHP-FPM status output:\\n%s\\n" "$(echo "$FPM_STATUS" | tail +5)"; fi;
70+
return
71+
else
72+
>&2 printf "PHP-FPM status page non reachable. Error: $RESPONSE_CODE\\n";
6573
exit 8;
6674
fi;
6775
}

test/testinfra/test_fpm.py

+35-15
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ def test_exit_when_no_status_page_is_configured(host, setup_fpm_to_default_fixtu
2222

2323
cmd = host.run("php-fpm-healthcheck -v")
2424
assert cmd.rc == 8
25-
assert "Trying to connect to php-fpm via:" in cmd.stdout
26-
assert "status output:" in cmd.stdout
27-
assert "php-fpm status page non reachable" in cmd.stderr
25+
assert "Trying to connect to PHP-FPM via:" in cmd.stdout
26+
assert "PHP-FPM status page non reachable. Error: 404" in cmd.stderr
2827

2928
@pytest.mark.php_fpm
3029
def test_fpm_on_socket(host, setup_fpm_to_default_fixture):
@@ -34,54 +33,75 @@ def test_fpm_on_socket(host, setup_fpm_to_default_fixture):
3433

3534
cmd = host.run("FCGI_CONNECT=/var/run/php-fpm.sock php-fpm-healthcheck -v")
3635
assert cmd.rc == 0
37-
assert "Trying to connect to php-fpm via:" in cmd.stdout
38-
assert "status output:" in cmd.stdout
36+
assert "Trying to connect to PHP-FPM via:" in cmd.stdout
37+
assert "PHP-FPM status output:" in cmd.stdout
3938
assert "pool:" in cmd.stdout
4039

4140
# https://github.com/renatomefi/php-fpm-healthcheck/issues/18
4241
@pytest.mark.php_fpm
4342
def test_fpm_on_socket_with_huge_env(host, setup_fpm_to_default_fixture):
4443
cmd = host.run("HUGE_ENV=\"$(dd if=/dev/zero bs=8192 count=1 | tr '\\000' '\\040')\" php-fpm-healthcheck -v")
4544
assert cmd.rc == 0
46-
assert "Trying to connect to php-fpm via:" in cmd.stdout
47-
assert "status output:" in cmd.stdout
45+
assert "Trying to connect to PHP-FPM via:" in cmd.stdout
46+
assert "PHP-FPM status output:" in cmd.stdout
4847
assert "pool:" in cmd.stdout
4948

5049
@pytest.mark.php_fpm
5150
def test_default_status_page_path(host, setup_fpm_to_default_fixture):
5251
cmd = host.run("php-fpm-healthcheck -v")
5352
assert cmd.rc == 0
54-
assert "Trying to connect to php-fpm via: localhost:9000/status" in cmd.stdout
53+
assert "Trying to connect to PHP-FPM via: localhost:9000/status" in cmd.stdout
5554

5655
@pytest.mark.php_fpm
5756
def test_exit_when_fpm_is_invalid_path(host, setup_fpm_to_default_fixture):
5857
cmd = host.run("FCGI_STATUS_PATH=/invalid php-fpm-healthcheck -v")
5958
assert cmd.rc == 8
60-
assert "Trying to connect to php-fpm via: localhost:9000/invalid" in cmd.stdout
61-
assert "File not found." in cmd.stdout
62-
assert "php-fpm status page non reachable" in cmd.stderr
59+
assert "Trying to connect to PHP-FPM via: localhost:9000/invalid" in cmd.stdout
60+
assert "PHP-FPM status page non reachable. Error: 404" in cmd.stderr
61+
62+
# @pytest.mark.php_fpm
63+
# def test_exit_when_fpm_has_500_error_code(host, setup_fpm_to_default_fixture):
64+
# This tests should be included when possible to use custom status page
65+
# host.run("mkdir -p /var/www/html/error")
66+
# host.run("touch /var/www/html/error/index.php")
67+
# host.run("echo \"<?php http_response_code(500);\" > /var/www/html/error/index.php")
68+
# cmd = host.run("FCGI_STATUS_PATH=/var/www/html/error/index.php php-fpm-healthcheck -v")
69+
# assert cmd.rc == 8
70+
# assert "Trying to connect to PHP-FPM via: localhost:9000/var/www/html/error/index.php" in cmd.stdout
71+
# assert "PHP-FPM status page non reachable. Error: 500" in cmd.stderr
72+
73+
# @pytest.mark.php_fpm
74+
# def test_exit_when_fpm_has_400_error_code(host, setup_fpm_to_default_fixture):
75+
# This tests should be included when possible to use custom status page
76+
# host.run("mkdir -p /var/www/html/error")
77+
# host.run("touch /var/www/html/error/index.php")
78+
# host.run("echo \"<?php http_response_code(400);\" > /var/www/html/error/index.php")
79+
# cmd = host.run("FCGI_STATUS_PATH=/var/www/html/error/index.php php-fpm-healthcheck -v")
80+
# assert cmd.rc == 8
81+
# assert "Trying to connect to PHP-FPM via: localhost:9000/var/www/html/error/index.php" in cmd.stdout
82+
# assert "PHP-FPM status page non reachable. Error: 400" in cmd.stderr
6383

6484
@pytest.mark.alpine
6585
def test_exit_when_fpm_is_not_reachable_apk(host, setup_fpm_to_default_fixture):
6686
cmd = host.run("FCGI_CONNECT=localhost:9001 php-fpm-healthcheck -v")
6787
assert cmd.rc in (111, 9)
68-
assert "Trying to connect to php-fpm via: localhost:9001" in cmd.stdout
88+
assert "Trying to connect to PHP-FPM via: localhost:9001" in cmd.stdout
6989

7090
@pytest.mark.alpine
7191
def test_exit_when_fpm_is_invalid_host_apk(host, setup_fpm_to_default_fixture):
7292
cmd = host.run("FCGI_CONNECT=abc php-fpm-healthcheck -v")
7393
assert cmd.rc in (2, 9)
74-
assert "Trying to connect to php-fpm via: abc" in cmd.stdout
94+
assert "Trying to connect to PHP-FPM via: abc" in cmd.stdout
7595

7696
@pytest.mark.stretch
7797
def test_exit_when_fpm_is_not_reachable_apt(host, setup_fpm_to_default_fixture):
7898
cmd = host.run("FCGI_CONNECT=localhost:9001 php-fpm-healthcheck -v")
7999
assert cmd.rc == 111
80-
assert "Trying to connect to php-fpm via: localhost:9001" in cmd.stdout
100+
assert "Trying to connect to PHP-FPM via: localhost:9001" in cmd.stdout
81101

82102
@pytest.mark.stretch
83103
def test_exit_when_fpm_is_invalid_host_apt(host, setup_fpm_to_default_fixture):
84104
cmd = host.run("FCGI_CONNECT=abc php-fpm-healthcheck -v")
85105
assert cmd.rc == 2
86-
assert "Trying to connect to php-fpm via: abc" in cmd.stdout
106+
assert "Trying to connect to PHP-FPM via: abc" in cmd.stdout
87107

test/testinfra/test_metrics.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ def test_metric_fail_accepted_conn_with_other_metrics(host):
1919
def test_metric_accepted_conn(host):
2020
cmd = host.run("php-fpm-healthcheck -v")
2121
assert cmd.rc == 0
22-
assert "Trying to connect to php-fpm via:" in cmd.stdout
23-
assert "status output:" in cmd.stdout
22+
assert "Trying to connect to PHP-FPM via:" in cmd.stdout
23+
assert "PHP-FPM status output:" in cmd.stdout
2424
assert "pool:" in cmd.stdout
2525

2626
@pytest.mark.php_fpm
2727
def test_listen_queue_len_and_listen_queue_vars_are_parsed_correctly(host):
2828
cmd = host.run("php-fpm-healthcheck --verbose --listen-queue=5 --max-listen-queue=1024")
2929
assert cmd.rc == 0
30-
assert "Trying to connect to php-fpm via:" in cmd.stdout
30+
assert "Trying to connect to PHP-FPM via:" in cmd.stdout
3131
assert "'listen queue' value '0' and expected is less than '5" in cmd.stdout
3232
assert "'max listen queue' value '0' and expected is less than '1024'" in cmd.stdout

test/testinfra/test_ping.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ def test_ping(host):
99
def test_ping_verbose(host):
1010
cmd = host.run("php-fpm-healthcheck -v")
1111
assert cmd.rc == 0
12-
assert "Trying to connect to php-fpm via:" in cmd.stdout
13-
assert "status output:" in cmd.stdout
12+
assert "Trying to connect to PHP-FPM via:" in cmd.stdout
13+
assert "PHP-FPM status output:" in cmd.stdout
1414
assert "pool:" in cmd.stdout

0 commit comments

Comments
 (0)