blob: ed9a49a24dd6e6e16e2c82bbf864889f778c4b96 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Health-checks functions.
3 *
Willy Tarreau26c25062009-03-08 09:38:41 +01004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreaub8816082008-01-18 12:18:15 +010014#include <assert.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020015#include <ctype.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <errno.h>
17#include <fcntl.h>
Willy Tarreau9b39dc52014-07-08 00:54:10 +020018#include <signal.h>
Simon Horman0ba0e4a2015-01-30 11:23:00 +090019#include <stdarg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020021#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020023#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024#include <unistd.h>
25#include <sys/socket.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040026#include <sys/types.h>
Simon Horman98637e52014-06-20 12:30:16 +090027#include <sys/wait.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028#include <netinet/in.h>
Willy Tarreau1274bc42009-07-15 07:16:31 +020029#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <arpa/inet.h>
31
Willy Tarreauc7e42382012-08-24 19:22:53 +020032#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/compat.h>
34#include <common/config.h>
35#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020036#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
39#include <types/global.h>
Simon Horman0ba0e4a2015-01-30 11:23:00 +090040#include <types/mailers.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020041#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010042#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
44#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020045#include <proto/checks.h>
William Lallemand9ed62032016-11-21 17:49:11 +010046#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/fd.h>
48#include <proto/log.h>
49#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020050#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010051#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010052#include <proto/proto_tcp.h>
Baptiste Assmann69e273f2013-12-11 00:52:19 +010053#include <proto/protocol.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010054#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/server.h>
Willy Tarreau48d6bf22016-06-21 16:27:34 +020056#include <proto/signal.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010057#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020059#include <proto/log.h>
60#include <proto/dns.h>
61#include <proto/proto_udp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020062
Olivier Houchard9130a962017-10-17 17:33:43 +020063#ifdef USE_OPENSSL
64#include <proto/ssl_sock.h>
65#endif /* USE_OPENSSL */
66
Willy Tarreaubd741542010-03-16 18:46:54 +010067static int httpchk_expect(struct server *s, int done);
Simon Hormane16c1b32015-01-30 11:22:57 +090068static int tcpcheck_get_step_id(struct check *);
Baptiste Assmann22b09d22015-05-01 08:03:04 +020069static char * tcpcheck_get_step_comment(struct check *, int);
Willy Tarreau6bdcab02017-10-04 18:41:00 +020070static int tcpcheck_main(struct check *);
Willy Tarreaubd741542010-03-16 18:46:54 +010071
Simon Horman63a4a822012-03-19 07:24:41 +090072static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010073 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
74 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020075 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020076
Willy Tarreau23964182014-05-20 20:56:30 +020077 /* Below we have finished checks */
78 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010079 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010080
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010081 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020082
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010083 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
84 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
85 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020086
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010087 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
88 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
89 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020090
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010091 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
92 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020093
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020094 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020095
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010096 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
97 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
98 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +090099
100 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
101 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +0200102 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200103};
104
Cyril Bontéac92a062014-12-27 22:28:38 +0100105const struct extcheck_env extcheck_envs[EXTCHK_SIZE] = {
106 [EXTCHK_PATH] = { "PATH", EXTCHK_SIZE_EVAL_INIT },
107 [EXTCHK_HAPROXY_PROXY_NAME] = { "HAPROXY_PROXY_NAME", EXTCHK_SIZE_EVAL_INIT },
108 [EXTCHK_HAPROXY_PROXY_ID] = { "HAPROXY_PROXY_ID", EXTCHK_SIZE_EVAL_INIT },
109 [EXTCHK_HAPROXY_PROXY_ADDR] = { "HAPROXY_PROXY_ADDR", EXTCHK_SIZE_EVAL_INIT },
110 [EXTCHK_HAPROXY_PROXY_PORT] = { "HAPROXY_PROXY_PORT", EXTCHK_SIZE_EVAL_INIT },
111 [EXTCHK_HAPROXY_SERVER_NAME] = { "HAPROXY_SERVER_NAME", EXTCHK_SIZE_EVAL_INIT },
112 [EXTCHK_HAPROXY_SERVER_ID] = { "HAPROXY_SERVER_ID", EXTCHK_SIZE_EVAL_INIT },
113 [EXTCHK_HAPROXY_SERVER_ADDR] = { "HAPROXY_SERVER_ADDR", EXTCHK_SIZE_EVAL_INIT },
114 [EXTCHK_HAPROXY_SERVER_PORT] = { "HAPROXY_SERVER_PORT", EXTCHK_SIZE_EVAL_INIT },
115 [EXTCHK_HAPROXY_SERVER_MAXCONN] = { "HAPROXY_SERVER_MAXCONN", EXTCHK_SIZE_EVAL_INIT },
116 [EXTCHK_HAPROXY_SERVER_CURCONN] = { "HAPROXY_SERVER_CURCONN", EXTCHK_SIZE_ULONG },
117};
118
Simon Horman63a4a822012-03-19 07:24:41 +0900119static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100120 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
121
122 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
123 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
124
125 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
126 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
127 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
128 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
129
130 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
131 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
132 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
133};
134
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200135/*
136 * Convert check_status code to description
137 */
138const char *get_check_status_description(short check_status) {
139
140 const char *desc;
141
142 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200143 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200144 else
145 desc = NULL;
146
147 if (desc && *desc)
148 return desc;
149 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200150 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200151}
152
153/*
154 * Convert check_status code to short info
155 */
156const char *get_check_status_info(short check_status) {
157
158 const char *info;
159
160 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200161 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200162 else
163 info = NULL;
164
165 if (info && *info)
166 return info;
167 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200168 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200169}
170
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100171const char *get_analyze_status(short analyze_status) {
172
173 const char *desc;
174
175 if (analyze_status < HANA_STATUS_SIZE)
176 desc = analyze_statuses[analyze_status].desc;
177 else
178 desc = NULL;
179
180 if (desc && *desc)
181 return desc;
182 else
183 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
184}
185
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200186/*
Simon Horman4a741432013-02-23 15:35:38 +0900187 * Set check->status, update check->duration and fill check->result with
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200188 * an adequate CHK_RES_* value. The new check->health is computed based
189 * on the result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200190 *
191 * Show information in logs about failed health check if server is UP
192 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200193 */
Simon Horman4a741432013-02-23 15:35:38 +0900194static void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100195{
Simon Horman4a741432013-02-23 15:35:38 +0900196 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200197 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200198 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900199
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200200 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100201 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900202 check->desc[0] = '\0';
203 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200204 return;
205 }
206
Simon Horman4a741432013-02-23 15:35:38 +0900207 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200208 return;
209
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200210 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900211 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
212 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200213 } else
Simon Horman4a741432013-02-23 15:35:38 +0900214 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200215
Simon Horman4a741432013-02-23 15:35:38 +0900216 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200217 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900218 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200219
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100220 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900221 check->duration = -1;
222 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200223 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900224 check->duration = tv_ms_elapsed(&check->start, &now);
225 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200226 }
227
Willy Tarreau23964182014-05-20 20:56:30 +0200228 /* no change is expected if no state change occurred */
229 if (check->result == CHK_RES_NEUTRAL)
230 return;
231
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200232 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200233
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200234 switch (check->result) {
235 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200236 /* Failure to connect to the agent as a secondary check should not
237 * cause the server to be marked down.
238 */
239 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900240 (check->status >= HCHK_STATUS_L57DATA)) &&
Willy Tarreau12634e12014-05-23 11:32:36 +0200241 (check->health >= check->rise)) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200242 s->counters.failed_checks++;
243 report = 1;
244 check->health--;
245 if (check->health < check->rise)
246 check->health = 0;
247 }
248 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200249
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200250 case CHK_RES_PASSED:
251 case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */
252 if ((check->health < check->rise + check->fall - 1) &&
253 (check->result == CHK_RES_PASSED || check->health > 0)) {
254 report = 1;
255 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200256
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200257 if (check->health >= check->rise)
258 check->health = check->rise + check->fall - 1; /* OK now */
259 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200260
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200261 /* clear consecutive_errors if observing is enabled */
262 if (s->onerror)
263 s->consecutive_errors = 0;
264 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100265
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200266 default:
267 break;
268 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200269
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200270 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
271 (status != prev_status || report)) {
272 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200273 "%s check for %sserver %s/%s %s%s",
274 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200275 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100276 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100277 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200278 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200279
Emeric Brun5a133512017-10-19 14:42:30 +0200280 srv_append_status(&trash, s, check, -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200281
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100282 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200283 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
284 (check->health >= check->rise) ? check->fall : check->rise,
285 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200286
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100287 Warning("%s.\n", trash.str);
288 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman7ea9be02015-04-30 13:10:33 +0900289 send_email_alert(s, LOG_INFO, "%s", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200290 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200291}
292
Willy Tarreau4eec5472014-05-20 22:32:27 +0200293/* Marks the check <check>'s server down if the current check is already failed
294 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200295 */
Willy Tarreau4eec5472014-05-20 22:32:27 +0200296static void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200297{
Simon Horman4a741432013-02-23 15:35:38 +0900298 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900299
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200300 /* The agent secondary check should only cause a server to be marked
301 * as down if check->status is HCHK_STATUS_L7STS, which indicates
302 * that the agent returned "fail", "stopped" or "down".
303 * The implication here is that failure to connect to the agent
304 * as a secondary check should not cause the server to be marked
305 * down. */
306 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
307 return;
308
Willy Tarreau4eec5472014-05-20 22:32:27 +0200309 if (check->health > 0)
310 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100311
Willy Tarreau4eec5472014-05-20 22:32:27 +0200312 /* We only report a reason for the check if we did not do so previously */
Emeric Brun5a133512017-10-19 14:42:30 +0200313 srv_set_stopped(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200314}
315
Willy Tarreauaf549582014-05-16 17:37:50 +0200316/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200317 * it isn't in maintenance, it is not tracking a down server and other checks
318 * comply. The rule is simple : by default, a server is up, unless any of the
319 * following conditions is true :
320 * - health check failed (check->health < rise)
321 * - agent check failed (agent->health < rise)
322 * - the server tracks a down server (track && track->state == STOPPED)
323 * Note that if the server has a slowstart, it will switch to STARTING instead
324 * of RUNNING. Also, only the health checks support the nolb mode, so the
325 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200326 */
Willy Tarreau3e048382014-05-21 10:30:54 +0200327static void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200328{
Simon Horman4a741432013-02-23 15:35:38 +0900329 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100330
Emeric Brun52a91d32017-08-31 14:41:55 +0200331 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200332 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100333
Emeric Brun52a91d32017-08-31 14:41:55 +0200334 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200335 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100336
Willy Tarreau3e048382014-05-21 10:30:54 +0200337 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
338 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100339
Willy Tarreau3e048382014-05-21 10:30:54 +0200340 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
341 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200342
Emeric Brun52a91d32017-08-31 14:41:55 +0200343 if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING)
Willy Tarreau3e048382014-05-21 10:30:54 +0200344 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100345
Emeric Brun5a133512017-10-19 14:42:30 +0200346 srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100347}
348
Willy Tarreaudb58b792014-05-21 13:57:23 +0200349/* Marks the check <check> as valid and tries to set its server into stopping mode
350 * if it was running or starting, and provided it isn't in maintenance and other
351 * checks comply. The conditions for the server to be marked in stopping mode are
352 * the same as for it to be turned up. Also, only the health checks support the
353 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200354 */
Willy Tarreaudb58b792014-05-21 13:57:23 +0200355static void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200356{
Simon Horman4a741432013-02-23 15:35:38 +0900357 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100358
Emeric Brun52a91d32017-08-31 14:41:55 +0200359 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200360 return;
361
Willy Tarreaudb58b792014-05-21 13:57:23 +0200362 if (check->state & CHK_ST_AGENT)
363 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100364
Emeric Brun52a91d32017-08-31 14:41:55 +0200365 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreaudb58b792014-05-21 13:57:23 +0200366 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100367
Willy Tarreaudb58b792014-05-21 13:57:23 +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 Tarreaudb58b792014-05-21 13:57:23 +0200371 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
372 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100373
Emeric Brun5a133512017-10-19 14:42:30 +0200374 srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100375}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200376
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100377/* note: use health_adjust() only, which first checks that the observe mode is
378 * enabled.
379 */
380void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100381{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100382 int failed;
383 int expire;
384
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100385 if (s->observe >= HANA_OBS_SIZE)
386 return;
387
Willy Tarreaubb956662013-01-24 00:37:39 +0100388 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100389 return;
390
391 switch (analyze_statuses[status].lr[s->observe - 1]) {
392 case 1:
393 failed = 1;
394 break;
395
396 case 2:
397 failed = 0;
398 break;
399
400 default:
401 return;
402 }
403
404 if (!failed) {
405 /* good: clear consecutive_errors */
406 s->consecutive_errors = 0;
407 return;
408 }
409
410 s->consecutive_errors++;
411
412 if (s->consecutive_errors < s->consecutive_errors_limit)
413 return;
414
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100415 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
416 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100417
418 switch (s->onerror) {
419 case HANA_ONERR_FASTINTER:
420 /* force fastinter - nothing to do here as all modes force it */
421 break;
422
423 case HANA_ONERR_SUDDTH:
424 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900425 if (s->check.health > s->check.rise)
426 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100427
428 /* no break - fall through */
429
430 case HANA_ONERR_FAILCHK:
431 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900432 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200433 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100434 break;
435
436 case HANA_ONERR_MARKDWN:
437 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900438 s->check.health = s->check.rise;
Simon Horman4a741432013-02-23 15:35:38 +0900439 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200440 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100441 break;
442
443 default:
444 /* write a warning? */
445 break;
446 }
447
448 s->consecutive_errors = 0;
449 s->counters.failed_hana++;
450
Simon Horman66183002013-02-23 10:16:43 +0900451 if (s->check.fastinter) {
452 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300453 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200454 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300455 /* requeue check task with new expire */
456 task_queue(s->check.task);
457 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100458 }
459}
460
Willy Tarreaua1dab552014-04-14 15:04:54 +0200461static int httpchk_build_status_header(struct server *s, char *buffer, int size)
Willy Tarreauef781042010-01-27 11:53:01 +0100462{
463 int sv_state;
464 int ratio;
465 int hlen = 0;
Joseph Lynch514061c2015-01-15 17:52:59 -0800466 char addr[46];
467 char port[6];
Willy Tarreauef781042010-01-27 11:53:01 +0100468 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
469 "UP %d/%d", "UP",
470 "NOLB %d/%d", "NOLB",
471 "no check" };
472
473 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
474 hlen += 24;
475
Willy Tarreauff5ae352013-12-11 20:36:34 +0100476 if (!(s->check.state & CHK_ST_ENABLED))
477 sv_state = 6;
Emeric Brun52a91d32017-08-31 14:41:55 +0200478 else if (s->cur_state != SRV_ST_STOPPED) {
Simon Horman58c32972013-11-25 10:46:38 +0900479 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100480 sv_state = 3; /* UP */
481 else
482 sv_state = 2; /* going down */
483
Emeric Brun52a91d32017-08-31 14:41:55 +0200484 if (s->cur_state == SRV_ST_STOPPING)
Willy Tarreauef781042010-01-27 11:53:01 +0100485 sv_state += 2;
486 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900487 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100488 sv_state = 1; /* going up */
489 else
490 sv_state = 0; /* DOWN */
491 }
492
Willy Tarreaua1dab552014-04-14 15:04:54 +0200493 hlen += snprintf(buffer + hlen, size - hlen,
Willy Tarreauef781042010-01-27 11:53:01 +0100494 srv_hlt_st[sv_state],
Emeric Brun52a91d32017-08-31 14:41:55 +0200495 (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
496 (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100497
Joseph Lynch514061c2015-01-15 17:52:59 -0800498 addr_to_str(&s->addr, addr, sizeof(addr));
Willy Tarreau04276f32017-01-06 17:41:29 +0100499 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
500 snprintf(port, sizeof(port), "%u", s->svc_port);
501 else
502 *port = 0;
Joseph Lynch514061c2015-01-15 17:52:59 -0800503
504 hlen += snprintf(buffer + hlen, size - hlen, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
505 addr, port, s->proxy->id, s->id,
Willy Tarreauef781042010-01-27 11:53:01 +0100506 global.node,
Emeric Brun52a91d32017-08-31 14:41:55 +0200507 (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
Willy Tarreauef781042010-01-27 11:53:01 +0100508 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
509 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
510 s->nbpend);
511
Emeric Brun52a91d32017-08-31 14:41:55 +0200512 if ((s->cur_state == SRV_ST_STARTING) &&
Willy Tarreauef781042010-01-27 11:53:01 +0100513 now.tv_sec < s->last_change + s->slowstart &&
514 now.tv_sec >= s->last_change) {
515 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
Willy Tarreaua1dab552014-04-14 15:04:54 +0200516 hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio);
Willy Tarreauef781042010-01-27 11:53:01 +0100517 }
518
519 buffer[hlen++] = '\r';
520 buffer[hlen++] = '\n';
521
522 return hlen;
523}
524
Willy Tarreau20a18342013-12-05 00:31:46 +0100525/* Check the connection. If an error has already been reported or the socket is
526 * closed, keep errno intact as it is supposed to contain the valid error code.
527 * If no error is reported, check the socket's error queue using getsockopt().
528 * Warning, this must be done only once when returning from poll, and never
529 * after an I/O error was attempted, otherwise the error queue might contain
530 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
531 * socket. Returns non-zero if an error was reported, zero if everything is
532 * clean (including a properly closed socket).
533 */
534static int retrieve_errno_from_socket(struct connection *conn)
535{
536 int skerr;
537 socklen_t lskerr = sizeof(skerr);
538
539 if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl))
540 return 1;
541
Willy Tarreau3c728722014-01-23 13:50:42 +0100542 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100543 return 0;
544
Willy Tarreau585744b2017-08-24 14:31:19 +0200545 if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
Willy Tarreau20a18342013-12-05 00:31:46 +0100546 errno = skerr;
547
548 if (errno == EAGAIN)
549 errno = 0;
550
551 if (!errno) {
552 /* we could not retrieve an error, that does not mean there is
553 * none. Just don't change anything and only report the prior
554 * error if any.
555 */
556 if (conn->flags & CO_FL_ERROR)
557 return 1;
558 else
559 return 0;
560 }
561
562 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
563 return 1;
564}
565
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100566/* Try to collect as much information as possible on the connection status,
567 * and adjust the server status accordingly. It may make use of <errno_bck>
568 * if non-null when the caller is absolutely certain of its validity (eg:
569 * checked just after a syscall). If the caller doesn't have a valid errno,
570 * it can pass zero, and retrieve_errno_from_socket() will be called to try
571 * to extract errno from the socket. If no error is reported, it will consider
572 * the <expired> flag. This is intended to be used when a connection error was
573 * reported in conn->flags or when a timeout was reported in <expired>. The
574 * function takes care of not updating a server status which was already set.
575 * All situations where at least one of <expired> or CO_FL_ERROR are set
576 * produce a status.
577 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200578static void chk_report_conn_err(struct check *check, int errno_bck, int expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100579{
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200580 struct connection *conn = check->conn;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100581 const char *err_msg;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200582 struct chunk *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200583 int step;
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200584 char *comment;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100585
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100586 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100587 return;
588
589 errno = errno_bck;
Willy Tarreau00149122017-10-04 18:05:01 +0200590 if (conn && (!errno || errno == EAGAIN))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100591 retrieve_errno_from_socket(conn);
592
Willy Tarreau00149122017-10-04 18:05:01 +0200593 if (conn && !(conn->flags & CO_FL_ERROR) && !expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100594 return;
595
596 /* we'll try to build a meaningful error message depending on the
597 * context of the error possibly present in conn->err_code, and the
598 * socket error possibly collected above. This is useful to know the
599 * exact step of the L6 layer (eg: SSL handshake).
600 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200601 chk = get_trash_chunk();
602
603 if (check->type == PR_O2_TCPCHK_CHK) {
Simon Hormane16c1b32015-01-30 11:22:57 +0900604 step = tcpcheck_get_step_id(check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200605 if (!step)
606 chunk_printf(chk, " at initial connection step of tcp-check");
607 else {
608 chunk_printf(chk, " at step %d of tcp-check", step);
609 /* we were looking for a string */
610 if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_CONNECT) {
611 if (check->last_started_step->port)
612 chunk_appendf(chk, " (connect port %d)" ,check->last_started_step->port);
613 else
614 chunk_appendf(chk, " (connect)");
615 }
616 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_EXPECT) {
617 if (check->last_started_step->string)
Baptiste Assmann96a5c9b2015-05-01 08:09:29 +0200618 chunk_appendf(chk, " (expect string '%s')", check->last_started_step->string);
Willy Tarreau213c6782014-10-02 14:51:02 +0200619 else if (check->last_started_step->expect_regex)
620 chunk_appendf(chk, " (expect regex)");
621 }
622 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_SEND) {
623 chunk_appendf(chk, " (send)");
624 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200625
626 comment = tcpcheck_get_step_comment(check, step);
627 if (comment)
628 chunk_appendf(chk, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200629 }
630 }
631
Willy Tarreau00149122017-10-04 18:05:01 +0200632 if (conn && conn->err_code) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100633 if (errno && errno != EAGAIN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200634 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100635 else
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200636 chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100637 err_msg = trash.str;
638 }
639 else {
640 if (errno && errno != EAGAIN) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200641 chunk_printf(&trash, "%s%s", strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100642 err_msg = trash.str;
643 }
644 else {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200645 err_msg = chk->str;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100646 }
647 }
648
Willy Tarreau00149122017-10-04 18:05:01 +0200649 if (check->state & CHK_ST_PORT_MISS) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200650 /* NOTE: this is reported after <fall> tries */
651 chunk_printf(chk, "No port available for the TCP connection");
652 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
653 }
654
Willy Tarreau00149122017-10-04 18:05:01 +0200655 if (!conn) {
656 /* connection allocation error before the connection was established */
657 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
658 }
659 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100660 /* L4 not established (yet) */
661 if (conn->flags & CO_FL_ERROR)
662 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
663 else if (expired)
664 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200665
666 /*
667 * might be due to a server IP change.
668 * Let's trigger a DNS resolution if none are currently running.
669 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200670 dns_trigger_resolution(check->server->dns_requester);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200671
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100672 }
673 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
674 /* L6 not established (yet) */
675 if (conn->flags & CO_FL_ERROR)
676 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
677 else if (expired)
678 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
679 }
680 else if (conn->flags & CO_FL_ERROR) {
681 /* I/O error after connection was established and before we could diagnose */
682 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
683 }
684 else if (expired) {
685 /* connection established but expired check */
686 if (check->type == PR_O2_SSL3_CHK)
687 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
688 else /* HTTP, SMTP, ... */
689 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
690 }
691
692 return;
693}
694
Willy Tarreaubaaee002006-06-26 02:48:02 +0200695/*
696 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200697 * the connection acknowledgement. If the proxy requires L7 health-checks,
698 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900699 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200700 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200701static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200702{
Simon Horman4a741432013-02-23 15:35:38 +0900703 struct check *check = conn->owner;
704 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900705 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200706
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100707 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100708 goto out_wakeup;
709
Willy Tarreau310987a2014-01-22 19:46:33 +0100710 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100711 return;
712
Willy Tarreau20a18342013-12-05 00:31:46 +0100713 if (retrieve_errno_from_socket(conn)) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200714 chk_report_conn_err(check, errno, 0);
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200715 __conn_xprt_stop_both(conn);
Willy Tarreau20a18342013-12-05 00:31:46 +0100716 goto out_wakeup;
717 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100718
Willy Tarreaubbae3f02017-08-30 09:59:52 +0200719 if (conn->flags & CO_FL_SOCK_WR_SH) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100720 /* if the output is closed, we can't do anything */
721 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200722 chk_report_conn_err(check, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100723 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200724 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200725
Willy Tarreau06559ac2013-12-05 01:53:08 +0100726 /* here, we know that the connection is established. That's enough for
727 * a pure TCP check.
728 */
729 if (!check->type)
730 goto out_wakeup;
731
Willy Tarreauc09572f2017-10-04 11:58:22 +0200732 /* wake() will take care of calling tcpcheck_main() */
733 if (check->type == PR_O2_TCPCHK_CHK)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200734 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200735
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100736 if (check->bo->o) {
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100737 conn->xprt->snd_buf(conn, check->bo, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100738 if (conn->flags & CO_FL_ERROR) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200739 chk_report_conn_err(check, errno, 0);
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200740 __conn_xprt_stop_both(conn);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100741 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200742 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100743 if (check->bo->o)
744 return;
745 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200746
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100747 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
748 if (s->proxy->timeout.check) {
749 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
750 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200751 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100752 goto out_nowake;
753
Willy Tarreau83749182007-04-15 20:56:27 +0200754 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200755 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200756 out_nowake:
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200757 __conn_xprt_stop_send(conn); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200758}
759
Willy Tarreaubaaee002006-06-26 02:48:02 +0200760/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200761 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200762 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900763 * set_server_check_status() to update check->status, check->duration
764 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200765
766 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
767 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
768 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
769 * response to an SSL HELLO (the principle is that this is enough to
770 * distinguish between an SSL server and a pure TCP relay). All other cases will
771 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
772 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200773 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200774static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200775{
Simon Horman4a741432013-02-23 15:35:38 +0900776 struct check *check = conn->owner;
777 struct server *s = check->server;
778 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200779 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100780 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200781 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200782
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100783 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +0200784 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200785
Willy Tarreau310987a2014-01-22 19:46:33 +0100786 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200787 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200788
Willy Tarreauc09572f2017-10-04 11:58:22 +0200789 /* wake() will take care of calling tcpcheck_main() */
790 if (check->type == PR_O2_TCPCHK_CHK)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200791 return;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200792
Willy Tarreau83749182007-04-15 20:56:27 +0200793 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
794 * but the connection was closed on the remote end. Fortunately, recv still
795 * works correctly and we don't need to do the getsockopt() on linux.
796 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000797
798 /* Set buffer to point to the end of the data already read, and check
799 * that there is free space remaining. If the buffer is full, proceed
800 * with running the checks without attempting another socket read.
801 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000802
Willy Tarreau03938182010-03-17 21:52:07 +0100803 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +0000804
Simon Horman4a741432013-02-23 15:35:38 +0900805 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreau54e917c2017-08-30 07:35:35 +0200806 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +0100807 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +0900808 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +0200809 /* Report network errors only if we got no other data. Otherwise
810 * we'll let the upper layers decide whether the response is OK
811 * or not. It is very common that an RST sent by the server is
812 * reported as an error just after the last data chunk.
813 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200814 chk_report_conn_err(check, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +0100815 goto out_wakeup;
816 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200817 }
818
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100819
Willy Tarreau03938182010-03-17 21:52:07 +0100820 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +0900821 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +0100822 */
Simon Horman4a741432013-02-23 15:35:38 +0900823 if (check->bi->i < check->bi->size)
824 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100825 else {
Simon Horman4a741432013-02-23 15:35:38 +0900826 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100827 done = 1; /* buffer full, don't wait for more data */
828 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200829
Nick Chalk57b1bf72010-03-16 15:50:46 +0000830 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +0900831 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +0200832 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900833 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100834 goto wait_more_data;
835
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100836 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +0900837 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
838 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
839 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
840 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
841 !isdigit((unsigned char) *(check->bi->data + 11))) {
842 cut_crlf(check->bi->data);
843 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200844
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100845 goto out_wakeup;
846 }
847
Simon Horman4a741432013-02-23 15:35:38 +0900848 check->code = str2uic(check->bi->data + 9);
849 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +0000850
Willy Tarreaubd741542010-03-16 18:46:54 +0100851 if ((s->proxy->options & PR_O_DISABLE404) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200852 (s->next_state != SRV_ST_STOPPED) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000853 /* 404 may be accepted as "stopping" only if the server was up */
854 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900855 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000856 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100857 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
858 /* Run content verification check... We know we have at least 13 chars */
859 if (!httpchk_expect(s, done))
860 goto wait_more_data;
861 }
862 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +0900863 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +0100864 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900865 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +0100866 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000867 else {
868 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900869 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000870 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200871 break;
872
873 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900874 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +0100875 goto wait_more_data;
876
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100877 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +0900878 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
879 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200880 else
Simon Horman4a741432013-02-23 15:35:38 +0900881 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200882 break;
883
884 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900885 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100886 goto wait_more_data;
887
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200888 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +0900889 if ((check->bi->i < strlen("000\r")) ||
890 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
891 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
892 !isdigit((unsigned char) *(check->bi->data + 2))) {
893 cut_crlf(check->bi->data);
894 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200895
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200896 goto out_wakeup;
897 }
898
Simon Horman4a741432013-02-23 15:35:38 +0900899 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200900
Simon Horman4a741432013-02-23 15:35:38 +0900901 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200902 cut_crlf(desc);
903
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100904 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +0900905 if (*check->bi->data == '2')
906 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
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_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200909 break;
910
Simon Hormana2b9dad2013-02-12 10:45:54 +0900911 case PR_O2_LB_AGENT_CHK: {
Willy Tarreau81f5d942013-12-09 20:51:51 +0100912 int status = HCHK_STATUS_CHECKED;
913 const char *hs = NULL; /* health status */
914 const char *as = NULL; /* admin status */
915 const char *ps = NULL; /* performance status */
Nenad Merdanovic174dd372016-04-24 23:10:06 +0200916 const char *cs = NULL; /* maxconn */
Willy Tarreau81f5d942013-12-09 20:51:51 +0100917 const char *err = NULL; /* first error to report */
918 const char *wrn = NULL; /* first warning to report */
919 char *cmd, *p;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900920
Willy Tarreau81f5d942013-12-09 20:51:51 +0100921 /* We're getting an agent check response. The agent could
922 * have been disabled in the mean time with a long check
923 * still pending. It is important that we ignore the whole
924 * response.
925 */
926 if (!(check->server->agent.state & CHK_ST_ENABLED))
927 break;
928
929 /* The agent supports strings made of a single line ended by the
930 * first CR ('\r') or LF ('\n'). This line is composed of words
931 * delimited by spaces (' '), tabs ('\t'), or commas (','). The
932 * line may optionally contained a description of a state change
933 * after a sharp ('#'), which is only considered if a health state
934 * is announced.
935 *
936 * Words may be composed of :
937 * - a numeric weight suffixed by the percent character ('%').
938 * - a health status among "up", "down", "stopped", and "fail".
939 * - an admin status among "ready", "drain", "maint".
940 *
941 * These words may appear in any order. If multiple words of the
942 * same category appear, the last one wins.
943 */
944
Willy Tarreau9809b782013-12-11 21:40:11 +0100945 p = check->bi->data;
946 while (*p && *p != '\n' && *p != '\r')
947 p++;
948
949 if (!*p) {
950 if (!done)
951 goto wait_more_data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900952
Willy Tarreau9809b782013-12-11 21:40:11 +0100953 /* at least inform the admin that the agent is mis-behaving */
954 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
955 break;
956 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100957
Willy Tarreau9809b782013-12-11 21:40:11 +0100958 *p = 0;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100959 cmd = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900960
Willy Tarreau81f5d942013-12-09 20:51:51 +0100961 while (*cmd) {
962 /* look for next word */
963 if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') {
964 cmd++;
965 continue;
966 }
Simon Horman671b6f02013-11-25 10:46:39 +0900967
Willy Tarreau81f5d942013-12-09 20:51:51 +0100968 if (*cmd == '#') {
969 /* this is the beginning of a health status description,
970 * skip the sharp and blanks.
971 */
972 cmd++;
973 while (*cmd == '\t' || *cmd == ' ')
974 cmd++;
Simon Horman671b6f02013-11-25 10:46:39 +0900975 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900976 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100977
978 /* find the end of the word so that we have a null-terminated
979 * word between <cmd> and <p>.
980 */
981 p = cmd + 1;
982 while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',')
983 p++;
984 if (*p)
985 *p++ = 0;
986
987 /* first, health statuses */
988 if (strcasecmp(cmd, "up") == 0) {
989 check->health = check->rise + check->fall - 1;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900990 status = HCHK_STATUS_L7OKD;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100991 hs = cmd;
992 }
993 else if (strcasecmp(cmd, "down") == 0) {
994 check->health = 0;
995 status = HCHK_STATUS_L7STS;
996 hs = cmd;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900997 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100998 else if (strcasecmp(cmd, "stopped") == 0) {
999 check->health = 0;
1000 status = HCHK_STATUS_L7STS;
1001 hs = cmd;
1002 }
1003 else if (strcasecmp(cmd, "fail") == 0) {
1004 check->health = 0;
1005 status = HCHK_STATUS_L7STS;
1006 hs = cmd;
1007 }
1008 /* admin statuses */
1009 else if (strcasecmp(cmd, "ready") == 0) {
1010 as = cmd;
1011 }
1012 else if (strcasecmp(cmd, "drain") == 0) {
1013 as = cmd;
1014 }
1015 else if (strcasecmp(cmd, "maint") == 0) {
1016 as = cmd;
1017 }
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001018 /* try to parse a weight here and keep the last one */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001019 else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) {
1020 ps = cmd;
1021 }
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001022 /* try to parse a maxconn here */
1023 else if (strncasecmp(cmd, "maxconn:", strlen("maxconn:")) == 0) {
1024 cs = cmd;
1025 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001026 else {
1027 /* keep a copy of the first error */
1028 if (!err)
1029 err = cmd;
1030 }
1031 /* skip to next word */
1032 cmd = p;
1033 }
1034 /* here, cmd points either to \0 or to the beginning of a
1035 * description. Skip possible leading spaces.
1036 */
1037 while (*cmd == ' ' || *cmd == '\n')
1038 cmd++;
1039
1040 /* First, update the admin status so that we avoid sending other
1041 * possibly useless warnings and can also update the health if
1042 * present after going back up.
1043 */
1044 if (as) {
1045 if (strcasecmp(as, "drain") == 0)
1046 srv_adm_set_drain(check->server);
1047 else if (strcasecmp(as, "maint") == 0)
1048 srv_adm_set_maint(check->server);
1049 else
1050 srv_adm_set_ready(check->server);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001051 }
1052
Willy Tarreau81f5d942013-12-09 20:51:51 +01001053 /* now change weights */
1054 if (ps) {
1055 const char *msg;
1056
1057 msg = server_parse_weight_change_request(s, ps);
1058 if (!wrn || !*wrn)
1059 wrn = msg;
1060 }
1061
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001062 if (cs) {
1063 const char *msg;
1064
1065 cs += strlen("maxconn:");
1066
1067 msg = server_parse_maxconn_change_request(s, cs);
1068 if (!wrn || !*wrn)
1069 wrn = msg;
1070 }
1071
Willy Tarreau81f5d942013-12-09 20:51:51 +01001072 /* and finally health status */
1073 if (hs) {
1074 /* We'll report some of the warnings and errors we have
1075 * here. Down reports are critical, we leave them untouched.
1076 * Lack of report, or report of 'UP' leaves the room for
1077 * ERR first, then WARN.
Simon Hormana2b9dad2013-02-12 10:45:54 +09001078 */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001079 const char *msg = cmd;
1080 struct chunk *t;
1081
1082 if (!*msg || status == HCHK_STATUS_L7OKD) {
1083 if (err && *err)
1084 msg = err;
1085 else if (wrn && *wrn)
1086 msg = wrn;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001087 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001088
1089 t = get_trash_chunk();
1090 chunk_printf(t, "via agent : %s%s%s%s",
1091 hs, *msg ? " (" : "",
1092 msg, *msg ? ")" : "");
1093
1094 set_server_check_status(check, status, t->str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001095 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001096 else if (err && *err) {
1097 /* No status change but we'd like to report something odd.
1098 * Just report the current state and copy the message.
1099 */
1100 chunk_printf(&trash, "agent reports an error : %s", err);
1101 set_server_check_status(check, status/*check->status*/, trash.str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001102
Willy Tarreau81f5d942013-12-09 20:51:51 +01001103 }
1104 else if (wrn && *wrn) {
1105 /* No status change but we'd like to report something odd.
1106 * Just report the current state and copy the message.
1107 */
1108 chunk_printf(&trash, "agent warns : %s", wrn);
1109 set_server_check_status(check, status/*check->status*/, trash.str);
1110 }
1111 else
1112 set_server_check_status(check, status, NULL);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001113 break;
1114 }
1115
Willy Tarreau1620ec32011-08-06 17:05:02 +02001116 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001117 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001118 goto wait_more_data;
1119
Simon Horman4a741432013-02-23 15:35:38 +09001120 if (check->bi->data[0] == 'R') {
1121 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001122 }
1123 else {
Simon Horman4a741432013-02-23 15:35:38 +09001124 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1125 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001126 else
1127 desc = "PostgreSQL unknown error";
1128
Simon Horman4a741432013-02-23 15:35:38 +09001129 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001130 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001131 break;
1132
1133 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001134 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001135 goto wait_more_data;
1136
Simon Horman4a741432013-02-23 15:35:38 +09001137 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1138 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001139 }
1140 else {
Simon Horman4a741432013-02-23 15:35:38 +09001141 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001142 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001143 break;
1144
1145 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001146 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001147 goto wait_more_data;
1148
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001149 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001150 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001151 /* We set the MySQL Version in description for information purpose
1152 * FIXME : it can be cool to use MySQL Version for other purpose,
1153 * like mark as down old MySQL server.
1154 */
Simon Horman4a741432013-02-23 15:35:38 +09001155 if (check->bi->i > 51) {
1156 desc = ltrim(check->bi->data + 5, ' ');
1157 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001158 }
1159 else {
1160 if (!done)
1161 goto wait_more_data;
1162 /* it seems we have a OK packet but without a valid length,
1163 * it must be a protocol error
1164 */
Simon Horman4a741432013-02-23 15:35:38 +09001165 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001166 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001167 }
1168 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001169 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001170 desc = ltrim(check->bi->data + 7, ' ');
1171 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001172 }
1173 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001174 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1175 (((unsigned int) *(check->bi->data + 1)) << 8) +
1176 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001177
Simon Horman4a741432013-02-23 15:35:38 +09001178 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001179 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001180 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001181 /* We have only one MySQL packet and it is a Handshake Initialization packet
1182 * but we need to have a second packet to know if it is alright
1183 */
Simon Horman4a741432013-02-23 15:35:38 +09001184 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001185 goto wait_more_data;
1186 }
1187 else {
1188 /* We have only one packet and it is an Error packet,
1189 * an error message is attached, so we can display it
1190 */
Simon Horman4a741432013-02-23 15:35:38 +09001191 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001192 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001193 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001194 }
Simon Horman4a741432013-02-23 15:35:38 +09001195 } else if (check->bi->i > first_packet_len + 4) {
1196 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1197 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1198 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001199
Simon Horman4a741432013-02-23 15:35:38 +09001200 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001201 /* We have 2 packets and that's good */
1202 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001203 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001204 /* No error packet */
1205 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001206 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001207 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001208 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001209 }
1210 else {
1211 /* An error message is attached in the Error packet
1212 * so we can display it ! :)
1213 */
Simon Horman4a741432013-02-23 15:35:38 +09001214 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001215 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001216 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001217 }
1218 }
1219 }
1220 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001221 if (!done)
1222 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001223 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001224 * it must be a protocol error
1225 */
Simon Horman4a741432013-02-23 15:35:38 +09001226 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001227 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001228 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001229 }
1230 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001231 break;
1232
1233 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001234 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001235 goto wait_more_data;
1236
1237 /* Check if the server speaks LDAP (ASN.1/BER)
1238 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1239 * http://tools.ietf.org/html/rfc4511
1240 */
1241
1242 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1243 * LDAPMessage: 0x30: SEQUENCE
1244 */
Simon Horman4a741432013-02-23 15:35:38 +09001245 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1246 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001247 }
1248 else {
1249 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001250 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001251
1252 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1253 * messageID: 0x02 0x01 0x01: INTEGER 1
1254 * protocolOp: 0x61: bindResponse
1255 */
1256 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001257 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1258 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001259
1260 goto out_wakeup;
1261 }
1262
1263 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001264 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001265
1266 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1267 * ldapResult: 0x0a 0x01: ENUMERATION
1268 */
1269 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001270 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1271 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001272
1273 goto out_wakeup;
1274 }
1275
1276 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1277 * resultCode
1278 */
Simon Horman4a741432013-02-23 15:35:38 +09001279 check->code = *(check->bi->data + msglen + 9);
1280 if (check->code) {
1281 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 +02001282 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001283 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001284 }
1285 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001286 break;
1287
Christopher Fauletba7bc162016-11-07 21:07:38 +01001288 case PR_O2_SPOP_CHK: {
1289 unsigned int framesz;
1290 char err[HCHK_DESC_LEN];
1291
1292 if (!done && check->bi->i < 4)
1293 goto wait_more_data;
1294
1295 memcpy(&framesz, check->bi->data, 4);
1296 framesz = ntohl(framesz);
1297
1298 if (!done && check->bi->i < (4+framesz))
1299 goto wait_more_data;
1300
Christopher Faulet8ef75252017-02-20 22:56:03 +01001301 if (!spoe_handle_healthcheck_response(check->bi->data+4, framesz, err, HCHK_DESC_LEN-1))
Christopher Fauletba7bc162016-11-07 21:07:38 +01001302 set_server_check_status(check, HCHK_STATUS_L7OKD, "SPOA server is ok");
1303 else
1304 set_server_check_status(check, HCHK_STATUS_L7STS, err);
1305 break;
1306 }
1307
Willy Tarreau1620ec32011-08-06 17:05:02 +02001308 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001309 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001310 break;
1311 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001312
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001313 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001314 /* collect possible new errors */
1315 if (conn->flags & CO_FL_ERROR)
Willy Tarreaub5259bf2017-10-04 14:47:29 +02001316 chk_report_conn_err(check, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001317
Nick Chalk57b1bf72010-03-16 15:50:46 +00001318 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001319 *check->bi->data = '\0';
1320 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001321
Steven Davidovitz544d4812017-03-08 11:06:20 -08001322 /* Close the connection... We still attempt to nicely close if,
1323 * for instance, SSL needs to send a "close notify." Later, we perform
1324 * a hard close and reset the connection if some data are pending,
1325 * otherwise we end up with many TIME_WAITs and eat all the source port
1326 * range quickly. To avoid sending RSTs all the time, we first try to
1327 * drain pending data.
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001328 */
Olivier Houchard1a0545f2017-09-13 18:30:23 +02001329 __conn_xprt_stop_both(conn);
1330 conn_xprt_shutw(conn);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001331
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001332 /* OK, let's not stay here forever */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001333 if (check->result == CHK_RES_FAILED)
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001334 conn->flags |= CO_FL_ERROR;
1335
Willy Tarreaufdccded2008-08-29 18:19:04 +02001336 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001337 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001338
1339 wait_more_data:
Olivier Houchard1a0545f2017-09-13 18:30:23 +02001340 __conn_xprt_want_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001341}
1342
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001343/*
1344 * This function is used only for server health-checks. It handles connection
1345 * status updates including errors. If necessary, it wakes the check task up.
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001346 * It returns 0 on normal cases, <0 if at least one close() has happened on the
1347 * connection (eg: reconnect).
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001348 */
1349static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001350{
Simon Horman4a741432013-02-23 15:35:38 +09001351 struct check *check = conn->owner;
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001352 int ret = 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001353
Willy Tarreauc09572f2017-10-04 11:58:22 +02001354 /* we may have to make progress on the TCP checks */
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001355 if (check->type == PR_O2_TCPCHK_CHK) {
1356 ret = tcpcheck_main(check);
Willy Tarreau00149122017-10-04 18:05:01 +02001357 conn = check->conn;
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001358 }
Willy Tarreauc09572f2017-10-04 11:58:22 +02001359
Willy Tarreau6c560da2012-11-24 11:14:45 +01001360 if (unlikely(conn->flags & CO_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001361 /* We may get error reports bypassing the I/O handlers, typically
1362 * the case when sending a pure TCP check which fails, then the I/O
1363 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001364 * main processing task so let's simply wake it up. If we get here,
1365 * we expect errno to still be valid.
1366 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +02001367 chk_report_conn_err(check, errno, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001368
Olivier Houchard1a0545f2017-09-13 18:30:23 +02001369 __conn_xprt_stop_both(conn);
Willy Tarreau2d351b62013-12-05 02:36:25 +01001370 task_wakeup(check->task, TASK_WOKEN_IO);
1371 }
Olivier Houchard1a0545f2017-09-13 18:30:23 +02001372 else if (!(conn->flags & (CO_FL_XPRT_RD_ENA|CO_FL_XPRT_WR_ENA|CO_FL_HANDSHAKE))) {
Willy Tarreau3be293f2014-02-05 18:31:24 +01001373 /* we may get here if only a connection probe was required : we
1374 * don't have any data to send nor anything expected in response,
1375 * so the completion of the connection establishment is enough.
1376 */
1377 task_wakeup(check->task, TASK_WOKEN_IO);
1378 }
Willy Tarreau2d351b62013-12-05 02:36:25 +01001379
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001380 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001381 /* We're here because nobody wants to handle the error, so we
1382 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001383 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01001384 conn_sock_drain(conn);
Willy Tarreau402dbc12017-10-05 17:53:13 +02001385 conn_full_close(conn);
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001386 ret = -1;
Willy Tarreau2d351b62013-12-05 02:36:25 +01001387 }
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001388
1389 /* if a connection got replaced, we must absolutely prevent the connection
1390 * handler from touching its fd, and perform the FD polling updates ourselves
1391 */
1392 if (ret < 0)
1393 conn_cond_update_polling(conn);
1394
1395 return ret;
Willy Tarreau20bea422012-07-06 12:00:49 +02001396}
1397
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001398struct data_cb check_conn_cb = {
1399 .recv = event_srv_chk_r,
1400 .send = event_srv_chk_w,
1401 .wake = wake_srv_chk,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +01001402 .name = "CHCK",
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001403};
1404
Willy Tarreaubaaee002006-06-26 02:48:02 +02001405/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001406 * updates the server's weight during a warmup stage. Once the final weight is
1407 * reached, the task automatically stops. Note that any server status change
1408 * must have updated s->last_change accordingly.
1409 */
1410static struct task *server_warmup(struct task *t)
1411{
1412 struct server *s = t->context;
1413
1414 /* by default, plan on stopping the task */
1415 t->expire = TICK_ETERNITY;
Emeric Brun52a91d32017-08-31 14:41:55 +02001416 if ((s->next_admin & SRV_ADMF_MAINT) ||
1417 (s->next_state != SRV_ST_STARTING))
Willy Tarreau2e993902011-10-31 11:53:20 +01001418 return t;
1419
Willy Tarreau892337c2014-05-13 23:41:20 +02001420 /* recalculate the weights and update the state */
Willy Tarreau004e0452013-11-21 11:22:01 +01001421 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001422
1423 /* probably that we can refill this server with a bit more connections */
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001424 pendconn_grab_from_px(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001425
1426 /* get back there in 1 second or 1/20th of the slowstart interval,
1427 * whichever is greater, resulting in small 5% steps.
1428 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001429 if (s->next_state == SRV_ST_STARTING)
Willy Tarreau2e993902011-10-31 11:53:20 +01001430 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1431 return t;
1432}
1433
Willy Tarreau894c6422017-10-04 15:58:52 +02001434/* returns the first NON-COMMENT tcp-check rule from list <list> or NULL if
1435 * none was found.
1436 */
1437static struct tcpcheck_rule *get_first_tcpcheck_rule(struct list *list)
1438{
1439 struct tcpcheck_rule *r;
1440
1441 list_for_each_entry(r, list, list) {
1442 if (r->action != TCPCHK_ACT_COMMENT)
1443 return r;
1444 }
1445 return NULL;
1446}
1447
Willy Tarreau2e993902011-10-31 11:53:20 +01001448/*
Simon Horman98637e52014-06-20 12:30:16 +09001449 * establish a server health-check that makes use of a connection.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001450 *
1451 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001452 * - SF_ERR_NONE if everything's OK and tcpcheck_main() was not called
1453 * - SF_ERR_UP if if everything's OK and tcpcheck_main() was called
1454 * - SF_ERR_SRVTO if there are no more servers
1455 * - SF_ERR_SRVCL if the connection was refused by the server
1456 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1457 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1458 * - SF_ERR_INTERNAL for any other purely internal errors
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001459 * - 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 +01001460 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001461 * Note that we try to prevent the network stack from sending the ACK during the
1462 * connect() when a pure TCP check is used (without PROXY protocol).
1463 */
Simon Horman98637e52014-06-20 12:30:16 +09001464static int connect_conn_chk(struct task *t)
Simon Hormanb00d17a2014-06-13 16:18:16 +09001465{
1466 struct check *check = t->context;
1467 struct server *s = check->server;
1468 struct connection *conn = check->conn;
1469 struct protocol *proto;
Willy Tarreauf411cce2017-10-04 16:21:19 +02001470 struct tcpcheck_rule *tcp_rule = NULL;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001471 int ret;
Willy Tarreauf3d34822014-12-08 12:11:28 +01001472 int quickack;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001473
Willy Tarreau00149122017-10-04 18:05:01 +02001474 /* we cannot have a connection here */
1475 if (conn)
1476 return SF_ERR_INTERNAL;
1477
Simon Hormanb00d17a2014-06-13 16:18:16 +09001478 /* tcpcheck send/expect initialisation */
Willy Tarreauf411cce2017-10-04 16:21:19 +02001479 if (check->type == PR_O2_TCPCHK_CHK) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001480 check->current_step = NULL;
Willy Tarreauf411cce2017-10-04 16:21:19 +02001481 tcp_rule = get_first_tcpcheck_rule(check->tcpcheck_rules);
1482 }
Simon Hormanb00d17a2014-06-13 16:18:16 +09001483
1484 /* prepare the check buffer.
1485 * This should not be used if check is the secondary agent check
1486 * of a server as s->proxy->check_req will relate to the
1487 * configuration of the primary check. Similarly, tcp-check uses
1488 * its own strings.
1489 */
1490 if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) {
1491 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
1492
1493 /* we want to check if this host replies to HTTP or SSLv3 requests
1494 * so we'll send the request, and won't wake the checker up now.
1495 */
1496 if ((check->type) == PR_O2_SSL3_CHK) {
1497 /* SSL requires that we put Unix time in the request */
1498 int gmt_time = htonl(date.tv_sec);
1499 memcpy(check->bo->data + 11, &gmt_time, 4);
1500 }
1501 else if ((check->type) == PR_O2_HTTP_CHK) {
1502 if (s->proxy->options2 & PR_O2_CHK_SNDST)
1503 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size));
Cyril Bonté32602d22015-01-30 00:07:07 +01001504 /* prevent HTTP keep-alive when "http-check expect" is used */
1505 if (s->proxy->options2 & PR_O2_EXP_TYPE)
1506 bo_putstr(check->bo, "Connection: close\r\n");
Simon Hormanb00d17a2014-06-13 16:18:16 +09001507 bo_putstr(check->bo, "\r\n");
1508 *check->bo->p = '\0'; /* to make gdb output easier to read */
1509 }
1510 }
1511
James Brown55f9ff12015-10-21 18:19:05 -07001512 if ((check->type & PR_O2_LB_AGENT_CHK) && check->send_string_len) {
1513 bo_putblk(check->bo, check->send_string, check->send_string_len);
1514 }
1515
Willy Tarreauf411cce2017-10-04 16:21:19 +02001516 /* for tcp-checks, the initial connection setup is handled separately as
1517 * it may be sent to a specific port and not to the server's.
1518 */
1519 if (tcp_rule && tcp_rule->action == TCPCHK_ACT_CONNECT) {
1520 tcpcheck_main(check);
1521 return SF_ERR_UP;
1522 }
1523
Simon Hormanb00d17a2014-06-13 16:18:16 +09001524 /* prepare a new connection */
Willy Tarreau00149122017-10-04 18:05:01 +02001525 conn = check->conn = conn_new();
1526 if (!check->conn)
1527 return SF_ERR_RESOURCE;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001528
Simon Horman41f58762015-01-30 11:22:56 +09001529 if (is_addr(&check->addr)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001530 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09001531 conn->addr.to = check->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001532 }
1533 else {
1534 /* we'll connect to the addr on the server */
1535 conn->addr.to = s->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001536 }
1537
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001538 if ((conn->addr.to.ss_family == AF_INET) || (conn->addr.to.ss_family == AF_INET6)) {
1539 int i = 0;
1540
1541 i = srv_check_healthcheck_port(check);
1542 if (i == 0) {
1543 conn->owner = check;
1544 return SF_ERR_CHK_PORT;
1545 }
1546
1547 set_host_port(&conn->addr.to, i);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001548 }
1549
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01001550 proto = protocol_by_family(conn->addr.to.ss_family);
1551
1552 conn_prepare(conn, proto, check->xprt);
1553 conn_attach(conn, check, &check_conn_cb);
1554 conn->target = &s->obj_type;
1555
1556 /* no client address */
1557 clear_addr(&conn->addr.from);
1558
Willy Tarreauf3d34822014-12-08 12:11:28 +01001559 /* only plain tcp-check supports quick ACK */
1560 quickack = check->type == 0 || check->type == PR_O2_TCPCHK_CHK;
1561
Willy Tarreauf411cce2017-10-04 16:21:19 +02001562 if (tcp_rule && tcp_rule->action == TCPCHK_ACT_EXPECT)
1563 quickack = 0;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001564
Willy Tarreaue7dff022015-04-03 01:14:29 +02001565 ret = SF_ERR_INTERNAL;
Olivier Houchardb68fda42017-08-04 18:39:01 +02001566 if (proto && proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01001567 ret = proto->connect(conn, check->type, quickack ? 2 : 0);
Olivier Houchard9130a962017-10-17 17:33:43 +02001568#ifdef USE_OPENSSL
1569 if (s->check.sni)
1570 ssl_sock_set_servername(conn, s->check.sni);
1571#endif
Willy Tarreauf4949772017-05-06 08:45:28 +02001572 if (s->check.send_proxy && !(check->state & CHK_ST_AGENT)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001573 conn->send_proxy_ofs = 1;
1574 conn->flags |= CO_FL_SEND_PROXY;
1575 }
1576
1577 return ret;
1578}
1579
Simon Horman98637e52014-06-20 12:30:16 +09001580static struct list pid_list = LIST_HEAD_INIT(pid_list);
1581static struct pool_head *pool2_pid_list;
1582
1583void block_sigchld(void)
1584{
1585 sigset_t set;
1586 sigemptyset(&set);
1587 sigaddset(&set, SIGCHLD);
Willy Tarreauebc92442016-06-21 17:29:46 +02001588 assert(sigprocmask(SIG_BLOCK, &set, NULL) == 0);
Simon Horman98637e52014-06-20 12:30:16 +09001589}
1590
1591void unblock_sigchld(void)
1592{
1593 sigset_t set;
1594 sigemptyset(&set);
Willy Tarreauebc92442016-06-21 17:29:46 +02001595 sigaddset(&set, SIGCHLD);
1596 assert(sigprocmask(SIG_UNBLOCK, &set, NULL) == 0);
Simon Horman98637e52014-06-20 12:30:16 +09001597}
1598
Simon Horman98637e52014-06-20 12:30:16 +09001599static struct pid_list *pid_list_add(pid_t pid, struct task *t)
1600{
1601 struct pid_list *elem;
1602 struct check *check = t->context;
1603
1604 elem = pool_alloc2(pool2_pid_list);
1605 if (!elem)
1606 return NULL;
1607 elem->pid = pid;
1608 elem->t = t;
1609 elem->exited = 0;
1610 check->curpid = elem;
1611 LIST_INIT(&elem->list);
1612 LIST_ADD(&pid_list, &elem->list);
1613 return elem;
1614}
1615
Simon Horman98637e52014-06-20 12:30:16 +09001616static void pid_list_del(struct pid_list *elem)
1617{
1618 struct check *check;
1619
1620 if (!elem)
1621 return;
1622
Simon Horman98637e52014-06-20 12:30:16 +09001623 LIST_DEL(&elem->list);
Simon Horman98637e52014-06-20 12:30:16 +09001624 if (!elem->exited)
1625 kill(elem->pid, SIGTERM);
1626
1627 check = elem->t->context;
1628 check->curpid = NULL;
1629 pool_free2(pool2_pid_list, elem);
1630}
1631
1632/* Called from inside SIGCHLD handler, SIGCHLD is blocked */
1633static void pid_list_expire(pid_t pid, int status)
1634{
1635 struct pid_list *elem;
1636
1637 list_for_each_entry(elem, &pid_list, list) {
1638 if (elem->pid == pid) {
1639 elem->t->expire = now_ms;
1640 elem->status = status;
1641 elem->exited = 1;
Cyril Bonté9dbcfab2014-08-07 01:55:39 +02001642 task_wakeup(elem->t, TASK_WOKEN_IO);
Simon Horman98637e52014-06-20 12:30:16 +09001643 return;
1644 }
1645 }
1646}
1647
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001648static void sigchld_handler(struct sig_handler *sh)
Simon Horman98637e52014-06-20 12:30:16 +09001649{
1650 pid_t pid;
1651 int status;
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001652
Simon Horman98637e52014-06-20 12:30:16 +09001653 while ((pid = waitpid(0, &status, WNOHANG)) > 0)
1654 pid_list_expire(pid, status);
1655}
1656
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001657static int init_pid_list(void)
1658{
Simon Horman98637e52014-06-20 12:30:16 +09001659 if (pool2_pid_list != NULL)
1660 /* Nothing to do */
1661 return 0;
1662
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001663 if (!signal_register_fct(SIGCHLD, sigchld_handler, SIGCHLD)) {
Simon Horman98637e52014-06-20 12:30:16 +09001664 Alert("Failed to set signal handler for external health checks: %s. Aborting.\n",
1665 strerror(errno));
1666 return 1;
1667 }
1668
1669 pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED);
1670 if (pool2_pid_list == NULL) {
1671 Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n",
1672 strerror(errno));
1673 return 1;
1674 }
1675
1676 return 0;
1677}
1678
Cyril Bontéac92a062014-12-27 22:28:38 +01001679/* helper macro to set an environment variable and jump to a specific label on failure. */
1680#define EXTCHK_SETENV(check, envidx, value, fail) { if (extchk_setenv(check, envidx, value)) goto fail; }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001681
1682/*
Cyril Bontéac92a062014-12-27 22:28:38 +01001683 * helper function to allocate enough memory to store an environment variable.
1684 * It will also check that the environment variable is updatable, and silently
1685 * fail if not.
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001686 */
Cyril Bontéac92a062014-12-27 22:28:38 +01001687static int extchk_setenv(struct check *check, int idx, const char *value)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001688{
1689 int len, ret;
Cyril Bontéac92a062014-12-27 22:28:38 +01001690 char *envname;
1691 int vmaxlen;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001692
Cyril Bontéac92a062014-12-27 22:28:38 +01001693 if (idx < 0 || idx >= EXTCHK_SIZE) {
1694 Alert("Illegal environment variable index %d. Aborting.\n", idx);
1695 return 1;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001696 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001697
1698 envname = extcheck_envs[idx].name;
1699 vmaxlen = extcheck_envs[idx].vmaxlen;
1700
1701 /* Check if the environment variable is already set, and silently reject
1702 * the update if this one is not updatable. */
1703 if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx]))
1704 return 0;
1705
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001706 /* Instead of sending NOT_USED, sending an empty value is preferable */
1707 if (strcmp(value, "NOT_USED") == 0) {
1708 value = "";
1709 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001710
1711 len = strlen(envname) + 1;
1712 if (vmaxlen == EXTCHK_SIZE_EVAL_INIT)
1713 len += strlen(value);
1714 else
1715 len += vmaxlen;
1716
1717 if (!check->envp[idx])
1718 check->envp[idx] = malloc(len + 1);
1719
1720 if (!check->envp[idx]) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001721 Alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname);
1722 return 1;
1723 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001724 ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001725 if (ret < 0) {
1726 Alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno));
1727 return 1;
1728 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001729 else if (ret > len) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001730 Alert("Environment variable '%s' was truncated. Aborting.\n", envname);
1731 return 1;
1732 }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001733 return 0;
1734}
Simon Horman98637e52014-06-20 12:30:16 +09001735
1736static int prepare_external_check(struct check *check)
1737{
1738 struct server *s = check->server;
1739 struct proxy *px = s->proxy;
1740 struct listener *listener = NULL, *l;
1741 int i;
Simon Horman98637e52014-06-20 12:30:16 +09001742 const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001743 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001744
1745 list_for_each_entry(l, &px->conf.listeners, by_fe)
1746 /* Use the first INET, INET6 or UNIX listener */
1747 if (l->addr.ss_family == AF_INET ||
1748 l->addr.ss_family == AF_INET6 ||
1749 l->addr.ss_family == AF_UNIX) {
1750 listener = l;
1751 break;
1752 }
1753
Simon Horman98637e52014-06-20 12:30:16 +09001754 check->curpid = NULL;
Cyril Bontéac92a062014-12-27 22:28:38 +01001755 check->envp = calloc((EXTCHK_SIZE + 1), sizeof(char *));
1756 if (!check->envp) {
1757 Alert("Failed to allocate memory for environment variables. Aborting\n");
1758 goto err;
1759 }
Simon Horman98637e52014-06-20 12:30:16 +09001760
Cyril Bontéac92a062014-12-27 22:28:38 +01001761 check->argv = calloc(6, sizeof(char *));
1762 if (!check->argv) {
1763 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001764 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001765 }
Simon Horman98637e52014-06-20 12:30:16 +09001766
1767 check->argv[0] = px->check_command;
1768
Cyril Bonté777be862014-12-02 21:21:35 +01001769 if (!listener) {
1770 check->argv[1] = strdup("NOT_USED");
1771 check->argv[2] = strdup("NOT_USED");
1772 }
1773 else if (listener->addr.ss_family == AF_INET ||
Simon Horman98637e52014-06-20 12:30:16 +09001774 listener->addr.ss_family == AF_INET6) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001775 addr_to_str(&listener->addr, buf, sizeof(buf));
1776 check->argv[1] = strdup(buf);
1777 port_to_str(&listener->addr, buf, sizeof(buf));
1778 check->argv[2] = strdup(buf);
Cyril Bonté777be862014-12-02 21:21:35 +01001779 }
1780 else if (listener->addr.ss_family == AF_UNIX) {
Simon Horman98637e52014-06-20 12:30:16 +09001781 const struct sockaddr_un *un;
1782
1783 un = (struct sockaddr_un *)&listener->addr;
1784 check->argv[1] = strdup(un->sun_path);
1785 check->argv[2] = strdup("NOT_USED");
Cyril Bonté777be862014-12-02 21:21:35 +01001786 }
1787 else {
Cyril Bontéac92a062014-12-27 22:28:38 +01001788 Alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001789 goto err;
1790 }
1791
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001792 addr_to_str(&s->addr, buf, sizeof(buf));
1793 check->argv[3] = strdup(buf);
Willy Tarreau04276f32017-01-06 17:41:29 +01001794
1795 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
1796 snprintf(buf, sizeof(buf), "%u", s->svc_port);
1797 else
1798 *buf = 0;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001799 check->argv[4] = strdup(buf);
Simon Horman98637e52014-06-20 12:30:16 +09001800
Cyril Bontéac92a062014-12-27 22:28:38 +01001801 for (i = 0; i < 5; i++) {
1802 if (!check->argv[i]) {
1803 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001804 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001805 }
1806 }
Simon Horman98637e52014-06-20 12:30:16 +09001807
Cyril Bontéac92a062014-12-27 22:28:38 +01001808 EXTCHK_SETENV(check, EXTCHK_PATH, path, err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001809 /* Add proxy environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001810 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err);
1811 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err);
1812 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err);
1813 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001814 /* Add server environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001815 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err);
1816 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err);
1817 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err);
1818 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err);
1819 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err);
1820 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), err);
1821
1822 /* Ensure that we don't leave any hole in check->envp */
1823 for (i = 0; i < EXTCHK_SIZE; i++)
1824 if (!check->envp[i])
1825 EXTCHK_SETENV(check, i, "", err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001826
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001827 return 1;
Simon Horman98637e52014-06-20 12:30:16 +09001828err:
1829 if (check->envp) {
Cyril Bontéac92a062014-12-27 22:28:38 +01001830 for (i = 0; i < EXTCHK_SIZE; i++)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001831 free(check->envp[i]);
Simon Horman98637e52014-06-20 12:30:16 +09001832 free(check->envp);
1833 check->envp = NULL;
1834 }
1835
1836 if (check->argv) {
1837 for (i = 1; i < 5; i++)
1838 free(check->argv[i]);
1839 free(check->argv);
1840 check->argv = NULL;
1841 }
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001842 return 0;
Simon Horman98637e52014-06-20 12:30:16 +09001843}
1844
Simon Hormanb00d17a2014-06-13 16:18:16 +09001845/*
Simon Horman98637e52014-06-20 12:30:16 +09001846 * establish a server health-check that makes use of a process.
1847 *
1848 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001849 * - SF_ERR_NONE if everything's OK
Willy Tarreaue7dff022015-04-03 01:14:29 +02001850 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
Tim Düsterhus4896c442016-11-29 02:15:19 +01001851 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Horman98637e52014-06-20 12:30:16 +09001852 *
1853 * Blocks and then unblocks SIGCHLD
1854 */
1855static int connect_proc_chk(struct task *t)
1856{
Cyril Bontéac92a062014-12-27 22:28:38 +01001857 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001858 struct check *check = t->context;
1859 struct server *s = check->server;
1860 struct proxy *px = s->proxy;
1861 int status;
1862 pid_t pid;
1863
Willy Tarreaue7dff022015-04-03 01:14:29 +02001864 status = SF_ERR_RESOURCE;
Simon Horman98637e52014-06-20 12:30:16 +09001865
1866 block_sigchld();
1867
1868 pid = fork();
1869 if (pid < 0) {
1870 Alert("Failed to fork process for external health check: %s. Aborting.\n",
1871 strerror(errno));
1872 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1873 goto out;
1874 }
1875 if (pid == 0) {
1876 /* Child */
1877 extern char **environ;
Willy Tarreaub7b24782016-06-21 15:32:29 +02001878 int fd;
1879
1880 /* close all FDs. Keep stdin/stdout/stderr in verbose mode */
1881 fd = (global.mode & (MODE_QUIET|MODE_VERBOSE)) == MODE_QUIET ? 0 : 3;
1882
1883 while (fd < global.rlimit_nofile)
1884 close(fd++);
1885
Simon Horman98637e52014-06-20 12:30:16 +09001886 environ = check->envp;
Cyril Bontéac92a062014-12-27 22:28:38 +01001887 extchk_setenv(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)));
Simon Horman98637e52014-06-20 12:30:16 +09001888 execvp(px->check_command, check->argv);
1889 Alert("Failed to exec process for external health check: %s. Aborting.\n",
1890 strerror(errno));
1891 exit(-1);
1892 }
1893
1894 /* Parent */
1895 if (check->result == CHK_RES_UNKNOWN) {
1896 if (pid_list_add(pid, t) != NULL) {
1897 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1898
1899 if (px->timeout.check && px->timeout.connect) {
1900 int t_con = tick_add(now_ms, px->timeout.connect);
1901 t->expire = tick_first(t->expire, t_con);
1902 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02001903 status = SF_ERR_NONE;
Simon Horman98637e52014-06-20 12:30:16 +09001904 goto out;
1905 }
1906 else {
1907 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1908 }
1909 kill(pid, SIGTERM); /* process creation error */
1910 }
1911 else
1912 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1913
1914out:
1915 unblock_sigchld();
1916 return status;
1917}
1918
1919/*
Willy Tarreau1e62e2a2017-10-04 15:07:02 +02001920 * manages a server health-check that uses an external process. Returns
Willy Tarreaubaaee002006-06-26 02:48:02 +02001921 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1922 */
Simon Horman98637e52014-06-20 12:30:16 +09001923static struct task *process_chk_proc(struct task *t)
1924{
1925 struct check *check = t->context;
1926 struct server *s = check->server;
Simon Horman98637e52014-06-20 12:30:16 +09001927 int rv;
1928 int ret;
1929 int expired = tick_is_expired(t->expire, now_ms);
1930
1931 if (!(check->state & CHK_ST_INPROGRESS)) {
1932 /* no check currently running */
1933 if (!expired) /* woke up too early */
1934 return t;
1935
1936 /* we don't send any health-checks when the proxy is
1937 * stopped, the server should not be checked or the check
1938 * is disabled.
1939 */
1940 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
1941 s->proxy->state == PR_STSTOPPED)
1942 goto reschedule;
1943
1944 /* we'll initiate a new check */
1945 set_server_check_status(check, HCHK_STATUS_START, NULL);
1946
1947 check->state |= CHK_ST_INPROGRESS;
1948
Simon Hormandbf70192015-01-30 11:22:53 +09001949 ret = connect_proc_chk(t);
Willy Tarreaud7c3fbd2017-10-04 15:19:26 +02001950 if (ret == SF_ERR_NONE) {
Willy Tarreau1e62e2a2017-10-04 15:07:02 +02001951 /* the process was forked, we allow up to min(inter,
1952 * timeout.connect) for it to report its status, but
1953 * only when timeout.check is set as it may be to short
1954 * for a full check otherwise.
Simon Horman98637e52014-06-20 12:30:16 +09001955 */
1956 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1957
1958 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1959 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1960 t->expire = tick_first(t->expire, t_con);
1961 }
1962
1963 goto reschedule;
Simon Horman98637e52014-06-20 12:30:16 +09001964 }
1965
Willy Tarreau1e62e2a2017-10-04 15:07:02 +02001966 /* here, we failed to start the check */
Simon Horman98637e52014-06-20 12:30:16 +09001967
1968 check->state &= ~CHK_ST_INPROGRESS;
1969 check_notify_failure(check);
1970
1971 /* we allow up to min(inter, timeout.connect) for a connection
1972 * to establish but only when timeout.check is set
1973 * as it may be to short for a full check otherwise
1974 */
1975 while (tick_is_expired(t->expire, now_ms)) {
1976 int t_con;
1977
1978 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1979 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1980
1981 if (s->proxy->timeout.check)
1982 t->expire = tick_first(t->expire, t_con);
1983 }
1984 }
1985 else {
1986 /* there was a test running.
1987 * First, let's check whether there was an uncaught error,
1988 * which can happen on connect timeout or error.
1989 */
1990 if (check->result == CHK_RES_UNKNOWN) {
1991 /* good connection is enough for pure TCP check */
1992 struct pid_list *elem = check->curpid;
1993 int status = HCHK_STATUS_UNKNOWN;
1994
1995 if (elem->exited) {
1996 status = elem->status; /* Save in case the process exits between use below */
1997 if (!WIFEXITED(status))
1998 check->code = -1;
1999 else
2000 check->code = WEXITSTATUS(status);
2001 if (!WIFEXITED(status) || WEXITSTATUS(status))
2002 status = HCHK_STATUS_PROCERR;
2003 else
2004 status = HCHK_STATUS_PROCOK;
2005 } else if (expired) {
2006 status = HCHK_STATUS_PROCTOUT;
Willy Tarreaudc3d1902014-07-08 00:56:27 +02002007 Warning("kill %d\n", (int)elem->pid);
Simon Horman98637e52014-06-20 12:30:16 +09002008 kill(elem->pid, SIGTERM);
2009 }
2010 set_server_check_status(check, status, NULL);
2011 }
2012
2013 if (check->result == CHK_RES_FAILED) {
2014 /* a failure or timeout detected */
2015 check_notify_failure(check);
2016 }
2017 else if (check->result == CHK_RES_CONDPASS) {
2018 /* check is OK but asks for stopping mode */
2019 check_notify_stopping(check);
2020 }
2021 else if (check->result == CHK_RES_PASSED) {
2022 /* a success was detected */
2023 check_notify_success(check);
2024 }
2025 check->state &= ~CHK_ST_INPROGRESS;
2026
2027 pid_list_del(check->curpid);
2028
2029 rv = 0;
2030 if (global.spread_checks > 0) {
2031 rv = srv_getinter(check) * global.spread_checks / 100;
2032 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
2033 }
2034 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
2035 }
2036
2037 reschedule:
2038 while (tick_is_expired(t->expire, now_ms))
2039 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2040 return t;
2041}
2042
2043/*
2044 * manages a server health-check that uses a connection. Returns
2045 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2046 */
2047static struct task *process_chk_conn(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002048{
Simon Horman4a741432013-02-23 15:35:38 +09002049 struct check *check = t->context;
2050 struct server *s = check->server;
2051 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002052 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002053 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002054 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002055
Willy Tarreau2c115e52013-12-11 19:41:16 +01002056 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01002057 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002058 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01002059 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002060
Simon Horman671b6f02013-11-25 10:46:39 +09002061 /* we don't send any health-checks when the proxy is
2062 * stopped, the server should not be checked or the check
2063 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002064 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01002065 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01002066 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01002067 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002068
2069 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09002070 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002071
Willy Tarreau2c115e52013-12-11 19:41:16 +01002072 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09002073 check->bi->p = check->bi->data;
2074 check->bi->i = 0;
2075 check->bo->p = check->bo->data;
2076 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002077
Simon Hormandbf70192015-01-30 11:22:53 +09002078 ret = connect_conn_chk(t);
Willy Tarreau00149122017-10-04 18:05:01 +02002079 conn = check->conn;
2080
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)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002097 conn_xprt_want_recv(conn); /* prepare for reading a possible reply */
Willy Tarreau06559ac2013-12-05 01:53:08 +01002098
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 Tarreau00149122017-10-04 18:05:01 +02002103 if (conn)
2104 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002105 chk_report_conn_err(check, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002106 break;
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02002107 /* should share same code than cases below */
2108 case SF_ERR_CHK_PORT:
2109 check->state |= CHK_ST_PORT_MISS;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002110 case SF_ERR_PRXCOND:
2111 case SF_ERR_RESOURCE:
2112 case SF_ERR_INTERNAL:
Willy Tarreau00149122017-10-04 18:05:01 +02002113 if (conn)
2114 conn->flags |= CO_FL_ERROR;
2115 chk_report_conn_err(check, conn ? 0 : ENOMEM, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002116 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002117 }
2118
Willy Tarreau5a78f362012-11-23 12:47:05 +01002119 /* here, we have seen a synchronous error, no fd was allocated */
Olivier Houchard390485a2017-10-24 19:03:30 +02002120 if (conn) {
2121 conn_free(conn);
2122 check->conn = conn = NULL;
2123 }
Willy Tarreau6b0a8502012-11-23 08:51:32 +01002124
Willy Tarreau2c115e52013-12-11 19:41:16 +01002125 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02002126 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002127
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002128 /* we allow up to min(inter, timeout.connect) for a connection
2129 * to establish but only when timeout.check is set
2130 * as it may be to short for a full check otherwise
2131 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002132 while (tick_is_expired(t->expire, now_ms)) {
2133 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002134
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002135 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09002136 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002137
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002138 if (s->proxy->timeout.check)
2139 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002140 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002141 }
2142 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02002143 /* there was a test running.
2144 * First, let's check whether there was an uncaught error,
2145 * which can happen on connect timeout or error.
2146 */
Simon Hormanccaabcd2014-06-20 12:29:47 +09002147 if (check->result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002148 /* good connection is enough for pure TCP check */
2149 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09002150 if (check->use_ssl)
2151 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02002152 else
Simon Horman4a741432013-02-23 15:35:38 +09002153 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002154 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002155 else if ((conn->flags & CO_FL_ERROR) || expired) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002156 chk_report_conn_err(check, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02002157 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002158 else
2159 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02002160 }
2161
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002162 /* check complete or aborted */
Willy Tarreau00149122017-10-04 18:05:01 +02002163 if (conn && conn->xprt) {
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002164 /* The check was aborted and the connection was not yet closed.
2165 * This can happen upon timeout, or when an external event such
2166 * as a failed response coupled with "observe layer7" caused the
2167 * server state to be suddenly changed.
2168 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01002169 conn_sock_drain(conn);
Willy Tarreau402dbc12017-10-05 17:53:13 +02002170 conn_full_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002171 }
2172
Willy Tarreau00149122017-10-04 18:05:01 +02002173 if (conn) {
2174 conn_free(conn);
2175 check->conn = conn = NULL;
2176 }
2177
Willy Tarreauaf549582014-05-16 17:37:50 +02002178 if (check->result == CHK_RES_FAILED) {
2179 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002180 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002181 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002182 else if (check->result == CHK_RES_CONDPASS) {
2183 /* check is OK but asks for stopping mode */
2184 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002185 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002186 else if (check->result == CHK_RES_PASSED) {
2187 /* a success was detected */
2188 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002189 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01002190 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002191
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002192 rv = 0;
2193 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002194 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002195 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002196 }
Simon Horman4a741432013-02-23 15:35:38 +09002197 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002198 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002199
2200 reschedule:
2201 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002202 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002203 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01002204 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002205}
2206
Simon Horman98637e52014-06-20 12:30:16 +09002207/*
2208 * manages a server health-check. Returns
2209 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2210 */
2211static struct task *process_chk(struct task *t)
2212{
2213 struct check *check = t->context;
2214
2215 if (check->type == PR_O2_EXT_CHK)
2216 return process_chk_proc(t);
2217 return process_chk_conn(t);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002218
Simon Horman98637e52014-06-20 12:30:16 +09002219}
2220
Simon Horman5c942422013-11-25 10:46:32 +09002221static int start_check_task(struct check *check, int mininter,
2222 int nbcheck, int srvpos)
2223{
2224 struct task *t;
2225 /* task for the check */
2226 if ((t = task_new()) == NULL) {
2227 Alert("Starting [%s:%s] check: out of memory.\n",
2228 check->server->proxy->id, check->server->id);
2229 return 0;
2230 }
2231
2232 check->task = t;
2233 t->process = process_chk;
2234 t->context = check;
2235
Willy Tarreau1746eec2014-04-25 10:46:47 +02002236 if (mininter < srv_getinter(check))
2237 mininter = srv_getinter(check);
2238
2239 if (global.max_spread_checks && mininter > global.max_spread_checks)
2240 mininter = global.max_spread_checks;
2241
Simon Horman5c942422013-11-25 10:46:32 +09002242 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002243 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002244 check->start = now;
2245 task_queue(t);
2246
2247 return 1;
2248}
2249
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002250/*
2251 * Start health-check.
Willy Tarreau865c5142016-12-21 20:04:48 +01002252 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002253 */
Willy Tarreau865c5142016-12-21 20:04:48 +01002254static int start_checks()
2255{
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002256
2257 struct proxy *px;
2258 struct server *s;
2259 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002260 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002261
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002262 /* 1- count the checkers to run simultaneously.
2263 * We also determine the minimum interval among all of those which
2264 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2265 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002266 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002267 * too short an interval for all others.
2268 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002269 for (px = proxy; px; px = px->next) {
2270 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002271 if (s->slowstart) {
2272 if ((t = task_new()) == NULL) {
2273 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Willy Tarreau865c5142016-12-21 20:04:48 +01002274 return ERR_ALERT | ERR_FATAL;
Willy Tarreaue7b73482013-11-21 11:50:50 +01002275 }
2276 /* We need a warmup task that will be called when the server
2277 * state switches from down to up.
2278 */
2279 s->warmup = t;
2280 t->process = server_warmup;
2281 t->context = s;
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002282 /* server can be in this state only because of */
Emeric Brun52a91d32017-08-31 14:41:55 +02002283 if (s->next_state == SRV_ST_STARTING)
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002284 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 +01002285 }
2286
Willy Tarreaud8514a22013-12-11 21:10:14 +01002287 if (s->check.state & CHK_ST_CONFIGURED) {
2288 nbcheck++;
2289 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2290 (!mininter || mininter > srv_getinter(&s->check)))
2291 mininter = srv_getinter(&s->check);
2292 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002293
Willy Tarreaud8514a22013-12-11 21:10:14 +01002294 if (s->agent.state & CHK_ST_CONFIGURED) {
2295 nbcheck++;
2296 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2297 (!mininter || mininter > srv_getinter(&s->agent)))
2298 mininter = srv_getinter(&s->agent);
2299 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002300 }
2301 }
2302
Simon Horman4a741432013-02-23 15:35:38 +09002303 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002304 return 0;
2305
2306 srand((unsigned)time(NULL));
2307
2308 /*
2309 * 2- start them as far as possible from each others. For this, we will
2310 * start them after their interval set to the min interval divided by
2311 * the number of servers, weighted by the server's position in the list.
2312 */
2313 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002314 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2315 if (init_pid_list()) {
2316 Alert("Starting [%s] check: out of memory.\n", px->id);
Willy Tarreau865c5142016-12-21 20:04:48 +01002317 return ERR_ALERT | ERR_FATAL;
Simon Horman98637e52014-06-20 12:30:16 +09002318 }
2319 }
2320
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002321 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002322 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002323 if (s->check.state & CHK_ST_CONFIGURED) {
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002324 if (s->check.type == PR_O2_EXT_CHK) {
2325 if (!prepare_external_check(&s->check))
Willy Tarreau865c5142016-12-21 20:04:48 +01002326 return ERR_ALERT | ERR_FATAL;
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002327 }
Simon Hormand60d6912013-11-25 10:46:36 +09002328 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
Willy Tarreau865c5142016-12-21 20:04:48 +01002329 return ERR_ALERT | ERR_FATAL;
Simon Hormand60d6912013-11-25 10:46:36 +09002330 srvpos++;
2331 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002332
Simon Hormand60d6912013-11-25 10:46:36 +09002333 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002334 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002335 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
Willy Tarreau865c5142016-12-21 20:04:48 +01002336 return ERR_ALERT | ERR_FATAL;
Simon Hormand60d6912013-11-25 10:46:36 +09002337 }
2338 srvpos++;
2339 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002340 }
2341 }
2342 return 0;
2343}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002344
2345/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002346 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002347 * The buffer MUST be terminated by a null byte before calling this function.
2348 * Sets server status appropriately. The caller is responsible for ensuring
2349 * that the buffer contains at least 13 characters. If <done> is zero, we may
2350 * return 0 to indicate that data is required to decide of a match.
2351 */
2352static int httpchk_expect(struct server *s, int done)
2353{
2354 static char status_msg[] = "HTTP status check returned code <000>";
2355 char status_code[] = "000";
2356 char *contentptr;
2357 int crlf;
2358 int ret;
2359
2360 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2361 case PR_O2_EXP_STS:
2362 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002363 memcpy(status_code, s->check.bi->data + 9, 3);
2364 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002365
2366 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2367 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2368 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002369 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002370
2371 /* we necessarily have the response, so there are no partial failures */
2372 if (s->proxy->options2 & PR_O2_EXP_INV)
2373 ret = !ret;
2374
Simon Horman4a741432013-02-23 15:35:38 +09002375 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002376 break;
2377
2378 case PR_O2_EXP_STR:
2379 case PR_O2_EXP_RSTR:
2380 /* very simple response parser: ignore CR and only count consecutive LFs,
2381 * stop with contentptr pointing to first char after the double CRLF or
2382 * to '\0' if crlf < 2.
2383 */
2384 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002385 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002386 if (crlf >= 2)
2387 break;
2388 if (*contentptr == '\r')
2389 continue;
2390 else if (*contentptr == '\n')
2391 crlf++;
2392 else
2393 crlf = 0;
2394 }
2395
2396 /* Check that response contains a body... */
2397 if (crlf < 2) {
2398 if (!done)
2399 return 0;
2400
Simon Horman4a741432013-02-23 15:35:38 +09002401 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002402 "HTTP content check could not find a response body");
2403 return 1;
2404 }
2405
2406 /* Check that response body is not empty... */
2407 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002408 if (!done)
2409 return 0;
2410
Simon Horman4a741432013-02-23 15:35:38 +09002411 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002412 "HTTP content check found empty response body");
2413 return 1;
2414 }
2415
2416 /* Check the response content against the supplied string
2417 * or regex... */
2418 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2419 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2420 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002421 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002422
2423 /* if we don't match, we may need to wait more */
2424 if (!ret && !done)
2425 return 0;
2426
2427 if (ret) {
2428 /* content matched */
2429 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002430 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002431 "HTTP check matched unwanted content");
2432 else
Simon Horman4a741432013-02-23 15:35:38 +09002433 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002434 "HTTP content check matched");
2435 }
2436 else {
2437 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002438 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002439 "HTTP check did not match unwanted content");
2440 else
Simon Horman4a741432013-02-23 15:35:38 +09002441 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002442 "HTTP content check did not match");
2443 }
2444 break;
2445 }
2446 return 1;
2447}
2448
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002449/*
2450 * return the id of a step in a send/expect session
2451 */
Simon Hormane16c1b32015-01-30 11:22:57 +09002452static int tcpcheck_get_step_id(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002453{
2454 struct tcpcheck_rule *cur = NULL, *next = NULL;
2455 int i = 0;
2456
Willy Tarreau213c6782014-10-02 14:51:02 +02002457 /* not even started anything yet => step 0 = initial connect */
Baptiste Assmannf95bc8e2015-04-25 16:03:06 +02002458 if (!check->current_step)
Willy Tarreau213c6782014-10-02 14:51:02 +02002459 return 0;
2460
Simon Hormane16c1b32015-01-30 11:22:57 +09002461 cur = check->last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002462
2463 /* no step => first step */
2464 if (cur == NULL)
2465 return 1;
2466
2467 /* increment i until current step */
Simon Hormane16c1b32015-01-30 11:22:57 +09002468 list_for_each_entry(next, check->tcpcheck_rules, list) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002469 if (next->list.p == &cur->list)
2470 break;
2471 ++i;
2472 }
2473
2474 return i;
2475}
2476
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002477/*
2478 * return the latest known comment before (including) the given stepid
2479 * returns NULL if no comment found
2480 */
2481static char * tcpcheck_get_step_comment(struct check *check, int stepid)
2482{
2483 struct tcpcheck_rule *cur = NULL;
2484 char *ret = NULL;
2485 int i = 0;
2486
2487 /* not even started anything yet, return latest comment found before any action */
2488 if (!check->current_step) {
2489 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2490 if (cur->action == TCPCHK_ACT_COMMENT)
2491 ret = cur->comment;
2492 else
2493 goto return_comment;
2494 }
2495 }
2496
2497 i = 1;
2498 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2499 if (cur->comment)
2500 ret = cur->comment;
2501
2502 if (i >= stepid)
2503 goto return_comment;
2504
2505 ++i;
2506 }
2507
2508 return_comment:
2509 return ret;
2510}
2511
Willy Tarreaube74b882017-10-04 16:22:49 +02002512/* proceed with next steps for the TCP checks <check>. Note that this is called
2513 * both from the connection's wake() callback and from the check scheduling task.
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002514 * It returns 0 on normal cases, or <0 if a close() has happened on an existing
2515 * connection, presenting the risk of an fd replacement.
Willy Tarreaube74b882017-10-04 16:22:49 +02002516 */
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002517static int tcpcheck_main(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002518{
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002519 char *contentptr, *comment;
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002520 struct tcpcheck_rule *next;
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002521 int done = 0, ret = 0, step = 0;
Willy Tarreaube74b882017-10-04 16:22:49 +02002522 struct connection *conn = check->conn;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002523 struct server *s = check->server;
2524 struct task *t = check->task;
Simon Hormane16c1b32015-01-30 11:22:57 +09002525 struct list *head = check->tcpcheck_rules;
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002526 int retcode = 0;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002527
Willy Tarreauef953952014-10-02 14:30:14 +02002528 /* here, we know that the check is complete or that it failed */
2529 if (check->result != CHK_RES_UNKNOWN)
2530 goto out_end_tcpcheck;
2531
2532 /* We have 4 possibilities here :
2533 * 1. we've not yet attempted step 1, and step 1 is a connect, so no
Willy Tarreau00149122017-10-04 18:05:01 +02002534 * connection attempt was made yet ; conn==NULL;current_step==NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002535 * 2. we've not yet attempted step 1, and step 1 is a not connect or
2536 * does not exist (no rule), so a connection attempt was made
Willy Tarreau00149122017-10-04 18:05:01 +02002537 * before coming here, conn!=NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002538 * 3. we're coming back after having started with step 1, so we may
Willy Tarreau00149122017-10-04 18:05:01 +02002539 * be waiting for a connection attempt to complete. conn!=NULL.
2540 * 4. the connection + handshake are complete. conn!=NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002541 *
2542 * #2 and #3 are quite similar, we want both the connection and the
2543 * handshake to complete before going any further. Thus we must always
2544 * wait for a connection to complete unless we're before and existing
2545 * step 1.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002546 */
Willy Tarreau449f9522015-05-13 15:39:48 +02002547
2548 /* find first rule and skip comments */
2549 next = LIST_NEXT(head, struct tcpcheck_rule *, list);
2550 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
2551 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
2552
Willy Tarreau00149122017-10-04 18:05:01 +02002553 if ((check->current_step || &next->list == head) &&
2554 (!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE))) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002555 /* we allow up to min(inter, timeout.connect) for a connection
2556 * to establish but only when timeout.check is set
2557 * as it may be to short for a full check otherwise
2558 */
2559 while (tick_is_expired(t->expire, now_ms)) {
2560 int t_con;
2561
2562 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2563 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2564
2565 if (s->proxy->timeout.check)
2566 t->expire = tick_first(t->expire, t_con);
2567 }
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002568 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002569 }
2570
Willy Tarreauef953952014-10-02 14:30:14 +02002571 /* special case: option tcp-check with no rule, a connect is enough */
Willy Tarreau449f9522015-05-13 15:39:48 +02002572 if (&next->list == head) {
Willy Tarreauef953952014-10-02 14:30:14 +02002573 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002574 goto out_end_tcpcheck;
Willy Tarreauef953952014-10-02 14:30:14 +02002575 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002576
Willy Tarreau213c6782014-10-02 14:51:02 +02002577 /* no step means first step initialisation */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002578 if (check->current_step == NULL) {
Willy Tarreau213c6782014-10-02 14:51:02 +02002579 check->last_started_step = NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002580 check->bo->p = check->bo->data;
2581 check->bo->o = 0;
2582 check->bi->p = check->bi->data;
2583 check->bi->i = 0;
Willy Tarreau449f9522015-05-13 15:39:48 +02002584 check->current_step = next;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002585 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2586 if (s->proxy->timeout.check)
2587 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2588 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002589
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002590 /* It's only the rules which will enable send/recv */
Willy Tarreau00149122017-10-04 18:05:01 +02002591 if (conn)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002592 __conn_xprt_stop_both(conn);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002593
Willy Tarreauabca5b62013-12-06 14:19:25 +01002594 while (1) {
Willy Tarreau263013d2015-05-13 11:59:14 +02002595 /* We have to try to flush the output buffer before reading, at
2596 * the end, or if we're about to send a string that does not fit
2597 * in the remaining space. That explains why we break out of the
Willy Tarreau00149122017-10-04 18:05:01 +02002598 * loop after this control. If we have data, conn is valid.
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002599 */
2600 if (check->bo->o &&
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002601 (&check->current_step->list == head ||
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002602 check->current_step->action != TCPCHK_ACT_SEND ||
2603 check->current_step->string_len >= buffer_total_space(check->bo))) {
2604
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002605 __conn_xprt_want_send(conn);
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002606 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002607 if (conn->flags & CO_FL_ERROR) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002608 chk_report_conn_err(check, errno, 0);
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002609 __conn_xprt_stop_both(conn);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002610 goto out_end_tcpcheck;
2611 }
Willy Tarreau263013d2015-05-13 11:59:14 +02002612 break;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002613 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002614 }
2615
Willy Tarreau263013d2015-05-13 11:59:14 +02002616 if (&check->current_step->list == head)
Willy Tarreauabca5b62013-12-06 14:19:25 +01002617 break;
2618
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002619 /* have 'next' point to the next rule or NULL if we're on the
2620 * last one, connect() needs this.
2621 */
Willy Tarreau5581c272015-05-13 12:24:53 +02002622 next = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002623
2624 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002625 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002626 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002627
2628 /* NULL if we're on the last rule */
Willy Tarreauf3d34822014-12-08 12:11:28 +01002629 if (&next->list == head)
2630 next = NULL;
2631
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002632 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2633 struct protocol *proto;
2634 struct xprt_ops *xprt;
2635
Willy Tarreau00149122017-10-04 18:05:01 +02002636 /* For a connect action we'll create a new connection.
2637 * We may also have to kill a previous one. But we don't
2638 * want to leave *without* a connection if we came here
2639 * from the connection layer, hence with a connection.
2640 * Thus we'll proceed in the following order :
2641 * 1: close but not release previous connection
2642 * 2: try to get a new connection
2643 * 3: release and replace the old one on success
2644 */
2645 if (check->conn) {
Willy Tarreau402dbc12017-10-05 17:53:13 +02002646 conn_full_close(check->conn);
Willy Tarreau00149122017-10-04 18:05:01 +02002647 retcode = -1; /* do not reuse the fd! */
2648 }
2649
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002650 /* mark the step as started */
2651 check->last_started_step = check->current_step;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002652
2653 /* prepare new connection */
Willy Tarreau00149122017-10-04 18:05:01 +02002654 conn = conn_new();
2655 if (!conn) {
2656 step = tcpcheck_get_step_id(check);
2657 chunk_printf(&trash, "TCPCHK error allocating connection at step %d", step);
2658 comment = tcpcheck_get_step_comment(check, step);
2659 if (comment)
2660 chunk_appendf(&trash, " comment: '%s'", comment);
2661 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2662 check->current_step = NULL;
2663 return retcode;
2664 }
2665
2666 if (check->conn)
2667 conn_free(check->conn);
2668 check->conn = conn;
2669
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002670 conn_attach(conn, check, &check_conn_cb);
2671 conn->target = &s->obj_type;
2672
2673 /* no client address */
2674 clear_addr(&conn->addr.from);
2675
Simon Horman41f58762015-01-30 11:22:56 +09002676 if (is_addr(&check->addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002677 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09002678 conn->addr.to = check->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002679 }
2680 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002681 /* we'll connect to the addr on the server */
2682 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002683 }
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01002684 proto = protocol_by_family(conn->addr.to.ss_family);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002685
2686 /* port */
2687 if (check->current_step->port)
2688 set_host_port(&conn->addr.to, check->current_step->port);
2689 else if (check->port)
2690 set_host_port(&conn->addr.to, check->port);
2691
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002692 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002693 xprt = xprt_get(XPRT_SSL);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002694 }
2695 else {
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002696 xprt = xprt_get(XPRT_RAW);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002697 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002698 conn_prepare(conn, proto, xprt);
2699
Willy Tarreaue7dff022015-04-03 01:14:29 +02002700 ret = SF_ERR_INTERNAL;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002701 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01002702 ret = proto->connect(conn,
2703 1 /* I/O polling is always needed */,
2704 (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002705 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2706 conn->send_proxy_ofs = 1;
2707 conn->flags |= CO_FL_SEND_PROXY;
2708 }
2709
2710 /* It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02002711 * - SF_ERR_NONE if everything's OK
2712 * - SF_ERR_SRVTO if there are no more servers
2713 * - SF_ERR_SRVCL if the connection was refused by the server
2714 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2715 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2716 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +01002717 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002718 * Note that we try to prevent the network stack from sending the ACK during the
2719 * connect() when a pure TCP check is used (without PROXY protocol).
2720 */
2721 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002722 case SF_ERR_NONE:
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002723 /* we allow up to min(inter, timeout.connect) for a connection
2724 * to establish but only when timeout.check is set
2725 * as it may be to short for a full check otherwise
2726 */
2727 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2728
2729 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2730 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2731 t->expire = tick_first(t->expire, t_con);
2732 }
2733 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002734 case SF_ERR_SRVTO: /* ETIMEDOUT */
2735 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002736 step = tcpcheck_get_step_id(check);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002737 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002738 step, strerror(errno));
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002739 comment = tcpcheck_get_step_comment(check, step);
2740 if (comment)
2741 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002742 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2743 goto out_end_tcpcheck;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002744 case SF_ERR_PRXCOND:
2745 case SF_ERR_RESOURCE:
2746 case SF_ERR_INTERNAL:
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002747 step = tcpcheck_get_step_id(check);
2748 chunk_printf(&trash, "TCPCHK error establishing connection at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002749 comment = tcpcheck_get_step_comment(check, step);
2750 if (comment)
2751 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002752 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2753 goto out_end_tcpcheck;
2754 }
2755
2756 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002757 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002758
2759 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002760 while (&check->current_step->list != head &&
2761 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002762 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002763
Willy Tarreauf2c87352015-05-13 12:08:21 +02002764 if (&check->current_step->list == head)
2765 break;
2766
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002767 /* don't do anything until the connection is established */
2768 if (!(conn->flags & CO_FL_CONNECTED)) {
2769 /* update expire time, should be done by process_chk */
2770 /* we allow up to min(inter, timeout.connect) for a connection
2771 * to establish but only when timeout.check is set
2772 * as it may be to short for a full check otherwise
2773 */
2774 while (tick_is_expired(t->expire, now_ms)) {
2775 int t_con;
2776
2777 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2778 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2779
2780 if (s->proxy->timeout.check)
2781 t->expire = tick_first(t->expire, t_con);
2782 }
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002783 return retcode;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002784 }
2785
2786 } /* end 'connect' */
2787 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2788 /* mark the step as started */
2789 check->last_started_step = check->current_step;
2790
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002791 /* reset the read buffer */
2792 if (*check->bi->data != '\0') {
2793 *check->bi->data = '\0';
2794 check->bi->i = 0;
2795 }
2796
Willy Tarreaubbae3f02017-08-30 09:59:52 +02002797 if (conn->flags & CO_FL_SOCK_WR_SH) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002798 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002799 chk_report_conn_err(check, 0, 0);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002800 goto out_end_tcpcheck;
2801 }
2802
Willy Tarreauabca5b62013-12-06 14:19:25 +01002803 if (check->current_step->string_len >= check->bo->size) {
2804 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2805 check->current_step->string_len, check->bo->size,
Simon Hormane16c1b32015-01-30 11:22:57 +09002806 tcpcheck_get_step_id(check));
Willy Tarreauabca5b62013-12-06 14:19:25 +01002807 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2808 goto out_end_tcpcheck;
2809 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002810
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002811 /* do not try to send if there is no space */
2812 if (check->current_step->string_len >= buffer_total_space(check->bo))
2813 continue;
2814
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002815 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2816 *check->bo->p = '\0'; /* to make gdb output easier to read */
2817
Willy Tarreauabca5b62013-12-06 14:19:25 +01002818 /* go to next rule and try to send */
Willy Tarreau5581c272015-05-13 12:24:53 +02002819 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002820
2821 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002822 while (&check->current_step->list != head &&
2823 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002824 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Willy Tarreauf2c87352015-05-13 12:08:21 +02002825
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002826 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002827 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002828 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002829 goto out_end_tcpcheck;
2830
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002831 __conn_xprt_want_recv(conn);
Willy Tarreau310987a2014-01-22 19:46:33 +01002832 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreau54e917c2017-08-30 07:35:35 +02002833 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002834 done = 1;
2835 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2836 /* Report network errors only if we got no other data. Otherwise
2837 * we'll let the upper layers decide whether the response is OK
2838 * or not. It is very common that an RST sent by the server is
2839 * reported as an error just after the last data chunk.
2840 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002841 chk_report_conn_err(check, errno, 0);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002842 goto out_end_tcpcheck;
2843 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002844 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002845 else
Willy Tarreau263013d2015-05-13 11:59:14 +02002846 break;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002847 }
2848
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002849 /* mark the step as started */
2850 check->last_started_step = check->current_step;
2851
2852
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002853 /* Intermediate or complete response received.
2854 * Terminate string in check->bi->data buffer.
2855 */
2856 if (check->bi->i < check->bi->size) {
2857 check->bi->data[check->bi->i] = '\0';
2858 }
2859 else {
2860 check->bi->data[check->bi->i - 1] = '\0';
2861 done = 1; /* buffer full, don't wait for more data */
2862 }
2863
2864 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002865
2866 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002867 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002868 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002869 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002870
2871 /* empty response */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002872 step = tcpcheck_get_step_id(check);
2873 chunk_printf(&trash, "TCPCHK got an empty response at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002874 comment = tcpcheck_get_step_comment(check, step);
2875 if (comment)
2876 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002877 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2878
2879 goto out_end_tcpcheck;
2880 }
2881
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002882 if (!done && (check->current_step->string != NULL) && (check->bi->i < check->current_step->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002883 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002884
Willy Tarreaua970c282013-12-06 12:47:19 +01002885 tcpcheck_expect:
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002886 if (check->current_step->string != NULL)
2887 ret = my_memmem(contentptr, check->bi->i, check->current_step->string, check->current_step->string_len) != NULL;
2888 else if (check->current_step->expect_regex != NULL)
2889 ret = regex_exec(check->current_step->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002890
2891 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002892 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002893
2894 /* matched */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002895 step = tcpcheck_get_step_id(check);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002896 if (ret) {
2897 /* matched but we did not want to => ERROR */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002898 if (check->current_step->inverse) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002899 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002900 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002901 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002902 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002903 }
2904 else {
2905 /* we were looking for a regex */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002906 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d", step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002907 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002908 comment = tcpcheck_get_step_comment(check, step);
2909 if (comment)
2910 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002911 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2912 goto out_end_tcpcheck;
2913 }
2914 /* matched and was supposed to => OK, next step */
2915 else {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002916 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002917 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002918
2919 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002920 while (&check->current_step->list != head &&
2921 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002922 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002923
Willy Tarreauf2c87352015-05-13 12:08:21 +02002924 if (&check->current_step->list == head)
2925 break;
2926
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002927 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002928 goto tcpcheck_expect;
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002929 __conn_xprt_stop_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002930 }
2931 }
2932 else {
2933 /* not matched */
2934 /* not matched and was not supposed to => OK, next step */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002935 if (check->current_step->inverse) {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002936 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002937 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002938
2939 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002940 while (&check->current_step->list != head &&
2941 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002942 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002943
Willy Tarreauf2c87352015-05-13 12:08:21 +02002944 if (&check->current_step->list == head)
2945 break;
2946
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002947 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002948 goto tcpcheck_expect;
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002949 __conn_xprt_stop_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002950 }
2951 /* not matched but was supposed to => ERROR */
2952 else {
2953 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002954 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002955 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002956 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002957 }
2958 else {
2959 /* we were looking for a regex */
2960 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002961 step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002962 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002963 comment = tcpcheck_get_step_comment(check, step);
2964 if (comment)
2965 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002966 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2967 goto out_end_tcpcheck;
2968 }
2969 }
2970 } /* end expect */
2971 } /* end loop over double chained step list */
2972
Willy Tarreau263013d2015-05-13 11:59:14 +02002973 /* We're waiting for some I/O to complete, we've reached the end of the
2974 * rules, or both. Do what we have to do, otherwise we're done.
2975 */
2976 if (&check->current_step->list == head && !check->bo->o) {
2977 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
2978 goto out_end_tcpcheck;
2979 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002980
Willy Tarreau53c5a042015-05-13 11:38:17 +02002981 /* warning, current_step may now point to the head */
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002982 if (check->bo->o)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002983 __conn_xprt_want_send(conn);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002984
Willy Tarreau53c5a042015-05-13 11:38:17 +02002985 if (&check->current_step->list != head &&
2986 check->current_step->action == TCPCHK_ACT_EXPECT)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002987 __conn_xprt_want_recv(conn);
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002988 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002989
2990 out_end_tcpcheck:
2991 /* collect possible new errors */
2992 if (conn->flags & CO_FL_ERROR)
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002993 chk_report_conn_err(check, 0, 0);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002994
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002995 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002996 check->current_step = NULL;
2997
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002998 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002999 conn->flags |= CO_FL_ERROR;
3000
Olivier Houchard1a0545f2017-09-13 18:30:23 +02003001 __conn_xprt_stop_both(conn);
Willy Tarreau6bdcab02017-10-04 18:41:00 +02003002 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003003}
3004
Simon Hormanb1900d52015-01-30 11:22:54 +09003005const char *init_check(struct check *check, int type)
3006{
3007 check->type = type;
3008
3009 /* Allocate buffer for requests... */
3010 if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3011 return "out of memory while allocating check buffer";
3012 }
3013 check->bi->size = global.tune.chksize;
3014
3015 /* Allocate buffer for responses... */
3016 if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3017 return "out of memory while allocating check buffer";
3018 }
3019 check->bo->size = global.tune.chksize;
3020
Simon Hormanb1900d52015-01-30 11:22:54 +09003021 return NULL;
3022}
3023
Simon Hormanbfb5d332015-01-30 11:22:55 +09003024void free_check(struct check *check)
3025{
3026 free(check->bi);
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01003027 check->bi = NULL;
Simon Hormanbfb5d332015-01-30 11:22:55 +09003028 free(check->bo);
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01003029 check->bo = NULL;
Simon Hormanbfb5d332015-01-30 11:22:55 +09003030 free(check->conn);
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01003031 check->conn = NULL;
Simon Hormanbfb5d332015-01-30 11:22:55 +09003032}
3033
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003034void email_alert_free(struct email_alert *alert)
3035{
3036 struct tcpcheck_rule *rule, *back;
3037
3038 if (!alert)
3039 return;
3040
3041 list_for_each_entry_safe(rule, back, &alert->tcpcheck_rules, list)
3042 free(rule);
3043 free(alert);
3044}
3045
3046static struct task *process_email_alert(struct task *t)
3047{
3048 struct check *check = t->context;
3049 struct email_alertq *q;
3050
3051 q = container_of(check, typeof(*q), check);
3052
3053 if (!(check->state & CHK_ST_ENABLED)) {
3054 if (LIST_ISEMPTY(&q->email_alerts)) {
3055 /* All alerts processed, delete check */
3056 task_delete(t);
3057 task_free(t);
3058 check->task = NULL;
3059 return NULL;
3060 } else {
3061 struct email_alert *alert;
3062
3063 alert = LIST_NEXT(&q->email_alerts, typeof(alert), list);
3064 check->tcpcheck_rules = &alert->tcpcheck_rules;
3065 LIST_DEL(&alert->list);
3066
3067 check->state |= CHK_ST_ENABLED;
3068 }
3069
3070 }
3071
3072 process_chk(t);
3073
3074 if (!(check->state & CHK_ST_INPROGRESS) && check->tcpcheck_rules) {
3075 struct email_alert *alert;
3076
3077 alert = container_of(check->tcpcheck_rules, typeof(*alert), tcpcheck_rules);
3078 email_alert_free(alert);
3079
3080 check->tcpcheck_rules = NULL;
3081 check->state &= ~CHK_ST_ENABLED;
3082 }
3083 return t;
3084}
3085
3086static int init_email_alert_checks(struct server *s)
3087{
3088 int i;
3089 struct mailer *mailer;
3090 const char *err_str;
3091 struct proxy *p = s->proxy;
3092
3093 if (p->email_alert.queues)
3094 /* Already initialised, nothing to do */
3095 return 1;
3096
3097 p->email_alert.queues = calloc(p->email_alert.mailers.m->count, sizeof *p->email_alert.queues);
3098 if (!p->email_alert.queues) {
3099 err_str = "out of memory while allocating checks array";
3100 goto error_alert;
3101 }
3102
3103 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3104 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
3105 struct email_alertq *q = &p->email_alert.queues[i];
3106 struct check *check = &q->check;
3107
3108
3109 LIST_INIT(&q->email_alerts);
3110
Pieter Baauw235fcfc2016-02-13 15:33:40 +01003111 check->inter = p->email_alert.mailers.m->timeout.mail;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003112 check->rise = DEF_AGENT_RISETIME;
3113 check->fall = DEF_AGENT_FALLTIME;
3114 err_str = init_check(check, PR_O2_TCPCHK_CHK);
3115 if (err_str) {
3116 goto error_free;
3117 }
3118
3119 check->xprt = mailer->xprt;
3120 if (!get_host_port(&mailer->addr))
3121 /* Default to submission port */
3122 check->port = 587;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003123 check->addr = mailer->addr;
3124 check->server = s;
3125 }
3126
3127 return 1;
3128
3129error_free:
3130 while (i-- > 1)
3131 task_free(p->email_alert.queues[i].check.task);
3132 free(p->email_alert.queues);
3133 p->email_alert.queues = NULL;
3134error_alert:
3135 Alert("Email alert [%s] could not be initialised: %s\n", p->id, err_str);
3136 return 0;
3137}
3138
3139
3140static int add_tcpcheck_expect_str(struct list *list, const char *str)
3141{
3142 struct tcpcheck_rule *tcpcheck;
3143
3144 tcpcheck = calloc(1, sizeof *tcpcheck);
3145 if (!tcpcheck)
3146 return 0;
3147
3148 tcpcheck->action = TCPCHK_ACT_EXPECT;
3149 tcpcheck->string = strdup(str);
3150 if (!tcpcheck->string) {
3151 free(tcpcheck);
3152 return 0;
3153 }
3154
3155 LIST_ADDQ(list, &tcpcheck->list);
3156 return 1;
3157}
3158
3159static int add_tcpcheck_send_strs(struct list *list, const char * const *strs)
3160{
3161 struct tcpcheck_rule *tcpcheck;
Willy Tarreau64345aa2016-08-10 19:29:09 +02003162 const char *in;
3163 char *dst;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003164 int i;
3165
3166 tcpcheck = calloc(1, sizeof *tcpcheck);
3167 if (!tcpcheck)
3168 return 0;
3169
3170 tcpcheck->action = TCPCHK_ACT_SEND;
3171
3172 tcpcheck->string_len = 0;
3173 for (i = 0; strs[i]; i++)
3174 tcpcheck->string_len += strlen(strs[i]);
3175
3176 tcpcheck->string = malloc(tcpcheck->string_len + 1);
3177 if (!tcpcheck->string) {
3178 free(tcpcheck);
3179 return 0;
3180 }
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003181
Willy Tarreau64345aa2016-08-10 19:29:09 +02003182 dst = tcpcheck->string;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003183 for (i = 0; strs[i]; i++)
Willy Tarreau64345aa2016-08-10 19:29:09 +02003184 for (in = strs[i]; (*dst = *in++); dst++);
3185 *dst = 0;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003186
3187 LIST_ADDQ(list, &tcpcheck->list);
3188 return 1;
3189}
3190
3191static int enqueue_one_email_alert(struct email_alertq *q, const char *msg)
3192{
3193 struct email_alert *alert = NULL;
3194 struct tcpcheck_rule *tcpcheck;
3195 struct check *check = &q->check;
3196 struct proxy *p = check->server->proxy;
3197
3198 alert = calloc(1, sizeof *alert);
3199 if (!alert) {
3200 goto error;
3201 }
3202 LIST_INIT(&alert->tcpcheck_rules);
3203
3204 tcpcheck = calloc(1, sizeof *tcpcheck);
3205 if (!tcpcheck)
3206 goto error;
3207 tcpcheck->action = TCPCHK_ACT_CONNECT;
3208 LIST_ADDQ(&alert->tcpcheck_rules, &tcpcheck->list);
3209
3210 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "220 "))
3211 goto error;
3212
3213 {
3214 const char * const strs[4] = { "EHLO ", p->email_alert.myhostname, "\r\n" };
3215 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3216 goto error;
3217 }
3218
3219 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3220 goto error;
3221
3222 {
3223 const char * const strs[4] = { "MAIL FROM:<", p->email_alert.from, ">\r\n" };
3224 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3225 goto error;
3226 }
3227
3228 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3229 goto error;
3230
3231 {
3232 const char * const strs[4] = { "RCPT TO:<", p->email_alert.to, ">\r\n" };
3233 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3234 goto error;
3235 }
3236
3237 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3238 goto error;
3239
3240 {
3241 const char * const strs[2] = { "DATA\r\n" };
3242 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3243 goto error;
3244 }
3245
3246 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "354 "))
3247 goto error;
3248
3249 {
3250 struct tm tm;
3251 char datestr[48];
3252 const char * const strs[18] = {
Pieter Baauw5e0964e2016-02-13 16:27:35 +01003253 "From: ", p->email_alert.from, "\r\n",
3254 "To: ", p->email_alert.to, "\r\n",
3255 "Date: ", datestr, "\r\n",
3256 "Subject: [HAproxy Alert] ", msg, "\r\n",
3257 "\r\n",
3258 msg, "\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003259 "\r\n",
Pieter Baauwed35c372015-07-22 19:51:54 +02003260 ".\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003261 NULL
3262 };
3263
3264 get_localtime(date.tv_sec, &tm);
3265
3266 if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %T %z (%Z)", &tm) == 0) {
3267 goto error;
3268 }
3269
3270 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3271 goto error;
3272 }
3273
3274 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3275 goto error;
3276
3277 {
3278 const char * const strs[2] = { "QUIT\r\n" };
3279 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3280 goto error;
3281 }
3282
3283 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "221 "))
3284 goto error;
3285
3286 if (!check->task) {
3287 struct task *t;
3288
3289 if ((t = task_new()) == NULL)
3290 goto error;
3291
3292 check->task = t;
3293 t->process = process_email_alert;
3294 t->context = check;
3295
3296 /* check this in one ms */
3297 t->expire = tick_add(now_ms, MS_TO_TICKS(1));
3298 check->start = now;
3299 task_queue(t);
3300 }
3301
3302 LIST_ADDQ(&q->email_alerts, &alert->list);
3303
3304 return 1;
3305
3306error:
3307 email_alert_free(alert);
3308 return 0;
3309}
3310
3311static void enqueue_email_alert(struct proxy *p, const char *msg)
3312{
3313 int i;
3314 struct mailer *mailer;
3315
3316 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3317 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
3318 if (!enqueue_one_email_alert(&p->email_alert.queues[i], msg)) {
3319 Alert("Email alert [%s] could not be enqueued: out of memory\n", p->id);
3320 return;
3321 }
3322 }
3323
3324 return;
3325}
3326
3327/*
3328 * Send email alert if configured.
3329 */
Simon Horman64e34162015-02-06 11:11:57 +09003330void send_email_alert(struct server *s, int level, const char *format, ...)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003331{
3332 va_list argp;
3333 char buf[1024];
3334 int len;
3335 struct proxy *p = s->proxy;
3336
Simon Horman64e34162015-02-06 11:11:57 +09003337 if (!p->email_alert.mailers.m || level > p->email_alert.level ||
3338 format == NULL || !init_email_alert_checks(s))
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003339 return;
3340
3341 va_start(argp, format);
3342 len = vsnprintf(buf, sizeof(buf), format, argp);
3343 va_end(argp);
3344
Thierry FOURNIER62c8a212017-02-09 12:19:27 +01003345 if (len < 0 || len >= sizeof(buf)) {
Cyril Bontéb65e0332015-12-04 03:07:08 +01003346 Alert("Email alert [%s] could not format message\n", p->id);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003347 return;
3348 }
3349
3350 enqueue_email_alert(p, buf);
3351}
3352
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003353/*
3354 * Return value:
3355 * the port to be used for the health check
3356 * 0 in case no port could be found for the check
3357 */
3358int srv_check_healthcheck_port(struct check *chk)
3359{
3360 int i = 0;
3361 struct server *srv = NULL;
3362
3363 srv = chk->server;
3364
3365 /* If neither a port nor an addr was specified and no check transport
3366 * layer is forced, then the transport layer used by the checks is the
3367 * same as for the production traffic. Otherwise we use raw_sock by
3368 * default, unless one is specified.
3369 */
3370 if (!chk->port && !is_addr(&chk->addr)) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003371 chk->use_ssl |= (srv->use_ssl || (srv->proxy->options & PR_O_TCPCHK_SSL));
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003372 chk->send_proxy |= (srv->pp_opts);
3373 }
3374
3375 /* by default, we use the health check port ocnfigured */
3376 if (chk->port > 0)
3377 return chk->port;
3378
3379 /* try to get the port from check_core.addr if check.port not set */
3380 i = get_host_port(&chk->addr);
3381 if (i > 0)
3382 return i;
3383
3384 /* try to get the port from server address */
3385 /* prevent MAPPORTS from working at this point, since checks could
3386 * not be performed in such case (MAPPORTS impose a relative ports
3387 * based on live traffic)
3388 */
3389 if (srv->flags & SRV_F_MAPPORTS)
3390 return 0;
Willy Tarreau04276f32017-01-06 17:41:29 +01003391
3392 i = srv->svc_port; /* by default */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003393 if (i > 0)
3394 return i;
3395
3396 return 0;
3397}
3398
Willy Tarreau865c5142016-12-21 20:04:48 +01003399__attribute__((constructor))
3400static void __check_init(void)
3401{
3402 hap_register_post_check(start_checks);
3403}
3404
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003405
Willy Tarreaubd741542010-03-16 18:46:54 +01003406/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003407 * Local variables:
3408 * c-indent-level: 8
3409 * c-basic-offset: 8
3410 * End:
3411 */