blob: 30523a733e2d5825819155936ca1315c2288e65e [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Health-checks functions.
3 *
Willy Tarreau26c25062009-03-08 09:38:41 +01004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
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 Tarreaub8816082008-01-18 12:18:15 +010014#include <assert.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020015#include <ctype.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <errno.h>
17#include <fcntl.h>
Willy Tarreau9b39dc52014-07-08 00:54:10 +020018#include <signal.h>
Simon Horman0ba0e4a2015-01-30 11:23:00 +090019#include <stdarg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020021#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020023#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024#include <unistd.h>
25#include <sys/socket.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040026#include <sys/types.h>
Simon Horman98637e52014-06-20 12:30:16 +090027#include <sys/wait.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028#include <netinet/in.h>
Willy Tarreau1274bc42009-07-15 07:16:31 +020029#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <arpa/inet.h>
31
Willy Tarreauc7e42382012-08-24 19:22:53 +020032#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/compat.h>
34#include <common/config.h>
35#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020036#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
39#include <types/global.h>
Simon Horman0ba0e4a2015-01-30 11:23:00 +090040#include <types/mailers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Baptiste Assmann69e273f2013-12-11 00:52:19 +010042#ifdef USE_OPENSSL
43#include <types/ssl_sock.h>
44#include <proto/ssl_sock.h>
45#endif /* USE_OPENSSL */
46
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020048#include <proto/checks.h>
Simon Hormana2b9dad2013-02-12 10:45:54 +090049#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/fd.h>
51#include <proto/log.h>
52#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020053#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010054#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010055#include <proto/proto_tcp.h>
Baptiste Assmann69e273f2013-12-11 00:52:19 +010056#include <proto/protocol.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010057#include <proto/proxy.h>
Willy Tarreaufb56aab2012-09-28 14:40:02 +020058#include <proto/raw_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/server.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010060#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/task.h>
62
Willy Tarreaubd741542010-03-16 18:46:54 +010063static int httpchk_expect(struct server *s, int done);
Simon Hormane16c1b32015-01-30 11:22:57 +090064static int tcpcheck_get_step_id(struct check *);
Baptiste Assmann22b09d22015-05-01 08:03:04 +020065static char * tcpcheck_get_step_comment(struct check *, int);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +020066static void tcpcheck_main(struct connection *);
Willy Tarreaubd741542010-03-16 18:46:54 +010067
Simon Horman63a4a822012-03-19 07:24:41 +090068static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010069 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
70 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020071 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020072
Willy Tarreau23964182014-05-20 20:56:30 +020073 /* Below we have finished checks */
74 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010075 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010076
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010077 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020078
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010079 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
80 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
81 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020082
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010083 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
84 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
85 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020086
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010087 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
88 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020089
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020090 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020091
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010092 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
93 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
94 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +090095
96 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
97 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +020098 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020099};
100
Cyril Bontéac92a062014-12-27 22:28:38 +0100101const struct extcheck_env extcheck_envs[EXTCHK_SIZE] = {
102 [EXTCHK_PATH] = { "PATH", EXTCHK_SIZE_EVAL_INIT },
103 [EXTCHK_HAPROXY_PROXY_NAME] = { "HAPROXY_PROXY_NAME", EXTCHK_SIZE_EVAL_INIT },
104 [EXTCHK_HAPROXY_PROXY_ID] = { "HAPROXY_PROXY_ID", EXTCHK_SIZE_EVAL_INIT },
105 [EXTCHK_HAPROXY_PROXY_ADDR] = { "HAPROXY_PROXY_ADDR", EXTCHK_SIZE_EVAL_INIT },
106 [EXTCHK_HAPROXY_PROXY_PORT] = { "HAPROXY_PROXY_PORT", EXTCHK_SIZE_EVAL_INIT },
107 [EXTCHK_HAPROXY_SERVER_NAME] = { "HAPROXY_SERVER_NAME", EXTCHK_SIZE_EVAL_INIT },
108 [EXTCHK_HAPROXY_SERVER_ID] = { "HAPROXY_SERVER_ID", EXTCHK_SIZE_EVAL_INIT },
109 [EXTCHK_HAPROXY_SERVER_ADDR] = { "HAPROXY_SERVER_ADDR", EXTCHK_SIZE_EVAL_INIT },
110 [EXTCHK_HAPROXY_SERVER_PORT] = { "HAPROXY_SERVER_PORT", EXTCHK_SIZE_EVAL_INIT },
111 [EXTCHK_HAPROXY_SERVER_MAXCONN] = { "HAPROXY_SERVER_MAXCONN", EXTCHK_SIZE_EVAL_INIT },
112 [EXTCHK_HAPROXY_SERVER_CURCONN] = { "HAPROXY_SERVER_CURCONN", EXTCHK_SIZE_ULONG },
113};
114
Simon Horman63a4a822012-03-19 07:24:41 +0900115static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100116 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
117
118 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
119 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
120
121 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
122 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
123 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
124 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
125
126 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
127 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
128 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
129};
130
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200131/*
132 * Convert check_status code to description
133 */
134const char *get_check_status_description(short check_status) {
135
136 const char *desc;
137
138 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200139 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200140 else
141 desc = NULL;
142
143 if (desc && *desc)
144 return desc;
145 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200146 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200147}
148
149/*
150 * Convert check_status code to short info
151 */
152const char *get_check_status_info(short check_status) {
153
154 const char *info;
155
156 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200157 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200158 else
159 info = NULL;
160
161 if (info && *info)
162 return info;
163 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200164 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200165}
166
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100167const char *get_analyze_status(short analyze_status) {
168
169 const char *desc;
170
171 if (analyze_status < HANA_STATUS_SIZE)
172 desc = analyze_statuses[analyze_status].desc;
173 else
174 desc = NULL;
175
176 if (desc && *desc)
177 return desc;
178 else
179 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
180}
181
Willy Tarreaua150cf12014-05-20 21:57:23 +0200182/* Builds a string containing some information about the health check's result.
183 * The output string is allocated from the trash chunks. If the check is NULL,
184 * NULL is returned. This is designed to be used when emitting logs about health
185 * checks.
Willy Tarreauddd329c2014-05-16 16:46:12 +0200186 */
Willy Tarreaua150cf12014-05-20 21:57:23 +0200187static const char *check_reason_string(struct check *check)
Willy Tarreauddd329c2014-05-16 16:46:12 +0200188{
Willy Tarreaua150cf12014-05-20 21:57:23 +0200189 struct chunk *msg;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200190
Willy Tarreaua150cf12014-05-20 21:57:23 +0200191 if (!check)
192 return NULL;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200193
Willy Tarreaua150cf12014-05-20 21:57:23 +0200194 msg = get_trash_chunk();
195 chunk_printf(msg, "reason: %s", get_check_status_description(check->status));
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200196
Willy Tarreaua150cf12014-05-20 21:57:23 +0200197 if (check->status >= HCHK_STATUS_L57DATA)
198 chunk_appendf(msg, ", code: %d", check->code);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200199
Willy Tarreaua150cf12014-05-20 21:57:23 +0200200 if (*check->desc) {
201 struct chunk src;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200202
Willy Tarreaua150cf12014-05-20 21:57:23 +0200203 chunk_appendf(msg, ", info: \"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200204
Willy Tarreaua150cf12014-05-20 21:57:23 +0200205 chunk_initlen(&src, check->desc, 0, strlen(check->desc));
206 chunk_asciiencode(msg, &src, '"');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200207
Willy Tarreaua150cf12014-05-20 21:57:23 +0200208 chunk_appendf(msg, "\"");
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200209 }
210
Willy Tarreaua150cf12014-05-20 21:57:23 +0200211 if (check->duration >= 0)
212 chunk_appendf(msg, ", check duration: %ldms", check->duration);
213
214 return msg->str;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200215}
216
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200217/*
Simon Horman4a741432013-02-23 15:35:38 +0900218 * Set check->status, update check->duration and fill check->result with
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200219 * an adequate CHK_RES_* value. The new check->health is computed based
220 * on the result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200221 *
222 * Show information in logs about failed health check if server is UP
223 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200224 */
Simon Horman4a741432013-02-23 15:35:38 +0900225static void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100226{
Simon Horman4a741432013-02-23 15:35:38 +0900227 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200228 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200229 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900230
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200231 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100232 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900233 check->desc[0] = '\0';
234 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200235 return;
236 }
237
Simon Horman4a741432013-02-23 15:35:38 +0900238 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200239 return;
240
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200241 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900242 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
243 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200244 } else
Simon Horman4a741432013-02-23 15:35:38 +0900245 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200246
Simon Horman4a741432013-02-23 15:35:38 +0900247 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200248 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900249 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200250
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100251 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900252 check->duration = -1;
253 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200254 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900255 check->duration = tv_ms_elapsed(&check->start, &now);
256 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200257 }
258
Willy Tarreau23964182014-05-20 20:56:30 +0200259 /* no change is expected if no state change occurred */
260 if (check->result == CHK_RES_NEUTRAL)
261 return;
262
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200263 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200264
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200265 switch (check->result) {
266 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200267 /* Failure to connect to the agent as a secondary check should not
268 * cause the server to be marked down.
269 */
270 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900271 (check->status >= HCHK_STATUS_L57DATA)) &&
Willy Tarreau12634e12014-05-23 11:32:36 +0200272 (check->health >= check->rise)) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200273 s->counters.failed_checks++;
274 report = 1;
275 check->health--;
276 if (check->health < check->rise)
277 check->health = 0;
278 }
279 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200280
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200281 case CHK_RES_PASSED:
282 case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */
283 if ((check->health < check->rise + check->fall - 1) &&
284 (check->result == CHK_RES_PASSED || check->health > 0)) {
285 report = 1;
286 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200287
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200288 if (check->health >= check->rise)
289 check->health = check->rise + check->fall - 1; /* OK now */
290 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200291
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200292 /* clear consecutive_errors if observing is enabled */
293 if (s->onerror)
294 s->consecutive_errors = 0;
295 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100296
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200297 default:
298 break;
299 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200300
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200301 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
302 (status != prev_status || report)) {
303 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200304 "%s check for %sserver %s/%s %s%s",
305 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200306 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100307 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100308 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200309 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200310
Willy Tarreaua150cf12014-05-20 21:57:23 +0200311 srv_append_status(&trash, s, check_reason_string(check), -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200312
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100313 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200314 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
315 (check->health >= check->rise) ? check->fall : check->rise,
316 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200317
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100318 Warning("%s.\n", trash.str);
319 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman7ea9be02015-04-30 13:10:33 +0900320 send_email_alert(s, LOG_INFO, "%s", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200321 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200322}
323
Willy Tarreau4eec5472014-05-20 22:32:27 +0200324/* Marks the check <check>'s server down if the current check is already failed
325 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200326 */
Willy Tarreau4eec5472014-05-20 22:32:27 +0200327static void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200328{
Simon Horman4a741432013-02-23 15:35:38 +0900329 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900330
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200331 /* The agent secondary check should only cause a server to be marked
332 * as down if check->status is HCHK_STATUS_L7STS, which indicates
333 * that the agent returned "fail", "stopped" or "down".
334 * The implication here is that failure to connect to the agent
335 * as a secondary check should not cause the server to be marked
336 * down. */
337 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
338 return;
339
Willy Tarreau4eec5472014-05-20 22:32:27 +0200340 if (check->health > 0)
341 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100342
Willy Tarreau4eec5472014-05-20 22:32:27 +0200343 /* We only report a reason for the check if we did not do so previously */
344 srv_set_stopped(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200345}
346
Willy Tarreauaf549582014-05-16 17:37:50 +0200347/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200348 * it isn't in maintenance, it is not tracking a down server and other checks
349 * comply. The rule is simple : by default, a server is up, unless any of the
350 * following conditions is true :
351 * - health check failed (check->health < rise)
352 * - agent check failed (agent->health < rise)
353 * - the server tracks a down server (track && track->state == STOPPED)
354 * Note that if the server has a slowstart, it will switch to STARTING instead
355 * of RUNNING. Also, only the health checks support the nolb mode, so the
356 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200357 */
Willy Tarreau3e048382014-05-21 10:30:54 +0200358static void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200359{
Simon Horman4a741432013-02-23 15:35:38 +0900360 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100361
Willy Tarreauaf549582014-05-16 17:37:50 +0200362 if (s->admin & SRV_ADMF_MAINT)
363 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100364
Willy Tarreau3e048382014-05-21 10:30:54 +0200365 if (s->track && s->track->state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200366 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100367
Willy Tarreau3e048382014-05-21 10:30:54 +0200368 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
369 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100370
Willy Tarreau3e048382014-05-21 10:30:54 +0200371 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
372 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200373
Willy Tarreau3e048382014-05-21 10:30:54 +0200374 if ((check->state & CHK_ST_AGENT) && s->state == SRV_ST_STOPPING)
375 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100376
Willy Tarreau3e048382014-05-21 10:30:54 +0200377 srv_set_running(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100378}
379
Willy Tarreaudb58b792014-05-21 13:57:23 +0200380/* Marks the check <check> as valid and tries to set its server into stopping mode
381 * if it was running or starting, and provided it isn't in maintenance and other
382 * checks comply. The conditions for the server to be marked in stopping mode are
383 * the same as for it to be turned up. Also, only the health checks support the
384 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200385 */
Willy Tarreaudb58b792014-05-21 13:57:23 +0200386static void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200387{
Simon Horman4a741432013-02-23 15:35:38 +0900388 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100389
Willy Tarreauaf549582014-05-16 17:37:50 +0200390 if (s->admin & SRV_ADMF_MAINT)
391 return;
392
Willy Tarreaudb58b792014-05-21 13:57:23 +0200393 if (check->state & CHK_ST_AGENT)
394 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100395
Willy Tarreaudb58b792014-05-21 13:57:23 +0200396 if (s->track && s->track->state == SRV_ST_STOPPED)
397 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100398
Willy Tarreaudb58b792014-05-21 13:57:23 +0200399 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
400 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100401
Willy Tarreaudb58b792014-05-21 13:57:23 +0200402 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
403 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100404
Willy Tarreaudb58b792014-05-21 13:57:23 +0200405 srv_set_stopping(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100406}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200407
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100408/* note: use health_adjust() only, which first checks that the observe mode is
409 * enabled.
410 */
411void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100412{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100413 int failed;
414 int expire;
415
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100416 if (s->observe >= HANA_OBS_SIZE)
417 return;
418
Willy Tarreaubb956662013-01-24 00:37:39 +0100419 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100420 return;
421
422 switch (analyze_statuses[status].lr[s->observe - 1]) {
423 case 1:
424 failed = 1;
425 break;
426
427 case 2:
428 failed = 0;
429 break;
430
431 default:
432 return;
433 }
434
435 if (!failed) {
436 /* good: clear consecutive_errors */
437 s->consecutive_errors = 0;
438 return;
439 }
440
441 s->consecutive_errors++;
442
443 if (s->consecutive_errors < s->consecutive_errors_limit)
444 return;
445
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100446 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
447 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100448
449 switch (s->onerror) {
450 case HANA_ONERR_FASTINTER:
451 /* force fastinter - nothing to do here as all modes force it */
452 break;
453
454 case HANA_ONERR_SUDDTH:
455 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900456 if (s->check.health > s->check.rise)
457 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100458
459 /* no break - fall through */
460
461 case HANA_ONERR_FAILCHK:
462 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900463 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200464 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100465 break;
466
467 case HANA_ONERR_MARKDWN:
468 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900469 s->check.health = s->check.rise;
Simon Horman4a741432013-02-23 15:35:38 +0900470 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200471 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100472 break;
473
474 default:
475 /* write a warning? */
476 break;
477 }
478
479 s->consecutive_errors = 0;
480 s->counters.failed_hana++;
481
Simon Horman66183002013-02-23 10:16:43 +0900482 if (s->check.fastinter) {
483 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300484 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200485 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300486 /* requeue check task with new expire */
487 task_queue(s->check.task);
488 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100489 }
490}
491
Willy Tarreaua1dab552014-04-14 15:04:54 +0200492static int httpchk_build_status_header(struct server *s, char *buffer, int size)
Willy Tarreauef781042010-01-27 11:53:01 +0100493{
494 int sv_state;
495 int ratio;
496 int hlen = 0;
Joseph Lynch514061c2015-01-15 17:52:59 -0800497 char addr[46];
498 char port[6];
Willy Tarreauef781042010-01-27 11:53:01 +0100499 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
500 "UP %d/%d", "UP",
501 "NOLB %d/%d", "NOLB",
502 "no check" };
503
504 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
505 hlen += 24;
506
Willy Tarreauff5ae352013-12-11 20:36:34 +0100507 if (!(s->check.state & CHK_ST_ENABLED))
508 sv_state = 6;
Willy Tarreau892337c2014-05-13 23:41:20 +0200509 else if (s->state != SRV_ST_STOPPED) {
Simon Horman58c32972013-11-25 10:46:38 +0900510 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100511 sv_state = 3; /* UP */
512 else
513 sv_state = 2; /* going down */
514
Willy Tarreau892337c2014-05-13 23:41:20 +0200515 if (s->state == SRV_ST_STOPPING)
Willy Tarreauef781042010-01-27 11:53:01 +0100516 sv_state += 2;
517 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900518 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100519 sv_state = 1; /* going up */
520 else
521 sv_state = 0; /* DOWN */
522 }
523
Willy Tarreaua1dab552014-04-14 15:04:54 +0200524 hlen += snprintf(buffer + hlen, size - hlen,
Willy Tarreauef781042010-01-27 11:53:01 +0100525 srv_hlt_st[sv_state],
Willy Tarreau892337c2014-05-13 23:41:20 +0200526 (s->state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
527 (s->state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100528
Joseph Lynch514061c2015-01-15 17:52:59 -0800529 addr_to_str(&s->addr, addr, sizeof(addr));
530 port_to_str(&s->addr, port, sizeof(port));
531
532 hlen += snprintf(buffer + hlen, size - hlen, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
533 addr, port, s->proxy->id, s->id,
Willy Tarreauef781042010-01-27 11:53:01 +0100534 global.node,
535 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
536 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
537 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
538 s->nbpend);
539
Willy Tarreau892337c2014-05-13 23:41:20 +0200540 if ((s->state == SRV_ST_STARTING) &&
Willy Tarreauef781042010-01-27 11:53:01 +0100541 now.tv_sec < s->last_change + s->slowstart &&
542 now.tv_sec >= s->last_change) {
543 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
Willy Tarreaua1dab552014-04-14 15:04:54 +0200544 hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio);
Willy Tarreauef781042010-01-27 11:53:01 +0100545 }
546
547 buffer[hlen++] = '\r';
548 buffer[hlen++] = '\n';
549
550 return hlen;
551}
552
Willy Tarreau20a18342013-12-05 00:31:46 +0100553/* Check the connection. If an error has already been reported or the socket is
554 * closed, keep errno intact as it is supposed to contain the valid error code.
555 * If no error is reported, check the socket's error queue using getsockopt().
556 * Warning, this must be done only once when returning from poll, and never
557 * after an I/O error was attempted, otherwise the error queue might contain
558 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
559 * socket. Returns non-zero if an error was reported, zero if everything is
560 * clean (including a properly closed socket).
561 */
562static int retrieve_errno_from_socket(struct connection *conn)
563{
564 int skerr;
565 socklen_t lskerr = sizeof(skerr);
566
567 if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl))
568 return 1;
569
Willy Tarreau3c728722014-01-23 13:50:42 +0100570 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100571 return 0;
572
573 if (getsockopt(conn->t.sock.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
574 errno = skerr;
575
576 if (errno == EAGAIN)
577 errno = 0;
578
579 if (!errno) {
580 /* we could not retrieve an error, that does not mean there is
581 * none. Just don't change anything and only report the prior
582 * error if any.
583 */
584 if (conn->flags & CO_FL_ERROR)
585 return 1;
586 else
587 return 0;
588 }
589
590 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
591 return 1;
592}
593
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100594/* Try to collect as much information as possible on the connection status,
595 * and adjust the server status accordingly. It may make use of <errno_bck>
596 * if non-null when the caller is absolutely certain of its validity (eg:
597 * checked just after a syscall). If the caller doesn't have a valid errno,
598 * it can pass zero, and retrieve_errno_from_socket() will be called to try
599 * to extract errno from the socket. If no error is reported, it will consider
600 * the <expired> flag. This is intended to be used when a connection error was
601 * reported in conn->flags or when a timeout was reported in <expired>. The
602 * function takes care of not updating a server status which was already set.
603 * All situations where at least one of <expired> or CO_FL_ERROR are set
604 * produce a status.
605 */
606static void chk_report_conn_err(struct connection *conn, int errno_bck, int expired)
607{
608 struct check *check = conn->owner;
609 const char *err_msg;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200610 struct chunk *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200611 int step;
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200612 char *comment;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100613
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100614 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100615 return;
616
617 errno = errno_bck;
618 if (!errno || errno == EAGAIN)
619 retrieve_errno_from_socket(conn);
620
621 if (!(conn->flags & CO_FL_ERROR) && !expired)
622 return;
623
624 /* we'll try to build a meaningful error message depending on the
625 * context of the error possibly present in conn->err_code, and the
626 * socket error possibly collected above. This is useful to know the
627 * exact step of the L6 layer (eg: SSL handshake).
628 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200629 chk = get_trash_chunk();
630
631 if (check->type == PR_O2_TCPCHK_CHK) {
Simon Hormane16c1b32015-01-30 11:22:57 +0900632 step = tcpcheck_get_step_id(check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200633 if (!step)
634 chunk_printf(chk, " at initial connection step of tcp-check");
635 else {
636 chunk_printf(chk, " at step %d of tcp-check", step);
637 /* we were looking for a string */
638 if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_CONNECT) {
639 if (check->last_started_step->port)
640 chunk_appendf(chk, " (connect port %d)" ,check->last_started_step->port);
641 else
642 chunk_appendf(chk, " (connect)");
643 }
644 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_EXPECT) {
645 if (check->last_started_step->string)
Baptiste Assmann96a5c9b2015-05-01 08:09:29 +0200646 chunk_appendf(chk, " (expect string '%s')", check->last_started_step->string);
Willy Tarreau213c6782014-10-02 14:51:02 +0200647 else if (check->last_started_step->expect_regex)
648 chunk_appendf(chk, " (expect regex)");
649 }
650 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_SEND) {
651 chunk_appendf(chk, " (send)");
652 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200653
654 comment = tcpcheck_get_step_comment(check, step);
655 if (comment)
656 chunk_appendf(chk, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200657 }
658 }
659
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100660 if (conn->err_code) {
661 if (errno && errno != EAGAIN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200662 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100663 else
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200664 chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100665 err_msg = trash.str;
666 }
667 else {
668 if (errno && errno != EAGAIN) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200669 chunk_printf(&trash, "%s%s", strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100670 err_msg = trash.str;
671 }
672 else {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200673 err_msg = chk->str;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100674 }
675 }
676
677 if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
678 /* L4 not established (yet) */
679 if (conn->flags & CO_FL_ERROR)
680 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
681 else if (expired)
682 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
683 }
684 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
685 /* L6 not established (yet) */
686 if (conn->flags & CO_FL_ERROR)
687 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
688 else if (expired)
689 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
690 }
691 else if (conn->flags & CO_FL_ERROR) {
692 /* I/O error after connection was established and before we could diagnose */
693 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
694 }
695 else if (expired) {
696 /* connection established but expired check */
697 if (check->type == PR_O2_SSL3_CHK)
698 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
699 else /* HTTP, SMTP, ... */
700 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
701 }
702
703 return;
704}
705
Willy Tarreaubaaee002006-06-26 02:48:02 +0200706/*
707 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200708 * the connection acknowledgement. If the proxy requires L7 health-checks,
709 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900710 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200711 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200712static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200713{
Simon Horman4a741432013-02-23 15:35:38 +0900714 struct check *check = conn->owner;
715 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900716 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200717
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100718 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100719 goto out_wakeup;
720
Willy Tarreau310987a2014-01-22 19:46:33 +0100721 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100722 return;
723
Willy Tarreau20a18342013-12-05 00:31:46 +0100724 if (retrieve_errno_from_socket(conn)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100725 chk_report_conn_err(conn, errno, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100726 __conn_data_stop_both(conn);
727 goto out_wakeup;
728 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100729
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100730 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100731 /* if the output is closed, we can't do anything */
732 conn->flags |= CO_FL_ERROR;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100733 chk_report_conn_err(conn, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100734 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200735 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200736
Willy Tarreau06559ac2013-12-05 01:53:08 +0100737 /* here, we know that the connection is established. That's enough for
738 * a pure TCP check.
739 */
740 if (!check->type)
741 goto out_wakeup;
742
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200743 if (check->type == PR_O2_TCPCHK_CHK) {
744 tcpcheck_main(conn);
745 return;
746 }
747
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100748 if (check->bo->o) {
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100749 conn->xprt->snd_buf(conn, check->bo, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100750 if (conn->flags & CO_FL_ERROR) {
751 chk_report_conn_err(conn, errno, 0);
752 __conn_data_stop_both(conn);
753 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200754 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100755 if (check->bo->o)
756 return;
757 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200758
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100759 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
760 if (s->proxy->timeout.check) {
761 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
762 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200763 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100764 goto out_nowake;
765
Willy Tarreau83749182007-04-15 20:56:27 +0200766 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200767 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200768 out_nowake:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200769 __conn_data_stop_send(conn); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200770}
771
Willy Tarreaubaaee002006-06-26 02:48:02 +0200772/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200773 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200774 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900775 * set_server_check_status() to update check->status, check->duration
776 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200777
778 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
779 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
780 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
781 * response to an SSL HELLO (the principle is that this is enough to
782 * distinguish between an SSL server and a pure TCP relay). All other cases will
783 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
784 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200785 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200786static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200787{
Simon Horman4a741432013-02-23 15:35:38 +0900788 struct check *check = conn->owner;
789 struct server *s = check->server;
790 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200791 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100792 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200793 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200794
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100795 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +0200796 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200797
Willy Tarreau310987a2014-01-22 19:46:33 +0100798 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200799 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200800
801 if (check->type == PR_O2_TCPCHK_CHK) {
802 tcpcheck_main(conn);
803 return;
804 }
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200805
Willy Tarreau83749182007-04-15 20:56:27 +0200806 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
807 * but the connection was closed on the remote end. Fortunately, recv still
808 * works correctly and we don't need to do the getsockopt() on linux.
809 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000810
811 /* Set buffer to point to the end of the data already read, and check
812 * that there is free space remaining. If the buffer is full, proceed
813 * with running the checks without attempting another socket read.
814 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000815
Willy Tarreau03938182010-03-17 21:52:07 +0100816 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +0000817
Simon Horman4a741432013-02-23 15:35:38 +0900818 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreauf1503172012-09-28 19:39:36 +0200819 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +0100820 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +0900821 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +0200822 /* Report network errors only if we got no other data. Otherwise
823 * we'll let the upper layers decide whether the response is OK
824 * or not. It is very common that an RST sent by the server is
825 * reported as an error just after the last data chunk.
826 */
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100827 chk_report_conn_err(conn, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +0100828 goto out_wakeup;
829 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200830 }
831
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100832
Willy Tarreau03938182010-03-17 21:52:07 +0100833 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +0900834 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +0100835 */
Simon Horman4a741432013-02-23 15:35:38 +0900836 if (check->bi->i < check->bi->size)
837 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100838 else {
Simon Horman4a741432013-02-23 15:35:38 +0900839 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100840 done = 1; /* buffer full, don't wait for more data */
841 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200842
Nick Chalk57b1bf72010-03-16 15:50:46 +0000843 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +0900844 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +0200845 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900846 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100847 goto wait_more_data;
848
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100849 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +0900850 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
851 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
852 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
853 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
854 !isdigit((unsigned char) *(check->bi->data + 11))) {
855 cut_crlf(check->bi->data);
856 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200857
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100858 goto out_wakeup;
859 }
860
Simon Horman4a741432013-02-23 15:35:38 +0900861 check->code = str2uic(check->bi->data + 9);
862 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +0000863
Willy Tarreaubd741542010-03-16 18:46:54 +0100864 if ((s->proxy->options & PR_O_DISABLE404) &&
Willy Tarreau892337c2014-05-13 23:41:20 +0200865 (s->state != SRV_ST_STOPPED) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000866 /* 404 may be accepted as "stopping" only if the server was up */
867 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900868 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000869 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100870 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
871 /* Run content verification check... We know we have at least 13 chars */
872 if (!httpchk_expect(s, done))
873 goto wait_more_data;
874 }
875 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +0900876 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +0100877 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900878 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +0100879 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000880 else {
881 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900882 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000883 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200884 break;
885
886 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900887 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +0100888 goto wait_more_data;
889
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100890 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +0900891 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
892 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200893 else
Simon Horman4a741432013-02-23 15:35:38 +0900894 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200895 break;
896
897 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900898 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100899 goto wait_more_data;
900
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200901 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +0900902 if ((check->bi->i < strlen("000\r")) ||
903 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
904 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
905 !isdigit((unsigned char) *(check->bi->data + 2))) {
906 cut_crlf(check->bi->data);
907 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200908
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200909 goto out_wakeup;
910 }
911
Simon Horman4a741432013-02-23 15:35:38 +0900912 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200913
Simon Horman4a741432013-02-23 15:35:38 +0900914 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200915 cut_crlf(desc);
916
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100917 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +0900918 if (*check->bi->data == '2')
919 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200920 else
Simon Horman4a741432013-02-23 15:35:38 +0900921 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200922 break;
923
Simon Hormana2b9dad2013-02-12 10:45:54 +0900924 case PR_O2_LB_AGENT_CHK: {
Willy Tarreau81f5d942013-12-09 20:51:51 +0100925 int status = HCHK_STATUS_CHECKED;
926 const char *hs = NULL; /* health status */
927 const char *as = NULL; /* admin status */
928 const char *ps = NULL; /* performance status */
929 const char *err = NULL; /* first error to report */
930 const char *wrn = NULL; /* first warning to report */
931 char *cmd, *p;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900932
Willy Tarreau81f5d942013-12-09 20:51:51 +0100933 /* We're getting an agent check response. The agent could
934 * have been disabled in the mean time with a long check
935 * still pending. It is important that we ignore the whole
936 * response.
937 */
938 if (!(check->server->agent.state & CHK_ST_ENABLED))
939 break;
940
941 /* The agent supports strings made of a single line ended by the
942 * first CR ('\r') or LF ('\n'). This line is composed of words
943 * delimited by spaces (' '), tabs ('\t'), or commas (','). The
944 * line may optionally contained a description of a state change
945 * after a sharp ('#'), which is only considered if a health state
946 * is announced.
947 *
948 * Words may be composed of :
949 * - a numeric weight suffixed by the percent character ('%').
950 * - a health status among "up", "down", "stopped", and "fail".
951 * - an admin status among "ready", "drain", "maint".
952 *
953 * These words may appear in any order. If multiple words of the
954 * same category appear, the last one wins.
955 */
956
Willy Tarreau9809b782013-12-11 21:40:11 +0100957 p = check->bi->data;
958 while (*p && *p != '\n' && *p != '\r')
959 p++;
960
961 if (!*p) {
962 if (!done)
963 goto wait_more_data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900964
Willy Tarreau9809b782013-12-11 21:40:11 +0100965 /* at least inform the admin that the agent is mis-behaving */
966 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
967 break;
968 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100969
Willy Tarreau9809b782013-12-11 21:40:11 +0100970 *p = 0;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100971 cmd = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900972
Willy Tarreau81f5d942013-12-09 20:51:51 +0100973 while (*cmd) {
974 /* look for next word */
975 if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') {
976 cmd++;
977 continue;
978 }
Simon Horman671b6f02013-11-25 10:46:39 +0900979
Willy Tarreau81f5d942013-12-09 20:51:51 +0100980 if (*cmd == '#') {
981 /* this is the beginning of a health status description,
982 * skip the sharp and blanks.
983 */
984 cmd++;
985 while (*cmd == '\t' || *cmd == ' ')
986 cmd++;
Simon Horman671b6f02013-11-25 10:46:39 +0900987 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900988 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100989
990 /* find the end of the word so that we have a null-terminated
991 * word between <cmd> and <p>.
992 */
993 p = cmd + 1;
994 while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',')
995 p++;
996 if (*p)
997 *p++ = 0;
998
999 /* first, health statuses */
1000 if (strcasecmp(cmd, "up") == 0) {
1001 check->health = check->rise + check->fall - 1;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001002 status = HCHK_STATUS_L7OKD;
Willy Tarreau81f5d942013-12-09 20:51:51 +01001003 hs = cmd;
1004 }
1005 else if (strcasecmp(cmd, "down") == 0) {
1006 check->health = 0;
1007 status = HCHK_STATUS_L7STS;
1008 hs = cmd;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001009 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001010 else if (strcasecmp(cmd, "stopped") == 0) {
1011 check->health = 0;
1012 status = HCHK_STATUS_L7STS;
1013 hs = cmd;
1014 }
1015 else if (strcasecmp(cmd, "fail") == 0) {
1016 check->health = 0;
1017 status = HCHK_STATUS_L7STS;
1018 hs = cmd;
1019 }
1020 /* admin statuses */
1021 else if (strcasecmp(cmd, "ready") == 0) {
1022 as = cmd;
1023 }
1024 else if (strcasecmp(cmd, "drain") == 0) {
1025 as = cmd;
1026 }
1027 else if (strcasecmp(cmd, "maint") == 0) {
1028 as = cmd;
1029 }
1030 /* else try to parse a weight here and keep the last one */
1031 else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) {
1032 ps = cmd;
1033 }
1034 else {
1035 /* keep a copy of the first error */
1036 if (!err)
1037 err = cmd;
1038 }
1039 /* skip to next word */
1040 cmd = p;
1041 }
1042 /* here, cmd points either to \0 or to the beginning of a
1043 * description. Skip possible leading spaces.
1044 */
1045 while (*cmd == ' ' || *cmd == '\n')
1046 cmd++;
1047
1048 /* First, update the admin status so that we avoid sending other
1049 * possibly useless warnings and can also update the health if
1050 * present after going back up.
1051 */
1052 if (as) {
1053 if (strcasecmp(as, "drain") == 0)
1054 srv_adm_set_drain(check->server);
1055 else if (strcasecmp(as, "maint") == 0)
1056 srv_adm_set_maint(check->server);
1057 else
1058 srv_adm_set_ready(check->server);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001059 }
1060
Willy Tarreau81f5d942013-12-09 20:51:51 +01001061 /* now change weights */
1062 if (ps) {
1063 const char *msg;
1064
1065 msg = server_parse_weight_change_request(s, ps);
1066 if (!wrn || !*wrn)
1067 wrn = msg;
1068 }
1069
1070 /* and finally health status */
1071 if (hs) {
1072 /* We'll report some of the warnings and errors we have
1073 * here. Down reports are critical, we leave them untouched.
1074 * Lack of report, or report of 'UP' leaves the room for
1075 * ERR first, then WARN.
Simon Hormana2b9dad2013-02-12 10:45:54 +09001076 */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001077 const char *msg = cmd;
1078 struct chunk *t;
1079
1080 if (!*msg || status == HCHK_STATUS_L7OKD) {
1081 if (err && *err)
1082 msg = err;
1083 else if (wrn && *wrn)
1084 msg = wrn;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001085 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001086
1087 t = get_trash_chunk();
1088 chunk_printf(t, "via agent : %s%s%s%s",
1089 hs, *msg ? " (" : "",
1090 msg, *msg ? ")" : "");
1091
1092 set_server_check_status(check, status, t->str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001093 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001094 else if (err && *err) {
1095 /* No status change but we'd like to report something odd.
1096 * Just report the current state and copy the message.
1097 */
1098 chunk_printf(&trash, "agent reports an error : %s", err);
1099 set_server_check_status(check, status/*check->status*/, trash.str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001100
Willy Tarreau81f5d942013-12-09 20:51:51 +01001101 }
1102 else if (wrn && *wrn) {
1103 /* No status change but we'd like to report something odd.
1104 * Just report the current state and copy the message.
1105 */
1106 chunk_printf(&trash, "agent warns : %s", wrn);
1107 set_server_check_status(check, status/*check->status*/, trash.str);
1108 }
1109 else
1110 set_server_check_status(check, status, NULL);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001111 break;
1112 }
1113
Willy Tarreau1620ec32011-08-06 17:05:02 +02001114 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001115 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001116 goto wait_more_data;
1117
Simon Horman4a741432013-02-23 15:35:38 +09001118 if (check->bi->data[0] == 'R') {
1119 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001120 }
1121 else {
Simon Horman4a741432013-02-23 15:35:38 +09001122 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1123 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001124 else
1125 desc = "PostgreSQL unknown error";
1126
Simon Horman4a741432013-02-23 15:35:38 +09001127 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001128 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001129 break;
1130
1131 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001132 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001133 goto wait_more_data;
1134
Simon Horman4a741432013-02-23 15:35:38 +09001135 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1136 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001137 }
1138 else {
Simon Horman4a741432013-02-23 15:35:38 +09001139 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001140 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001141 break;
1142
1143 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001144 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001145 goto wait_more_data;
1146
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001147 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001148 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001149 /* We set the MySQL Version in description for information purpose
1150 * FIXME : it can be cool to use MySQL Version for other purpose,
1151 * like mark as down old MySQL server.
1152 */
Simon Horman4a741432013-02-23 15:35:38 +09001153 if (check->bi->i > 51) {
1154 desc = ltrim(check->bi->data + 5, ' ');
1155 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001156 }
1157 else {
1158 if (!done)
1159 goto wait_more_data;
1160 /* it seems we have a OK packet but without a valid length,
1161 * it must be a protocol error
1162 */
Simon Horman4a741432013-02-23 15:35:38 +09001163 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001164 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001165 }
1166 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001167 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001168 desc = ltrim(check->bi->data + 7, ' ');
1169 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001170 }
1171 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001172 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1173 (((unsigned int) *(check->bi->data + 1)) << 8) +
1174 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001175
Simon Horman4a741432013-02-23 15:35:38 +09001176 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001177 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001178 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001179 /* We have only one MySQL packet and it is a Handshake Initialization packet
1180 * but we need to have a second packet to know if it is alright
1181 */
Simon Horman4a741432013-02-23 15:35:38 +09001182 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001183 goto wait_more_data;
1184 }
1185 else {
1186 /* We have only one packet and it is an Error packet,
1187 * an error message is attached, so we can display it
1188 */
Simon Horman4a741432013-02-23 15:35:38 +09001189 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001190 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001191 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001192 }
Simon Horman4a741432013-02-23 15:35:38 +09001193 } else if (check->bi->i > first_packet_len + 4) {
1194 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1195 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1196 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001197
Simon Horman4a741432013-02-23 15:35:38 +09001198 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001199 /* We have 2 packets and that's good */
1200 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001201 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001202 /* No error packet */
1203 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001204 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001205 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001206 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001207 }
1208 else {
1209 /* An error message is attached in the Error packet
1210 * so we can display it ! :)
1211 */
Simon Horman4a741432013-02-23 15:35:38 +09001212 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001213 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001214 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001215 }
1216 }
1217 }
1218 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001219 if (!done)
1220 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001221 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001222 * it must be a protocol error
1223 */
Simon Horman4a741432013-02-23 15:35:38 +09001224 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001225 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001226 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001227 }
1228 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001229 break;
1230
1231 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001232 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001233 goto wait_more_data;
1234
1235 /* Check if the server speaks LDAP (ASN.1/BER)
1236 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1237 * http://tools.ietf.org/html/rfc4511
1238 */
1239
1240 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1241 * LDAPMessage: 0x30: SEQUENCE
1242 */
Simon Horman4a741432013-02-23 15:35:38 +09001243 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1244 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001245 }
1246 else {
1247 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001248 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001249
1250 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1251 * messageID: 0x02 0x01 0x01: INTEGER 1
1252 * protocolOp: 0x61: bindResponse
1253 */
1254 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001255 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1256 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001257
1258 goto out_wakeup;
1259 }
1260
1261 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001262 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001263
1264 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1265 * ldapResult: 0x0a 0x01: ENUMERATION
1266 */
1267 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001268 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1269 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001270
1271 goto out_wakeup;
1272 }
1273
1274 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1275 * resultCode
1276 */
Simon Horman4a741432013-02-23 15:35:38 +09001277 check->code = *(check->bi->data + msglen + 9);
1278 if (check->code) {
1279 set_server_check_status(check, HCHK_STATUS_L7STS, "See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001280 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001281 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001282 }
1283 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001284 break;
1285
1286 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001287 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001288 break;
1289 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001290
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001291 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001292 /* collect possible new errors */
1293 if (conn->flags & CO_FL_ERROR)
1294 chk_report_conn_err(conn, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001295
Nick Chalk57b1bf72010-03-16 15:50:46 +00001296 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001297 *check->bi->data = '\0';
1298 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001299
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001300 /* Close the connection... We absolutely want to perform a hard close
1301 * and reset the connection if some data are pending, otherwise we end
1302 * up with many TIME_WAITs and eat all the source port range quickly.
1303 * To avoid sending RSTs all the time, we first try to drain pending
1304 * data.
1305 */
Willy Tarreaub4017d02015-03-12 23:11:26 +01001306 __conn_data_stop_both(conn);
1307 conn_data_shutw_hard(conn);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001308
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001309 /* OK, let's not stay here forever */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001310 if (check->result == CHK_RES_FAILED)
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001311 conn->flags |= CO_FL_ERROR;
1312
Willy Tarreaufdccded2008-08-29 18:19:04 +02001313 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001314 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001315
1316 wait_more_data:
Willy Tarreauf817e9f2014-01-10 16:58:45 +01001317 __conn_data_want_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001318}
1319
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001320/*
1321 * This function is used only for server health-checks. It handles connection
1322 * status updates including errors. If necessary, it wakes the check task up.
1323 * It always returns 0.
1324 */
1325static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001326{
Simon Horman4a741432013-02-23 15:35:38 +09001327 struct check *check = conn->owner;
Willy Tarreau20bea422012-07-06 12:00:49 +02001328
Willy Tarreau6c560da2012-11-24 11:14:45 +01001329 if (unlikely(conn->flags & CO_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001330 /* We may get error reports bypassing the I/O handlers, typically
1331 * the case when sending a pure TCP check which fails, then the I/O
1332 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001333 * main processing task so let's simply wake it up. If we get here,
1334 * we expect errno to still be valid.
1335 */
1336 chk_report_conn_err(conn, errno, 0);
1337
Willy Tarreau2d351b62013-12-05 02:36:25 +01001338 __conn_data_stop_both(conn);
1339 task_wakeup(check->task, TASK_WOKEN_IO);
1340 }
Willy Tarreau3be293f2014-02-05 18:31:24 +01001341 else if (!(conn->flags & (CO_FL_DATA_RD_ENA|CO_FL_DATA_WR_ENA|CO_FL_HANDSHAKE))) {
1342 /* we may get here if only a connection probe was required : we
1343 * don't have any data to send nor anything expected in response,
1344 * so the completion of the connection establishment is enough.
1345 */
1346 task_wakeup(check->task, TASK_WOKEN_IO);
1347 }
Willy Tarreau2d351b62013-12-05 02:36:25 +01001348
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001349 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001350 /* We're here because nobody wants to handle the error, so we
1351 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001352 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01001353 conn_sock_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02001354 conn_force_close(conn);
Willy Tarreau2d351b62013-12-05 02:36:25 +01001355 }
Willy Tarreau3267d362012-08-17 23:53:56 +02001356 return 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001357}
1358
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001359struct data_cb check_conn_cb = {
1360 .recv = event_srv_chk_r,
1361 .send = event_srv_chk_w,
1362 .wake = wake_srv_chk,
1363};
1364
Willy Tarreaubaaee002006-06-26 02:48:02 +02001365/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001366 * updates the server's weight during a warmup stage. Once the final weight is
1367 * reached, the task automatically stops. Note that any server status change
1368 * must have updated s->last_change accordingly.
1369 */
1370static struct task *server_warmup(struct task *t)
1371{
1372 struct server *s = t->context;
1373
1374 /* by default, plan on stopping the task */
1375 t->expire = TICK_ETERNITY;
Willy Tarreau20125212014-05-13 19:44:56 +02001376 if ((s->admin & SRV_ADMF_MAINT) ||
Willy Tarreau892337c2014-05-13 23:41:20 +02001377 (s->state != SRV_ST_STARTING))
Willy Tarreau2e993902011-10-31 11:53:20 +01001378 return t;
1379
Willy Tarreau892337c2014-05-13 23:41:20 +02001380 /* recalculate the weights and update the state */
Willy Tarreau004e0452013-11-21 11:22:01 +01001381 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001382
1383 /* probably that we can refill this server with a bit more connections */
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001384 pendconn_grab_from_px(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001385
1386 /* get back there in 1 second or 1/20th of the slowstart interval,
1387 * whichever is greater, resulting in small 5% steps.
1388 */
Willy Tarreau892337c2014-05-13 23:41:20 +02001389 if (s->state == SRV_ST_STARTING)
Willy Tarreau2e993902011-10-31 11:53:20 +01001390 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1391 return t;
1392}
1393
1394/*
Simon Horman98637e52014-06-20 12:30:16 +09001395 * establish a server health-check that makes use of a connection.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001396 *
1397 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001398 * - SF_ERR_NONE if everything's OK and tcpcheck_main() was not called
1399 * - SF_ERR_UP if if everything's OK and tcpcheck_main() was called
1400 * - SF_ERR_SRVTO if there are no more servers
1401 * - SF_ERR_SRVCL if the connection was refused by the server
1402 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1403 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1404 * - SF_ERR_INTERNAL for any other purely internal errors
1405 * Additionnally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001406 * Note that we try to prevent the network stack from sending the ACK during the
1407 * connect() when a pure TCP check is used (without PROXY protocol).
1408 */
Simon Horman98637e52014-06-20 12:30:16 +09001409static int connect_conn_chk(struct task *t)
Simon Hormanb00d17a2014-06-13 16:18:16 +09001410{
1411 struct check *check = t->context;
1412 struct server *s = check->server;
1413 struct connection *conn = check->conn;
1414 struct protocol *proto;
1415 int ret;
Willy Tarreauf3d34822014-12-08 12:11:28 +01001416 int quickack;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001417
1418 /* tcpcheck send/expect initialisation */
1419 if (check->type == PR_O2_TCPCHK_CHK)
1420 check->current_step = NULL;
1421
1422 /* prepare the check buffer.
1423 * This should not be used if check is the secondary agent check
1424 * of a server as s->proxy->check_req will relate to the
1425 * configuration of the primary check. Similarly, tcp-check uses
1426 * its own strings.
1427 */
1428 if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) {
1429 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
1430
1431 /* we want to check if this host replies to HTTP or SSLv3 requests
1432 * so we'll send the request, and won't wake the checker up now.
1433 */
1434 if ((check->type) == PR_O2_SSL3_CHK) {
1435 /* SSL requires that we put Unix time in the request */
1436 int gmt_time = htonl(date.tv_sec);
1437 memcpy(check->bo->data + 11, &gmt_time, 4);
1438 }
1439 else if ((check->type) == PR_O2_HTTP_CHK) {
1440 if (s->proxy->options2 & PR_O2_CHK_SNDST)
1441 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size));
Cyril Bonté32602d22015-01-30 00:07:07 +01001442 /* prevent HTTP keep-alive when "http-check expect" is used */
1443 if (s->proxy->options2 & PR_O2_EXP_TYPE)
1444 bo_putstr(check->bo, "Connection: close\r\n");
Simon Hormanb00d17a2014-06-13 16:18:16 +09001445 bo_putstr(check->bo, "\r\n");
1446 *check->bo->p = '\0'; /* to make gdb output easier to read */
1447 }
1448 }
1449
1450 /* prepare a new connection */
1451 conn_init(conn);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001452
Simon Horman41f58762015-01-30 11:22:56 +09001453 if (is_addr(&check->addr)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001454 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09001455 conn->addr.to = check->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001456 }
1457 else {
1458 /* we'll connect to the addr on the server */
1459 conn->addr.to = s->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001460 }
1461
1462 if (check->port) {
1463 set_host_port(&conn->addr.to, check->port);
1464 }
1465
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01001466 proto = protocol_by_family(conn->addr.to.ss_family);
1467
1468 conn_prepare(conn, proto, check->xprt);
1469 conn_attach(conn, check, &check_conn_cb);
1470 conn->target = &s->obj_type;
1471
1472 /* no client address */
1473 clear_addr(&conn->addr.from);
1474
Willy Tarreauf3d34822014-12-08 12:11:28 +01001475 /* only plain tcp-check supports quick ACK */
1476 quickack = check->type == 0 || check->type == PR_O2_TCPCHK_CHK;
1477
Simon Hormane16c1b32015-01-30 11:22:57 +09001478 if (check->type == PR_O2_TCPCHK_CHK && !LIST_ISEMPTY(check->tcpcheck_rules)) {
1479 struct tcpcheck_rule *r = (struct tcpcheck_rule *) check->tcpcheck_rules->n;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001480 /* if first step is a 'connect', then tcpcheck_main must run it */
1481 if (r->action == TCPCHK_ACT_CONNECT) {
1482 tcpcheck_main(conn);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001483 return SF_ERR_UP;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001484 }
Willy Tarreauf3d34822014-12-08 12:11:28 +01001485 if (r->action == TCPCHK_ACT_EXPECT)
1486 quickack = 0;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001487 }
1488
Willy Tarreaue7dff022015-04-03 01:14:29 +02001489 ret = SF_ERR_INTERNAL;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001490 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01001491 ret = proto->connect(conn, check->type, quickack ? 2 : 0);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001492 conn->flags |= CO_FL_WAKE_DATA;
1493 if (s->check.send_proxy) {
1494 conn->send_proxy_ofs = 1;
1495 conn->flags |= CO_FL_SEND_PROXY;
1496 }
1497
1498 return ret;
1499}
1500
Simon Horman98637e52014-06-20 12:30:16 +09001501static struct list pid_list = LIST_HEAD_INIT(pid_list);
1502static struct pool_head *pool2_pid_list;
1503
1504void block_sigchld(void)
1505{
1506 sigset_t set;
1507 sigemptyset(&set);
1508 sigaddset(&set, SIGCHLD);
1509 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
1510}
1511
1512void unblock_sigchld(void)
1513{
1514 sigset_t set;
1515 sigemptyset(&set);
1516 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
1517}
1518
1519/* Call with SIGCHLD blocked */
1520static struct pid_list *pid_list_add(pid_t pid, struct task *t)
1521{
1522 struct pid_list *elem;
1523 struct check *check = t->context;
1524
1525 elem = pool_alloc2(pool2_pid_list);
1526 if (!elem)
1527 return NULL;
1528 elem->pid = pid;
1529 elem->t = t;
1530 elem->exited = 0;
1531 check->curpid = elem;
1532 LIST_INIT(&elem->list);
1533 LIST_ADD(&pid_list, &elem->list);
1534 return elem;
1535}
1536
1537/* Blocks blocks and then unblocks SIGCHLD */
1538static void pid_list_del(struct pid_list *elem)
1539{
1540 struct check *check;
1541
1542 if (!elem)
1543 return;
1544
1545 block_sigchld();
1546 LIST_DEL(&elem->list);
1547 unblock_sigchld();
1548 if (!elem->exited)
1549 kill(elem->pid, SIGTERM);
1550
1551 check = elem->t->context;
1552 check->curpid = NULL;
1553 pool_free2(pool2_pid_list, elem);
1554}
1555
1556/* Called from inside SIGCHLD handler, SIGCHLD is blocked */
1557static void pid_list_expire(pid_t pid, int status)
1558{
1559 struct pid_list *elem;
1560
1561 list_for_each_entry(elem, &pid_list, list) {
1562 if (elem->pid == pid) {
1563 elem->t->expire = now_ms;
1564 elem->status = status;
1565 elem->exited = 1;
Cyril Bonté9dbcfab2014-08-07 01:55:39 +02001566 task_wakeup(elem->t, TASK_WOKEN_IO);
Simon Horman98637e52014-06-20 12:30:16 +09001567 return;
1568 }
1569 }
1570}
1571
1572static void sigchld_handler(int signal)
1573{
1574 pid_t pid;
1575 int status;
1576 while ((pid = waitpid(0, &status, WNOHANG)) > 0)
1577 pid_list_expire(pid, status);
1578}
1579
1580static int init_pid_list(void) {
1581 struct sigaction action = {
1582 .sa_handler = sigchld_handler,
1583 .sa_flags = SA_NOCLDSTOP
1584 };
1585
1586 if (pool2_pid_list != NULL)
1587 /* Nothing to do */
1588 return 0;
1589
1590 if (sigaction(SIGCHLD, &action, NULL)) {
1591 Alert("Failed to set signal handler for external health checks: %s. Aborting.\n",
1592 strerror(errno));
1593 return 1;
1594 }
1595
1596 pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED);
1597 if (pool2_pid_list == NULL) {
1598 Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n",
1599 strerror(errno));
1600 return 1;
1601 }
1602
1603 return 0;
1604}
1605
Cyril Bontéac92a062014-12-27 22:28:38 +01001606/* helper macro to set an environment variable and jump to a specific label on failure. */
1607#define EXTCHK_SETENV(check, envidx, value, fail) { if (extchk_setenv(check, envidx, value)) goto fail; }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001608
1609/*
Cyril Bontéac92a062014-12-27 22:28:38 +01001610 * helper function to allocate enough memory to store an environment variable.
1611 * It will also check that the environment variable is updatable, and silently
1612 * fail if not.
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001613 */
Cyril Bontéac92a062014-12-27 22:28:38 +01001614static int extchk_setenv(struct check *check, int idx, const char *value)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001615{
1616 int len, ret;
Cyril Bontéac92a062014-12-27 22:28:38 +01001617 char *envname;
1618 int vmaxlen;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001619
Cyril Bontéac92a062014-12-27 22:28:38 +01001620 if (idx < 0 || idx >= EXTCHK_SIZE) {
1621 Alert("Illegal environment variable index %d. Aborting.\n", idx);
1622 return 1;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001623 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001624
1625 envname = extcheck_envs[idx].name;
1626 vmaxlen = extcheck_envs[idx].vmaxlen;
1627
1628 /* Check if the environment variable is already set, and silently reject
1629 * the update if this one is not updatable. */
1630 if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx]))
1631 return 0;
1632
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001633 /* Instead of sending NOT_USED, sending an empty value is preferable */
1634 if (strcmp(value, "NOT_USED") == 0) {
1635 value = "";
1636 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001637
1638 len = strlen(envname) + 1;
1639 if (vmaxlen == EXTCHK_SIZE_EVAL_INIT)
1640 len += strlen(value);
1641 else
1642 len += vmaxlen;
1643
1644 if (!check->envp[idx])
1645 check->envp[idx] = malloc(len + 1);
1646
1647 if (!check->envp[idx]) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001648 Alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname);
1649 return 1;
1650 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001651 ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001652 if (ret < 0) {
1653 Alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno));
1654 return 1;
1655 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001656 else if (ret > len) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001657 Alert("Environment variable '%s' was truncated. Aborting.\n", envname);
1658 return 1;
1659 }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001660 return 0;
1661}
Simon Horman98637e52014-06-20 12:30:16 +09001662
1663static int prepare_external_check(struct check *check)
1664{
1665 struct server *s = check->server;
1666 struct proxy *px = s->proxy;
1667 struct listener *listener = NULL, *l;
1668 int i;
Simon Horman98637e52014-06-20 12:30:16 +09001669 const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001670 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001671
1672 list_for_each_entry(l, &px->conf.listeners, by_fe)
1673 /* Use the first INET, INET6 or UNIX listener */
1674 if (l->addr.ss_family == AF_INET ||
1675 l->addr.ss_family == AF_INET6 ||
1676 l->addr.ss_family == AF_UNIX) {
1677 listener = l;
1678 break;
1679 }
1680
Simon Horman98637e52014-06-20 12:30:16 +09001681 check->curpid = NULL;
Cyril Bontéac92a062014-12-27 22:28:38 +01001682 check->envp = calloc((EXTCHK_SIZE + 1), sizeof(char *));
1683 if (!check->envp) {
1684 Alert("Failed to allocate memory for environment variables. Aborting\n");
1685 goto err;
1686 }
Simon Horman98637e52014-06-20 12:30:16 +09001687
Cyril Bontéac92a062014-12-27 22:28:38 +01001688 check->argv = calloc(6, sizeof(char *));
1689 if (!check->argv) {
1690 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001691 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001692 }
Simon Horman98637e52014-06-20 12:30:16 +09001693
1694 check->argv[0] = px->check_command;
1695
Cyril Bonté777be862014-12-02 21:21:35 +01001696 if (!listener) {
1697 check->argv[1] = strdup("NOT_USED");
1698 check->argv[2] = strdup("NOT_USED");
1699 }
1700 else if (listener->addr.ss_family == AF_INET ||
Simon Horman98637e52014-06-20 12:30:16 +09001701 listener->addr.ss_family == AF_INET6) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001702 addr_to_str(&listener->addr, buf, sizeof(buf));
1703 check->argv[1] = strdup(buf);
1704 port_to_str(&listener->addr, buf, sizeof(buf));
1705 check->argv[2] = strdup(buf);
Cyril Bonté777be862014-12-02 21:21:35 +01001706 }
1707 else if (listener->addr.ss_family == AF_UNIX) {
Simon Horman98637e52014-06-20 12:30:16 +09001708 const struct sockaddr_un *un;
1709
1710 un = (struct sockaddr_un *)&listener->addr;
1711 check->argv[1] = strdup(un->sun_path);
1712 check->argv[2] = strdup("NOT_USED");
Cyril Bonté777be862014-12-02 21:21:35 +01001713 }
1714 else {
Cyril Bontéac92a062014-12-27 22:28:38 +01001715 Alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001716 goto err;
1717 }
1718
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001719 addr_to_str(&s->addr, buf, sizeof(buf));
1720 check->argv[3] = strdup(buf);
1721 port_to_str(&s->addr, buf, sizeof(buf));
1722 check->argv[4] = strdup(buf);
Simon Horman98637e52014-06-20 12:30:16 +09001723
Cyril Bontéac92a062014-12-27 22:28:38 +01001724 for (i = 0; i < 5; i++) {
1725 if (!check->argv[i]) {
1726 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001727 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001728 }
1729 }
Simon Horman98637e52014-06-20 12:30:16 +09001730
Cyril Bontéac92a062014-12-27 22:28:38 +01001731 EXTCHK_SETENV(check, EXTCHK_PATH, path, err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001732 /* Add proxy environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001733 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err);
1734 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err);
1735 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err);
1736 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001737 /* Add server environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001738 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err);
1739 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err);
1740 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err);
1741 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err);
1742 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err);
1743 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), err);
1744
1745 /* Ensure that we don't leave any hole in check->envp */
1746 for (i = 0; i < EXTCHK_SIZE; i++)
1747 if (!check->envp[i])
1748 EXTCHK_SETENV(check, i, "", err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001749
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001750 return 1;
Simon Horman98637e52014-06-20 12:30:16 +09001751err:
1752 if (check->envp) {
Cyril Bontéac92a062014-12-27 22:28:38 +01001753 for (i = 0; i < EXTCHK_SIZE; i++)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001754 free(check->envp[i]);
Simon Horman98637e52014-06-20 12:30:16 +09001755 free(check->envp);
1756 check->envp = NULL;
1757 }
1758
1759 if (check->argv) {
1760 for (i = 1; i < 5; i++)
1761 free(check->argv[i]);
1762 free(check->argv);
1763 check->argv = NULL;
1764 }
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001765 return 0;
Simon Horman98637e52014-06-20 12:30:16 +09001766}
1767
Simon Hormanb00d17a2014-06-13 16:18:16 +09001768/*
Simon Horman98637e52014-06-20 12:30:16 +09001769 * establish a server health-check that makes use of a process.
1770 *
1771 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001772 * - SF_ERR_NONE if everything's OK
1773 * - SF_ERR_SRVTO if there are no more servers
1774 * - SF_ERR_SRVCL if the connection was refused by the server
1775 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1776 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1777 * - SF_ERR_INTERNAL for any other purely internal errors
1778 * Additionnally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Horman98637e52014-06-20 12:30:16 +09001779 *
1780 * Blocks and then unblocks SIGCHLD
1781 */
1782static int connect_proc_chk(struct task *t)
1783{
Cyril Bontéac92a062014-12-27 22:28:38 +01001784 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001785 struct check *check = t->context;
1786 struct server *s = check->server;
1787 struct proxy *px = s->proxy;
1788 int status;
1789 pid_t pid;
1790
Willy Tarreaue7dff022015-04-03 01:14:29 +02001791 status = SF_ERR_RESOURCE;
Simon Horman98637e52014-06-20 12:30:16 +09001792
1793 block_sigchld();
1794
1795 pid = fork();
1796 if (pid < 0) {
1797 Alert("Failed to fork process for external health check: %s. Aborting.\n",
1798 strerror(errno));
1799 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1800 goto out;
1801 }
1802 if (pid == 0) {
1803 /* Child */
1804 extern char **environ;
1805 environ = check->envp;
Cyril Bontéac92a062014-12-27 22:28:38 +01001806 extchk_setenv(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)));
Simon Horman98637e52014-06-20 12:30:16 +09001807 execvp(px->check_command, check->argv);
1808 Alert("Failed to exec process for external health check: %s. Aborting.\n",
1809 strerror(errno));
1810 exit(-1);
1811 }
1812
1813 /* Parent */
1814 if (check->result == CHK_RES_UNKNOWN) {
1815 if (pid_list_add(pid, t) != NULL) {
1816 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1817
1818 if (px->timeout.check && px->timeout.connect) {
1819 int t_con = tick_add(now_ms, px->timeout.connect);
1820 t->expire = tick_first(t->expire, t_con);
1821 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02001822 status = SF_ERR_NONE;
Simon Horman98637e52014-06-20 12:30:16 +09001823 goto out;
1824 }
1825 else {
1826 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1827 }
1828 kill(pid, SIGTERM); /* process creation error */
1829 }
1830 else
1831 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1832
1833out:
1834 unblock_sigchld();
1835 return status;
1836}
1837
1838/*
Simon Horman98637e52014-06-20 12:30:16 +09001839 * manages a server health-check that uses a process. Returns
Willy Tarreaubaaee002006-06-26 02:48:02 +02001840 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1841 */
Simon Horman98637e52014-06-20 12:30:16 +09001842static struct task *process_chk_proc(struct task *t)
1843{
1844 struct check *check = t->context;
1845 struct server *s = check->server;
1846 struct connection *conn = check->conn;
1847 int rv;
1848 int ret;
1849 int expired = tick_is_expired(t->expire, now_ms);
1850
1851 if (!(check->state & CHK_ST_INPROGRESS)) {
1852 /* no check currently running */
1853 if (!expired) /* woke up too early */
1854 return t;
1855
1856 /* we don't send any health-checks when the proxy is
1857 * stopped, the server should not be checked or the check
1858 * is disabled.
1859 */
1860 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
1861 s->proxy->state == PR_STSTOPPED)
1862 goto reschedule;
1863
1864 /* we'll initiate a new check */
1865 set_server_check_status(check, HCHK_STATUS_START, NULL);
1866
1867 check->state |= CHK_ST_INPROGRESS;
1868
Simon Hormandbf70192015-01-30 11:22:53 +09001869 ret = connect_proc_chk(t);
Simon Horman98637e52014-06-20 12:30:16 +09001870 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001871 case SF_ERR_UP:
Simon Horman98637e52014-06-20 12:30:16 +09001872 return t;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001873 case SF_ERR_NONE:
Simon Horman98637e52014-06-20 12:30:16 +09001874 /* we allow up to min(inter, timeout.connect) for a connection
1875 * to establish but only when timeout.check is set
1876 * as it may be to short for a full check otherwise
1877 */
1878 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1879
1880 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1881 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1882 t->expire = tick_first(t->expire, t_con);
1883 }
1884
1885 goto reschedule;
1886
Willy Tarreaue7dff022015-04-03 01:14:29 +02001887 case SF_ERR_SRVTO: /* ETIMEDOUT */
1888 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Simon Horman98637e52014-06-20 12:30:16 +09001889 conn->flags |= CO_FL_ERROR;
1890 chk_report_conn_err(conn, errno, 0);
1891 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001892 case SF_ERR_PRXCOND:
1893 case SF_ERR_RESOURCE:
1894 case SF_ERR_INTERNAL:
Simon Horman98637e52014-06-20 12:30:16 +09001895 conn->flags |= CO_FL_ERROR;
1896 chk_report_conn_err(conn, 0, 0);
1897 break;
1898 }
1899
1900 /* here, we have seen a synchronous error, no fd was allocated */
1901
1902 check->state &= ~CHK_ST_INPROGRESS;
1903 check_notify_failure(check);
1904
1905 /* we allow up to min(inter, timeout.connect) for a connection
1906 * to establish but only when timeout.check is set
1907 * as it may be to short for a full check otherwise
1908 */
1909 while (tick_is_expired(t->expire, now_ms)) {
1910 int t_con;
1911
1912 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1913 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1914
1915 if (s->proxy->timeout.check)
1916 t->expire = tick_first(t->expire, t_con);
1917 }
1918 }
1919 else {
1920 /* there was a test running.
1921 * First, let's check whether there was an uncaught error,
1922 * which can happen on connect timeout or error.
1923 */
1924 if (check->result == CHK_RES_UNKNOWN) {
1925 /* good connection is enough for pure TCP check */
1926 struct pid_list *elem = check->curpid;
1927 int status = HCHK_STATUS_UNKNOWN;
1928
1929 if (elem->exited) {
1930 status = elem->status; /* Save in case the process exits between use below */
1931 if (!WIFEXITED(status))
1932 check->code = -1;
1933 else
1934 check->code = WEXITSTATUS(status);
1935 if (!WIFEXITED(status) || WEXITSTATUS(status))
1936 status = HCHK_STATUS_PROCERR;
1937 else
1938 status = HCHK_STATUS_PROCOK;
1939 } else if (expired) {
1940 status = HCHK_STATUS_PROCTOUT;
Willy Tarreaudc3d1902014-07-08 00:56:27 +02001941 Warning("kill %d\n", (int)elem->pid);
Simon Horman98637e52014-06-20 12:30:16 +09001942 kill(elem->pid, SIGTERM);
1943 }
1944 set_server_check_status(check, status, NULL);
1945 }
1946
1947 if (check->result == CHK_RES_FAILED) {
1948 /* a failure or timeout detected */
1949 check_notify_failure(check);
1950 }
1951 else if (check->result == CHK_RES_CONDPASS) {
1952 /* check is OK but asks for stopping mode */
1953 check_notify_stopping(check);
1954 }
1955 else if (check->result == CHK_RES_PASSED) {
1956 /* a success was detected */
1957 check_notify_success(check);
1958 }
1959 check->state &= ~CHK_ST_INPROGRESS;
1960
1961 pid_list_del(check->curpid);
1962
1963 rv = 0;
1964 if (global.spread_checks > 0) {
1965 rv = srv_getinter(check) * global.spread_checks / 100;
1966 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
1967 }
1968 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
1969 }
1970
1971 reschedule:
1972 while (tick_is_expired(t->expire, now_ms))
1973 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1974 return t;
1975}
1976
1977/*
1978 * manages a server health-check that uses a connection. Returns
1979 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1980 */
1981static struct task *process_chk_conn(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001982{
Simon Horman4a741432013-02-23 15:35:38 +09001983 struct check *check = t->context;
1984 struct server *s = check->server;
1985 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001986 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001987 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001988 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001989
Willy Tarreau2c115e52013-12-11 19:41:16 +01001990 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01001991 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001992 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01001993 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001994
Simon Horman671b6f02013-11-25 10:46:39 +09001995 /* we don't send any health-checks when the proxy is
1996 * stopped, the server should not be checked or the check
1997 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001998 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01001999 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01002000 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01002001 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002002
2003 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09002004 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002005
Willy Tarreau2c115e52013-12-11 19:41:16 +01002006 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09002007 check->bi->p = check->bi->data;
2008 check->bi->i = 0;
2009 check->bo->p = check->bo->data;
2010 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002011
Simon Hormandbf70192015-01-30 11:22:53 +09002012 ret = connect_conn_chk(t);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002013 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002014 case SF_ERR_UP:
Simon Hormanb00d17a2014-06-13 16:18:16 +09002015 return t;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002016 case SF_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002017 /* we allow up to min(inter, timeout.connect) for a connection
2018 * to establish but only when timeout.check is set
2019 * as it may be to short for a full check otherwise
2020 */
Simon Horman4a741432013-02-23 15:35:38 +09002021 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002022
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002023 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2024 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2025 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002026 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01002027
2028 if (check->type)
2029 conn_data_want_recv(conn); /* prepare for reading a possible reply */
2030
Willy Tarreau5a78f362012-11-23 12:47:05 +01002031 goto reschedule;
2032
Willy Tarreaue7dff022015-04-03 01:14:29 +02002033 case SF_ERR_SRVTO: /* ETIMEDOUT */
2034 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau4bd07de2014-01-24 16:10:57 +01002035 conn->flags |= CO_FL_ERROR;
2036 chk_report_conn_err(conn, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002037 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002038 case SF_ERR_PRXCOND:
2039 case SF_ERR_RESOURCE:
2040 case SF_ERR_INTERNAL:
Willy Tarreau4bd07de2014-01-24 16:10:57 +01002041 conn->flags |= CO_FL_ERROR;
2042 chk_report_conn_err(conn, 0, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002043 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002044 }
2045
Willy Tarreau5a78f362012-11-23 12:47:05 +01002046 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01002047
Willy Tarreau2c115e52013-12-11 19:41:16 +01002048 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02002049 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002050
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002051 /* we allow up to min(inter, timeout.connect) for a connection
2052 * to establish but only when timeout.check is set
2053 * as it may be to short for a full check otherwise
2054 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002055 while (tick_is_expired(t->expire, now_ms)) {
2056 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002057
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002058 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09002059 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002060
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002061 if (s->proxy->timeout.check)
2062 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002063 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002064 }
2065 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02002066 /* there was a test running.
2067 * First, let's check whether there was an uncaught error,
2068 * which can happen on connect timeout or error.
2069 */
Simon Hormanccaabcd2014-06-20 12:29:47 +09002070 if (check->result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002071 /* good connection is enough for pure TCP check */
2072 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09002073 if (check->use_ssl)
2074 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02002075 else
Simon Horman4a741432013-02-23 15:35:38 +09002076 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002077 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002078 else if ((conn->flags & CO_FL_ERROR) || expired) {
2079 chk_report_conn_err(conn, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02002080 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002081 else
2082 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02002083 }
2084
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002085 /* check complete or aborted */
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002086 if (conn->xprt) {
2087 /* The check was aborted and the connection was not yet closed.
2088 * This can happen upon timeout, or when an external event such
2089 * as a failed response coupled with "observe layer7" caused the
2090 * server state to be suddenly changed.
2091 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01002092 conn_sock_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02002093 conn_force_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002094 }
2095
Willy Tarreauaf549582014-05-16 17:37:50 +02002096 if (check->result == CHK_RES_FAILED) {
2097 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002098 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002099 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002100 else if (check->result == CHK_RES_CONDPASS) {
2101 /* check is OK but asks for stopping mode */
2102 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002103 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002104 else if (check->result == CHK_RES_PASSED) {
2105 /* a success was detected */
2106 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002107 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01002108 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002109
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002110 rv = 0;
2111 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002112 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002113 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002114 }
Simon Horman4a741432013-02-23 15:35:38 +09002115 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002116 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002117
2118 reschedule:
2119 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002120 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002121 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01002122 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002123}
2124
Simon Horman98637e52014-06-20 12:30:16 +09002125/*
2126 * manages a server health-check. Returns
2127 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2128 */
2129static struct task *process_chk(struct task *t)
2130{
2131 struct check *check = t->context;
2132
2133 if (check->type == PR_O2_EXT_CHK)
2134 return process_chk_proc(t);
2135 return process_chk_conn(t);
2136}
2137
Simon Horman5c942422013-11-25 10:46:32 +09002138static int start_check_task(struct check *check, int mininter,
2139 int nbcheck, int srvpos)
2140{
2141 struct task *t;
2142 /* task for the check */
2143 if ((t = task_new()) == NULL) {
2144 Alert("Starting [%s:%s] check: out of memory.\n",
2145 check->server->proxy->id, check->server->id);
2146 return 0;
2147 }
2148
2149 check->task = t;
2150 t->process = process_chk;
2151 t->context = check;
2152
Willy Tarreau1746eec2014-04-25 10:46:47 +02002153 if (mininter < srv_getinter(check))
2154 mininter = srv_getinter(check);
2155
2156 if (global.max_spread_checks && mininter > global.max_spread_checks)
2157 mininter = global.max_spread_checks;
2158
Simon Horman5c942422013-11-25 10:46:32 +09002159 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002160 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002161 check->start = now;
2162 task_queue(t);
2163
2164 return 1;
2165}
2166
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002167/*
2168 * Start health-check.
2169 * Returns 0 if OK, -1 if error, and prints the error in this case.
2170 */
2171int start_checks() {
2172
2173 struct proxy *px;
2174 struct server *s;
2175 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002176 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002177
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002178 /* 1- count the checkers to run simultaneously.
2179 * We also determine the minimum interval among all of those which
2180 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2181 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002182 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002183 * too short an interval for all others.
2184 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002185 for (px = proxy; px; px = px->next) {
2186 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002187 if (s->slowstart) {
2188 if ((t = task_new()) == NULL) {
2189 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
2190 return -1;
2191 }
2192 /* We need a warmup task that will be called when the server
2193 * state switches from down to up.
2194 */
2195 s->warmup = t;
2196 t->process = server_warmup;
2197 t->context = s;
2198 t->expire = TICK_ETERNITY;
2199 }
2200
Willy Tarreaud8514a22013-12-11 21:10:14 +01002201 if (s->check.state & CHK_ST_CONFIGURED) {
2202 nbcheck++;
2203 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2204 (!mininter || mininter > srv_getinter(&s->check)))
2205 mininter = srv_getinter(&s->check);
2206 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002207
Willy Tarreaud8514a22013-12-11 21:10:14 +01002208 if (s->agent.state & CHK_ST_CONFIGURED) {
2209 nbcheck++;
2210 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2211 (!mininter || mininter > srv_getinter(&s->agent)))
2212 mininter = srv_getinter(&s->agent);
2213 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002214 }
2215 }
2216
Simon Horman4a741432013-02-23 15:35:38 +09002217 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002218 return 0;
2219
2220 srand((unsigned)time(NULL));
2221
2222 /*
2223 * 2- start them as far as possible from each others. For this, we will
2224 * start them after their interval set to the min interval divided by
2225 * the number of servers, weighted by the server's position in the list.
2226 */
2227 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002228 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2229 if (init_pid_list()) {
2230 Alert("Starting [%s] check: out of memory.\n", px->id);
2231 return -1;
2232 }
2233 }
2234
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002235 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002236 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002237 if (s->check.state & CHK_ST_CONFIGURED) {
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002238 if (s->check.type == PR_O2_EXT_CHK) {
2239 if (!prepare_external_check(&s->check))
2240 return -1;
2241 }
Simon Hormand60d6912013-11-25 10:46:36 +09002242 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
2243 return -1;
2244 srvpos++;
2245 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002246
Simon Hormand60d6912013-11-25 10:46:36 +09002247 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002248 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002249 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
2250 return -1;
2251 }
2252 srvpos++;
2253 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002254 }
2255 }
2256 return 0;
2257}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002258
2259/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002260 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002261 * The buffer MUST be terminated by a null byte before calling this function.
2262 * Sets server status appropriately. The caller is responsible for ensuring
2263 * that the buffer contains at least 13 characters. If <done> is zero, we may
2264 * return 0 to indicate that data is required to decide of a match.
2265 */
2266static int httpchk_expect(struct server *s, int done)
2267{
2268 static char status_msg[] = "HTTP status check returned code <000>";
2269 char status_code[] = "000";
2270 char *contentptr;
2271 int crlf;
2272 int ret;
2273
2274 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2275 case PR_O2_EXP_STS:
2276 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002277 memcpy(status_code, s->check.bi->data + 9, 3);
2278 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002279
2280 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2281 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2282 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002283 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002284
2285 /* we necessarily have the response, so there are no partial failures */
2286 if (s->proxy->options2 & PR_O2_EXP_INV)
2287 ret = !ret;
2288
Simon Horman4a741432013-02-23 15:35:38 +09002289 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002290 break;
2291
2292 case PR_O2_EXP_STR:
2293 case PR_O2_EXP_RSTR:
2294 /* very simple response parser: ignore CR and only count consecutive LFs,
2295 * stop with contentptr pointing to first char after the double CRLF or
2296 * to '\0' if crlf < 2.
2297 */
2298 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002299 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002300 if (crlf >= 2)
2301 break;
2302 if (*contentptr == '\r')
2303 continue;
2304 else if (*contentptr == '\n')
2305 crlf++;
2306 else
2307 crlf = 0;
2308 }
2309
2310 /* Check that response contains a body... */
2311 if (crlf < 2) {
2312 if (!done)
2313 return 0;
2314
Simon Horman4a741432013-02-23 15:35:38 +09002315 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002316 "HTTP content check could not find a response body");
2317 return 1;
2318 }
2319
2320 /* Check that response body is not empty... */
2321 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002322 if (!done)
2323 return 0;
2324
Simon Horman4a741432013-02-23 15:35:38 +09002325 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002326 "HTTP content check found empty response body");
2327 return 1;
2328 }
2329
2330 /* Check the response content against the supplied string
2331 * or regex... */
2332 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2333 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2334 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002335 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002336
2337 /* if we don't match, we may need to wait more */
2338 if (!ret && !done)
2339 return 0;
2340
2341 if (ret) {
2342 /* content matched */
2343 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002344 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002345 "HTTP check matched unwanted content");
2346 else
Simon Horman4a741432013-02-23 15:35:38 +09002347 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002348 "HTTP content check matched");
2349 }
2350 else {
2351 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002352 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002353 "HTTP check did not match unwanted content");
2354 else
Simon Horman4a741432013-02-23 15:35:38 +09002355 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002356 "HTTP content check did not match");
2357 }
2358 break;
2359 }
2360 return 1;
2361}
2362
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002363/*
2364 * return the id of a step in a send/expect session
2365 */
Simon Hormane16c1b32015-01-30 11:22:57 +09002366static int tcpcheck_get_step_id(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002367{
2368 struct tcpcheck_rule *cur = NULL, *next = NULL;
2369 int i = 0;
2370
Willy Tarreau213c6782014-10-02 14:51:02 +02002371 /* not even started anything yet => step 0 = initial connect */
Baptiste Assmannf95bc8e2015-04-25 16:03:06 +02002372 if (!check->current_step)
Willy Tarreau213c6782014-10-02 14:51:02 +02002373 return 0;
2374
Simon Hormane16c1b32015-01-30 11:22:57 +09002375 cur = check->last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002376
2377 /* no step => first step */
2378 if (cur == NULL)
2379 return 1;
2380
2381 /* increment i until current step */
Simon Hormane16c1b32015-01-30 11:22:57 +09002382 list_for_each_entry(next, check->tcpcheck_rules, list) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002383 if (next->list.p == &cur->list)
2384 break;
2385 ++i;
2386 }
2387
2388 return i;
2389}
2390
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002391/*
2392 * return the latest known comment before (including) the given stepid
2393 * returns NULL if no comment found
2394 */
2395static char * tcpcheck_get_step_comment(struct check *check, int stepid)
2396{
2397 struct tcpcheck_rule *cur = NULL;
2398 char *ret = NULL;
2399 int i = 0;
2400
2401 /* not even started anything yet, return latest comment found before any action */
2402 if (!check->current_step) {
2403 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2404 if (cur->action == TCPCHK_ACT_COMMENT)
2405 ret = cur->comment;
2406 else
2407 goto return_comment;
2408 }
2409 }
2410
2411 i = 1;
2412 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2413 if (cur->comment)
2414 ret = cur->comment;
2415
2416 if (i >= stepid)
2417 goto return_comment;
2418
2419 ++i;
2420 }
2421
2422 return_comment:
2423 return ret;
2424}
2425
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002426static void tcpcheck_main(struct connection *conn)
2427{
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002428 char *contentptr, *comment;
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002429 struct tcpcheck_rule *next;
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002430 int done = 0, ret = 0, step = 0;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002431 struct check *check = conn->owner;
2432 struct server *s = check->server;
2433 struct task *t = check->task;
Simon Hormane16c1b32015-01-30 11:22:57 +09002434 struct list *head = check->tcpcheck_rules;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002435
Willy Tarreauef953952014-10-02 14:30:14 +02002436 /* here, we know that the check is complete or that it failed */
2437 if (check->result != CHK_RES_UNKNOWN)
2438 goto out_end_tcpcheck;
2439
2440 /* We have 4 possibilities here :
2441 * 1. we've not yet attempted step 1, and step 1 is a connect, so no
2442 * connection attempt was made yet ;
2443 * 2. we've not yet attempted step 1, and step 1 is a not connect or
2444 * does not exist (no rule), so a connection attempt was made
2445 * before coming here.
2446 * 3. we're coming back after having started with step 1, so we may
2447 * be waiting for a connection attempt to complete.
2448 * 4. the connection + handshake are complete
2449 *
2450 * #2 and #3 are quite similar, we want both the connection and the
2451 * handshake to complete before going any further. Thus we must always
2452 * wait for a connection to complete unless we're before and existing
2453 * step 1.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002454 */
Willy Tarreauef953952014-10-02 14:30:14 +02002455 if ((!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE)) &&
2456 (check->current_step || LIST_ISEMPTY(head))) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002457 /* we allow up to min(inter, timeout.connect) for a connection
2458 * to establish but only when timeout.check is set
2459 * as it may be to short for a full check otherwise
2460 */
2461 while (tick_is_expired(t->expire, now_ms)) {
2462 int t_con;
2463
2464 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2465 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2466
2467 if (s->proxy->timeout.check)
2468 t->expire = tick_first(t->expire, t_con);
2469 }
2470 return;
2471 }
2472
Willy Tarreauef953952014-10-02 14:30:14 +02002473 /* special case: option tcp-check with no rule, a connect is enough */
2474 if (LIST_ISEMPTY(head)) {
2475 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002476 goto out_end_tcpcheck;
Willy Tarreauef953952014-10-02 14:30:14 +02002477 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002478
Willy Tarreau213c6782014-10-02 14:51:02 +02002479 /* no step means first step initialisation */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002480 if (check->current_step == NULL) {
Willy Tarreau213c6782014-10-02 14:51:02 +02002481 check->last_started_step = NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002482 check->bo->p = check->bo->data;
2483 check->bo->o = 0;
2484 check->bi->p = check->bi->data;
2485 check->bi->i = 0;
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002486 check->current_step = LIST_ELEM(head->n, struct tcpcheck_rule *, list);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002487 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2488 if (s->proxy->timeout.check)
2489 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2490 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002491
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002492 /* It's only the rules which will enable send/recv */
2493 __conn_data_stop_both(conn);
2494
Willy Tarreauabca5b62013-12-06 14:19:25 +01002495 while (1) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002496 /* we have to try to flush the output buffer before reading, at the end,
2497 * or if we're about to send a string that does not fit in the remaining space.
2498 */
2499 if (check->bo->o &&
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002500 (&check->current_step->list == head ||
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002501 check->current_step->action != TCPCHK_ACT_SEND ||
2502 check->current_step->string_len >= buffer_total_space(check->bo))) {
2503
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002504 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002505 if (conn->flags & CO_FL_ERROR) {
2506 chk_report_conn_err(conn, errno, 0);
2507 __conn_data_stop_both(conn);
2508 goto out_end_tcpcheck;
2509 }
2510 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002511 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002512 }
2513
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002514 /* did we reach the end ? If so, let's check that everything was sent */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002515 if (&check->current_step->list == head) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002516 if (check->bo->o)
2517 goto out_need_io;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002518 break;
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002519 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002520
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002521 /* have 'next' point to the next rule or NULL if we're on the
2522 * last one, connect() needs this.
2523 */
2524 next = (struct tcpcheck_rule *)check->current_step->list.n;
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002525
2526 /* bypass all comment rules */
2527 while (next->action == TCPCHK_ACT_COMMENT)
2528 next = (struct tcpcheck_rule *)next->list.n;
2529
2530 /* NULL if we're on the last rule */
Willy Tarreauf3d34822014-12-08 12:11:28 +01002531 if (&next->list == head)
2532 next = NULL;
2533
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002534 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2535 struct protocol *proto;
2536 struct xprt_ops *xprt;
2537
2538 /* mark the step as started */
2539 check->last_started_step = check->current_step;
2540 /* first, shut existing connection */
2541 conn_force_close(conn);
2542
2543 /* prepare new connection */
2544 /* initialization */
2545 conn_init(conn);
2546 conn_attach(conn, check, &check_conn_cb);
2547 conn->target = &s->obj_type;
2548
2549 /* no client address */
2550 clear_addr(&conn->addr.from);
2551
Simon Horman41f58762015-01-30 11:22:56 +09002552 if (is_addr(&check->addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002553 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09002554 conn->addr.to = check->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002555 }
2556 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002557 /* we'll connect to the addr on the server */
2558 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002559 }
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01002560 proto = protocol_by_family(conn->addr.to.ss_family);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002561
2562 /* port */
2563 if (check->current_step->port)
2564 set_host_port(&conn->addr.to, check->current_step->port);
2565 else if (check->port)
2566 set_host_port(&conn->addr.to, check->port);
2567
2568#ifdef USE_OPENSSL
2569 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
2570 xprt = &ssl_sock;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002571 }
2572 else {
2573 xprt = &raw_sock;
2574 }
2575#else /* USE_OPENSSL */
2576 xprt = &raw_sock;
2577#endif /* USE_OPENSSL */
2578 conn_prepare(conn, proto, xprt);
2579
Willy Tarreaue7dff022015-04-03 01:14:29 +02002580 ret = SF_ERR_INTERNAL;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002581 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01002582 ret = proto->connect(conn,
2583 1 /* I/O polling is always needed */,
2584 (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002585 conn->flags |= CO_FL_WAKE_DATA;
2586 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2587 conn->send_proxy_ofs = 1;
2588 conn->flags |= CO_FL_SEND_PROXY;
2589 }
2590
2591 /* It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02002592 * - SF_ERR_NONE if everything's OK
2593 * - SF_ERR_SRVTO if there are no more servers
2594 * - SF_ERR_SRVCL if the connection was refused by the server
2595 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2596 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2597 * - SF_ERR_INTERNAL for any other purely internal errors
2598 * Additionnally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002599 * Note that we try to prevent the network stack from sending the ACK during the
2600 * connect() when a pure TCP check is used (without PROXY protocol).
2601 */
2602 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002603 case SF_ERR_NONE:
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002604 /* we allow up to min(inter, timeout.connect) for a connection
2605 * to establish but only when timeout.check is set
2606 * as it may be to short for a full check otherwise
2607 */
2608 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2609
2610 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2611 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2612 t->expire = tick_first(t->expire, t_con);
2613 }
2614 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002615 case SF_ERR_SRVTO: /* ETIMEDOUT */
2616 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002617 step = tcpcheck_get_step_id(check);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002618 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002619 step, strerror(errno));
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002620 comment = tcpcheck_get_step_comment(check, step);
2621 if (comment)
2622 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002623 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2624 goto out_end_tcpcheck;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002625 case SF_ERR_PRXCOND:
2626 case SF_ERR_RESOURCE:
2627 case SF_ERR_INTERNAL:
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002628 step = tcpcheck_get_step_id(check);
2629 chunk_printf(&trash, "TCPCHK error establishing connection at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002630 comment = tcpcheck_get_step_comment(check, step);
2631 if (comment)
2632 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002633 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2634 goto out_end_tcpcheck;
2635 }
2636
2637 /* allow next rule */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002638 check->current_step = (struct tcpcheck_rule *)check->current_step->list.n;
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002639
2640 /* bypass all comment rules */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002641 while (check->current_step->action == TCPCHK_ACT_COMMENT)
2642 check->current_step = (struct tcpcheck_rule *)check->current_step->list.n;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002643
2644 /* don't do anything until the connection is established */
2645 if (!(conn->flags & CO_FL_CONNECTED)) {
2646 /* update expire time, should be done by process_chk */
2647 /* we allow up to min(inter, timeout.connect) for a connection
2648 * to establish but only when timeout.check is set
2649 * as it may be to short for a full check otherwise
2650 */
2651 while (tick_is_expired(t->expire, now_ms)) {
2652 int t_con;
2653
2654 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2655 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2656
2657 if (s->proxy->timeout.check)
2658 t->expire = tick_first(t->expire, t_con);
2659 }
2660 return;
2661 }
2662
2663 } /* end 'connect' */
2664 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2665 /* mark the step as started */
2666 check->last_started_step = check->current_step;
2667
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002668 /* reset the read buffer */
2669 if (*check->bi->data != '\0') {
2670 *check->bi->data = '\0';
2671 check->bi->i = 0;
2672 }
2673
2674 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
2675 conn->flags |= CO_FL_ERROR;
2676 chk_report_conn_err(conn, 0, 0);
2677 goto out_end_tcpcheck;
2678 }
2679
Willy Tarreauabca5b62013-12-06 14:19:25 +01002680 if (check->current_step->string_len >= check->bo->size) {
2681 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2682 check->current_step->string_len, check->bo->size,
Simon Hormane16c1b32015-01-30 11:22:57 +09002683 tcpcheck_get_step_id(check));
Willy Tarreauabca5b62013-12-06 14:19:25 +01002684 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2685 goto out_end_tcpcheck;
2686 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002687
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002688 /* do not try to send if there is no space */
2689 if (check->current_step->string_len >= buffer_total_space(check->bo))
2690 continue;
2691
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002692 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2693 *check->bo->p = '\0'; /* to make gdb output easier to read */
2694
Willy Tarreauabca5b62013-12-06 14:19:25 +01002695 /* go to next rule and try to send */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002696 check->current_step = (struct tcpcheck_rule *)check->current_step->list.n;
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002697
2698 /* bypass all comment rules */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002699 while (check->current_step->action == TCPCHK_ACT_COMMENT)
2700 check->current_step = (struct tcpcheck_rule *)check->current_step->list.n;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002701 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002702 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002703 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002704 goto out_end_tcpcheck;
2705
Willy Tarreau310987a2014-01-22 19:46:33 +01002706 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002707 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
2708 done = 1;
2709 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2710 /* Report network errors only if we got no other data. Otherwise
2711 * we'll let the upper layers decide whether the response is OK
2712 * or not. It is very common that an RST sent by the server is
2713 * reported as an error just after the last data chunk.
2714 */
2715 chk_report_conn_err(conn, errno, 0);
2716 goto out_end_tcpcheck;
2717 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002718 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002719 else
2720 goto out_need_io;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002721 }
2722
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002723 /* mark the step as started */
2724 check->last_started_step = check->current_step;
2725
2726
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002727 /* Intermediate or complete response received.
2728 * Terminate string in check->bi->data buffer.
2729 */
2730 if (check->bi->i < check->bi->size) {
2731 check->bi->data[check->bi->i] = '\0';
2732 }
2733 else {
2734 check->bi->data[check->bi->i - 1] = '\0';
2735 done = 1; /* buffer full, don't wait for more data */
2736 }
2737
2738 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002739
2740 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002741 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002742 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002743 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002744
2745 /* empty response */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002746 step = tcpcheck_get_step_id(check);
2747 chunk_printf(&trash, "TCPCHK got an empty response at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002748 comment = tcpcheck_get_step_comment(check, step);
2749 if (comment)
2750 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002751 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2752
2753 goto out_end_tcpcheck;
2754 }
2755
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002756 if (!done && (check->current_step->string != NULL) && (check->bi->i < check->current_step->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002757 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002758
Willy Tarreaua970c282013-12-06 12:47:19 +01002759 tcpcheck_expect:
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002760 if (check->current_step->string != NULL)
2761 ret = my_memmem(contentptr, check->bi->i, check->current_step->string, check->current_step->string_len) != NULL;
2762 else if (check->current_step->expect_regex != NULL)
2763 ret = regex_exec(check->current_step->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002764
2765 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002766 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002767
2768 /* matched */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002769 step = tcpcheck_get_step_id(check);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002770 if (ret) {
2771 /* matched but we did not want to => ERROR */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002772 if (check->current_step->inverse) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002773 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002774 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002775 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002776 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002777 }
2778 else {
2779 /* we were looking for a regex */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002780 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d", step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002781 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002782 comment = tcpcheck_get_step_comment(check, step);
2783 if (comment)
2784 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002785 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2786 goto out_end_tcpcheck;
2787 }
2788 /* matched and was supposed to => OK, next step */
2789 else {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002790 /* allow next rule */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002791 check->current_step = (struct tcpcheck_rule *)check->current_step->list.n;
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002792
2793 /* bypass all comment rules */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002794 while (check->current_step->action == TCPCHK_ACT_COMMENT)
2795 check->current_step = (struct tcpcheck_rule *)check->current_step->list.n;
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002796
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002797 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002798 goto tcpcheck_expect;
2799 __conn_data_stop_recv(conn);
2800 }
2801 }
2802 else {
2803 /* not matched */
2804 /* not matched and was not supposed to => OK, next step */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002805 if (check->current_step->inverse) {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002806 /* allow next rule */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002807 check->current_step = (struct tcpcheck_rule *)check->current_step->list.n;
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002808
2809 /* bypass all comment rules */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002810 while (check->current_step->action == TCPCHK_ACT_COMMENT)
2811 check->current_step = (struct tcpcheck_rule *)check->current_step->list.n;
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002812
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002813 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002814 goto tcpcheck_expect;
2815 __conn_data_stop_recv(conn);
2816 }
2817 /* not matched but was supposed to => ERROR */
2818 else {
2819 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002820 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002821 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002822 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002823 }
2824 else {
2825 /* we were looking for a regex */
2826 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002827 step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002828 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002829 comment = tcpcheck_get_step_comment(check, step);
2830 if (comment)
2831 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002832 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2833 goto out_end_tcpcheck;
2834 }
2835 }
2836 } /* end expect */
2837 } /* end loop over double chained step list */
2838
2839 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
2840 goto out_end_tcpcheck;
2841
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002842 out_need_io:
Willy Tarreau53c5a042015-05-13 11:38:17 +02002843 /* warning, current_step may now point to the head */
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002844 if (check->bo->o)
2845 __conn_data_want_send(conn);
2846
Willy Tarreau53c5a042015-05-13 11:38:17 +02002847 if (&check->current_step->list != head &&
2848 check->current_step->action == TCPCHK_ACT_EXPECT)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002849 __conn_data_want_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002850 return;
2851
2852 out_end_tcpcheck:
2853 /* collect possible new errors */
2854 if (conn->flags & CO_FL_ERROR)
2855 chk_report_conn_err(conn, 0, 0);
2856
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002857 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002858 check->current_step = NULL;
2859
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002860 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002861 conn->flags |= CO_FL_ERROR;
2862
2863 __conn_data_stop_both(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002864 return;
2865}
2866
Simon Hormanb1900d52015-01-30 11:22:54 +09002867const char *init_check(struct check *check, int type)
2868{
2869 check->type = type;
2870
2871 /* Allocate buffer for requests... */
2872 if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
2873 return "out of memory while allocating check buffer";
2874 }
2875 check->bi->size = global.tune.chksize;
2876
2877 /* Allocate buffer for responses... */
2878 if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
2879 return "out of memory while allocating check buffer";
2880 }
2881 check->bo->size = global.tune.chksize;
2882
2883 /* Allocate buffer for partial results... */
2884 if ((check->conn = calloc(1, sizeof(struct connection))) == NULL) {
2885 return "out of memory while allocating check connection";
2886 }
2887
2888 check->conn->t.sock.fd = -1; /* no agent in progress yet */
2889
2890 return NULL;
2891}
2892
Simon Hormanbfb5d332015-01-30 11:22:55 +09002893void free_check(struct check *check)
2894{
2895 free(check->bi);
2896 free(check->bo);
2897 free(check->conn);
2898}
2899
Simon Horman0ba0e4a2015-01-30 11:23:00 +09002900void email_alert_free(struct email_alert *alert)
2901{
2902 struct tcpcheck_rule *rule, *back;
2903
2904 if (!alert)
2905 return;
2906
2907 list_for_each_entry_safe(rule, back, &alert->tcpcheck_rules, list)
2908 free(rule);
2909 free(alert);
2910}
2911
2912static struct task *process_email_alert(struct task *t)
2913{
2914 struct check *check = t->context;
2915 struct email_alertq *q;
2916
2917 q = container_of(check, typeof(*q), check);
2918
2919 if (!(check->state & CHK_ST_ENABLED)) {
2920 if (LIST_ISEMPTY(&q->email_alerts)) {
2921 /* All alerts processed, delete check */
2922 task_delete(t);
2923 task_free(t);
2924 check->task = NULL;
2925 return NULL;
2926 } else {
2927 struct email_alert *alert;
2928
2929 alert = LIST_NEXT(&q->email_alerts, typeof(alert), list);
2930 check->tcpcheck_rules = &alert->tcpcheck_rules;
2931 LIST_DEL(&alert->list);
2932
2933 check->state |= CHK_ST_ENABLED;
2934 }
2935
2936 }
2937
2938 process_chk(t);
2939
2940 if (!(check->state & CHK_ST_INPROGRESS) && check->tcpcheck_rules) {
2941 struct email_alert *alert;
2942
2943 alert = container_of(check->tcpcheck_rules, typeof(*alert), tcpcheck_rules);
2944 email_alert_free(alert);
2945
2946 check->tcpcheck_rules = NULL;
2947 check->state &= ~CHK_ST_ENABLED;
2948 }
2949 return t;
2950}
2951
2952static int init_email_alert_checks(struct server *s)
2953{
2954 int i;
2955 struct mailer *mailer;
2956 const char *err_str;
2957 struct proxy *p = s->proxy;
2958
2959 if (p->email_alert.queues)
2960 /* Already initialised, nothing to do */
2961 return 1;
2962
2963 p->email_alert.queues = calloc(p->email_alert.mailers.m->count, sizeof *p->email_alert.queues);
2964 if (!p->email_alert.queues) {
2965 err_str = "out of memory while allocating checks array";
2966 goto error_alert;
2967 }
2968
2969 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
2970 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
2971 struct email_alertq *q = &p->email_alert.queues[i];
2972 struct check *check = &q->check;
2973
2974
2975 LIST_INIT(&q->email_alerts);
2976
2977 check->inter = DEF_CHKINTR; /* XXX: Would like to Skip to the next alert, if any, ASAP.
2978 * But need enough time so that timeouts don't occur
2979 * during tcp check procssing. For now just us an arbitrary default. */
2980 check->rise = DEF_AGENT_RISETIME;
2981 check->fall = DEF_AGENT_FALLTIME;
2982 err_str = init_check(check, PR_O2_TCPCHK_CHK);
2983 if (err_str) {
2984 goto error_free;
2985 }
2986
2987 check->xprt = mailer->xprt;
2988 if (!get_host_port(&mailer->addr))
2989 /* Default to submission port */
2990 check->port = 587;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09002991 check->addr = mailer->addr;
2992 check->server = s;
2993 }
2994
2995 return 1;
2996
2997error_free:
2998 while (i-- > 1)
2999 task_free(p->email_alert.queues[i].check.task);
3000 free(p->email_alert.queues);
3001 p->email_alert.queues = NULL;
3002error_alert:
3003 Alert("Email alert [%s] could not be initialised: %s\n", p->id, err_str);
3004 return 0;
3005}
3006
3007
3008static int add_tcpcheck_expect_str(struct list *list, const char *str)
3009{
3010 struct tcpcheck_rule *tcpcheck;
3011
3012 tcpcheck = calloc(1, sizeof *tcpcheck);
3013 if (!tcpcheck)
3014 return 0;
3015
3016 tcpcheck->action = TCPCHK_ACT_EXPECT;
3017 tcpcheck->string = strdup(str);
3018 if (!tcpcheck->string) {
3019 free(tcpcheck);
3020 return 0;
3021 }
3022
3023 LIST_ADDQ(list, &tcpcheck->list);
3024 return 1;
3025}
3026
3027static int add_tcpcheck_send_strs(struct list *list, const char * const *strs)
3028{
3029 struct tcpcheck_rule *tcpcheck;
3030 int i;
3031
3032 tcpcheck = calloc(1, sizeof *tcpcheck);
3033 if (!tcpcheck)
3034 return 0;
3035
3036 tcpcheck->action = TCPCHK_ACT_SEND;
3037
3038 tcpcheck->string_len = 0;
3039 for (i = 0; strs[i]; i++)
3040 tcpcheck->string_len += strlen(strs[i]);
3041
3042 tcpcheck->string = malloc(tcpcheck->string_len + 1);
3043 if (!tcpcheck->string) {
3044 free(tcpcheck);
3045 return 0;
3046 }
3047 tcpcheck->string[0] = '\0';
3048
3049 for (i = 0; strs[i]; i++)
3050 strcat(tcpcheck->string, strs[i]);
3051
3052 LIST_ADDQ(list, &tcpcheck->list);
3053 return 1;
3054}
3055
3056static int enqueue_one_email_alert(struct email_alertq *q, const char *msg)
3057{
3058 struct email_alert *alert = NULL;
3059 struct tcpcheck_rule *tcpcheck;
3060 struct check *check = &q->check;
3061 struct proxy *p = check->server->proxy;
3062
3063 alert = calloc(1, sizeof *alert);
3064 if (!alert) {
3065 goto error;
3066 }
3067 LIST_INIT(&alert->tcpcheck_rules);
3068
3069 tcpcheck = calloc(1, sizeof *tcpcheck);
3070 if (!tcpcheck)
3071 goto error;
3072 tcpcheck->action = TCPCHK_ACT_CONNECT;
3073 LIST_ADDQ(&alert->tcpcheck_rules, &tcpcheck->list);
3074
3075 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "220 "))
3076 goto error;
3077
3078 {
3079 const char * const strs[4] = { "EHLO ", p->email_alert.myhostname, "\r\n" };
3080 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3081 goto error;
3082 }
3083
3084 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3085 goto error;
3086
3087 {
3088 const char * const strs[4] = { "MAIL FROM:<", p->email_alert.from, ">\r\n" };
3089 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3090 goto error;
3091 }
3092
3093 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3094 goto error;
3095
3096 {
3097 const char * const strs[4] = { "RCPT TO:<", p->email_alert.to, ">\r\n" };
3098 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3099 goto error;
3100 }
3101
3102 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3103 goto error;
3104
3105 {
3106 const char * const strs[2] = { "DATA\r\n" };
3107 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3108 goto error;
3109 }
3110
3111 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "354 "))
3112 goto error;
3113
3114 {
3115 struct tm tm;
3116 char datestr[48];
3117 const char * const strs[18] = {
3118 "From: ", p->email_alert.from, "\n",
3119 "To: ", p->email_alert.to, "\n",
3120 "Date: ", datestr, "\n",
3121 "Subject: [HAproxy Alert] ", msg, "\n",
3122 "\n",
3123 msg, "\n",
3124 ".\r\n",
3125 "\r\n",
3126 NULL
3127 };
3128
3129 get_localtime(date.tv_sec, &tm);
3130
3131 if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %T %z (%Z)", &tm) == 0) {
3132 goto error;
3133 }
3134
3135 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3136 goto error;
3137 }
3138
3139 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3140 goto error;
3141
3142 {
3143 const char * const strs[2] = { "QUIT\r\n" };
3144 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3145 goto error;
3146 }
3147
3148 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "221 "))
3149 goto error;
3150
3151 if (!check->task) {
3152 struct task *t;
3153
3154 if ((t = task_new()) == NULL)
3155 goto error;
3156
3157 check->task = t;
3158 t->process = process_email_alert;
3159 t->context = check;
3160
3161 /* check this in one ms */
3162 t->expire = tick_add(now_ms, MS_TO_TICKS(1));
3163 check->start = now;
3164 task_queue(t);
3165 }
3166
3167 LIST_ADDQ(&q->email_alerts, &alert->list);
3168
3169 return 1;
3170
3171error:
3172 email_alert_free(alert);
3173 return 0;
3174}
3175
3176static void enqueue_email_alert(struct proxy *p, const char *msg)
3177{
3178 int i;
3179 struct mailer *mailer;
3180
3181 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3182 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
3183 if (!enqueue_one_email_alert(&p->email_alert.queues[i], msg)) {
3184 Alert("Email alert [%s] could not be enqueued: out of memory\n", p->id);
3185 return;
3186 }
3187 }
3188
3189 return;
3190}
3191
3192/*
3193 * Send email alert if configured.
3194 */
Simon Horman64e34162015-02-06 11:11:57 +09003195void send_email_alert(struct server *s, int level, const char *format, ...)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003196{
3197 va_list argp;
3198 char buf[1024];
3199 int len;
3200 struct proxy *p = s->proxy;
3201
Simon Horman64e34162015-02-06 11:11:57 +09003202 if (!p->email_alert.mailers.m || level > p->email_alert.level ||
3203 format == NULL || !init_email_alert_checks(s))
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003204 return;
3205
3206 va_start(argp, format);
3207 len = vsnprintf(buf, sizeof(buf), format, argp);
3208 va_end(argp);
3209
3210 if (len < 0) {
3211 Alert("Email alert [%s] could format message\n", p->id);
3212 return;
3213 }
3214
3215 enqueue_email_alert(p, buf);
3216}
3217
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003218
Willy Tarreaubd741542010-03-16 18:46:54 +01003219/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003220 * Local variables:
3221 * c-indent-level: 8
3222 * c-basic-offset: 8
3223 * End:
3224 */