blob: 8d8c31c803031d98d8d618fc300715b978fd31c2 [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)) {
Willy Tarreau5581c272015-05-13 12:24:53 +02001479 struct tcpcheck_rule *r;
1480
1481 r = LIST_NEXT(check->tcpcheck_rules, struct tcpcheck_rule *, list);
1482
Simon Hormanb00d17a2014-06-13 16:18:16 +09001483 /* if first step is a 'connect', then tcpcheck_main must run it */
1484 if (r->action == TCPCHK_ACT_CONNECT) {
1485 tcpcheck_main(conn);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001486 return SF_ERR_UP;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001487 }
Willy Tarreauf3d34822014-12-08 12:11:28 +01001488 if (r->action == TCPCHK_ACT_EXPECT)
1489 quickack = 0;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001490 }
1491
Willy Tarreaue7dff022015-04-03 01:14:29 +02001492 ret = SF_ERR_INTERNAL;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001493 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01001494 ret = proto->connect(conn, check->type, quickack ? 2 : 0);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001495 conn->flags |= CO_FL_WAKE_DATA;
1496 if (s->check.send_proxy) {
1497 conn->send_proxy_ofs = 1;
1498 conn->flags |= CO_FL_SEND_PROXY;
1499 }
1500
1501 return ret;
1502}
1503
Simon Horman98637e52014-06-20 12:30:16 +09001504static struct list pid_list = LIST_HEAD_INIT(pid_list);
1505static struct pool_head *pool2_pid_list;
1506
1507void block_sigchld(void)
1508{
1509 sigset_t set;
1510 sigemptyset(&set);
1511 sigaddset(&set, SIGCHLD);
1512 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
1513}
1514
1515void unblock_sigchld(void)
1516{
1517 sigset_t set;
1518 sigemptyset(&set);
1519 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
1520}
1521
1522/* Call with SIGCHLD blocked */
1523static struct pid_list *pid_list_add(pid_t pid, struct task *t)
1524{
1525 struct pid_list *elem;
1526 struct check *check = t->context;
1527
1528 elem = pool_alloc2(pool2_pid_list);
1529 if (!elem)
1530 return NULL;
1531 elem->pid = pid;
1532 elem->t = t;
1533 elem->exited = 0;
1534 check->curpid = elem;
1535 LIST_INIT(&elem->list);
1536 LIST_ADD(&pid_list, &elem->list);
1537 return elem;
1538}
1539
1540/* Blocks blocks and then unblocks SIGCHLD */
1541static void pid_list_del(struct pid_list *elem)
1542{
1543 struct check *check;
1544
1545 if (!elem)
1546 return;
1547
1548 block_sigchld();
1549 LIST_DEL(&elem->list);
1550 unblock_sigchld();
1551 if (!elem->exited)
1552 kill(elem->pid, SIGTERM);
1553
1554 check = elem->t->context;
1555 check->curpid = NULL;
1556 pool_free2(pool2_pid_list, elem);
1557}
1558
1559/* Called from inside SIGCHLD handler, SIGCHLD is blocked */
1560static void pid_list_expire(pid_t pid, int status)
1561{
1562 struct pid_list *elem;
1563
1564 list_for_each_entry(elem, &pid_list, list) {
1565 if (elem->pid == pid) {
1566 elem->t->expire = now_ms;
1567 elem->status = status;
1568 elem->exited = 1;
Cyril Bonté9dbcfab2014-08-07 01:55:39 +02001569 task_wakeup(elem->t, TASK_WOKEN_IO);
Simon Horman98637e52014-06-20 12:30:16 +09001570 return;
1571 }
1572 }
1573}
1574
1575static void sigchld_handler(int signal)
1576{
1577 pid_t pid;
1578 int status;
1579 while ((pid = waitpid(0, &status, WNOHANG)) > 0)
1580 pid_list_expire(pid, status);
1581}
1582
1583static int init_pid_list(void) {
1584 struct sigaction action = {
1585 .sa_handler = sigchld_handler,
1586 .sa_flags = SA_NOCLDSTOP
1587 };
1588
1589 if (pool2_pid_list != NULL)
1590 /* Nothing to do */
1591 return 0;
1592
1593 if (sigaction(SIGCHLD, &action, NULL)) {
1594 Alert("Failed to set signal handler for external health checks: %s. Aborting.\n",
1595 strerror(errno));
1596 return 1;
1597 }
1598
1599 pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED);
1600 if (pool2_pid_list == NULL) {
1601 Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n",
1602 strerror(errno));
1603 return 1;
1604 }
1605
1606 return 0;
1607}
1608
Cyril Bontéac92a062014-12-27 22:28:38 +01001609/* helper macro to set an environment variable and jump to a specific label on failure. */
1610#define EXTCHK_SETENV(check, envidx, value, fail) { if (extchk_setenv(check, envidx, value)) goto fail; }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001611
1612/*
Cyril Bontéac92a062014-12-27 22:28:38 +01001613 * helper function to allocate enough memory to store an environment variable.
1614 * It will also check that the environment variable is updatable, and silently
1615 * fail if not.
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001616 */
Cyril Bontéac92a062014-12-27 22:28:38 +01001617static int extchk_setenv(struct check *check, int idx, const char *value)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001618{
1619 int len, ret;
Cyril Bontéac92a062014-12-27 22:28:38 +01001620 char *envname;
1621 int vmaxlen;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001622
Cyril Bontéac92a062014-12-27 22:28:38 +01001623 if (idx < 0 || idx >= EXTCHK_SIZE) {
1624 Alert("Illegal environment variable index %d. Aborting.\n", idx);
1625 return 1;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001626 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001627
1628 envname = extcheck_envs[idx].name;
1629 vmaxlen = extcheck_envs[idx].vmaxlen;
1630
1631 /* Check if the environment variable is already set, and silently reject
1632 * the update if this one is not updatable. */
1633 if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx]))
1634 return 0;
1635
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001636 /* Instead of sending NOT_USED, sending an empty value is preferable */
1637 if (strcmp(value, "NOT_USED") == 0) {
1638 value = "";
1639 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001640
1641 len = strlen(envname) + 1;
1642 if (vmaxlen == EXTCHK_SIZE_EVAL_INIT)
1643 len += strlen(value);
1644 else
1645 len += vmaxlen;
1646
1647 if (!check->envp[idx])
1648 check->envp[idx] = malloc(len + 1);
1649
1650 if (!check->envp[idx]) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001651 Alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname);
1652 return 1;
1653 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001654 ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001655 if (ret < 0) {
1656 Alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno));
1657 return 1;
1658 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001659 else if (ret > len) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001660 Alert("Environment variable '%s' was truncated. Aborting.\n", envname);
1661 return 1;
1662 }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001663 return 0;
1664}
Simon Horman98637e52014-06-20 12:30:16 +09001665
1666static int prepare_external_check(struct check *check)
1667{
1668 struct server *s = check->server;
1669 struct proxy *px = s->proxy;
1670 struct listener *listener = NULL, *l;
1671 int i;
Simon Horman98637e52014-06-20 12:30:16 +09001672 const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001673 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001674
1675 list_for_each_entry(l, &px->conf.listeners, by_fe)
1676 /* Use the first INET, INET6 or UNIX listener */
1677 if (l->addr.ss_family == AF_INET ||
1678 l->addr.ss_family == AF_INET6 ||
1679 l->addr.ss_family == AF_UNIX) {
1680 listener = l;
1681 break;
1682 }
1683
Simon Horman98637e52014-06-20 12:30:16 +09001684 check->curpid = NULL;
Cyril Bontéac92a062014-12-27 22:28:38 +01001685 check->envp = calloc((EXTCHK_SIZE + 1), sizeof(char *));
1686 if (!check->envp) {
1687 Alert("Failed to allocate memory for environment variables. Aborting\n");
1688 goto err;
1689 }
Simon Horman98637e52014-06-20 12:30:16 +09001690
Cyril Bontéac92a062014-12-27 22:28:38 +01001691 check->argv = calloc(6, sizeof(char *));
1692 if (!check->argv) {
1693 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001694 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001695 }
Simon Horman98637e52014-06-20 12:30:16 +09001696
1697 check->argv[0] = px->check_command;
1698
Cyril Bonté777be862014-12-02 21:21:35 +01001699 if (!listener) {
1700 check->argv[1] = strdup("NOT_USED");
1701 check->argv[2] = strdup("NOT_USED");
1702 }
1703 else if (listener->addr.ss_family == AF_INET ||
Simon Horman98637e52014-06-20 12:30:16 +09001704 listener->addr.ss_family == AF_INET6) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001705 addr_to_str(&listener->addr, buf, sizeof(buf));
1706 check->argv[1] = strdup(buf);
1707 port_to_str(&listener->addr, buf, sizeof(buf));
1708 check->argv[2] = strdup(buf);
Cyril Bonté777be862014-12-02 21:21:35 +01001709 }
1710 else if (listener->addr.ss_family == AF_UNIX) {
Simon Horman98637e52014-06-20 12:30:16 +09001711 const struct sockaddr_un *un;
1712
1713 un = (struct sockaddr_un *)&listener->addr;
1714 check->argv[1] = strdup(un->sun_path);
1715 check->argv[2] = strdup("NOT_USED");
Cyril Bonté777be862014-12-02 21:21:35 +01001716 }
1717 else {
Cyril Bontéac92a062014-12-27 22:28:38 +01001718 Alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001719 goto err;
1720 }
1721
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001722 addr_to_str(&s->addr, buf, sizeof(buf));
1723 check->argv[3] = strdup(buf);
1724 port_to_str(&s->addr, buf, sizeof(buf));
1725 check->argv[4] = strdup(buf);
Simon Horman98637e52014-06-20 12:30:16 +09001726
Cyril Bontéac92a062014-12-27 22:28:38 +01001727 for (i = 0; i < 5; i++) {
1728 if (!check->argv[i]) {
1729 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001730 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001731 }
1732 }
Simon Horman98637e52014-06-20 12:30:16 +09001733
Cyril Bontéac92a062014-12-27 22:28:38 +01001734 EXTCHK_SETENV(check, EXTCHK_PATH, path, err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001735 /* Add proxy environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001736 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err);
1737 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err);
1738 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err);
1739 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001740 /* Add server environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001741 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err);
1742 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err);
1743 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err);
1744 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err);
1745 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err);
1746 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), err);
1747
1748 /* Ensure that we don't leave any hole in check->envp */
1749 for (i = 0; i < EXTCHK_SIZE; i++)
1750 if (!check->envp[i])
1751 EXTCHK_SETENV(check, i, "", err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001752
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001753 return 1;
Simon Horman98637e52014-06-20 12:30:16 +09001754err:
1755 if (check->envp) {
Cyril Bontéac92a062014-12-27 22:28:38 +01001756 for (i = 0; i < EXTCHK_SIZE; i++)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001757 free(check->envp[i]);
Simon Horman98637e52014-06-20 12:30:16 +09001758 free(check->envp);
1759 check->envp = NULL;
1760 }
1761
1762 if (check->argv) {
1763 for (i = 1; i < 5; i++)
1764 free(check->argv[i]);
1765 free(check->argv);
1766 check->argv = NULL;
1767 }
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001768 return 0;
Simon Horman98637e52014-06-20 12:30:16 +09001769}
1770
Simon Hormanb00d17a2014-06-13 16:18:16 +09001771/*
Simon Horman98637e52014-06-20 12:30:16 +09001772 * establish a server health-check that makes use of a process.
1773 *
1774 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001775 * - SF_ERR_NONE if everything's OK
1776 * - SF_ERR_SRVTO if there are no more servers
1777 * - SF_ERR_SRVCL if the connection was refused by the server
1778 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1779 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1780 * - SF_ERR_INTERNAL for any other purely internal errors
1781 * Additionnally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Horman98637e52014-06-20 12:30:16 +09001782 *
1783 * Blocks and then unblocks SIGCHLD
1784 */
1785static int connect_proc_chk(struct task *t)
1786{
Cyril Bontéac92a062014-12-27 22:28:38 +01001787 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001788 struct check *check = t->context;
1789 struct server *s = check->server;
1790 struct proxy *px = s->proxy;
1791 int status;
1792 pid_t pid;
1793
Willy Tarreaue7dff022015-04-03 01:14:29 +02001794 status = SF_ERR_RESOURCE;
Simon Horman98637e52014-06-20 12:30:16 +09001795
1796 block_sigchld();
1797
1798 pid = fork();
1799 if (pid < 0) {
1800 Alert("Failed to fork process for external health check: %s. Aborting.\n",
1801 strerror(errno));
1802 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1803 goto out;
1804 }
1805 if (pid == 0) {
1806 /* Child */
1807 extern char **environ;
1808 environ = check->envp;
Cyril Bontéac92a062014-12-27 22:28:38 +01001809 extchk_setenv(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)));
Simon Horman98637e52014-06-20 12:30:16 +09001810 execvp(px->check_command, check->argv);
1811 Alert("Failed to exec process for external health check: %s. Aborting.\n",
1812 strerror(errno));
1813 exit(-1);
1814 }
1815
1816 /* Parent */
1817 if (check->result == CHK_RES_UNKNOWN) {
1818 if (pid_list_add(pid, t) != NULL) {
1819 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1820
1821 if (px->timeout.check && px->timeout.connect) {
1822 int t_con = tick_add(now_ms, px->timeout.connect);
1823 t->expire = tick_first(t->expire, t_con);
1824 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02001825 status = SF_ERR_NONE;
Simon Horman98637e52014-06-20 12:30:16 +09001826 goto out;
1827 }
1828 else {
1829 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1830 }
1831 kill(pid, SIGTERM); /* process creation error */
1832 }
1833 else
1834 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1835
1836out:
1837 unblock_sigchld();
1838 return status;
1839}
1840
1841/*
Simon Horman98637e52014-06-20 12:30:16 +09001842 * manages a server health-check that uses a process. Returns
Willy Tarreaubaaee002006-06-26 02:48:02 +02001843 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1844 */
Simon Horman98637e52014-06-20 12:30:16 +09001845static struct task *process_chk_proc(struct task *t)
1846{
1847 struct check *check = t->context;
1848 struct server *s = check->server;
1849 struct connection *conn = check->conn;
1850 int rv;
1851 int ret;
1852 int expired = tick_is_expired(t->expire, now_ms);
1853
1854 if (!(check->state & CHK_ST_INPROGRESS)) {
1855 /* no check currently running */
1856 if (!expired) /* woke up too early */
1857 return t;
1858
1859 /* we don't send any health-checks when the proxy is
1860 * stopped, the server should not be checked or the check
1861 * is disabled.
1862 */
1863 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
1864 s->proxy->state == PR_STSTOPPED)
1865 goto reschedule;
1866
1867 /* we'll initiate a new check */
1868 set_server_check_status(check, HCHK_STATUS_START, NULL);
1869
1870 check->state |= CHK_ST_INPROGRESS;
1871
Simon Hormandbf70192015-01-30 11:22:53 +09001872 ret = connect_proc_chk(t);
Simon Horman98637e52014-06-20 12:30:16 +09001873 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001874 case SF_ERR_UP:
Simon Horman98637e52014-06-20 12:30:16 +09001875 return t;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001876 case SF_ERR_NONE:
Simon Horman98637e52014-06-20 12:30:16 +09001877 /* we allow up to min(inter, timeout.connect) for a connection
1878 * to establish but only when timeout.check is set
1879 * as it may be to short for a full check otherwise
1880 */
1881 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1882
1883 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1884 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1885 t->expire = tick_first(t->expire, t_con);
1886 }
1887
1888 goto reschedule;
1889
Willy Tarreaue7dff022015-04-03 01:14:29 +02001890 case SF_ERR_SRVTO: /* ETIMEDOUT */
1891 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Simon Horman98637e52014-06-20 12:30:16 +09001892 conn->flags |= CO_FL_ERROR;
1893 chk_report_conn_err(conn, errno, 0);
1894 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001895 case SF_ERR_PRXCOND:
1896 case SF_ERR_RESOURCE:
1897 case SF_ERR_INTERNAL:
Simon Horman98637e52014-06-20 12:30:16 +09001898 conn->flags |= CO_FL_ERROR;
1899 chk_report_conn_err(conn, 0, 0);
1900 break;
1901 }
1902
1903 /* here, we have seen a synchronous error, no fd was allocated */
1904
1905 check->state &= ~CHK_ST_INPROGRESS;
1906 check_notify_failure(check);
1907
1908 /* we allow up to min(inter, timeout.connect) for a connection
1909 * to establish but only when timeout.check is set
1910 * as it may be to short for a full check otherwise
1911 */
1912 while (tick_is_expired(t->expire, now_ms)) {
1913 int t_con;
1914
1915 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1916 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1917
1918 if (s->proxy->timeout.check)
1919 t->expire = tick_first(t->expire, t_con);
1920 }
1921 }
1922 else {
1923 /* there was a test running.
1924 * First, let's check whether there was an uncaught error,
1925 * which can happen on connect timeout or error.
1926 */
1927 if (check->result == CHK_RES_UNKNOWN) {
1928 /* good connection is enough for pure TCP check */
1929 struct pid_list *elem = check->curpid;
1930 int status = HCHK_STATUS_UNKNOWN;
1931
1932 if (elem->exited) {
1933 status = elem->status; /* Save in case the process exits between use below */
1934 if (!WIFEXITED(status))
1935 check->code = -1;
1936 else
1937 check->code = WEXITSTATUS(status);
1938 if (!WIFEXITED(status) || WEXITSTATUS(status))
1939 status = HCHK_STATUS_PROCERR;
1940 else
1941 status = HCHK_STATUS_PROCOK;
1942 } else if (expired) {
1943 status = HCHK_STATUS_PROCTOUT;
Willy Tarreaudc3d1902014-07-08 00:56:27 +02001944 Warning("kill %d\n", (int)elem->pid);
Simon Horman98637e52014-06-20 12:30:16 +09001945 kill(elem->pid, SIGTERM);
1946 }
1947 set_server_check_status(check, status, NULL);
1948 }
1949
1950 if (check->result == CHK_RES_FAILED) {
1951 /* a failure or timeout detected */
1952 check_notify_failure(check);
1953 }
1954 else if (check->result == CHK_RES_CONDPASS) {
1955 /* check is OK but asks for stopping mode */
1956 check_notify_stopping(check);
1957 }
1958 else if (check->result == CHK_RES_PASSED) {
1959 /* a success was detected */
1960 check_notify_success(check);
1961 }
1962 check->state &= ~CHK_ST_INPROGRESS;
1963
1964 pid_list_del(check->curpid);
1965
1966 rv = 0;
1967 if (global.spread_checks > 0) {
1968 rv = srv_getinter(check) * global.spread_checks / 100;
1969 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
1970 }
1971 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
1972 }
1973
1974 reschedule:
1975 while (tick_is_expired(t->expire, now_ms))
1976 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1977 return t;
1978}
1979
1980/*
1981 * manages a server health-check that uses a connection. Returns
1982 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1983 */
1984static struct task *process_chk_conn(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001985{
Simon Horman4a741432013-02-23 15:35:38 +09001986 struct check *check = t->context;
1987 struct server *s = check->server;
1988 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001989 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001990 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001991 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001992
Willy Tarreau2c115e52013-12-11 19:41:16 +01001993 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01001994 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001995 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01001996 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001997
Simon Horman671b6f02013-11-25 10:46:39 +09001998 /* we don't send any health-checks when the proxy is
1999 * stopped, the server should not be checked or the check
2000 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002001 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01002002 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01002003 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01002004 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002005
2006 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09002007 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002008
Willy Tarreau2c115e52013-12-11 19:41:16 +01002009 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09002010 check->bi->p = check->bi->data;
2011 check->bi->i = 0;
2012 check->bo->p = check->bo->data;
2013 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002014
Simon Hormandbf70192015-01-30 11:22:53 +09002015 ret = connect_conn_chk(t);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002016 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002017 case SF_ERR_UP:
Simon Hormanb00d17a2014-06-13 16:18:16 +09002018 return t;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002019 case SF_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002020 /* we allow up to min(inter, timeout.connect) for a connection
2021 * to establish but only when timeout.check is set
2022 * as it may be to short for a full check otherwise
2023 */
Simon Horman4a741432013-02-23 15:35:38 +09002024 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002025
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002026 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2027 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2028 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002029 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01002030
2031 if (check->type)
2032 conn_data_want_recv(conn); /* prepare for reading a possible reply */
2033
Willy Tarreau5a78f362012-11-23 12:47:05 +01002034 goto reschedule;
2035
Willy Tarreaue7dff022015-04-03 01:14:29 +02002036 case SF_ERR_SRVTO: /* ETIMEDOUT */
2037 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau4bd07de2014-01-24 16:10:57 +01002038 conn->flags |= CO_FL_ERROR;
2039 chk_report_conn_err(conn, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002040 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002041 case SF_ERR_PRXCOND:
2042 case SF_ERR_RESOURCE:
2043 case SF_ERR_INTERNAL:
Willy Tarreau4bd07de2014-01-24 16:10:57 +01002044 conn->flags |= CO_FL_ERROR;
2045 chk_report_conn_err(conn, 0, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002046 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002047 }
2048
Willy Tarreau5a78f362012-11-23 12:47:05 +01002049 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01002050
Willy Tarreau2c115e52013-12-11 19:41:16 +01002051 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02002052 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002053
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002054 /* we allow up to min(inter, timeout.connect) for a connection
2055 * to establish but only when timeout.check is set
2056 * as it may be to short for a full check otherwise
2057 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002058 while (tick_is_expired(t->expire, now_ms)) {
2059 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002060
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002061 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09002062 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002063
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002064 if (s->proxy->timeout.check)
2065 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002066 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002067 }
2068 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02002069 /* there was a test running.
2070 * First, let's check whether there was an uncaught error,
2071 * which can happen on connect timeout or error.
2072 */
Simon Hormanccaabcd2014-06-20 12:29:47 +09002073 if (check->result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002074 /* good connection is enough for pure TCP check */
2075 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09002076 if (check->use_ssl)
2077 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02002078 else
Simon Horman4a741432013-02-23 15:35:38 +09002079 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002080 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002081 else if ((conn->flags & CO_FL_ERROR) || expired) {
2082 chk_report_conn_err(conn, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02002083 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002084 else
2085 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02002086 }
2087
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002088 /* check complete or aborted */
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002089 if (conn->xprt) {
2090 /* The check was aborted and the connection was not yet closed.
2091 * This can happen upon timeout, or when an external event such
2092 * as a failed response coupled with "observe layer7" caused the
2093 * server state to be suddenly changed.
2094 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01002095 conn_sock_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02002096 conn_force_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002097 }
2098
Willy Tarreauaf549582014-05-16 17:37:50 +02002099 if (check->result == CHK_RES_FAILED) {
2100 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002101 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002102 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002103 else if (check->result == CHK_RES_CONDPASS) {
2104 /* check is OK but asks for stopping mode */
2105 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002106 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002107 else if (check->result == CHK_RES_PASSED) {
2108 /* a success was detected */
2109 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002110 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01002111 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002112
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002113 rv = 0;
2114 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002115 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002116 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002117 }
Simon Horman4a741432013-02-23 15:35:38 +09002118 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002119 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002120
2121 reschedule:
2122 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002123 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002124 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01002125 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002126}
2127
Simon Horman98637e52014-06-20 12:30:16 +09002128/*
2129 * manages a server health-check. Returns
2130 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2131 */
2132static struct task *process_chk(struct task *t)
2133{
2134 struct check *check = t->context;
2135
2136 if (check->type == PR_O2_EXT_CHK)
2137 return process_chk_proc(t);
2138 return process_chk_conn(t);
2139}
2140
Simon Horman5c942422013-11-25 10:46:32 +09002141static int start_check_task(struct check *check, int mininter,
2142 int nbcheck, int srvpos)
2143{
2144 struct task *t;
2145 /* task for the check */
2146 if ((t = task_new()) == NULL) {
2147 Alert("Starting [%s:%s] check: out of memory.\n",
2148 check->server->proxy->id, check->server->id);
2149 return 0;
2150 }
2151
2152 check->task = t;
2153 t->process = process_chk;
2154 t->context = check;
2155
Willy Tarreau1746eec2014-04-25 10:46:47 +02002156 if (mininter < srv_getinter(check))
2157 mininter = srv_getinter(check);
2158
2159 if (global.max_spread_checks && mininter > global.max_spread_checks)
2160 mininter = global.max_spread_checks;
2161
Simon Horman5c942422013-11-25 10:46:32 +09002162 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002163 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002164 check->start = now;
2165 task_queue(t);
2166
2167 return 1;
2168}
2169
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002170/*
2171 * Start health-check.
2172 * Returns 0 if OK, -1 if error, and prints the error in this case.
2173 */
2174int start_checks() {
2175
2176 struct proxy *px;
2177 struct server *s;
2178 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002179 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002180
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002181 /* 1- count the checkers to run simultaneously.
2182 * We also determine the minimum interval among all of those which
2183 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2184 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002185 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002186 * too short an interval for all others.
2187 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002188 for (px = proxy; px; px = px->next) {
2189 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002190 if (s->slowstart) {
2191 if ((t = task_new()) == NULL) {
2192 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
2193 return -1;
2194 }
2195 /* We need a warmup task that will be called when the server
2196 * state switches from down to up.
2197 */
2198 s->warmup = t;
2199 t->process = server_warmup;
2200 t->context = s;
2201 t->expire = TICK_ETERNITY;
2202 }
2203
Willy Tarreaud8514a22013-12-11 21:10:14 +01002204 if (s->check.state & CHK_ST_CONFIGURED) {
2205 nbcheck++;
2206 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2207 (!mininter || mininter > srv_getinter(&s->check)))
2208 mininter = srv_getinter(&s->check);
2209 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002210
Willy Tarreaud8514a22013-12-11 21:10:14 +01002211 if (s->agent.state & CHK_ST_CONFIGURED) {
2212 nbcheck++;
2213 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2214 (!mininter || mininter > srv_getinter(&s->agent)))
2215 mininter = srv_getinter(&s->agent);
2216 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002217 }
2218 }
2219
Simon Horman4a741432013-02-23 15:35:38 +09002220 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002221 return 0;
2222
2223 srand((unsigned)time(NULL));
2224
2225 /*
2226 * 2- start them as far as possible from each others. For this, we will
2227 * start them after their interval set to the min interval divided by
2228 * the number of servers, weighted by the server's position in the list.
2229 */
2230 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002231 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2232 if (init_pid_list()) {
2233 Alert("Starting [%s] check: out of memory.\n", px->id);
2234 return -1;
2235 }
2236 }
2237
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002238 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002239 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002240 if (s->check.state & CHK_ST_CONFIGURED) {
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002241 if (s->check.type == PR_O2_EXT_CHK) {
2242 if (!prepare_external_check(&s->check))
2243 return -1;
2244 }
Simon Hormand60d6912013-11-25 10:46:36 +09002245 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
2246 return -1;
2247 srvpos++;
2248 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002249
Simon Hormand60d6912013-11-25 10:46:36 +09002250 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002251 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002252 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
2253 return -1;
2254 }
2255 srvpos++;
2256 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002257 }
2258 }
2259 return 0;
2260}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002261
2262/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002263 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002264 * The buffer MUST be terminated by a null byte before calling this function.
2265 * Sets server status appropriately. The caller is responsible for ensuring
2266 * that the buffer contains at least 13 characters. If <done> is zero, we may
2267 * return 0 to indicate that data is required to decide of a match.
2268 */
2269static int httpchk_expect(struct server *s, int done)
2270{
2271 static char status_msg[] = "HTTP status check returned code <000>";
2272 char status_code[] = "000";
2273 char *contentptr;
2274 int crlf;
2275 int ret;
2276
2277 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2278 case PR_O2_EXP_STS:
2279 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002280 memcpy(status_code, s->check.bi->data + 9, 3);
2281 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002282
2283 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2284 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2285 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002286 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002287
2288 /* we necessarily have the response, so there are no partial failures */
2289 if (s->proxy->options2 & PR_O2_EXP_INV)
2290 ret = !ret;
2291
Simon Horman4a741432013-02-23 15:35:38 +09002292 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002293 break;
2294
2295 case PR_O2_EXP_STR:
2296 case PR_O2_EXP_RSTR:
2297 /* very simple response parser: ignore CR and only count consecutive LFs,
2298 * stop with contentptr pointing to first char after the double CRLF or
2299 * to '\0' if crlf < 2.
2300 */
2301 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002302 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002303 if (crlf >= 2)
2304 break;
2305 if (*contentptr == '\r')
2306 continue;
2307 else if (*contentptr == '\n')
2308 crlf++;
2309 else
2310 crlf = 0;
2311 }
2312
2313 /* Check that response contains a body... */
2314 if (crlf < 2) {
2315 if (!done)
2316 return 0;
2317
Simon Horman4a741432013-02-23 15:35:38 +09002318 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002319 "HTTP content check could not find a response body");
2320 return 1;
2321 }
2322
2323 /* Check that response body is not empty... */
2324 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002325 if (!done)
2326 return 0;
2327
Simon Horman4a741432013-02-23 15:35:38 +09002328 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002329 "HTTP content check found empty response body");
2330 return 1;
2331 }
2332
2333 /* Check the response content against the supplied string
2334 * or regex... */
2335 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2336 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2337 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002338 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002339
2340 /* if we don't match, we may need to wait more */
2341 if (!ret && !done)
2342 return 0;
2343
2344 if (ret) {
2345 /* content matched */
2346 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002347 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002348 "HTTP check matched unwanted content");
2349 else
Simon Horman4a741432013-02-23 15:35:38 +09002350 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002351 "HTTP content check matched");
2352 }
2353 else {
2354 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002355 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002356 "HTTP check did not match unwanted content");
2357 else
Simon Horman4a741432013-02-23 15:35:38 +09002358 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002359 "HTTP content check did not match");
2360 }
2361 break;
2362 }
2363 return 1;
2364}
2365
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002366/*
2367 * return the id of a step in a send/expect session
2368 */
Simon Hormane16c1b32015-01-30 11:22:57 +09002369static int tcpcheck_get_step_id(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002370{
2371 struct tcpcheck_rule *cur = NULL, *next = NULL;
2372 int i = 0;
2373
Willy Tarreau213c6782014-10-02 14:51:02 +02002374 /* not even started anything yet => step 0 = initial connect */
Baptiste Assmannf95bc8e2015-04-25 16:03:06 +02002375 if (!check->current_step)
Willy Tarreau213c6782014-10-02 14:51:02 +02002376 return 0;
2377
Simon Hormane16c1b32015-01-30 11:22:57 +09002378 cur = check->last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002379
2380 /* no step => first step */
2381 if (cur == NULL)
2382 return 1;
2383
2384 /* increment i until current step */
Simon Hormane16c1b32015-01-30 11:22:57 +09002385 list_for_each_entry(next, check->tcpcheck_rules, list) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002386 if (next->list.p == &cur->list)
2387 break;
2388 ++i;
2389 }
2390
2391 return i;
2392}
2393
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002394/*
2395 * return the latest known comment before (including) the given stepid
2396 * returns NULL if no comment found
2397 */
2398static char * tcpcheck_get_step_comment(struct check *check, int stepid)
2399{
2400 struct tcpcheck_rule *cur = NULL;
2401 char *ret = NULL;
2402 int i = 0;
2403
2404 /* not even started anything yet, return latest comment found before any action */
2405 if (!check->current_step) {
2406 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2407 if (cur->action == TCPCHK_ACT_COMMENT)
2408 ret = cur->comment;
2409 else
2410 goto return_comment;
2411 }
2412 }
2413
2414 i = 1;
2415 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2416 if (cur->comment)
2417 ret = cur->comment;
2418
2419 if (i >= stepid)
2420 goto return_comment;
2421
2422 ++i;
2423 }
2424
2425 return_comment:
2426 return ret;
2427}
2428
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002429static void tcpcheck_main(struct connection *conn)
2430{
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002431 char *contentptr, *comment;
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002432 struct tcpcheck_rule *next;
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002433 int done = 0, ret = 0, step = 0;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002434 struct check *check = conn->owner;
2435 struct server *s = check->server;
2436 struct task *t = check->task;
Simon Hormane16c1b32015-01-30 11:22:57 +09002437 struct list *head = check->tcpcheck_rules;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002438
Willy Tarreauef953952014-10-02 14:30:14 +02002439 /* here, we know that the check is complete or that it failed */
2440 if (check->result != CHK_RES_UNKNOWN)
2441 goto out_end_tcpcheck;
2442
2443 /* We have 4 possibilities here :
2444 * 1. we've not yet attempted step 1, and step 1 is a connect, so no
2445 * connection attempt was made yet ;
2446 * 2. we've not yet attempted step 1, and step 1 is a not connect or
2447 * does not exist (no rule), so a connection attempt was made
2448 * before coming here.
2449 * 3. we're coming back after having started with step 1, so we may
2450 * be waiting for a connection attempt to complete.
2451 * 4. the connection + handshake are complete
2452 *
2453 * #2 and #3 are quite similar, we want both the connection and the
2454 * handshake to complete before going any further. Thus we must always
2455 * wait for a connection to complete unless we're before and existing
2456 * step 1.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002457 */
Willy Tarreau449f9522015-05-13 15:39:48 +02002458
2459 /* find first rule and skip comments */
2460 next = LIST_NEXT(head, struct tcpcheck_rule *, list);
2461 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
2462 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
2463
Willy Tarreauef953952014-10-02 14:30:14 +02002464 if ((!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE)) &&
Willy Tarreau449f9522015-05-13 15:39:48 +02002465 (check->current_step || &next->list == head)) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002466 /* we allow up to min(inter, timeout.connect) for a connection
2467 * to establish but only when timeout.check is set
2468 * as it may be to short for a full check otherwise
2469 */
2470 while (tick_is_expired(t->expire, now_ms)) {
2471 int t_con;
2472
2473 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2474 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2475
2476 if (s->proxy->timeout.check)
2477 t->expire = tick_first(t->expire, t_con);
2478 }
2479 return;
2480 }
2481
Willy Tarreauef953952014-10-02 14:30:14 +02002482 /* special case: option tcp-check with no rule, a connect is enough */
Willy Tarreau449f9522015-05-13 15:39:48 +02002483 if (&next->list == head) {
Willy Tarreauef953952014-10-02 14:30:14 +02002484 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002485 goto out_end_tcpcheck;
Willy Tarreauef953952014-10-02 14:30:14 +02002486 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002487
Willy Tarreau213c6782014-10-02 14:51:02 +02002488 /* no step means first step initialisation */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002489 if (check->current_step == NULL) {
Willy Tarreau213c6782014-10-02 14:51:02 +02002490 check->last_started_step = NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002491 check->bo->p = check->bo->data;
2492 check->bo->o = 0;
2493 check->bi->p = check->bi->data;
2494 check->bi->i = 0;
Willy Tarreau449f9522015-05-13 15:39:48 +02002495 check->current_step = next;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002496 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2497 if (s->proxy->timeout.check)
2498 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2499 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002500
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002501 /* It's only the rules which will enable send/recv */
2502 __conn_data_stop_both(conn);
2503
Willy Tarreauabca5b62013-12-06 14:19:25 +01002504 while (1) {
Willy Tarreau263013d2015-05-13 11:59:14 +02002505 /* We have to try to flush the output buffer before reading, at
2506 * the end, or if we're about to send a string that does not fit
2507 * in the remaining space. That explains why we break out of the
2508 * loop after this control.
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002509 */
2510 if (check->bo->o &&
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002511 (&check->current_step->list == head ||
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002512 check->current_step->action != TCPCHK_ACT_SEND ||
2513 check->current_step->string_len >= buffer_total_space(check->bo))) {
2514
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002515 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002516 if (conn->flags & CO_FL_ERROR) {
2517 chk_report_conn_err(conn, errno, 0);
2518 __conn_data_stop_both(conn);
2519 goto out_end_tcpcheck;
2520 }
Willy Tarreau263013d2015-05-13 11:59:14 +02002521 break;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002522 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002523 }
2524
Willy Tarreau263013d2015-05-13 11:59:14 +02002525 if (&check->current_step->list == head)
Willy Tarreauabca5b62013-12-06 14:19:25 +01002526 break;
2527
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002528 /* have 'next' point to the next rule or NULL if we're on the
2529 * last one, connect() needs this.
2530 */
Willy Tarreau5581c272015-05-13 12:24:53 +02002531 next = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002532
2533 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002534 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002535 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002536
2537 /* NULL if we're on the last rule */
Willy Tarreauf3d34822014-12-08 12:11:28 +01002538 if (&next->list == head)
2539 next = NULL;
2540
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002541 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2542 struct protocol *proto;
2543 struct xprt_ops *xprt;
2544
2545 /* mark the step as started */
2546 check->last_started_step = check->current_step;
2547 /* first, shut existing connection */
2548 conn_force_close(conn);
2549
2550 /* prepare new connection */
2551 /* initialization */
2552 conn_init(conn);
2553 conn_attach(conn, check, &check_conn_cb);
2554 conn->target = &s->obj_type;
2555
2556 /* no client address */
2557 clear_addr(&conn->addr.from);
2558
Simon Horman41f58762015-01-30 11:22:56 +09002559 if (is_addr(&check->addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002560 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09002561 conn->addr.to = check->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002562 }
2563 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002564 /* we'll connect to the addr on the server */
2565 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002566 }
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01002567 proto = protocol_by_family(conn->addr.to.ss_family);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002568
2569 /* port */
2570 if (check->current_step->port)
2571 set_host_port(&conn->addr.to, check->current_step->port);
2572 else if (check->port)
2573 set_host_port(&conn->addr.to, check->port);
2574
2575#ifdef USE_OPENSSL
2576 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
2577 xprt = &ssl_sock;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002578 }
2579 else {
2580 xprt = &raw_sock;
2581 }
2582#else /* USE_OPENSSL */
2583 xprt = &raw_sock;
2584#endif /* USE_OPENSSL */
2585 conn_prepare(conn, proto, xprt);
2586
Willy Tarreaue7dff022015-04-03 01:14:29 +02002587 ret = SF_ERR_INTERNAL;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002588 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01002589 ret = proto->connect(conn,
2590 1 /* I/O polling is always needed */,
2591 (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002592 conn->flags |= CO_FL_WAKE_DATA;
2593 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2594 conn->send_proxy_ofs = 1;
2595 conn->flags |= CO_FL_SEND_PROXY;
2596 }
2597
2598 /* It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02002599 * - SF_ERR_NONE if everything's OK
2600 * - SF_ERR_SRVTO if there are no more servers
2601 * - SF_ERR_SRVCL if the connection was refused by the server
2602 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2603 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2604 * - SF_ERR_INTERNAL for any other purely internal errors
2605 * Additionnally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002606 * Note that we try to prevent the network stack from sending the ACK during the
2607 * connect() when a pure TCP check is used (without PROXY protocol).
2608 */
2609 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002610 case SF_ERR_NONE:
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002611 /* we allow up to min(inter, timeout.connect) for a connection
2612 * to establish but only when timeout.check is set
2613 * as it may be to short for a full check otherwise
2614 */
2615 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2616
2617 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2618 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2619 t->expire = tick_first(t->expire, t_con);
2620 }
2621 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002622 case SF_ERR_SRVTO: /* ETIMEDOUT */
2623 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002624 step = tcpcheck_get_step_id(check);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002625 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002626 step, strerror(errno));
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002627 comment = tcpcheck_get_step_comment(check, step);
2628 if (comment)
2629 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002630 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2631 goto out_end_tcpcheck;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002632 case SF_ERR_PRXCOND:
2633 case SF_ERR_RESOURCE:
2634 case SF_ERR_INTERNAL:
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002635 step = tcpcheck_get_step_id(check);
2636 chunk_printf(&trash, "TCPCHK error establishing connection at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002637 comment = tcpcheck_get_step_comment(check, step);
2638 if (comment)
2639 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002640 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2641 goto out_end_tcpcheck;
2642 }
2643
2644 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002645 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002646
2647 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002648 while (&check->current_step->list != head &&
2649 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002650 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002651
Willy Tarreauf2c87352015-05-13 12:08:21 +02002652 if (&check->current_step->list == head)
2653 break;
2654
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002655 /* don't do anything until the connection is established */
2656 if (!(conn->flags & CO_FL_CONNECTED)) {
2657 /* update expire time, should be done by process_chk */
2658 /* we allow up to min(inter, timeout.connect) for a connection
2659 * to establish but only when timeout.check is set
2660 * as it may be to short for a full check otherwise
2661 */
2662 while (tick_is_expired(t->expire, now_ms)) {
2663 int t_con;
2664
2665 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2666 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2667
2668 if (s->proxy->timeout.check)
2669 t->expire = tick_first(t->expire, t_con);
2670 }
2671 return;
2672 }
2673
2674 } /* end 'connect' */
2675 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2676 /* mark the step as started */
2677 check->last_started_step = check->current_step;
2678
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002679 /* reset the read buffer */
2680 if (*check->bi->data != '\0') {
2681 *check->bi->data = '\0';
2682 check->bi->i = 0;
2683 }
2684
2685 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
2686 conn->flags |= CO_FL_ERROR;
2687 chk_report_conn_err(conn, 0, 0);
2688 goto out_end_tcpcheck;
2689 }
2690
Willy Tarreauabca5b62013-12-06 14:19:25 +01002691 if (check->current_step->string_len >= check->bo->size) {
2692 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2693 check->current_step->string_len, check->bo->size,
Simon Hormane16c1b32015-01-30 11:22:57 +09002694 tcpcheck_get_step_id(check));
Willy Tarreauabca5b62013-12-06 14:19:25 +01002695 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2696 goto out_end_tcpcheck;
2697 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002698
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002699 /* do not try to send if there is no space */
2700 if (check->current_step->string_len >= buffer_total_space(check->bo))
2701 continue;
2702
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002703 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2704 *check->bo->p = '\0'; /* to make gdb output easier to read */
2705
Willy Tarreauabca5b62013-12-06 14:19:25 +01002706 /* go to next rule and try to send */
Willy Tarreau5581c272015-05-13 12:24:53 +02002707 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002708
2709 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002710 while (&check->current_step->list != head &&
2711 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002712 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Willy Tarreauf2c87352015-05-13 12:08:21 +02002713
2714 if (&check->current_step->list == head)
2715 break;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002716 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002717 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002718 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002719 goto out_end_tcpcheck;
2720
Willy Tarreau310987a2014-01-22 19:46:33 +01002721 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002722 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
2723 done = 1;
2724 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2725 /* Report network errors only if we got no other data. Otherwise
2726 * we'll let the upper layers decide whether the response is OK
2727 * or not. It is very common that an RST sent by the server is
2728 * reported as an error just after the last data chunk.
2729 */
2730 chk_report_conn_err(conn, errno, 0);
2731 goto out_end_tcpcheck;
2732 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002733 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002734 else
Willy Tarreau263013d2015-05-13 11:59:14 +02002735 break;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002736 }
2737
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002738 /* mark the step as started */
2739 check->last_started_step = check->current_step;
2740
2741
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002742 /* Intermediate or complete response received.
2743 * Terminate string in check->bi->data buffer.
2744 */
2745 if (check->bi->i < check->bi->size) {
2746 check->bi->data[check->bi->i] = '\0';
2747 }
2748 else {
2749 check->bi->data[check->bi->i - 1] = '\0';
2750 done = 1; /* buffer full, don't wait for more data */
2751 }
2752
2753 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002754
2755 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002756 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002757 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002758 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002759
2760 /* empty response */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002761 step = tcpcheck_get_step_id(check);
2762 chunk_printf(&trash, "TCPCHK got an empty response at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002763 comment = tcpcheck_get_step_comment(check, step);
2764 if (comment)
2765 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002766 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2767
2768 goto out_end_tcpcheck;
2769 }
2770
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002771 if (!done && (check->current_step->string != NULL) && (check->bi->i < check->current_step->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002772 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002773
Willy Tarreaua970c282013-12-06 12:47:19 +01002774 tcpcheck_expect:
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002775 if (check->current_step->string != NULL)
2776 ret = my_memmem(contentptr, check->bi->i, check->current_step->string, check->current_step->string_len) != NULL;
2777 else if (check->current_step->expect_regex != NULL)
2778 ret = regex_exec(check->current_step->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002779
2780 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002781 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002782
2783 /* matched */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002784 step = tcpcheck_get_step_id(check);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002785 if (ret) {
2786 /* matched but we did not want to => ERROR */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002787 if (check->current_step->inverse) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002788 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002789 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002790 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002791 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002792 }
2793 else {
2794 /* we were looking for a regex */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002795 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d", step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002796 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002797 comment = tcpcheck_get_step_comment(check, step);
2798 if (comment)
2799 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002800 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2801 goto out_end_tcpcheck;
2802 }
2803 /* matched and was supposed to => OK, next step */
2804 else {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002805 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002806 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002807
2808 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002809 while (&check->current_step->list != head &&
2810 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002811 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002812
Willy Tarreauf2c87352015-05-13 12:08:21 +02002813 if (&check->current_step->list == head)
2814 break;
2815
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002816 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002817 goto tcpcheck_expect;
2818 __conn_data_stop_recv(conn);
2819 }
2820 }
2821 else {
2822 /* not matched */
2823 /* not matched and was not supposed to => OK, next step */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002824 if (check->current_step->inverse) {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002825 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002826 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002827
2828 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002829 while (&check->current_step->list != head &&
2830 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002831 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002832
Willy Tarreauf2c87352015-05-13 12:08:21 +02002833 if (&check->current_step->list == head)
2834 break;
2835
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002836 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002837 goto tcpcheck_expect;
2838 __conn_data_stop_recv(conn);
2839 }
2840 /* not matched but was supposed to => ERROR */
2841 else {
2842 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002843 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002844 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002845 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002846 }
2847 else {
2848 /* we were looking for a regex */
2849 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002850 step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002851 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002852 comment = tcpcheck_get_step_comment(check, step);
2853 if (comment)
2854 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002855 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2856 goto out_end_tcpcheck;
2857 }
2858 }
2859 } /* end expect */
2860 } /* end loop over double chained step list */
2861
Willy Tarreau263013d2015-05-13 11:59:14 +02002862 /* We're waiting for some I/O to complete, we've reached the end of the
2863 * rules, or both. Do what we have to do, otherwise we're done.
2864 */
2865 if (&check->current_step->list == head && !check->bo->o) {
2866 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
2867 goto out_end_tcpcheck;
2868 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002869
Willy Tarreau53c5a042015-05-13 11:38:17 +02002870 /* warning, current_step may now point to the head */
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002871 if (check->bo->o)
2872 __conn_data_want_send(conn);
2873
Willy Tarreau53c5a042015-05-13 11:38:17 +02002874 if (&check->current_step->list != head &&
2875 check->current_step->action == TCPCHK_ACT_EXPECT)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002876 __conn_data_want_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002877 return;
2878
2879 out_end_tcpcheck:
2880 /* collect possible new errors */
2881 if (conn->flags & CO_FL_ERROR)
2882 chk_report_conn_err(conn, 0, 0);
2883
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002884 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002885 check->current_step = NULL;
2886
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002887 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002888 conn->flags |= CO_FL_ERROR;
2889
2890 __conn_data_stop_both(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002891 return;
2892}
2893
Simon Hormanb1900d52015-01-30 11:22:54 +09002894const char *init_check(struct check *check, int type)
2895{
2896 check->type = type;
2897
2898 /* Allocate buffer for requests... */
2899 if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
2900 return "out of memory while allocating check buffer";
2901 }
2902 check->bi->size = global.tune.chksize;
2903
2904 /* Allocate buffer for responses... */
2905 if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
2906 return "out of memory while allocating check buffer";
2907 }
2908 check->bo->size = global.tune.chksize;
2909
2910 /* Allocate buffer for partial results... */
2911 if ((check->conn = calloc(1, sizeof(struct connection))) == NULL) {
2912 return "out of memory while allocating check connection";
2913 }
2914
2915 check->conn->t.sock.fd = -1; /* no agent in progress yet */
2916
2917 return NULL;
2918}
2919
Simon Hormanbfb5d332015-01-30 11:22:55 +09002920void free_check(struct check *check)
2921{
2922 free(check->bi);
2923 free(check->bo);
2924 free(check->conn);
2925}
2926
Simon Horman0ba0e4a2015-01-30 11:23:00 +09002927void email_alert_free(struct email_alert *alert)
2928{
2929 struct tcpcheck_rule *rule, *back;
2930
2931 if (!alert)
2932 return;
2933
2934 list_for_each_entry_safe(rule, back, &alert->tcpcheck_rules, list)
2935 free(rule);
2936 free(alert);
2937}
2938
2939static struct task *process_email_alert(struct task *t)
2940{
2941 struct check *check = t->context;
2942 struct email_alertq *q;
2943
2944 q = container_of(check, typeof(*q), check);
2945
2946 if (!(check->state & CHK_ST_ENABLED)) {
2947 if (LIST_ISEMPTY(&q->email_alerts)) {
2948 /* All alerts processed, delete check */
2949 task_delete(t);
2950 task_free(t);
2951 check->task = NULL;
2952 return NULL;
2953 } else {
2954 struct email_alert *alert;
2955
2956 alert = LIST_NEXT(&q->email_alerts, typeof(alert), list);
2957 check->tcpcheck_rules = &alert->tcpcheck_rules;
2958 LIST_DEL(&alert->list);
2959
2960 check->state |= CHK_ST_ENABLED;
2961 }
2962
2963 }
2964
2965 process_chk(t);
2966
2967 if (!(check->state & CHK_ST_INPROGRESS) && check->tcpcheck_rules) {
2968 struct email_alert *alert;
2969
2970 alert = container_of(check->tcpcheck_rules, typeof(*alert), tcpcheck_rules);
2971 email_alert_free(alert);
2972
2973 check->tcpcheck_rules = NULL;
2974 check->state &= ~CHK_ST_ENABLED;
2975 }
2976 return t;
2977}
2978
2979static int init_email_alert_checks(struct server *s)
2980{
2981 int i;
2982 struct mailer *mailer;
2983 const char *err_str;
2984 struct proxy *p = s->proxy;
2985
2986 if (p->email_alert.queues)
2987 /* Already initialised, nothing to do */
2988 return 1;
2989
2990 p->email_alert.queues = calloc(p->email_alert.mailers.m->count, sizeof *p->email_alert.queues);
2991 if (!p->email_alert.queues) {
2992 err_str = "out of memory while allocating checks array";
2993 goto error_alert;
2994 }
2995
2996 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
2997 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
2998 struct email_alertq *q = &p->email_alert.queues[i];
2999 struct check *check = &q->check;
3000
3001
3002 LIST_INIT(&q->email_alerts);
3003
3004 check->inter = DEF_CHKINTR; /* XXX: Would like to Skip to the next alert, if any, ASAP.
3005 * But need enough time so that timeouts don't occur
3006 * during tcp check procssing. For now just us an arbitrary default. */
3007 check->rise = DEF_AGENT_RISETIME;
3008 check->fall = DEF_AGENT_FALLTIME;
3009 err_str = init_check(check, PR_O2_TCPCHK_CHK);
3010 if (err_str) {
3011 goto error_free;
3012 }
3013
3014 check->xprt = mailer->xprt;
3015 if (!get_host_port(&mailer->addr))
3016 /* Default to submission port */
3017 check->port = 587;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003018 check->addr = mailer->addr;
3019 check->server = s;
3020 }
3021
3022 return 1;
3023
3024error_free:
3025 while (i-- > 1)
3026 task_free(p->email_alert.queues[i].check.task);
3027 free(p->email_alert.queues);
3028 p->email_alert.queues = NULL;
3029error_alert:
3030 Alert("Email alert [%s] could not be initialised: %s\n", p->id, err_str);
3031 return 0;
3032}
3033
3034
3035static int add_tcpcheck_expect_str(struct list *list, const char *str)
3036{
3037 struct tcpcheck_rule *tcpcheck;
3038
3039 tcpcheck = calloc(1, sizeof *tcpcheck);
3040 if (!tcpcheck)
3041 return 0;
3042
3043 tcpcheck->action = TCPCHK_ACT_EXPECT;
3044 tcpcheck->string = strdup(str);
3045 if (!tcpcheck->string) {
3046 free(tcpcheck);
3047 return 0;
3048 }
3049
3050 LIST_ADDQ(list, &tcpcheck->list);
3051 return 1;
3052}
3053
3054static int add_tcpcheck_send_strs(struct list *list, const char * const *strs)
3055{
3056 struct tcpcheck_rule *tcpcheck;
3057 int i;
3058
3059 tcpcheck = calloc(1, sizeof *tcpcheck);
3060 if (!tcpcheck)
3061 return 0;
3062
3063 tcpcheck->action = TCPCHK_ACT_SEND;
3064
3065 tcpcheck->string_len = 0;
3066 for (i = 0; strs[i]; i++)
3067 tcpcheck->string_len += strlen(strs[i]);
3068
3069 tcpcheck->string = malloc(tcpcheck->string_len + 1);
3070 if (!tcpcheck->string) {
3071 free(tcpcheck);
3072 return 0;
3073 }
3074 tcpcheck->string[0] = '\0';
3075
3076 for (i = 0; strs[i]; i++)
3077 strcat(tcpcheck->string, strs[i]);
3078
3079 LIST_ADDQ(list, &tcpcheck->list);
3080 return 1;
3081}
3082
3083static int enqueue_one_email_alert(struct email_alertq *q, const char *msg)
3084{
3085 struct email_alert *alert = NULL;
3086 struct tcpcheck_rule *tcpcheck;
3087 struct check *check = &q->check;
3088 struct proxy *p = check->server->proxy;
3089
3090 alert = calloc(1, sizeof *alert);
3091 if (!alert) {
3092 goto error;
3093 }
3094 LIST_INIT(&alert->tcpcheck_rules);
3095
3096 tcpcheck = calloc(1, sizeof *tcpcheck);
3097 if (!tcpcheck)
3098 goto error;
3099 tcpcheck->action = TCPCHK_ACT_CONNECT;
3100 LIST_ADDQ(&alert->tcpcheck_rules, &tcpcheck->list);
3101
3102 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "220 "))
3103 goto error;
3104
3105 {
3106 const char * const strs[4] = { "EHLO ", p->email_alert.myhostname, "\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, "250 "))
3112 goto error;
3113
3114 {
3115 const char * const strs[4] = { "MAIL FROM:<", p->email_alert.from, ">\r\n" };
3116 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3117 goto error;
3118 }
3119
3120 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3121 goto error;
3122
3123 {
3124 const char * const strs[4] = { "RCPT TO:<", p->email_alert.to, ">\r\n" };
3125 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3126 goto error;
3127 }
3128
3129 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3130 goto error;
3131
3132 {
3133 const char * const strs[2] = { "DATA\r\n" };
3134 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3135 goto error;
3136 }
3137
3138 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "354 "))
3139 goto error;
3140
3141 {
3142 struct tm tm;
3143 char datestr[48];
3144 const char * const strs[18] = {
3145 "From: ", p->email_alert.from, "\n",
3146 "To: ", p->email_alert.to, "\n",
3147 "Date: ", datestr, "\n",
3148 "Subject: [HAproxy Alert] ", msg, "\n",
3149 "\n",
3150 msg, "\n",
3151 ".\r\n",
3152 "\r\n",
3153 NULL
3154 };
3155
3156 get_localtime(date.tv_sec, &tm);
3157
3158 if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %T %z (%Z)", &tm) == 0) {
3159 goto error;
3160 }
3161
3162 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3163 goto error;
3164 }
3165
3166 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3167 goto error;
3168
3169 {
3170 const char * const strs[2] = { "QUIT\r\n" };
3171 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3172 goto error;
3173 }
3174
3175 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "221 "))
3176 goto error;
3177
3178 if (!check->task) {
3179 struct task *t;
3180
3181 if ((t = task_new()) == NULL)
3182 goto error;
3183
3184 check->task = t;
3185 t->process = process_email_alert;
3186 t->context = check;
3187
3188 /* check this in one ms */
3189 t->expire = tick_add(now_ms, MS_TO_TICKS(1));
3190 check->start = now;
3191 task_queue(t);
3192 }
3193
3194 LIST_ADDQ(&q->email_alerts, &alert->list);
3195
3196 return 1;
3197
3198error:
3199 email_alert_free(alert);
3200 return 0;
3201}
3202
3203static void enqueue_email_alert(struct proxy *p, const char *msg)
3204{
3205 int i;
3206 struct mailer *mailer;
3207
3208 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3209 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
3210 if (!enqueue_one_email_alert(&p->email_alert.queues[i], msg)) {
3211 Alert("Email alert [%s] could not be enqueued: out of memory\n", p->id);
3212 return;
3213 }
3214 }
3215
3216 return;
3217}
3218
3219/*
3220 * Send email alert if configured.
3221 */
Simon Horman64e34162015-02-06 11:11:57 +09003222void send_email_alert(struct server *s, int level, const char *format, ...)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003223{
3224 va_list argp;
3225 char buf[1024];
3226 int len;
3227 struct proxy *p = s->proxy;
3228
Simon Horman64e34162015-02-06 11:11:57 +09003229 if (!p->email_alert.mailers.m || level > p->email_alert.level ||
3230 format == NULL || !init_email_alert_checks(s))
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003231 return;
3232
3233 va_start(argp, format);
3234 len = vsnprintf(buf, sizeof(buf), format, argp);
3235 va_end(argp);
3236
3237 if (len < 0) {
3238 Alert("Email alert [%s] could format message\n", p->id);
3239 return;
3240 }
3241
3242 enqueue_email_alert(p, buf);
3243}
3244
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003245
Willy Tarreaubd741542010-03-16 18:46:54 +01003246/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003247 * Local variables:
3248 * c-indent-level: 8
3249 * c-basic-offset: 8
3250 * End:
3251 */