Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Health-checks functions. |
| 3 | * |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 4 | * Copyright 2000-2009 Willy Tarreau <w@1wt.eu> |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 5 | * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
Willy Tarreau | b881608 | 2008-01-18 12:18:15 +0100 | [diff] [blame] | 14 | #include <assert.h> |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 15 | #include <ctype.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 16 | #include <errno.h> |
| 17 | #include <fcntl.h> |
Willy Tarreau | 9b39dc5 | 2014-07-08 00:54:10 +0200 | [diff] [blame] | 18 | #include <signal.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 20 | #include <stdlib.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 21 | #include <string.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 22 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | #include <sys/socket.h> |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 25 | #include <sys/types.h> |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 26 | #include <sys/wait.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | #include <netinet/in.h> |
Willy Tarreau | 1274bc4 | 2009-07-15 07:16:31 +0200 | [diff] [blame] | 28 | #include <netinet/tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | #include <arpa/inet.h> |
| 30 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 31 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 32 | #include <common/compat.h> |
| 33 | #include <common/config.h> |
| 34 | #include <common/mini-clist.h> |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 35 | #include <common/standard.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 36 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | |
| 38 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 40 | #ifdef USE_OPENSSL |
| 41 | #include <types/ssl_sock.h> |
| 42 | #include <proto/ssl_sock.h> |
| 43 | #endif /* USE_OPENSSL */ |
| 44 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | #include <proto/backend.h> |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 46 | #include <proto/checks.h> |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 47 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | #include <proto/fd.h> |
| 49 | #include <proto/log.h> |
| 50 | #include <proto/queue.h> |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 51 | #include <proto/port_range.h> |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 52 | #include <proto/proto_http.h> |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 53 | #include <proto/proto_tcp.h> |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 54 | #include <proto/protocol.h> |
Willy Tarreau | 2b5652f | 2006-12-31 17:46:05 +0100 | [diff] [blame] | 55 | #include <proto/proxy.h> |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 56 | #include <proto/raw_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 57 | #include <proto/server.h> |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 58 | #include <proto/session.h> |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 59 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | #include <proto/task.h> |
| 61 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 62 | static int httpchk_expect(struct server *s, int done); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 63 | static int tcpcheck_get_step_id(struct server *); |
| 64 | static void tcpcheck_main(struct connection *); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 65 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 66 | static const struct check_status check_statuses[HCHK_STATUS_SIZE] = { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 67 | [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" }, |
| 68 | [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" }, |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 69 | [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 70 | |
Willy Tarreau | 2396418 | 2014-05-20 20:56:30 +0200 | [diff] [blame] | 71 | /* Below we have finished checks */ |
| 72 | [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" }, |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 73 | [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" }, |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 74 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 75 | [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 76 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 77 | [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" }, |
| 78 | [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" }, |
| 79 | [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 80 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 81 | [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" }, |
| 82 | [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" }, |
| 83 | [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 84 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 85 | [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" }, |
| 86 | [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 87 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 88 | [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 89 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 90 | [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" }, |
| 91 | [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" }, |
| 92 | [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" }, |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 93 | |
| 94 | [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" }, |
| 95 | [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" }, |
Cyril Bonté | 77010d8 | 2014-08-07 01:55:37 +0200 | [diff] [blame] | 96 | [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 97 | }; |
| 98 | |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 99 | const struct extcheck_env extcheck_envs[EXTCHK_SIZE] = { |
| 100 | [EXTCHK_PATH] = { "PATH", EXTCHK_SIZE_EVAL_INIT }, |
| 101 | [EXTCHK_HAPROXY_PROXY_NAME] = { "HAPROXY_PROXY_NAME", EXTCHK_SIZE_EVAL_INIT }, |
| 102 | [EXTCHK_HAPROXY_PROXY_ID] = { "HAPROXY_PROXY_ID", EXTCHK_SIZE_EVAL_INIT }, |
| 103 | [EXTCHK_HAPROXY_PROXY_ADDR] = { "HAPROXY_PROXY_ADDR", EXTCHK_SIZE_EVAL_INIT }, |
| 104 | [EXTCHK_HAPROXY_PROXY_PORT] = { "HAPROXY_PROXY_PORT", EXTCHK_SIZE_EVAL_INIT }, |
| 105 | [EXTCHK_HAPROXY_SERVER_NAME] = { "HAPROXY_SERVER_NAME", EXTCHK_SIZE_EVAL_INIT }, |
| 106 | [EXTCHK_HAPROXY_SERVER_ID] = { "HAPROXY_SERVER_ID", EXTCHK_SIZE_EVAL_INIT }, |
| 107 | [EXTCHK_HAPROXY_SERVER_ADDR] = { "HAPROXY_SERVER_ADDR", EXTCHK_SIZE_EVAL_INIT }, |
| 108 | [EXTCHK_HAPROXY_SERVER_PORT] = { "HAPROXY_SERVER_PORT", EXTCHK_SIZE_EVAL_INIT }, |
| 109 | [EXTCHK_HAPROXY_SERVER_MAXCONN] = { "HAPROXY_SERVER_MAXCONN", EXTCHK_SIZE_EVAL_INIT }, |
| 110 | [EXTCHK_HAPROXY_SERVER_CURCONN] = { "HAPROXY_SERVER_CURCONN", EXTCHK_SIZE_ULONG }, |
| 111 | }; |
| 112 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 113 | static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */ |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 114 | [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }}, |
| 115 | |
| 116 | [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }}, |
| 117 | [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }}, |
| 118 | |
| 119 | [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }}, |
| 120 | [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }}, |
| 121 | [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }}, |
| 122 | [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }}, |
| 123 | |
| 124 | [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }}, |
| 125 | [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }}, |
| 126 | [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }}, |
| 127 | }; |
| 128 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 129 | /* |
| 130 | * Convert check_status code to description |
| 131 | */ |
| 132 | const char *get_check_status_description(short check_status) { |
| 133 | |
| 134 | const char *desc; |
| 135 | |
| 136 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 137 | desc = check_statuses[check_status].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 138 | else |
| 139 | desc = NULL; |
| 140 | |
| 141 | if (desc && *desc) |
| 142 | return desc; |
| 143 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 144 | return check_statuses[HCHK_STATUS_UNKNOWN].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Convert check_status code to short info |
| 149 | */ |
| 150 | const char *get_check_status_info(short check_status) { |
| 151 | |
| 152 | const char *info; |
| 153 | |
| 154 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 155 | info = check_statuses[check_status].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 156 | else |
| 157 | info = NULL; |
| 158 | |
| 159 | if (info && *info) |
| 160 | return info; |
| 161 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 162 | return check_statuses[HCHK_STATUS_UNKNOWN].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 165 | const char *get_analyze_status(short analyze_status) { |
| 166 | |
| 167 | const char *desc; |
| 168 | |
| 169 | if (analyze_status < HANA_STATUS_SIZE) |
| 170 | desc = analyze_statuses[analyze_status].desc; |
| 171 | else |
| 172 | desc = NULL; |
| 173 | |
| 174 | if (desc && *desc) |
| 175 | return desc; |
| 176 | else |
| 177 | return analyze_statuses[HANA_STATUS_UNKNOWN].desc; |
| 178 | } |
| 179 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 180 | /* Builds a string containing some information about the health check's result. |
| 181 | * The output string is allocated from the trash chunks. If the check is NULL, |
| 182 | * NULL is returned. This is designed to be used when emitting logs about health |
| 183 | * checks. |
Willy Tarreau | ddd329c | 2014-05-16 16:46:12 +0200 | [diff] [blame] | 184 | */ |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 185 | static const char *check_reason_string(struct check *check) |
Willy Tarreau | ddd329c | 2014-05-16 16:46:12 +0200 | [diff] [blame] | 186 | { |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 187 | struct chunk *msg; |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 188 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 189 | if (!check) |
| 190 | return NULL; |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 191 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 192 | msg = get_trash_chunk(); |
| 193 | chunk_printf(msg, "reason: %s", get_check_status_description(check->status)); |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 194 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 195 | if (check->status >= HCHK_STATUS_L57DATA) |
| 196 | chunk_appendf(msg, ", code: %d", check->code); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 197 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 198 | if (*check->desc) { |
| 199 | struct chunk src; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 200 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 201 | chunk_appendf(msg, ", info: \""); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 202 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 203 | chunk_initlen(&src, check->desc, 0, strlen(check->desc)); |
| 204 | chunk_asciiencode(msg, &src, '"'); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 205 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 206 | chunk_appendf(msg, "\""); |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 209 | if (check->duration >= 0) |
| 210 | chunk_appendf(msg, ", check duration: %ldms", check->duration); |
| 211 | |
| 212 | return msg->str; |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 215 | /* |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 216 | * Set check->status, update check->duration and fill check->result with |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 217 | * an adequate CHK_RES_* value. The new check->health is computed based |
| 218 | * on the result. |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 219 | * |
| 220 | * Show information in logs about failed health check if server is UP |
| 221 | * or succeeded health checks if server is DOWN. |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 222 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 223 | static void set_server_check_status(struct check *check, short status, const char *desc) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 224 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 225 | struct server *s = check->server; |
Willy Tarreau | bef1b32 | 2014-05-13 21:01:39 +0200 | [diff] [blame] | 226 | short prev_status = check->status; |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 227 | int report = 0; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 228 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 229 | if (status == HCHK_STATUS_START) { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 230 | check->result = CHK_RES_UNKNOWN; /* no result yet */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 231 | check->desc[0] = '\0'; |
| 232 | check->start = now; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 233 | return; |
| 234 | } |
| 235 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 236 | if (!check->status) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 237 | return; |
| 238 | |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 239 | if (desc && *desc) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 240 | strncpy(check->desc, desc, HCHK_DESC_LEN-1); |
| 241 | check->desc[HCHK_DESC_LEN-1] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 242 | } else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 243 | check->desc[0] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 244 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 245 | check->status = status; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 246 | if (check_statuses[status].result) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 247 | check->result = check_statuses[status].result; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 248 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 249 | if (status == HCHK_STATUS_HANA) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 250 | check->duration = -1; |
| 251 | else if (!tv_iszero(&check->start)) { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 252 | /* set_server_check_status() may be called more than once */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 253 | check->duration = tv_ms_elapsed(&check->start, &now); |
| 254 | tv_zero(&check->start); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Willy Tarreau | 2396418 | 2014-05-20 20:56:30 +0200 | [diff] [blame] | 257 | /* no change is expected if no state change occurred */ |
| 258 | if (check->result == CHK_RES_NEUTRAL) |
| 259 | return; |
| 260 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 261 | report = 0; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 262 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 263 | switch (check->result) { |
| 264 | case CHK_RES_FAILED: |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 265 | /* Failure to connect to the agent as a secondary check should not |
| 266 | * cause the server to be marked down. |
| 267 | */ |
| 268 | if ((!(check->state & CHK_ST_AGENT) || |
| 269 | (check->status >= HCHK_STATUS_L7TOUT)) && |
| 270 | (check->health >= check->rise)) { |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 271 | s->counters.failed_checks++; |
| 272 | report = 1; |
| 273 | check->health--; |
| 274 | if (check->health < check->rise) |
| 275 | check->health = 0; |
| 276 | } |
| 277 | break; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 278 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 279 | case CHK_RES_PASSED: |
| 280 | case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */ |
| 281 | if ((check->health < check->rise + check->fall - 1) && |
| 282 | (check->result == CHK_RES_PASSED || check->health > 0)) { |
| 283 | report = 1; |
| 284 | check->health++; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 285 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 286 | if (check->health >= check->rise) |
| 287 | check->health = check->rise + check->fall - 1; /* OK now */ |
| 288 | } |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 289 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 290 | /* clear consecutive_errors if observing is enabled */ |
| 291 | if (s->onerror) |
| 292 | s->consecutive_errors = 0; |
| 293 | break; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 294 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 295 | default: |
| 296 | break; |
| 297 | } |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 298 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 299 | if (s->proxy->options2 & PR_O2_LOGHCHKS && |
| 300 | (status != prev_status || report)) { |
| 301 | chunk_printf(&trash, |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 302 | "%s check for %sserver %s/%s %s%s", |
| 303 | (check->state & CHK_ST_AGENT) ? "Agent" : "Health", |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 304 | s->flags & SRV_F_BACKUP ? "backup " : "", |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 305 | s->proxy->id, s->id, |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 306 | (check->result == CHK_RES_CONDPASS) ? "conditionally ":"", |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 307 | (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 308 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 309 | srv_append_status(&trash, s, check_reason_string(check), -1, 0); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 310 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 311 | chunk_appendf(&trash, ", status: %d/%d %s", |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 312 | (check->health >= check->rise) ? check->health - check->rise + 1 : check->health, |
| 313 | (check->health >= check->rise) ? check->fall : check->rise, |
| 314 | (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 315 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 316 | Warning("%s.\n", trash.str); |
| 317 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 318 | } |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 319 | } |
| 320 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 321 | /* Marks the check <check>'s server down if the current check is already failed |
| 322 | * and the server is not down yet nor in maintenance. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 323 | */ |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 324 | static void check_notify_failure(struct check *check) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 325 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 326 | struct server *s = check->server; |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 327 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 328 | /* The agent secondary check should only cause a server to be marked |
| 329 | * as down if check->status is HCHK_STATUS_L7STS, which indicates |
| 330 | * that the agent returned "fail", "stopped" or "down". |
| 331 | * The implication here is that failure to connect to the agent |
| 332 | * as a secondary check should not cause the server to be marked |
| 333 | * down. */ |
| 334 | if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS) |
| 335 | return; |
| 336 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 337 | if (check->health > 0) |
| 338 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 339 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 340 | /* We only report a reason for the check if we did not do so previously */ |
| 341 | srv_set_stopped(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 344 | /* Marks the check <check> as valid and tries to set its server up, provided |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 345 | * it isn't in maintenance, it is not tracking a down server and other checks |
| 346 | * comply. The rule is simple : by default, a server is up, unless any of the |
| 347 | * following conditions is true : |
| 348 | * - health check failed (check->health < rise) |
| 349 | * - agent check failed (agent->health < rise) |
| 350 | * - the server tracks a down server (track && track->state == STOPPED) |
| 351 | * Note that if the server has a slowstart, it will switch to STARTING instead |
| 352 | * of RUNNING. Also, only the health checks support the nolb mode, so the |
| 353 | * agent's success may not take the server out of this mode. |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 354 | */ |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 355 | static void check_notify_success(struct check *check) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 356 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 357 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 358 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 359 | if (s->admin & SRV_ADMF_MAINT) |
| 360 | return; |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 361 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 362 | if (s->track && s->track->state == SRV_ST_STOPPED) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 363 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 364 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 365 | if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise)) |
| 366 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 367 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 368 | if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise)) |
| 369 | return; |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 370 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 371 | if ((check->state & CHK_ST_AGENT) && s->state == SRV_ST_STOPPING) |
| 372 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 373 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 374 | srv_set_running(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 375 | } |
| 376 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 377 | /* Marks the check <check> as valid and tries to set its server into stopping mode |
| 378 | * if it was running or starting, and provided it isn't in maintenance and other |
| 379 | * checks comply. The conditions for the server to be marked in stopping mode are |
| 380 | * the same as for it to be turned up. Also, only the health checks support the |
| 381 | * nolb mode. |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 382 | */ |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 383 | static void check_notify_stopping(struct check *check) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 384 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 385 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 386 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 387 | if (s->admin & SRV_ADMF_MAINT) |
| 388 | return; |
| 389 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 390 | if (check->state & CHK_ST_AGENT) |
| 391 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 392 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 393 | if (s->track && s->track->state == SRV_ST_STOPPED) |
| 394 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 395 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 396 | if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise)) |
| 397 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 398 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 399 | if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise)) |
| 400 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 401 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 402 | srv_set_stopping(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 403 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 404 | |
Willy Tarreau | 9fe7aae | 2013-12-31 23:47:37 +0100 | [diff] [blame] | 405 | /* note: use health_adjust() only, which first checks that the observe mode is |
| 406 | * enabled. |
| 407 | */ |
| 408 | void __health_adjust(struct server *s, short status) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 409 | { |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 410 | int failed; |
| 411 | int expire; |
| 412 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 413 | if (s->observe >= HANA_OBS_SIZE) |
| 414 | return; |
| 415 | |
Willy Tarreau | bb95666 | 2013-01-24 00:37:39 +0100 | [diff] [blame] | 416 | if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc) |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 417 | return; |
| 418 | |
| 419 | switch (analyze_statuses[status].lr[s->observe - 1]) { |
| 420 | case 1: |
| 421 | failed = 1; |
| 422 | break; |
| 423 | |
| 424 | case 2: |
| 425 | failed = 0; |
| 426 | break; |
| 427 | |
| 428 | default: |
| 429 | return; |
| 430 | } |
| 431 | |
| 432 | if (!failed) { |
| 433 | /* good: clear consecutive_errors */ |
| 434 | s->consecutive_errors = 0; |
| 435 | return; |
| 436 | } |
| 437 | |
| 438 | s->consecutive_errors++; |
| 439 | |
| 440 | if (s->consecutive_errors < s->consecutive_errors_limit) |
| 441 | return; |
| 442 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 443 | chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s", |
| 444 | s->consecutive_errors, get_analyze_status(status)); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 445 | |
| 446 | switch (s->onerror) { |
| 447 | case HANA_ONERR_FASTINTER: |
| 448 | /* force fastinter - nothing to do here as all modes force it */ |
| 449 | break; |
| 450 | |
| 451 | case HANA_ONERR_SUDDTH: |
| 452 | /* simulate a pre-fatal failed health check */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 453 | if (s->check.health > s->check.rise) |
| 454 | s->check.health = s->check.rise + 1; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 455 | |
| 456 | /* no break - fall through */ |
| 457 | |
| 458 | case HANA_ONERR_FAILCHK: |
| 459 | /* simulate a failed health check */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 460 | set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 461 | check_notify_failure(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 462 | break; |
| 463 | |
| 464 | case HANA_ONERR_MARKDWN: |
| 465 | /* mark server down */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 466 | s->check.health = s->check.rise; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 467 | set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 468 | check_notify_failure(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 469 | break; |
| 470 | |
| 471 | default: |
| 472 | /* write a warning? */ |
| 473 | break; |
| 474 | } |
| 475 | |
| 476 | s->consecutive_errors = 0; |
| 477 | s->counters.failed_hana++; |
| 478 | |
Simon Horman | 6618300 | 2013-02-23 10:16:43 +0900 | [diff] [blame] | 479 | if (s->check.fastinter) { |
| 480 | expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter)); |
Sergiy Prykhodko | 1d57e50 | 2013-09-21 12:05:00 +0300 | [diff] [blame] | 481 | if (s->check.task->expire > expire) { |
Willy Tarreau | 5b3a202 | 2012-09-28 15:01:02 +0200 | [diff] [blame] | 482 | s->check.task->expire = expire; |
Sergiy Prykhodko | 1d57e50 | 2013-09-21 12:05:00 +0300 | [diff] [blame] | 483 | /* requeue check task with new expire */ |
| 484 | task_queue(s->check.task); |
| 485 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
Willy Tarreau | a1dab55 | 2014-04-14 15:04:54 +0200 | [diff] [blame] | 489 | static int httpchk_build_status_header(struct server *s, char *buffer, int size) |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 490 | { |
| 491 | int sv_state; |
| 492 | int ratio; |
| 493 | int hlen = 0; |
| 494 | const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d", |
| 495 | "UP %d/%d", "UP", |
| 496 | "NOLB %d/%d", "NOLB", |
| 497 | "no check" }; |
| 498 | |
| 499 | memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24); |
| 500 | hlen += 24; |
| 501 | |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 502 | if (!(s->check.state & CHK_ST_ENABLED)) |
| 503 | sv_state = 6; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 504 | else if (s->state != SRV_ST_STOPPED) { |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 505 | if (s->check.health == s->check.rise + s->check.fall - 1) |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 506 | sv_state = 3; /* UP */ |
| 507 | else |
| 508 | sv_state = 2; /* going down */ |
| 509 | |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 510 | if (s->state == SRV_ST_STOPPING) |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 511 | sv_state += 2; |
| 512 | } else { |
Simon Horman | 125d099 | 2013-02-24 17:23:38 +0900 | [diff] [blame] | 513 | if (s->check.health) |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 514 | sv_state = 1; /* going up */ |
| 515 | else |
| 516 | sv_state = 0; /* DOWN */ |
| 517 | } |
| 518 | |
Willy Tarreau | a1dab55 | 2014-04-14 15:04:54 +0200 | [diff] [blame] | 519 | hlen += snprintf(buffer + hlen, size - hlen, |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 520 | srv_hlt_st[sv_state], |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 521 | (s->state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health), |
| 522 | (s->state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise)); |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 523 | |
Willy Tarreau | a1dab55 | 2014-04-14 15:04:54 +0200 | [diff] [blame] | 524 | hlen += snprintf(buffer + hlen, size - hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d", |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 525 | s->proxy->id, s->id, |
| 526 | global.node, |
| 527 | (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 528 | (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 529 | s->cur_sess, s->proxy->beconn - s->proxy->nbpend, |
| 530 | s->nbpend); |
| 531 | |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 532 | if ((s->state == SRV_ST_STARTING) && |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 533 | now.tv_sec < s->last_change + s->slowstart && |
| 534 | now.tv_sec >= s->last_change) { |
| 535 | ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart); |
Willy Tarreau | a1dab55 | 2014-04-14 15:04:54 +0200 | [diff] [blame] | 536 | hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio); |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | buffer[hlen++] = '\r'; |
| 540 | buffer[hlen++] = '\n'; |
| 541 | |
| 542 | return hlen; |
| 543 | } |
| 544 | |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 545 | /* Check the connection. If an error has already been reported or the socket is |
| 546 | * closed, keep errno intact as it is supposed to contain the valid error code. |
| 547 | * If no error is reported, check the socket's error queue using getsockopt(). |
| 548 | * Warning, this must be done only once when returning from poll, and never |
| 549 | * after an I/O error was attempted, otherwise the error queue might contain |
| 550 | * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the |
| 551 | * socket. Returns non-zero if an error was reported, zero if everything is |
| 552 | * clean (including a properly closed socket). |
| 553 | */ |
| 554 | static int retrieve_errno_from_socket(struct connection *conn) |
| 555 | { |
| 556 | int skerr; |
| 557 | socklen_t lskerr = sizeof(skerr); |
| 558 | |
| 559 | if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl)) |
| 560 | return 1; |
| 561 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 562 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 563 | return 0; |
| 564 | |
| 565 | if (getsockopt(conn->t.sock.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0) |
| 566 | errno = skerr; |
| 567 | |
| 568 | if (errno == EAGAIN) |
| 569 | errno = 0; |
| 570 | |
| 571 | if (!errno) { |
| 572 | /* we could not retrieve an error, that does not mean there is |
| 573 | * none. Just don't change anything and only report the prior |
| 574 | * error if any. |
| 575 | */ |
| 576 | if (conn->flags & CO_FL_ERROR) |
| 577 | return 1; |
| 578 | else |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH; |
| 583 | return 1; |
| 584 | } |
| 585 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 586 | /* Try to collect as much information as possible on the connection status, |
| 587 | * and adjust the server status accordingly. It may make use of <errno_bck> |
| 588 | * if non-null when the caller is absolutely certain of its validity (eg: |
| 589 | * checked just after a syscall). If the caller doesn't have a valid errno, |
| 590 | * it can pass zero, and retrieve_errno_from_socket() will be called to try |
| 591 | * to extract errno from the socket. If no error is reported, it will consider |
| 592 | * the <expired> flag. This is intended to be used when a connection error was |
| 593 | * reported in conn->flags or when a timeout was reported in <expired>. The |
| 594 | * function takes care of not updating a server status which was already set. |
| 595 | * All situations where at least one of <expired> or CO_FL_ERROR are set |
| 596 | * produce a status. |
| 597 | */ |
| 598 | static void chk_report_conn_err(struct connection *conn, int errno_bck, int expired) |
| 599 | { |
| 600 | struct check *check = conn->owner; |
| 601 | const char *err_msg; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 602 | struct chunk *chk; |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 603 | int step; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 604 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 605 | if (check->result != CHK_RES_UNKNOWN) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 606 | return; |
| 607 | |
| 608 | errno = errno_bck; |
| 609 | if (!errno || errno == EAGAIN) |
| 610 | retrieve_errno_from_socket(conn); |
| 611 | |
| 612 | if (!(conn->flags & CO_FL_ERROR) && !expired) |
| 613 | return; |
| 614 | |
| 615 | /* we'll try to build a meaningful error message depending on the |
| 616 | * context of the error possibly present in conn->err_code, and the |
| 617 | * socket error possibly collected above. This is useful to know the |
| 618 | * exact step of the L6 layer (eg: SSL handshake). |
| 619 | */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 620 | chk = get_trash_chunk(); |
| 621 | |
| 622 | if (check->type == PR_O2_TCPCHK_CHK) { |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 623 | step = tcpcheck_get_step_id(check->server); |
| 624 | if (!step) |
| 625 | chunk_printf(chk, " at initial connection step of tcp-check"); |
| 626 | else { |
| 627 | chunk_printf(chk, " at step %d of tcp-check", step); |
| 628 | /* we were looking for a string */ |
| 629 | if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_CONNECT) { |
| 630 | if (check->last_started_step->port) |
| 631 | chunk_appendf(chk, " (connect port %d)" ,check->last_started_step->port); |
| 632 | else |
| 633 | chunk_appendf(chk, " (connect)"); |
| 634 | } |
| 635 | else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_EXPECT) { |
| 636 | if (check->last_started_step->string) |
| 637 | chunk_appendf(chk, " (string '%s')", check->last_started_step->string); |
| 638 | else if (check->last_started_step->expect_regex) |
| 639 | chunk_appendf(chk, " (expect regex)"); |
| 640 | } |
| 641 | else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_SEND) { |
| 642 | chunk_appendf(chk, " (send)"); |
| 643 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 647 | if (conn->err_code) { |
| 648 | if (errno && errno != EAGAIN) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 649 | chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 650 | else |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 651 | chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 652 | err_msg = trash.str; |
| 653 | } |
| 654 | else { |
| 655 | if (errno && errno != EAGAIN) { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 656 | chunk_printf(&trash, "%s%s", strerror(errno), chk->str); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 657 | err_msg = trash.str; |
| 658 | } |
| 659 | else { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 660 | err_msg = chk->str; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | |
| 664 | if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) { |
| 665 | /* L4 not established (yet) */ |
| 666 | if (conn->flags & CO_FL_ERROR) |
| 667 | set_server_check_status(check, HCHK_STATUS_L4CON, err_msg); |
| 668 | else if (expired) |
| 669 | set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg); |
| 670 | } |
| 671 | else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) { |
| 672 | /* L6 not established (yet) */ |
| 673 | if (conn->flags & CO_FL_ERROR) |
| 674 | set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg); |
| 675 | else if (expired) |
| 676 | set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg); |
| 677 | } |
| 678 | else if (conn->flags & CO_FL_ERROR) { |
| 679 | /* I/O error after connection was established and before we could diagnose */ |
| 680 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 681 | } |
| 682 | else if (expired) { |
| 683 | /* connection established but expired check */ |
| 684 | if (check->type == PR_O2_SSL3_CHK) |
| 685 | set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg); |
| 686 | else /* HTTP, SMTP, ... */ |
| 687 | set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg); |
| 688 | } |
| 689 | |
| 690 | return; |
| 691 | } |
| 692 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 693 | /* |
| 694 | * This function is used only for server health-checks. It handles |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 695 | * the connection acknowledgement. If the proxy requires L7 health-checks, |
| 696 | * it sends the request. In other cases, it calls set_server_check_status() |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 697 | * to set check->status, check->duration and check->result. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 698 | */ |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 699 | static void event_srv_chk_w(struct connection *conn) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 700 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 701 | struct check *check = conn->owner; |
| 702 | struct server *s = check->server; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 703 | struct task *t = check->task; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 704 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 705 | if (unlikely(check->result == CHK_RES_FAILED)) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 706 | goto out_wakeup; |
| 707 | |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 708 | if (conn->flags & CO_FL_HANDSHAKE) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 709 | return; |
| 710 | |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 711 | if (retrieve_errno_from_socket(conn)) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 712 | chk_report_conn_err(conn, errno, 0); |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 713 | __conn_data_stop_both(conn); |
| 714 | goto out_wakeup; |
| 715 | } |
Krzysztof Piotr Oledzki | 6492db5 | 2010-01-02 22:03:01 +0100 | [diff] [blame] | 716 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 717 | if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) { |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 718 | /* if the output is closed, we can't do anything */ |
| 719 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 720 | chk_report_conn_err(conn, 0, 0); |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 721 | goto out_wakeup; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 722 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 723 | |
Willy Tarreau | 06559ac | 2013-12-05 01:53:08 +0100 | [diff] [blame] | 724 | /* here, we know that the connection is established. That's enough for |
| 725 | * a pure TCP check. |
| 726 | */ |
| 727 | if (!check->type) |
| 728 | goto out_wakeup; |
| 729 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 730 | if (check->type == PR_O2_TCPCHK_CHK) { |
| 731 | tcpcheck_main(conn); |
| 732 | return; |
| 733 | } |
| 734 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 735 | if (check->bo->o) { |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 736 | conn->xprt->snd_buf(conn, check->bo, 0); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 737 | if (conn->flags & CO_FL_ERROR) { |
| 738 | chk_report_conn_err(conn, errno, 0); |
| 739 | __conn_data_stop_both(conn); |
| 740 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 741 | } |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 742 | if (check->bo->o) |
| 743 | return; |
| 744 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 745 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 746 | /* full request sent, we allow up to <timeout.check> if nonzero for a response */ |
| 747 | if (s->proxy->timeout.check) { |
| 748 | t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check); |
| 749 | task_queue(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 750 | } |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 751 | goto out_nowake; |
| 752 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 753 | out_wakeup: |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 754 | task_wakeup(t, TASK_WOKEN_IO); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 755 | out_nowake: |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 756 | __conn_data_stop_send(conn); /* nothing more to write */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 757 | } |
| 758 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 759 | /* |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 760 | * This function is used only for server health-checks. It handles the server's |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 761 | * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 762 | * set_server_check_status() to update check->status, check->duration |
| 763 | * and check->result. |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 764 | |
| 765 | * The set_server_check_status function is called with HCHK_STATUS_L7OKD if |
| 766 | * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server |
| 767 | * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in |
| 768 | * response to an SSL HELLO (the principle is that this is enough to |
| 769 | * distinguish between an SSL server and a pure TCP relay). All other cases will |
| 770 | * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP, |
| 771 | * etc. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 772 | */ |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 773 | static void event_srv_chk_r(struct connection *conn) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 774 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 775 | struct check *check = conn->owner; |
| 776 | struct server *s = check->server; |
| 777 | struct task *t = check->task; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 778 | char *desc; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 779 | int done; |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 780 | unsigned short msglen; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 781 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 782 | if (unlikely(check->result == CHK_RES_FAILED)) |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 783 | goto out_wakeup; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 784 | |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 785 | if (conn->flags & CO_FL_HANDSHAKE) |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 786 | return; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 787 | |
| 788 | if (check->type == PR_O2_TCPCHK_CHK) { |
| 789 | tcpcheck_main(conn); |
| 790 | return; |
| 791 | } |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 792 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 793 | /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available |
| 794 | * but the connection was closed on the remote end. Fortunately, recv still |
| 795 | * works correctly and we don't need to do the getsockopt() on linux. |
| 796 | */ |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 797 | |
| 798 | /* Set buffer to point to the end of the data already read, and check |
| 799 | * that there is free space remaining. If the buffer is full, proceed |
| 800 | * with running the checks without attempting another socket read. |
| 801 | */ |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 802 | |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 803 | done = 0; |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 804 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 805 | conn->xprt->rcv_buf(conn, check->bi, check->bi->size); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 806 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) { |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 807 | done = 1; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 808 | if ((conn->flags & CO_FL_ERROR) && !check->bi->i) { |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 809 | /* Report network errors only if we got no other data. Otherwise |
| 810 | * we'll let the upper layers decide whether the response is OK |
| 811 | * or not. It is very common that an RST sent by the server is |
| 812 | * reported as an error just after the last data chunk. |
| 813 | */ |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 814 | chk_report_conn_err(conn, errno, 0); |
Willy Tarreau | c1a0796 | 2010-03-16 20:55:43 +0100 | [diff] [blame] | 815 | goto out_wakeup; |
| 816 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 817 | } |
| 818 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 819 | |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 820 | /* Intermediate or complete response received. |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 821 | * Terminate string in check->bi->data buffer. |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 822 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 823 | if (check->bi->i < check->bi->size) |
| 824 | check->bi->data[check->bi->i] = '\0'; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 825 | else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 826 | check->bi->data[check->bi->i - 1] = '\0'; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 827 | done = 1; /* buffer full, don't wait for more data */ |
| 828 | } |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 829 | |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 830 | /* Run the checks... */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 831 | switch (check->type) { |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 832 | case PR_O2_HTTP_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 833 | if (!done && check->bi->i < strlen("HTTP/1.0 000\r")) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 834 | goto wait_more_data; |
| 835 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 836 | /* Check if the server speaks HTTP 1.X */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 837 | if ((check->bi->i < strlen("HTTP/1.0 000\r")) || |
| 838 | (memcmp(check->bi->data, "HTTP/1.", 7) != 0 || |
| 839 | (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) || |
| 840 | !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) || |
| 841 | !isdigit((unsigned char) *(check->bi->data + 11))) { |
| 842 | cut_crlf(check->bi->data); |
| 843 | set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 844 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 845 | goto out_wakeup; |
| 846 | } |
| 847 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 848 | check->code = str2uic(check->bi->data + 9); |
| 849 | desc = ltrim(check->bi->data + 12, ' '); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 850 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 851 | if ((s->proxy->options & PR_O_DISABLE404) && |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 852 | (s->state != SRV_ST_STOPPED) && (check->code == 404)) { |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 853 | /* 404 may be accepted as "stopping" only if the server was up */ |
| 854 | cut_crlf(desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 855 | set_server_check_status(check, HCHK_STATUS_L7OKCD, desc); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 856 | } |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 857 | else if (s->proxy->options2 & PR_O2_EXP_TYPE) { |
| 858 | /* Run content verification check... We know we have at least 13 chars */ |
| 859 | if (!httpchk_expect(s, done)) |
| 860 | goto wait_more_data; |
| 861 | } |
| 862 | /* check the reply : HTTP/1.X 2xx and 3xx are OK */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 863 | else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') { |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 864 | cut_crlf(desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 865 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 866 | } |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 867 | else { |
| 868 | cut_crlf(desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 869 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 870 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 871 | break; |
| 872 | |
| 873 | case PR_O2_SSL3_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 874 | if (!done && check->bi->i < 5) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 875 | goto wait_more_data; |
| 876 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 877 | /* Check for SSLv3 alert or handshake */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 878 | if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16)) |
| 879 | set_server_check_status(check, HCHK_STATUS_L6OK, NULL); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 880 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 881 | set_server_check_status(check, HCHK_STATUS_L6RSP, NULL); |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 882 | break; |
| 883 | |
| 884 | case PR_O2_SMTP_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 885 | if (!done && check->bi->i < strlen("000\r")) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 886 | goto wait_more_data; |
| 887 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 888 | /* Check if the server speaks SMTP */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 889 | if ((check->bi->i < strlen("000\r")) || |
| 890 | (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') || |
| 891 | !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) || |
| 892 | !isdigit((unsigned char) *(check->bi->data + 2))) { |
| 893 | cut_crlf(check->bi->data); |
| 894 | set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 895 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 896 | goto out_wakeup; |
| 897 | } |
| 898 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 899 | check->code = str2uic(check->bi->data); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 900 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 901 | desc = ltrim(check->bi->data + 3, ' '); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 902 | cut_crlf(desc); |
| 903 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 904 | /* Check for SMTP code 2xx (should be 250) */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 905 | if (*check->bi->data == '2') |
| 906 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 907 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 908 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 909 | break; |
| 910 | |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 911 | case PR_O2_LB_AGENT_CHK: { |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 912 | int status = HCHK_STATUS_CHECKED; |
| 913 | const char *hs = NULL; /* health status */ |
| 914 | const char *as = NULL; /* admin status */ |
| 915 | const char *ps = NULL; /* performance status */ |
| 916 | const char *err = NULL; /* first error to report */ |
| 917 | const char *wrn = NULL; /* first warning to report */ |
| 918 | char *cmd, *p; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 919 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 920 | /* We're getting an agent check response. The agent could |
| 921 | * have been disabled in the mean time with a long check |
| 922 | * still pending. It is important that we ignore the whole |
| 923 | * response. |
| 924 | */ |
| 925 | if (!(check->server->agent.state & CHK_ST_ENABLED)) |
| 926 | break; |
| 927 | |
| 928 | /* The agent supports strings made of a single line ended by the |
| 929 | * first CR ('\r') or LF ('\n'). This line is composed of words |
| 930 | * delimited by spaces (' '), tabs ('\t'), or commas (','). The |
| 931 | * line may optionally contained a description of a state change |
| 932 | * after a sharp ('#'), which is only considered if a health state |
| 933 | * is announced. |
| 934 | * |
| 935 | * Words may be composed of : |
| 936 | * - a numeric weight suffixed by the percent character ('%'). |
| 937 | * - a health status among "up", "down", "stopped", and "fail". |
| 938 | * - an admin status among "ready", "drain", "maint". |
| 939 | * |
| 940 | * These words may appear in any order. If multiple words of the |
| 941 | * same category appear, the last one wins. |
| 942 | */ |
| 943 | |
Willy Tarreau | 9809b78 | 2013-12-11 21:40:11 +0100 | [diff] [blame] | 944 | p = check->bi->data; |
| 945 | while (*p && *p != '\n' && *p != '\r') |
| 946 | p++; |
| 947 | |
| 948 | if (!*p) { |
| 949 | if (!done) |
| 950 | goto wait_more_data; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 951 | |
Willy Tarreau | 9809b78 | 2013-12-11 21:40:11 +0100 | [diff] [blame] | 952 | /* at least inform the admin that the agent is mis-behaving */ |
| 953 | set_server_check_status(check, check->status, "Ignoring incomplete line from agent"); |
| 954 | break; |
| 955 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 956 | |
Willy Tarreau | 9809b78 | 2013-12-11 21:40:11 +0100 | [diff] [blame] | 957 | *p = 0; |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 958 | cmd = check->bi->data; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 959 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 960 | while (*cmd) { |
| 961 | /* look for next word */ |
| 962 | if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') { |
| 963 | cmd++; |
| 964 | continue; |
| 965 | } |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 966 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 967 | if (*cmd == '#') { |
| 968 | /* this is the beginning of a health status description, |
| 969 | * skip the sharp and blanks. |
| 970 | */ |
| 971 | cmd++; |
| 972 | while (*cmd == '\t' || *cmd == ' ') |
| 973 | cmd++; |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 974 | break; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 975 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 976 | |
| 977 | /* find the end of the word so that we have a null-terminated |
| 978 | * word between <cmd> and <p>. |
| 979 | */ |
| 980 | p = cmd + 1; |
| 981 | while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',') |
| 982 | p++; |
| 983 | if (*p) |
| 984 | *p++ = 0; |
| 985 | |
| 986 | /* first, health statuses */ |
| 987 | if (strcasecmp(cmd, "up") == 0) { |
| 988 | check->health = check->rise + check->fall - 1; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 989 | status = HCHK_STATUS_L7OKD; |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 990 | hs = cmd; |
| 991 | } |
| 992 | else if (strcasecmp(cmd, "down") == 0) { |
| 993 | check->health = 0; |
| 994 | status = HCHK_STATUS_L7STS; |
| 995 | hs = cmd; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 996 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 997 | else if (strcasecmp(cmd, "stopped") == 0) { |
| 998 | check->health = 0; |
| 999 | status = HCHK_STATUS_L7STS; |
| 1000 | hs = cmd; |
| 1001 | } |
| 1002 | else if (strcasecmp(cmd, "fail") == 0) { |
| 1003 | check->health = 0; |
| 1004 | status = HCHK_STATUS_L7STS; |
| 1005 | hs = cmd; |
| 1006 | } |
| 1007 | /* admin statuses */ |
| 1008 | else if (strcasecmp(cmd, "ready") == 0) { |
| 1009 | as = cmd; |
| 1010 | } |
| 1011 | else if (strcasecmp(cmd, "drain") == 0) { |
| 1012 | as = cmd; |
| 1013 | } |
| 1014 | else if (strcasecmp(cmd, "maint") == 0) { |
| 1015 | as = cmd; |
| 1016 | } |
| 1017 | /* else try to parse a weight here and keep the last one */ |
| 1018 | else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) { |
| 1019 | ps = cmd; |
| 1020 | } |
| 1021 | else { |
| 1022 | /* keep a copy of the first error */ |
| 1023 | if (!err) |
| 1024 | err = cmd; |
| 1025 | } |
| 1026 | /* skip to next word */ |
| 1027 | cmd = p; |
| 1028 | } |
| 1029 | /* here, cmd points either to \0 or to the beginning of a |
| 1030 | * description. Skip possible leading spaces. |
| 1031 | */ |
| 1032 | while (*cmd == ' ' || *cmd == '\n') |
| 1033 | cmd++; |
| 1034 | |
| 1035 | /* First, update the admin status so that we avoid sending other |
| 1036 | * possibly useless warnings and can also update the health if |
| 1037 | * present after going back up. |
| 1038 | */ |
| 1039 | if (as) { |
| 1040 | if (strcasecmp(as, "drain") == 0) |
| 1041 | srv_adm_set_drain(check->server); |
| 1042 | else if (strcasecmp(as, "maint") == 0) |
| 1043 | srv_adm_set_maint(check->server); |
| 1044 | else |
| 1045 | srv_adm_set_ready(check->server); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1046 | } |
| 1047 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1048 | /* now change weights */ |
| 1049 | if (ps) { |
| 1050 | const char *msg; |
| 1051 | |
| 1052 | msg = server_parse_weight_change_request(s, ps); |
| 1053 | if (!wrn || !*wrn) |
| 1054 | wrn = msg; |
| 1055 | } |
| 1056 | |
| 1057 | /* and finally health status */ |
| 1058 | if (hs) { |
| 1059 | /* We'll report some of the warnings and errors we have |
| 1060 | * here. Down reports are critical, we leave them untouched. |
| 1061 | * Lack of report, or report of 'UP' leaves the room for |
| 1062 | * ERR first, then WARN. |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1063 | */ |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1064 | const char *msg = cmd; |
| 1065 | struct chunk *t; |
| 1066 | |
| 1067 | if (!*msg || status == HCHK_STATUS_L7OKD) { |
| 1068 | if (err && *err) |
| 1069 | msg = err; |
| 1070 | else if (wrn && *wrn) |
| 1071 | msg = wrn; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1072 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1073 | |
| 1074 | t = get_trash_chunk(); |
| 1075 | chunk_printf(t, "via agent : %s%s%s%s", |
| 1076 | hs, *msg ? " (" : "", |
| 1077 | msg, *msg ? ")" : ""); |
| 1078 | |
| 1079 | set_server_check_status(check, status, t->str); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1080 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1081 | else if (err && *err) { |
| 1082 | /* No status change but we'd like to report something odd. |
| 1083 | * Just report the current state and copy the message. |
| 1084 | */ |
| 1085 | chunk_printf(&trash, "agent reports an error : %s", err); |
| 1086 | set_server_check_status(check, status/*check->status*/, trash.str); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1087 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1088 | } |
| 1089 | else if (wrn && *wrn) { |
| 1090 | /* No status change but we'd like to report something odd. |
| 1091 | * Just report the current state and copy the message. |
| 1092 | */ |
| 1093 | chunk_printf(&trash, "agent warns : %s", wrn); |
| 1094 | set_server_check_status(check, status/*check->status*/, trash.str); |
| 1095 | } |
| 1096 | else |
| 1097 | set_server_check_status(check, status, NULL); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1098 | break; |
| 1099 | } |
| 1100 | |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1101 | case PR_O2_PGSQL_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1102 | if (!done && check->bi->i < 9) |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1103 | goto wait_more_data; |
| 1104 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1105 | if (check->bi->data[0] == 'R') { |
| 1106 | set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok"); |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1107 | } |
| 1108 | else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1109 | if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0)) |
| 1110 | desc = &check->bi->data[6]; |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1111 | else |
| 1112 | desc = "PostgreSQL unknown error"; |
| 1113 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1114 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1115 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1116 | break; |
| 1117 | |
| 1118 | case PR_O2_REDIS_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1119 | if (!done && check->bi->i < 7) |
Hervé COMMOWICK | ec032d6 | 2011-08-05 16:23:48 +0200 | [diff] [blame] | 1120 | goto wait_more_data; |
| 1121 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1122 | if (strcmp(check->bi->data, "+PONG\r\n") == 0) { |
| 1123 | set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok"); |
Hervé COMMOWICK | ec032d6 | 2011-08-05 16:23:48 +0200 | [diff] [blame] | 1124 | } |
| 1125 | else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1126 | set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data); |
Hervé COMMOWICK | ec032d6 | 2011-08-05 16:23:48 +0200 | [diff] [blame] | 1127 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1128 | break; |
| 1129 | |
| 1130 | case PR_O2_MYSQL_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1131 | if (!done && check->bi->i < 5) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1132 | goto wait_more_data; |
| 1133 | |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1134 | if (s->proxy->check_len == 0) { // old mode |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1135 | if (*(check->bi->data + 4) != '\xff') { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1136 | /* We set the MySQL Version in description for information purpose |
| 1137 | * FIXME : it can be cool to use MySQL Version for other purpose, |
| 1138 | * like mark as down old MySQL server. |
| 1139 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1140 | if (check->bi->i > 51) { |
| 1141 | desc = ltrim(check->bi->data + 5, ' '); |
| 1142 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1143 | } |
| 1144 | else { |
| 1145 | if (!done) |
| 1146 | goto wait_more_data; |
| 1147 | /* it seems we have a OK packet but without a valid length, |
| 1148 | * it must be a protocol error |
| 1149 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1150 | set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1151 | } |
Hervé COMMOWICK | 698ae00 | 2010-01-12 09:25:13 +0100 | [diff] [blame] | 1152 | } |
| 1153 | else { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1154 | /* An error message is attached in the Error packet */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1155 | desc = ltrim(check->bi->data + 7, ' '); |
| 1156 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1157 | } |
| 1158 | } else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1159 | unsigned int first_packet_len = ((unsigned int) *check->bi->data) + |
| 1160 | (((unsigned int) *(check->bi->data + 1)) << 8) + |
| 1161 | (((unsigned int) *(check->bi->data + 2)) << 16); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1162 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1163 | if (check->bi->i == first_packet_len + 4) { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1164 | /* MySQL Error packet always begin with field_count = 0xff */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1165 | if (*(check->bi->data + 4) != '\xff') { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1166 | /* We have only one MySQL packet and it is a Handshake Initialization packet |
| 1167 | * but we need to have a second packet to know if it is alright |
| 1168 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1169 | if (!done && check->bi->i < first_packet_len + 5) |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1170 | goto wait_more_data; |
| 1171 | } |
| 1172 | else { |
| 1173 | /* We have only one packet and it is an Error packet, |
| 1174 | * an error message is attached, so we can display it |
| 1175 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1176 | desc = &check->bi->data[7]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1177 | //Warning("onlyoneERR: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1178 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1179 | } |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1180 | } else if (check->bi->i > first_packet_len + 4) { |
| 1181 | unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) + |
| 1182 | (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) + |
| 1183 | (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1184 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1185 | if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1186 | /* We have 2 packets and that's good */ |
| 1187 | /* Check if the second packet is a MySQL Error packet or not */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1188 | if (*(check->bi->data + first_packet_len + 8) != '\xff') { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1189 | /* No error packet */ |
| 1190 | /* We set the MySQL Version in description for information purpose */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1191 | desc = &check->bi->data[5]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1192 | //Warning("2packetOK: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1193 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1194 | } |
| 1195 | else { |
| 1196 | /* An error message is attached in the Error packet |
| 1197 | * so we can display it ! :) |
| 1198 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1199 | desc = &check->bi->data[first_packet_len+11]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1200 | //Warning("2packetERR: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1201 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | } |
| 1205 | else { |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1206 | if (!done) |
| 1207 | goto wait_more_data; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1208 | /* it seems we have a Handshake Initialization packet but without a valid length, |
Hervé COMMOWICK | 698ae00 | 2010-01-12 09:25:13 +0100 | [diff] [blame] | 1209 | * it must be a protocol error |
| 1210 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1211 | desc = &check->bi->data[5]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1212 | //Warning("protoerr: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1213 | set_server_check_status(check, HCHK_STATUS_L7RSP, desc); |
Hervé COMMOWICK | 698ae00 | 2010-01-12 09:25:13 +0100 | [diff] [blame] | 1214 | } |
| 1215 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1216 | break; |
| 1217 | |
| 1218 | case PR_O2_LDAP_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1219 | if (!done && check->bi->i < 14) |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1220 | goto wait_more_data; |
| 1221 | |
| 1222 | /* Check if the server speaks LDAP (ASN.1/BER) |
| 1223 | * http://en.wikipedia.org/wiki/Basic_Encoding_Rules |
| 1224 | * http://tools.ietf.org/html/rfc4511 |
| 1225 | */ |
| 1226 | |
| 1227 | /* http://tools.ietf.org/html/rfc4511#section-4.1.1 |
| 1228 | * LDAPMessage: 0x30: SEQUENCE |
| 1229 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1230 | if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) { |
| 1231 | set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1232 | } |
| 1233 | else { |
| 1234 | /* size of LDAPMessage */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1235 | msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0; |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1236 | |
| 1237 | /* http://tools.ietf.org/html/rfc4511#section-4.2.2 |
| 1238 | * messageID: 0x02 0x01 0x01: INTEGER 1 |
| 1239 | * protocolOp: 0x61: bindResponse |
| 1240 | */ |
| 1241 | if ((msglen > 2) || |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1242 | (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) { |
| 1243 | set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1244 | |
| 1245 | goto out_wakeup; |
| 1246 | } |
| 1247 | |
| 1248 | /* size of bindResponse */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1249 | msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0; |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1250 | |
| 1251 | /* http://tools.ietf.org/html/rfc4511#section-4.1.9 |
| 1252 | * ldapResult: 0x0a 0x01: ENUMERATION |
| 1253 | */ |
| 1254 | if ((msglen > 4) || |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1255 | (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) { |
| 1256 | set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1257 | |
| 1258 | goto out_wakeup; |
| 1259 | } |
| 1260 | |
| 1261 | /* http://tools.ietf.org/html/rfc4511#section-4.1.9 |
| 1262 | * resultCode |
| 1263 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1264 | check->code = *(check->bi->data + msglen + 9); |
| 1265 | if (check->code) { |
| 1266 | set_server_check_status(check, HCHK_STATUS_L7STS, "See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1267 | } else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1268 | set_server_check_status(check, HCHK_STATUS_L7OKD, "Success"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1269 | } |
| 1270 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1271 | break; |
| 1272 | |
| 1273 | default: |
Willy Tarreau | 06559ac | 2013-12-05 01:53:08 +0100 | [diff] [blame] | 1274 | /* for other checks (eg: pure TCP), delegate to the main task */ |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1275 | break; |
| 1276 | } /* switch */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 1277 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1278 | out_wakeup: |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1279 | /* collect possible new errors */ |
| 1280 | if (conn->flags & CO_FL_ERROR) |
| 1281 | chk_report_conn_err(conn, 0, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1282 | |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 1283 | /* Reset the check buffer... */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1284 | *check->bi->data = '\0'; |
| 1285 | check->bi->i = 0; |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 1286 | |
Willy Tarreau | fd29cc5 | 2012-11-23 09:18:20 +0100 | [diff] [blame] | 1287 | /* Close the connection... We absolutely want to perform a hard close |
| 1288 | * and reset the connection if some data are pending, otherwise we end |
| 1289 | * up with many TIME_WAITs and eat all the source port range quickly. |
| 1290 | * To avoid sending RSTs all the time, we first try to drain pending |
| 1291 | * data. |
| 1292 | */ |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1293 | if (conn->xprt && conn->xprt->shutw) |
| 1294 | conn->xprt->shutw(conn, 0); |
Willy Tarreau | 2b57cb8 | 2013-06-10 19:56:38 +0200 | [diff] [blame] | 1295 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1296 | /* OK, let's not stay here forever */ |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 1297 | if (check->result == CHK_RES_FAILED) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1298 | conn->flags |= CO_FL_ERROR; |
| 1299 | |
Willy Tarreau | a522f80 | 2012-11-23 08:56:35 +0100 | [diff] [blame] | 1300 | __conn_data_stop_both(conn); |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 1301 | task_wakeup(t, TASK_WOKEN_IO); |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 1302 | return; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1303 | |
| 1304 | wait_more_data: |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 1305 | __conn_data_want_recv(conn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1306 | } |
| 1307 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1308 | /* |
| 1309 | * This function is used only for server health-checks. It handles connection |
| 1310 | * status updates including errors. If necessary, it wakes the check task up. |
| 1311 | * It always returns 0. |
| 1312 | */ |
| 1313 | static int wake_srv_chk(struct connection *conn) |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1314 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1315 | struct check *check = conn->owner; |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1316 | |
Willy Tarreau | 6c560da | 2012-11-24 11:14:45 +0100 | [diff] [blame] | 1317 | if (unlikely(conn->flags & CO_FL_ERROR)) { |
Willy Tarreau | 02b0f58 | 2013-12-03 15:42:33 +0100 | [diff] [blame] | 1318 | /* We may get error reports bypassing the I/O handlers, typically |
| 1319 | * the case when sending a pure TCP check which fails, then the I/O |
| 1320 | * handlers above are not called. This is completely handled by the |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1321 | * main processing task so let's simply wake it up. If we get here, |
| 1322 | * we expect errno to still be valid. |
| 1323 | */ |
| 1324 | chk_report_conn_err(conn, errno, 0); |
| 1325 | |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 1326 | __conn_data_stop_both(conn); |
| 1327 | task_wakeup(check->task, TASK_WOKEN_IO); |
| 1328 | } |
Willy Tarreau | 3be293f | 2014-02-05 18:31:24 +0100 | [diff] [blame] | 1329 | else if (!(conn->flags & (CO_FL_DATA_RD_ENA|CO_FL_DATA_WR_ENA|CO_FL_HANDSHAKE))) { |
| 1330 | /* we may get here if only a connection probe was required : we |
| 1331 | * don't have any data to send nor anything expected in response, |
| 1332 | * so the completion of the connection establishment is enough. |
| 1333 | */ |
| 1334 | task_wakeup(check->task, TASK_WOKEN_IO); |
| 1335 | } |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 1336 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 1337 | if (check->result != CHK_RES_UNKNOWN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1338 | /* We're here because nobody wants to handle the error, so we |
| 1339 | * sure want to abort the hard way. |
Willy Tarreau | 02b0f58 | 2013-12-03 15:42:33 +0100 | [diff] [blame] | 1340 | */ |
Willy Tarreau | 46be2e5 | 2014-01-20 12:10:52 +0100 | [diff] [blame] | 1341 | conn_drain(conn); |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 1342 | conn_force_close(conn); |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 1343 | } |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 1344 | return 0; |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1345 | } |
| 1346 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1347 | struct data_cb check_conn_cb = { |
| 1348 | .recv = event_srv_chk_r, |
| 1349 | .send = event_srv_chk_w, |
| 1350 | .wake = wake_srv_chk, |
| 1351 | }; |
| 1352 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1353 | /* |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1354 | * updates the server's weight during a warmup stage. Once the final weight is |
| 1355 | * reached, the task automatically stops. Note that any server status change |
| 1356 | * must have updated s->last_change accordingly. |
| 1357 | */ |
| 1358 | static struct task *server_warmup(struct task *t) |
| 1359 | { |
| 1360 | struct server *s = t->context; |
| 1361 | |
| 1362 | /* by default, plan on stopping the task */ |
| 1363 | t->expire = TICK_ETERNITY; |
Willy Tarreau | 2012521 | 2014-05-13 19:44:56 +0200 | [diff] [blame] | 1364 | if ((s->admin & SRV_ADMF_MAINT) || |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 1365 | (s->state != SRV_ST_STARTING)) |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1366 | return t; |
| 1367 | |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 1368 | /* recalculate the weights and update the state */ |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1369 | server_recalc_eweight(s); |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1370 | |
| 1371 | /* probably that we can refill this server with a bit more connections */ |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1372 | pendconn_grab_from_px(s); |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1373 | |
| 1374 | /* get back there in 1 second or 1/20th of the slowstart interval, |
| 1375 | * whichever is greater, resulting in small 5% steps. |
| 1376 | */ |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 1377 | if (s->state == SRV_ST_STARTING) |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1378 | t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))); |
| 1379 | return t; |
| 1380 | } |
| 1381 | |
| 1382 | /* |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1383 | * establish a server health-check that makes use of a connection. |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1384 | * |
| 1385 | * It can return one of : |
| 1386 | * - SN_ERR_NONE if everything's OK and tcpcheck_main() was not called |
| 1387 | * - SN_ERR_UP if if everything's OK and tcpcheck_main() was called |
| 1388 | * - SN_ERR_SRVTO if there are no more servers |
| 1389 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 1390 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1391 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1392 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 1393 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 1394 | * Note that we try to prevent the network stack from sending the ACK during the |
| 1395 | * connect() when a pure TCP check is used (without PROXY protocol). |
| 1396 | */ |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1397 | static int connect_conn_chk(struct task *t) |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1398 | { |
| 1399 | struct check *check = t->context; |
| 1400 | struct server *s = check->server; |
| 1401 | struct connection *conn = check->conn; |
| 1402 | struct protocol *proto; |
| 1403 | int ret; |
Willy Tarreau | f3d3482 | 2014-12-08 12:11:28 +0100 | [diff] [blame] | 1404 | int quickack; |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1405 | |
| 1406 | /* tcpcheck send/expect initialisation */ |
| 1407 | if (check->type == PR_O2_TCPCHK_CHK) |
| 1408 | check->current_step = NULL; |
| 1409 | |
| 1410 | /* prepare the check buffer. |
| 1411 | * This should not be used if check is the secondary agent check |
| 1412 | * of a server as s->proxy->check_req will relate to the |
| 1413 | * configuration of the primary check. Similarly, tcp-check uses |
| 1414 | * its own strings. |
| 1415 | */ |
| 1416 | if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) { |
| 1417 | bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len); |
| 1418 | |
| 1419 | /* we want to check if this host replies to HTTP or SSLv3 requests |
| 1420 | * so we'll send the request, and won't wake the checker up now. |
| 1421 | */ |
| 1422 | if ((check->type) == PR_O2_SSL3_CHK) { |
| 1423 | /* SSL requires that we put Unix time in the request */ |
| 1424 | int gmt_time = htonl(date.tv_sec); |
| 1425 | memcpy(check->bo->data + 11, &gmt_time, 4); |
| 1426 | } |
| 1427 | else if ((check->type) == PR_O2_HTTP_CHK) { |
| 1428 | if (s->proxy->options2 & PR_O2_CHK_SNDST) |
| 1429 | bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size)); |
Cyril Bonté | 32602d2 | 2015-01-30 00:07:07 +0100 | [diff] [blame] | 1430 | /* prevent HTTP keep-alive when "http-check expect" is used */ |
| 1431 | if (s->proxy->options2 & PR_O2_EXP_TYPE) |
| 1432 | bo_putstr(check->bo, "Connection: close\r\n"); |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1433 | bo_putstr(check->bo, "\r\n"); |
| 1434 | *check->bo->p = '\0'; /* to make gdb output easier to read */ |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | /* prepare a new connection */ |
| 1439 | conn_init(conn); |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 1440 | conn_prepare(conn, s->check_common.proto, check->xprt); |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1441 | conn_attach(conn, check, &check_conn_cb); |
| 1442 | conn->target = &s->obj_type; |
| 1443 | |
| 1444 | /* no client address */ |
| 1445 | clear_addr(&conn->addr.from); |
| 1446 | |
| 1447 | if (is_addr(&s->check_common.addr)) { |
| 1448 | |
| 1449 | /* we'll connect to the check addr specified on the server */ |
| 1450 | conn->addr.to = s->check_common.addr; |
| 1451 | proto = s->check_common.proto; |
| 1452 | } |
| 1453 | else { |
| 1454 | /* we'll connect to the addr on the server */ |
| 1455 | conn->addr.to = s->addr; |
| 1456 | proto = s->proto; |
| 1457 | } |
| 1458 | |
| 1459 | if (check->port) { |
| 1460 | set_host_port(&conn->addr.to, check->port); |
| 1461 | } |
| 1462 | |
Willy Tarreau | f3d3482 | 2014-12-08 12:11:28 +0100 | [diff] [blame] | 1463 | /* only plain tcp-check supports quick ACK */ |
| 1464 | quickack = check->type == 0 || check->type == PR_O2_TCPCHK_CHK; |
| 1465 | |
Willy Tarreau | d2a4959 | 2014-12-08 11:52:28 +0100 | [diff] [blame] | 1466 | if (check->type == PR_O2_TCPCHK_CHK && !LIST_ISEMPTY(&s->proxy->tcpcheck_rules)) { |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1467 | struct tcpcheck_rule *r = (struct tcpcheck_rule *) s->proxy->tcpcheck_rules.n; |
| 1468 | /* if first step is a 'connect', then tcpcheck_main must run it */ |
| 1469 | if (r->action == TCPCHK_ACT_CONNECT) { |
| 1470 | tcpcheck_main(conn); |
| 1471 | return SN_ERR_UP; |
| 1472 | } |
Willy Tarreau | f3d3482 | 2014-12-08 12:11:28 +0100 | [diff] [blame] | 1473 | if (r->action == TCPCHK_ACT_EXPECT) |
| 1474 | quickack = 0; |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | ret = SN_ERR_INTERNAL; |
| 1478 | if (proto->connect) |
Willy Tarreau | f3d3482 | 2014-12-08 12:11:28 +0100 | [diff] [blame] | 1479 | ret = proto->connect(conn, check->type, quickack ? 2 : 0); |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1480 | conn->flags |= CO_FL_WAKE_DATA; |
| 1481 | if (s->check.send_proxy) { |
| 1482 | conn->send_proxy_ofs = 1; |
| 1483 | conn->flags |= CO_FL_SEND_PROXY; |
| 1484 | } |
| 1485 | |
| 1486 | return ret; |
| 1487 | } |
| 1488 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1489 | static struct list pid_list = LIST_HEAD_INIT(pid_list); |
| 1490 | static struct pool_head *pool2_pid_list; |
| 1491 | |
| 1492 | void block_sigchld(void) |
| 1493 | { |
| 1494 | sigset_t set; |
| 1495 | sigemptyset(&set); |
| 1496 | sigaddset(&set, SIGCHLD); |
| 1497 | assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0); |
| 1498 | } |
| 1499 | |
| 1500 | void unblock_sigchld(void) |
| 1501 | { |
| 1502 | sigset_t set; |
| 1503 | sigemptyset(&set); |
| 1504 | assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0); |
| 1505 | } |
| 1506 | |
| 1507 | /* Call with SIGCHLD blocked */ |
| 1508 | static struct pid_list *pid_list_add(pid_t pid, struct task *t) |
| 1509 | { |
| 1510 | struct pid_list *elem; |
| 1511 | struct check *check = t->context; |
| 1512 | |
| 1513 | elem = pool_alloc2(pool2_pid_list); |
| 1514 | if (!elem) |
| 1515 | return NULL; |
| 1516 | elem->pid = pid; |
| 1517 | elem->t = t; |
| 1518 | elem->exited = 0; |
| 1519 | check->curpid = elem; |
| 1520 | LIST_INIT(&elem->list); |
| 1521 | LIST_ADD(&pid_list, &elem->list); |
| 1522 | return elem; |
| 1523 | } |
| 1524 | |
| 1525 | /* Blocks blocks and then unblocks SIGCHLD */ |
| 1526 | static void pid_list_del(struct pid_list *elem) |
| 1527 | { |
| 1528 | struct check *check; |
| 1529 | |
| 1530 | if (!elem) |
| 1531 | return; |
| 1532 | |
| 1533 | block_sigchld(); |
| 1534 | LIST_DEL(&elem->list); |
| 1535 | unblock_sigchld(); |
| 1536 | if (!elem->exited) |
| 1537 | kill(elem->pid, SIGTERM); |
| 1538 | |
| 1539 | check = elem->t->context; |
| 1540 | check->curpid = NULL; |
| 1541 | pool_free2(pool2_pid_list, elem); |
| 1542 | } |
| 1543 | |
| 1544 | /* Called from inside SIGCHLD handler, SIGCHLD is blocked */ |
| 1545 | static void pid_list_expire(pid_t pid, int status) |
| 1546 | { |
| 1547 | struct pid_list *elem; |
| 1548 | |
| 1549 | list_for_each_entry(elem, &pid_list, list) { |
| 1550 | if (elem->pid == pid) { |
| 1551 | elem->t->expire = now_ms; |
| 1552 | elem->status = status; |
| 1553 | elem->exited = 1; |
Cyril Bonté | 9dbcfab | 2014-08-07 01:55:39 +0200 | [diff] [blame] | 1554 | task_wakeup(elem->t, TASK_WOKEN_IO); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1555 | return; |
| 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | static void sigchld_handler(int signal) |
| 1561 | { |
| 1562 | pid_t pid; |
| 1563 | int status; |
| 1564 | while ((pid = waitpid(0, &status, WNOHANG)) > 0) |
| 1565 | pid_list_expire(pid, status); |
| 1566 | } |
| 1567 | |
| 1568 | static int init_pid_list(void) { |
| 1569 | struct sigaction action = { |
| 1570 | .sa_handler = sigchld_handler, |
| 1571 | .sa_flags = SA_NOCLDSTOP |
| 1572 | }; |
| 1573 | |
| 1574 | if (pool2_pid_list != NULL) |
| 1575 | /* Nothing to do */ |
| 1576 | return 0; |
| 1577 | |
| 1578 | if (sigaction(SIGCHLD, &action, NULL)) { |
| 1579 | Alert("Failed to set signal handler for external health checks: %s. Aborting.\n", |
| 1580 | strerror(errno)); |
| 1581 | return 1; |
| 1582 | } |
| 1583 | |
| 1584 | pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED); |
| 1585 | if (pool2_pid_list == NULL) { |
| 1586 | Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n", |
| 1587 | strerror(errno)); |
| 1588 | return 1; |
| 1589 | } |
| 1590 | |
| 1591 | return 0; |
| 1592 | } |
| 1593 | |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1594 | /* helper macro to set an environment variable and jump to a specific label on failure. */ |
| 1595 | #define EXTCHK_SETENV(check, envidx, value, fail) { if (extchk_setenv(check, envidx, value)) goto fail; } |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1596 | |
| 1597 | /* |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1598 | * helper function to allocate enough memory to store an environment variable. |
| 1599 | * It will also check that the environment variable is updatable, and silently |
| 1600 | * fail if not. |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1601 | */ |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1602 | static int extchk_setenv(struct check *check, int idx, const char *value) |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1603 | { |
| 1604 | int len, ret; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1605 | char *envname; |
| 1606 | int vmaxlen; |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1607 | |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1608 | if (idx < 0 || idx >= EXTCHK_SIZE) { |
| 1609 | Alert("Illegal environment variable index %d. Aborting.\n", idx); |
| 1610 | return 1; |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1611 | } |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1612 | |
| 1613 | envname = extcheck_envs[idx].name; |
| 1614 | vmaxlen = extcheck_envs[idx].vmaxlen; |
| 1615 | |
| 1616 | /* Check if the environment variable is already set, and silently reject |
| 1617 | * the update if this one is not updatable. */ |
| 1618 | if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx])) |
| 1619 | return 0; |
| 1620 | |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1621 | /* Instead of sending NOT_USED, sending an empty value is preferable */ |
| 1622 | if (strcmp(value, "NOT_USED") == 0) { |
| 1623 | value = ""; |
| 1624 | } |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1625 | |
| 1626 | len = strlen(envname) + 1; |
| 1627 | if (vmaxlen == EXTCHK_SIZE_EVAL_INIT) |
| 1628 | len += strlen(value); |
| 1629 | else |
| 1630 | len += vmaxlen; |
| 1631 | |
| 1632 | if (!check->envp[idx]) |
| 1633 | check->envp[idx] = malloc(len + 1); |
| 1634 | |
| 1635 | if (!check->envp[idx]) { |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1636 | Alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname); |
| 1637 | return 1; |
| 1638 | } |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1639 | ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value); |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1640 | if (ret < 0) { |
| 1641 | Alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno)); |
| 1642 | return 1; |
| 1643 | } |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1644 | else if (ret > len) { |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1645 | Alert("Environment variable '%s' was truncated. Aborting.\n", envname); |
| 1646 | return 1; |
| 1647 | } |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1648 | return 0; |
| 1649 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1650 | |
| 1651 | static int prepare_external_check(struct check *check) |
| 1652 | { |
| 1653 | struct server *s = check->server; |
| 1654 | struct proxy *px = s->proxy; |
| 1655 | struct listener *listener = NULL, *l; |
| 1656 | int i; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1657 | const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH; |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1658 | char buf[256]; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1659 | |
| 1660 | list_for_each_entry(l, &px->conf.listeners, by_fe) |
| 1661 | /* Use the first INET, INET6 or UNIX listener */ |
| 1662 | if (l->addr.ss_family == AF_INET || |
| 1663 | l->addr.ss_family == AF_INET6 || |
| 1664 | l->addr.ss_family == AF_UNIX) { |
| 1665 | listener = l; |
| 1666 | break; |
| 1667 | } |
| 1668 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1669 | check->curpid = NULL; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1670 | check->envp = calloc((EXTCHK_SIZE + 1), sizeof(char *)); |
| 1671 | if (!check->envp) { |
| 1672 | Alert("Failed to allocate memory for environment variables. Aborting\n"); |
| 1673 | goto err; |
| 1674 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1675 | |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1676 | check->argv = calloc(6, sizeof(char *)); |
| 1677 | if (!check->argv) { |
| 1678 | Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1679 | goto err; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1680 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1681 | |
| 1682 | check->argv[0] = px->check_command; |
| 1683 | |
Cyril Bonté | 777be86 | 2014-12-02 21:21:35 +0100 | [diff] [blame] | 1684 | if (!listener) { |
| 1685 | check->argv[1] = strdup("NOT_USED"); |
| 1686 | check->argv[2] = strdup("NOT_USED"); |
| 1687 | } |
| 1688 | else if (listener->addr.ss_family == AF_INET || |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1689 | listener->addr.ss_family == AF_INET6) { |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1690 | addr_to_str(&listener->addr, buf, sizeof(buf)); |
| 1691 | check->argv[1] = strdup(buf); |
| 1692 | port_to_str(&listener->addr, buf, sizeof(buf)); |
| 1693 | check->argv[2] = strdup(buf); |
Cyril Bonté | 777be86 | 2014-12-02 21:21:35 +0100 | [diff] [blame] | 1694 | } |
| 1695 | else if (listener->addr.ss_family == AF_UNIX) { |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1696 | const struct sockaddr_un *un; |
| 1697 | |
| 1698 | un = (struct sockaddr_un *)&listener->addr; |
| 1699 | check->argv[1] = strdup(un->sun_path); |
| 1700 | check->argv[2] = strdup("NOT_USED"); |
Cyril Bonté | 777be86 | 2014-12-02 21:21:35 +0100 | [diff] [blame] | 1701 | } |
| 1702 | else { |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1703 | Alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1704 | goto err; |
| 1705 | } |
| 1706 | |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1707 | addr_to_str(&s->addr, buf, sizeof(buf)); |
| 1708 | check->argv[3] = strdup(buf); |
| 1709 | port_to_str(&s->addr, buf, sizeof(buf)); |
| 1710 | check->argv[4] = strdup(buf); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1711 | |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1712 | for (i = 0; i < 5; i++) { |
| 1713 | if (!check->argv[i]) { |
| 1714 | Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1715 | goto err; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1716 | } |
| 1717 | } |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1718 | |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1719 | EXTCHK_SETENV(check, EXTCHK_PATH, path, err); |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1720 | /* Add proxy environment variables */ |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1721 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err); |
| 1722 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err); |
| 1723 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err); |
| 1724 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err); |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1725 | /* Add server environment variables */ |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1726 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err); |
| 1727 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err); |
| 1728 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err); |
| 1729 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err); |
| 1730 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err); |
| 1731 | EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), err); |
| 1732 | |
| 1733 | /* Ensure that we don't leave any hole in check->envp */ |
| 1734 | for (i = 0; i < EXTCHK_SIZE; i++) |
| 1735 | if (!check->envp[i]) |
| 1736 | EXTCHK_SETENV(check, i, "", err); |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1737 | |
Cyril Bonté | 99c5bf5 | 2014-08-07 01:55:38 +0200 | [diff] [blame] | 1738 | return 1; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1739 | err: |
| 1740 | if (check->envp) { |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1741 | for (i = 0; i < EXTCHK_SIZE; i++) |
Cyril Bonté | 9ede66b | 2014-12-02 21:21:36 +0100 | [diff] [blame] | 1742 | free(check->envp[i]); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1743 | free(check->envp); |
| 1744 | check->envp = NULL; |
| 1745 | } |
| 1746 | |
| 1747 | if (check->argv) { |
| 1748 | for (i = 1; i < 5; i++) |
| 1749 | free(check->argv[i]); |
| 1750 | free(check->argv); |
| 1751 | check->argv = NULL; |
| 1752 | } |
Cyril Bonté | 99c5bf5 | 2014-08-07 01:55:38 +0200 | [diff] [blame] | 1753 | return 0; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1754 | } |
| 1755 | |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1756 | /* |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1757 | * establish a server health-check that makes use of a process. |
| 1758 | * |
| 1759 | * It can return one of : |
| 1760 | * - SN_ERR_NONE if everything's OK |
| 1761 | * - SN_ERR_SRVTO if there are no more servers |
| 1762 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 1763 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1764 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1765 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 1766 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 1767 | * |
| 1768 | * Blocks and then unblocks SIGCHLD |
| 1769 | */ |
| 1770 | static int connect_proc_chk(struct task *t) |
| 1771 | { |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1772 | char buf[256]; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1773 | struct check *check = t->context; |
| 1774 | struct server *s = check->server; |
| 1775 | struct proxy *px = s->proxy; |
| 1776 | int status; |
| 1777 | pid_t pid; |
| 1778 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1779 | status = SN_ERR_RESOURCE; |
| 1780 | |
| 1781 | block_sigchld(); |
| 1782 | |
| 1783 | pid = fork(); |
| 1784 | if (pid < 0) { |
| 1785 | Alert("Failed to fork process for external health check: %s. Aborting.\n", |
| 1786 | strerror(errno)); |
| 1787 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 1788 | goto out; |
| 1789 | } |
| 1790 | if (pid == 0) { |
| 1791 | /* Child */ |
| 1792 | extern char **environ; |
| 1793 | environ = check->envp; |
Cyril Bonté | ac92a06 | 2014-12-27 22:28:38 +0100 | [diff] [blame] | 1794 | extchk_setenv(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf))); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1795 | execvp(px->check_command, check->argv); |
| 1796 | Alert("Failed to exec process for external health check: %s. Aborting.\n", |
| 1797 | strerror(errno)); |
| 1798 | exit(-1); |
| 1799 | } |
| 1800 | |
| 1801 | /* Parent */ |
| 1802 | if (check->result == CHK_RES_UNKNOWN) { |
| 1803 | if (pid_list_add(pid, t) != NULL) { |
| 1804 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 1805 | |
| 1806 | if (px->timeout.check && px->timeout.connect) { |
| 1807 | int t_con = tick_add(now_ms, px->timeout.connect); |
| 1808 | t->expire = tick_first(t->expire, t_con); |
| 1809 | } |
| 1810 | status = SN_ERR_NONE; |
| 1811 | goto out; |
| 1812 | } |
| 1813 | else { |
| 1814 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 1815 | } |
| 1816 | kill(pid, SIGTERM); /* process creation error */ |
| 1817 | } |
| 1818 | else |
| 1819 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 1820 | |
| 1821 | out: |
| 1822 | unblock_sigchld(); |
| 1823 | return status; |
| 1824 | } |
| 1825 | |
| 1826 | /* |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1827 | * manages a server health-check that uses a process. Returns |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1828 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 1829 | */ |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1830 | static struct task *process_chk_proc(struct task *t) |
| 1831 | { |
| 1832 | struct check *check = t->context; |
| 1833 | struct server *s = check->server; |
| 1834 | struct connection *conn = check->conn; |
| 1835 | int rv; |
| 1836 | int ret; |
| 1837 | int expired = tick_is_expired(t->expire, now_ms); |
| 1838 | |
| 1839 | if (!(check->state & CHK_ST_INPROGRESS)) { |
| 1840 | /* no check currently running */ |
| 1841 | if (!expired) /* woke up too early */ |
| 1842 | return t; |
| 1843 | |
| 1844 | /* we don't send any health-checks when the proxy is |
| 1845 | * stopped, the server should not be checked or the check |
| 1846 | * is disabled. |
| 1847 | */ |
| 1848 | if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) || |
| 1849 | s->proxy->state == PR_STSTOPPED) |
| 1850 | goto reschedule; |
| 1851 | |
| 1852 | /* we'll initiate a new check */ |
| 1853 | set_server_check_status(check, HCHK_STATUS_START, NULL); |
| 1854 | |
| 1855 | check->state |= CHK_ST_INPROGRESS; |
| 1856 | |
Simon Horman | dbf7019 | 2015-01-30 11:22:53 +0900 | [diff] [blame^] | 1857 | ret = connect_proc_chk(t); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1858 | switch (ret) { |
| 1859 | case SN_ERR_UP: |
| 1860 | return t; |
| 1861 | case SN_ERR_NONE: |
| 1862 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1863 | * to establish but only when timeout.check is set |
| 1864 | * as it may be to short for a full check otherwise |
| 1865 | */ |
| 1866 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 1867 | |
| 1868 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 1869 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 1870 | t->expire = tick_first(t->expire, t_con); |
| 1871 | } |
| 1872 | |
| 1873 | goto reschedule; |
| 1874 | |
| 1875 | case SN_ERR_SRVTO: /* ETIMEDOUT */ |
| 1876 | case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */ |
| 1877 | conn->flags |= CO_FL_ERROR; |
| 1878 | chk_report_conn_err(conn, errno, 0); |
| 1879 | break; |
| 1880 | case SN_ERR_PRXCOND: |
| 1881 | case SN_ERR_RESOURCE: |
| 1882 | case SN_ERR_INTERNAL: |
| 1883 | conn->flags |= CO_FL_ERROR; |
| 1884 | chk_report_conn_err(conn, 0, 0); |
| 1885 | break; |
| 1886 | } |
| 1887 | |
| 1888 | /* here, we have seen a synchronous error, no fd was allocated */ |
| 1889 | |
| 1890 | check->state &= ~CHK_ST_INPROGRESS; |
| 1891 | check_notify_failure(check); |
| 1892 | |
| 1893 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1894 | * to establish but only when timeout.check is set |
| 1895 | * as it may be to short for a full check otherwise |
| 1896 | */ |
| 1897 | while (tick_is_expired(t->expire, now_ms)) { |
| 1898 | int t_con; |
| 1899 | |
| 1900 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 1901 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 1902 | |
| 1903 | if (s->proxy->timeout.check) |
| 1904 | t->expire = tick_first(t->expire, t_con); |
| 1905 | } |
| 1906 | } |
| 1907 | else { |
| 1908 | /* there was a test running. |
| 1909 | * First, let's check whether there was an uncaught error, |
| 1910 | * which can happen on connect timeout or error. |
| 1911 | */ |
| 1912 | if (check->result == CHK_RES_UNKNOWN) { |
| 1913 | /* good connection is enough for pure TCP check */ |
| 1914 | struct pid_list *elem = check->curpid; |
| 1915 | int status = HCHK_STATUS_UNKNOWN; |
| 1916 | |
| 1917 | if (elem->exited) { |
| 1918 | status = elem->status; /* Save in case the process exits between use below */ |
| 1919 | if (!WIFEXITED(status)) |
| 1920 | check->code = -1; |
| 1921 | else |
| 1922 | check->code = WEXITSTATUS(status); |
| 1923 | if (!WIFEXITED(status) || WEXITSTATUS(status)) |
| 1924 | status = HCHK_STATUS_PROCERR; |
| 1925 | else |
| 1926 | status = HCHK_STATUS_PROCOK; |
| 1927 | } else if (expired) { |
| 1928 | status = HCHK_STATUS_PROCTOUT; |
Willy Tarreau | dc3d190 | 2014-07-08 00:56:27 +0200 | [diff] [blame] | 1929 | Warning("kill %d\n", (int)elem->pid); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1930 | kill(elem->pid, SIGTERM); |
| 1931 | } |
| 1932 | set_server_check_status(check, status, NULL); |
| 1933 | } |
| 1934 | |
| 1935 | if (check->result == CHK_RES_FAILED) { |
| 1936 | /* a failure or timeout detected */ |
| 1937 | check_notify_failure(check); |
| 1938 | } |
| 1939 | else if (check->result == CHK_RES_CONDPASS) { |
| 1940 | /* check is OK but asks for stopping mode */ |
| 1941 | check_notify_stopping(check); |
| 1942 | } |
| 1943 | else if (check->result == CHK_RES_PASSED) { |
| 1944 | /* a success was detected */ |
| 1945 | check_notify_success(check); |
| 1946 | } |
| 1947 | check->state &= ~CHK_ST_INPROGRESS; |
| 1948 | |
| 1949 | pid_list_del(check->curpid); |
| 1950 | |
| 1951 | rv = 0; |
| 1952 | if (global.spread_checks > 0) { |
| 1953 | rv = srv_getinter(check) * global.spread_checks / 100; |
| 1954 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
| 1955 | } |
| 1956 | t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv)); |
| 1957 | } |
| 1958 | |
| 1959 | reschedule: |
| 1960 | while (tick_is_expired(t->expire, now_ms)) |
| 1961 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 1962 | return t; |
| 1963 | } |
| 1964 | |
| 1965 | /* |
| 1966 | * manages a server health-check that uses a connection. Returns |
| 1967 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 1968 | */ |
| 1969 | static struct task *process_chk_conn(struct task *t) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1970 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1971 | struct check *check = t->context; |
| 1972 | struct server *s = check->server; |
| 1973 | struct connection *conn = check->conn; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1974 | int rv; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1975 | int ret; |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1976 | int expired = tick_is_expired(t->expire, now_ms); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1977 | |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1978 | if (!(check->state & CHK_ST_INPROGRESS)) { |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1979 | /* no check currently running */ |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1980 | if (!expired) /* woke up too early */ |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1981 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1982 | |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 1983 | /* we don't send any health-checks when the proxy is |
| 1984 | * stopped, the server should not be checked or the check |
| 1985 | * is disabled. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1986 | */ |
Willy Tarreau | 0d924cc | 2013-12-11 21:26:24 +0100 | [diff] [blame] | 1987 | if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) || |
Willy Tarreau | 33a08db | 2013-12-11 21:03:31 +0100 | [diff] [blame] | 1988 | s->proxy->state == PR_STSTOPPED) |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1989 | goto reschedule; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1990 | |
| 1991 | /* we'll initiate a new check */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1992 | set_server_check_status(check, HCHK_STATUS_START, NULL); |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1993 | |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1994 | check->state |= CHK_ST_INPROGRESS; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1995 | check->bi->p = check->bi->data; |
| 1996 | check->bi->i = 0; |
| 1997 | check->bo->p = check->bo->data; |
| 1998 | check->bo->o = 0; |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1999 | |
Simon Horman | dbf7019 | 2015-01-30 11:22:53 +0900 | [diff] [blame^] | 2000 | ret = connect_conn_chk(t); |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 2001 | switch (ret) { |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 2002 | case SN_ERR_UP: |
| 2003 | return t; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 2004 | case SN_ERR_NONE: |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 2005 | /* we allow up to min(inter, timeout.connect) for a connection |
| 2006 | * to establish but only when timeout.check is set |
| 2007 | * as it may be to short for a full check otherwise |
| 2008 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2009 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 2010 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 2011 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 2012 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 2013 | t->expire = tick_first(t->expire, t_con); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2014 | } |
Willy Tarreau | 06559ac | 2013-12-05 01:53:08 +0100 | [diff] [blame] | 2015 | |
| 2016 | if (check->type) |
| 2017 | conn_data_want_recv(conn); /* prepare for reading a possible reply */ |
| 2018 | |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 2019 | goto reschedule; |
| 2020 | |
| 2021 | case SN_ERR_SRVTO: /* ETIMEDOUT */ |
| 2022 | case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */ |
Willy Tarreau | 4bd07de | 2014-01-24 16:10:57 +0100 | [diff] [blame] | 2023 | conn->flags |= CO_FL_ERROR; |
| 2024 | chk_report_conn_err(conn, errno, 0); |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 2025 | break; |
| 2026 | case SN_ERR_PRXCOND: |
| 2027 | case SN_ERR_RESOURCE: |
| 2028 | case SN_ERR_INTERNAL: |
Willy Tarreau | 4bd07de | 2014-01-24 16:10:57 +0100 | [diff] [blame] | 2029 | conn->flags |= CO_FL_ERROR; |
| 2030 | chk_report_conn_err(conn, 0, 0); |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 2031 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2032 | } |
| 2033 | |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 2034 | /* here, we have seen a synchronous error, no fd was allocated */ |
Willy Tarreau | 6b0a850 | 2012-11-23 08:51:32 +0100 | [diff] [blame] | 2035 | |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 2036 | check->state &= ~CHK_ST_INPROGRESS; |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 2037 | check_notify_failure(check); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2038 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 2039 | /* we allow up to min(inter, timeout.connect) for a connection |
| 2040 | * to establish but only when timeout.check is set |
| 2041 | * as it may be to short for a full check otherwise |
| 2042 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2043 | while (tick_is_expired(t->expire, now_ms)) { |
| 2044 | int t_con; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 2045 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2046 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2047 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 2048 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2049 | if (s->proxy->timeout.check) |
| 2050 | t->expire = tick_first(t->expire, t_con); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 2051 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2052 | } |
| 2053 | else { |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 2054 | /* there was a test running. |
| 2055 | * First, let's check whether there was an uncaught error, |
| 2056 | * which can happen on connect timeout or error. |
| 2057 | */ |
Simon Horman | ccaabcd | 2014-06-20 12:29:47 +0900 | [diff] [blame] | 2058 | if (check->result == CHK_RES_UNKNOWN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 2059 | /* good connection is enough for pure TCP check */ |
| 2060 | if ((conn->flags & CO_FL_CONNECTED) && !check->type) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2061 | if (check->use_ssl) |
| 2062 | set_server_check_status(check, HCHK_STATUS_L6OK, NULL); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 2063 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2064 | set_server_check_status(check, HCHK_STATUS_L4OK, NULL); |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 2065 | } |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 2066 | else if ((conn->flags & CO_FL_ERROR) || expired) { |
| 2067 | chk_report_conn_err(conn, 0, expired); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 2068 | } |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 2069 | else |
| 2070 | goto out_wait; /* timeout not reached, wait again */ |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 2071 | } |
| 2072 | |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 2073 | /* check complete or aborted */ |
Willy Tarreau | 5ba04f6 | 2013-02-12 15:23:12 +0100 | [diff] [blame] | 2074 | if (conn->xprt) { |
| 2075 | /* The check was aborted and the connection was not yet closed. |
| 2076 | * This can happen upon timeout, or when an external event such |
| 2077 | * as a failed response coupled with "observe layer7" caused the |
| 2078 | * server state to be suddenly changed. |
| 2079 | */ |
Willy Tarreau | 46be2e5 | 2014-01-20 12:10:52 +0100 | [diff] [blame] | 2080 | conn_drain(conn); |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 2081 | conn_force_close(conn); |
Willy Tarreau | 5ba04f6 | 2013-02-12 15:23:12 +0100 | [diff] [blame] | 2082 | } |
| 2083 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 2084 | if (check->result == CHK_RES_FAILED) { |
| 2085 | /* a failure or timeout detected */ |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 2086 | check_notify_failure(check); |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 2087 | } |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 2088 | else if (check->result == CHK_RES_CONDPASS) { |
| 2089 | /* check is OK but asks for stopping mode */ |
| 2090 | check_notify_stopping(check); |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 2091 | } |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 2092 | else if (check->result == CHK_RES_PASSED) { |
| 2093 | /* a success was detected */ |
| 2094 | check_notify_success(check); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2095 | } |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 2096 | check->state &= ~CHK_ST_INPROGRESS; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2097 | |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 2098 | rv = 0; |
| 2099 | if (global.spread_checks > 0) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2100 | rv = srv_getinter(check) * global.spread_checks / 100; |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 2101 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2102 | } |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2103 | t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2104 | } |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 2105 | |
| 2106 | reschedule: |
| 2107 | while (tick_is_expired(t->expire, now_ms)) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2108 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 2109 | out_wait: |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 2110 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2111 | } |
| 2112 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 2113 | /* |
| 2114 | * manages a server health-check. Returns |
| 2115 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 2116 | */ |
| 2117 | static struct task *process_chk(struct task *t) |
| 2118 | { |
| 2119 | struct check *check = t->context; |
| 2120 | |
| 2121 | if (check->type == PR_O2_EXT_CHK) |
| 2122 | return process_chk_proc(t); |
| 2123 | return process_chk_conn(t); |
| 2124 | } |
| 2125 | |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 2126 | static int start_check_task(struct check *check, int mininter, |
| 2127 | int nbcheck, int srvpos) |
| 2128 | { |
| 2129 | struct task *t; |
| 2130 | /* task for the check */ |
| 2131 | if ((t = task_new()) == NULL) { |
| 2132 | Alert("Starting [%s:%s] check: out of memory.\n", |
| 2133 | check->server->proxy->id, check->server->id); |
| 2134 | return 0; |
| 2135 | } |
| 2136 | |
| 2137 | check->task = t; |
| 2138 | t->process = process_chk; |
| 2139 | t->context = check; |
| 2140 | |
Willy Tarreau | 1746eec | 2014-04-25 10:46:47 +0200 | [diff] [blame] | 2141 | if (mininter < srv_getinter(check)) |
| 2142 | mininter = srv_getinter(check); |
| 2143 | |
| 2144 | if (global.max_spread_checks && mininter > global.max_spread_checks) |
| 2145 | mininter = global.max_spread_checks; |
| 2146 | |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 2147 | /* check this every ms */ |
Willy Tarreau | 1746eec | 2014-04-25 10:46:47 +0200 | [diff] [blame] | 2148 | t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck)); |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 2149 | check->start = now; |
| 2150 | task_queue(t); |
| 2151 | |
| 2152 | return 1; |
| 2153 | } |
| 2154 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2155 | /* |
| 2156 | * Start health-check. |
| 2157 | * Returns 0 if OK, -1 if error, and prints the error in this case. |
| 2158 | */ |
| 2159 | int start_checks() { |
| 2160 | |
| 2161 | struct proxy *px; |
| 2162 | struct server *s; |
| 2163 | struct task *t; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2164 | int nbcheck=0, mininter=0, srvpos=0; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2165 | |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 2166 | /* 1- count the checkers to run simultaneously. |
| 2167 | * We also determine the minimum interval among all of those which |
| 2168 | * have an interval larger than SRV_CHK_INTER_THRES. This interval |
| 2169 | * will be used to spread their start-up date. Those which have |
Jamie Gloudon | 801a0a3 | 2012-08-25 00:18:33 -0400 | [diff] [blame] | 2170 | * a shorter interval will start independently and will not dictate |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 2171 | * too short an interval for all others. |
| 2172 | */ |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2173 | for (px = proxy; px; px = px->next) { |
| 2174 | for (s = px->srv; s; s = s->next) { |
Willy Tarreau | e7b7348 | 2013-11-21 11:50:50 +0100 | [diff] [blame] | 2175 | if (s->slowstart) { |
| 2176 | if ((t = task_new()) == NULL) { |
| 2177 | Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 2178 | return -1; |
| 2179 | } |
| 2180 | /* We need a warmup task that will be called when the server |
| 2181 | * state switches from down to up. |
| 2182 | */ |
| 2183 | s->warmup = t; |
| 2184 | t->process = server_warmup; |
| 2185 | t->context = s; |
| 2186 | t->expire = TICK_ETERNITY; |
| 2187 | } |
| 2188 | |
Willy Tarreau | d8514a2 | 2013-12-11 21:10:14 +0100 | [diff] [blame] | 2189 | if (s->check.state & CHK_ST_CONFIGURED) { |
| 2190 | nbcheck++; |
| 2191 | if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) && |
| 2192 | (!mininter || mininter > srv_getinter(&s->check))) |
| 2193 | mininter = srv_getinter(&s->check); |
| 2194 | } |
Willy Tarreau | 15f3910 | 2013-12-11 20:41:18 +0100 | [diff] [blame] | 2195 | |
Willy Tarreau | d8514a2 | 2013-12-11 21:10:14 +0100 | [diff] [blame] | 2196 | if (s->agent.state & CHK_ST_CONFIGURED) { |
| 2197 | nbcheck++; |
| 2198 | if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) && |
| 2199 | (!mininter || mininter > srv_getinter(&s->agent))) |
| 2200 | mininter = srv_getinter(&s->agent); |
| 2201 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2202 | } |
| 2203 | } |
| 2204 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2205 | if (!nbcheck) |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2206 | return 0; |
| 2207 | |
| 2208 | srand((unsigned)time(NULL)); |
| 2209 | |
| 2210 | /* |
| 2211 | * 2- start them as far as possible from each others. For this, we will |
| 2212 | * start them after their interval set to the min interval divided by |
| 2213 | * the number of servers, weighted by the server's position in the list. |
| 2214 | */ |
| 2215 | for (px = proxy; px; px = px->next) { |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 2216 | if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) { |
| 2217 | if (init_pid_list()) { |
| 2218 | Alert("Starting [%s] check: out of memory.\n", px->id); |
| 2219 | return -1; |
| 2220 | } |
| 2221 | } |
| 2222 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2223 | for (s = px->srv; s; s = s->next) { |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2224 | /* A task for the main check */ |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 2225 | if (s->check.state & CHK_ST_CONFIGURED) { |
Cyril Bonté | 99c5bf5 | 2014-08-07 01:55:38 +0200 | [diff] [blame] | 2226 | if (s->check.type == PR_O2_EXT_CHK) { |
| 2227 | if (!prepare_external_check(&s->check)) |
| 2228 | return -1; |
| 2229 | } |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2230 | if (!start_check_task(&s->check, mininter, nbcheck, srvpos)) |
| 2231 | return -1; |
| 2232 | srvpos++; |
| 2233 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2234 | |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2235 | /* A task for a auxiliary agent check */ |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 2236 | if (s->agent.state & CHK_ST_CONFIGURED) { |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2237 | if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) { |
| 2238 | return -1; |
| 2239 | } |
| 2240 | srvpos++; |
| 2241 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2242 | } |
| 2243 | } |
| 2244 | return 0; |
| 2245 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2246 | |
| 2247 | /* |
Willy Tarreau | 5b3a202 | 2012-09-28 15:01:02 +0200 | [diff] [blame] | 2248 | * Perform content verification check on data in s->check.buffer buffer. |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2249 | * The buffer MUST be terminated by a null byte before calling this function. |
| 2250 | * Sets server status appropriately. The caller is responsible for ensuring |
| 2251 | * that the buffer contains at least 13 characters. If <done> is zero, we may |
| 2252 | * return 0 to indicate that data is required to decide of a match. |
| 2253 | */ |
| 2254 | static int httpchk_expect(struct server *s, int done) |
| 2255 | { |
| 2256 | static char status_msg[] = "HTTP status check returned code <000>"; |
| 2257 | char status_code[] = "000"; |
| 2258 | char *contentptr; |
| 2259 | int crlf; |
| 2260 | int ret; |
| 2261 | |
| 2262 | switch (s->proxy->options2 & PR_O2_EXP_TYPE) { |
| 2263 | case PR_O2_EXP_STS: |
| 2264 | case PR_O2_EXP_RSTS: |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 2265 | memcpy(status_code, s->check.bi->data + 9, 3); |
| 2266 | memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2267 | |
| 2268 | if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS) |
| 2269 | ret = strncmp(s->proxy->expect_str, status_code, 3) == 0; |
| 2270 | else |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 2271 | ret = regex_exec(s->proxy->expect_regex, status_code); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2272 | |
| 2273 | /* we necessarily have the response, so there are no partial failures */ |
| 2274 | if (s->proxy->options2 & PR_O2_EXP_INV) |
| 2275 | ret = !ret; |
| 2276 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2277 | set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2278 | break; |
| 2279 | |
| 2280 | case PR_O2_EXP_STR: |
| 2281 | case PR_O2_EXP_RSTR: |
| 2282 | /* very simple response parser: ignore CR and only count consecutive LFs, |
| 2283 | * stop with contentptr pointing to first char after the double CRLF or |
| 2284 | * to '\0' if crlf < 2. |
| 2285 | */ |
| 2286 | crlf = 0; |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 2287 | for (contentptr = s->check.bi->data; *contentptr; contentptr++) { |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2288 | if (crlf >= 2) |
| 2289 | break; |
| 2290 | if (*contentptr == '\r') |
| 2291 | continue; |
| 2292 | else if (*contentptr == '\n') |
| 2293 | crlf++; |
| 2294 | else |
| 2295 | crlf = 0; |
| 2296 | } |
| 2297 | |
| 2298 | /* Check that response contains a body... */ |
| 2299 | if (crlf < 2) { |
| 2300 | if (!done) |
| 2301 | return 0; |
| 2302 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2303 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2304 | "HTTP content check could not find a response body"); |
| 2305 | return 1; |
| 2306 | } |
| 2307 | |
| 2308 | /* Check that response body is not empty... */ |
| 2309 | if (*contentptr == '\0') { |
Willy Tarreau | a164fb5 | 2011-04-13 09:32:41 +0200 | [diff] [blame] | 2310 | if (!done) |
| 2311 | return 0; |
| 2312 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2313 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2314 | "HTTP content check found empty response body"); |
| 2315 | return 1; |
| 2316 | } |
| 2317 | |
| 2318 | /* Check the response content against the supplied string |
| 2319 | * or regex... */ |
| 2320 | if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR) |
| 2321 | ret = strstr(contentptr, s->proxy->expect_str) != NULL; |
| 2322 | else |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 2323 | ret = regex_exec(s->proxy->expect_regex, contentptr); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2324 | |
| 2325 | /* if we don't match, we may need to wait more */ |
| 2326 | if (!ret && !done) |
| 2327 | return 0; |
| 2328 | |
| 2329 | if (ret) { |
| 2330 | /* content matched */ |
| 2331 | if (s->proxy->options2 & PR_O2_EXP_INV) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2332 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2333 | "HTTP check matched unwanted content"); |
| 2334 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2335 | set_server_check_status(&s->check, HCHK_STATUS_L7OKD, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2336 | "HTTP content check matched"); |
| 2337 | } |
| 2338 | else { |
| 2339 | if (s->proxy->options2 & PR_O2_EXP_INV) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2340 | set_server_check_status(&s->check, HCHK_STATUS_L7OKD, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2341 | "HTTP check did not match unwanted content"); |
| 2342 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2343 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2344 | "HTTP content check did not match"); |
| 2345 | } |
| 2346 | break; |
| 2347 | } |
| 2348 | return 1; |
| 2349 | } |
| 2350 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2351 | /* |
| 2352 | * return the id of a step in a send/expect session |
| 2353 | */ |
| 2354 | static int tcpcheck_get_step_id(struct server *s) |
| 2355 | { |
| 2356 | struct tcpcheck_rule *cur = NULL, *next = NULL; |
| 2357 | int i = 0; |
| 2358 | |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 2359 | /* not even started anything yet => step 0 = initial connect */ |
| 2360 | if (!s->check.current_step) |
| 2361 | return 0; |
| 2362 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2363 | cur = s->check.last_started_step; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2364 | |
| 2365 | /* no step => first step */ |
| 2366 | if (cur == NULL) |
| 2367 | return 1; |
| 2368 | |
| 2369 | /* increment i until current step */ |
| 2370 | list_for_each_entry(next, &s->proxy->tcpcheck_rules, list) { |
| 2371 | if (next->list.p == &cur->list) |
| 2372 | break; |
| 2373 | ++i; |
| 2374 | } |
| 2375 | |
| 2376 | return i; |
| 2377 | } |
| 2378 | |
| 2379 | static void tcpcheck_main(struct connection *conn) |
| 2380 | { |
| 2381 | char *contentptr; |
Willy Tarreau | f3d3482 | 2014-12-08 12:11:28 +0100 | [diff] [blame] | 2382 | struct tcpcheck_rule *cur, *next; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2383 | int done = 0, ret = 0; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2384 | struct check *check = conn->owner; |
| 2385 | struct server *s = check->server; |
| 2386 | struct task *t = check->task; |
Willy Tarreau | ef95395 | 2014-10-02 14:30:14 +0200 | [diff] [blame] | 2387 | struct list *head = &s->proxy->tcpcheck_rules; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2388 | |
Willy Tarreau | ef95395 | 2014-10-02 14:30:14 +0200 | [diff] [blame] | 2389 | /* here, we know that the check is complete or that it failed */ |
| 2390 | if (check->result != CHK_RES_UNKNOWN) |
| 2391 | goto out_end_tcpcheck; |
| 2392 | |
| 2393 | /* We have 4 possibilities here : |
| 2394 | * 1. we've not yet attempted step 1, and step 1 is a connect, so no |
| 2395 | * connection attempt was made yet ; |
| 2396 | * 2. we've not yet attempted step 1, and step 1 is a not connect or |
| 2397 | * does not exist (no rule), so a connection attempt was made |
| 2398 | * before coming here. |
| 2399 | * 3. we're coming back after having started with step 1, so we may |
| 2400 | * be waiting for a connection attempt to complete. |
| 2401 | * 4. the connection + handshake are complete |
| 2402 | * |
| 2403 | * #2 and #3 are quite similar, we want both the connection and the |
| 2404 | * handshake to complete before going any further. Thus we must always |
| 2405 | * wait for a connection to complete unless we're before and existing |
| 2406 | * step 1. |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2407 | */ |
Willy Tarreau | ef95395 | 2014-10-02 14:30:14 +0200 | [diff] [blame] | 2408 | if ((!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE)) && |
| 2409 | (check->current_step || LIST_ISEMPTY(head))) { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2410 | /* we allow up to min(inter, timeout.connect) for a connection |
| 2411 | * to establish but only when timeout.check is set |
| 2412 | * as it may be to short for a full check otherwise |
| 2413 | */ |
| 2414 | while (tick_is_expired(t->expire, now_ms)) { |
| 2415 | int t_con; |
| 2416 | |
| 2417 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 2418 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 2419 | |
| 2420 | if (s->proxy->timeout.check) |
| 2421 | t->expire = tick_first(t->expire, t_con); |
| 2422 | } |
| 2423 | return; |
| 2424 | } |
| 2425 | |
Willy Tarreau | ef95395 | 2014-10-02 14:30:14 +0200 | [diff] [blame] | 2426 | /* special case: option tcp-check with no rule, a connect is enough */ |
| 2427 | if (LIST_ISEMPTY(head)) { |
| 2428 | set_server_check_status(check, HCHK_STATUS_L4OK, NULL); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2429 | goto out_end_tcpcheck; |
Willy Tarreau | ef95395 | 2014-10-02 14:30:14 +0200 | [diff] [blame] | 2430 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2431 | |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 2432 | /* no step means first step initialisation */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2433 | if (check->current_step == NULL) { |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 2434 | check->last_started_step = NULL; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2435 | check->bo->p = check->bo->data; |
| 2436 | check->bo->o = 0; |
| 2437 | check->bi->p = check->bi->data; |
| 2438 | check->bi->i = 0; |
| 2439 | cur = check->current_step = LIST_ELEM(head->n, struct tcpcheck_rule *, list); |
| 2440 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 2441 | if (s->proxy->timeout.check) |
| 2442 | t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check); |
| 2443 | } |
| 2444 | /* keep on processing step */ |
| 2445 | else { |
| 2446 | cur = check->current_step; |
| 2447 | } |
| 2448 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2449 | /* It's only the rules which will enable send/recv */ |
| 2450 | __conn_data_stop_both(conn); |
| 2451 | |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2452 | while (1) { |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2453 | /* we have to try to flush the output buffer before reading, at the end, |
| 2454 | * or if we're about to send a string that does not fit in the remaining space. |
| 2455 | */ |
| 2456 | if (check->bo->o && |
| 2457 | (&cur->list == head || |
| 2458 | check->current_step->action != TCPCHK_ACT_SEND || |
| 2459 | check->current_step->string_len >= buffer_total_space(check->bo))) { |
| 2460 | |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 2461 | if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) { |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2462 | if (conn->flags & CO_FL_ERROR) { |
| 2463 | chk_report_conn_err(conn, errno, 0); |
| 2464 | __conn_data_stop_both(conn); |
| 2465 | goto out_end_tcpcheck; |
| 2466 | } |
| 2467 | goto out_need_io; |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2468 | } |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2469 | } |
| 2470 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2471 | /* did we reach the end ? If so, let's check that everything was sent */ |
| 2472 | if (&cur->list == head) { |
| 2473 | if (check->bo->o) |
| 2474 | goto out_need_io; |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2475 | break; |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2476 | } |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2477 | |
Willy Tarreau | f3d3482 | 2014-12-08 12:11:28 +0100 | [diff] [blame] | 2478 | /* have 'next' point to the next rule or NULL if we're on the last one */ |
| 2479 | next = (struct tcpcheck_rule *)cur->list.n; |
| 2480 | if (&next->list == head) |
| 2481 | next = NULL; |
| 2482 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2483 | if (check->current_step->action == TCPCHK_ACT_CONNECT) { |
| 2484 | struct protocol *proto; |
| 2485 | struct xprt_ops *xprt; |
| 2486 | |
| 2487 | /* mark the step as started */ |
| 2488 | check->last_started_step = check->current_step; |
| 2489 | /* first, shut existing connection */ |
| 2490 | conn_force_close(conn); |
| 2491 | |
| 2492 | /* prepare new connection */ |
| 2493 | /* initialization */ |
| 2494 | conn_init(conn); |
| 2495 | conn_attach(conn, check, &check_conn_cb); |
| 2496 | conn->target = &s->obj_type; |
| 2497 | |
| 2498 | /* no client address */ |
| 2499 | clear_addr(&conn->addr.from); |
| 2500 | |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 2501 | if (is_addr(&s->check_common.addr)) { |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2502 | /* we'll connect to the check addr specified on the server */ |
| 2503 | conn->addr.to = s->check_common.addr; |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 2504 | proto = s->check_common.proto; |
| 2505 | } |
| 2506 | else { |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2507 | /* we'll connect to the addr on the server */ |
| 2508 | conn->addr.to = s->addr; |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 2509 | proto = s->proto; |
| 2510 | } |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2511 | |
| 2512 | /* port */ |
| 2513 | if (check->current_step->port) |
| 2514 | set_host_port(&conn->addr.to, check->current_step->port); |
| 2515 | else if (check->port) |
| 2516 | set_host_port(&conn->addr.to, check->port); |
| 2517 | |
| 2518 | #ifdef USE_OPENSSL |
| 2519 | if (check->current_step->conn_opts & TCPCHK_OPT_SSL) { |
| 2520 | xprt = &ssl_sock; |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2521 | } |
| 2522 | else { |
| 2523 | xprt = &raw_sock; |
| 2524 | } |
| 2525 | #else /* USE_OPENSSL */ |
| 2526 | xprt = &raw_sock; |
| 2527 | #endif /* USE_OPENSSL */ |
| 2528 | conn_prepare(conn, proto, xprt); |
| 2529 | |
| 2530 | ret = SN_ERR_INTERNAL; |
| 2531 | if (proto->connect) |
Willy Tarreau | f3d3482 | 2014-12-08 12:11:28 +0100 | [diff] [blame] | 2532 | ret = proto->connect(conn, |
| 2533 | 1 /* I/O polling is always needed */, |
| 2534 | (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2); |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2535 | conn->flags |= CO_FL_WAKE_DATA; |
| 2536 | if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) { |
| 2537 | conn->send_proxy_ofs = 1; |
| 2538 | conn->flags |= CO_FL_SEND_PROXY; |
| 2539 | } |
| 2540 | |
| 2541 | /* It can return one of : |
| 2542 | * - SN_ERR_NONE if everything's OK |
| 2543 | * - SN_ERR_SRVTO if there are no more servers |
| 2544 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 2545 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 2546 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 2547 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 2548 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 2549 | * Note that we try to prevent the network stack from sending the ACK during the |
| 2550 | * connect() when a pure TCP check is used (without PROXY protocol). |
| 2551 | */ |
| 2552 | switch (ret) { |
| 2553 | case SN_ERR_NONE: |
| 2554 | /* we allow up to min(inter, timeout.connect) for a connection |
| 2555 | * to establish but only when timeout.check is set |
| 2556 | * as it may be to short for a full check otherwise |
| 2557 | */ |
| 2558 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 2559 | |
| 2560 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 2561 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 2562 | t->expire = tick_first(t->expire, t_con); |
| 2563 | } |
| 2564 | break; |
| 2565 | case SN_ERR_SRVTO: /* ETIMEDOUT */ |
| 2566 | case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */ |
| 2567 | chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s", |
| 2568 | tcpcheck_get_step_id(s), strerror(errno)); |
| 2569 | set_server_check_status(check, HCHK_STATUS_L4CON, trash.str); |
| 2570 | goto out_end_tcpcheck; |
| 2571 | case SN_ERR_PRXCOND: |
| 2572 | case SN_ERR_RESOURCE: |
| 2573 | case SN_ERR_INTERNAL: |
| 2574 | chunk_printf(&trash, "TCPCHK error establishing connection at step %d", |
| 2575 | tcpcheck_get_step_id(s)); |
| 2576 | set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str); |
| 2577 | goto out_end_tcpcheck; |
| 2578 | } |
| 2579 | |
| 2580 | /* allow next rule */ |
| 2581 | cur = (struct tcpcheck_rule *)cur->list.n; |
| 2582 | check->current_step = cur; |
| 2583 | |
| 2584 | /* don't do anything until the connection is established */ |
| 2585 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 2586 | /* update expire time, should be done by process_chk */ |
| 2587 | /* we allow up to min(inter, timeout.connect) for a connection |
| 2588 | * to establish but only when timeout.check is set |
| 2589 | * as it may be to short for a full check otherwise |
| 2590 | */ |
| 2591 | while (tick_is_expired(t->expire, now_ms)) { |
| 2592 | int t_con; |
| 2593 | |
| 2594 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 2595 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 2596 | |
| 2597 | if (s->proxy->timeout.check) |
| 2598 | t->expire = tick_first(t->expire, t_con); |
| 2599 | } |
| 2600 | return; |
| 2601 | } |
| 2602 | |
| 2603 | } /* end 'connect' */ |
| 2604 | else if (check->current_step->action == TCPCHK_ACT_SEND) { |
| 2605 | /* mark the step as started */ |
| 2606 | check->last_started_step = check->current_step; |
| 2607 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2608 | /* reset the read buffer */ |
| 2609 | if (*check->bi->data != '\0') { |
| 2610 | *check->bi->data = '\0'; |
| 2611 | check->bi->i = 0; |
| 2612 | } |
| 2613 | |
| 2614 | if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) { |
| 2615 | conn->flags |= CO_FL_ERROR; |
| 2616 | chk_report_conn_err(conn, 0, 0); |
| 2617 | goto out_end_tcpcheck; |
| 2618 | } |
| 2619 | |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2620 | if (check->current_step->string_len >= check->bo->size) { |
| 2621 | chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d", |
| 2622 | check->current_step->string_len, check->bo->size, |
| 2623 | tcpcheck_get_step_id(s)); |
| 2624 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2625 | goto out_end_tcpcheck; |
| 2626 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2627 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2628 | /* do not try to send if there is no space */ |
| 2629 | if (check->current_step->string_len >= buffer_total_space(check->bo)) |
| 2630 | continue; |
| 2631 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2632 | bo_putblk(check->bo, check->current_step->string, check->current_step->string_len); |
| 2633 | *check->bo->p = '\0'; /* to make gdb output easier to read */ |
| 2634 | |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2635 | /* go to next rule and try to send */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2636 | cur = (struct tcpcheck_rule *)cur->list.n; |
| 2637 | check->current_step = cur; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2638 | } /* end 'send' */ |
Willy Tarreau | 98aec9f | 2013-12-06 16:16:41 +0100 | [diff] [blame] | 2639 | else if (check->current_step->action == TCPCHK_ACT_EXPECT) { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 2640 | if (unlikely(check->result == CHK_RES_FAILED)) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2641 | goto out_end_tcpcheck; |
| 2642 | |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 2643 | if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) { |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2644 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) { |
| 2645 | done = 1; |
| 2646 | if ((conn->flags & CO_FL_ERROR) && !check->bi->i) { |
| 2647 | /* Report network errors only if we got no other data. Otherwise |
| 2648 | * we'll let the upper layers decide whether the response is OK |
| 2649 | * or not. It is very common that an RST sent by the server is |
| 2650 | * reported as an error just after the last data chunk. |
| 2651 | */ |
| 2652 | chk_report_conn_err(conn, errno, 0); |
| 2653 | goto out_end_tcpcheck; |
| 2654 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2655 | } |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2656 | else |
| 2657 | goto out_need_io; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2658 | } |
| 2659 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2660 | /* mark the step as started */ |
| 2661 | check->last_started_step = check->current_step; |
| 2662 | |
| 2663 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2664 | /* Intermediate or complete response received. |
| 2665 | * Terminate string in check->bi->data buffer. |
| 2666 | */ |
| 2667 | if (check->bi->i < check->bi->size) { |
| 2668 | check->bi->data[check->bi->i] = '\0'; |
| 2669 | } |
| 2670 | else { |
| 2671 | check->bi->data[check->bi->i - 1] = '\0'; |
| 2672 | done = 1; /* buffer full, don't wait for more data */ |
| 2673 | } |
| 2674 | |
| 2675 | contentptr = check->bi->data; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2676 | |
| 2677 | /* Check that response body is not empty... */ |
Willy Tarreau | ec6b012 | 2014-05-13 17:57:29 +0200 | [diff] [blame] | 2678 | if (!check->bi->i) { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2679 | if (!done) |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2680 | continue; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2681 | |
| 2682 | /* empty response */ |
| 2683 | chunk_printf(&trash, "TCPCHK got an empty response at step %d", |
| 2684 | tcpcheck_get_step_id(s)); |
| 2685 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2686 | |
| 2687 | goto out_end_tcpcheck; |
| 2688 | } |
| 2689 | |
| 2690 | if (!done && (cur->string != NULL) && (check->bi->i < cur->string_len) ) |
Willy Tarreau | a970c28 | 2013-12-06 12:47:19 +0100 | [diff] [blame] | 2691 | continue; /* try to read more */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2692 | |
Willy Tarreau | a970c28 | 2013-12-06 12:47:19 +0100 | [diff] [blame] | 2693 | tcpcheck_expect: |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2694 | if (cur->string != NULL) |
Willy Tarreau | ec6b012 | 2014-05-13 17:57:29 +0200 | [diff] [blame] | 2695 | ret = my_memmem(contentptr, check->bi->i, cur->string, cur->string_len) != NULL; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2696 | else if (cur->expect_regex != NULL) |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 2697 | ret = regex_exec(cur->expect_regex, contentptr); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2698 | |
| 2699 | if (!ret && !done) |
Willy Tarreau | a970c28 | 2013-12-06 12:47:19 +0100 | [diff] [blame] | 2700 | continue; /* try to read more */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2701 | |
| 2702 | /* matched */ |
| 2703 | if (ret) { |
| 2704 | /* matched but we did not want to => ERROR */ |
| 2705 | if (cur->inverse) { |
| 2706 | /* we were looking for a string */ |
| 2707 | if (cur->string != NULL) { |
| 2708 | chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d", |
| 2709 | cur->string, tcpcheck_get_step_id(s)); |
| 2710 | } |
| 2711 | else { |
| 2712 | /* we were looking for a regex */ |
| 2713 | chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d", |
| 2714 | tcpcheck_get_step_id(s)); |
| 2715 | } |
| 2716 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2717 | goto out_end_tcpcheck; |
| 2718 | } |
| 2719 | /* matched and was supposed to => OK, next step */ |
| 2720 | else { |
| 2721 | cur = (struct tcpcheck_rule*)cur->list.n; |
| 2722 | check->current_step = cur; |
Willy Tarreau | 98aec9f | 2013-12-06 16:16:41 +0100 | [diff] [blame] | 2723 | if (check->current_step->action == TCPCHK_ACT_EXPECT) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2724 | goto tcpcheck_expect; |
| 2725 | __conn_data_stop_recv(conn); |
| 2726 | } |
| 2727 | } |
| 2728 | else { |
| 2729 | /* not matched */ |
| 2730 | /* not matched and was not supposed to => OK, next step */ |
| 2731 | if (cur->inverse) { |
| 2732 | cur = (struct tcpcheck_rule*)cur->list.n; |
| 2733 | check->current_step = cur; |
Willy Tarreau | 98aec9f | 2013-12-06 16:16:41 +0100 | [diff] [blame] | 2734 | if (check->current_step->action == TCPCHK_ACT_EXPECT) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2735 | goto tcpcheck_expect; |
| 2736 | __conn_data_stop_recv(conn); |
| 2737 | } |
| 2738 | /* not matched but was supposed to => ERROR */ |
| 2739 | else { |
| 2740 | /* we were looking for a string */ |
| 2741 | if (cur->string != NULL) { |
| 2742 | chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d", |
| 2743 | cur->string, tcpcheck_get_step_id(s)); |
| 2744 | } |
| 2745 | else { |
| 2746 | /* we were looking for a regex */ |
| 2747 | chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d", |
| 2748 | tcpcheck_get_step_id(s)); |
| 2749 | } |
| 2750 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2751 | goto out_end_tcpcheck; |
| 2752 | } |
| 2753 | } |
| 2754 | } /* end expect */ |
| 2755 | } /* end loop over double chained step list */ |
| 2756 | |
| 2757 | set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)"); |
| 2758 | goto out_end_tcpcheck; |
| 2759 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2760 | out_need_io: |
| 2761 | if (check->bo->o) |
| 2762 | __conn_data_want_send(conn); |
| 2763 | |
| 2764 | if (check->current_step->action == TCPCHK_ACT_EXPECT) |
| 2765 | __conn_data_want_recv(conn); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2766 | return; |
| 2767 | |
| 2768 | out_end_tcpcheck: |
| 2769 | /* collect possible new errors */ |
| 2770 | if (conn->flags & CO_FL_ERROR) |
| 2771 | chk_report_conn_err(conn, 0, 0); |
| 2772 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2773 | /* cleanup before leaving */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2774 | check->current_step = NULL; |
| 2775 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 2776 | if (check->result == CHK_RES_FAILED) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2777 | conn->flags |= CO_FL_ERROR; |
| 2778 | |
| 2779 | __conn_data_stop_both(conn); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2780 | |
| 2781 | return; |
| 2782 | } |
| 2783 | |
| 2784 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2785 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2786 | * Local variables: |
| 2787 | * c-indent-level: 8 |
| 2788 | * c-basic-offset: 8 |
| 2789 | * End: |
| 2790 | */ |