blob: 2075e3fb721cb143c51788b842261f99348cad72 [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
Willy Tarreaubd741542010-03-16 18:46:54 +010063static int httpchk_expect(struct server *s, int done);
Simon Hormane16c1b32015-01-30 11:22:57 +090064static int tcpcheck_get_step_id(struct check *);
Baptiste Assmann22b09d22015-05-01 08:03:04 +020065static char * tcpcheck_get_step_comment(struct check *, int);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +020066static void tcpcheck_main(struct connection *);
Willy Tarreaubd741542010-03-16 18:46:54 +010067
Simon Horman63a4a822012-03-19 07:24:41 +090068static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010069 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
70 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020071 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020072
Willy Tarreau23964182014-05-20 20:56:30 +020073 /* Below we have finished checks */
74 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010075 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010076
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010077 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020078
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010079 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
80 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
81 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020082
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010083 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
84 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
85 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020086
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010087 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
88 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020089
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020090 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020091
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010092 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
93 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
94 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +090095
96 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
97 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +020098 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020099};
100
Cyril Bontéac92a062014-12-27 22:28:38 +0100101const struct extcheck_env extcheck_envs[EXTCHK_SIZE] = {
102 [EXTCHK_PATH] = { "PATH", EXTCHK_SIZE_EVAL_INIT },
103 [EXTCHK_HAPROXY_PROXY_NAME] = { "HAPROXY_PROXY_NAME", EXTCHK_SIZE_EVAL_INIT },
104 [EXTCHK_HAPROXY_PROXY_ID] = { "HAPROXY_PROXY_ID", EXTCHK_SIZE_EVAL_INIT },
105 [EXTCHK_HAPROXY_PROXY_ADDR] = { "HAPROXY_PROXY_ADDR", EXTCHK_SIZE_EVAL_INIT },
106 [EXTCHK_HAPROXY_PROXY_PORT] = { "HAPROXY_PROXY_PORT", EXTCHK_SIZE_EVAL_INIT },
107 [EXTCHK_HAPROXY_SERVER_NAME] = { "HAPROXY_SERVER_NAME", EXTCHK_SIZE_EVAL_INIT },
108 [EXTCHK_HAPROXY_SERVER_ID] = { "HAPROXY_SERVER_ID", EXTCHK_SIZE_EVAL_INIT },
109 [EXTCHK_HAPROXY_SERVER_ADDR] = { "HAPROXY_SERVER_ADDR", EXTCHK_SIZE_EVAL_INIT },
110 [EXTCHK_HAPROXY_SERVER_PORT] = { "HAPROXY_SERVER_PORT", EXTCHK_SIZE_EVAL_INIT },
111 [EXTCHK_HAPROXY_SERVER_MAXCONN] = { "HAPROXY_SERVER_MAXCONN", EXTCHK_SIZE_EVAL_INIT },
112 [EXTCHK_HAPROXY_SERVER_CURCONN] = { "HAPROXY_SERVER_CURCONN", EXTCHK_SIZE_ULONG },
113};
114
Simon Horman63a4a822012-03-19 07:24:41 +0900115static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100116 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
117
118 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
119 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
120
121 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
122 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
123 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
124 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
125
126 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
127 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
128 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
129};
130
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200131/*
132 * Convert check_status code to description
133 */
134const char *get_check_status_description(short check_status) {
135
136 const char *desc;
137
138 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200139 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200140 else
141 desc = NULL;
142
143 if (desc && *desc)
144 return desc;
145 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200146 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200147}
148
149/*
150 * Convert check_status code to short info
151 */
152const char *get_check_status_info(short check_status) {
153
154 const char *info;
155
156 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200157 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200158 else
159 info = NULL;
160
161 if (info && *info)
162 return info;
163 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200164 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200165}
166
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100167const char *get_analyze_status(short analyze_status) {
168
169 const char *desc;
170
171 if (analyze_status < HANA_STATUS_SIZE)
172 desc = analyze_statuses[analyze_status].desc;
173 else
174 desc = NULL;
175
176 if (desc && *desc)
177 return desc;
178 else
179 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
180}
181
Willy Tarreaua150cf12014-05-20 21:57:23 +0200182/* Builds a string containing some information about the health check's result.
183 * The output string is allocated from the trash chunks. If the check is NULL,
184 * NULL is returned. This is designed to be used when emitting logs about health
185 * checks.
Willy Tarreauddd329c2014-05-16 16:46:12 +0200186 */
Willy Tarreaua150cf12014-05-20 21:57:23 +0200187static const char *check_reason_string(struct check *check)
Willy Tarreauddd329c2014-05-16 16:46:12 +0200188{
Willy Tarreaua150cf12014-05-20 21:57:23 +0200189 struct chunk *msg;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200190
Willy Tarreaua150cf12014-05-20 21:57:23 +0200191 if (!check)
192 return NULL;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200193
Willy Tarreaua150cf12014-05-20 21:57:23 +0200194 msg = get_trash_chunk();
195 chunk_printf(msg, "reason: %s", get_check_status_description(check->status));
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200196
Willy Tarreaua150cf12014-05-20 21:57:23 +0200197 if (check->status >= HCHK_STATUS_L57DATA)
198 chunk_appendf(msg, ", code: %d", check->code);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200199
Willy Tarreaua150cf12014-05-20 21:57:23 +0200200 if (*check->desc) {
201 struct chunk src;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200202
Willy Tarreaua150cf12014-05-20 21:57:23 +0200203 chunk_appendf(msg, ", info: \"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200204
Willy Tarreaua150cf12014-05-20 21:57:23 +0200205 chunk_initlen(&src, check->desc, 0, strlen(check->desc));
206 chunk_asciiencode(msg, &src, '"');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200207
Willy Tarreaua150cf12014-05-20 21:57:23 +0200208 chunk_appendf(msg, "\"");
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200209 }
210
Willy Tarreaua150cf12014-05-20 21:57:23 +0200211 if (check->duration >= 0)
212 chunk_appendf(msg, ", check duration: %ldms", check->duration);
213
214 return msg->str;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200215}
216
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200217/*
Simon Horman4a741432013-02-23 15:35:38 +0900218 * Set check->status, update check->duration and fill check->result with
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200219 * an adequate CHK_RES_* value. The new check->health is computed based
220 * on the result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200221 *
222 * Show information in logs about failed health check if server is UP
223 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200224 */
Simon Horman4a741432013-02-23 15:35:38 +0900225static void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100226{
Simon Horman4a741432013-02-23 15:35:38 +0900227 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200228 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200229 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900230
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200231 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100232 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900233 check->desc[0] = '\0';
234 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200235 return;
236 }
237
Simon Horman4a741432013-02-23 15:35:38 +0900238 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200239 return;
240
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200241 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900242 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
243 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200244 } else
Simon Horman4a741432013-02-23 15:35:38 +0900245 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200246
Simon Horman4a741432013-02-23 15:35:38 +0900247 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200248 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900249 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200250
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100251 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900252 check->duration = -1;
253 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200254 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900255 check->duration = tv_ms_elapsed(&check->start, &now);
256 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200257 }
258
Willy Tarreau23964182014-05-20 20:56:30 +0200259 /* no change is expected if no state change occurred */
260 if (check->result == CHK_RES_NEUTRAL)
261 return;
262
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200263 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200264
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200265 switch (check->result) {
266 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200267 /* Failure to connect to the agent as a secondary check should not
268 * cause the server to be marked down.
269 */
270 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900271 (check->status >= HCHK_STATUS_L57DATA)) &&
Willy Tarreau12634e12014-05-23 11:32:36 +0200272 (check->health >= check->rise)) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200273 s->counters.failed_checks++;
274 report = 1;
275 check->health--;
276 if (check->health < check->rise)
277 check->health = 0;
278 }
279 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200280
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200281 case CHK_RES_PASSED:
282 case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */
283 if ((check->health < check->rise + check->fall - 1) &&
284 (check->result == CHK_RES_PASSED || check->health > 0)) {
285 report = 1;
286 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200287
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200288 if (check->health >= check->rise)
289 check->health = check->rise + check->fall - 1; /* OK now */
290 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200291
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200292 /* clear consecutive_errors if observing is enabled */
293 if (s->onerror)
294 s->consecutive_errors = 0;
295 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100296
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200297 default:
298 break;
299 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200300
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200301 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
302 (status != prev_status || report)) {
303 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200304 "%s check for %sserver %s/%s %s%s",
305 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200306 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100307 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100308 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200309 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200310
Willy Tarreaua150cf12014-05-20 21:57:23 +0200311 srv_append_status(&trash, s, check_reason_string(check), -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200312
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100313 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200314 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
315 (check->health >= check->rise) ? check->fall : check->rise,
316 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200317
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100318 Warning("%s.\n", trash.str);
319 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman7ea9be02015-04-30 13:10:33 +0900320 send_email_alert(s, LOG_INFO, "%s", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200321 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200322}
323
Willy Tarreau4eec5472014-05-20 22:32:27 +0200324/* Marks the check <check>'s server down if the current check is already failed
325 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200326 */
Willy Tarreau4eec5472014-05-20 22:32:27 +0200327static void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200328{
Simon Horman4a741432013-02-23 15:35:38 +0900329 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900330
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200331 /* The agent secondary check should only cause a server to be marked
332 * as down if check->status is HCHK_STATUS_L7STS, which indicates
333 * that the agent returned "fail", "stopped" or "down".
334 * The implication here is that failure to connect to the agent
335 * as a secondary check should not cause the server to be marked
336 * down. */
337 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
338 return;
339
Willy Tarreau4eec5472014-05-20 22:32:27 +0200340 if (check->health > 0)
341 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100342
Willy Tarreau4eec5472014-05-20 22:32:27 +0200343 /* We only report a reason for the check if we did not do so previously */
344 srv_set_stopped(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200345}
346
Willy Tarreauaf549582014-05-16 17:37:50 +0200347/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200348 * it isn't in maintenance, it is not tracking a down server and other checks
349 * comply. The rule is simple : by default, a server is up, unless any of the
350 * following conditions is true :
351 * - health check failed (check->health < rise)
352 * - agent check failed (agent->health < rise)
353 * - the server tracks a down server (track && track->state == STOPPED)
354 * Note that if the server has a slowstart, it will switch to STARTING instead
355 * of RUNNING. Also, only the health checks support the nolb mode, so the
356 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200357 */
Willy Tarreau3e048382014-05-21 10:30:54 +0200358static void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200359{
Simon Horman4a741432013-02-23 15:35:38 +0900360 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100361
Willy Tarreauaf549582014-05-16 17:37:50 +0200362 if (s->admin & SRV_ADMF_MAINT)
363 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100364
Willy Tarreau3e048382014-05-21 10:30:54 +0200365 if (s->track && s->track->state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200366 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100367
Willy Tarreau3e048382014-05-21 10:30:54 +0200368 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
369 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100370
Willy Tarreau3e048382014-05-21 10:30:54 +0200371 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
372 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200373
Willy Tarreau3e048382014-05-21 10:30:54 +0200374 if ((check->state & CHK_ST_AGENT) && s->state == SRV_ST_STOPPING)
375 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100376
Willy Tarreau3e048382014-05-21 10:30:54 +0200377 srv_set_running(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100378}
379
Willy Tarreaudb58b792014-05-21 13:57:23 +0200380/* Marks the check <check> as valid and tries to set its server into stopping mode
381 * if it was running or starting, and provided it isn't in maintenance and other
382 * checks comply. The conditions for the server to be marked in stopping mode are
383 * the same as for it to be turned up. Also, only the health checks support the
384 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200385 */
Willy Tarreaudb58b792014-05-21 13:57:23 +0200386static void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200387{
Simon Horman4a741432013-02-23 15:35:38 +0900388 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100389
Willy Tarreauaf549582014-05-16 17:37:50 +0200390 if (s->admin & SRV_ADMF_MAINT)
391 return;
392
Willy Tarreaudb58b792014-05-21 13:57:23 +0200393 if (check->state & CHK_ST_AGENT)
394 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100395
Willy Tarreaudb58b792014-05-21 13:57:23 +0200396 if (s->track && s->track->state == SRV_ST_STOPPED)
397 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100398
Willy Tarreaudb58b792014-05-21 13:57:23 +0200399 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
400 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100401
Willy Tarreaudb58b792014-05-21 13:57:23 +0200402 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
403 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100404
Willy Tarreaudb58b792014-05-21 13:57:23 +0200405 srv_set_stopping(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100406}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200407
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100408/* note: use health_adjust() only, which first checks that the observe mode is
409 * enabled.
410 */
411void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100412{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100413 int failed;
414 int expire;
415
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100416 if (s->observe >= HANA_OBS_SIZE)
417 return;
418
Willy Tarreaubb956662013-01-24 00:37:39 +0100419 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100420 return;
421
422 switch (analyze_statuses[status].lr[s->observe - 1]) {
423 case 1:
424 failed = 1;
425 break;
426
427 case 2:
428 failed = 0;
429 break;
430
431 default:
432 return;
433 }
434
435 if (!failed) {
436 /* good: clear consecutive_errors */
437 s->consecutive_errors = 0;
438 return;
439 }
440
441 s->consecutive_errors++;
442
443 if (s->consecutive_errors < s->consecutive_errors_limit)
444 return;
445
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100446 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
447 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100448
449 switch (s->onerror) {
450 case HANA_ONERR_FASTINTER:
451 /* force fastinter - nothing to do here as all modes force it */
452 break;
453
454 case HANA_ONERR_SUDDTH:
455 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900456 if (s->check.health > s->check.rise)
457 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100458
459 /* no break - fall through */
460
461 case HANA_ONERR_FAILCHK:
462 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900463 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200464 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100465 break;
466
467 case HANA_ONERR_MARKDWN:
468 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900469 s->check.health = s->check.rise;
Simon Horman4a741432013-02-23 15:35:38 +0900470 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200471 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100472 break;
473
474 default:
475 /* write a warning? */
476 break;
477 }
478
479 s->consecutive_errors = 0;
480 s->counters.failed_hana++;
481
Simon Horman66183002013-02-23 10:16:43 +0900482 if (s->check.fastinter) {
483 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300484 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200485 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300486 /* requeue check task with new expire */
487 task_queue(s->check.task);
488 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100489 }
490}
491
Willy Tarreaua1dab552014-04-14 15:04:54 +0200492static int httpchk_build_status_header(struct server *s, char *buffer, int size)
Willy Tarreauef781042010-01-27 11:53:01 +0100493{
494 int sv_state;
495 int ratio;
496 int hlen = 0;
Joseph Lynch514061c2015-01-15 17:52:59 -0800497 char addr[46];
498 char port[6];
Willy Tarreauef781042010-01-27 11:53:01 +0100499 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
500 "UP %d/%d", "UP",
501 "NOLB %d/%d", "NOLB",
502 "no check" };
503
504 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
505 hlen += 24;
506
Willy Tarreauff5ae352013-12-11 20:36:34 +0100507 if (!(s->check.state & CHK_ST_ENABLED))
508 sv_state = 6;
Willy Tarreau892337c2014-05-13 23:41:20 +0200509 else if (s->state != SRV_ST_STOPPED) {
Simon Horman58c32972013-11-25 10:46:38 +0900510 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100511 sv_state = 3; /* UP */
512 else
513 sv_state = 2; /* going down */
514
Willy Tarreau892337c2014-05-13 23:41:20 +0200515 if (s->state == SRV_ST_STOPPING)
Willy Tarreauef781042010-01-27 11:53:01 +0100516 sv_state += 2;
517 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900518 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100519 sv_state = 1; /* going up */
520 else
521 sv_state = 0; /* DOWN */
522 }
523
Willy Tarreaua1dab552014-04-14 15:04:54 +0200524 hlen += snprintf(buffer + hlen, size - hlen,
Willy Tarreauef781042010-01-27 11:53:01 +0100525 srv_hlt_st[sv_state],
Willy Tarreau892337c2014-05-13 23:41:20 +0200526 (s->state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
527 (s->state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100528
Joseph Lynch514061c2015-01-15 17:52:59 -0800529 addr_to_str(&s->addr, addr, sizeof(addr));
530 port_to_str(&s->addr, port, sizeof(port));
531
532 hlen += snprintf(buffer + hlen, size - hlen, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
533 addr, port, s->proxy->id, s->id,
Willy Tarreauef781042010-01-27 11:53:01 +0100534 global.node,
535 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
536 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
537 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
538 s->nbpend);
539
Willy Tarreau892337c2014-05-13 23:41:20 +0200540 if ((s->state == SRV_ST_STARTING) &&
Willy Tarreauef781042010-01-27 11:53:01 +0100541 now.tv_sec < s->last_change + s->slowstart &&
542 now.tv_sec >= s->last_change) {
543 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
Willy Tarreaua1dab552014-04-14 15:04:54 +0200544 hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio);
Willy Tarreauef781042010-01-27 11:53:01 +0100545 }
546
547 buffer[hlen++] = '\r';
548 buffer[hlen++] = '\n';
549
550 return hlen;
551}
552
Willy Tarreau20a18342013-12-05 00:31:46 +0100553/* Check the connection. If an error has already been reported or the socket is
554 * closed, keep errno intact as it is supposed to contain the valid error code.
555 * If no error is reported, check the socket's error queue using getsockopt().
556 * Warning, this must be done only once when returning from poll, and never
557 * after an I/O error was attempted, otherwise the error queue might contain
558 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
559 * socket. Returns non-zero if an error was reported, zero if everything is
560 * clean (including a properly closed socket).
561 */
562static int retrieve_errno_from_socket(struct connection *conn)
563{
564 int skerr;
565 socklen_t lskerr = sizeof(skerr);
566
567 if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl))
568 return 1;
569
Willy Tarreau3c728722014-01-23 13:50:42 +0100570 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100571 return 0;
572
573 if (getsockopt(conn->t.sock.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
574 errno = skerr;
575
576 if (errno == EAGAIN)
577 errno = 0;
578
579 if (!errno) {
580 /* we could not retrieve an error, that does not mean there is
581 * none. Just don't change anything and only report the prior
582 * error if any.
583 */
584 if (conn->flags & CO_FL_ERROR)
585 return 1;
586 else
587 return 0;
588 }
589
590 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
591 return 1;
592}
593
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100594/* Try to collect as much information as possible on the connection status,
595 * and adjust the server status accordingly. It may make use of <errno_bck>
596 * if non-null when the caller is absolutely certain of its validity (eg:
597 * checked just after a syscall). If the caller doesn't have a valid errno,
598 * it can pass zero, and retrieve_errno_from_socket() will be called to try
599 * to extract errno from the socket. If no error is reported, it will consider
600 * the <expired> flag. This is intended to be used when a connection error was
601 * reported in conn->flags or when a timeout was reported in <expired>. The
602 * function takes care of not updating a server status which was already set.
603 * All situations where at least one of <expired> or CO_FL_ERROR are set
604 * produce a status.
605 */
606static void chk_report_conn_err(struct connection *conn, int errno_bck, int expired)
607{
608 struct check *check = conn->owner;
609 const char *err_msg;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200610 struct chunk *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200611 int step;
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200612 char *comment;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100613
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100614 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100615 return;
616
617 errno = errno_bck;
618 if (!errno || errno == EAGAIN)
619 retrieve_errno_from_socket(conn);
620
621 if (!(conn->flags & CO_FL_ERROR) && !expired)
622 return;
623
624 /* we'll try to build a meaningful error message depending on the
625 * context of the error possibly present in conn->err_code, and the
626 * socket error possibly collected above. This is useful to know the
627 * exact step of the L6 layer (eg: SSL handshake).
628 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200629 chk = get_trash_chunk();
630
631 if (check->type == PR_O2_TCPCHK_CHK) {
Simon Hormane16c1b32015-01-30 11:22:57 +0900632 step = tcpcheck_get_step_id(check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200633 if (!step)
634 chunk_printf(chk, " at initial connection step of tcp-check");
635 else {
636 chunk_printf(chk, " at step %d of tcp-check", step);
637 /* we were looking for a string */
638 if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_CONNECT) {
639 if (check->last_started_step->port)
640 chunk_appendf(chk, " (connect port %d)" ,check->last_started_step->port);
641 else
642 chunk_appendf(chk, " (connect)");
643 }
644 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_EXPECT) {
645 if (check->last_started_step->string)
Baptiste Assmann96a5c9b2015-05-01 08:09:29 +0200646 chunk_appendf(chk, " (expect string '%s')", check->last_started_step->string);
Willy Tarreau213c6782014-10-02 14:51:02 +0200647 else if (check->last_started_step->expect_regex)
648 chunk_appendf(chk, " (expect regex)");
649 }
650 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_SEND) {
651 chunk_appendf(chk, " (send)");
652 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200653
654 comment = tcpcheck_get_step_comment(check, step);
655 if (comment)
656 chunk_appendf(chk, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200657 }
658 }
659
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100660 if (conn->err_code) {
661 if (errno && errno != EAGAIN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200662 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100663 else
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200664 chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100665 err_msg = trash.str;
666 }
667 else {
668 if (errno && errno != EAGAIN) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200669 chunk_printf(&trash, "%s%s", strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100670 err_msg = trash.str;
671 }
672 else {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200673 err_msg = chk->str;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100674 }
675 }
676
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200677 if (check->state & CHK_ST_PORT_MISS) {
678 /* NOTE: this is reported after <fall> tries */
679 chunk_printf(chk, "No port available for the TCP connection");
680 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
681 }
682
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100683 if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
684 /* L4 not established (yet) */
685 if (conn->flags & CO_FL_ERROR)
686 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
687 else if (expired)
688 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200689
690 /*
691 * might be due to a server IP change.
692 * Let's trigger a DNS resolution if none are currently running.
693 */
694 if ((check->server->resolution) && (check->server->resolution->step == RSLV_STEP_NONE))
695 trigger_resolution(check->server);
696
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100697 }
698 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
699 /* L6 not established (yet) */
700 if (conn->flags & CO_FL_ERROR)
701 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
702 else if (expired)
703 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
704 }
705 else if (conn->flags & CO_FL_ERROR) {
706 /* I/O error after connection was established and before we could diagnose */
707 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
708 }
709 else if (expired) {
710 /* connection established but expired check */
711 if (check->type == PR_O2_SSL3_CHK)
712 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
713 else /* HTTP, SMTP, ... */
714 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
715 }
716
717 return;
718}
719
Willy Tarreaubaaee002006-06-26 02:48:02 +0200720/*
721 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200722 * the connection acknowledgement. If the proxy requires L7 health-checks,
723 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900724 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200725 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200726static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200727{
Simon Horman4a741432013-02-23 15:35:38 +0900728 struct check *check = conn->owner;
729 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900730 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200731
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100732 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100733 goto out_wakeup;
734
Willy Tarreau310987a2014-01-22 19:46:33 +0100735 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100736 return;
737
Willy Tarreau20a18342013-12-05 00:31:46 +0100738 if (retrieve_errno_from_socket(conn)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100739 chk_report_conn_err(conn, errno, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100740 __conn_data_stop_both(conn);
741 goto out_wakeup;
742 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100743
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100744 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100745 /* if the output is closed, we can't do anything */
746 conn->flags |= CO_FL_ERROR;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100747 chk_report_conn_err(conn, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100748 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200749 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200750
Willy Tarreau06559ac2013-12-05 01:53:08 +0100751 /* here, we know that the connection is established. That's enough for
752 * a pure TCP check.
753 */
754 if (!check->type)
755 goto out_wakeup;
756
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200757 if (check->type == PR_O2_TCPCHK_CHK) {
758 tcpcheck_main(conn);
759 return;
760 }
761
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100762 if (check->bo->o) {
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100763 conn->xprt->snd_buf(conn, check->bo, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100764 if (conn->flags & CO_FL_ERROR) {
765 chk_report_conn_err(conn, errno, 0);
766 __conn_data_stop_both(conn);
767 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200768 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100769 if (check->bo->o)
770 return;
771 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200772
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100773 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
774 if (s->proxy->timeout.check) {
775 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
776 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200777 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100778 goto out_nowake;
779
Willy Tarreau83749182007-04-15 20:56:27 +0200780 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200781 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200782 out_nowake:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200783 __conn_data_stop_send(conn); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200784}
785
Willy Tarreaubaaee002006-06-26 02:48:02 +0200786/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200787 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200788 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900789 * set_server_check_status() to update check->status, check->duration
790 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200791
792 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
793 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
794 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
795 * response to an SSL HELLO (the principle is that this is enough to
796 * distinguish between an SSL server and a pure TCP relay). All other cases will
797 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
798 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200799 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200800static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200801{
Simon Horman4a741432013-02-23 15:35:38 +0900802 struct check *check = conn->owner;
803 struct server *s = check->server;
804 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200805 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100806 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200807 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200808
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100809 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +0200810 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200811
Willy Tarreau310987a2014-01-22 19:46:33 +0100812 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200813 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200814
815 if (check->type == PR_O2_TCPCHK_CHK) {
816 tcpcheck_main(conn);
817 return;
818 }
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200819
Willy Tarreau83749182007-04-15 20:56:27 +0200820 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
821 * but the connection was closed on the remote end. Fortunately, recv still
822 * works correctly and we don't need to do the getsockopt() on linux.
823 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000824
825 /* Set buffer to point to the end of the data already read, and check
826 * that there is free space remaining. If the buffer is full, proceed
827 * with running the checks without attempting another socket read.
828 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000829
Willy Tarreau03938182010-03-17 21:52:07 +0100830 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +0000831
Simon Horman4a741432013-02-23 15:35:38 +0900832 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreauf1503172012-09-28 19:39:36 +0200833 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +0100834 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +0900835 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +0200836 /* Report network errors only if we got no other data. Otherwise
837 * we'll let the upper layers decide whether the response is OK
838 * or not. It is very common that an RST sent by the server is
839 * reported as an error just after the last data chunk.
840 */
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100841 chk_report_conn_err(conn, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +0100842 goto out_wakeup;
843 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200844 }
845
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100846
Willy Tarreau03938182010-03-17 21:52:07 +0100847 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +0900848 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +0100849 */
Simon Horman4a741432013-02-23 15:35:38 +0900850 if (check->bi->i < check->bi->size)
851 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100852 else {
Simon Horman4a741432013-02-23 15:35:38 +0900853 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100854 done = 1; /* buffer full, don't wait for more data */
855 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200856
Nick Chalk57b1bf72010-03-16 15:50:46 +0000857 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +0900858 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +0200859 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900860 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100861 goto wait_more_data;
862
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100863 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +0900864 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
865 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
866 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
867 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
868 !isdigit((unsigned char) *(check->bi->data + 11))) {
869 cut_crlf(check->bi->data);
870 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200871
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100872 goto out_wakeup;
873 }
874
Simon Horman4a741432013-02-23 15:35:38 +0900875 check->code = str2uic(check->bi->data + 9);
876 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +0000877
Willy Tarreaubd741542010-03-16 18:46:54 +0100878 if ((s->proxy->options & PR_O_DISABLE404) &&
Willy Tarreau892337c2014-05-13 23:41:20 +0200879 (s->state != SRV_ST_STOPPED) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000880 /* 404 may be accepted as "stopping" only if the server was up */
881 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900882 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000883 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100884 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
885 /* Run content verification check... We know we have at least 13 chars */
886 if (!httpchk_expect(s, done))
887 goto wait_more_data;
888 }
889 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +0900890 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +0100891 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900892 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +0100893 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000894 else {
895 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900896 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000897 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200898 break;
899
900 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900901 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +0100902 goto wait_more_data;
903
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100904 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +0900905 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
906 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200907 else
Simon Horman4a741432013-02-23 15:35:38 +0900908 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200909 break;
910
911 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900912 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100913 goto wait_more_data;
914
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200915 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +0900916 if ((check->bi->i < strlen("000\r")) ||
917 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
918 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
919 !isdigit((unsigned char) *(check->bi->data + 2))) {
920 cut_crlf(check->bi->data);
921 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200922
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200923 goto out_wakeup;
924 }
925
Simon Horman4a741432013-02-23 15:35:38 +0900926 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200927
Simon Horman4a741432013-02-23 15:35:38 +0900928 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200929 cut_crlf(desc);
930
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100931 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +0900932 if (*check->bi->data == '2')
933 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200934 else
Simon Horman4a741432013-02-23 15:35:38 +0900935 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200936 break;
937
Simon Hormana2b9dad2013-02-12 10:45:54 +0900938 case PR_O2_LB_AGENT_CHK: {
Willy Tarreau81f5d942013-12-09 20:51:51 +0100939 int status = HCHK_STATUS_CHECKED;
940 const char *hs = NULL; /* health status */
941 const char *as = NULL; /* admin status */
942 const char *ps = NULL; /* performance status */
Nenad Merdanovic174dd372016-04-24 23:10:06 +0200943 const char *cs = NULL; /* maxconn */
Willy Tarreau81f5d942013-12-09 20:51:51 +0100944 const char *err = NULL; /* first error to report */
945 const char *wrn = NULL; /* first warning to report */
946 char *cmd, *p;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900947
Willy Tarreau81f5d942013-12-09 20:51:51 +0100948 /* We're getting an agent check response. The agent could
949 * have been disabled in the mean time with a long check
950 * still pending. It is important that we ignore the whole
951 * response.
952 */
953 if (!(check->server->agent.state & CHK_ST_ENABLED))
954 break;
955
956 /* The agent supports strings made of a single line ended by the
957 * first CR ('\r') or LF ('\n'). This line is composed of words
958 * delimited by spaces (' '), tabs ('\t'), or commas (','). The
959 * line may optionally contained a description of a state change
960 * after a sharp ('#'), which is only considered if a health state
961 * is announced.
962 *
963 * Words may be composed of :
964 * - a numeric weight suffixed by the percent character ('%').
965 * - a health status among "up", "down", "stopped", and "fail".
966 * - an admin status among "ready", "drain", "maint".
967 *
968 * These words may appear in any order. If multiple words of the
969 * same category appear, the last one wins.
970 */
971
Willy Tarreau9809b782013-12-11 21:40:11 +0100972 p = check->bi->data;
973 while (*p && *p != '\n' && *p != '\r')
974 p++;
975
976 if (!*p) {
977 if (!done)
978 goto wait_more_data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900979
Willy Tarreau9809b782013-12-11 21:40:11 +0100980 /* at least inform the admin that the agent is mis-behaving */
981 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
982 break;
983 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100984
Willy Tarreau9809b782013-12-11 21:40:11 +0100985 *p = 0;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100986 cmd = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900987
Willy Tarreau81f5d942013-12-09 20:51:51 +0100988 while (*cmd) {
989 /* look for next word */
990 if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') {
991 cmd++;
992 continue;
993 }
Simon Horman671b6f02013-11-25 10:46:39 +0900994
Willy Tarreau81f5d942013-12-09 20:51:51 +0100995 if (*cmd == '#') {
996 /* this is the beginning of a health status description,
997 * skip the sharp and blanks.
998 */
999 cmd++;
1000 while (*cmd == '\t' || *cmd == ' ')
1001 cmd++;
Simon Horman671b6f02013-11-25 10:46:39 +09001002 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001003 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001004
1005 /* find the end of the word so that we have a null-terminated
1006 * word between <cmd> and <p>.
1007 */
1008 p = cmd + 1;
1009 while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',')
1010 p++;
1011 if (*p)
1012 *p++ = 0;
1013
1014 /* first, health statuses */
1015 if (strcasecmp(cmd, "up") == 0) {
1016 check->health = check->rise + check->fall - 1;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001017 status = HCHK_STATUS_L7OKD;
Willy Tarreau81f5d942013-12-09 20:51:51 +01001018 hs = cmd;
1019 }
1020 else if (strcasecmp(cmd, "down") == 0) {
1021 check->health = 0;
1022 status = HCHK_STATUS_L7STS;
1023 hs = cmd;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001024 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001025 else if (strcasecmp(cmd, "stopped") == 0) {
1026 check->health = 0;
1027 status = HCHK_STATUS_L7STS;
1028 hs = cmd;
1029 }
1030 else if (strcasecmp(cmd, "fail") == 0) {
1031 check->health = 0;
1032 status = HCHK_STATUS_L7STS;
1033 hs = cmd;
1034 }
1035 /* admin statuses */
1036 else if (strcasecmp(cmd, "ready") == 0) {
1037 as = cmd;
1038 }
1039 else if (strcasecmp(cmd, "drain") == 0) {
1040 as = cmd;
1041 }
1042 else if (strcasecmp(cmd, "maint") == 0) {
1043 as = cmd;
1044 }
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001045 /* try to parse a weight here and keep the last one */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001046 else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) {
1047 ps = cmd;
1048 }
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001049 /* try to parse a maxconn here */
1050 else if (strncasecmp(cmd, "maxconn:", strlen("maxconn:")) == 0) {
1051 cs = cmd;
1052 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001053 else {
1054 /* keep a copy of the first error */
1055 if (!err)
1056 err = cmd;
1057 }
1058 /* skip to next word */
1059 cmd = p;
1060 }
1061 /* here, cmd points either to \0 or to the beginning of a
1062 * description. Skip possible leading spaces.
1063 */
1064 while (*cmd == ' ' || *cmd == '\n')
1065 cmd++;
1066
1067 /* First, update the admin status so that we avoid sending other
1068 * possibly useless warnings and can also update the health if
1069 * present after going back up.
1070 */
1071 if (as) {
1072 if (strcasecmp(as, "drain") == 0)
1073 srv_adm_set_drain(check->server);
1074 else if (strcasecmp(as, "maint") == 0)
1075 srv_adm_set_maint(check->server);
1076 else
1077 srv_adm_set_ready(check->server);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001078 }
1079
Willy Tarreau81f5d942013-12-09 20:51:51 +01001080 /* now change weights */
1081 if (ps) {
1082 const char *msg;
1083
1084 msg = server_parse_weight_change_request(s, ps);
1085 if (!wrn || !*wrn)
1086 wrn = msg;
1087 }
1088
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001089 if (cs) {
1090 const char *msg;
1091
1092 cs += strlen("maxconn:");
1093
1094 msg = server_parse_maxconn_change_request(s, cs);
1095 if (!wrn || !*wrn)
1096 wrn = msg;
1097 }
1098
Willy Tarreau81f5d942013-12-09 20:51:51 +01001099 /* and finally health status */
1100 if (hs) {
1101 /* We'll report some of the warnings and errors we have
1102 * here. Down reports are critical, we leave them untouched.
1103 * Lack of report, or report of 'UP' leaves the room for
1104 * ERR first, then WARN.
Simon Hormana2b9dad2013-02-12 10:45:54 +09001105 */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001106 const char *msg = cmd;
1107 struct chunk *t;
1108
1109 if (!*msg || status == HCHK_STATUS_L7OKD) {
1110 if (err && *err)
1111 msg = err;
1112 else if (wrn && *wrn)
1113 msg = wrn;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001114 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001115
1116 t = get_trash_chunk();
1117 chunk_printf(t, "via agent : %s%s%s%s",
1118 hs, *msg ? " (" : "",
1119 msg, *msg ? ")" : "");
1120
1121 set_server_check_status(check, status, t->str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001122 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001123 else if (err && *err) {
1124 /* No status change but we'd like to report something odd.
1125 * Just report the current state and copy the message.
1126 */
1127 chunk_printf(&trash, "agent reports an error : %s", err);
1128 set_server_check_status(check, status/*check->status*/, trash.str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001129
Willy Tarreau81f5d942013-12-09 20:51:51 +01001130 }
1131 else if (wrn && *wrn) {
1132 /* No status change but we'd like to report something odd.
1133 * Just report the current state and copy the message.
1134 */
1135 chunk_printf(&trash, "agent warns : %s", wrn);
1136 set_server_check_status(check, status/*check->status*/, trash.str);
1137 }
1138 else
1139 set_server_check_status(check, status, NULL);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001140 break;
1141 }
1142
Willy Tarreau1620ec32011-08-06 17:05:02 +02001143 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001144 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001145 goto wait_more_data;
1146
Simon Horman4a741432013-02-23 15:35:38 +09001147 if (check->bi->data[0] == 'R') {
1148 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001149 }
1150 else {
Simon Horman4a741432013-02-23 15:35:38 +09001151 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1152 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001153 else
1154 desc = "PostgreSQL unknown error";
1155
Simon Horman4a741432013-02-23 15:35:38 +09001156 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001157 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001158 break;
1159
1160 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001161 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001162 goto wait_more_data;
1163
Simon Horman4a741432013-02-23 15:35:38 +09001164 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1165 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001166 }
1167 else {
Simon Horman4a741432013-02-23 15:35:38 +09001168 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001169 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001170 break;
1171
1172 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001173 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001174 goto wait_more_data;
1175
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001176 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001177 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001178 /* We set the MySQL Version in description for information purpose
1179 * FIXME : it can be cool to use MySQL Version for other purpose,
1180 * like mark as down old MySQL server.
1181 */
Simon Horman4a741432013-02-23 15:35:38 +09001182 if (check->bi->i > 51) {
1183 desc = ltrim(check->bi->data + 5, ' ');
1184 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001185 }
1186 else {
1187 if (!done)
1188 goto wait_more_data;
1189 /* it seems we have a OK packet but without a valid length,
1190 * it must be a protocol error
1191 */
Simon Horman4a741432013-02-23 15:35:38 +09001192 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001193 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001194 }
1195 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001196 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001197 desc = ltrim(check->bi->data + 7, ' ');
1198 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001199 }
1200 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001201 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1202 (((unsigned int) *(check->bi->data + 1)) << 8) +
1203 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001204
Simon Horman4a741432013-02-23 15:35:38 +09001205 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001206 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001207 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001208 /* We have only one MySQL packet and it is a Handshake Initialization packet
1209 * but we need to have a second packet to know if it is alright
1210 */
Simon Horman4a741432013-02-23 15:35:38 +09001211 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001212 goto wait_more_data;
1213 }
1214 else {
1215 /* We have only one packet and it is an Error packet,
1216 * an error message is attached, so we can display it
1217 */
Simon Horman4a741432013-02-23 15:35:38 +09001218 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001219 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001220 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001221 }
Simon Horman4a741432013-02-23 15:35:38 +09001222 } else if (check->bi->i > first_packet_len + 4) {
1223 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1224 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1225 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001226
Simon Horman4a741432013-02-23 15:35:38 +09001227 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001228 /* We have 2 packets and that's good */
1229 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001230 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001231 /* No error packet */
1232 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001233 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001234 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001235 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001236 }
1237 else {
1238 /* An error message is attached in the Error packet
1239 * so we can display it ! :)
1240 */
Simon Horman4a741432013-02-23 15:35:38 +09001241 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001242 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001243 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001244 }
1245 }
1246 }
1247 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001248 if (!done)
1249 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001250 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001251 * it must be a protocol error
1252 */
Simon Horman4a741432013-02-23 15:35:38 +09001253 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001254 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001255 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001256 }
1257 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001258 break;
1259
1260 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001261 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001262 goto wait_more_data;
1263
1264 /* Check if the server speaks LDAP (ASN.1/BER)
1265 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1266 * http://tools.ietf.org/html/rfc4511
1267 */
1268
1269 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1270 * LDAPMessage: 0x30: SEQUENCE
1271 */
Simon Horman4a741432013-02-23 15:35:38 +09001272 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1273 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001274 }
1275 else {
1276 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001277 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001278
1279 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1280 * messageID: 0x02 0x01 0x01: INTEGER 1
1281 * protocolOp: 0x61: bindResponse
1282 */
1283 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001284 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1285 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001286
1287 goto out_wakeup;
1288 }
1289
1290 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001291 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001292
1293 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1294 * ldapResult: 0x0a 0x01: ENUMERATION
1295 */
1296 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001297 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1298 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001299
1300 goto out_wakeup;
1301 }
1302
1303 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1304 * resultCode
1305 */
Simon Horman4a741432013-02-23 15:35:38 +09001306 check->code = *(check->bi->data + msglen + 9);
1307 if (check->code) {
1308 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 +02001309 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001310 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001311 }
1312 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001313 break;
1314
Christopher Fauletba7bc162016-11-07 21:07:38 +01001315 case PR_O2_SPOP_CHK: {
1316 unsigned int framesz;
1317 char err[HCHK_DESC_LEN];
1318
1319 if (!done && check->bi->i < 4)
1320 goto wait_more_data;
1321
1322 memcpy(&framesz, check->bi->data, 4);
1323 framesz = ntohl(framesz);
1324
1325 if (!done && check->bi->i < (4+framesz))
1326 goto wait_more_data;
1327
1328 if (!handle_spoe_healthcheck_response(check->bi->data+4, framesz, err, HCHK_DESC_LEN-1))
1329 set_server_check_status(check, HCHK_STATUS_L7OKD, "SPOA server is ok");
1330 else
1331 set_server_check_status(check, HCHK_STATUS_L7STS, err);
1332 break;
1333 }
1334
Willy Tarreau1620ec32011-08-06 17:05:02 +02001335 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001336 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001337 break;
1338 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001339
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001340 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001341 /* collect possible new errors */
1342 if (conn->flags & CO_FL_ERROR)
1343 chk_report_conn_err(conn, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001344
Nick Chalk57b1bf72010-03-16 15:50:46 +00001345 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001346 *check->bi->data = '\0';
1347 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001348
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001349 /* Close the connection... We absolutely want to perform a hard close
1350 * and reset the connection if some data are pending, otherwise we end
1351 * up with many TIME_WAITs and eat all the source port range quickly.
1352 * To avoid sending RSTs all the time, we first try to drain pending
1353 * data.
1354 */
Willy Tarreaub4017d02015-03-12 23:11:26 +01001355 __conn_data_stop_both(conn);
1356 conn_data_shutw_hard(conn);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001357
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001358 /* OK, let's not stay here forever */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001359 if (check->result == CHK_RES_FAILED)
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001360 conn->flags |= CO_FL_ERROR;
1361
Willy Tarreaufdccded2008-08-29 18:19:04 +02001362 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001363 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001364
1365 wait_more_data:
Willy Tarreauf817e9f2014-01-10 16:58:45 +01001366 __conn_data_want_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001367}
1368
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001369/*
1370 * This function is used only for server health-checks. It handles connection
1371 * status updates including errors. If necessary, it wakes the check task up.
1372 * It always returns 0.
1373 */
1374static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001375{
Simon Horman4a741432013-02-23 15:35:38 +09001376 struct check *check = conn->owner;
Willy Tarreau20bea422012-07-06 12:00:49 +02001377
Willy Tarreau6c560da2012-11-24 11:14:45 +01001378 if (unlikely(conn->flags & CO_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001379 /* We may get error reports bypassing the I/O handlers, typically
1380 * the case when sending a pure TCP check which fails, then the I/O
1381 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001382 * main processing task so let's simply wake it up. If we get here,
1383 * we expect errno to still be valid.
1384 */
1385 chk_report_conn_err(conn, errno, 0);
1386
Willy Tarreau2d351b62013-12-05 02:36:25 +01001387 __conn_data_stop_both(conn);
1388 task_wakeup(check->task, TASK_WOKEN_IO);
1389 }
Willy Tarreau3be293f2014-02-05 18:31:24 +01001390 else if (!(conn->flags & (CO_FL_DATA_RD_ENA|CO_FL_DATA_WR_ENA|CO_FL_HANDSHAKE))) {
1391 /* we may get here if only a connection probe was required : we
1392 * don't have any data to send nor anything expected in response,
1393 * so the completion of the connection establishment is enough.
1394 */
1395 task_wakeup(check->task, TASK_WOKEN_IO);
1396 }
Willy Tarreau2d351b62013-12-05 02:36:25 +01001397
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001398 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001399 /* We're here because nobody wants to handle the error, so we
1400 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001401 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01001402 conn_sock_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02001403 conn_force_close(conn);
Willy Tarreau2d351b62013-12-05 02:36:25 +01001404 }
Willy Tarreau3267d362012-08-17 23:53:56 +02001405 return 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001406}
1407
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001408struct data_cb check_conn_cb = {
1409 .recv = event_srv_chk_r,
1410 .send = event_srv_chk_w,
1411 .wake = wake_srv_chk,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +01001412 .name = "CHCK",
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001413};
1414
Willy Tarreaubaaee002006-06-26 02:48:02 +02001415/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001416 * updates the server's weight during a warmup stage. Once the final weight is
1417 * reached, the task automatically stops. Note that any server status change
1418 * must have updated s->last_change accordingly.
1419 */
1420static struct task *server_warmup(struct task *t)
1421{
1422 struct server *s = t->context;
1423
1424 /* by default, plan on stopping the task */
1425 t->expire = TICK_ETERNITY;
Willy Tarreau20125212014-05-13 19:44:56 +02001426 if ((s->admin & SRV_ADMF_MAINT) ||
Willy Tarreau892337c2014-05-13 23:41:20 +02001427 (s->state != SRV_ST_STARTING))
Willy Tarreau2e993902011-10-31 11:53:20 +01001428 return t;
1429
Willy Tarreau892337c2014-05-13 23:41:20 +02001430 /* recalculate the weights and update the state */
Willy Tarreau004e0452013-11-21 11:22:01 +01001431 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001432
1433 /* probably that we can refill this server with a bit more connections */
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001434 pendconn_grab_from_px(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001435
1436 /* get back there in 1 second or 1/20th of the slowstart interval,
1437 * whichever is greater, resulting in small 5% steps.
1438 */
Willy Tarreau892337c2014-05-13 23:41:20 +02001439 if (s->state == SRV_ST_STARTING)
Willy Tarreau2e993902011-10-31 11:53:20 +01001440 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1441 return t;
1442}
1443
1444/*
Simon Horman98637e52014-06-20 12:30:16 +09001445 * establish a server health-check that makes use of a connection.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001446 *
1447 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001448 * - SF_ERR_NONE if everything's OK and tcpcheck_main() was not called
1449 * - SF_ERR_UP if if everything's OK and tcpcheck_main() was called
1450 * - SF_ERR_SRVTO if there are no more servers
1451 * - SF_ERR_SRVCL if the connection was refused by the server
1452 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1453 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1454 * - SF_ERR_INTERNAL for any other purely internal errors
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001455 * - 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 +01001456 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001457 * Note that we try to prevent the network stack from sending the ACK during the
1458 * connect() when a pure TCP check is used (without PROXY protocol).
1459 */
Simon Horman98637e52014-06-20 12:30:16 +09001460static int connect_conn_chk(struct task *t)
Simon Hormanb00d17a2014-06-13 16:18:16 +09001461{
1462 struct check *check = t->context;
1463 struct server *s = check->server;
1464 struct connection *conn = check->conn;
1465 struct protocol *proto;
1466 int ret;
Willy Tarreauf3d34822014-12-08 12:11:28 +01001467 int quickack;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001468
1469 /* tcpcheck send/expect initialisation */
1470 if (check->type == PR_O2_TCPCHK_CHK)
1471 check->current_step = NULL;
1472
1473 /* prepare the check buffer.
1474 * This should not be used if check is the secondary agent check
1475 * of a server as s->proxy->check_req will relate to the
1476 * configuration of the primary check. Similarly, tcp-check uses
1477 * its own strings.
1478 */
1479 if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) {
1480 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
1481
1482 /* we want to check if this host replies to HTTP or SSLv3 requests
1483 * so we'll send the request, and won't wake the checker up now.
1484 */
1485 if ((check->type) == PR_O2_SSL3_CHK) {
1486 /* SSL requires that we put Unix time in the request */
1487 int gmt_time = htonl(date.tv_sec);
1488 memcpy(check->bo->data + 11, &gmt_time, 4);
1489 }
1490 else if ((check->type) == PR_O2_HTTP_CHK) {
1491 if (s->proxy->options2 & PR_O2_CHK_SNDST)
1492 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size));
Cyril Bonté32602d22015-01-30 00:07:07 +01001493 /* prevent HTTP keep-alive when "http-check expect" is used */
1494 if (s->proxy->options2 & PR_O2_EXP_TYPE)
1495 bo_putstr(check->bo, "Connection: close\r\n");
Simon Hormanb00d17a2014-06-13 16:18:16 +09001496 bo_putstr(check->bo, "\r\n");
1497 *check->bo->p = '\0'; /* to make gdb output easier to read */
1498 }
1499 }
1500
James Brown55f9ff12015-10-21 18:19:05 -07001501 if ((check->type & PR_O2_LB_AGENT_CHK) && check->send_string_len) {
1502 bo_putblk(check->bo, check->send_string, check->send_string_len);
1503 }
1504
Simon Hormanb00d17a2014-06-13 16:18:16 +09001505 /* prepare a new connection */
1506 conn_init(conn);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001507
Simon Horman41f58762015-01-30 11:22:56 +09001508 if (is_addr(&check->addr)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001509 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09001510 conn->addr.to = check->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001511 }
1512 else {
1513 /* we'll connect to the addr on the server */
1514 conn->addr.to = s->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001515 }
1516
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001517 if ((conn->addr.to.ss_family == AF_INET) || (conn->addr.to.ss_family == AF_INET6)) {
1518 int i = 0;
1519
1520 i = srv_check_healthcheck_port(check);
1521 if (i == 0) {
1522 conn->owner = check;
1523 return SF_ERR_CHK_PORT;
1524 }
1525
1526 set_host_port(&conn->addr.to, i);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001527 }
1528
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01001529 proto = protocol_by_family(conn->addr.to.ss_family);
1530
1531 conn_prepare(conn, proto, check->xprt);
1532 conn_attach(conn, check, &check_conn_cb);
1533 conn->target = &s->obj_type;
1534
1535 /* no client address */
1536 clear_addr(&conn->addr.from);
1537
Willy Tarreauf3d34822014-12-08 12:11:28 +01001538 /* only plain tcp-check supports quick ACK */
1539 quickack = check->type == 0 || check->type == PR_O2_TCPCHK_CHK;
1540
Simon Hormane16c1b32015-01-30 11:22:57 +09001541 if (check->type == PR_O2_TCPCHK_CHK && !LIST_ISEMPTY(check->tcpcheck_rules)) {
Willy Tarreau5581c272015-05-13 12:24:53 +02001542 struct tcpcheck_rule *r;
1543
1544 r = LIST_NEXT(check->tcpcheck_rules, struct tcpcheck_rule *, list);
1545
Simon Hormanb00d17a2014-06-13 16:18:16 +09001546 /* if first step is a 'connect', then tcpcheck_main must run it */
1547 if (r->action == TCPCHK_ACT_CONNECT) {
1548 tcpcheck_main(conn);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001549 return SF_ERR_UP;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001550 }
Willy Tarreauf3d34822014-12-08 12:11:28 +01001551 if (r->action == TCPCHK_ACT_EXPECT)
1552 quickack = 0;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001553 }
1554
Willy Tarreaue7dff022015-04-03 01:14:29 +02001555 ret = SF_ERR_INTERNAL;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001556 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01001557 ret = proto->connect(conn, check->type, quickack ? 2 : 0);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001558 conn->flags |= CO_FL_WAKE_DATA;
1559 if (s->check.send_proxy) {
1560 conn->send_proxy_ofs = 1;
1561 conn->flags |= CO_FL_SEND_PROXY;
1562 }
1563
1564 return ret;
1565}
1566
Simon Horman98637e52014-06-20 12:30:16 +09001567static struct list pid_list = LIST_HEAD_INIT(pid_list);
1568static struct pool_head *pool2_pid_list;
1569
1570void block_sigchld(void)
1571{
1572 sigset_t set;
1573 sigemptyset(&set);
1574 sigaddset(&set, SIGCHLD);
Willy Tarreauebc92442016-06-21 17:29:46 +02001575 assert(sigprocmask(SIG_BLOCK, &set, NULL) == 0);
Simon Horman98637e52014-06-20 12:30:16 +09001576}
1577
1578void unblock_sigchld(void)
1579{
1580 sigset_t set;
1581 sigemptyset(&set);
Willy Tarreauebc92442016-06-21 17:29:46 +02001582 sigaddset(&set, SIGCHLD);
1583 assert(sigprocmask(SIG_UNBLOCK, &set, NULL) == 0);
Simon Horman98637e52014-06-20 12:30:16 +09001584}
1585
Simon Horman98637e52014-06-20 12:30:16 +09001586static struct pid_list *pid_list_add(pid_t pid, struct task *t)
1587{
1588 struct pid_list *elem;
1589 struct check *check = t->context;
1590
1591 elem = pool_alloc2(pool2_pid_list);
1592 if (!elem)
1593 return NULL;
1594 elem->pid = pid;
1595 elem->t = t;
1596 elem->exited = 0;
1597 check->curpid = elem;
1598 LIST_INIT(&elem->list);
1599 LIST_ADD(&pid_list, &elem->list);
1600 return elem;
1601}
1602
Simon Horman98637e52014-06-20 12:30:16 +09001603static void pid_list_del(struct pid_list *elem)
1604{
1605 struct check *check;
1606
1607 if (!elem)
1608 return;
1609
Simon Horman98637e52014-06-20 12:30:16 +09001610 LIST_DEL(&elem->list);
Simon Horman98637e52014-06-20 12:30:16 +09001611 if (!elem->exited)
1612 kill(elem->pid, SIGTERM);
1613
1614 check = elem->t->context;
1615 check->curpid = NULL;
1616 pool_free2(pool2_pid_list, elem);
1617}
1618
1619/* Called from inside SIGCHLD handler, SIGCHLD is blocked */
1620static void pid_list_expire(pid_t pid, int status)
1621{
1622 struct pid_list *elem;
1623
1624 list_for_each_entry(elem, &pid_list, list) {
1625 if (elem->pid == pid) {
1626 elem->t->expire = now_ms;
1627 elem->status = status;
1628 elem->exited = 1;
Cyril Bonté9dbcfab2014-08-07 01:55:39 +02001629 task_wakeup(elem->t, TASK_WOKEN_IO);
Simon Horman98637e52014-06-20 12:30:16 +09001630 return;
1631 }
1632 }
1633}
1634
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001635static void sigchld_handler(struct sig_handler *sh)
Simon Horman98637e52014-06-20 12:30:16 +09001636{
1637 pid_t pid;
1638 int status;
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001639
Simon Horman98637e52014-06-20 12:30:16 +09001640 while ((pid = waitpid(0, &status, WNOHANG)) > 0)
1641 pid_list_expire(pid, status);
1642}
1643
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001644static int init_pid_list(void)
1645{
Simon Horman98637e52014-06-20 12:30:16 +09001646 if (pool2_pid_list != NULL)
1647 /* Nothing to do */
1648 return 0;
1649
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001650 if (!signal_register_fct(SIGCHLD, sigchld_handler, SIGCHLD)) {
Simon Horman98637e52014-06-20 12:30:16 +09001651 Alert("Failed to set signal handler for external health checks: %s. Aborting.\n",
1652 strerror(errno));
1653 return 1;
1654 }
1655
1656 pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED);
1657 if (pool2_pid_list == NULL) {
1658 Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n",
1659 strerror(errno));
1660 return 1;
1661 }
1662
1663 return 0;
1664}
1665
Cyril Bontéac92a062014-12-27 22:28:38 +01001666/* helper macro to set an environment variable and jump to a specific label on failure. */
1667#define EXTCHK_SETENV(check, envidx, value, fail) { if (extchk_setenv(check, envidx, value)) goto fail; }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001668
1669/*
Cyril Bontéac92a062014-12-27 22:28:38 +01001670 * helper function to allocate enough memory to store an environment variable.
1671 * It will also check that the environment variable is updatable, and silently
1672 * fail if not.
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001673 */
Cyril Bontéac92a062014-12-27 22:28:38 +01001674static int extchk_setenv(struct check *check, int idx, const char *value)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001675{
1676 int len, ret;
Cyril Bontéac92a062014-12-27 22:28:38 +01001677 char *envname;
1678 int vmaxlen;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001679
Cyril Bontéac92a062014-12-27 22:28:38 +01001680 if (idx < 0 || idx >= EXTCHK_SIZE) {
1681 Alert("Illegal environment variable index %d. Aborting.\n", idx);
1682 return 1;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001683 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001684
1685 envname = extcheck_envs[idx].name;
1686 vmaxlen = extcheck_envs[idx].vmaxlen;
1687
1688 /* Check if the environment variable is already set, and silently reject
1689 * the update if this one is not updatable. */
1690 if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx]))
1691 return 0;
1692
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001693 /* Instead of sending NOT_USED, sending an empty value is preferable */
1694 if (strcmp(value, "NOT_USED") == 0) {
1695 value = "";
1696 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001697
1698 len = strlen(envname) + 1;
1699 if (vmaxlen == EXTCHK_SIZE_EVAL_INIT)
1700 len += strlen(value);
1701 else
1702 len += vmaxlen;
1703
1704 if (!check->envp[idx])
1705 check->envp[idx] = malloc(len + 1);
1706
1707 if (!check->envp[idx]) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001708 Alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname);
1709 return 1;
1710 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001711 ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001712 if (ret < 0) {
1713 Alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno));
1714 return 1;
1715 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001716 else if (ret > len) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001717 Alert("Environment variable '%s' was truncated. Aborting.\n", envname);
1718 return 1;
1719 }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001720 return 0;
1721}
Simon Horman98637e52014-06-20 12:30:16 +09001722
1723static int prepare_external_check(struct check *check)
1724{
1725 struct server *s = check->server;
1726 struct proxy *px = s->proxy;
1727 struct listener *listener = NULL, *l;
1728 int i;
Simon Horman98637e52014-06-20 12:30:16 +09001729 const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001730 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001731
1732 list_for_each_entry(l, &px->conf.listeners, by_fe)
1733 /* Use the first INET, INET6 or UNIX listener */
1734 if (l->addr.ss_family == AF_INET ||
1735 l->addr.ss_family == AF_INET6 ||
1736 l->addr.ss_family == AF_UNIX) {
1737 listener = l;
1738 break;
1739 }
1740
Simon Horman98637e52014-06-20 12:30:16 +09001741 check->curpid = NULL;
Cyril Bontéac92a062014-12-27 22:28:38 +01001742 check->envp = calloc((EXTCHK_SIZE + 1), sizeof(char *));
1743 if (!check->envp) {
1744 Alert("Failed to allocate memory for environment variables. Aborting\n");
1745 goto err;
1746 }
Simon Horman98637e52014-06-20 12:30:16 +09001747
Cyril Bontéac92a062014-12-27 22:28:38 +01001748 check->argv = calloc(6, sizeof(char *));
1749 if (!check->argv) {
1750 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001751 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001752 }
Simon Horman98637e52014-06-20 12:30:16 +09001753
1754 check->argv[0] = px->check_command;
1755
Cyril Bonté777be862014-12-02 21:21:35 +01001756 if (!listener) {
1757 check->argv[1] = strdup("NOT_USED");
1758 check->argv[2] = strdup("NOT_USED");
1759 }
1760 else if (listener->addr.ss_family == AF_INET ||
Simon Horman98637e52014-06-20 12:30:16 +09001761 listener->addr.ss_family == AF_INET6) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001762 addr_to_str(&listener->addr, buf, sizeof(buf));
1763 check->argv[1] = strdup(buf);
1764 port_to_str(&listener->addr, buf, sizeof(buf));
1765 check->argv[2] = strdup(buf);
Cyril Bonté777be862014-12-02 21:21:35 +01001766 }
1767 else if (listener->addr.ss_family == AF_UNIX) {
Simon Horman98637e52014-06-20 12:30:16 +09001768 const struct sockaddr_un *un;
1769
1770 un = (struct sockaddr_un *)&listener->addr;
1771 check->argv[1] = strdup(un->sun_path);
1772 check->argv[2] = strdup("NOT_USED");
Cyril Bonté777be862014-12-02 21:21:35 +01001773 }
1774 else {
Cyril Bontéac92a062014-12-27 22:28:38 +01001775 Alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001776 goto err;
1777 }
1778
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001779 addr_to_str(&s->addr, buf, sizeof(buf));
1780 check->argv[3] = strdup(buf);
1781 port_to_str(&s->addr, buf, sizeof(buf));
1782 check->argv[4] = strdup(buf);
Simon Horman98637e52014-06-20 12:30:16 +09001783
Cyril Bontéac92a062014-12-27 22:28:38 +01001784 for (i = 0; i < 5; i++) {
1785 if (!check->argv[i]) {
1786 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001787 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001788 }
1789 }
Simon Horman98637e52014-06-20 12:30:16 +09001790
Cyril Bontéac92a062014-12-27 22:28:38 +01001791 EXTCHK_SETENV(check, EXTCHK_PATH, path, err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001792 /* Add proxy environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001793 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err);
1794 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err);
1795 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err);
1796 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001797 /* Add server environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001798 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err);
1799 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err);
1800 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err);
1801 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err);
1802 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err);
1803 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), err);
1804
1805 /* Ensure that we don't leave any hole in check->envp */
1806 for (i = 0; i < EXTCHK_SIZE; i++)
1807 if (!check->envp[i])
1808 EXTCHK_SETENV(check, i, "", err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001809
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001810 return 1;
Simon Horman98637e52014-06-20 12:30:16 +09001811err:
1812 if (check->envp) {
Cyril Bontéac92a062014-12-27 22:28:38 +01001813 for (i = 0; i < EXTCHK_SIZE; i++)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001814 free(check->envp[i]);
Simon Horman98637e52014-06-20 12:30:16 +09001815 free(check->envp);
1816 check->envp = NULL;
1817 }
1818
1819 if (check->argv) {
1820 for (i = 1; i < 5; i++)
1821 free(check->argv[i]);
1822 free(check->argv);
1823 check->argv = NULL;
1824 }
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001825 return 0;
Simon Horman98637e52014-06-20 12:30:16 +09001826}
1827
Simon Hormanb00d17a2014-06-13 16:18:16 +09001828/*
Simon Horman98637e52014-06-20 12:30:16 +09001829 * establish a server health-check that makes use of a process.
1830 *
1831 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001832 * - SF_ERR_NONE if everything's OK
1833 * - SF_ERR_SRVTO if there are no more servers
1834 * - SF_ERR_SRVCL if the connection was refused by the server
1835 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1836 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1837 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +01001838 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Horman98637e52014-06-20 12:30:16 +09001839 *
1840 * Blocks and then unblocks SIGCHLD
1841 */
1842static int connect_proc_chk(struct task *t)
1843{
Cyril Bontéac92a062014-12-27 22:28:38 +01001844 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001845 struct check *check = t->context;
1846 struct server *s = check->server;
1847 struct proxy *px = s->proxy;
1848 int status;
1849 pid_t pid;
1850
Willy Tarreaue7dff022015-04-03 01:14:29 +02001851 status = SF_ERR_RESOURCE;
Simon Horman98637e52014-06-20 12:30:16 +09001852
1853 block_sigchld();
1854
1855 pid = fork();
1856 if (pid < 0) {
1857 Alert("Failed to fork process for external health check: %s. Aborting.\n",
1858 strerror(errno));
1859 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1860 goto out;
1861 }
1862 if (pid == 0) {
1863 /* Child */
1864 extern char **environ;
Willy Tarreaub7b24782016-06-21 15:32:29 +02001865 int fd;
1866
1867 /* close all FDs. Keep stdin/stdout/stderr in verbose mode */
1868 fd = (global.mode & (MODE_QUIET|MODE_VERBOSE)) == MODE_QUIET ? 0 : 3;
1869
1870 while (fd < global.rlimit_nofile)
1871 close(fd++);
1872
Simon Horman98637e52014-06-20 12:30:16 +09001873 environ = check->envp;
Cyril Bontéac92a062014-12-27 22:28:38 +01001874 extchk_setenv(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)));
Simon Horman98637e52014-06-20 12:30:16 +09001875 execvp(px->check_command, check->argv);
1876 Alert("Failed to exec process for external health check: %s. Aborting.\n",
1877 strerror(errno));
1878 exit(-1);
1879 }
1880
1881 /* Parent */
1882 if (check->result == CHK_RES_UNKNOWN) {
1883 if (pid_list_add(pid, t) != NULL) {
1884 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1885
1886 if (px->timeout.check && px->timeout.connect) {
1887 int t_con = tick_add(now_ms, px->timeout.connect);
1888 t->expire = tick_first(t->expire, t_con);
1889 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02001890 status = SF_ERR_NONE;
Simon Horman98637e52014-06-20 12:30:16 +09001891 goto out;
1892 }
1893 else {
1894 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1895 }
1896 kill(pid, SIGTERM); /* process creation error */
1897 }
1898 else
1899 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1900
1901out:
1902 unblock_sigchld();
1903 return status;
1904}
1905
1906/*
Simon Horman98637e52014-06-20 12:30:16 +09001907 * manages a server health-check that uses a process. Returns
Willy Tarreaubaaee002006-06-26 02:48:02 +02001908 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1909 */
Simon Horman98637e52014-06-20 12:30:16 +09001910static struct task *process_chk_proc(struct task *t)
1911{
1912 struct check *check = t->context;
1913 struct server *s = check->server;
1914 struct connection *conn = check->conn;
1915 int rv;
1916 int ret;
1917 int expired = tick_is_expired(t->expire, now_ms);
1918
1919 if (!(check->state & CHK_ST_INPROGRESS)) {
1920 /* no check currently running */
1921 if (!expired) /* woke up too early */
1922 return t;
1923
1924 /* we don't send any health-checks when the proxy is
1925 * stopped, the server should not be checked or the check
1926 * is disabled.
1927 */
1928 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
1929 s->proxy->state == PR_STSTOPPED)
1930 goto reschedule;
1931
1932 /* we'll initiate a new check */
1933 set_server_check_status(check, HCHK_STATUS_START, NULL);
1934
1935 check->state |= CHK_ST_INPROGRESS;
1936
Simon Hormandbf70192015-01-30 11:22:53 +09001937 ret = connect_proc_chk(t);
Simon Horman98637e52014-06-20 12:30:16 +09001938 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001939 case SF_ERR_UP:
Simon Horman98637e52014-06-20 12:30:16 +09001940 return t;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001941 case SF_ERR_NONE:
Simon Horman98637e52014-06-20 12:30:16 +09001942 /* we allow up to min(inter, timeout.connect) for a connection
1943 * to establish but only when timeout.check is set
1944 * as it may be to short for a full check otherwise
1945 */
1946 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1947
1948 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1949 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1950 t->expire = tick_first(t->expire, t_con);
1951 }
1952
1953 goto reschedule;
1954
Willy Tarreaue7dff022015-04-03 01:14:29 +02001955 case SF_ERR_SRVTO: /* ETIMEDOUT */
1956 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Simon Horman98637e52014-06-20 12:30:16 +09001957 conn->flags |= CO_FL_ERROR;
1958 chk_report_conn_err(conn, errno, 0);
1959 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001960 case SF_ERR_PRXCOND:
1961 case SF_ERR_RESOURCE:
1962 case SF_ERR_INTERNAL:
Simon Horman98637e52014-06-20 12:30:16 +09001963 conn->flags |= CO_FL_ERROR;
1964 chk_report_conn_err(conn, 0, 0);
1965 break;
1966 }
1967
1968 /* here, we have seen a synchronous error, no fd was allocated */
1969
1970 check->state &= ~CHK_ST_INPROGRESS;
1971 check_notify_failure(check);
1972
1973 /* we allow up to min(inter, timeout.connect) for a connection
1974 * to establish but only when timeout.check is set
1975 * as it may be to short for a full check otherwise
1976 */
1977 while (tick_is_expired(t->expire, now_ms)) {
1978 int t_con;
1979
1980 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1981 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1982
1983 if (s->proxy->timeout.check)
1984 t->expire = tick_first(t->expire, t_con);
1985 }
1986 }
1987 else {
1988 /* there was a test running.
1989 * First, let's check whether there was an uncaught error,
1990 * which can happen on connect timeout or error.
1991 */
1992 if (check->result == CHK_RES_UNKNOWN) {
1993 /* good connection is enough for pure TCP check */
1994 struct pid_list *elem = check->curpid;
1995 int status = HCHK_STATUS_UNKNOWN;
1996
1997 if (elem->exited) {
1998 status = elem->status; /* Save in case the process exits between use below */
1999 if (!WIFEXITED(status))
2000 check->code = -1;
2001 else
2002 check->code = WEXITSTATUS(status);
2003 if (!WIFEXITED(status) || WEXITSTATUS(status))
2004 status = HCHK_STATUS_PROCERR;
2005 else
2006 status = HCHK_STATUS_PROCOK;
2007 } else if (expired) {
2008 status = HCHK_STATUS_PROCTOUT;
Willy Tarreaudc3d1902014-07-08 00:56:27 +02002009 Warning("kill %d\n", (int)elem->pid);
Simon Horman98637e52014-06-20 12:30:16 +09002010 kill(elem->pid, SIGTERM);
2011 }
2012 set_server_check_status(check, status, NULL);
2013 }
2014
2015 if (check->result == CHK_RES_FAILED) {
2016 /* a failure or timeout detected */
2017 check_notify_failure(check);
2018 }
2019 else if (check->result == CHK_RES_CONDPASS) {
2020 /* check is OK but asks for stopping mode */
2021 check_notify_stopping(check);
2022 }
2023 else if (check->result == CHK_RES_PASSED) {
2024 /* a success was detected */
2025 check_notify_success(check);
2026 }
2027 check->state &= ~CHK_ST_INPROGRESS;
2028
2029 pid_list_del(check->curpid);
2030
2031 rv = 0;
2032 if (global.spread_checks > 0) {
2033 rv = srv_getinter(check) * global.spread_checks / 100;
2034 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
2035 }
2036 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
2037 }
2038
2039 reschedule:
2040 while (tick_is_expired(t->expire, now_ms))
2041 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2042 return t;
2043}
2044
2045/*
2046 * manages a server health-check that uses a connection. Returns
2047 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2048 */
2049static struct task *process_chk_conn(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002050{
Simon Horman4a741432013-02-23 15:35:38 +09002051 struct check *check = t->context;
2052 struct server *s = check->server;
2053 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002054 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002055 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002056 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002057
Willy Tarreau2c115e52013-12-11 19:41:16 +01002058 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01002059 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002060 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01002061 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002062
Simon Horman671b6f02013-11-25 10:46:39 +09002063 /* we don't send any health-checks when the proxy is
2064 * stopped, the server should not be checked or the check
2065 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002066 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01002067 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01002068 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01002069 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002070
2071 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09002072 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002073
Willy Tarreau2c115e52013-12-11 19:41:16 +01002074 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09002075 check->bi->p = check->bi->data;
2076 check->bi->i = 0;
2077 check->bo->p = check->bo->data;
2078 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002079
Simon Hormandbf70192015-01-30 11:22:53 +09002080 ret = connect_conn_chk(t);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002081 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002082 case SF_ERR_UP:
Simon Hormanb00d17a2014-06-13 16:18:16 +09002083 return t;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002084 case SF_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002085 /* we allow up to min(inter, timeout.connect) for a connection
2086 * to establish but only when timeout.check is set
2087 * as it may be to short for a full check otherwise
2088 */
Simon Horman4a741432013-02-23 15:35:38 +09002089 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002090
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002091 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2092 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2093 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002094 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01002095
2096 if (check->type)
2097 conn_data_want_recv(conn); /* prepare for reading a possible reply */
2098
Willy Tarreau5a78f362012-11-23 12:47:05 +01002099 goto reschedule;
2100
Willy Tarreaue7dff022015-04-03 01:14:29 +02002101 case SF_ERR_SRVTO: /* ETIMEDOUT */
2102 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau4bd07de2014-01-24 16:10:57 +01002103 conn->flags |= CO_FL_ERROR;
2104 chk_report_conn_err(conn, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002105 break;
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02002106 /* should share same code than cases below */
2107 case SF_ERR_CHK_PORT:
2108 check->state |= CHK_ST_PORT_MISS;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002109 case SF_ERR_PRXCOND:
2110 case SF_ERR_RESOURCE:
2111 case SF_ERR_INTERNAL:
Willy Tarreau4bd07de2014-01-24 16:10:57 +01002112 conn->flags |= CO_FL_ERROR;
2113 chk_report_conn_err(conn, 0, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002114 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002115 }
2116
Willy Tarreau5a78f362012-11-23 12:47:05 +01002117 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01002118
Willy Tarreau2c115e52013-12-11 19:41:16 +01002119 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02002120 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002121
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002122 /* we allow up to min(inter, timeout.connect) for a connection
2123 * to establish but only when timeout.check is set
2124 * as it may be to short for a full check otherwise
2125 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002126 while (tick_is_expired(t->expire, now_ms)) {
2127 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002128
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002129 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09002130 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002131
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002132 if (s->proxy->timeout.check)
2133 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002134 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002135 }
2136 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02002137 /* there was a test running.
2138 * First, let's check whether there was an uncaught error,
2139 * which can happen on connect timeout or error.
2140 */
Simon Hormanccaabcd2014-06-20 12:29:47 +09002141 if (check->result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002142 /* good connection is enough for pure TCP check */
2143 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09002144 if (check->use_ssl)
2145 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02002146 else
Simon Horman4a741432013-02-23 15:35:38 +09002147 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002148 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002149 else if ((conn->flags & CO_FL_ERROR) || expired) {
2150 chk_report_conn_err(conn, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02002151 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002152 else
2153 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02002154 }
2155
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002156 /* check complete or aborted */
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002157 if (conn->xprt) {
2158 /* The check was aborted and the connection was not yet closed.
2159 * This can happen upon timeout, or when an external event such
2160 * as a failed response coupled with "observe layer7" caused the
2161 * server state to be suddenly changed.
2162 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01002163 conn_sock_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02002164 conn_force_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002165 }
2166
Willy Tarreauaf549582014-05-16 17:37:50 +02002167 if (check->result == CHK_RES_FAILED) {
2168 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002169 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002170 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002171 else if (check->result == CHK_RES_CONDPASS) {
2172 /* check is OK but asks for stopping mode */
2173 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002174 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002175 else if (check->result == CHK_RES_PASSED) {
2176 /* a success was detected */
2177 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002178 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01002179 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002180
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002181 rv = 0;
2182 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002183 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002184 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002185 }
Simon Horman4a741432013-02-23 15:35:38 +09002186 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002187 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002188
2189 reschedule:
2190 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002191 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002192 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01002193 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002194}
2195
Simon Horman98637e52014-06-20 12:30:16 +09002196/*
2197 * manages a server health-check. Returns
2198 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2199 */
2200static struct task *process_chk(struct task *t)
2201{
2202 struct check *check = t->context;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002203 struct server *s = check->server;
2204 struct dns_resolution *resolution = s->resolution;
2205
2206 /* trigger name resolution */
2207 if ((s->check.state & CHK_ST_ENABLED) && (resolution)) {
2208 /* check if a no resolution is running for this server */
2209 if (resolution->step == RSLV_STEP_NONE) {
2210 /*
2211 * if there has not been any name resolution for a longer period than
2212 * hold.valid, let's trigger a new one.
2213 */
Baptiste Assmannf0d93702015-09-03 10:59:39 +02002214 if (!resolution->last_resolution || tick_is_expired(tick_add(resolution->last_resolution, resolution->resolvers->hold.valid), now_ms)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002215 trigger_resolution(s);
2216 }
2217 }
2218 }
Simon Horman98637e52014-06-20 12:30:16 +09002219
2220 if (check->type == PR_O2_EXT_CHK)
2221 return process_chk_proc(t);
2222 return process_chk_conn(t);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002223
2224}
2225
2226/*
2227 * Initiates a new name resolution:
2228 * - generates a query id
2229 * - configure the resolution structure
2230 * - startup the resolvers task if required
2231 *
2232 * returns:
2233 * - 0 in case of error or if resolution already running
2234 * - 1 if everything started properly
2235 */
2236int trigger_resolution(struct server *s)
2237{
2238 struct dns_resolution *resolution;
2239 struct dns_resolvers *resolvers;
2240 int query_id;
2241 int i;
2242
2243 resolution = s->resolution;
2244 resolvers = resolution->resolvers;
2245
2246 /*
2247 * check if a resolution has already been started for this server
2248 * return directly to avoid resolution pill up
2249 */
2250 if (resolution->step != RSLV_STEP_NONE)
2251 return 0;
2252
2253 /* generates a query id */
2254 i = 0;
2255 do {
2256 query_id = dns_rnd16();
2257 /* we do try only 100 times to find a free query id */
2258 if (i++ > 100) {
2259 chunk_printf(&trash, "could not generate a query id for %s/%s, in resolvers %s",
2260 s->proxy->id, s->id, resolvers->id);
2261
2262 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2263 return 0;
2264 }
2265 } while (eb32_lookup(&resolvers->query_ids, query_id));
2266
2267 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
2268
2269 /* now update resolution parameters */
2270 resolution->query_id = query_id;
2271 resolution->qid.key = query_id;
2272 resolution->step = RSLV_STEP_RUNNING;
Thierry Fournierada34842016-02-17 21:25:09 +01002273 resolution->opts = &s->dns_opts;
2274 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00002275 resolution->query_type = DNS_RTYPE_A;
2276 } else {
2277 resolution->query_type = DNS_RTYPE_AAAA;
2278 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002279 resolution->try = resolvers->resolve_retries;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002280 resolution->try_cname = 0;
2281 resolution->nb_responses = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002282 eb32_insert(&resolvers->query_ids, &resolution->qid);
2283
2284 dns_send_query(resolution);
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002285 resolution->try -= 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002286
2287 /* update wakeup date if this resolution is the only one in the FIFO list */
2288 if (dns_check_resolution_queue(resolvers) == 1) {
2289 /* update task timeout */
2290 dns_update_resolvers_timeout(resolvers);
2291 task_queue(resolvers->t);
2292 }
2293
2294 return 1;
Simon Horman98637e52014-06-20 12:30:16 +09002295}
2296
Simon Horman5c942422013-11-25 10:46:32 +09002297static int start_check_task(struct check *check, int mininter,
2298 int nbcheck, int srvpos)
2299{
2300 struct task *t;
2301 /* task for the check */
2302 if ((t = task_new()) == NULL) {
2303 Alert("Starting [%s:%s] check: out of memory.\n",
2304 check->server->proxy->id, check->server->id);
2305 return 0;
2306 }
2307
2308 check->task = t;
2309 t->process = process_chk;
2310 t->context = check;
2311
Willy Tarreau1746eec2014-04-25 10:46:47 +02002312 if (mininter < srv_getinter(check))
2313 mininter = srv_getinter(check);
2314
2315 if (global.max_spread_checks && mininter > global.max_spread_checks)
2316 mininter = global.max_spread_checks;
2317
Simon Horman5c942422013-11-25 10:46:32 +09002318 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002319 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002320 check->start = now;
2321 task_queue(t);
2322
2323 return 1;
2324}
2325
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002326/*
2327 * Start health-check.
Willy Tarreau865c5142016-12-21 20:04:48 +01002328 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002329 */
Willy Tarreau865c5142016-12-21 20:04:48 +01002330static int start_checks()
2331{
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002332
2333 struct proxy *px;
2334 struct server *s;
2335 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002336 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002337
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002338 /* 1- count the checkers to run simultaneously.
2339 * We also determine the minimum interval among all of those which
2340 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2341 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002342 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002343 * too short an interval for all others.
2344 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002345 for (px = proxy; px; px = px->next) {
2346 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002347 if (s->slowstart) {
2348 if ((t = task_new()) == NULL) {
2349 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Willy Tarreau865c5142016-12-21 20:04:48 +01002350 return ERR_ALERT | ERR_FATAL;
Willy Tarreaue7b73482013-11-21 11:50:50 +01002351 }
2352 /* We need a warmup task that will be called when the server
2353 * state switches from down to up.
2354 */
2355 s->warmup = t;
2356 t->process = server_warmup;
2357 t->context = s;
2358 t->expire = TICK_ETERNITY;
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002359 /* server can be in this state only because of */
2360 if (s->state == SRV_ST_STARTING)
2361 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 +01002362 }
2363
Willy Tarreaud8514a22013-12-11 21:10:14 +01002364 if (s->check.state & CHK_ST_CONFIGURED) {
2365 nbcheck++;
2366 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2367 (!mininter || mininter > srv_getinter(&s->check)))
2368 mininter = srv_getinter(&s->check);
2369 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002370
Willy Tarreaud8514a22013-12-11 21:10:14 +01002371 if (s->agent.state & CHK_ST_CONFIGURED) {
2372 nbcheck++;
2373 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2374 (!mininter || mininter > srv_getinter(&s->agent)))
2375 mininter = srv_getinter(&s->agent);
2376 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002377 }
2378 }
2379
Simon Horman4a741432013-02-23 15:35:38 +09002380 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002381 return 0;
2382
2383 srand((unsigned)time(NULL));
2384
2385 /*
2386 * 2- start them as far as possible from each others. For this, we will
2387 * start them after their interval set to the min interval divided by
2388 * the number of servers, weighted by the server's position in the list.
2389 */
2390 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002391 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2392 if (init_pid_list()) {
2393 Alert("Starting [%s] check: out of memory.\n", px->id);
Willy Tarreau865c5142016-12-21 20:04:48 +01002394 return ERR_ALERT | ERR_FATAL;
Simon Horman98637e52014-06-20 12:30:16 +09002395 }
2396 }
2397
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002398 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002399 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002400 if (s->check.state & CHK_ST_CONFIGURED) {
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002401 if (s->check.type == PR_O2_EXT_CHK) {
2402 if (!prepare_external_check(&s->check))
Willy Tarreau865c5142016-12-21 20:04:48 +01002403 return ERR_ALERT | ERR_FATAL;
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002404 }
Simon Hormand60d6912013-11-25 10:46:36 +09002405 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
Willy Tarreau865c5142016-12-21 20:04:48 +01002406 return ERR_ALERT | ERR_FATAL;
Simon Hormand60d6912013-11-25 10:46:36 +09002407 srvpos++;
2408 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002409
Simon Hormand60d6912013-11-25 10:46:36 +09002410 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002411 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002412 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
Willy Tarreau865c5142016-12-21 20:04:48 +01002413 return ERR_ALERT | ERR_FATAL;
Simon Hormand60d6912013-11-25 10:46:36 +09002414 }
2415 srvpos++;
2416 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002417 }
2418 }
2419 return 0;
2420}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002421
2422/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002423 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002424 * The buffer MUST be terminated by a null byte before calling this function.
2425 * Sets server status appropriately. The caller is responsible for ensuring
2426 * that the buffer contains at least 13 characters. If <done> is zero, we may
2427 * return 0 to indicate that data is required to decide of a match.
2428 */
2429static int httpchk_expect(struct server *s, int done)
2430{
2431 static char status_msg[] = "HTTP status check returned code <000>";
2432 char status_code[] = "000";
2433 char *contentptr;
2434 int crlf;
2435 int ret;
2436
2437 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2438 case PR_O2_EXP_STS:
2439 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002440 memcpy(status_code, s->check.bi->data + 9, 3);
2441 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002442
2443 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2444 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2445 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002446 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002447
2448 /* we necessarily have the response, so there are no partial failures */
2449 if (s->proxy->options2 & PR_O2_EXP_INV)
2450 ret = !ret;
2451
Simon Horman4a741432013-02-23 15:35:38 +09002452 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002453 break;
2454
2455 case PR_O2_EXP_STR:
2456 case PR_O2_EXP_RSTR:
2457 /* very simple response parser: ignore CR and only count consecutive LFs,
2458 * stop with contentptr pointing to first char after the double CRLF or
2459 * to '\0' if crlf < 2.
2460 */
2461 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002462 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002463 if (crlf >= 2)
2464 break;
2465 if (*contentptr == '\r')
2466 continue;
2467 else if (*contentptr == '\n')
2468 crlf++;
2469 else
2470 crlf = 0;
2471 }
2472
2473 /* Check that response contains a body... */
2474 if (crlf < 2) {
2475 if (!done)
2476 return 0;
2477
Simon Horman4a741432013-02-23 15:35:38 +09002478 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002479 "HTTP content check could not find a response body");
2480 return 1;
2481 }
2482
2483 /* Check that response body is not empty... */
2484 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002485 if (!done)
2486 return 0;
2487
Simon Horman4a741432013-02-23 15:35:38 +09002488 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002489 "HTTP content check found empty response body");
2490 return 1;
2491 }
2492
2493 /* Check the response content against the supplied string
2494 * or regex... */
2495 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2496 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2497 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002498 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002499
2500 /* if we don't match, we may need to wait more */
2501 if (!ret && !done)
2502 return 0;
2503
2504 if (ret) {
2505 /* content matched */
2506 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002507 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002508 "HTTP check matched unwanted content");
2509 else
Simon Horman4a741432013-02-23 15:35:38 +09002510 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002511 "HTTP content check matched");
2512 }
2513 else {
2514 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002515 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002516 "HTTP check did not match unwanted content");
2517 else
Simon Horman4a741432013-02-23 15:35:38 +09002518 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002519 "HTTP content check did not match");
2520 }
2521 break;
2522 }
2523 return 1;
2524}
2525
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002526/*
2527 * return the id of a step in a send/expect session
2528 */
Simon Hormane16c1b32015-01-30 11:22:57 +09002529static int tcpcheck_get_step_id(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002530{
2531 struct tcpcheck_rule *cur = NULL, *next = NULL;
2532 int i = 0;
2533
Willy Tarreau213c6782014-10-02 14:51:02 +02002534 /* not even started anything yet => step 0 = initial connect */
Baptiste Assmannf95bc8e2015-04-25 16:03:06 +02002535 if (!check->current_step)
Willy Tarreau213c6782014-10-02 14:51:02 +02002536 return 0;
2537
Simon Hormane16c1b32015-01-30 11:22:57 +09002538 cur = check->last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002539
2540 /* no step => first step */
2541 if (cur == NULL)
2542 return 1;
2543
2544 /* increment i until current step */
Simon Hormane16c1b32015-01-30 11:22:57 +09002545 list_for_each_entry(next, check->tcpcheck_rules, list) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002546 if (next->list.p == &cur->list)
2547 break;
2548 ++i;
2549 }
2550
2551 return i;
2552}
2553
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002554/*
2555 * return the latest known comment before (including) the given stepid
2556 * returns NULL if no comment found
2557 */
2558static char * tcpcheck_get_step_comment(struct check *check, int stepid)
2559{
2560 struct tcpcheck_rule *cur = NULL;
2561 char *ret = NULL;
2562 int i = 0;
2563
2564 /* not even started anything yet, return latest comment found before any action */
2565 if (!check->current_step) {
2566 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2567 if (cur->action == TCPCHK_ACT_COMMENT)
2568 ret = cur->comment;
2569 else
2570 goto return_comment;
2571 }
2572 }
2573
2574 i = 1;
2575 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2576 if (cur->comment)
2577 ret = cur->comment;
2578
2579 if (i >= stepid)
2580 goto return_comment;
2581
2582 ++i;
2583 }
2584
2585 return_comment:
2586 return ret;
2587}
2588
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002589static void tcpcheck_main(struct connection *conn)
2590{
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002591 char *contentptr, *comment;
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002592 struct tcpcheck_rule *next;
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002593 int done = 0, ret = 0, step = 0;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002594 struct check *check = conn->owner;
2595 struct server *s = check->server;
2596 struct task *t = check->task;
Simon Hormane16c1b32015-01-30 11:22:57 +09002597 struct list *head = check->tcpcheck_rules;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002598
Willy Tarreauef953952014-10-02 14:30:14 +02002599 /* here, we know that the check is complete or that it failed */
2600 if (check->result != CHK_RES_UNKNOWN)
2601 goto out_end_tcpcheck;
2602
2603 /* We have 4 possibilities here :
2604 * 1. we've not yet attempted step 1, and step 1 is a connect, so no
2605 * connection attempt was made yet ;
2606 * 2. we've not yet attempted step 1, and step 1 is a not connect or
2607 * does not exist (no rule), so a connection attempt was made
2608 * before coming here.
2609 * 3. we're coming back after having started with step 1, so we may
2610 * be waiting for a connection attempt to complete.
2611 * 4. the connection + handshake are complete
2612 *
2613 * #2 and #3 are quite similar, we want both the connection and the
2614 * handshake to complete before going any further. Thus we must always
2615 * wait for a connection to complete unless we're before and existing
2616 * step 1.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002617 */
Willy Tarreau449f9522015-05-13 15:39:48 +02002618
2619 /* find first rule and skip comments */
2620 next = LIST_NEXT(head, struct tcpcheck_rule *, list);
2621 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
2622 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
2623
Willy Tarreauef953952014-10-02 14:30:14 +02002624 if ((!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE)) &&
Willy Tarreau449f9522015-05-13 15:39:48 +02002625 (check->current_step || &next->list == head)) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002626 /* we allow up to min(inter, timeout.connect) for a connection
2627 * to establish but only when timeout.check is set
2628 * as it may be to short for a full check otherwise
2629 */
2630 while (tick_is_expired(t->expire, now_ms)) {
2631 int t_con;
2632
2633 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2634 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2635
2636 if (s->proxy->timeout.check)
2637 t->expire = tick_first(t->expire, t_con);
2638 }
2639 return;
2640 }
2641
Willy Tarreauef953952014-10-02 14:30:14 +02002642 /* special case: option tcp-check with no rule, a connect is enough */
Willy Tarreau449f9522015-05-13 15:39:48 +02002643 if (&next->list == head) {
Willy Tarreauef953952014-10-02 14:30:14 +02002644 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002645 goto out_end_tcpcheck;
Willy Tarreauef953952014-10-02 14:30:14 +02002646 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002647
Willy Tarreau213c6782014-10-02 14:51:02 +02002648 /* no step means first step initialisation */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002649 if (check->current_step == NULL) {
Willy Tarreau213c6782014-10-02 14:51:02 +02002650 check->last_started_step = NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002651 check->bo->p = check->bo->data;
2652 check->bo->o = 0;
2653 check->bi->p = check->bi->data;
2654 check->bi->i = 0;
Willy Tarreau449f9522015-05-13 15:39:48 +02002655 check->current_step = next;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002656 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2657 if (s->proxy->timeout.check)
2658 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2659 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002660
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002661 /* It's only the rules which will enable send/recv */
2662 __conn_data_stop_both(conn);
2663
Willy Tarreauabca5b62013-12-06 14:19:25 +01002664 while (1) {
Willy Tarreau263013d2015-05-13 11:59:14 +02002665 /* We have to try to flush the output buffer before reading, at
2666 * the end, or if we're about to send a string that does not fit
2667 * in the remaining space. That explains why we break out of the
2668 * loop after this control.
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002669 */
2670 if (check->bo->o &&
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002671 (&check->current_step->list == head ||
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002672 check->current_step->action != TCPCHK_ACT_SEND ||
2673 check->current_step->string_len >= buffer_total_space(check->bo))) {
2674
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002675 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002676 if (conn->flags & CO_FL_ERROR) {
2677 chk_report_conn_err(conn, errno, 0);
2678 __conn_data_stop_both(conn);
2679 goto out_end_tcpcheck;
2680 }
Willy Tarreau263013d2015-05-13 11:59:14 +02002681 break;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002682 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002683 }
2684
Willy Tarreau263013d2015-05-13 11:59:14 +02002685 if (&check->current_step->list == head)
Willy Tarreauabca5b62013-12-06 14:19:25 +01002686 break;
2687
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002688 /* have 'next' point to the next rule or NULL if we're on the
2689 * last one, connect() needs this.
2690 */
Willy Tarreau5581c272015-05-13 12:24:53 +02002691 next = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002692
2693 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002694 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002695 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002696
2697 /* NULL if we're on the last rule */
Willy Tarreauf3d34822014-12-08 12:11:28 +01002698 if (&next->list == head)
2699 next = NULL;
2700
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002701 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2702 struct protocol *proto;
2703 struct xprt_ops *xprt;
2704
2705 /* mark the step as started */
2706 check->last_started_step = check->current_step;
2707 /* first, shut existing connection */
2708 conn_force_close(conn);
2709
2710 /* prepare new connection */
2711 /* initialization */
2712 conn_init(conn);
2713 conn_attach(conn, check, &check_conn_cb);
2714 conn->target = &s->obj_type;
2715
2716 /* no client address */
2717 clear_addr(&conn->addr.from);
2718
Simon Horman41f58762015-01-30 11:22:56 +09002719 if (is_addr(&check->addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002720 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09002721 conn->addr.to = check->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002722 }
2723 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002724 /* we'll connect to the addr on the server */
2725 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002726 }
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01002727 proto = protocol_by_family(conn->addr.to.ss_family);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002728
2729 /* port */
2730 if (check->current_step->port)
2731 set_host_port(&conn->addr.to, check->current_step->port);
2732 else if (check->port)
2733 set_host_port(&conn->addr.to, check->port);
2734
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002735 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002736 xprt = xprt_get(XPRT_SSL);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002737 }
2738 else {
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002739 xprt = xprt_get(XPRT_RAW);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002740 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002741 conn_prepare(conn, proto, xprt);
2742
Willy Tarreaue7dff022015-04-03 01:14:29 +02002743 ret = SF_ERR_INTERNAL;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002744 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01002745 ret = proto->connect(conn,
2746 1 /* I/O polling is always needed */,
2747 (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002748 conn->flags |= CO_FL_WAKE_DATA;
2749 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2750 conn->send_proxy_ofs = 1;
2751 conn->flags |= CO_FL_SEND_PROXY;
2752 }
2753
2754 /* It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02002755 * - SF_ERR_NONE if everything's OK
2756 * - SF_ERR_SRVTO if there are no more servers
2757 * - SF_ERR_SRVCL if the connection was refused by the server
2758 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2759 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2760 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +01002761 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002762 * Note that we try to prevent the network stack from sending the ACK during the
2763 * connect() when a pure TCP check is used (without PROXY protocol).
2764 */
2765 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002766 case SF_ERR_NONE:
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002767 /* we allow up to min(inter, timeout.connect) for a connection
2768 * to establish but only when timeout.check is set
2769 * as it may be to short for a full check otherwise
2770 */
2771 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2772
2773 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2774 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2775 t->expire = tick_first(t->expire, t_con);
2776 }
2777 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002778 case SF_ERR_SRVTO: /* ETIMEDOUT */
2779 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002780 step = tcpcheck_get_step_id(check);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002781 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002782 step, strerror(errno));
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002783 comment = tcpcheck_get_step_comment(check, step);
2784 if (comment)
2785 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002786 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2787 goto out_end_tcpcheck;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002788 case SF_ERR_PRXCOND:
2789 case SF_ERR_RESOURCE:
2790 case SF_ERR_INTERNAL:
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002791 step = tcpcheck_get_step_id(check);
2792 chunk_printf(&trash, "TCPCHK error establishing connection at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002793 comment = tcpcheck_get_step_comment(check, step);
2794 if (comment)
2795 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002796 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2797 goto out_end_tcpcheck;
2798 }
2799
2800 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002801 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002802
2803 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002804 while (&check->current_step->list != head &&
2805 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002806 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002807
Willy Tarreauf2c87352015-05-13 12:08:21 +02002808 if (&check->current_step->list == head)
2809 break;
2810
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002811 /* don't do anything until the connection is established */
2812 if (!(conn->flags & CO_FL_CONNECTED)) {
2813 /* update expire time, should be done by process_chk */
2814 /* we allow up to min(inter, timeout.connect) for a connection
2815 * to establish but only when timeout.check is set
2816 * as it may be to short for a full check otherwise
2817 */
2818 while (tick_is_expired(t->expire, now_ms)) {
2819 int t_con;
2820
2821 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2822 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2823
2824 if (s->proxy->timeout.check)
2825 t->expire = tick_first(t->expire, t_con);
2826 }
2827 return;
2828 }
2829
2830 } /* end 'connect' */
2831 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2832 /* mark the step as started */
2833 check->last_started_step = check->current_step;
2834
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002835 /* reset the read buffer */
2836 if (*check->bi->data != '\0') {
2837 *check->bi->data = '\0';
2838 check->bi->i = 0;
2839 }
2840
2841 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
2842 conn->flags |= CO_FL_ERROR;
2843 chk_report_conn_err(conn, 0, 0);
2844 goto out_end_tcpcheck;
2845 }
2846
Willy Tarreauabca5b62013-12-06 14:19:25 +01002847 if (check->current_step->string_len >= check->bo->size) {
2848 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2849 check->current_step->string_len, check->bo->size,
Simon Hormane16c1b32015-01-30 11:22:57 +09002850 tcpcheck_get_step_id(check));
Willy Tarreauabca5b62013-12-06 14:19:25 +01002851 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2852 goto out_end_tcpcheck;
2853 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002854
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002855 /* do not try to send if there is no space */
2856 if (check->current_step->string_len >= buffer_total_space(check->bo))
2857 continue;
2858
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002859 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2860 *check->bo->p = '\0'; /* to make gdb output easier to read */
2861
Willy Tarreauabca5b62013-12-06 14:19:25 +01002862 /* go to next rule and try to send */
Willy Tarreau5581c272015-05-13 12:24:53 +02002863 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002864
2865 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002866 while (&check->current_step->list != head &&
2867 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002868 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Willy Tarreauf2c87352015-05-13 12:08:21 +02002869
2870 if (&check->current_step->list == head)
2871 break;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002872 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002873 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002874 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002875 goto out_end_tcpcheck;
2876
Willy Tarreau310987a2014-01-22 19:46:33 +01002877 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002878 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
2879 done = 1;
2880 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2881 /* Report network errors only if we got no other data. Otherwise
2882 * we'll let the upper layers decide whether the response is OK
2883 * or not. It is very common that an RST sent by the server is
2884 * reported as an error just after the last data chunk.
2885 */
2886 chk_report_conn_err(conn, errno, 0);
2887 goto out_end_tcpcheck;
2888 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002889 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002890 else
Willy Tarreau263013d2015-05-13 11:59:14 +02002891 break;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002892 }
2893
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002894 /* mark the step as started */
2895 check->last_started_step = check->current_step;
2896
2897
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002898 /* Intermediate or complete response received.
2899 * Terminate string in check->bi->data buffer.
2900 */
2901 if (check->bi->i < check->bi->size) {
2902 check->bi->data[check->bi->i] = '\0';
2903 }
2904 else {
2905 check->bi->data[check->bi->i - 1] = '\0';
2906 done = 1; /* buffer full, don't wait for more data */
2907 }
2908
2909 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002910
2911 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002912 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002913 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002914 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002915
2916 /* empty response */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002917 step = tcpcheck_get_step_id(check);
2918 chunk_printf(&trash, "TCPCHK got an empty response at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002919 comment = tcpcheck_get_step_comment(check, step);
2920 if (comment)
2921 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002922 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2923
2924 goto out_end_tcpcheck;
2925 }
2926
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002927 if (!done && (check->current_step->string != NULL) && (check->bi->i < check->current_step->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002928 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002929
Willy Tarreaua970c282013-12-06 12:47:19 +01002930 tcpcheck_expect:
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002931 if (check->current_step->string != NULL)
2932 ret = my_memmem(contentptr, check->bi->i, check->current_step->string, check->current_step->string_len) != NULL;
2933 else if (check->current_step->expect_regex != NULL)
2934 ret = regex_exec(check->current_step->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002935
2936 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002937 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002938
2939 /* matched */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002940 step = tcpcheck_get_step_id(check);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002941 if (ret) {
2942 /* matched but we did not want to => ERROR */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002943 if (check->current_step->inverse) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002944 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002945 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002946 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002947 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002948 }
2949 else {
2950 /* we were looking for a regex */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002951 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d", step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002952 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002953 comment = tcpcheck_get_step_comment(check, step);
2954 if (comment)
2955 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002956 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2957 goto out_end_tcpcheck;
2958 }
2959 /* matched and was supposed to => OK, next step */
2960 else {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002961 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002962 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002963
2964 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002965 while (&check->current_step->list != head &&
2966 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002967 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002968
Willy Tarreauf2c87352015-05-13 12:08:21 +02002969 if (&check->current_step->list == head)
2970 break;
2971
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002972 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002973 goto tcpcheck_expect;
2974 __conn_data_stop_recv(conn);
2975 }
2976 }
2977 else {
2978 /* not matched */
2979 /* not matched and was not supposed to => OK, next step */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002980 if (check->current_step->inverse) {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002981 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002982 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002983
2984 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002985 while (&check->current_step->list != head &&
2986 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002987 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002988
Willy Tarreauf2c87352015-05-13 12:08:21 +02002989 if (&check->current_step->list == head)
2990 break;
2991
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002992 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002993 goto tcpcheck_expect;
2994 __conn_data_stop_recv(conn);
2995 }
2996 /* not matched but was supposed to => ERROR */
2997 else {
2998 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002999 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003000 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02003001 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003002 }
3003 else {
3004 /* we were looking for a regex */
3005 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02003006 step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003007 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02003008 comment = tcpcheck_get_step_comment(check, step);
3009 if (comment)
3010 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003011 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
3012 goto out_end_tcpcheck;
3013 }
3014 }
3015 } /* end expect */
3016 } /* end loop over double chained step list */
3017
Willy Tarreau263013d2015-05-13 11:59:14 +02003018 /* We're waiting for some I/O to complete, we've reached the end of the
3019 * rules, or both. Do what we have to do, otherwise we're done.
3020 */
3021 if (&check->current_step->list == head && !check->bo->o) {
3022 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
3023 goto out_end_tcpcheck;
3024 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003025
Willy Tarreau53c5a042015-05-13 11:38:17 +02003026 /* warning, current_step may now point to the head */
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01003027 if (check->bo->o)
3028 __conn_data_want_send(conn);
3029
Willy Tarreau53c5a042015-05-13 11:38:17 +02003030 if (&check->current_step->list != head &&
3031 check->current_step->action == TCPCHK_ACT_EXPECT)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01003032 __conn_data_want_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003033 return;
3034
3035 out_end_tcpcheck:
3036 /* collect possible new errors */
3037 if (conn->flags & CO_FL_ERROR)
3038 chk_report_conn_err(conn, 0, 0);
3039
Baptiste Assmann69e273f2013-12-11 00:52:19 +01003040 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003041 check->current_step = NULL;
3042
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01003043 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003044 conn->flags |= CO_FL_ERROR;
3045
3046 __conn_data_stop_both(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003047 return;
3048}
3049
Simon Hormanb1900d52015-01-30 11:22:54 +09003050const char *init_check(struct check *check, int type)
3051{
3052 check->type = type;
3053
3054 /* Allocate buffer for requests... */
3055 if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3056 return "out of memory while allocating check buffer";
3057 }
3058 check->bi->size = global.tune.chksize;
3059
3060 /* Allocate buffer for responses... */
3061 if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3062 return "out of memory while allocating check buffer";
3063 }
3064 check->bo->size = global.tune.chksize;
3065
3066 /* Allocate buffer for partial results... */
3067 if ((check->conn = calloc(1, sizeof(struct connection))) == NULL) {
3068 return "out of memory while allocating check connection";
3069 }
3070
3071 check->conn->t.sock.fd = -1; /* no agent in progress yet */
3072
3073 return NULL;
3074}
3075
Simon Hormanbfb5d332015-01-30 11:22:55 +09003076void free_check(struct check *check)
3077{
3078 free(check->bi);
3079 free(check->bo);
3080 free(check->conn);
3081}
3082
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003083void email_alert_free(struct email_alert *alert)
3084{
3085 struct tcpcheck_rule *rule, *back;
3086
3087 if (!alert)
3088 return;
3089
3090 list_for_each_entry_safe(rule, back, &alert->tcpcheck_rules, list)
3091 free(rule);
3092 free(alert);
3093}
3094
3095static struct task *process_email_alert(struct task *t)
3096{
3097 struct check *check = t->context;
3098 struct email_alertq *q;
3099
3100 q = container_of(check, typeof(*q), check);
3101
3102 if (!(check->state & CHK_ST_ENABLED)) {
3103 if (LIST_ISEMPTY(&q->email_alerts)) {
3104 /* All alerts processed, delete check */
3105 task_delete(t);
3106 task_free(t);
3107 check->task = NULL;
3108 return NULL;
3109 } else {
3110 struct email_alert *alert;
3111
3112 alert = LIST_NEXT(&q->email_alerts, typeof(alert), list);
3113 check->tcpcheck_rules = &alert->tcpcheck_rules;
3114 LIST_DEL(&alert->list);
3115
3116 check->state |= CHK_ST_ENABLED;
3117 }
3118
3119 }
3120
3121 process_chk(t);
3122
3123 if (!(check->state & CHK_ST_INPROGRESS) && check->tcpcheck_rules) {
3124 struct email_alert *alert;
3125
3126 alert = container_of(check->tcpcheck_rules, typeof(*alert), tcpcheck_rules);
3127 email_alert_free(alert);
3128
3129 check->tcpcheck_rules = NULL;
3130 check->state &= ~CHK_ST_ENABLED;
3131 }
3132 return t;
3133}
3134
3135static int init_email_alert_checks(struct server *s)
3136{
3137 int i;
3138 struct mailer *mailer;
3139 const char *err_str;
3140 struct proxy *p = s->proxy;
3141
3142 if (p->email_alert.queues)
3143 /* Already initialised, nothing to do */
3144 return 1;
3145
3146 p->email_alert.queues = calloc(p->email_alert.mailers.m->count, sizeof *p->email_alert.queues);
3147 if (!p->email_alert.queues) {
3148 err_str = "out of memory while allocating checks array";
3149 goto error_alert;
3150 }
3151
3152 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3153 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
3154 struct email_alertq *q = &p->email_alert.queues[i];
3155 struct check *check = &q->check;
3156
3157
3158 LIST_INIT(&q->email_alerts);
3159
Pieter Baauw235fcfc2016-02-13 15:33:40 +01003160 check->inter = p->email_alert.mailers.m->timeout.mail;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003161 check->rise = DEF_AGENT_RISETIME;
3162 check->fall = DEF_AGENT_FALLTIME;
3163 err_str = init_check(check, PR_O2_TCPCHK_CHK);
3164 if (err_str) {
3165 goto error_free;
3166 }
3167
3168 check->xprt = mailer->xprt;
3169 if (!get_host_port(&mailer->addr))
3170 /* Default to submission port */
3171 check->port = 587;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003172 check->addr = mailer->addr;
3173 check->server = s;
3174 }
3175
3176 return 1;
3177
3178error_free:
3179 while (i-- > 1)
3180 task_free(p->email_alert.queues[i].check.task);
3181 free(p->email_alert.queues);
3182 p->email_alert.queues = NULL;
3183error_alert:
3184 Alert("Email alert [%s] could not be initialised: %s\n", p->id, err_str);
3185 return 0;
3186}
3187
3188
3189static int add_tcpcheck_expect_str(struct list *list, const char *str)
3190{
3191 struct tcpcheck_rule *tcpcheck;
3192
3193 tcpcheck = calloc(1, sizeof *tcpcheck);
3194 if (!tcpcheck)
3195 return 0;
3196
3197 tcpcheck->action = TCPCHK_ACT_EXPECT;
3198 tcpcheck->string = strdup(str);
3199 if (!tcpcheck->string) {
3200 free(tcpcheck);
3201 return 0;
3202 }
3203
3204 LIST_ADDQ(list, &tcpcheck->list);
3205 return 1;
3206}
3207
3208static int add_tcpcheck_send_strs(struct list *list, const char * const *strs)
3209{
3210 struct tcpcheck_rule *tcpcheck;
Willy Tarreau64345aa2016-08-10 19:29:09 +02003211 const char *in;
3212 char *dst;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003213 int i;
3214
3215 tcpcheck = calloc(1, sizeof *tcpcheck);
3216 if (!tcpcheck)
3217 return 0;
3218
3219 tcpcheck->action = TCPCHK_ACT_SEND;
3220
3221 tcpcheck->string_len = 0;
3222 for (i = 0; strs[i]; i++)
3223 tcpcheck->string_len += strlen(strs[i]);
3224
3225 tcpcheck->string = malloc(tcpcheck->string_len + 1);
3226 if (!tcpcheck->string) {
3227 free(tcpcheck);
3228 return 0;
3229 }
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003230
Willy Tarreau64345aa2016-08-10 19:29:09 +02003231 dst = tcpcheck->string;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003232 for (i = 0; strs[i]; i++)
Willy Tarreau64345aa2016-08-10 19:29:09 +02003233 for (in = strs[i]; (*dst = *in++); dst++);
3234 *dst = 0;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003235
3236 LIST_ADDQ(list, &tcpcheck->list);
3237 return 1;
3238}
3239
3240static int enqueue_one_email_alert(struct email_alertq *q, const char *msg)
3241{
3242 struct email_alert *alert = NULL;
3243 struct tcpcheck_rule *tcpcheck;
3244 struct check *check = &q->check;
3245 struct proxy *p = check->server->proxy;
3246
3247 alert = calloc(1, sizeof *alert);
3248 if (!alert) {
3249 goto error;
3250 }
3251 LIST_INIT(&alert->tcpcheck_rules);
3252
3253 tcpcheck = calloc(1, sizeof *tcpcheck);
3254 if (!tcpcheck)
3255 goto error;
3256 tcpcheck->action = TCPCHK_ACT_CONNECT;
3257 LIST_ADDQ(&alert->tcpcheck_rules, &tcpcheck->list);
3258
3259 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "220 "))
3260 goto error;
3261
3262 {
3263 const char * const strs[4] = { "EHLO ", p->email_alert.myhostname, "\r\n" };
3264 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3265 goto error;
3266 }
3267
3268 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3269 goto error;
3270
3271 {
3272 const char * const strs[4] = { "MAIL FROM:<", p->email_alert.from, ">\r\n" };
3273 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3274 goto error;
3275 }
3276
3277 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3278 goto error;
3279
3280 {
3281 const char * const strs[4] = { "RCPT TO:<", p->email_alert.to, ">\r\n" };
3282 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3283 goto error;
3284 }
3285
3286 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3287 goto error;
3288
3289 {
3290 const char * const strs[2] = { "DATA\r\n" };
3291 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3292 goto error;
3293 }
3294
3295 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "354 "))
3296 goto error;
3297
3298 {
3299 struct tm tm;
3300 char datestr[48];
3301 const char * const strs[18] = {
Pieter Baauw5e0964e2016-02-13 16:27:35 +01003302 "From: ", p->email_alert.from, "\r\n",
3303 "To: ", p->email_alert.to, "\r\n",
3304 "Date: ", datestr, "\r\n",
3305 "Subject: [HAproxy Alert] ", msg, "\r\n",
3306 "\r\n",
3307 msg, "\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003308 "\r\n",
Pieter Baauwed35c372015-07-22 19:51:54 +02003309 ".\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003310 NULL
3311 };
3312
3313 get_localtime(date.tv_sec, &tm);
3314
3315 if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %T %z (%Z)", &tm) == 0) {
3316 goto error;
3317 }
3318
3319 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3320 goto error;
3321 }
3322
3323 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3324 goto error;
3325
3326 {
3327 const char * const strs[2] = { "QUIT\r\n" };
3328 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3329 goto error;
3330 }
3331
3332 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "221 "))
3333 goto error;
3334
3335 if (!check->task) {
3336 struct task *t;
3337
3338 if ((t = task_new()) == NULL)
3339 goto error;
3340
3341 check->task = t;
3342 t->process = process_email_alert;
3343 t->context = check;
3344
3345 /* check this in one ms */
3346 t->expire = tick_add(now_ms, MS_TO_TICKS(1));
3347 check->start = now;
3348 task_queue(t);
3349 }
3350
3351 LIST_ADDQ(&q->email_alerts, &alert->list);
3352
3353 return 1;
3354
3355error:
3356 email_alert_free(alert);
3357 return 0;
3358}
3359
3360static void enqueue_email_alert(struct proxy *p, const char *msg)
3361{
3362 int i;
3363 struct mailer *mailer;
3364
3365 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3366 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
3367 if (!enqueue_one_email_alert(&p->email_alert.queues[i], msg)) {
3368 Alert("Email alert [%s] could not be enqueued: out of memory\n", p->id);
3369 return;
3370 }
3371 }
3372
3373 return;
3374}
3375
3376/*
3377 * Send email alert if configured.
3378 */
Simon Horman64e34162015-02-06 11:11:57 +09003379void send_email_alert(struct server *s, int level, const char *format, ...)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003380{
3381 va_list argp;
3382 char buf[1024];
3383 int len;
3384 struct proxy *p = s->proxy;
3385
Simon Horman64e34162015-02-06 11:11:57 +09003386 if (!p->email_alert.mailers.m || level > p->email_alert.level ||
3387 format == NULL || !init_email_alert_checks(s))
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003388 return;
3389
3390 va_start(argp, format);
3391 len = vsnprintf(buf, sizeof(buf), format, argp);
3392 va_end(argp);
3393
3394 if (len < 0) {
Cyril Bontéb65e0332015-12-04 03:07:08 +01003395 Alert("Email alert [%s] could not format message\n", p->id);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003396 return;
3397 }
3398
3399 enqueue_email_alert(p, buf);
3400}
3401
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003402/*
3403 * Return value:
3404 * the port to be used for the health check
3405 * 0 in case no port could be found for the check
3406 */
3407int srv_check_healthcheck_port(struct check *chk)
3408{
3409 int i = 0;
3410 struct server *srv = NULL;
3411
3412 srv = chk->server;
3413
3414 /* If neither a port nor an addr was specified and no check transport
3415 * layer is forced, then the transport layer used by the checks is the
3416 * same as for the production traffic. Otherwise we use raw_sock by
3417 * default, unless one is specified.
3418 */
3419 if (!chk->port && !is_addr(&chk->addr)) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003420 chk->use_ssl |= (srv->use_ssl || (srv->proxy->options & PR_O_TCPCHK_SSL));
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003421 chk->send_proxy |= (srv->pp_opts);
3422 }
3423
3424 /* by default, we use the health check port ocnfigured */
3425 if (chk->port > 0)
3426 return chk->port;
3427
3428 /* try to get the port from check_core.addr if check.port not set */
3429 i = get_host_port(&chk->addr);
3430 if (i > 0)
3431 return i;
3432
3433 /* try to get the port from server address */
3434 /* prevent MAPPORTS from working at this point, since checks could
3435 * not be performed in such case (MAPPORTS impose a relative ports
3436 * based on live traffic)
3437 */
3438 if (srv->flags & SRV_F_MAPPORTS)
3439 return 0;
3440 i = get_host_port(&srv->addr); /* by default */
3441 if (i > 0)
3442 return i;
3443
3444 return 0;
3445}
3446
Willy Tarreau865c5142016-12-21 20:04:48 +01003447__attribute__((constructor))
3448static void __check_init(void)
3449{
3450 hap_register_post_check(start_checks);
3451}
3452
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003453
Willy Tarreaubd741542010-03-16 18:46:54 +01003454/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003455 * Local variables:
3456 * c-indent-level: 8
3457 * c-basic-offset: 8
3458 * End:
3459 */