Skip to content

Commit e895b6b

Browse files
author
Gabriel Fernandes
committed
Adjust get fpm status error messages
1 parent 953ca22 commit e895b6b

File tree

4 files changed

+33
-31
lines changed

4 files changed

+33
-31
lines changed

php-fpm-healthcheck

Lines changed: 15 additions & 11 deletions
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 - Malformed PHP fpm status page
28+
# 111 - Couldn't connect to PHP fpm, is it running?
2629
#
2730
# Available options:
2831
# -v|--verbose
@@ -52,22 +55,23 @@ 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-
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)
58-
RESPONSE_CODE=$(echo "$FPM_STATUS" | grep -o -E "^Status:\s*\d{3}" | sed 's/[^0-9]*//g')
59-
FPM_STATUS=$(echo "$FPM_STATUS" | tail +5)
58+
if test "$VERBOSE" = 1; then printf "Trying to connect to PHP-FPM via: %s%s\\n" "$1" "$SCRIPT_NAME"; fi;
6059

61-
if test "$VERBOSE" = 1; then printf "php-fpm status output:\\n%s\\n" "$FPM_STATUS"; fi;
60+
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)
6261

62+
RESPONSE_CODE=$(echo "$FPM_STATUS" | grep -o -E "^Status:\s*\d{3}" | sed 's/[^0-9]*//g')
6363
if ! test "$RESPONSE_CODE" -eq "$RESPONSE_CODE" 2> /dev/null; then
64-
return
64+
>&2 echo "Malformed PHP-FPM status page. Aborting.";
65+
exit 10;
6566
fi
6667

6768
if test "$RESPONSE_CODE" -lt 200 -o "$RESPONSE_CODE" -ge 400; then
68-
>&2 printf "php-fpm status page non reachable\\n";
69+
>&2 printf "Unable to reach PHP-FPM status page. Response code: %s\\n" "$RESPONSE_CODE";
6970
exit 8;
7071
fi;
72+
73+
FPM_STATUS=$(echo "$FPM_STATUS" | tail +5)
74+
if test "$VERBOSE" = 1; then printf "PHP-FPM status output:\\n%s\\n" "$FPM_STATUS"; fi;
7175
}
7276

7377
# $1 - fpm option
@@ -113,7 +117,7 @@ check_fpm_health() {
113117
}
114118

115119
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
116-
>&2 echo "Invalid options, terminating." ; exit 3
120+
>&2 echo "Invalid options. Aborting." ; exit 3
117121
fi;
118122

119123
eval set -- "$GETOPT"

test/testinfra/test_fpm.py

Lines changed: 13 additions & 15 deletions
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 "Unable to reach PHP-FPM status page. Response code: 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,53 @@ 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 "Unable to reach PHP-FPM status page. Response code: 404" in cmd.stderr
6361

6462
@pytest.mark.alpine
6563
def test_exit_when_fpm_is_not_reachable_apk(host, setup_fpm_to_default_fixture):
6664
cmd = host.run("FCGI_CONNECT=localhost:9001 php-fpm-healthcheck -v")
6765
assert cmd.rc in (111, 9)
68-
assert "Trying to connect to php-fpm via: localhost:9001" in cmd.stdout
66+
assert "Trying to connect to PHP-FPM via: localhost:9001" in cmd.stdout
6967

7068
@pytest.mark.alpine
7169
def test_exit_when_fpm_is_invalid_host_apk(host, setup_fpm_to_default_fixture):
7270
cmd = host.run("FCGI_CONNECT=abc php-fpm-healthcheck -v")
7371
assert cmd.rc in (2, 9)
74-
assert "Trying to connect to php-fpm via: abc" in cmd.stdout
72+
assert "Trying to connect to PHP-FPM via: abc" in cmd.stdout
7573

7674
@pytest.mark.stretch
7775
def test_exit_when_fpm_is_not_reachable_apt(host, setup_fpm_to_default_fixture):
7876
cmd = host.run("FCGI_CONNECT=localhost:9001 php-fpm-healthcheck -v")
7977
assert cmd.rc == 111
80-
assert "Trying to connect to php-fpm via: localhost:9001" in cmd.stdout
78+
assert "Trying to connect to PHP-FPM via: localhost:9001" in cmd.stdout
8179

8280
@pytest.mark.stretch
8381
def test_exit_when_fpm_is_invalid_host_apt(host, setup_fpm_to_default_fixture):
8482
cmd = host.run("FCGI_CONNECT=abc php-fpm-healthcheck -v")
8583
assert cmd.rc == 2
86-
assert "Trying to connect to php-fpm via: abc" in cmd.stdout
84+
assert "Trying to connect to PHP-FPM via: abc" in cmd.stdout
8785

test/testinfra/test_metrics.py

Lines changed: 3 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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)