blob: 413365b2429fe00380a16126b4724d4eebc6e96a [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>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020041#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010042#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
44#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020045#include <proto/checks.h>
William Lallemand9ed62032016-11-21 17:49:11 +010046#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/fd.h>
48#include <proto/log.h>
49#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020050#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010051#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010052#include <proto/proto_tcp.h>
Baptiste Assmann69e273f2013-12-11 00:52:19 +010053#include <proto/protocol.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010054#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/server.h>
Willy Tarreau48d6bf22016-06-21 16:27:34 +020056#include <proto/signal.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010057#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020059#include <proto/log.h>
60#include <proto/dns.h>
61#include <proto/proto_udp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020062
Olivier Houchard9130a962017-10-17 17:33:43 +020063#ifdef USE_OPENSSL
64#include <proto/ssl_sock.h>
65#endif /* USE_OPENSSL */
66
Willy Tarreaubd741542010-03-16 18:46:54 +010067static int httpchk_expect(struct server *s, int done);
Simon Hormane16c1b32015-01-30 11:22:57 +090068static int tcpcheck_get_step_id(struct check *);
Baptiste Assmann22b09d22015-05-01 08:03:04 +020069static char * tcpcheck_get_step_comment(struct check *, int);
Willy Tarreau6bdcab02017-10-04 18:41:00 +020070static int tcpcheck_main(struct check *);
Willy Tarreaubd741542010-03-16 18:46:54 +010071
Simon Horman63a4a822012-03-19 07:24:41 +090072static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010073 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
74 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020075 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020076
Willy Tarreau23964182014-05-20 20:56:30 +020077 /* Below we have finished checks */
78 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010079 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010080
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010081 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020082
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010083 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
84 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
85 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020086
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010087 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
88 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
89 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020090
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010091 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
92 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020093
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020094 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020095
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010096 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
97 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
98 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +090099
100 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
101 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +0200102 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200103};
104
Cyril Bontéac92a062014-12-27 22:28:38 +0100105const struct extcheck_env extcheck_envs[EXTCHK_SIZE] = {
106 [EXTCHK_PATH] = { "PATH", EXTCHK_SIZE_EVAL_INIT },
107 [EXTCHK_HAPROXY_PROXY_NAME] = { "HAPROXY_PROXY_NAME", EXTCHK_SIZE_EVAL_INIT },
108 [EXTCHK_HAPROXY_PROXY_ID] = { "HAPROXY_PROXY_ID", EXTCHK_SIZE_EVAL_INIT },
109 [EXTCHK_HAPROXY_PROXY_ADDR] = { "HAPROXY_PROXY_ADDR", EXTCHK_SIZE_EVAL_INIT },
110 [EXTCHK_HAPROXY_PROXY_PORT] = { "HAPROXY_PROXY_PORT", EXTCHK_SIZE_EVAL_INIT },
111 [EXTCHK_HAPROXY_SERVER_NAME] = { "HAPROXY_SERVER_NAME", EXTCHK_SIZE_EVAL_INIT },
112 [EXTCHK_HAPROXY_SERVER_ID] = { "HAPROXY_SERVER_ID", EXTCHK_SIZE_EVAL_INIT },
113 [EXTCHK_HAPROXY_SERVER_ADDR] = { "HAPROXY_SERVER_ADDR", EXTCHK_SIZE_EVAL_INIT },
114 [EXTCHK_HAPROXY_SERVER_PORT] = { "HAPROXY_SERVER_PORT", EXTCHK_SIZE_EVAL_INIT },
115 [EXTCHK_HAPROXY_SERVER_MAXCONN] = { "HAPROXY_SERVER_MAXCONN", EXTCHK_SIZE_EVAL_INIT },
116 [EXTCHK_HAPROXY_SERVER_CURCONN] = { "HAPROXY_SERVER_CURCONN", EXTCHK_SIZE_ULONG },
117};
118
Simon Horman63a4a822012-03-19 07:24:41 +0900119static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100120 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
121
122 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
123 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
124
125 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
126 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
127 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
128 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
129
130 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
131 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
132 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
133};
134
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200135/*
136 * Convert check_status code to description
137 */
138const char *get_check_status_description(short check_status) {
139
140 const char *desc;
141
142 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200143 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200144 else
145 desc = NULL;
146
147 if (desc && *desc)
148 return desc;
149 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200150 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200151}
152
153/*
154 * Convert check_status code to short info
155 */
156const char *get_check_status_info(short check_status) {
157
158 const char *info;
159
160 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200161 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200162 else
163 info = NULL;
164
165 if (info && *info)
166 return info;
167 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200168 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200169}
170
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100171const char *get_analyze_status(short analyze_status) {
172
173 const char *desc;
174
175 if (analyze_status < HANA_STATUS_SIZE)
176 desc = analyze_statuses[analyze_status].desc;
177 else
178 desc = NULL;
179
180 if (desc && *desc)
181 return desc;
182 else
183 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
184}
185
Willy Tarreaua150cf12014-05-20 21:57:23 +0200186/* Builds a string containing some information about the health check's result.
187 * The output string is allocated from the trash chunks. If the check is NULL,
188 * NULL is returned. This is designed to be used when emitting logs about health
189 * checks.
Willy Tarreauddd329c2014-05-16 16:46:12 +0200190 */
Willy Tarreaua150cf12014-05-20 21:57:23 +0200191static const char *check_reason_string(struct check *check)
Willy Tarreauddd329c2014-05-16 16:46:12 +0200192{
Willy Tarreaua150cf12014-05-20 21:57:23 +0200193 struct chunk *msg;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200194
Willy Tarreaua150cf12014-05-20 21:57:23 +0200195 if (!check)
196 return NULL;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200197
Willy Tarreaua150cf12014-05-20 21:57:23 +0200198 msg = get_trash_chunk();
199 chunk_printf(msg, "reason: %s", get_check_status_description(check->status));
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200200
Willy Tarreaua150cf12014-05-20 21:57:23 +0200201 if (check->status >= HCHK_STATUS_L57DATA)
202 chunk_appendf(msg, ", code: %d", check->code);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200203
Willy Tarreaua150cf12014-05-20 21:57:23 +0200204 if (*check->desc) {
205 struct chunk src;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200206
Willy Tarreaua150cf12014-05-20 21:57:23 +0200207 chunk_appendf(msg, ", info: \"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200208
Willy Tarreaua150cf12014-05-20 21:57:23 +0200209 chunk_initlen(&src, check->desc, 0, strlen(check->desc));
210 chunk_asciiencode(msg, &src, '"');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200211
Willy Tarreaua150cf12014-05-20 21:57:23 +0200212 chunk_appendf(msg, "\"");
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200213 }
214
Willy Tarreaua150cf12014-05-20 21:57:23 +0200215 if (check->duration >= 0)
216 chunk_appendf(msg, ", check duration: %ldms", check->duration);
217
218 return msg->str;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200219}
220
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200221/*
Simon Horman4a741432013-02-23 15:35:38 +0900222 * Set check->status, update check->duration and fill check->result with
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200223 * an adequate CHK_RES_* value. The new check->health is computed based
224 * on the result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200225 *
226 * Show information in logs about failed health check if server is UP
227 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200228 */
Simon Horman4a741432013-02-23 15:35:38 +0900229static void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100230{
Simon Horman4a741432013-02-23 15:35:38 +0900231 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200232 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200233 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900234
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200235 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100236 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900237 check->desc[0] = '\0';
238 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200239 return;
240 }
241
Simon Horman4a741432013-02-23 15:35:38 +0900242 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200243 return;
244
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200245 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900246 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
247 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200248 } else
Simon Horman4a741432013-02-23 15:35:38 +0900249 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200250
Simon Horman4a741432013-02-23 15:35:38 +0900251 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200252 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900253 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200254
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100255 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900256 check->duration = -1;
257 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200258 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900259 check->duration = tv_ms_elapsed(&check->start, &now);
260 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200261 }
262
Willy Tarreau23964182014-05-20 20:56:30 +0200263 /* no change is expected if no state change occurred */
264 if (check->result == CHK_RES_NEUTRAL)
265 return;
266
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200267 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200268
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200269 switch (check->result) {
270 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200271 /* Failure to connect to the agent as a secondary check should not
272 * cause the server to be marked down.
273 */
274 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900275 (check->status >= HCHK_STATUS_L57DATA)) &&
Willy Tarreau12634e12014-05-23 11:32:36 +0200276 (check->health >= check->rise)) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200277 s->counters.failed_checks++;
278 report = 1;
279 check->health--;
280 if (check->health < check->rise)
281 check->health = 0;
282 }
283 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200284
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200285 case CHK_RES_PASSED:
286 case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */
287 if ((check->health < check->rise + check->fall - 1) &&
288 (check->result == CHK_RES_PASSED || check->health > 0)) {
289 report = 1;
290 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200291
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200292 if (check->health >= check->rise)
293 check->health = check->rise + check->fall - 1; /* OK now */
294 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200295
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200296 /* clear consecutive_errors if observing is enabled */
297 if (s->onerror)
298 s->consecutive_errors = 0;
299 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100300
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200301 default:
302 break;
303 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200304
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200305 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
306 (status != prev_status || report)) {
307 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200308 "%s check for %sserver %s/%s %s%s",
309 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200310 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100311 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100312 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200313 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200314
Willy Tarreaua150cf12014-05-20 21:57:23 +0200315 srv_append_status(&trash, s, check_reason_string(check), -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200316
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100317 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200318 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
319 (check->health >= check->rise) ? check->fall : check->rise,
320 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200321
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100322 Warning("%s.\n", trash.str);
323 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman7ea9be02015-04-30 13:10:33 +0900324 send_email_alert(s, LOG_INFO, "%s", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200325 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200326}
327
Willy Tarreau4eec5472014-05-20 22:32:27 +0200328/* Marks the check <check>'s server down if the current check is already failed
329 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200330 */
Willy Tarreau4eec5472014-05-20 22:32:27 +0200331static void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332{
Simon Horman4a741432013-02-23 15:35:38 +0900333 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900334
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200335 /* The agent secondary check should only cause a server to be marked
336 * as down if check->status is HCHK_STATUS_L7STS, which indicates
337 * that the agent returned "fail", "stopped" or "down".
338 * The implication here is that failure to connect to the agent
339 * as a secondary check should not cause the server to be marked
340 * down. */
341 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
342 return;
343
Willy Tarreau4eec5472014-05-20 22:32:27 +0200344 if (check->health > 0)
345 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100346
Willy Tarreau4eec5472014-05-20 22:32:27 +0200347 /* We only report a reason for the check if we did not do so previously */
348 srv_set_stopped(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349}
350
Willy Tarreauaf549582014-05-16 17:37:50 +0200351/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200352 * it isn't in maintenance, it is not tracking a down server and other checks
353 * comply. The rule is simple : by default, a server is up, unless any of the
354 * following conditions is true :
355 * - health check failed (check->health < rise)
356 * - agent check failed (agent->health < rise)
357 * - the server tracks a down server (track && track->state == STOPPED)
358 * Note that if the server has a slowstart, it will switch to STARTING instead
359 * of RUNNING. Also, only the health checks support the nolb mode, so the
360 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200361 */
Willy Tarreau3e048382014-05-21 10:30:54 +0200362static void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200363{
Simon Horman4a741432013-02-23 15:35:38 +0900364 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100365
Emeric Brun52a91d32017-08-31 14:41:55 +0200366 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200367 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100368
Emeric Brun52a91d32017-08-31 14:41:55 +0200369 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200370 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100371
Willy Tarreau3e048382014-05-21 10:30:54 +0200372 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
373 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100374
Willy Tarreau3e048382014-05-21 10:30:54 +0200375 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
376 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200377
Emeric Brun52a91d32017-08-31 14:41:55 +0200378 if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING)
Willy Tarreau3e048382014-05-21 10:30:54 +0200379 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100380
Willy Tarreau3e048382014-05-21 10:30:54 +0200381 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 +0100382}
383
Willy Tarreaudb58b792014-05-21 13:57:23 +0200384/* Marks the check <check> as valid and tries to set its server into stopping mode
385 * if it was running or starting, and provided it isn't in maintenance and other
386 * checks comply. The conditions for the server to be marked in stopping mode are
387 * the same as for it to be turned up. Also, only the health checks support the
388 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200389 */
Willy Tarreaudb58b792014-05-21 13:57:23 +0200390static void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200391{
Simon Horman4a741432013-02-23 15:35:38 +0900392 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100393
Emeric Brun52a91d32017-08-31 14:41:55 +0200394 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200395 return;
396
Willy Tarreaudb58b792014-05-21 13:57:23 +0200397 if (check->state & CHK_ST_AGENT)
398 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100399
Emeric Brun52a91d32017-08-31 14:41:55 +0200400 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreaudb58b792014-05-21 13:57:23 +0200401 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100402
Willy Tarreaudb58b792014-05-21 13:57:23 +0200403 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
404 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100405
Willy Tarreaudb58b792014-05-21 13:57:23 +0200406 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
407 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100408
Willy Tarreaudb58b792014-05-21 13:57:23 +0200409 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 +0100410}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100412/* note: use health_adjust() only, which first checks that the observe mode is
413 * enabled.
414 */
415void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100416{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100417 int failed;
418 int expire;
419
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100420 if (s->observe >= HANA_OBS_SIZE)
421 return;
422
Willy Tarreaubb956662013-01-24 00:37:39 +0100423 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100424 return;
425
426 switch (analyze_statuses[status].lr[s->observe - 1]) {
427 case 1:
428 failed = 1;
429 break;
430
431 case 2:
432 failed = 0;
433 break;
434
435 default:
436 return;
437 }
438
439 if (!failed) {
440 /* good: clear consecutive_errors */
441 s->consecutive_errors = 0;
442 return;
443 }
444
445 s->consecutive_errors++;
446
447 if (s->consecutive_errors < s->consecutive_errors_limit)
448 return;
449
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100450 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
451 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100452
453 switch (s->onerror) {
454 case HANA_ONERR_FASTINTER:
455 /* force fastinter - nothing to do here as all modes force it */
456 break;
457
458 case HANA_ONERR_SUDDTH:
459 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900460 if (s->check.health > s->check.rise)
461 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100462
463 /* no break - fall through */
464
465 case HANA_ONERR_FAILCHK:
466 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900467 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200468 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100469 break;
470
471 case HANA_ONERR_MARKDWN:
472 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900473 s->check.health = s->check.rise;
Simon Horman4a741432013-02-23 15:35:38 +0900474 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200475 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100476 break;
477
478 default:
479 /* write a warning? */
480 break;
481 }
482
483 s->consecutive_errors = 0;
484 s->counters.failed_hana++;
485
Simon Horman66183002013-02-23 10:16:43 +0900486 if (s->check.fastinter) {
487 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300488 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200489 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300490 /* requeue check task with new expire */
491 task_queue(s->check.task);
492 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100493 }
494}
495
Willy Tarreaua1dab552014-04-14 15:04:54 +0200496static int httpchk_build_status_header(struct server *s, char *buffer, int size)
Willy Tarreauef781042010-01-27 11:53:01 +0100497{
498 int sv_state;
499 int ratio;
500 int hlen = 0;
Joseph Lynch514061c2015-01-15 17:52:59 -0800501 char addr[46];
502 char port[6];
Willy Tarreauef781042010-01-27 11:53:01 +0100503 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
504 "UP %d/%d", "UP",
505 "NOLB %d/%d", "NOLB",
506 "no check" };
507
508 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
509 hlen += 24;
510
Willy Tarreauff5ae352013-12-11 20:36:34 +0100511 if (!(s->check.state & CHK_ST_ENABLED))
512 sv_state = 6;
Emeric Brun52a91d32017-08-31 14:41:55 +0200513 else if (s->cur_state != SRV_ST_STOPPED) {
Simon Horman58c32972013-11-25 10:46:38 +0900514 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100515 sv_state = 3; /* UP */
516 else
517 sv_state = 2; /* going down */
518
Emeric Brun52a91d32017-08-31 14:41:55 +0200519 if (s->cur_state == SRV_ST_STOPPING)
Willy Tarreauef781042010-01-27 11:53:01 +0100520 sv_state += 2;
521 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900522 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100523 sv_state = 1; /* going up */
524 else
525 sv_state = 0; /* DOWN */
526 }
527
Willy Tarreaua1dab552014-04-14 15:04:54 +0200528 hlen += snprintf(buffer + hlen, size - hlen,
Willy Tarreauef781042010-01-27 11:53:01 +0100529 srv_hlt_st[sv_state],
Emeric Brun52a91d32017-08-31 14:41:55 +0200530 (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
531 (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100532
Joseph Lynch514061c2015-01-15 17:52:59 -0800533 addr_to_str(&s->addr, addr, sizeof(addr));
Willy Tarreau04276f32017-01-06 17:41:29 +0100534 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
535 snprintf(port, sizeof(port), "%u", s->svc_port);
536 else
537 *port = 0;
Joseph Lynch514061c2015-01-15 17:52:59 -0800538
539 hlen += snprintf(buffer + hlen, size - hlen, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
540 addr, port, s->proxy->id, s->id,
Willy Tarreauef781042010-01-27 11:53:01 +0100541 global.node,
Emeric Brun52a91d32017-08-31 14:41:55 +0200542 (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
Willy Tarreauef781042010-01-27 11:53:01 +0100543 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
544 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
545 s->nbpend);
546
Emeric Brun52a91d32017-08-31 14:41:55 +0200547 if ((s->cur_state == SRV_ST_STARTING) &&
Willy Tarreauef781042010-01-27 11:53:01 +0100548 now.tv_sec < s->last_change + s->slowstart &&
549 now.tv_sec >= s->last_change) {
550 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
Willy Tarreaua1dab552014-04-14 15:04:54 +0200551 hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio);
Willy Tarreauef781042010-01-27 11:53:01 +0100552 }
553
554 buffer[hlen++] = '\r';
555 buffer[hlen++] = '\n';
556
557 return hlen;
558}
559
Willy Tarreau20a18342013-12-05 00:31:46 +0100560/* Check the connection. If an error has already been reported or the socket is
561 * closed, keep errno intact as it is supposed to contain the valid error code.
562 * If no error is reported, check the socket's error queue using getsockopt().
563 * Warning, this must be done only once when returning from poll, and never
564 * after an I/O error was attempted, otherwise the error queue might contain
565 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
566 * socket. Returns non-zero if an error was reported, zero if everything is
567 * clean (including a properly closed socket).
568 */
569static int retrieve_errno_from_socket(struct connection *conn)
570{
571 int skerr;
572 socklen_t lskerr = sizeof(skerr);
573
574 if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl))
575 return 1;
576
Willy Tarreau3c728722014-01-23 13:50:42 +0100577 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100578 return 0;
579
Willy Tarreau585744b2017-08-24 14:31:19 +0200580 if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
Willy Tarreau20a18342013-12-05 00:31:46 +0100581 errno = skerr;
582
583 if (errno == EAGAIN)
584 errno = 0;
585
586 if (!errno) {
587 /* we could not retrieve an error, that does not mean there is
588 * none. Just don't change anything and only report the prior
589 * error if any.
590 */
591 if (conn->flags & CO_FL_ERROR)
592 return 1;
593 else
594 return 0;
595 }
596
597 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
598 return 1;
599}
600
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100601/* Try to collect as much information as possible on the connection status,
602 * and adjust the server status accordingly. It may make use of <errno_bck>
603 * if non-null when the caller is absolutely certain of its validity (eg:
604 * checked just after a syscall). If the caller doesn't have a valid errno,
605 * it can pass zero, and retrieve_errno_from_socket() will be called to try
606 * to extract errno from the socket. If no error is reported, it will consider
607 * the <expired> flag. This is intended to be used when a connection error was
608 * reported in conn->flags or when a timeout was reported in <expired>. The
609 * function takes care of not updating a server status which was already set.
610 * All situations where at least one of <expired> or CO_FL_ERROR are set
611 * produce a status.
612 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200613static void chk_report_conn_err(struct check *check, int errno_bck, int expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100614{
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200615 struct connection *conn = check->conn;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100616 const char *err_msg;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200617 struct chunk *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200618 int step;
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200619 char *comment;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100620
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100621 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100622 return;
623
624 errno = errno_bck;
Willy Tarreau00149122017-10-04 18:05:01 +0200625 if (conn && (!errno || errno == EAGAIN))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100626 retrieve_errno_from_socket(conn);
627
Willy Tarreau00149122017-10-04 18:05:01 +0200628 if (conn && !(conn->flags & CO_FL_ERROR) && !expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100629 return;
630
631 /* we'll try to build a meaningful error message depending on the
632 * context of the error possibly present in conn->err_code, and the
633 * socket error possibly collected above. This is useful to know the
634 * exact step of the L6 layer (eg: SSL handshake).
635 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200636 chk = get_trash_chunk();
637
638 if (check->type == PR_O2_TCPCHK_CHK) {
Simon Hormane16c1b32015-01-30 11:22:57 +0900639 step = tcpcheck_get_step_id(check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200640 if (!step)
641 chunk_printf(chk, " at initial connection step of tcp-check");
642 else {
643 chunk_printf(chk, " at step %d of tcp-check", step);
644 /* we were looking for a string */
645 if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_CONNECT) {
646 if (check->last_started_step->port)
647 chunk_appendf(chk, " (connect port %d)" ,check->last_started_step->port);
648 else
649 chunk_appendf(chk, " (connect)");
650 }
651 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_EXPECT) {
652 if (check->last_started_step->string)
Baptiste Assmann96a5c9b2015-05-01 08:09:29 +0200653 chunk_appendf(chk, " (expect string '%s')", check->last_started_step->string);
Willy Tarreau213c6782014-10-02 14:51:02 +0200654 else if (check->last_started_step->expect_regex)
655 chunk_appendf(chk, " (expect regex)");
656 }
657 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_SEND) {
658 chunk_appendf(chk, " (send)");
659 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200660
661 comment = tcpcheck_get_step_comment(check, step);
662 if (comment)
663 chunk_appendf(chk, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200664 }
665 }
666
Willy Tarreau00149122017-10-04 18:05:01 +0200667 if (conn && conn->err_code) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100668 if (errno && errno != EAGAIN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200669 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100670 else
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200671 chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100672 err_msg = trash.str;
673 }
674 else {
675 if (errno && errno != EAGAIN) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200676 chunk_printf(&trash, "%s%s", strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100677 err_msg = trash.str;
678 }
679 else {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200680 err_msg = chk->str;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100681 }
682 }
683
Willy Tarreau00149122017-10-04 18:05:01 +0200684 if (check->state & CHK_ST_PORT_MISS) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200685 /* NOTE: this is reported after <fall> tries */
686 chunk_printf(chk, "No port available for the TCP connection");
687 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
688 }
689
Willy Tarreau00149122017-10-04 18:05:01 +0200690 if (!conn) {
691 /* connection allocation error before the connection was established */
692 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
693 }
694 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100695 /* L4 not established (yet) */
696 if (conn->flags & CO_FL_ERROR)
697 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
698 else if (expired)
699 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200700
701 /*
702 * might be due to a server IP change.
703 * Let's trigger a DNS resolution if none are currently running.
704 */
705 if ((check->server->resolution) && (check->server->resolution->step == RSLV_STEP_NONE))
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200706 dns_trigger_resolution(check->server->resolution);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200707
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100708 }
709 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
710 /* L6 not established (yet) */
711 if (conn->flags & CO_FL_ERROR)
712 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
713 else if (expired)
714 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
715 }
716 else if (conn->flags & CO_FL_ERROR) {
717 /* I/O error after connection was established and before we could diagnose */
718 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
719 }
720 else if (expired) {
721 /* connection established but expired check */
722 if (check->type == PR_O2_SSL3_CHK)
723 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
724 else /* HTTP, SMTP, ... */
725 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
726 }
727
728 return;
729}
730
Willy Tarreaubaaee002006-06-26 02:48:02 +0200731/*
732 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200733 * the connection acknowledgement. If the proxy requires L7 health-checks,
734 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900735 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200736 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200737static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200738{
Simon Horman4a741432013-02-23 15:35:38 +0900739 struct check *check = conn->owner;
740 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900741 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200742
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100743 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100744 goto out_wakeup;
745
Willy Tarreau310987a2014-01-22 19:46:33 +0100746 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100747 return;
748
Willy Tarreau20a18342013-12-05 00:31:46 +0100749 if (retrieve_errno_from_socket(conn)) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200750 chk_report_conn_err(check, errno, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100751 __conn_data_stop_both(conn);
752 goto out_wakeup;
753 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100754
Willy Tarreaubbae3f02017-08-30 09:59:52 +0200755 if (conn->flags & CO_FL_SOCK_WR_SH) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100756 /* if the output is closed, we can't do anything */
757 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200758 chk_report_conn_err(check, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100759 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200760 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200761
Willy Tarreau06559ac2013-12-05 01:53:08 +0100762 /* here, we know that the connection is established. That's enough for
763 * a pure TCP check.
764 */
765 if (!check->type)
766 goto out_wakeup;
767
Willy Tarreauc09572f2017-10-04 11:58:22 +0200768 /* wake() will take care of calling tcpcheck_main() */
769 if (check->type == PR_O2_TCPCHK_CHK)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200770 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200771
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100772 if (check->bo->o) {
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100773 conn->xprt->snd_buf(conn, check->bo, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100774 if (conn->flags & CO_FL_ERROR) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200775 chk_report_conn_err(check, errno, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100776 __conn_data_stop_both(conn);
777 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200778 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100779 if (check->bo->o)
780 return;
781 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200782
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100783 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
784 if (s->proxy->timeout.check) {
785 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
786 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200787 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100788 goto out_nowake;
789
Willy Tarreau83749182007-04-15 20:56:27 +0200790 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200791 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200792 out_nowake:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200793 __conn_data_stop_send(conn); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200794}
795
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200797 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200798 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900799 * set_server_check_status() to update check->status, check->duration
800 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200801
802 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
803 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
804 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
805 * response to an SSL HELLO (the principle is that this is enough to
806 * distinguish between an SSL server and a pure TCP relay). All other cases will
807 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
808 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200809 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200810static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200811{
Simon Horman4a741432013-02-23 15:35:38 +0900812 struct check *check = conn->owner;
813 struct server *s = check->server;
814 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200815 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100816 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200817 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200818
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100819 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +0200820 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200821
Willy Tarreau310987a2014-01-22 19:46:33 +0100822 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200823 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200824
Willy Tarreauc09572f2017-10-04 11:58:22 +0200825 /* wake() will take care of calling tcpcheck_main() */
826 if (check->type == PR_O2_TCPCHK_CHK)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200827 return;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200828
Willy Tarreau83749182007-04-15 20:56:27 +0200829 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
830 * but the connection was closed on the remote end. Fortunately, recv still
831 * works correctly and we don't need to do the getsockopt() on linux.
832 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000833
834 /* Set buffer to point to the end of the data already read, and check
835 * that there is free space remaining. If the buffer is full, proceed
836 * with running the checks without attempting another socket read.
837 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000838
Willy Tarreau03938182010-03-17 21:52:07 +0100839 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +0000840
Simon Horman4a741432013-02-23 15:35:38 +0900841 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreau54e917c2017-08-30 07:35:35 +0200842 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +0100843 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +0900844 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +0200845 /* Report network errors only if we got no other data. Otherwise
846 * we'll let the upper layers decide whether the response is OK
847 * or not. It is very common that an RST sent by the server is
848 * reported as an error just after the last data chunk.
849 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200850 chk_report_conn_err(check, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +0100851 goto out_wakeup;
852 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200853 }
854
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100855
Willy Tarreau03938182010-03-17 21:52:07 +0100856 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +0900857 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +0100858 */
Simon Horman4a741432013-02-23 15:35:38 +0900859 if (check->bi->i < check->bi->size)
860 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100861 else {
Simon Horman4a741432013-02-23 15:35:38 +0900862 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100863 done = 1; /* buffer full, don't wait for more data */
864 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200865
Nick Chalk57b1bf72010-03-16 15:50:46 +0000866 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +0900867 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +0200868 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900869 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100870 goto wait_more_data;
871
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100872 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +0900873 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
874 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
875 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
876 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
877 !isdigit((unsigned char) *(check->bi->data + 11))) {
878 cut_crlf(check->bi->data);
879 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200880
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100881 goto out_wakeup;
882 }
883
Simon Horman4a741432013-02-23 15:35:38 +0900884 check->code = str2uic(check->bi->data + 9);
885 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +0000886
Willy Tarreaubd741542010-03-16 18:46:54 +0100887 if ((s->proxy->options & PR_O_DISABLE404) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200888 (s->next_state != SRV_ST_STOPPED) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000889 /* 404 may be accepted as "stopping" only if the server was up */
890 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900891 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000892 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100893 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
894 /* Run content verification check... We know we have at least 13 chars */
895 if (!httpchk_expect(s, done))
896 goto wait_more_data;
897 }
898 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +0900899 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +0100900 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900901 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +0100902 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000903 else {
904 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900905 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000906 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200907 break;
908
909 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900910 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +0100911 goto wait_more_data;
912
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100913 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +0900914 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
915 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200916 else
Simon Horman4a741432013-02-23 15:35:38 +0900917 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200918 break;
919
920 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900921 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100922 goto wait_more_data;
923
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200924 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +0900925 if ((check->bi->i < strlen("000\r")) ||
926 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
927 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
928 !isdigit((unsigned char) *(check->bi->data + 2))) {
929 cut_crlf(check->bi->data);
930 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200931
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200932 goto out_wakeup;
933 }
934
Simon Horman4a741432013-02-23 15:35:38 +0900935 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200936
Simon Horman4a741432013-02-23 15:35:38 +0900937 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200938 cut_crlf(desc);
939
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100940 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +0900941 if (*check->bi->data == '2')
942 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200943 else
Simon Horman4a741432013-02-23 15:35:38 +0900944 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200945 break;
946
Simon Hormana2b9dad2013-02-12 10:45:54 +0900947 case PR_O2_LB_AGENT_CHK: {
Willy Tarreau81f5d942013-12-09 20:51:51 +0100948 int status = HCHK_STATUS_CHECKED;
949 const char *hs = NULL; /* health status */
950 const char *as = NULL; /* admin status */
951 const char *ps = NULL; /* performance status */
Nenad Merdanovic174dd372016-04-24 23:10:06 +0200952 const char *cs = NULL; /* maxconn */
Willy Tarreau81f5d942013-12-09 20:51:51 +0100953 const char *err = NULL; /* first error to report */
954 const char *wrn = NULL; /* first warning to report */
955 char *cmd, *p;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900956
Willy Tarreau81f5d942013-12-09 20:51:51 +0100957 /* We're getting an agent check response. The agent could
958 * have been disabled in the mean time with a long check
959 * still pending. It is important that we ignore the whole
960 * response.
961 */
962 if (!(check->server->agent.state & CHK_ST_ENABLED))
963 break;
964
965 /* The agent supports strings made of a single line ended by the
966 * first CR ('\r') or LF ('\n'). This line is composed of words
967 * delimited by spaces (' '), tabs ('\t'), or commas (','). The
968 * line may optionally contained a description of a state change
969 * after a sharp ('#'), which is only considered if a health state
970 * is announced.
971 *
972 * Words may be composed of :
973 * - a numeric weight suffixed by the percent character ('%').
974 * - a health status among "up", "down", "stopped", and "fail".
975 * - an admin status among "ready", "drain", "maint".
976 *
977 * These words may appear in any order. If multiple words of the
978 * same category appear, the last one wins.
979 */
980
Willy Tarreau9809b782013-12-11 21:40:11 +0100981 p = check->bi->data;
982 while (*p && *p != '\n' && *p != '\r')
983 p++;
984
985 if (!*p) {
986 if (!done)
987 goto wait_more_data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900988
Willy Tarreau9809b782013-12-11 21:40:11 +0100989 /* at least inform the admin that the agent is mis-behaving */
990 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
991 break;
992 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100993
Willy Tarreau9809b782013-12-11 21:40:11 +0100994 *p = 0;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100995 cmd = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900996
Willy Tarreau81f5d942013-12-09 20:51:51 +0100997 while (*cmd) {
998 /* look for next word */
999 if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') {
1000 cmd++;
1001 continue;
1002 }
Simon Horman671b6f02013-11-25 10:46:39 +09001003
Willy Tarreau81f5d942013-12-09 20:51:51 +01001004 if (*cmd == '#') {
1005 /* this is the beginning of a health status description,
1006 * skip the sharp and blanks.
1007 */
1008 cmd++;
1009 while (*cmd == '\t' || *cmd == ' ')
1010 cmd++;
Simon Horman671b6f02013-11-25 10:46:39 +09001011 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001012 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001013
1014 /* find the end of the word so that we have a null-terminated
1015 * word between <cmd> and <p>.
1016 */
1017 p = cmd + 1;
1018 while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',')
1019 p++;
1020 if (*p)
1021 *p++ = 0;
1022
1023 /* first, health statuses */
1024 if (strcasecmp(cmd, "up") == 0) {
1025 check->health = check->rise + check->fall - 1;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001026 status = HCHK_STATUS_L7OKD;
Willy Tarreau81f5d942013-12-09 20:51:51 +01001027 hs = cmd;
1028 }
1029 else if (strcasecmp(cmd, "down") == 0) {
1030 check->health = 0;
1031 status = HCHK_STATUS_L7STS;
1032 hs = cmd;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001033 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001034 else if (strcasecmp(cmd, "stopped") == 0) {
1035 check->health = 0;
1036 status = HCHK_STATUS_L7STS;
1037 hs = cmd;
1038 }
1039 else if (strcasecmp(cmd, "fail") == 0) {
1040 check->health = 0;
1041 status = HCHK_STATUS_L7STS;
1042 hs = cmd;
1043 }
1044 /* admin statuses */
1045 else if (strcasecmp(cmd, "ready") == 0) {
1046 as = cmd;
1047 }
1048 else if (strcasecmp(cmd, "drain") == 0) {
1049 as = cmd;
1050 }
1051 else if (strcasecmp(cmd, "maint") == 0) {
1052 as = cmd;
1053 }
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001054 /* try to parse a weight here and keep the last one */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001055 else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) {
1056 ps = cmd;
1057 }
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001058 /* try to parse a maxconn here */
1059 else if (strncasecmp(cmd, "maxconn:", strlen("maxconn:")) == 0) {
1060 cs = cmd;
1061 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001062 else {
1063 /* keep a copy of the first error */
1064 if (!err)
1065 err = cmd;
1066 }
1067 /* skip to next word */
1068 cmd = p;
1069 }
1070 /* here, cmd points either to \0 or to the beginning of a
1071 * description. Skip possible leading spaces.
1072 */
1073 while (*cmd == ' ' || *cmd == '\n')
1074 cmd++;
1075
1076 /* First, update the admin status so that we avoid sending other
1077 * possibly useless warnings and can also update the health if
1078 * present after going back up.
1079 */
1080 if (as) {
1081 if (strcasecmp(as, "drain") == 0)
1082 srv_adm_set_drain(check->server);
1083 else if (strcasecmp(as, "maint") == 0)
1084 srv_adm_set_maint(check->server);
1085 else
1086 srv_adm_set_ready(check->server);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001087 }
1088
Willy Tarreau81f5d942013-12-09 20:51:51 +01001089 /* now change weights */
1090 if (ps) {
1091 const char *msg;
1092
1093 msg = server_parse_weight_change_request(s, ps);
1094 if (!wrn || !*wrn)
1095 wrn = msg;
1096 }
1097
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001098 if (cs) {
1099 const char *msg;
1100
1101 cs += strlen("maxconn:");
1102
1103 msg = server_parse_maxconn_change_request(s, cs);
1104 if (!wrn || !*wrn)
1105 wrn = msg;
1106 }
1107
Willy Tarreau81f5d942013-12-09 20:51:51 +01001108 /* and finally health status */
1109 if (hs) {
1110 /* We'll report some of the warnings and errors we have
1111 * here. Down reports are critical, we leave them untouched.
1112 * Lack of report, or report of 'UP' leaves the room for
1113 * ERR first, then WARN.
Simon Hormana2b9dad2013-02-12 10:45:54 +09001114 */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001115 const char *msg = cmd;
1116 struct chunk *t;
1117
1118 if (!*msg || status == HCHK_STATUS_L7OKD) {
1119 if (err && *err)
1120 msg = err;
1121 else if (wrn && *wrn)
1122 msg = wrn;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001123 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001124
1125 t = get_trash_chunk();
1126 chunk_printf(t, "via agent : %s%s%s%s",
1127 hs, *msg ? " (" : "",
1128 msg, *msg ? ")" : "");
1129
1130 set_server_check_status(check, status, t->str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001131 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001132 else if (err && *err) {
1133 /* No status change but we'd like to report something odd.
1134 * Just report the current state and copy the message.
1135 */
1136 chunk_printf(&trash, "agent reports an error : %s", err);
1137 set_server_check_status(check, status/*check->status*/, trash.str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001138
Willy Tarreau81f5d942013-12-09 20:51:51 +01001139 }
1140 else if (wrn && *wrn) {
1141 /* No status change but we'd like to report something odd.
1142 * Just report the current state and copy the message.
1143 */
1144 chunk_printf(&trash, "agent warns : %s", wrn);
1145 set_server_check_status(check, status/*check->status*/, trash.str);
1146 }
1147 else
1148 set_server_check_status(check, status, NULL);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001149 break;
1150 }
1151
Willy Tarreau1620ec32011-08-06 17:05:02 +02001152 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001153 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001154 goto wait_more_data;
1155
Simon Horman4a741432013-02-23 15:35:38 +09001156 if (check->bi->data[0] == 'R') {
1157 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001158 }
1159 else {
Simon Horman4a741432013-02-23 15:35:38 +09001160 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1161 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001162 else
1163 desc = "PostgreSQL unknown error";
1164
Simon Horman4a741432013-02-23 15:35:38 +09001165 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001166 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001167 break;
1168
1169 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001170 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001171 goto wait_more_data;
1172
Simon Horman4a741432013-02-23 15:35:38 +09001173 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1174 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001175 }
1176 else {
Simon Horman4a741432013-02-23 15:35:38 +09001177 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001178 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001179 break;
1180
1181 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001182 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001183 goto wait_more_data;
1184
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001185 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001186 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001187 /* We set the MySQL Version in description for information purpose
1188 * FIXME : it can be cool to use MySQL Version for other purpose,
1189 * like mark as down old MySQL server.
1190 */
Simon Horman4a741432013-02-23 15:35:38 +09001191 if (check->bi->i > 51) {
1192 desc = ltrim(check->bi->data + 5, ' ');
1193 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001194 }
1195 else {
1196 if (!done)
1197 goto wait_more_data;
1198 /* it seems we have a OK packet but without a valid length,
1199 * it must be a protocol error
1200 */
Simon Horman4a741432013-02-23 15:35:38 +09001201 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001202 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001203 }
1204 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001205 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001206 desc = ltrim(check->bi->data + 7, ' ');
1207 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001208 }
1209 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001210 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1211 (((unsigned int) *(check->bi->data + 1)) << 8) +
1212 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001213
Simon Horman4a741432013-02-23 15:35:38 +09001214 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001215 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001216 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001217 /* We have only one MySQL packet and it is a Handshake Initialization packet
1218 * but we need to have a second packet to know if it is alright
1219 */
Simon Horman4a741432013-02-23 15:35:38 +09001220 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001221 goto wait_more_data;
1222 }
1223 else {
1224 /* We have only one packet and it is an Error packet,
1225 * an error message is attached, so we can display it
1226 */
Simon Horman4a741432013-02-23 15:35:38 +09001227 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001228 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001229 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001230 }
Simon Horman4a741432013-02-23 15:35:38 +09001231 } else if (check->bi->i > first_packet_len + 4) {
1232 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1233 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1234 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001235
Simon Horman4a741432013-02-23 15:35:38 +09001236 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001237 /* We have 2 packets and that's good */
1238 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001239 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001240 /* No error packet */
1241 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001242 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001243 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001244 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001245 }
1246 else {
1247 /* An error message is attached in the Error packet
1248 * so we can display it ! :)
1249 */
Simon Horman4a741432013-02-23 15:35:38 +09001250 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001251 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001252 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001253 }
1254 }
1255 }
1256 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001257 if (!done)
1258 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001259 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001260 * it must be a protocol error
1261 */
Simon Horman4a741432013-02-23 15:35:38 +09001262 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001263 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001264 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001265 }
1266 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001267 break;
1268
1269 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001270 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001271 goto wait_more_data;
1272
1273 /* Check if the server speaks LDAP (ASN.1/BER)
1274 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1275 * http://tools.ietf.org/html/rfc4511
1276 */
1277
1278 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1279 * LDAPMessage: 0x30: SEQUENCE
1280 */
Simon Horman4a741432013-02-23 15:35:38 +09001281 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1282 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001283 }
1284 else {
1285 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001286 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001287
1288 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1289 * messageID: 0x02 0x01 0x01: INTEGER 1
1290 * protocolOp: 0x61: bindResponse
1291 */
1292 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001293 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1294 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001295
1296 goto out_wakeup;
1297 }
1298
1299 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001300 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001301
1302 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1303 * ldapResult: 0x0a 0x01: ENUMERATION
1304 */
1305 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001306 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1307 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001308
1309 goto out_wakeup;
1310 }
1311
1312 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1313 * resultCode
1314 */
Simon Horman4a741432013-02-23 15:35:38 +09001315 check->code = *(check->bi->data + msglen + 9);
1316 if (check->code) {
1317 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 +02001318 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001319 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001320 }
1321 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001322 break;
1323
Christopher Fauletba7bc162016-11-07 21:07:38 +01001324 case PR_O2_SPOP_CHK: {
1325 unsigned int framesz;
1326 char err[HCHK_DESC_LEN];
1327
1328 if (!done && check->bi->i < 4)
1329 goto wait_more_data;
1330
1331 memcpy(&framesz, check->bi->data, 4);
1332 framesz = ntohl(framesz);
1333
1334 if (!done && check->bi->i < (4+framesz))
1335 goto wait_more_data;
1336
Christopher Faulet8ef75252017-02-20 22:56:03 +01001337 if (!spoe_handle_healthcheck_response(check->bi->data+4, framesz, err, HCHK_DESC_LEN-1))
Christopher Fauletba7bc162016-11-07 21:07:38 +01001338 set_server_check_status(check, HCHK_STATUS_L7OKD, "SPOA server is ok");
1339 else
1340 set_server_check_status(check, HCHK_STATUS_L7STS, err);
1341 break;
1342 }
1343
Willy Tarreau1620ec32011-08-06 17:05:02 +02001344 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001345 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001346 break;
1347 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001348
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001349 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001350 /* collect possible new errors */
1351 if (conn->flags & CO_FL_ERROR)
Willy Tarreaub5259bf2017-10-04 14:47:29 +02001352 chk_report_conn_err(check, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001353
Nick Chalk57b1bf72010-03-16 15:50:46 +00001354 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001355 *check->bi->data = '\0';
1356 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001357
Steven Davidovitz544d4812017-03-08 11:06:20 -08001358 /* Close the connection... We still attempt to nicely close if,
1359 * for instance, SSL needs to send a "close notify." Later, we perform
1360 * a hard close and reset the connection if some data are pending,
1361 * otherwise we end up with many TIME_WAITs and eat all the source port
1362 * range quickly. To avoid sending RSTs all the time, we first try to
1363 * drain pending data.
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001364 */
Willy Tarreaub4017d02015-03-12 23:11:26 +01001365 __conn_data_stop_both(conn);
Steven Davidovitz544d4812017-03-08 11:06:20 -08001366 conn_data_shutw(conn);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001367
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001368 /* OK, let's not stay here forever */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001369 if (check->result == CHK_RES_FAILED)
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001370 conn->flags |= CO_FL_ERROR;
1371
Willy Tarreaufdccded2008-08-29 18:19:04 +02001372 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001373 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001374
1375 wait_more_data:
Willy Tarreauf817e9f2014-01-10 16:58:45 +01001376 __conn_data_want_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001377}
1378
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001379/*
1380 * This function is used only for server health-checks. It handles connection
1381 * status updates including errors. If necessary, it wakes the check task up.
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001382 * It returns 0 on normal cases, <0 if at least one close() has happened on the
1383 * connection (eg: reconnect).
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001384 */
1385static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001386{
Simon Horman4a741432013-02-23 15:35:38 +09001387 struct check *check = conn->owner;
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001388 int ret = 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001389
Willy Tarreauc09572f2017-10-04 11:58:22 +02001390 /* we may have to make progress on the TCP checks */
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001391 if (check->type == PR_O2_TCPCHK_CHK) {
1392 ret = tcpcheck_main(check);
Willy Tarreau00149122017-10-04 18:05:01 +02001393 conn = check->conn;
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001394 }
Willy Tarreauc09572f2017-10-04 11:58:22 +02001395
Willy Tarreau6c560da2012-11-24 11:14:45 +01001396 if (unlikely(conn->flags & CO_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001397 /* We may get error reports bypassing the I/O handlers, typically
1398 * the case when sending a pure TCP check which fails, then the I/O
1399 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001400 * main processing task so let's simply wake it up. If we get here,
1401 * we expect errno to still be valid.
1402 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +02001403 chk_report_conn_err(check, errno, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001404
Willy Tarreau2d351b62013-12-05 02:36:25 +01001405 __conn_data_stop_both(conn);
1406 task_wakeup(check->task, TASK_WOKEN_IO);
1407 }
Willy Tarreau3be293f2014-02-05 18:31:24 +01001408 else if (!(conn->flags & (CO_FL_DATA_RD_ENA|CO_FL_DATA_WR_ENA|CO_FL_HANDSHAKE))) {
1409 /* we may get here if only a connection probe was required : we
1410 * don't have any data to send nor anything expected in response,
1411 * so the completion of the connection establishment is enough.
1412 */
1413 task_wakeup(check->task, TASK_WOKEN_IO);
1414 }
Willy Tarreau2d351b62013-12-05 02:36:25 +01001415
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001416 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001417 /* We're here because nobody wants to handle the error, so we
1418 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001419 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01001420 conn_sock_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02001421 conn_force_close(conn);
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001422 ret = -1;
Willy Tarreau2d351b62013-12-05 02:36:25 +01001423 }
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001424
1425 /* if a connection got replaced, we must absolutely prevent the connection
1426 * handler from touching its fd, and perform the FD polling updates ourselves
1427 */
1428 if (ret < 0)
1429 conn_cond_update_polling(conn);
1430
1431 return ret;
Willy Tarreau20bea422012-07-06 12:00:49 +02001432}
1433
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001434struct data_cb check_conn_cb = {
1435 .recv = event_srv_chk_r,
1436 .send = event_srv_chk_w,
1437 .wake = wake_srv_chk,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +01001438 .name = "CHCK",
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001439};
1440
Willy Tarreaubaaee002006-06-26 02:48:02 +02001441/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001442 * updates the server's weight during a warmup stage. Once the final weight is
1443 * reached, the task automatically stops. Note that any server status change
1444 * must have updated s->last_change accordingly.
1445 */
1446static struct task *server_warmup(struct task *t)
1447{
1448 struct server *s = t->context;
1449
1450 /* by default, plan on stopping the task */
1451 t->expire = TICK_ETERNITY;
Emeric Brun52a91d32017-08-31 14:41:55 +02001452 if ((s->next_admin & SRV_ADMF_MAINT) ||
1453 (s->next_state != SRV_ST_STARTING))
Willy Tarreau2e993902011-10-31 11:53:20 +01001454 return t;
1455
Willy Tarreau892337c2014-05-13 23:41:20 +02001456 /* recalculate the weights and update the state */
Willy Tarreau004e0452013-11-21 11:22:01 +01001457 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001458
1459 /* probably that we can refill this server with a bit more connections */
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001460 pendconn_grab_from_px(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001461
1462 /* get back there in 1 second or 1/20th of the slowstart interval,
1463 * whichever is greater, resulting in small 5% steps.
1464 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001465 if (s->next_state == SRV_ST_STARTING)
Willy Tarreau2e993902011-10-31 11:53:20 +01001466 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1467 return t;
1468}
1469
Willy Tarreau894c6422017-10-04 15:58:52 +02001470/* returns the first NON-COMMENT tcp-check rule from list <list> or NULL if
1471 * none was found.
1472 */
1473static struct tcpcheck_rule *get_first_tcpcheck_rule(struct list *list)
1474{
1475 struct tcpcheck_rule *r;
1476
1477 list_for_each_entry(r, list, list) {
1478 if (r->action != TCPCHK_ACT_COMMENT)
1479 return r;
1480 }
1481 return NULL;
1482}
1483
Willy Tarreau2e993902011-10-31 11:53:20 +01001484/*
Simon Horman98637e52014-06-20 12:30:16 +09001485 * establish a server health-check that makes use of a connection.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001486 *
1487 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001488 * - SF_ERR_NONE if everything's OK and tcpcheck_main() was not called
1489 * - SF_ERR_UP if if everything's OK and tcpcheck_main() was called
1490 * - SF_ERR_SRVTO if there are no more servers
1491 * - SF_ERR_SRVCL if the connection was refused by the server
1492 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1493 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1494 * - SF_ERR_INTERNAL for any other purely internal errors
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001495 * - SF_ERR_CHK_PORT if no port could be found to run a health check on an AF_INET* socket
Tim Düsterhus4896c442016-11-29 02:15:19 +01001496 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001497 * Note that we try to prevent the network stack from sending the ACK during the
1498 * connect() when a pure TCP check is used (without PROXY protocol).
1499 */
Simon Horman98637e52014-06-20 12:30:16 +09001500static int connect_conn_chk(struct task *t)
Simon Hormanb00d17a2014-06-13 16:18:16 +09001501{
1502 struct check *check = t->context;
1503 struct server *s = check->server;
1504 struct connection *conn = check->conn;
1505 struct protocol *proto;
Willy Tarreauf411cce2017-10-04 16:21:19 +02001506 struct tcpcheck_rule *tcp_rule = NULL;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001507 int ret;
Willy Tarreauf3d34822014-12-08 12:11:28 +01001508 int quickack;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001509
Willy Tarreau00149122017-10-04 18:05:01 +02001510 /* we cannot have a connection here */
1511 if (conn)
1512 return SF_ERR_INTERNAL;
1513
Simon Hormanb00d17a2014-06-13 16:18:16 +09001514 /* tcpcheck send/expect initialisation */
Willy Tarreauf411cce2017-10-04 16:21:19 +02001515 if (check->type == PR_O2_TCPCHK_CHK) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001516 check->current_step = NULL;
Willy Tarreauf411cce2017-10-04 16:21:19 +02001517 tcp_rule = get_first_tcpcheck_rule(check->tcpcheck_rules);
1518 }
Simon Hormanb00d17a2014-06-13 16:18:16 +09001519
1520 /* prepare the check buffer.
1521 * This should not be used if check is the secondary agent check
1522 * of a server as s->proxy->check_req will relate to the
1523 * configuration of the primary check. Similarly, tcp-check uses
1524 * its own strings.
1525 */
1526 if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) {
1527 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
1528
1529 /* we want to check if this host replies to HTTP or SSLv3 requests
1530 * so we'll send the request, and won't wake the checker up now.
1531 */
1532 if ((check->type) == PR_O2_SSL3_CHK) {
1533 /* SSL requires that we put Unix time in the request */
1534 int gmt_time = htonl(date.tv_sec);
1535 memcpy(check->bo->data + 11, &gmt_time, 4);
1536 }
1537 else if ((check->type) == PR_O2_HTTP_CHK) {
1538 if (s->proxy->options2 & PR_O2_CHK_SNDST)
1539 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size));
Cyril Bonté32602d22015-01-30 00:07:07 +01001540 /* prevent HTTP keep-alive when "http-check expect" is used */
1541 if (s->proxy->options2 & PR_O2_EXP_TYPE)
1542 bo_putstr(check->bo, "Connection: close\r\n");
Simon Hormanb00d17a2014-06-13 16:18:16 +09001543 bo_putstr(check->bo, "\r\n");
1544 *check->bo->p = '\0'; /* to make gdb output easier to read */
1545 }
1546 }
1547
James Brown55f9ff12015-10-21 18:19:05 -07001548 if ((check->type & PR_O2_LB_AGENT_CHK) && check->send_string_len) {
1549 bo_putblk(check->bo, check->send_string, check->send_string_len);
1550 }
1551
Willy Tarreauf411cce2017-10-04 16:21:19 +02001552 /* for tcp-checks, the initial connection setup is handled separately as
1553 * it may be sent to a specific port and not to the server's.
1554 */
1555 if (tcp_rule && tcp_rule->action == TCPCHK_ACT_CONNECT) {
1556 tcpcheck_main(check);
1557 return SF_ERR_UP;
1558 }
1559
Simon Hormanb00d17a2014-06-13 16:18:16 +09001560 /* prepare a new connection */
Willy Tarreau00149122017-10-04 18:05:01 +02001561 conn = check->conn = conn_new();
1562 if (!check->conn)
1563 return SF_ERR_RESOURCE;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001564
Simon Horman41f58762015-01-30 11:22:56 +09001565 if (is_addr(&check->addr)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001566 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09001567 conn->addr.to = check->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001568 }
1569 else {
1570 /* we'll connect to the addr on the server */
1571 conn->addr.to = s->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001572 }
1573
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001574 if ((conn->addr.to.ss_family == AF_INET) || (conn->addr.to.ss_family == AF_INET6)) {
1575 int i = 0;
1576
1577 i = srv_check_healthcheck_port(check);
1578 if (i == 0) {
1579 conn->owner = check;
1580 return SF_ERR_CHK_PORT;
1581 }
1582
1583 set_host_port(&conn->addr.to, i);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001584 }
1585
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01001586 proto = protocol_by_family(conn->addr.to.ss_family);
1587
1588 conn_prepare(conn, proto, check->xprt);
1589 conn_attach(conn, check, &check_conn_cb);
1590 conn->target = &s->obj_type;
1591
1592 /* no client address */
1593 clear_addr(&conn->addr.from);
1594
Willy Tarreauf3d34822014-12-08 12:11:28 +01001595 /* only plain tcp-check supports quick ACK */
1596 quickack = check->type == 0 || check->type == PR_O2_TCPCHK_CHK;
1597
Willy Tarreauf411cce2017-10-04 16:21:19 +02001598 if (tcp_rule && tcp_rule->action == TCPCHK_ACT_EXPECT)
1599 quickack = 0;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001600
Willy Tarreaue7dff022015-04-03 01:14:29 +02001601 ret = SF_ERR_INTERNAL;
Olivier Houchardb68fda42017-08-04 18:39:01 +02001602 if (proto && proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01001603 ret = proto->connect(conn, check->type, quickack ? 2 : 0);
Olivier Houchard9130a962017-10-17 17:33:43 +02001604#ifdef USE_OPENSSL
1605 if (s->check.sni)
1606 ssl_sock_set_servername(conn, s->check.sni);
1607#endif
Willy Tarreauf4949772017-05-06 08:45:28 +02001608 if (s->check.send_proxy && !(check->state & CHK_ST_AGENT)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001609 conn->send_proxy_ofs = 1;
1610 conn->flags |= CO_FL_SEND_PROXY;
1611 }
1612
1613 return ret;
1614}
1615
Simon Horman98637e52014-06-20 12:30:16 +09001616static struct list pid_list = LIST_HEAD_INIT(pid_list);
1617static struct pool_head *pool2_pid_list;
1618
1619void block_sigchld(void)
1620{
1621 sigset_t set;
1622 sigemptyset(&set);
1623 sigaddset(&set, SIGCHLD);
Willy Tarreauebc92442016-06-21 17:29:46 +02001624 assert(sigprocmask(SIG_BLOCK, &set, NULL) == 0);
Simon Horman98637e52014-06-20 12:30:16 +09001625}
1626
1627void unblock_sigchld(void)
1628{
1629 sigset_t set;
1630 sigemptyset(&set);
Willy Tarreauebc92442016-06-21 17:29:46 +02001631 sigaddset(&set, SIGCHLD);
1632 assert(sigprocmask(SIG_UNBLOCK, &set, NULL) == 0);
Simon Horman98637e52014-06-20 12:30:16 +09001633}
1634
Simon Horman98637e52014-06-20 12:30:16 +09001635static struct pid_list *pid_list_add(pid_t pid, struct task *t)
1636{
1637 struct pid_list *elem;
1638 struct check *check = t->context;
1639
1640 elem = pool_alloc2(pool2_pid_list);
1641 if (!elem)
1642 return NULL;
1643 elem->pid = pid;
1644 elem->t = t;
1645 elem->exited = 0;
1646 check->curpid = elem;
1647 LIST_INIT(&elem->list);
1648 LIST_ADD(&pid_list, &elem->list);
1649 return elem;
1650}
1651
Simon Horman98637e52014-06-20 12:30:16 +09001652static void pid_list_del(struct pid_list *elem)
1653{
1654 struct check *check;
1655
1656 if (!elem)
1657 return;
1658
Simon Horman98637e52014-06-20 12:30:16 +09001659 LIST_DEL(&elem->list);
Simon Horman98637e52014-06-20 12:30:16 +09001660 if (!elem->exited)
1661 kill(elem->pid, SIGTERM);
1662
1663 check = elem->t->context;
1664 check->curpid = NULL;
1665 pool_free2(pool2_pid_list, elem);
1666}
1667
1668/* Called from inside SIGCHLD handler, SIGCHLD is blocked */
1669static void pid_list_expire(pid_t pid, int status)
1670{
1671 struct pid_list *elem;
1672
1673 list_for_each_entry(elem, &pid_list, list) {
1674 if (elem->pid == pid) {
1675 elem->t->expire = now_ms;
1676 elem->status = status;
1677 elem->exited = 1;
Cyril Bonté9dbcfab2014-08-07 01:55:39 +02001678 task_wakeup(elem->t, TASK_WOKEN_IO);
Simon Horman98637e52014-06-20 12:30:16 +09001679 return;
1680 }
1681 }
1682}
1683
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001684static void sigchld_handler(struct sig_handler *sh)
Simon Horman98637e52014-06-20 12:30:16 +09001685{
1686 pid_t pid;
1687 int status;
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001688
Simon Horman98637e52014-06-20 12:30:16 +09001689 while ((pid = waitpid(0, &status, WNOHANG)) > 0)
1690 pid_list_expire(pid, status);
1691}
1692
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001693static int init_pid_list(void)
1694{
Simon Horman98637e52014-06-20 12:30:16 +09001695 if (pool2_pid_list != NULL)
1696 /* Nothing to do */
1697 return 0;
1698
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001699 if (!signal_register_fct(SIGCHLD, sigchld_handler, SIGCHLD)) {
Simon Horman98637e52014-06-20 12:30:16 +09001700 Alert("Failed to set signal handler for external health checks: %s. Aborting.\n",
1701 strerror(errno));
1702 return 1;
1703 }
1704
1705 pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED);
1706 if (pool2_pid_list == NULL) {
1707 Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n",
1708 strerror(errno));
1709 return 1;
1710 }
1711
1712 return 0;
1713}
1714
Cyril Bontéac92a062014-12-27 22:28:38 +01001715/* helper macro to set an environment variable and jump to a specific label on failure. */
1716#define EXTCHK_SETENV(check, envidx, value, fail) { if (extchk_setenv(check, envidx, value)) goto fail; }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001717
1718/*
Cyril Bontéac92a062014-12-27 22:28:38 +01001719 * helper function to allocate enough memory to store an environment variable.
1720 * It will also check that the environment variable is updatable, and silently
1721 * fail if not.
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001722 */
Cyril Bontéac92a062014-12-27 22:28:38 +01001723static int extchk_setenv(struct check *check, int idx, const char *value)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001724{
1725 int len, ret;
Cyril Bontéac92a062014-12-27 22:28:38 +01001726 char *envname;
1727 int vmaxlen;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001728
Cyril Bontéac92a062014-12-27 22:28:38 +01001729 if (idx < 0 || idx >= EXTCHK_SIZE) {
1730 Alert("Illegal environment variable index %d. Aborting.\n", idx);
1731 return 1;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001732 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001733
1734 envname = extcheck_envs[idx].name;
1735 vmaxlen = extcheck_envs[idx].vmaxlen;
1736
1737 /* Check if the environment variable is already set, and silently reject
1738 * the update if this one is not updatable. */
1739 if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx]))
1740 return 0;
1741
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001742 /* Instead of sending NOT_USED, sending an empty value is preferable */
1743 if (strcmp(value, "NOT_USED") == 0) {
1744 value = "";
1745 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001746
1747 len = strlen(envname) + 1;
1748 if (vmaxlen == EXTCHK_SIZE_EVAL_INIT)
1749 len += strlen(value);
1750 else
1751 len += vmaxlen;
1752
1753 if (!check->envp[idx])
1754 check->envp[idx] = malloc(len + 1);
1755
1756 if (!check->envp[idx]) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001757 Alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname);
1758 return 1;
1759 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001760 ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001761 if (ret < 0) {
1762 Alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno));
1763 return 1;
1764 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001765 else if (ret > len) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001766 Alert("Environment variable '%s' was truncated. Aborting.\n", envname);
1767 return 1;
1768 }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001769 return 0;
1770}
Simon Horman98637e52014-06-20 12:30:16 +09001771
1772static int prepare_external_check(struct check *check)
1773{
1774 struct server *s = check->server;
1775 struct proxy *px = s->proxy;
1776 struct listener *listener = NULL, *l;
1777 int i;
Simon Horman98637e52014-06-20 12:30:16 +09001778 const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001779 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001780
1781 list_for_each_entry(l, &px->conf.listeners, by_fe)
1782 /* Use the first INET, INET6 or UNIX listener */
1783 if (l->addr.ss_family == AF_INET ||
1784 l->addr.ss_family == AF_INET6 ||
1785 l->addr.ss_family == AF_UNIX) {
1786 listener = l;
1787 break;
1788 }
1789
Simon Horman98637e52014-06-20 12:30:16 +09001790 check->curpid = NULL;
Cyril Bontéac92a062014-12-27 22:28:38 +01001791 check->envp = calloc((EXTCHK_SIZE + 1), sizeof(char *));
1792 if (!check->envp) {
1793 Alert("Failed to allocate memory for environment variables. Aborting\n");
1794 goto err;
1795 }
Simon Horman98637e52014-06-20 12:30:16 +09001796
Cyril Bontéac92a062014-12-27 22:28:38 +01001797 check->argv = calloc(6, sizeof(char *));
1798 if (!check->argv) {
1799 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001800 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001801 }
Simon Horman98637e52014-06-20 12:30:16 +09001802
1803 check->argv[0] = px->check_command;
1804
Cyril Bonté777be862014-12-02 21:21:35 +01001805 if (!listener) {
1806 check->argv[1] = strdup("NOT_USED");
1807 check->argv[2] = strdup("NOT_USED");
1808 }
1809 else if (listener->addr.ss_family == AF_INET ||
Simon Horman98637e52014-06-20 12:30:16 +09001810 listener->addr.ss_family == AF_INET6) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001811 addr_to_str(&listener->addr, buf, sizeof(buf));
1812 check->argv[1] = strdup(buf);
1813 port_to_str(&listener->addr, buf, sizeof(buf));
1814 check->argv[2] = strdup(buf);
Cyril Bonté777be862014-12-02 21:21:35 +01001815 }
1816 else if (listener->addr.ss_family == AF_UNIX) {
Simon Horman98637e52014-06-20 12:30:16 +09001817 const struct sockaddr_un *un;
1818
1819 un = (struct sockaddr_un *)&listener->addr;
1820 check->argv[1] = strdup(un->sun_path);
1821 check->argv[2] = strdup("NOT_USED");
Cyril Bonté777be862014-12-02 21:21:35 +01001822 }
1823 else {
Cyril Bontéac92a062014-12-27 22:28:38 +01001824 Alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001825 goto err;
1826 }
1827
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001828 addr_to_str(&s->addr, buf, sizeof(buf));
1829 check->argv[3] = strdup(buf);
Willy Tarreau04276f32017-01-06 17:41:29 +01001830
1831 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
1832 snprintf(buf, sizeof(buf), "%u", s->svc_port);
1833 else
1834 *buf = 0;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001835 check->argv[4] = strdup(buf);
Simon Horman98637e52014-06-20 12:30:16 +09001836
Cyril Bontéac92a062014-12-27 22:28:38 +01001837 for (i = 0; i < 5; i++) {
1838 if (!check->argv[i]) {
1839 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001840 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001841 }
1842 }
Simon Horman98637e52014-06-20 12:30:16 +09001843
Cyril Bontéac92a062014-12-27 22:28:38 +01001844 EXTCHK_SETENV(check, EXTCHK_PATH, path, err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001845 /* Add proxy environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001846 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err);
1847 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err);
1848 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err);
1849 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001850 /* Add server environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001851 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err);
1852 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err);
1853 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err);
1854 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err);
1855 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err);
1856 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), err);
1857
1858 /* Ensure that we don't leave any hole in check->envp */
1859 for (i = 0; i < EXTCHK_SIZE; i++)
1860 if (!check->envp[i])
1861 EXTCHK_SETENV(check, i, "", err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001862
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001863 return 1;
Simon Horman98637e52014-06-20 12:30:16 +09001864err:
1865 if (check->envp) {
Cyril Bontéac92a062014-12-27 22:28:38 +01001866 for (i = 0; i < EXTCHK_SIZE; i++)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001867 free(check->envp[i]);
Simon Horman98637e52014-06-20 12:30:16 +09001868 free(check->envp);
1869 check->envp = NULL;
1870 }
1871
1872 if (check->argv) {
1873 for (i = 1; i < 5; i++)
1874 free(check->argv[i]);
1875 free(check->argv);
1876 check->argv = NULL;
1877 }
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001878 return 0;
Simon Horman98637e52014-06-20 12:30:16 +09001879}
1880
Simon Hormanb00d17a2014-06-13 16:18:16 +09001881/*
Simon Horman98637e52014-06-20 12:30:16 +09001882 * establish a server health-check that makes use of a process.
1883 *
1884 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001885 * - SF_ERR_NONE if everything's OK
Willy Tarreaue7dff022015-04-03 01:14:29 +02001886 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
Tim Düsterhus4896c442016-11-29 02:15:19 +01001887 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Horman98637e52014-06-20 12:30:16 +09001888 *
1889 * Blocks and then unblocks SIGCHLD
1890 */
1891static int connect_proc_chk(struct task *t)
1892{
Cyril Bontéac92a062014-12-27 22:28:38 +01001893 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001894 struct check *check = t->context;
1895 struct server *s = check->server;
1896 struct proxy *px = s->proxy;
1897 int status;
1898 pid_t pid;
1899
Willy Tarreaue7dff022015-04-03 01:14:29 +02001900 status = SF_ERR_RESOURCE;
Simon Horman98637e52014-06-20 12:30:16 +09001901
1902 block_sigchld();
1903
1904 pid = fork();
1905 if (pid < 0) {
1906 Alert("Failed to fork process for external health check: %s. Aborting.\n",
1907 strerror(errno));
1908 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1909 goto out;
1910 }
1911 if (pid == 0) {
1912 /* Child */
1913 extern char **environ;
Willy Tarreaub7b24782016-06-21 15:32:29 +02001914 int fd;
1915
1916 /* close all FDs. Keep stdin/stdout/stderr in verbose mode */
1917 fd = (global.mode & (MODE_QUIET|MODE_VERBOSE)) == MODE_QUIET ? 0 : 3;
1918
1919 while (fd < global.rlimit_nofile)
1920 close(fd++);
1921
Simon Horman98637e52014-06-20 12:30:16 +09001922 environ = check->envp;
Cyril Bontéac92a062014-12-27 22:28:38 +01001923 extchk_setenv(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)));
Simon Horman98637e52014-06-20 12:30:16 +09001924 execvp(px->check_command, check->argv);
1925 Alert("Failed to exec process for external health check: %s. Aborting.\n",
1926 strerror(errno));
1927 exit(-1);
1928 }
1929
1930 /* Parent */
1931 if (check->result == CHK_RES_UNKNOWN) {
1932 if (pid_list_add(pid, t) != NULL) {
1933 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1934
1935 if (px->timeout.check && px->timeout.connect) {
1936 int t_con = tick_add(now_ms, px->timeout.connect);
1937 t->expire = tick_first(t->expire, t_con);
1938 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02001939 status = SF_ERR_NONE;
Simon Horman98637e52014-06-20 12:30:16 +09001940 goto out;
1941 }
1942 else {
1943 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1944 }
1945 kill(pid, SIGTERM); /* process creation error */
1946 }
1947 else
1948 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1949
1950out:
1951 unblock_sigchld();
1952 return status;
1953}
1954
1955/*
Willy Tarreau1e62e2a2017-10-04 15:07:02 +02001956 * manages a server health-check that uses an external process. Returns
Willy Tarreaubaaee002006-06-26 02:48:02 +02001957 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1958 */
Simon Horman98637e52014-06-20 12:30:16 +09001959static struct task *process_chk_proc(struct task *t)
1960{
1961 struct check *check = t->context;
1962 struct server *s = check->server;
Simon Horman98637e52014-06-20 12:30:16 +09001963 int rv;
1964 int ret;
1965 int expired = tick_is_expired(t->expire, now_ms);
1966
1967 if (!(check->state & CHK_ST_INPROGRESS)) {
1968 /* no check currently running */
1969 if (!expired) /* woke up too early */
1970 return t;
1971
1972 /* we don't send any health-checks when the proxy is
1973 * stopped, the server should not be checked or the check
1974 * is disabled.
1975 */
1976 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
1977 s->proxy->state == PR_STSTOPPED)
1978 goto reschedule;
1979
1980 /* we'll initiate a new check */
1981 set_server_check_status(check, HCHK_STATUS_START, NULL);
1982
1983 check->state |= CHK_ST_INPROGRESS;
1984
Simon Hormandbf70192015-01-30 11:22:53 +09001985 ret = connect_proc_chk(t);
Willy Tarreaud7c3fbd2017-10-04 15:19:26 +02001986 if (ret == SF_ERR_NONE) {
Willy Tarreau1e62e2a2017-10-04 15:07:02 +02001987 /* the process was forked, we allow up to min(inter,
1988 * timeout.connect) for it to report its status, but
1989 * only when timeout.check is set as it may be to short
1990 * for a full check otherwise.
Simon Horman98637e52014-06-20 12:30:16 +09001991 */
1992 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1993
1994 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1995 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1996 t->expire = tick_first(t->expire, t_con);
1997 }
1998
1999 goto reschedule;
Simon Horman98637e52014-06-20 12:30:16 +09002000 }
2001
Willy Tarreau1e62e2a2017-10-04 15:07:02 +02002002 /* here, we failed to start the check */
Simon Horman98637e52014-06-20 12:30:16 +09002003
2004 check->state &= ~CHK_ST_INPROGRESS;
2005 check_notify_failure(check);
2006
2007 /* we allow up to min(inter, timeout.connect) for a connection
2008 * to establish but only when timeout.check is set
2009 * as it may be to short for a full check otherwise
2010 */
2011 while (tick_is_expired(t->expire, now_ms)) {
2012 int t_con;
2013
2014 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2015 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2016
2017 if (s->proxy->timeout.check)
2018 t->expire = tick_first(t->expire, t_con);
2019 }
2020 }
2021 else {
2022 /* there was a test running.
2023 * First, let's check whether there was an uncaught error,
2024 * which can happen on connect timeout or error.
2025 */
2026 if (check->result == CHK_RES_UNKNOWN) {
2027 /* good connection is enough for pure TCP check */
2028 struct pid_list *elem = check->curpid;
2029 int status = HCHK_STATUS_UNKNOWN;
2030
2031 if (elem->exited) {
2032 status = elem->status; /* Save in case the process exits between use below */
2033 if (!WIFEXITED(status))
2034 check->code = -1;
2035 else
2036 check->code = WEXITSTATUS(status);
2037 if (!WIFEXITED(status) || WEXITSTATUS(status))
2038 status = HCHK_STATUS_PROCERR;
2039 else
2040 status = HCHK_STATUS_PROCOK;
2041 } else if (expired) {
2042 status = HCHK_STATUS_PROCTOUT;
Willy Tarreaudc3d1902014-07-08 00:56:27 +02002043 Warning("kill %d\n", (int)elem->pid);
Simon Horman98637e52014-06-20 12:30:16 +09002044 kill(elem->pid, SIGTERM);
2045 }
2046 set_server_check_status(check, status, NULL);
2047 }
2048
2049 if (check->result == CHK_RES_FAILED) {
2050 /* a failure or timeout detected */
2051 check_notify_failure(check);
2052 }
2053 else if (check->result == CHK_RES_CONDPASS) {
2054 /* check is OK but asks for stopping mode */
2055 check_notify_stopping(check);
2056 }
2057 else if (check->result == CHK_RES_PASSED) {
2058 /* a success was detected */
2059 check_notify_success(check);
2060 }
2061 check->state &= ~CHK_ST_INPROGRESS;
2062
2063 pid_list_del(check->curpid);
2064
2065 rv = 0;
2066 if (global.spread_checks > 0) {
2067 rv = srv_getinter(check) * global.spread_checks / 100;
2068 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
2069 }
2070 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
2071 }
2072
2073 reschedule:
2074 while (tick_is_expired(t->expire, now_ms))
2075 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2076 return t;
2077}
2078
2079/*
2080 * manages a server health-check that uses a connection. Returns
2081 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2082 */
2083static struct task *process_chk_conn(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002084{
Simon Horman4a741432013-02-23 15:35:38 +09002085 struct check *check = t->context;
2086 struct server *s = check->server;
2087 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002088 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002089 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002090 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002091
Willy Tarreau2c115e52013-12-11 19:41:16 +01002092 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01002093 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002094 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01002095 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002096
Simon Horman671b6f02013-11-25 10:46:39 +09002097 /* we don't send any health-checks when the proxy is
2098 * stopped, the server should not be checked or the check
2099 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002100 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01002101 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01002102 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01002103 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002104
2105 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09002106 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002107
Willy Tarreau2c115e52013-12-11 19:41:16 +01002108 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09002109 check->bi->p = check->bi->data;
2110 check->bi->i = 0;
2111 check->bo->p = check->bo->data;
2112 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002113
Simon Hormandbf70192015-01-30 11:22:53 +09002114 ret = connect_conn_chk(t);
Willy Tarreau00149122017-10-04 18:05:01 +02002115 conn = check->conn;
2116
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002117 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002118 case SF_ERR_UP:
Simon Hormanb00d17a2014-06-13 16:18:16 +09002119 return t;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002120 case SF_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002121 /* we allow up to min(inter, timeout.connect) for a connection
2122 * to establish but only when timeout.check is set
2123 * as it may be to short for a full check otherwise
2124 */
Simon Horman4a741432013-02-23 15:35:38 +09002125 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002126
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002127 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2128 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2129 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002130 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01002131
2132 if (check->type)
2133 conn_data_want_recv(conn); /* prepare for reading a possible reply */
2134
Willy Tarreau5a78f362012-11-23 12:47:05 +01002135 goto reschedule;
2136
Willy Tarreaue7dff022015-04-03 01:14:29 +02002137 case SF_ERR_SRVTO: /* ETIMEDOUT */
2138 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau00149122017-10-04 18:05:01 +02002139 if (conn)
2140 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002141 chk_report_conn_err(check, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002142 break;
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02002143 /* should share same code than cases below */
2144 case SF_ERR_CHK_PORT:
2145 check->state |= CHK_ST_PORT_MISS;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002146 case SF_ERR_PRXCOND:
2147 case SF_ERR_RESOURCE:
2148 case SF_ERR_INTERNAL:
Willy Tarreau00149122017-10-04 18:05:01 +02002149 if (conn)
2150 conn->flags |= CO_FL_ERROR;
2151 chk_report_conn_err(check, conn ? 0 : ENOMEM, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002152 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002153 }
2154
Willy Tarreau5a78f362012-11-23 12:47:05 +01002155 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01002156
Willy Tarreau2c115e52013-12-11 19:41:16 +01002157 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02002158 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002159
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002160 /* we allow up to min(inter, timeout.connect) for a connection
2161 * to establish but only when timeout.check is set
2162 * as it may be to short for a full check otherwise
2163 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002164 while (tick_is_expired(t->expire, now_ms)) {
2165 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002166
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002167 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09002168 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002169
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002170 if (s->proxy->timeout.check)
2171 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002172 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002173 }
2174 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02002175 /* there was a test running.
2176 * First, let's check whether there was an uncaught error,
2177 * which can happen on connect timeout or error.
2178 */
Simon Hormanccaabcd2014-06-20 12:29:47 +09002179 if (check->result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002180 /* good connection is enough for pure TCP check */
2181 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09002182 if (check->use_ssl)
2183 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02002184 else
Simon Horman4a741432013-02-23 15:35:38 +09002185 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002186 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002187 else if ((conn->flags & CO_FL_ERROR) || expired) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002188 chk_report_conn_err(check, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02002189 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002190 else
2191 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02002192 }
2193
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002194 /* check complete or aborted */
Willy Tarreau00149122017-10-04 18:05:01 +02002195 if (conn && conn->xprt) {
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002196 /* The check was aborted and the connection was not yet closed.
2197 * This can happen upon timeout, or when an external event such
2198 * as a failed response coupled with "observe layer7" caused the
2199 * server state to be suddenly changed.
2200 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01002201 conn_sock_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02002202 conn_force_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002203 }
2204
Willy Tarreau00149122017-10-04 18:05:01 +02002205 if (conn) {
2206 conn_free(conn);
2207 check->conn = conn = NULL;
2208 }
2209
Willy Tarreauaf549582014-05-16 17:37:50 +02002210 if (check->result == CHK_RES_FAILED) {
2211 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002212 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002213 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002214 else if (check->result == CHK_RES_CONDPASS) {
2215 /* check is OK but asks for stopping mode */
2216 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002217 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002218 else if (check->result == CHK_RES_PASSED) {
2219 /* a success was detected */
2220 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002221 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01002222 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002223
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002224 rv = 0;
2225 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002226 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002227 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002228 }
Simon Horman4a741432013-02-23 15:35:38 +09002229 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002230 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002231
2232 reschedule:
2233 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002234 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002235 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01002236 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002237}
2238
Simon Horman98637e52014-06-20 12:30:16 +09002239/*
2240 * manages a server health-check. Returns
2241 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2242 */
2243static struct task *process_chk(struct task *t)
2244{
2245 struct check *check = t->context;
2246
2247 if (check->type == PR_O2_EXT_CHK)
2248 return process_chk_proc(t);
2249 return process_chk_conn(t);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002250
Simon Horman98637e52014-06-20 12:30:16 +09002251}
2252
Simon Horman5c942422013-11-25 10:46:32 +09002253static int start_check_task(struct check *check, int mininter,
2254 int nbcheck, int srvpos)
2255{
2256 struct task *t;
2257 /* task for the check */
2258 if ((t = task_new()) == NULL) {
2259 Alert("Starting [%s:%s] check: out of memory.\n",
2260 check->server->proxy->id, check->server->id);
2261 return 0;
2262 }
2263
2264 check->task = t;
2265 t->process = process_chk;
2266 t->context = check;
2267
Willy Tarreau1746eec2014-04-25 10:46:47 +02002268 if (mininter < srv_getinter(check))
2269 mininter = srv_getinter(check);
2270
2271 if (global.max_spread_checks && mininter > global.max_spread_checks)
2272 mininter = global.max_spread_checks;
2273
Simon Horman5c942422013-11-25 10:46:32 +09002274 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002275 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002276 check->start = now;
2277 task_queue(t);
2278
2279 return 1;
2280}
2281
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002282/*
2283 * Start health-check.
Willy Tarreau865c5142016-12-21 20:04:48 +01002284 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002285 */
Willy Tarreau865c5142016-12-21 20:04:48 +01002286static int start_checks()
2287{
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002288
2289 struct proxy *px;
2290 struct server *s;
2291 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002292 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002293
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002294 /* 1- count the checkers to run simultaneously.
2295 * We also determine the minimum interval among all of those which
2296 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2297 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002298 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002299 * too short an interval for all others.
2300 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002301 for (px = proxy; px; px = px->next) {
2302 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002303 if (s->slowstart) {
2304 if ((t = task_new()) == NULL) {
2305 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Willy Tarreau865c5142016-12-21 20:04:48 +01002306 return ERR_ALERT | ERR_FATAL;
Willy Tarreaue7b73482013-11-21 11:50:50 +01002307 }
2308 /* We need a warmup task that will be called when the server
2309 * state switches from down to up.
2310 */
2311 s->warmup = t;
2312 t->process = server_warmup;
2313 t->context = s;
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002314 /* server can be in this state only because of */
Emeric Brun52a91d32017-08-31 14:41:55 +02002315 if (s->next_state == SRV_ST_STARTING)
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002316 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, (now.tv_sec - s->last_change)) / 20)));
Willy Tarreaue7b73482013-11-21 11:50:50 +01002317 }
2318
Willy Tarreaud8514a22013-12-11 21:10:14 +01002319 if (s->check.state & CHK_ST_CONFIGURED) {
2320 nbcheck++;
2321 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2322 (!mininter || mininter > srv_getinter(&s->check)))
2323 mininter = srv_getinter(&s->check);
2324 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002325
Willy Tarreaud8514a22013-12-11 21:10:14 +01002326 if (s->agent.state & CHK_ST_CONFIGURED) {
2327 nbcheck++;
2328 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2329 (!mininter || mininter > srv_getinter(&s->agent)))
2330 mininter = srv_getinter(&s->agent);
2331 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002332 }
2333 }
2334
Simon Horman4a741432013-02-23 15:35:38 +09002335 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002336 return 0;
2337
2338 srand((unsigned)time(NULL));
2339
2340 /*
2341 * 2- start them as far as possible from each others. For this, we will
2342 * start them after their interval set to the min interval divided by
2343 * the number of servers, weighted by the server's position in the list.
2344 */
2345 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002346 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2347 if (init_pid_list()) {
2348 Alert("Starting [%s] check: out of memory.\n", px->id);
Willy Tarreau865c5142016-12-21 20:04:48 +01002349 return ERR_ALERT | ERR_FATAL;
Simon Horman98637e52014-06-20 12:30:16 +09002350 }
2351 }
2352
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002353 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002354 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002355 if (s->check.state & CHK_ST_CONFIGURED) {
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002356 if (s->check.type == PR_O2_EXT_CHK) {
2357 if (!prepare_external_check(&s->check))
Willy Tarreau865c5142016-12-21 20:04:48 +01002358 return ERR_ALERT | ERR_FATAL;
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002359 }
Simon Hormand60d6912013-11-25 10:46:36 +09002360 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
Willy Tarreau865c5142016-12-21 20:04:48 +01002361 return ERR_ALERT | ERR_FATAL;
Simon Hormand60d6912013-11-25 10:46:36 +09002362 srvpos++;
2363 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002364
Simon Hormand60d6912013-11-25 10:46:36 +09002365 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002366 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002367 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
Willy Tarreau865c5142016-12-21 20:04:48 +01002368 return ERR_ALERT | ERR_FATAL;
Simon Hormand60d6912013-11-25 10:46:36 +09002369 }
2370 srvpos++;
2371 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002372 }
2373 }
2374 return 0;
2375}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002376
2377/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002378 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002379 * The buffer MUST be terminated by a null byte before calling this function.
2380 * Sets server status appropriately. The caller is responsible for ensuring
2381 * that the buffer contains at least 13 characters. If <done> is zero, we may
2382 * return 0 to indicate that data is required to decide of a match.
2383 */
2384static int httpchk_expect(struct server *s, int done)
2385{
2386 static char status_msg[] = "HTTP status check returned code <000>";
2387 char status_code[] = "000";
2388 char *contentptr;
2389 int crlf;
2390 int ret;
2391
2392 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2393 case PR_O2_EXP_STS:
2394 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002395 memcpy(status_code, s->check.bi->data + 9, 3);
2396 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002397
2398 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2399 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2400 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002401 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002402
2403 /* we necessarily have the response, so there are no partial failures */
2404 if (s->proxy->options2 & PR_O2_EXP_INV)
2405 ret = !ret;
2406
Simon Horman4a741432013-02-23 15:35:38 +09002407 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002408 break;
2409
2410 case PR_O2_EXP_STR:
2411 case PR_O2_EXP_RSTR:
2412 /* very simple response parser: ignore CR and only count consecutive LFs,
2413 * stop with contentptr pointing to first char after the double CRLF or
2414 * to '\0' if crlf < 2.
2415 */
2416 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002417 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002418 if (crlf >= 2)
2419 break;
2420 if (*contentptr == '\r')
2421 continue;
2422 else if (*contentptr == '\n')
2423 crlf++;
2424 else
2425 crlf = 0;
2426 }
2427
2428 /* Check that response contains a body... */
2429 if (crlf < 2) {
2430 if (!done)
2431 return 0;
2432
Simon Horman4a741432013-02-23 15:35:38 +09002433 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002434 "HTTP content check could not find a response body");
2435 return 1;
2436 }
2437
2438 /* Check that response body is not empty... */
2439 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002440 if (!done)
2441 return 0;
2442
Simon Horman4a741432013-02-23 15:35:38 +09002443 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002444 "HTTP content check found empty response body");
2445 return 1;
2446 }
2447
2448 /* Check the response content against the supplied string
2449 * or regex... */
2450 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2451 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2452 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002453 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002454
2455 /* if we don't match, we may need to wait more */
2456 if (!ret && !done)
2457 return 0;
2458
2459 if (ret) {
2460 /* content matched */
2461 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002462 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002463 "HTTP check matched unwanted content");
2464 else
Simon Horman4a741432013-02-23 15:35:38 +09002465 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002466 "HTTP content check matched");
2467 }
2468 else {
2469 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002470 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002471 "HTTP check did not match unwanted content");
2472 else
Simon Horman4a741432013-02-23 15:35:38 +09002473 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002474 "HTTP content check did not match");
2475 }
2476 break;
2477 }
2478 return 1;
2479}
2480
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002481/*
2482 * return the id of a step in a send/expect session
2483 */
Simon Hormane16c1b32015-01-30 11:22:57 +09002484static int tcpcheck_get_step_id(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002485{
2486 struct tcpcheck_rule *cur = NULL, *next = NULL;
2487 int i = 0;
2488
Willy Tarreau213c6782014-10-02 14:51:02 +02002489 /* not even started anything yet => step 0 = initial connect */
Baptiste Assmannf95bc8e2015-04-25 16:03:06 +02002490 if (!check->current_step)
Willy Tarreau213c6782014-10-02 14:51:02 +02002491 return 0;
2492
Simon Hormane16c1b32015-01-30 11:22:57 +09002493 cur = check->last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002494
2495 /* no step => first step */
2496 if (cur == NULL)
2497 return 1;
2498
2499 /* increment i until current step */
Simon Hormane16c1b32015-01-30 11:22:57 +09002500 list_for_each_entry(next, check->tcpcheck_rules, list) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002501 if (next->list.p == &cur->list)
2502 break;
2503 ++i;
2504 }
2505
2506 return i;
2507}
2508
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002509/*
2510 * return the latest known comment before (including) the given stepid
2511 * returns NULL if no comment found
2512 */
2513static char * tcpcheck_get_step_comment(struct check *check, int stepid)
2514{
2515 struct tcpcheck_rule *cur = NULL;
2516 char *ret = NULL;
2517 int i = 0;
2518
2519 /* not even started anything yet, return latest comment found before any action */
2520 if (!check->current_step) {
2521 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2522 if (cur->action == TCPCHK_ACT_COMMENT)
2523 ret = cur->comment;
2524 else
2525 goto return_comment;
2526 }
2527 }
2528
2529 i = 1;
2530 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2531 if (cur->comment)
2532 ret = cur->comment;
2533
2534 if (i >= stepid)
2535 goto return_comment;
2536
2537 ++i;
2538 }
2539
2540 return_comment:
2541 return ret;
2542}
2543
Willy Tarreaube74b882017-10-04 16:22:49 +02002544/* proceed with next steps for the TCP checks <check>. Note that this is called
2545 * both from the connection's wake() callback and from the check scheduling task.
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002546 * It returns 0 on normal cases, or <0 if a close() has happened on an existing
2547 * connection, presenting the risk of an fd replacement.
Willy Tarreaube74b882017-10-04 16:22:49 +02002548 */
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002549static int tcpcheck_main(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002550{
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002551 char *contentptr, *comment;
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002552 struct tcpcheck_rule *next;
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002553 int done = 0, ret = 0, step = 0;
Willy Tarreaube74b882017-10-04 16:22:49 +02002554 struct connection *conn = check->conn;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002555 struct server *s = check->server;
2556 struct task *t = check->task;
Simon Hormane16c1b32015-01-30 11:22:57 +09002557 struct list *head = check->tcpcheck_rules;
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002558 int retcode = 0;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002559
Willy Tarreauef953952014-10-02 14:30:14 +02002560 /* here, we know that the check is complete or that it failed */
2561 if (check->result != CHK_RES_UNKNOWN)
2562 goto out_end_tcpcheck;
2563
2564 /* We have 4 possibilities here :
2565 * 1. we've not yet attempted step 1, and step 1 is a connect, so no
Willy Tarreau00149122017-10-04 18:05:01 +02002566 * connection attempt was made yet ; conn==NULL;current_step==NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002567 * 2. we've not yet attempted step 1, and step 1 is a not connect or
2568 * does not exist (no rule), so a connection attempt was made
Willy Tarreau00149122017-10-04 18:05:01 +02002569 * before coming here, conn!=NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002570 * 3. we're coming back after having started with step 1, so we may
Willy Tarreau00149122017-10-04 18:05:01 +02002571 * be waiting for a connection attempt to complete. conn!=NULL.
2572 * 4. the connection + handshake are complete. conn!=NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002573 *
2574 * #2 and #3 are quite similar, we want both the connection and the
2575 * handshake to complete before going any further. Thus we must always
2576 * wait for a connection to complete unless we're before and existing
2577 * step 1.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002578 */
Willy Tarreau449f9522015-05-13 15:39:48 +02002579
2580 /* find first rule and skip comments */
2581 next = LIST_NEXT(head, struct tcpcheck_rule *, list);
2582 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
2583 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
2584
Willy Tarreau00149122017-10-04 18:05:01 +02002585 if ((check->current_step || &next->list == head) &&
2586 (!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE))) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002587 /* we allow up to min(inter, timeout.connect) for a connection
2588 * to establish but only when timeout.check is set
2589 * as it may be to short for a full check otherwise
2590 */
2591 while (tick_is_expired(t->expire, now_ms)) {
2592 int t_con;
2593
2594 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2595 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2596
2597 if (s->proxy->timeout.check)
2598 t->expire = tick_first(t->expire, t_con);
2599 }
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002600 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002601 }
2602
Willy Tarreauef953952014-10-02 14:30:14 +02002603 /* special case: option tcp-check with no rule, a connect is enough */
Willy Tarreau449f9522015-05-13 15:39:48 +02002604 if (&next->list == head) {
Willy Tarreauef953952014-10-02 14:30:14 +02002605 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002606 goto out_end_tcpcheck;
Willy Tarreauef953952014-10-02 14:30:14 +02002607 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002608
Willy Tarreau213c6782014-10-02 14:51:02 +02002609 /* no step means first step initialisation */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002610 if (check->current_step == NULL) {
Willy Tarreau213c6782014-10-02 14:51:02 +02002611 check->last_started_step = NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002612 check->bo->p = check->bo->data;
2613 check->bo->o = 0;
2614 check->bi->p = check->bi->data;
2615 check->bi->i = 0;
Willy Tarreau449f9522015-05-13 15:39:48 +02002616 check->current_step = next;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002617 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2618 if (s->proxy->timeout.check)
2619 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2620 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002621
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002622 /* It's only the rules which will enable send/recv */
Willy Tarreau00149122017-10-04 18:05:01 +02002623 if (conn)
2624 __conn_data_stop_both(conn);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002625
Willy Tarreauabca5b62013-12-06 14:19:25 +01002626 while (1) {
Willy Tarreau263013d2015-05-13 11:59:14 +02002627 /* We have to try to flush the output buffer before reading, at
2628 * the end, or if we're about to send a string that does not fit
2629 * in the remaining space. That explains why we break out of the
Willy Tarreau00149122017-10-04 18:05:01 +02002630 * loop after this control. If we have data, conn is valid.
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002631 */
2632 if (check->bo->o &&
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002633 (&check->current_step->list == head ||
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002634 check->current_step->action != TCPCHK_ACT_SEND ||
2635 check->current_step->string_len >= buffer_total_space(check->bo))) {
2636
Willy Tarreaua3782e72017-10-04 08:45:19 +02002637 __conn_data_want_send(conn);
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002638 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002639 if (conn->flags & CO_FL_ERROR) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002640 chk_report_conn_err(check, errno, 0);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002641 __conn_data_stop_both(conn);
2642 goto out_end_tcpcheck;
2643 }
Willy Tarreau263013d2015-05-13 11:59:14 +02002644 break;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002645 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002646 }
2647
Willy Tarreau263013d2015-05-13 11:59:14 +02002648 if (&check->current_step->list == head)
Willy Tarreauabca5b62013-12-06 14:19:25 +01002649 break;
2650
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002651 /* have 'next' point to the next rule or NULL if we're on the
2652 * last one, connect() needs this.
2653 */
Willy Tarreau5581c272015-05-13 12:24:53 +02002654 next = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002655
2656 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002657 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002658 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002659
2660 /* NULL if we're on the last rule */
Willy Tarreauf3d34822014-12-08 12:11:28 +01002661 if (&next->list == head)
2662 next = NULL;
2663
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002664 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2665 struct protocol *proto;
2666 struct xprt_ops *xprt;
2667
Willy Tarreau00149122017-10-04 18:05:01 +02002668 /* For a connect action we'll create a new connection.
2669 * We may also have to kill a previous one. But we don't
2670 * want to leave *without* a connection if we came here
2671 * from the connection layer, hence with a connection.
2672 * Thus we'll proceed in the following order :
2673 * 1: close but not release previous connection
2674 * 2: try to get a new connection
2675 * 3: release and replace the old one on success
2676 */
2677 if (check->conn) {
2678 conn_force_close(check->conn);
2679 retcode = -1; /* do not reuse the fd! */
2680 }
2681
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002682 /* mark the step as started */
2683 check->last_started_step = check->current_step;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002684
2685 /* prepare new connection */
Willy Tarreau00149122017-10-04 18:05:01 +02002686 conn = conn_new();
2687 if (!conn) {
2688 step = tcpcheck_get_step_id(check);
2689 chunk_printf(&trash, "TCPCHK error allocating connection at step %d", step);
2690 comment = tcpcheck_get_step_comment(check, step);
2691 if (comment)
2692 chunk_appendf(&trash, " comment: '%s'", comment);
2693 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2694 check->current_step = NULL;
2695 return retcode;
2696 }
2697
2698 if (check->conn)
2699 conn_free(check->conn);
2700 check->conn = conn;
2701
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002702 conn_attach(conn, check, &check_conn_cb);
2703 conn->target = &s->obj_type;
2704
2705 /* no client address */
2706 clear_addr(&conn->addr.from);
2707
Simon Horman41f58762015-01-30 11:22:56 +09002708 if (is_addr(&check->addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002709 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09002710 conn->addr.to = check->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002711 }
2712 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002713 /* we'll connect to the addr on the server */
2714 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002715 }
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01002716 proto = protocol_by_family(conn->addr.to.ss_family);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002717
2718 /* port */
2719 if (check->current_step->port)
2720 set_host_port(&conn->addr.to, check->current_step->port);
2721 else if (check->port)
2722 set_host_port(&conn->addr.to, check->port);
2723
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002724 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002725 xprt = xprt_get(XPRT_SSL);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002726 }
2727 else {
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002728 xprt = xprt_get(XPRT_RAW);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002729 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002730 conn_prepare(conn, proto, xprt);
2731
Willy Tarreaue7dff022015-04-03 01:14:29 +02002732 ret = SF_ERR_INTERNAL;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002733 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01002734 ret = proto->connect(conn,
2735 1 /* I/O polling is always needed */,
2736 (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002737 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2738 conn->send_proxy_ofs = 1;
2739 conn->flags |= CO_FL_SEND_PROXY;
2740 }
2741
2742 /* It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02002743 * - SF_ERR_NONE if everything's OK
2744 * - SF_ERR_SRVTO if there are no more servers
2745 * - SF_ERR_SRVCL if the connection was refused by the server
2746 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2747 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2748 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +01002749 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002750 * Note that we try to prevent the network stack from sending the ACK during the
2751 * connect() when a pure TCP check is used (without PROXY protocol).
2752 */
2753 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002754 case SF_ERR_NONE:
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002755 /* we allow up to min(inter, timeout.connect) for a connection
2756 * to establish but only when timeout.check is set
2757 * as it may be to short for a full check otherwise
2758 */
2759 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2760
2761 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2762 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2763 t->expire = tick_first(t->expire, t_con);
2764 }
2765 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002766 case SF_ERR_SRVTO: /* ETIMEDOUT */
2767 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002768 step = tcpcheck_get_step_id(check);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002769 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002770 step, strerror(errno));
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002771 comment = tcpcheck_get_step_comment(check, step);
2772 if (comment)
2773 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002774 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2775 goto out_end_tcpcheck;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002776 case SF_ERR_PRXCOND:
2777 case SF_ERR_RESOURCE:
2778 case SF_ERR_INTERNAL:
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002779 step = tcpcheck_get_step_id(check);
2780 chunk_printf(&trash, "TCPCHK error establishing connection at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002781 comment = tcpcheck_get_step_comment(check, step);
2782 if (comment)
2783 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002784 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2785 goto out_end_tcpcheck;
2786 }
2787
2788 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002789 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002790
2791 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002792 while (&check->current_step->list != head &&
2793 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002794 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002795
Willy Tarreauf2c87352015-05-13 12:08:21 +02002796 if (&check->current_step->list == head)
2797 break;
2798
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002799 /* don't do anything until the connection is established */
2800 if (!(conn->flags & CO_FL_CONNECTED)) {
2801 /* update expire time, should be done by process_chk */
2802 /* we allow up to min(inter, timeout.connect) for a connection
2803 * to establish but only when timeout.check is set
2804 * as it may be to short for a full check otherwise
2805 */
2806 while (tick_is_expired(t->expire, now_ms)) {
2807 int t_con;
2808
2809 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2810 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2811
2812 if (s->proxy->timeout.check)
2813 t->expire = tick_first(t->expire, t_con);
2814 }
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002815 return retcode;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002816 }
2817
2818 } /* end 'connect' */
2819 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2820 /* mark the step as started */
2821 check->last_started_step = check->current_step;
2822
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002823 /* reset the read buffer */
2824 if (*check->bi->data != '\0') {
2825 *check->bi->data = '\0';
2826 check->bi->i = 0;
2827 }
2828
Willy Tarreaubbae3f02017-08-30 09:59:52 +02002829 if (conn->flags & CO_FL_SOCK_WR_SH) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002830 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002831 chk_report_conn_err(check, 0, 0);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002832 goto out_end_tcpcheck;
2833 }
2834
Willy Tarreauabca5b62013-12-06 14:19:25 +01002835 if (check->current_step->string_len >= check->bo->size) {
2836 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2837 check->current_step->string_len, check->bo->size,
Simon Hormane16c1b32015-01-30 11:22:57 +09002838 tcpcheck_get_step_id(check));
Willy Tarreauabca5b62013-12-06 14:19:25 +01002839 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2840 goto out_end_tcpcheck;
2841 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002842
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002843 /* do not try to send if there is no space */
2844 if (check->current_step->string_len >= buffer_total_space(check->bo))
2845 continue;
2846
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002847 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2848 *check->bo->p = '\0'; /* to make gdb output easier to read */
2849
Willy Tarreauabca5b62013-12-06 14:19:25 +01002850 /* go to next rule and try to send */
Willy Tarreau5581c272015-05-13 12:24:53 +02002851 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002852
2853 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002854 while (&check->current_step->list != head &&
2855 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002856 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Willy Tarreauf2c87352015-05-13 12:08:21 +02002857
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002858 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002859 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002860 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002861 goto out_end_tcpcheck;
2862
Willy Tarreaua3782e72017-10-04 08:45:19 +02002863 __conn_data_want_recv(conn);
Willy Tarreau310987a2014-01-22 19:46:33 +01002864 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreau54e917c2017-08-30 07:35:35 +02002865 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002866 done = 1;
2867 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2868 /* Report network errors only if we got no other data. Otherwise
2869 * we'll let the upper layers decide whether the response is OK
2870 * or not. It is very common that an RST sent by the server is
2871 * reported as an error just after the last data chunk.
2872 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002873 chk_report_conn_err(check, errno, 0);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002874 goto out_end_tcpcheck;
2875 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002876 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002877 else
Willy Tarreau263013d2015-05-13 11:59:14 +02002878 break;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002879 }
2880
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002881 /* mark the step as started */
2882 check->last_started_step = check->current_step;
2883
2884
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002885 /* Intermediate or complete response received.
2886 * Terminate string in check->bi->data buffer.
2887 */
2888 if (check->bi->i < check->bi->size) {
2889 check->bi->data[check->bi->i] = '\0';
2890 }
2891 else {
2892 check->bi->data[check->bi->i - 1] = '\0';
2893 done = 1; /* buffer full, don't wait for more data */
2894 }
2895
2896 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002897
2898 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002899 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002900 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002901 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002902
2903 /* empty response */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002904 step = tcpcheck_get_step_id(check);
2905 chunk_printf(&trash, "TCPCHK got an empty response at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002906 comment = tcpcheck_get_step_comment(check, step);
2907 if (comment)
2908 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002909 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2910
2911 goto out_end_tcpcheck;
2912 }
2913
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002914 if (!done && (check->current_step->string != NULL) && (check->bi->i < check->current_step->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002915 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002916
Willy Tarreaua970c282013-12-06 12:47:19 +01002917 tcpcheck_expect:
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002918 if (check->current_step->string != NULL)
2919 ret = my_memmem(contentptr, check->bi->i, check->current_step->string, check->current_step->string_len) != NULL;
2920 else if (check->current_step->expect_regex != NULL)
2921 ret = regex_exec(check->current_step->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002922
2923 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002924 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002925
2926 /* matched */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002927 step = tcpcheck_get_step_id(check);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002928 if (ret) {
2929 /* matched but we did not want to => ERROR */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002930 if (check->current_step->inverse) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002931 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002932 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002933 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002934 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002935 }
2936 else {
2937 /* we were looking for a regex */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002938 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d", step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002939 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002940 comment = tcpcheck_get_step_comment(check, step);
2941 if (comment)
2942 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002943 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2944 goto out_end_tcpcheck;
2945 }
2946 /* matched and was supposed to => OK, next step */
2947 else {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002948 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002949 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002950
2951 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002952 while (&check->current_step->list != head &&
2953 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002954 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002955
Willy Tarreauf2c87352015-05-13 12:08:21 +02002956 if (&check->current_step->list == head)
2957 break;
2958
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002959 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002960 goto tcpcheck_expect;
2961 __conn_data_stop_recv(conn);
2962 }
2963 }
2964 else {
2965 /* not matched */
2966 /* not matched and was not supposed to => OK, next step */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002967 if (check->current_step->inverse) {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002968 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002969 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002970
2971 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002972 while (&check->current_step->list != head &&
2973 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002974 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002975
Willy Tarreauf2c87352015-05-13 12:08:21 +02002976 if (&check->current_step->list == head)
2977 break;
2978
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002979 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002980 goto tcpcheck_expect;
2981 __conn_data_stop_recv(conn);
2982 }
2983 /* not matched but was supposed to => ERROR */
2984 else {
2985 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002986 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002987 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002988 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002989 }
2990 else {
2991 /* we were looking for a regex */
2992 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002993 step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002994 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002995 comment = tcpcheck_get_step_comment(check, step);
2996 if (comment)
2997 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002998 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2999 goto out_end_tcpcheck;
3000 }
3001 }
3002 } /* end expect */
3003 } /* end loop over double chained step list */
3004
Willy Tarreau263013d2015-05-13 11:59:14 +02003005 /* We're waiting for some I/O to complete, we've reached the end of the
3006 * rules, or both. Do what we have to do, otherwise we're done.
3007 */
3008 if (&check->current_step->list == head && !check->bo->o) {
3009 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
3010 goto out_end_tcpcheck;
3011 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003012
Willy Tarreau53c5a042015-05-13 11:38:17 +02003013 /* warning, current_step may now point to the head */
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01003014 if (check->bo->o)
3015 __conn_data_want_send(conn);
3016
Willy Tarreau53c5a042015-05-13 11:38:17 +02003017 if (&check->current_step->list != head &&
3018 check->current_step->action == TCPCHK_ACT_EXPECT)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01003019 __conn_data_want_recv(conn);
Willy Tarreau6bdcab02017-10-04 18:41:00 +02003020 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003021
3022 out_end_tcpcheck:
3023 /* collect possible new errors */
3024 if (conn->flags & CO_FL_ERROR)
Willy Tarreaub5259bf2017-10-04 14:47:29 +02003025 chk_report_conn_err(check, 0, 0);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003026
Baptiste Assmann69e273f2013-12-11 00:52:19 +01003027 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003028 check->current_step = NULL;
3029
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01003030 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003031 conn->flags |= CO_FL_ERROR;
3032
3033 __conn_data_stop_both(conn);
Willy Tarreau6bdcab02017-10-04 18:41:00 +02003034 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003035}
3036
Simon Hormanb1900d52015-01-30 11:22:54 +09003037const char *init_check(struct check *check, int type)
3038{
3039 check->type = type;
3040
3041 /* Allocate buffer for requests... */
3042 if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3043 return "out of memory while allocating check buffer";
3044 }
3045 check->bi->size = global.tune.chksize;
3046
3047 /* Allocate buffer for responses... */
3048 if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3049 return "out of memory while allocating check buffer";
3050 }
3051 check->bo->size = global.tune.chksize;
3052
Simon Hormanb1900d52015-01-30 11:22:54 +09003053 return NULL;
3054}
3055
Simon Hormanbfb5d332015-01-30 11:22:55 +09003056void free_check(struct check *check)
3057{
3058 free(check->bi);
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01003059 check->bi = NULL;
Simon Hormanbfb5d332015-01-30 11:22:55 +09003060 free(check->bo);
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01003061 check->bo = NULL;
Simon Hormanbfb5d332015-01-30 11:22:55 +09003062 free(check->conn);
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01003063 check->conn = NULL;
Simon Hormanbfb5d332015-01-30 11:22:55 +09003064}
3065
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003066void email_alert_free(struct email_alert *alert)
3067{
3068 struct tcpcheck_rule *rule, *back;
3069
3070 if (!alert)
3071 return;
3072
3073 list_for_each_entry_safe(rule, back, &alert->tcpcheck_rules, list)
3074 free(rule);
3075 free(alert);
3076}
3077
3078static struct task *process_email_alert(struct task *t)
3079{
3080 struct check *check = t->context;
3081 struct email_alertq *q;
3082
3083 q = container_of(check, typeof(*q), check);
3084
3085 if (!(check->state & CHK_ST_ENABLED)) {
3086 if (LIST_ISEMPTY(&q->email_alerts)) {
3087 /* All alerts processed, delete check */
3088 task_delete(t);
3089 task_free(t);
3090 check->task = NULL;
3091 return NULL;
3092 } else {
3093 struct email_alert *alert;
3094
3095 alert = LIST_NEXT(&q->email_alerts, typeof(alert), list);
3096 check->tcpcheck_rules = &alert->tcpcheck_rules;
3097 LIST_DEL(&alert->list);
3098
3099 check->state |= CHK_ST_ENABLED;
3100 }
3101
3102 }
3103
3104 process_chk(t);
3105
3106 if (!(check->state & CHK_ST_INPROGRESS) && check->tcpcheck_rules) {
3107 struct email_alert *alert;
3108
3109 alert = container_of(check->tcpcheck_rules, typeof(*alert), tcpcheck_rules);
3110 email_alert_free(alert);
3111
3112 check->tcpcheck_rules = NULL;
3113 check->state &= ~CHK_ST_ENABLED;
3114 }
3115 return t;
3116}
3117
3118static int init_email_alert_checks(struct server *s)
3119{
3120 int i;
3121 struct mailer *mailer;
3122 const char *err_str;
3123 struct proxy *p = s->proxy;
3124
3125 if (p->email_alert.queues)
3126 /* Already initialised, nothing to do */
3127 return 1;
3128
3129 p->email_alert.queues = calloc(p->email_alert.mailers.m->count, sizeof *p->email_alert.queues);
3130 if (!p->email_alert.queues) {
3131 err_str = "out of memory while allocating checks array";
3132 goto error_alert;
3133 }
3134
3135 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3136 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
3137 struct email_alertq *q = &p->email_alert.queues[i];
3138 struct check *check = &q->check;
3139
3140
3141 LIST_INIT(&q->email_alerts);
3142
Pieter Baauw235fcfc2016-02-13 15:33:40 +01003143 check->inter = p->email_alert.mailers.m->timeout.mail;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003144 check->rise = DEF_AGENT_RISETIME;
3145 check->fall = DEF_AGENT_FALLTIME;
3146 err_str = init_check(check, PR_O2_TCPCHK_CHK);
3147 if (err_str) {
3148 goto error_free;
3149 }
3150
3151 check->xprt = mailer->xprt;
3152 if (!get_host_port(&mailer->addr))
3153 /* Default to submission port */
3154 check->port = 587;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003155 check->addr = mailer->addr;
3156 check->server = s;
3157 }
3158
3159 return 1;
3160
3161error_free:
3162 while (i-- > 1)
3163 task_free(p->email_alert.queues[i].check.task);
3164 free(p->email_alert.queues);
3165 p->email_alert.queues = NULL;
3166error_alert:
3167 Alert("Email alert [%s] could not be initialised: %s\n", p->id, err_str);
3168 return 0;
3169}
3170
3171
3172static int add_tcpcheck_expect_str(struct list *list, const char *str)
3173{
3174 struct tcpcheck_rule *tcpcheck;
3175
3176 tcpcheck = calloc(1, sizeof *tcpcheck);
3177 if (!tcpcheck)
3178 return 0;
3179
3180 tcpcheck->action = TCPCHK_ACT_EXPECT;
3181 tcpcheck->string = strdup(str);
3182 if (!tcpcheck->string) {
3183 free(tcpcheck);
3184 return 0;
3185 }
3186
3187 LIST_ADDQ(list, &tcpcheck->list);
3188 return 1;
3189}
3190
3191static int add_tcpcheck_send_strs(struct list *list, const char * const *strs)
3192{
3193 struct tcpcheck_rule *tcpcheck;
Willy Tarreau64345aa2016-08-10 19:29:09 +02003194 const char *in;
3195 char *dst;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003196 int i;
3197
3198 tcpcheck = calloc(1, sizeof *tcpcheck);
3199 if (!tcpcheck)
3200 return 0;
3201
3202 tcpcheck->action = TCPCHK_ACT_SEND;
3203
3204 tcpcheck->string_len = 0;
3205 for (i = 0; strs[i]; i++)
3206 tcpcheck->string_len += strlen(strs[i]);
3207
3208 tcpcheck->string = malloc(tcpcheck->string_len + 1);
3209 if (!tcpcheck->string) {
3210 free(tcpcheck);
3211 return 0;
3212 }
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003213
Willy Tarreau64345aa2016-08-10 19:29:09 +02003214 dst = tcpcheck->string;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003215 for (i = 0; strs[i]; i++)
Willy Tarreau64345aa2016-08-10 19:29:09 +02003216 for (in = strs[i]; (*dst = *in++); dst++);
3217 *dst = 0;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003218
3219 LIST_ADDQ(list, &tcpcheck->list);
3220 return 1;
3221}
3222
3223static int enqueue_one_email_alert(struct email_alertq *q, const char *msg)
3224{
3225 struct email_alert *alert = NULL;
3226 struct tcpcheck_rule *tcpcheck;
3227 struct check *check = &q->check;
3228 struct proxy *p = check->server->proxy;
3229
3230 alert = calloc(1, sizeof *alert);
3231 if (!alert) {
3232 goto error;
3233 }
3234 LIST_INIT(&alert->tcpcheck_rules);
3235
3236 tcpcheck = calloc(1, sizeof *tcpcheck);
3237 if (!tcpcheck)
3238 goto error;
3239 tcpcheck->action = TCPCHK_ACT_CONNECT;
3240 LIST_ADDQ(&alert->tcpcheck_rules, &tcpcheck->list);
3241
3242 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "220 "))
3243 goto error;
3244
3245 {
3246 const char * const strs[4] = { "EHLO ", p->email_alert.myhostname, "\r\n" };
3247 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3248 goto error;
3249 }
3250
3251 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3252 goto error;
3253
3254 {
3255 const char * const strs[4] = { "MAIL FROM:<", p->email_alert.from, ">\r\n" };
3256 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3257 goto error;
3258 }
3259
3260 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3261 goto error;
3262
3263 {
3264 const char * const strs[4] = { "RCPT TO:<", p->email_alert.to, ">\r\n" };
3265 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3266 goto error;
3267 }
3268
3269 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3270 goto error;
3271
3272 {
3273 const char * const strs[2] = { "DATA\r\n" };
3274 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3275 goto error;
3276 }
3277
3278 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "354 "))
3279 goto error;
3280
3281 {
3282 struct tm tm;
3283 char datestr[48];
3284 const char * const strs[18] = {
Pieter Baauw5e0964e2016-02-13 16:27:35 +01003285 "From: ", p->email_alert.from, "\r\n",
3286 "To: ", p->email_alert.to, "\r\n",
3287 "Date: ", datestr, "\r\n",
3288 "Subject: [HAproxy Alert] ", msg, "\r\n",
3289 "\r\n",
3290 msg, "\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003291 "\r\n",
Pieter Baauwed35c372015-07-22 19:51:54 +02003292 ".\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003293 NULL
3294 };
3295
3296 get_localtime(date.tv_sec, &tm);
3297
3298 if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %T %z (%Z)", &tm) == 0) {
3299 goto error;
3300 }
3301
3302 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3303 goto error;
3304 }
3305
3306 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3307 goto error;
3308
3309 {
3310 const char * const strs[2] = { "QUIT\r\n" };
3311 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3312 goto error;
3313 }
3314
3315 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "221 "))
3316 goto error;
3317
3318 if (!check->task) {
3319 struct task *t;
3320
3321 if ((t = task_new()) == NULL)
3322 goto error;
3323
3324 check->task = t;
3325 t->process = process_email_alert;
3326 t->context = check;
3327
3328 /* check this in one ms */
3329 t->expire = tick_add(now_ms, MS_TO_TICKS(1));
3330 check->start = now;
3331 task_queue(t);
3332 }
3333
3334 LIST_ADDQ(&q->email_alerts, &alert->list);
3335
3336 return 1;
3337
3338error:
3339 email_alert_free(alert);
3340 return 0;
3341}
3342
3343static void enqueue_email_alert(struct proxy *p, const char *msg)
3344{
3345 int i;
3346 struct mailer *mailer;
3347
3348 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3349 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
3350 if (!enqueue_one_email_alert(&p->email_alert.queues[i], msg)) {
3351 Alert("Email alert [%s] could not be enqueued: out of memory\n", p->id);
3352 return;
3353 }
3354 }
3355
3356 return;
3357}
3358
3359/*
3360 * Send email alert if configured.
3361 */
Simon Horman64e34162015-02-06 11:11:57 +09003362void send_email_alert(struct server *s, int level, const char *format, ...)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003363{
3364 va_list argp;
3365 char buf[1024];
3366 int len;
3367 struct proxy *p = s->proxy;
3368
Simon Horman64e34162015-02-06 11:11:57 +09003369 if (!p->email_alert.mailers.m || level > p->email_alert.level ||
3370 format == NULL || !init_email_alert_checks(s))
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003371 return;
3372
3373 va_start(argp, format);
3374 len = vsnprintf(buf, sizeof(buf), format, argp);
3375 va_end(argp);
3376
Thierry FOURNIER62c8a212017-02-09 12:19:27 +01003377 if (len < 0 || len >= sizeof(buf)) {
Cyril Bontéb65e0332015-12-04 03:07:08 +01003378 Alert("Email alert [%s] could not format message\n", p->id);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003379 return;
3380 }
3381
3382 enqueue_email_alert(p, buf);
3383}
3384
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003385/*
3386 * Return value:
3387 * the port to be used for the health check
3388 * 0 in case no port could be found for the check
3389 */
3390int srv_check_healthcheck_port(struct check *chk)
3391{
3392 int i = 0;
3393 struct server *srv = NULL;
3394
3395 srv = chk->server;
3396
3397 /* If neither a port nor an addr was specified and no check transport
3398 * layer is forced, then the transport layer used by the checks is the
3399 * same as for the production traffic. Otherwise we use raw_sock by
3400 * default, unless one is specified.
3401 */
3402 if (!chk->port && !is_addr(&chk->addr)) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003403 chk->use_ssl |= (srv->use_ssl || (srv->proxy->options & PR_O_TCPCHK_SSL));
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003404 chk->send_proxy |= (srv->pp_opts);
3405 }
3406
3407 /* by default, we use the health check port ocnfigured */
3408 if (chk->port > 0)
3409 return chk->port;
3410
3411 /* try to get the port from check_core.addr if check.port not set */
3412 i = get_host_port(&chk->addr);
3413 if (i > 0)
3414 return i;
3415
3416 /* try to get the port from server address */
3417 /* prevent MAPPORTS from working at this point, since checks could
3418 * not be performed in such case (MAPPORTS impose a relative ports
3419 * based on live traffic)
3420 */
3421 if (srv->flags & SRV_F_MAPPORTS)
3422 return 0;
Willy Tarreau04276f32017-01-06 17:41:29 +01003423
3424 i = srv->svc_port; /* by default */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003425 if (i > 0)
3426 return i;
3427
3428 return 0;
3429}
3430
Willy Tarreau865c5142016-12-21 20:04:48 +01003431__attribute__((constructor))
3432static void __check_init(void)
3433{
3434 hap_register_post_check(start_checks);
3435}
3436
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003437
Willy Tarreaubd741542010-03-16 18:46:54 +01003438/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003439 * Local variables:
3440 * c-indent-level: 8
3441 * c-basic-offset: 8
3442 * End:
3443 */