blob: 8eb2c7a45face48dbe0b629522100267af7df8a6 [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
Baptiste Assmann69e273f2013-12-11 00:52:19 +010044#ifdef USE_OPENSSL
45#include <types/ssl_sock.h>
46#include <proto/ssl_sock.h>
47#endif /* USE_OPENSSL */
48
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020050#include <proto/checks.h>
William Lallemand9ed62032016-11-21 17:49:11 +010051#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/fd.h>
53#include <proto/log.h>
54#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020055#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010056#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010057#include <proto/proto_tcp.h>
Baptiste Assmann69e273f2013-12-11 00:52:19 +010058#include <proto/protocol.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010059#include <proto/proxy.h>
Willy Tarreaufb56aab2012-09-28 14:40:02 +020060#include <proto/raw_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/server.h>
Willy Tarreau48d6bf22016-06-21 16:27:34 +020062#include <proto/signal.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010063#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020065#include <proto/log.h>
66#include <proto/dns.h>
67#include <proto/proto_udp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreaubd741542010-03-16 18:46:54 +010069static int httpchk_expect(struct server *s, int done);
Simon Hormane16c1b32015-01-30 11:22:57 +090070static int tcpcheck_get_step_id(struct check *);
Baptiste Assmann22b09d22015-05-01 08:03:04 +020071static char * tcpcheck_get_step_comment(struct check *, int);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +020072static void tcpcheck_main(struct connection *);
Willy Tarreaubd741542010-03-16 18:46:54 +010073
Simon Horman63a4a822012-03-19 07:24:41 +090074static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010075 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
76 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020077 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020078
Willy Tarreau23964182014-05-20 20:56:30 +020079 /* Below we have finished checks */
80 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010081 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010082
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010083 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020084
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010085 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
86 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
87 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020088
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010089 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
90 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
91 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020092
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010093 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
94 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020095
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020096 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020097
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010098 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
99 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
100 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +0900101
102 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
103 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +0200104 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200105};
106
Cyril Bontéac92a062014-12-27 22:28:38 +0100107const struct extcheck_env extcheck_envs[EXTCHK_SIZE] = {
108 [EXTCHK_PATH] = { "PATH", EXTCHK_SIZE_EVAL_INIT },
109 [EXTCHK_HAPROXY_PROXY_NAME] = { "HAPROXY_PROXY_NAME", EXTCHK_SIZE_EVAL_INIT },
110 [EXTCHK_HAPROXY_PROXY_ID] = { "HAPROXY_PROXY_ID", EXTCHK_SIZE_EVAL_INIT },
111 [EXTCHK_HAPROXY_PROXY_ADDR] = { "HAPROXY_PROXY_ADDR", EXTCHK_SIZE_EVAL_INIT },
112 [EXTCHK_HAPROXY_PROXY_PORT] = { "HAPROXY_PROXY_PORT", EXTCHK_SIZE_EVAL_INIT },
113 [EXTCHK_HAPROXY_SERVER_NAME] = { "HAPROXY_SERVER_NAME", EXTCHK_SIZE_EVAL_INIT },
114 [EXTCHK_HAPROXY_SERVER_ID] = { "HAPROXY_SERVER_ID", EXTCHK_SIZE_EVAL_INIT },
115 [EXTCHK_HAPROXY_SERVER_ADDR] = { "HAPROXY_SERVER_ADDR", EXTCHK_SIZE_EVAL_INIT },
116 [EXTCHK_HAPROXY_SERVER_PORT] = { "HAPROXY_SERVER_PORT", EXTCHK_SIZE_EVAL_INIT },
117 [EXTCHK_HAPROXY_SERVER_MAXCONN] = { "HAPROXY_SERVER_MAXCONN", EXTCHK_SIZE_EVAL_INIT },
118 [EXTCHK_HAPROXY_SERVER_CURCONN] = { "HAPROXY_SERVER_CURCONN", EXTCHK_SIZE_ULONG },
119};
120
Simon Horman63a4a822012-03-19 07:24:41 +0900121static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100122 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
123
124 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
125 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
126
127 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
128 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
129 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
130 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
131
132 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
133 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
134 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
135};
136
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200137/*
138 * Convert check_status code to description
139 */
140const char *get_check_status_description(short check_status) {
141
142 const char *desc;
143
144 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200145 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200146 else
147 desc = NULL;
148
149 if (desc && *desc)
150 return desc;
151 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200152 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200153}
154
155/*
156 * Convert check_status code to short info
157 */
158const char *get_check_status_info(short check_status) {
159
160 const char *info;
161
162 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200163 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200164 else
165 info = NULL;
166
167 if (info && *info)
168 return info;
169 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200170 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200171}
172
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100173const char *get_analyze_status(short analyze_status) {
174
175 const char *desc;
176
177 if (analyze_status < HANA_STATUS_SIZE)
178 desc = analyze_statuses[analyze_status].desc;
179 else
180 desc = NULL;
181
182 if (desc && *desc)
183 return desc;
184 else
185 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
186}
187
Willy Tarreaua150cf12014-05-20 21:57:23 +0200188/* Builds a string containing some information about the health check's result.
189 * The output string is allocated from the trash chunks. If the check is NULL,
190 * NULL is returned. This is designed to be used when emitting logs about health
191 * checks.
Willy Tarreauddd329c2014-05-16 16:46:12 +0200192 */
Willy Tarreaua150cf12014-05-20 21:57:23 +0200193static const char *check_reason_string(struct check *check)
Willy Tarreauddd329c2014-05-16 16:46:12 +0200194{
Willy Tarreaua150cf12014-05-20 21:57:23 +0200195 struct chunk *msg;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200196
Willy Tarreaua150cf12014-05-20 21:57:23 +0200197 if (!check)
198 return NULL;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200199
Willy Tarreaua150cf12014-05-20 21:57:23 +0200200 msg = get_trash_chunk();
201 chunk_printf(msg, "reason: %s", get_check_status_description(check->status));
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200202
Willy Tarreaua150cf12014-05-20 21:57:23 +0200203 if (check->status >= HCHK_STATUS_L57DATA)
204 chunk_appendf(msg, ", code: %d", check->code);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200205
Willy Tarreaua150cf12014-05-20 21:57:23 +0200206 if (*check->desc) {
207 struct chunk src;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200208
Willy Tarreaua150cf12014-05-20 21:57:23 +0200209 chunk_appendf(msg, ", info: \"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200210
Willy Tarreaua150cf12014-05-20 21:57:23 +0200211 chunk_initlen(&src, check->desc, 0, strlen(check->desc));
212 chunk_asciiencode(msg, &src, '"');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200213
Willy Tarreaua150cf12014-05-20 21:57:23 +0200214 chunk_appendf(msg, "\"");
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200215 }
216
Willy Tarreaua150cf12014-05-20 21:57:23 +0200217 if (check->duration >= 0)
218 chunk_appendf(msg, ", check duration: %ldms", check->duration);
219
220 return msg->str;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200221}
222
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200223/*
Simon Horman4a741432013-02-23 15:35:38 +0900224 * Set check->status, update check->duration and fill check->result with
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200225 * an adequate CHK_RES_* value. The new check->health is computed based
226 * on the result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200227 *
228 * Show information in logs about failed health check if server is UP
229 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200230 */
Simon Horman4a741432013-02-23 15:35:38 +0900231static void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100232{
Simon Horman4a741432013-02-23 15:35:38 +0900233 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200234 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200235 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900236
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200237 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100238 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900239 check->desc[0] = '\0';
240 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200241 return;
242 }
243
Simon Horman4a741432013-02-23 15:35:38 +0900244 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200245 return;
246
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200247 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900248 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
249 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200250 } else
Simon Horman4a741432013-02-23 15:35:38 +0900251 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200252
Simon Horman4a741432013-02-23 15:35:38 +0900253 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200254 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900255 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200256
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100257 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900258 check->duration = -1;
259 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200260 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900261 check->duration = tv_ms_elapsed(&check->start, &now);
262 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200263 }
264
Willy Tarreau23964182014-05-20 20:56:30 +0200265 /* no change is expected if no state change occurred */
266 if (check->result == CHK_RES_NEUTRAL)
267 return;
268
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200269 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200270
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200271 switch (check->result) {
272 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200273 /* Failure to connect to the agent as a secondary check should not
274 * cause the server to be marked down.
275 */
276 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900277 (check->status >= HCHK_STATUS_L57DATA)) &&
Willy Tarreau12634e12014-05-23 11:32:36 +0200278 (check->health >= check->rise)) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200279 s->counters.failed_checks++;
280 report = 1;
281 check->health--;
282 if (check->health < check->rise)
283 check->health = 0;
284 }
285 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200286
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200287 case CHK_RES_PASSED:
288 case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */
289 if ((check->health < check->rise + check->fall - 1) &&
290 (check->result == CHK_RES_PASSED || check->health > 0)) {
291 report = 1;
292 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200293
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200294 if (check->health >= check->rise)
295 check->health = check->rise + check->fall - 1; /* OK now */
296 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200297
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200298 /* clear consecutive_errors if observing is enabled */
299 if (s->onerror)
300 s->consecutive_errors = 0;
301 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100302
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200303 default:
304 break;
305 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200306
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200307 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
308 (status != prev_status || report)) {
309 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200310 "%s check for %sserver %s/%s %s%s",
311 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200312 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100313 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100314 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200315 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200316
Willy Tarreaua150cf12014-05-20 21:57:23 +0200317 srv_append_status(&trash, s, check_reason_string(check), -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200318
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100319 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200320 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
321 (check->health >= check->rise) ? check->fall : check->rise,
322 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200323
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100324 Warning("%s.\n", trash.str);
325 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman7ea9be02015-04-30 13:10:33 +0900326 send_email_alert(s, LOG_INFO, "%s", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200327 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200328}
329
Willy Tarreau4eec5472014-05-20 22:32:27 +0200330/* Marks the check <check>'s server down if the current check is already failed
331 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332 */
Willy Tarreau4eec5472014-05-20 22:32:27 +0200333static void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334{
Simon Horman4a741432013-02-23 15:35:38 +0900335 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900336
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200337 /* The agent secondary check should only cause a server to be marked
338 * as down if check->status is HCHK_STATUS_L7STS, which indicates
339 * that the agent returned "fail", "stopped" or "down".
340 * The implication here is that failure to connect to the agent
341 * as a secondary check should not cause the server to be marked
342 * down. */
343 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
344 return;
345
Willy Tarreau4eec5472014-05-20 22:32:27 +0200346 if (check->health > 0)
347 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100348
Willy Tarreau4eec5472014-05-20 22:32:27 +0200349 /* We only report a reason for the check if we did not do so previously */
350 srv_set_stopped(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200351}
352
Willy Tarreauaf549582014-05-16 17:37:50 +0200353/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200354 * it isn't in maintenance, it is not tracking a down server and other checks
355 * comply. The rule is simple : by default, a server is up, unless any of the
356 * following conditions is true :
357 * - health check failed (check->health < rise)
358 * - agent check failed (agent->health < rise)
359 * - the server tracks a down server (track && track->state == STOPPED)
360 * Note that if the server has a slowstart, it will switch to STARTING instead
361 * of RUNNING. Also, only the health checks support the nolb mode, so the
362 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200363 */
Willy Tarreau3e048382014-05-21 10:30:54 +0200364static void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200365{
Simon Horman4a741432013-02-23 15:35:38 +0900366 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100367
Willy Tarreauaf549582014-05-16 17:37:50 +0200368 if (s->admin & SRV_ADMF_MAINT)
369 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100370
Willy Tarreau3e048382014-05-21 10:30:54 +0200371 if (s->track && s->track->state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200372 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100373
Willy Tarreau3e048382014-05-21 10:30:54 +0200374 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
375 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100376
Willy Tarreau3e048382014-05-21 10:30:54 +0200377 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
378 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200379
Willy Tarreau3e048382014-05-21 10:30:54 +0200380 if ((check->state & CHK_ST_AGENT) && s->state == SRV_ST_STOPPING)
381 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100382
Willy Tarreau3e048382014-05-21 10:30:54 +0200383 srv_set_running(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100384}
385
Willy Tarreaudb58b792014-05-21 13:57:23 +0200386/* Marks the check <check> as valid and tries to set its server into stopping mode
387 * if it was running or starting, and provided it isn't in maintenance and other
388 * checks comply. The conditions for the server to be marked in stopping mode are
389 * the same as for it to be turned up. Also, only the health checks support the
390 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200391 */
Willy Tarreaudb58b792014-05-21 13:57:23 +0200392static void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200393{
Simon Horman4a741432013-02-23 15:35:38 +0900394 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100395
Willy Tarreauaf549582014-05-16 17:37:50 +0200396 if (s->admin & SRV_ADMF_MAINT)
397 return;
398
Willy Tarreaudb58b792014-05-21 13:57:23 +0200399 if (check->state & CHK_ST_AGENT)
400 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100401
Willy Tarreaudb58b792014-05-21 13:57:23 +0200402 if (s->track && s->track->state == SRV_ST_STOPPED)
403 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100404
Willy Tarreaudb58b792014-05-21 13:57:23 +0200405 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
406 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100407
Willy Tarreaudb58b792014-05-21 13:57:23 +0200408 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
409 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100410
Willy Tarreaudb58b792014-05-21 13:57:23 +0200411 srv_set_stopping(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100412}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100414/* note: use health_adjust() only, which first checks that the observe mode is
415 * enabled.
416 */
417void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100418{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100419 int failed;
420 int expire;
421
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100422 if (s->observe >= HANA_OBS_SIZE)
423 return;
424
Willy Tarreaubb956662013-01-24 00:37:39 +0100425 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100426 return;
427
428 switch (analyze_statuses[status].lr[s->observe - 1]) {
429 case 1:
430 failed = 1;
431 break;
432
433 case 2:
434 failed = 0;
435 break;
436
437 default:
438 return;
439 }
440
441 if (!failed) {
442 /* good: clear consecutive_errors */
443 s->consecutive_errors = 0;
444 return;
445 }
446
447 s->consecutive_errors++;
448
449 if (s->consecutive_errors < s->consecutive_errors_limit)
450 return;
451
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100452 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
453 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100454
455 switch (s->onerror) {
456 case HANA_ONERR_FASTINTER:
457 /* force fastinter - nothing to do here as all modes force it */
458 break;
459
460 case HANA_ONERR_SUDDTH:
461 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900462 if (s->check.health > s->check.rise)
463 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100464
465 /* no break - fall through */
466
467 case HANA_ONERR_FAILCHK:
468 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900469 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200470 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100471 break;
472
473 case HANA_ONERR_MARKDWN:
474 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900475 s->check.health = s->check.rise;
Simon Horman4a741432013-02-23 15:35:38 +0900476 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200477 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100478 break;
479
480 default:
481 /* write a warning? */
482 break;
483 }
484
485 s->consecutive_errors = 0;
486 s->counters.failed_hana++;
487
Simon Horman66183002013-02-23 10:16:43 +0900488 if (s->check.fastinter) {
489 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300490 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200491 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300492 /* requeue check task with new expire */
493 task_queue(s->check.task);
494 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100495 }
496}
497
Willy Tarreaua1dab552014-04-14 15:04:54 +0200498static int httpchk_build_status_header(struct server *s, char *buffer, int size)
Willy Tarreauef781042010-01-27 11:53:01 +0100499{
500 int sv_state;
501 int ratio;
502 int hlen = 0;
Joseph Lynch514061c2015-01-15 17:52:59 -0800503 char addr[46];
504 char port[6];
Willy Tarreauef781042010-01-27 11:53:01 +0100505 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
506 "UP %d/%d", "UP",
507 "NOLB %d/%d", "NOLB",
508 "no check" };
509
510 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
511 hlen += 24;
512
Willy Tarreauff5ae352013-12-11 20:36:34 +0100513 if (!(s->check.state & CHK_ST_ENABLED))
514 sv_state = 6;
Willy Tarreau892337c2014-05-13 23:41:20 +0200515 else if (s->state != SRV_ST_STOPPED) {
Simon Horman58c32972013-11-25 10:46:38 +0900516 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100517 sv_state = 3; /* UP */
518 else
519 sv_state = 2; /* going down */
520
Willy Tarreau892337c2014-05-13 23:41:20 +0200521 if (s->state == SRV_ST_STOPPING)
Willy Tarreauef781042010-01-27 11:53:01 +0100522 sv_state += 2;
523 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900524 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100525 sv_state = 1; /* going up */
526 else
527 sv_state = 0; /* DOWN */
528 }
529
Willy Tarreaua1dab552014-04-14 15:04:54 +0200530 hlen += snprintf(buffer + hlen, size - hlen,
Willy Tarreauef781042010-01-27 11:53:01 +0100531 srv_hlt_st[sv_state],
Willy Tarreau892337c2014-05-13 23:41:20 +0200532 (s->state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
533 (s->state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100534
Joseph Lynch514061c2015-01-15 17:52:59 -0800535 addr_to_str(&s->addr, addr, sizeof(addr));
536 port_to_str(&s->addr, port, sizeof(port));
537
538 hlen += snprintf(buffer + hlen, size - hlen, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
539 addr, port, s->proxy->id, s->id,
Willy Tarreauef781042010-01-27 11:53:01 +0100540 global.node,
541 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
542 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
543 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
544 s->nbpend);
545
Willy Tarreau892337c2014-05-13 23:41:20 +0200546 if ((s->state == SRV_ST_STARTING) &&
Willy Tarreauef781042010-01-27 11:53:01 +0100547 now.tv_sec < s->last_change + s->slowstart &&
548 now.tv_sec >= s->last_change) {
549 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
Willy Tarreaua1dab552014-04-14 15:04:54 +0200550 hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio);
Willy Tarreauef781042010-01-27 11:53:01 +0100551 }
552
553 buffer[hlen++] = '\r';
554 buffer[hlen++] = '\n';
555
556 return hlen;
557}
558
Willy Tarreau20a18342013-12-05 00:31:46 +0100559/* Check the connection. If an error has already been reported or the socket is
560 * closed, keep errno intact as it is supposed to contain the valid error code.
561 * If no error is reported, check the socket's error queue using getsockopt().
562 * Warning, this must be done only once when returning from poll, and never
563 * after an I/O error was attempted, otherwise the error queue might contain
564 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
565 * socket. Returns non-zero if an error was reported, zero if everything is
566 * clean (including a properly closed socket).
567 */
568static int retrieve_errno_from_socket(struct connection *conn)
569{
570 int skerr;
571 socklen_t lskerr = sizeof(skerr);
572
573 if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl))
574 return 1;
575
Willy Tarreau3c728722014-01-23 13:50:42 +0100576 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100577 return 0;
578
579 if (getsockopt(conn->t.sock.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
580 errno = skerr;
581
582 if (errno == EAGAIN)
583 errno = 0;
584
585 if (!errno) {
586 /* we could not retrieve an error, that does not mean there is
587 * none. Just don't change anything and only report the prior
588 * error if any.
589 */
590 if (conn->flags & CO_FL_ERROR)
591 return 1;
592 else
593 return 0;
594 }
595
596 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
597 return 1;
598}
599
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100600/* Try to collect as much information as possible on the connection status,
601 * and adjust the server status accordingly. It may make use of <errno_bck>
602 * if non-null when the caller is absolutely certain of its validity (eg:
603 * checked just after a syscall). If the caller doesn't have a valid errno,
604 * it can pass zero, and retrieve_errno_from_socket() will be called to try
605 * to extract errno from the socket. If no error is reported, it will consider
606 * the <expired> flag. This is intended to be used when a connection error was
607 * reported in conn->flags or when a timeout was reported in <expired>. The
608 * function takes care of not updating a server status which was already set.
609 * All situations where at least one of <expired> or CO_FL_ERROR are set
610 * produce a status.
611 */
612static void chk_report_conn_err(struct connection *conn, int errno_bck, int expired)
613{
614 struct check *check = conn->owner;
615 const char *err_msg;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200616 struct chunk *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200617 int step;
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200618 char *comment;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100619
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100620 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100621 return;
622
623 errno = errno_bck;
624 if (!errno || errno == EAGAIN)
625 retrieve_errno_from_socket(conn);
626
627 if (!(conn->flags & CO_FL_ERROR) && !expired)
628 return;
629
630 /* we'll try to build a meaningful error message depending on the
631 * context of the error possibly present in conn->err_code, and the
632 * socket error possibly collected above. This is useful to know the
633 * exact step of the L6 layer (eg: SSL handshake).
634 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200635 chk = get_trash_chunk();
636
637 if (check->type == PR_O2_TCPCHK_CHK) {
Simon Hormane16c1b32015-01-30 11:22:57 +0900638 step = tcpcheck_get_step_id(check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200639 if (!step)
640 chunk_printf(chk, " at initial connection step of tcp-check");
641 else {
642 chunk_printf(chk, " at step %d of tcp-check", step);
643 /* we were looking for a string */
644 if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_CONNECT) {
645 if (check->last_started_step->port)
646 chunk_appendf(chk, " (connect port %d)" ,check->last_started_step->port);
647 else
648 chunk_appendf(chk, " (connect)");
649 }
650 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_EXPECT) {
651 if (check->last_started_step->string)
Baptiste Assmann96a5c9b2015-05-01 08:09:29 +0200652 chunk_appendf(chk, " (expect string '%s')", check->last_started_step->string);
Willy Tarreau213c6782014-10-02 14:51:02 +0200653 else if (check->last_started_step->expect_regex)
654 chunk_appendf(chk, " (expect regex)");
655 }
656 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_SEND) {
657 chunk_appendf(chk, " (send)");
658 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200659
660 comment = tcpcheck_get_step_comment(check, step);
661 if (comment)
662 chunk_appendf(chk, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200663 }
664 }
665
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100666 if (conn->err_code) {
667 if (errno && errno != EAGAIN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200668 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100669 else
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200670 chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100671 err_msg = trash.str;
672 }
673 else {
674 if (errno && errno != EAGAIN) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200675 chunk_printf(&trash, "%s%s", strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100676 err_msg = trash.str;
677 }
678 else {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200679 err_msg = chk->str;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100680 }
681 }
682
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200683 if (check->state & CHK_ST_PORT_MISS) {
684 /* NOTE: this is reported after <fall> tries */
685 chunk_printf(chk, "No port available for the TCP connection");
686 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
687 }
688
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100689 if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
690 /* L4 not established (yet) */
691 if (conn->flags & CO_FL_ERROR)
692 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
693 else if (expired)
694 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200695
696 /*
697 * might be due to a server IP change.
698 * Let's trigger a DNS resolution if none are currently running.
699 */
700 if ((check->server->resolution) && (check->server->resolution->step == RSLV_STEP_NONE))
701 trigger_resolution(check->server);
702
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100703 }
704 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
705 /* L6 not established (yet) */
706 if (conn->flags & CO_FL_ERROR)
707 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
708 else if (expired)
709 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
710 }
711 else if (conn->flags & CO_FL_ERROR) {
712 /* I/O error after connection was established and before we could diagnose */
713 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
714 }
715 else if (expired) {
716 /* connection established but expired check */
717 if (check->type == PR_O2_SSL3_CHK)
718 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
719 else /* HTTP, SMTP, ... */
720 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
721 }
722
723 return;
724}
725
Willy Tarreaubaaee002006-06-26 02:48:02 +0200726/*
727 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200728 * the connection acknowledgement. If the proxy requires L7 health-checks,
729 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900730 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200731 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200732static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200733{
Simon Horman4a741432013-02-23 15:35:38 +0900734 struct check *check = conn->owner;
735 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900736 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200737
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100738 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100739 goto out_wakeup;
740
Willy Tarreau310987a2014-01-22 19:46:33 +0100741 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100742 return;
743
Willy Tarreau20a18342013-12-05 00:31:46 +0100744 if (retrieve_errno_from_socket(conn)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100745 chk_report_conn_err(conn, errno, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100746 __conn_data_stop_both(conn);
747 goto out_wakeup;
748 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100749
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100750 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100751 /* if the output is closed, we can't do anything */
752 conn->flags |= CO_FL_ERROR;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100753 chk_report_conn_err(conn, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100754 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200755 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200756
Willy Tarreau06559ac2013-12-05 01:53:08 +0100757 /* here, we know that the connection is established. That's enough for
758 * a pure TCP check.
759 */
760 if (!check->type)
761 goto out_wakeup;
762
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200763 if (check->type == PR_O2_TCPCHK_CHK) {
764 tcpcheck_main(conn);
765 return;
766 }
767
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100768 if (check->bo->o) {
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100769 conn->xprt->snd_buf(conn, check->bo, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100770 if (conn->flags & CO_FL_ERROR) {
771 chk_report_conn_err(conn, errno, 0);
772 __conn_data_stop_both(conn);
773 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200774 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100775 if (check->bo->o)
776 return;
777 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200778
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100779 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
780 if (s->proxy->timeout.check) {
781 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
782 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200783 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100784 goto out_nowake;
785
Willy Tarreau83749182007-04-15 20:56:27 +0200786 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200787 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200788 out_nowake:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200789 __conn_data_stop_send(conn); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200790}
791
Willy Tarreaubaaee002006-06-26 02:48:02 +0200792/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200793 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200794 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900795 * set_server_check_status() to update check->status, check->duration
796 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200797
798 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
799 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
800 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
801 * response to an SSL HELLO (the principle is that this is enough to
802 * distinguish between an SSL server and a pure TCP relay). All other cases will
803 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
804 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200806static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200807{
Simon Horman4a741432013-02-23 15:35:38 +0900808 struct check *check = conn->owner;
809 struct server *s = check->server;
810 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200811 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100812 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200813 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200814
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100815 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +0200816 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200817
Willy Tarreau310987a2014-01-22 19:46:33 +0100818 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200819 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200820
821 if (check->type == PR_O2_TCPCHK_CHK) {
822 tcpcheck_main(conn);
823 return;
824 }
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200825
Willy Tarreau83749182007-04-15 20:56:27 +0200826 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
827 * but the connection was closed on the remote end. Fortunately, recv still
828 * works correctly and we don't need to do the getsockopt() on linux.
829 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000830
831 /* Set buffer to point to the end of the data already read, and check
832 * that there is free space remaining. If the buffer is full, proceed
833 * with running the checks without attempting another socket read.
834 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000835
Willy Tarreau03938182010-03-17 21:52:07 +0100836 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +0000837
Simon Horman4a741432013-02-23 15:35:38 +0900838 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreauf1503172012-09-28 19:39:36 +0200839 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +0100840 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +0900841 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +0200842 /* Report network errors only if we got no other data. Otherwise
843 * we'll let the upper layers decide whether the response is OK
844 * or not. It is very common that an RST sent by the server is
845 * reported as an error just after the last data chunk.
846 */
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100847 chk_report_conn_err(conn, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +0100848 goto out_wakeup;
849 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200850 }
851
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100852
Willy Tarreau03938182010-03-17 21:52:07 +0100853 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +0900854 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +0100855 */
Simon Horman4a741432013-02-23 15:35:38 +0900856 if (check->bi->i < check->bi->size)
857 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100858 else {
Simon Horman4a741432013-02-23 15:35:38 +0900859 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100860 done = 1; /* buffer full, don't wait for more data */
861 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200862
Nick Chalk57b1bf72010-03-16 15:50:46 +0000863 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +0900864 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +0200865 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900866 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100867 goto wait_more_data;
868
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100869 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +0900870 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
871 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
872 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
873 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
874 !isdigit((unsigned char) *(check->bi->data + 11))) {
875 cut_crlf(check->bi->data);
876 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200877
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100878 goto out_wakeup;
879 }
880
Simon Horman4a741432013-02-23 15:35:38 +0900881 check->code = str2uic(check->bi->data + 9);
882 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +0000883
Willy Tarreaubd741542010-03-16 18:46:54 +0100884 if ((s->proxy->options & PR_O_DISABLE404) &&
Willy Tarreau892337c2014-05-13 23:41:20 +0200885 (s->state != SRV_ST_STOPPED) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000886 /* 404 may be accepted as "stopping" only if the server was up */
887 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900888 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000889 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100890 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
891 /* Run content verification check... We know we have at least 13 chars */
892 if (!httpchk_expect(s, done))
893 goto wait_more_data;
894 }
895 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +0900896 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +0100897 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900898 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +0100899 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000900 else {
901 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900902 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000903 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200904 break;
905
906 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900907 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +0100908 goto wait_more_data;
909
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100910 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +0900911 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
912 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200913 else
Simon Horman4a741432013-02-23 15:35:38 +0900914 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200915 break;
916
917 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900918 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100919 goto wait_more_data;
920
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200921 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +0900922 if ((check->bi->i < strlen("000\r")) ||
923 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
924 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
925 !isdigit((unsigned char) *(check->bi->data + 2))) {
926 cut_crlf(check->bi->data);
927 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200928
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200929 goto out_wakeup;
930 }
931
Simon Horman4a741432013-02-23 15:35:38 +0900932 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200933
Simon Horman4a741432013-02-23 15:35:38 +0900934 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200935 cut_crlf(desc);
936
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100937 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +0900938 if (*check->bi->data == '2')
939 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200940 else
Simon Horman4a741432013-02-23 15:35:38 +0900941 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200942 break;
943
Simon Hormana2b9dad2013-02-12 10:45:54 +0900944 case PR_O2_LB_AGENT_CHK: {
Willy Tarreau81f5d942013-12-09 20:51:51 +0100945 int status = HCHK_STATUS_CHECKED;
946 const char *hs = NULL; /* health status */
947 const char *as = NULL; /* admin status */
948 const char *ps = NULL; /* performance status */
Nenad Merdanovic174dd372016-04-24 23:10:06 +0200949 const char *cs = NULL; /* maxconn */
Willy Tarreau81f5d942013-12-09 20:51:51 +0100950 const char *err = NULL; /* first error to report */
951 const char *wrn = NULL; /* first warning to report */
952 char *cmd, *p;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900953
Willy Tarreau81f5d942013-12-09 20:51:51 +0100954 /* We're getting an agent check response. The agent could
955 * have been disabled in the mean time with a long check
956 * still pending. It is important that we ignore the whole
957 * response.
958 */
959 if (!(check->server->agent.state & CHK_ST_ENABLED))
960 break;
961
962 /* The agent supports strings made of a single line ended by the
963 * first CR ('\r') or LF ('\n'). This line is composed of words
964 * delimited by spaces (' '), tabs ('\t'), or commas (','). The
965 * line may optionally contained a description of a state change
966 * after a sharp ('#'), which is only considered if a health state
967 * is announced.
968 *
969 * Words may be composed of :
970 * - a numeric weight suffixed by the percent character ('%').
971 * - a health status among "up", "down", "stopped", and "fail".
972 * - an admin status among "ready", "drain", "maint".
973 *
974 * These words may appear in any order. If multiple words of the
975 * same category appear, the last one wins.
976 */
977
Willy Tarreau9809b782013-12-11 21:40:11 +0100978 p = check->bi->data;
979 while (*p && *p != '\n' && *p != '\r')
980 p++;
981
982 if (!*p) {
983 if (!done)
984 goto wait_more_data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900985
Willy Tarreau9809b782013-12-11 21:40:11 +0100986 /* at least inform the admin that the agent is mis-behaving */
987 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
988 break;
989 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100990
Willy Tarreau9809b782013-12-11 21:40:11 +0100991 *p = 0;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100992 cmd = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900993
Willy Tarreau81f5d942013-12-09 20:51:51 +0100994 while (*cmd) {
995 /* look for next word */
996 if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') {
997 cmd++;
998 continue;
999 }
Simon Horman671b6f02013-11-25 10:46:39 +09001000
Willy Tarreau81f5d942013-12-09 20:51:51 +01001001 if (*cmd == '#') {
1002 /* this is the beginning of a health status description,
1003 * skip the sharp and blanks.
1004 */
1005 cmd++;
1006 while (*cmd == '\t' || *cmd == ' ')
1007 cmd++;
Simon Horman671b6f02013-11-25 10:46:39 +09001008 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001009 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001010
1011 /* find the end of the word so that we have a null-terminated
1012 * word between <cmd> and <p>.
1013 */
1014 p = cmd + 1;
1015 while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',')
1016 p++;
1017 if (*p)
1018 *p++ = 0;
1019
1020 /* first, health statuses */
1021 if (strcasecmp(cmd, "up") == 0) {
1022 check->health = check->rise + check->fall - 1;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001023 status = HCHK_STATUS_L7OKD;
Willy Tarreau81f5d942013-12-09 20:51:51 +01001024 hs = cmd;
1025 }
1026 else if (strcasecmp(cmd, "down") == 0) {
1027 check->health = 0;
1028 status = HCHK_STATUS_L7STS;
1029 hs = cmd;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001030 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001031 else if (strcasecmp(cmd, "stopped") == 0) {
1032 check->health = 0;
1033 status = HCHK_STATUS_L7STS;
1034 hs = cmd;
1035 }
1036 else if (strcasecmp(cmd, "fail") == 0) {
1037 check->health = 0;
1038 status = HCHK_STATUS_L7STS;
1039 hs = cmd;
1040 }
1041 /* admin statuses */
1042 else if (strcasecmp(cmd, "ready") == 0) {
1043 as = cmd;
1044 }
1045 else if (strcasecmp(cmd, "drain") == 0) {
1046 as = cmd;
1047 }
1048 else if (strcasecmp(cmd, "maint") == 0) {
1049 as = cmd;
1050 }
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001051 /* try to parse a weight here and keep the last one */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001052 else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) {
1053 ps = cmd;
1054 }
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001055 /* try to parse a maxconn here */
1056 else if (strncasecmp(cmd, "maxconn:", strlen("maxconn:")) == 0) {
1057 cs = cmd;
1058 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001059 else {
1060 /* keep a copy of the first error */
1061 if (!err)
1062 err = cmd;
1063 }
1064 /* skip to next word */
1065 cmd = p;
1066 }
1067 /* here, cmd points either to \0 or to the beginning of a
1068 * description. Skip possible leading spaces.
1069 */
1070 while (*cmd == ' ' || *cmd == '\n')
1071 cmd++;
1072
1073 /* First, update the admin status so that we avoid sending other
1074 * possibly useless warnings and can also update the health if
1075 * present after going back up.
1076 */
1077 if (as) {
1078 if (strcasecmp(as, "drain") == 0)
1079 srv_adm_set_drain(check->server);
1080 else if (strcasecmp(as, "maint") == 0)
1081 srv_adm_set_maint(check->server);
1082 else
1083 srv_adm_set_ready(check->server);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001084 }
1085
Willy Tarreau81f5d942013-12-09 20:51:51 +01001086 /* now change weights */
1087 if (ps) {
1088 const char *msg;
1089
1090 msg = server_parse_weight_change_request(s, ps);
1091 if (!wrn || !*wrn)
1092 wrn = msg;
1093 }
1094
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001095 if (cs) {
1096 const char *msg;
1097
1098 cs += strlen("maxconn:");
1099
1100 msg = server_parse_maxconn_change_request(s, cs);
1101 if (!wrn || !*wrn)
1102 wrn = msg;
1103 }
1104
Willy Tarreau81f5d942013-12-09 20:51:51 +01001105 /* and finally health status */
1106 if (hs) {
1107 /* We'll report some of the warnings and errors we have
1108 * here. Down reports are critical, we leave them untouched.
1109 * Lack of report, or report of 'UP' leaves the room for
1110 * ERR first, then WARN.
Simon Hormana2b9dad2013-02-12 10:45:54 +09001111 */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001112 const char *msg = cmd;
1113 struct chunk *t;
1114
1115 if (!*msg || status == HCHK_STATUS_L7OKD) {
1116 if (err && *err)
1117 msg = err;
1118 else if (wrn && *wrn)
1119 msg = wrn;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001120 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001121
1122 t = get_trash_chunk();
1123 chunk_printf(t, "via agent : %s%s%s%s",
1124 hs, *msg ? " (" : "",
1125 msg, *msg ? ")" : "");
1126
1127 set_server_check_status(check, status, t->str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001128 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001129 else if (err && *err) {
1130 /* No status change but we'd like to report something odd.
1131 * Just report the current state and copy the message.
1132 */
1133 chunk_printf(&trash, "agent reports an error : %s", err);
1134 set_server_check_status(check, status/*check->status*/, trash.str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001135
Willy Tarreau81f5d942013-12-09 20:51:51 +01001136 }
1137 else if (wrn && *wrn) {
1138 /* No status change but we'd like to report something odd.
1139 * Just report the current state and copy the message.
1140 */
1141 chunk_printf(&trash, "agent warns : %s", wrn);
1142 set_server_check_status(check, status/*check->status*/, trash.str);
1143 }
1144 else
1145 set_server_check_status(check, status, NULL);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001146 break;
1147 }
1148
Willy Tarreau1620ec32011-08-06 17:05:02 +02001149 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001150 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001151 goto wait_more_data;
1152
Simon Horman4a741432013-02-23 15:35:38 +09001153 if (check->bi->data[0] == 'R') {
1154 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001155 }
1156 else {
Simon Horman4a741432013-02-23 15:35:38 +09001157 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1158 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001159 else
1160 desc = "PostgreSQL unknown error";
1161
Simon Horman4a741432013-02-23 15:35:38 +09001162 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001163 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001164 break;
1165
1166 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001167 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001168 goto wait_more_data;
1169
Simon Horman4a741432013-02-23 15:35:38 +09001170 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1171 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001172 }
1173 else {
Simon Horman4a741432013-02-23 15:35:38 +09001174 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001175 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001176 break;
1177
1178 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001179 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001180 goto wait_more_data;
1181
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001182 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001183 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001184 /* We set the MySQL Version in description for information purpose
1185 * FIXME : it can be cool to use MySQL Version for other purpose,
1186 * like mark as down old MySQL server.
1187 */
Simon Horman4a741432013-02-23 15:35:38 +09001188 if (check->bi->i > 51) {
1189 desc = ltrim(check->bi->data + 5, ' ');
1190 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001191 }
1192 else {
1193 if (!done)
1194 goto wait_more_data;
1195 /* it seems we have a OK packet but without a valid length,
1196 * it must be a protocol error
1197 */
Simon Horman4a741432013-02-23 15:35:38 +09001198 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001199 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001200 }
1201 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001202 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001203 desc = ltrim(check->bi->data + 7, ' ');
1204 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001205 }
1206 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001207 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1208 (((unsigned int) *(check->bi->data + 1)) << 8) +
1209 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001210
Simon Horman4a741432013-02-23 15:35:38 +09001211 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001212 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001213 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001214 /* We have only one MySQL packet and it is a Handshake Initialization packet
1215 * but we need to have a second packet to know if it is alright
1216 */
Simon Horman4a741432013-02-23 15:35:38 +09001217 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001218 goto wait_more_data;
1219 }
1220 else {
1221 /* We have only one packet and it is an Error packet,
1222 * an error message is attached, so we can display it
1223 */
Simon Horman4a741432013-02-23 15:35:38 +09001224 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001225 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001226 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001227 }
Simon Horman4a741432013-02-23 15:35:38 +09001228 } else if (check->bi->i > first_packet_len + 4) {
1229 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1230 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1231 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001232
Simon Horman4a741432013-02-23 15:35:38 +09001233 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001234 /* We have 2 packets and that's good */
1235 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001236 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001237 /* No error packet */
1238 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001239 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001240 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001241 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001242 }
1243 else {
1244 /* An error message is attached in the Error packet
1245 * so we can display it ! :)
1246 */
Simon Horman4a741432013-02-23 15:35:38 +09001247 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001248 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001249 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001250 }
1251 }
1252 }
1253 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001254 if (!done)
1255 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001256 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001257 * it must be a protocol error
1258 */
Simon Horman4a741432013-02-23 15:35:38 +09001259 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001260 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001261 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001262 }
1263 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001264 break;
1265
1266 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001267 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001268 goto wait_more_data;
1269
1270 /* Check if the server speaks LDAP (ASN.1/BER)
1271 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1272 * http://tools.ietf.org/html/rfc4511
1273 */
1274
1275 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1276 * LDAPMessage: 0x30: SEQUENCE
1277 */
Simon Horman4a741432013-02-23 15:35:38 +09001278 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1279 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001280 }
1281 else {
1282 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001283 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001284
1285 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1286 * messageID: 0x02 0x01 0x01: INTEGER 1
1287 * protocolOp: 0x61: bindResponse
1288 */
1289 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001290 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1291 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001292
1293 goto out_wakeup;
1294 }
1295
1296 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001297 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001298
1299 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1300 * ldapResult: 0x0a 0x01: ENUMERATION
1301 */
1302 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001303 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1304 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001305
1306 goto out_wakeup;
1307 }
1308
1309 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1310 * resultCode
1311 */
Simon Horman4a741432013-02-23 15:35:38 +09001312 check->code = *(check->bi->data + msglen + 9);
1313 if (check->code) {
1314 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 +02001315 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001316 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001317 }
1318 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001319 break;
1320
Christopher Fauletba7bc162016-11-07 21:07:38 +01001321 case PR_O2_SPOP_CHK: {
1322 unsigned int framesz;
1323 char err[HCHK_DESC_LEN];
1324
1325 if (!done && check->bi->i < 4)
1326 goto wait_more_data;
1327
1328 memcpy(&framesz, check->bi->data, 4);
1329 framesz = ntohl(framesz);
1330
1331 if (!done && check->bi->i < (4+framesz))
1332 goto wait_more_data;
1333
1334 if (!handle_spoe_healthcheck_response(check->bi->data+4, framesz, err, HCHK_DESC_LEN-1))
1335 set_server_check_status(check, HCHK_STATUS_L7OKD, "SPOA server is ok");
1336 else
1337 set_server_check_status(check, HCHK_STATUS_L7STS, err);
1338 break;
1339 }
1340
Willy Tarreau1620ec32011-08-06 17:05:02 +02001341 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001342 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001343 break;
1344 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001345
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001346 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001347 /* collect possible new errors */
1348 if (conn->flags & CO_FL_ERROR)
1349 chk_report_conn_err(conn, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001350
Nick Chalk57b1bf72010-03-16 15:50:46 +00001351 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001352 *check->bi->data = '\0';
1353 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001354
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001355 /* Close the connection... We absolutely want to perform a hard close
1356 * and reset the connection if some data are pending, otherwise we end
1357 * up with many TIME_WAITs and eat all the source port range quickly.
1358 * To avoid sending RSTs all the time, we first try to drain pending
1359 * data.
1360 */
Willy Tarreaub4017d02015-03-12 23:11:26 +01001361 __conn_data_stop_both(conn);
1362 conn_data_shutw_hard(conn);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001363
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001364 /* OK, let's not stay here forever */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001365 if (check->result == CHK_RES_FAILED)
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001366 conn->flags |= CO_FL_ERROR;
1367
Willy Tarreaufdccded2008-08-29 18:19:04 +02001368 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001369 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001370
1371 wait_more_data:
Willy Tarreauf817e9f2014-01-10 16:58:45 +01001372 __conn_data_want_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001373}
1374
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001375/*
1376 * This function is used only for server health-checks. It handles connection
1377 * status updates including errors. If necessary, it wakes the check task up.
1378 * It always returns 0.
1379 */
1380static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001381{
Simon Horman4a741432013-02-23 15:35:38 +09001382 struct check *check = conn->owner;
Willy Tarreau20bea422012-07-06 12:00:49 +02001383
Willy Tarreau6c560da2012-11-24 11:14:45 +01001384 if (unlikely(conn->flags & CO_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001385 /* We may get error reports bypassing the I/O handlers, typically
1386 * the case when sending a pure TCP check which fails, then the I/O
1387 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001388 * main processing task so let's simply wake it up. If we get here,
1389 * we expect errno to still be valid.
1390 */
1391 chk_report_conn_err(conn, errno, 0);
1392
Willy Tarreau2d351b62013-12-05 02:36:25 +01001393 __conn_data_stop_both(conn);
1394 task_wakeup(check->task, TASK_WOKEN_IO);
1395 }
Willy Tarreau3be293f2014-02-05 18:31:24 +01001396 else if (!(conn->flags & (CO_FL_DATA_RD_ENA|CO_FL_DATA_WR_ENA|CO_FL_HANDSHAKE))) {
1397 /* we may get here if only a connection probe was required : we
1398 * don't have any data to send nor anything expected in response,
1399 * so the completion of the connection establishment is enough.
1400 */
1401 task_wakeup(check->task, TASK_WOKEN_IO);
1402 }
Willy Tarreau2d351b62013-12-05 02:36:25 +01001403
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001404 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001405 /* We're here because nobody wants to handle the error, so we
1406 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001407 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01001408 conn_sock_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02001409 conn_force_close(conn);
Willy Tarreau2d351b62013-12-05 02:36:25 +01001410 }
Willy Tarreau3267d362012-08-17 23:53:56 +02001411 return 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001412}
1413
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001414struct data_cb check_conn_cb = {
1415 .recv = event_srv_chk_r,
1416 .send = event_srv_chk_w,
1417 .wake = wake_srv_chk,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +01001418 .name = "CHCK",
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001419};
1420
Willy Tarreaubaaee002006-06-26 02:48:02 +02001421/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001422 * updates the server's weight during a warmup stage. Once the final weight is
1423 * reached, the task automatically stops. Note that any server status change
1424 * must have updated s->last_change accordingly.
1425 */
1426static struct task *server_warmup(struct task *t)
1427{
1428 struct server *s = t->context;
1429
1430 /* by default, plan on stopping the task */
1431 t->expire = TICK_ETERNITY;
Willy Tarreau20125212014-05-13 19:44:56 +02001432 if ((s->admin & SRV_ADMF_MAINT) ||
Willy Tarreau892337c2014-05-13 23:41:20 +02001433 (s->state != SRV_ST_STARTING))
Willy Tarreau2e993902011-10-31 11:53:20 +01001434 return t;
1435
Willy Tarreau892337c2014-05-13 23:41:20 +02001436 /* recalculate the weights and update the state */
Willy Tarreau004e0452013-11-21 11:22:01 +01001437 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001438
1439 /* probably that we can refill this server with a bit more connections */
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001440 pendconn_grab_from_px(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001441
1442 /* get back there in 1 second or 1/20th of the slowstart interval,
1443 * whichever is greater, resulting in small 5% steps.
1444 */
Willy Tarreau892337c2014-05-13 23:41:20 +02001445 if (s->state == SRV_ST_STARTING)
Willy Tarreau2e993902011-10-31 11:53:20 +01001446 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1447 return t;
1448}
1449
1450/*
Simon Horman98637e52014-06-20 12:30:16 +09001451 * establish a server health-check that makes use of a connection.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001452 *
1453 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001454 * - SF_ERR_NONE if everything's OK and tcpcheck_main() was not called
1455 * - SF_ERR_UP if if everything's OK and tcpcheck_main() was called
1456 * - SF_ERR_SRVTO if there are no more servers
1457 * - SF_ERR_SRVCL if the connection was refused by the server
1458 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1459 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1460 * - SF_ERR_INTERNAL for any other purely internal errors
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001461 * - 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 +01001462 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001463 * Note that we try to prevent the network stack from sending the ACK during the
1464 * connect() when a pure TCP check is used (without PROXY protocol).
1465 */
Simon Horman98637e52014-06-20 12:30:16 +09001466static int connect_conn_chk(struct task *t)
Simon Hormanb00d17a2014-06-13 16:18:16 +09001467{
1468 struct check *check = t->context;
1469 struct server *s = check->server;
1470 struct connection *conn = check->conn;
1471 struct protocol *proto;
1472 int ret;
Willy Tarreauf3d34822014-12-08 12:11:28 +01001473 int quickack;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001474
1475 /* tcpcheck send/expect initialisation */
1476 if (check->type == PR_O2_TCPCHK_CHK)
1477 check->current_step = NULL;
1478
1479 /* prepare the check buffer.
1480 * This should not be used if check is the secondary agent check
1481 * of a server as s->proxy->check_req will relate to the
1482 * configuration of the primary check. Similarly, tcp-check uses
1483 * its own strings.
1484 */
1485 if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) {
1486 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
1487
1488 /* we want to check if this host replies to HTTP or SSLv3 requests
1489 * so we'll send the request, and won't wake the checker up now.
1490 */
1491 if ((check->type) == PR_O2_SSL3_CHK) {
1492 /* SSL requires that we put Unix time in the request */
1493 int gmt_time = htonl(date.tv_sec);
1494 memcpy(check->bo->data + 11, &gmt_time, 4);
1495 }
1496 else if ((check->type) == PR_O2_HTTP_CHK) {
1497 if (s->proxy->options2 & PR_O2_CHK_SNDST)
1498 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size));
Cyril Bonté32602d22015-01-30 00:07:07 +01001499 /* prevent HTTP keep-alive when "http-check expect" is used */
1500 if (s->proxy->options2 & PR_O2_EXP_TYPE)
1501 bo_putstr(check->bo, "Connection: close\r\n");
Simon Hormanb00d17a2014-06-13 16:18:16 +09001502 bo_putstr(check->bo, "\r\n");
1503 *check->bo->p = '\0'; /* to make gdb output easier to read */
1504 }
1505 }
1506
James Brown55f9ff12015-10-21 18:19:05 -07001507 if ((check->type & PR_O2_LB_AGENT_CHK) && check->send_string_len) {
1508 bo_putblk(check->bo, check->send_string, check->send_string_len);
1509 }
1510
Simon Hormanb00d17a2014-06-13 16:18:16 +09001511 /* prepare a new connection */
1512 conn_init(conn);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001513
Simon Horman41f58762015-01-30 11:22:56 +09001514 if (is_addr(&check->addr)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001515 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09001516 conn->addr.to = check->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001517 }
1518 else {
1519 /* we'll connect to the addr on the server */
1520 conn->addr.to = s->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001521 }
1522
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001523 if ((conn->addr.to.ss_family == AF_INET) || (conn->addr.to.ss_family == AF_INET6)) {
1524 int i = 0;
1525
1526 i = srv_check_healthcheck_port(check);
1527 if (i == 0) {
1528 conn->owner = check;
1529 return SF_ERR_CHK_PORT;
1530 }
1531
1532 set_host_port(&conn->addr.to, i);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001533 }
1534
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01001535 proto = protocol_by_family(conn->addr.to.ss_family);
1536
1537 conn_prepare(conn, proto, check->xprt);
1538 conn_attach(conn, check, &check_conn_cb);
1539 conn->target = &s->obj_type;
1540
1541 /* no client address */
1542 clear_addr(&conn->addr.from);
1543
Willy Tarreauf3d34822014-12-08 12:11:28 +01001544 /* only plain tcp-check supports quick ACK */
1545 quickack = check->type == 0 || check->type == PR_O2_TCPCHK_CHK;
1546
Simon Hormane16c1b32015-01-30 11:22:57 +09001547 if (check->type == PR_O2_TCPCHK_CHK && !LIST_ISEMPTY(check->tcpcheck_rules)) {
Willy Tarreau5581c272015-05-13 12:24:53 +02001548 struct tcpcheck_rule *r;
1549
1550 r = LIST_NEXT(check->tcpcheck_rules, struct tcpcheck_rule *, list);
1551
Simon Hormanb00d17a2014-06-13 16:18:16 +09001552 /* if first step is a 'connect', then tcpcheck_main must run it */
1553 if (r->action == TCPCHK_ACT_CONNECT) {
1554 tcpcheck_main(conn);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001555 return SF_ERR_UP;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001556 }
Willy Tarreauf3d34822014-12-08 12:11:28 +01001557 if (r->action == TCPCHK_ACT_EXPECT)
1558 quickack = 0;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001559 }
1560
Willy Tarreaue7dff022015-04-03 01:14:29 +02001561 ret = SF_ERR_INTERNAL;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001562 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01001563 ret = proto->connect(conn, check->type, quickack ? 2 : 0);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001564 conn->flags |= CO_FL_WAKE_DATA;
1565 if (s->check.send_proxy) {
1566 conn->send_proxy_ofs = 1;
1567 conn->flags |= CO_FL_SEND_PROXY;
1568 }
1569
1570 return ret;
1571}
1572
Simon Horman98637e52014-06-20 12:30:16 +09001573static struct list pid_list = LIST_HEAD_INIT(pid_list);
1574static struct pool_head *pool2_pid_list;
1575
1576void block_sigchld(void)
1577{
1578 sigset_t set;
1579 sigemptyset(&set);
1580 sigaddset(&set, SIGCHLD);
Willy Tarreauebc92442016-06-21 17:29:46 +02001581 assert(sigprocmask(SIG_BLOCK, &set, NULL) == 0);
Simon Horman98637e52014-06-20 12:30:16 +09001582}
1583
1584void unblock_sigchld(void)
1585{
1586 sigset_t set;
1587 sigemptyset(&set);
Willy Tarreauebc92442016-06-21 17:29:46 +02001588 sigaddset(&set, SIGCHLD);
1589 assert(sigprocmask(SIG_UNBLOCK, &set, NULL) == 0);
Simon Horman98637e52014-06-20 12:30:16 +09001590}
1591
Simon Horman98637e52014-06-20 12:30:16 +09001592static struct pid_list *pid_list_add(pid_t pid, struct task *t)
1593{
1594 struct pid_list *elem;
1595 struct check *check = t->context;
1596
1597 elem = pool_alloc2(pool2_pid_list);
1598 if (!elem)
1599 return NULL;
1600 elem->pid = pid;
1601 elem->t = t;
1602 elem->exited = 0;
1603 check->curpid = elem;
1604 LIST_INIT(&elem->list);
1605 LIST_ADD(&pid_list, &elem->list);
1606 return elem;
1607}
1608
Simon Horman98637e52014-06-20 12:30:16 +09001609static void pid_list_del(struct pid_list *elem)
1610{
1611 struct check *check;
1612
1613 if (!elem)
1614 return;
1615
Simon Horman98637e52014-06-20 12:30:16 +09001616 LIST_DEL(&elem->list);
Simon Horman98637e52014-06-20 12:30:16 +09001617 if (!elem->exited)
1618 kill(elem->pid, SIGTERM);
1619
1620 check = elem->t->context;
1621 check->curpid = NULL;
1622 pool_free2(pool2_pid_list, elem);
1623}
1624
1625/* Called from inside SIGCHLD handler, SIGCHLD is blocked */
1626static void pid_list_expire(pid_t pid, int status)
1627{
1628 struct pid_list *elem;
1629
1630 list_for_each_entry(elem, &pid_list, list) {
1631 if (elem->pid == pid) {
1632 elem->t->expire = now_ms;
1633 elem->status = status;
1634 elem->exited = 1;
Cyril Bonté9dbcfab2014-08-07 01:55:39 +02001635 task_wakeup(elem->t, TASK_WOKEN_IO);
Simon Horman98637e52014-06-20 12:30:16 +09001636 return;
1637 }
1638 }
1639}
1640
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001641static void sigchld_handler(struct sig_handler *sh)
Simon Horman98637e52014-06-20 12:30:16 +09001642{
1643 pid_t pid;
1644 int status;
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001645
Simon Horman98637e52014-06-20 12:30:16 +09001646 while ((pid = waitpid(0, &status, WNOHANG)) > 0)
1647 pid_list_expire(pid, status);
1648}
1649
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001650static int init_pid_list(void)
1651{
Simon Horman98637e52014-06-20 12:30:16 +09001652 if (pool2_pid_list != NULL)
1653 /* Nothing to do */
1654 return 0;
1655
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001656 if (!signal_register_fct(SIGCHLD, sigchld_handler, SIGCHLD)) {
Simon Horman98637e52014-06-20 12:30:16 +09001657 Alert("Failed to set signal handler for external health checks: %s. Aborting.\n",
1658 strerror(errno));
1659 return 1;
1660 }
1661
1662 pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED);
1663 if (pool2_pid_list == NULL) {
1664 Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n",
1665 strerror(errno));
1666 return 1;
1667 }
1668
1669 return 0;
1670}
1671
Cyril Bontéac92a062014-12-27 22:28:38 +01001672/* helper macro to set an environment variable and jump to a specific label on failure. */
1673#define EXTCHK_SETENV(check, envidx, value, fail) { if (extchk_setenv(check, envidx, value)) goto fail; }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001674
1675/*
Cyril Bontéac92a062014-12-27 22:28:38 +01001676 * helper function to allocate enough memory to store an environment variable.
1677 * It will also check that the environment variable is updatable, and silently
1678 * fail if not.
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001679 */
Cyril Bontéac92a062014-12-27 22:28:38 +01001680static int extchk_setenv(struct check *check, int idx, const char *value)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001681{
1682 int len, ret;
Cyril Bontéac92a062014-12-27 22:28:38 +01001683 char *envname;
1684 int vmaxlen;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001685
Cyril Bontéac92a062014-12-27 22:28:38 +01001686 if (idx < 0 || idx >= EXTCHK_SIZE) {
1687 Alert("Illegal environment variable index %d. Aborting.\n", idx);
1688 return 1;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001689 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001690
1691 envname = extcheck_envs[idx].name;
1692 vmaxlen = extcheck_envs[idx].vmaxlen;
1693
1694 /* Check if the environment variable is already set, and silently reject
1695 * the update if this one is not updatable. */
1696 if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx]))
1697 return 0;
1698
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001699 /* Instead of sending NOT_USED, sending an empty value is preferable */
1700 if (strcmp(value, "NOT_USED") == 0) {
1701 value = "";
1702 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001703
1704 len = strlen(envname) + 1;
1705 if (vmaxlen == EXTCHK_SIZE_EVAL_INIT)
1706 len += strlen(value);
1707 else
1708 len += vmaxlen;
1709
1710 if (!check->envp[idx])
1711 check->envp[idx] = malloc(len + 1);
1712
1713 if (!check->envp[idx]) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001714 Alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname);
1715 return 1;
1716 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001717 ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001718 if (ret < 0) {
1719 Alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno));
1720 return 1;
1721 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001722 else if (ret > len) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001723 Alert("Environment variable '%s' was truncated. Aborting.\n", envname);
1724 return 1;
1725 }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001726 return 0;
1727}
Simon Horman98637e52014-06-20 12:30:16 +09001728
1729static int prepare_external_check(struct check *check)
1730{
1731 struct server *s = check->server;
1732 struct proxy *px = s->proxy;
1733 struct listener *listener = NULL, *l;
1734 int i;
Simon Horman98637e52014-06-20 12:30:16 +09001735 const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001736 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001737
1738 list_for_each_entry(l, &px->conf.listeners, by_fe)
1739 /* Use the first INET, INET6 or UNIX listener */
1740 if (l->addr.ss_family == AF_INET ||
1741 l->addr.ss_family == AF_INET6 ||
1742 l->addr.ss_family == AF_UNIX) {
1743 listener = l;
1744 break;
1745 }
1746
Simon Horman98637e52014-06-20 12:30:16 +09001747 check->curpid = NULL;
Cyril Bontéac92a062014-12-27 22:28:38 +01001748 check->envp = calloc((EXTCHK_SIZE + 1), sizeof(char *));
1749 if (!check->envp) {
1750 Alert("Failed to allocate memory for environment variables. Aborting\n");
1751 goto err;
1752 }
Simon Horman98637e52014-06-20 12:30:16 +09001753
Cyril Bontéac92a062014-12-27 22:28:38 +01001754 check->argv = calloc(6, sizeof(char *));
1755 if (!check->argv) {
1756 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001757 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001758 }
Simon Horman98637e52014-06-20 12:30:16 +09001759
1760 check->argv[0] = px->check_command;
1761
Cyril Bonté777be862014-12-02 21:21:35 +01001762 if (!listener) {
1763 check->argv[1] = strdup("NOT_USED");
1764 check->argv[2] = strdup("NOT_USED");
1765 }
1766 else if (listener->addr.ss_family == AF_INET ||
Simon Horman98637e52014-06-20 12:30:16 +09001767 listener->addr.ss_family == AF_INET6) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001768 addr_to_str(&listener->addr, buf, sizeof(buf));
1769 check->argv[1] = strdup(buf);
1770 port_to_str(&listener->addr, buf, sizeof(buf));
1771 check->argv[2] = strdup(buf);
Cyril Bonté777be862014-12-02 21:21:35 +01001772 }
1773 else if (listener->addr.ss_family == AF_UNIX) {
Simon Horman98637e52014-06-20 12:30:16 +09001774 const struct sockaddr_un *un;
1775
1776 un = (struct sockaddr_un *)&listener->addr;
1777 check->argv[1] = strdup(un->sun_path);
1778 check->argv[2] = strdup("NOT_USED");
Cyril Bonté777be862014-12-02 21:21:35 +01001779 }
1780 else {
Cyril Bontéac92a062014-12-27 22:28:38 +01001781 Alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001782 goto err;
1783 }
1784
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001785 addr_to_str(&s->addr, buf, sizeof(buf));
1786 check->argv[3] = strdup(buf);
1787 port_to_str(&s->addr, buf, sizeof(buf));
1788 check->argv[4] = strdup(buf);
Simon Horman98637e52014-06-20 12:30:16 +09001789
Cyril Bontéac92a062014-12-27 22:28:38 +01001790 for (i = 0; i < 5; i++) {
1791 if (!check->argv[i]) {
1792 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001793 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001794 }
1795 }
Simon Horman98637e52014-06-20 12:30:16 +09001796
Cyril Bontéac92a062014-12-27 22:28:38 +01001797 EXTCHK_SETENV(check, EXTCHK_PATH, path, err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001798 /* Add proxy environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001799 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err);
1800 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err);
1801 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err);
1802 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001803 /* Add server environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001804 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err);
1805 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err);
1806 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err);
1807 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err);
1808 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err);
1809 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), err);
1810
1811 /* Ensure that we don't leave any hole in check->envp */
1812 for (i = 0; i < EXTCHK_SIZE; i++)
1813 if (!check->envp[i])
1814 EXTCHK_SETENV(check, i, "", err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001815
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001816 return 1;
Simon Horman98637e52014-06-20 12:30:16 +09001817err:
1818 if (check->envp) {
Cyril Bontéac92a062014-12-27 22:28:38 +01001819 for (i = 0; i < EXTCHK_SIZE; i++)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001820 free(check->envp[i]);
Simon Horman98637e52014-06-20 12:30:16 +09001821 free(check->envp);
1822 check->envp = NULL;
1823 }
1824
1825 if (check->argv) {
1826 for (i = 1; i < 5; i++)
1827 free(check->argv[i]);
1828 free(check->argv);
1829 check->argv = NULL;
1830 }
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001831 return 0;
Simon Horman98637e52014-06-20 12:30:16 +09001832}
1833
Simon Hormanb00d17a2014-06-13 16:18:16 +09001834/*
Simon Horman98637e52014-06-20 12:30:16 +09001835 * establish a server health-check that makes use of a process.
1836 *
1837 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001838 * - SF_ERR_NONE if everything's OK
1839 * - SF_ERR_SRVTO if there are no more servers
1840 * - SF_ERR_SRVCL if the connection was refused by the server
1841 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1842 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1843 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +01001844 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Horman98637e52014-06-20 12:30:16 +09001845 *
1846 * Blocks and then unblocks SIGCHLD
1847 */
1848static int connect_proc_chk(struct task *t)
1849{
Cyril Bontéac92a062014-12-27 22:28:38 +01001850 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001851 struct check *check = t->context;
1852 struct server *s = check->server;
1853 struct proxy *px = s->proxy;
1854 int status;
1855 pid_t pid;
1856
Willy Tarreaue7dff022015-04-03 01:14:29 +02001857 status = SF_ERR_RESOURCE;
Simon Horman98637e52014-06-20 12:30:16 +09001858
1859 block_sigchld();
1860
1861 pid = fork();
1862 if (pid < 0) {
1863 Alert("Failed to fork process for external health check: %s. Aborting.\n",
1864 strerror(errno));
1865 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1866 goto out;
1867 }
1868 if (pid == 0) {
1869 /* Child */
1870 extern char **environ;
Willy Tarreaub7b24782016-06-21 15:32:29 +02001871 int fd;
1872
1873 /* close all FDs. Keep stdin/stdout/stderr in verbose mode */
1874 fd = (global.mode & (MODE_QUIET|MODE_VERBOSE)) == MODE_QUIET ? 0 : 3;
1875
1876 while (fd < global.rlimit_nofile)
1877 close(fd++);
1878
Simon Horman98637e52014-06-20 12:30:16 +09001879 environ = check->envp;
Cyril Bontéac92a062014-12-27 22:28:38 +01001880 extchk_setenv(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)));
Simon Horman98637e52014-06-20 12:30:16 +09001881 execvp(px->check_command, check->argv);
1882 Alert("Failed to exec process for external health check: %s. Aborting.\n",
1883 strerror(errno));
1884 exit(-1);
1885 }
1886
1887 /* Parent */
1888 if (check->result == CHK_RES_UNKNOWN) {
1889 if (pid_list_add(pid, t) != NULL) {
1890 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1891
1892 if (px->timeout.check && px->timeout.connect) {
1893 int t_con = tick_add(now_ms, px->timeout.connect);
1894 t->expire = tick_first(t->expire, t_con);
1895 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02001896 status = SF_ERR_NONE;
Simon Horman98637e52014-06-20 12:30:16 +09001897 goto out;
1898 }
1899 else {
1900 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1901 }
1902 kill(pid, SIGTERM); /* process creation error */
1903 }
1904 else
1905 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1906
1907out:
1908 unblock_sigchld();
1909 return status;
1910}
1911
1912/*
Simon Horman98637e52014-06-20 12:30:16 +09001913 * manages a server health-check that uses a process. Returns
Willy Tarreaubaaee002006-06-26 02:48:02 +02001914 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1915 */
Simon Horman98637e52014-06-20 12:30:16 +09001916static struct task *process_chk_proc(struct task *t)
1917{
1918 struct check *check = t->context;
1919 struct server *s = check->server;
1920 struct connection *conn = check->conn;
1921 int rv;
1922 int ret;
1923 int expired = tick_is_expired(t->expire, now_ms);
1924
1925 if (!(check->state & CHK_ST_INPROGRESS)) {
1926 /* no check currently running */
1927 if (!expired) /* woke up too early */
1928 return t;
1929
1930 /* we don't send any health-checks when the proxy is
1931 * stopped, the server should not be checked or the check
1932 * is disabled.
1933 */
1934 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
1935 s->proxy->state == PR_STSTOPPED)
1936 goto reschedule;
1937
1938 /* we'll initiate a new check */
1939 set_server_check_status(check, HCHK_STATUS_START, NULL);
1940
1941 check->state |= CHK_ST_INPROGRESS;
1942
Simon Hormandbf70192015-01-30 11:22:53 +09001943 ret = connect_proc_chk(t);
Simon Horman98637e52014-06-20 12:30:16 +09001944 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001945 case SF_ERR_UP:
Simon Horman98637e52014-06-20 12:30:16 +09001946 return t;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001947 case SF_ERR_NONE:
Simon Horman98637e52014-06-20 12:30:16 +09001948 /* we allow up to min(inter, timeout.connect) for a connection
1949 * to establish but only when timeout.check is set
1950 * as it may be to short for a full check otherwise
1951 */
1952 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1953
1954 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1955 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1956 t->expire = tick_first(t->expire, t_con);
1957 }
1958
1959 goto reschedule;
1960
Willy Tarreaue7dff022015-04-03 01:14:29 +02001961 case SF_ERR_SRVTO: /* ETIMEDOUT */
1962 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Simon Horman98637e52014-06-20 12:30:16 +09001963 conn->flags |= CO_FL_ERROR;
1964 chk_report_conn_err(conn, errno, 0);
1965 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001966 case SF_ERR_PRXCOND:
1967 case SF_ERR_RESOURCE:
1968 case SF_ERR_INTERNAL:
Simon Horman98637e52014-06-20 12:30:16 +09001969 conn->flags |= CO_FL_ERROR;
1970 chk_report_conn_err(conn, 0, 0);
1971 break;
1972 }
1973
1974 /* here, we have seen a synchronous error, no fd was allocated */
1975
1976 check->state &= ~CHK_ST_INPROGRESS;
1977 check_notify_failure(check);
1978
1979 /* we allow up to min(inter, timeout.connect) for a connection
1980 * to establish but only when timeout.check is set
1981 * as it may be to short for a full check otherwise
1982 */
1983 while (tick_is_expired(t->expire, now_ms)) {
1984 int t_con;
1985
1986 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1987 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1988
1989 if (s->proxy->timeout.check)
1990 t->expire = tick_first(t->expire, t_con);
1991 }
1992 }
1993 else {
1994 /* there was a test running.
1995 * First, let's check whether there was an uncaught error,
1996 * which can happen on connect timeout or error.
1997 */
1998 if (check->result == CHK_RES_UNKNOWN) {
1999 /* good connection is enough for pure TCP check */
2000 struct pid_list *elem = check->curpid;
2001 int status = HCHK_STATUS_UNKNOWN;
2002
2003 if (elem->exited) {
2004 status = elem->status; /* Save in case the process exits between use below */
2005 if (!WIFEXITED(status))
2006 check->code = -1;
2007 else
2008 check->code = WEXITSTATUS(status);
2009 if (!WIFEXITED(status) || WEXITSTATUS(status))
2010 status = HCHK_STATUS_PROCERR;
2011 else
2012 status = HCHK_STATUS_PROCOK;
2013 } else if (expired) {
2014 status = HCHK_STATUS_PROCTOUT;
Willy Tarreaudc3d1902014-07-08 00:56:27 +02002015 Warning("kill %d\n", (int)elem->pid);
Simon Horman98637e52014-06-20 12:30:16 +09002016 kill(elem->pid, SIGTERM);
2017 }
2018 set_server_check_status(check, status, NULL);
2019 }
2020
2021 if (check->result == CHK_RES_FAILED) {
2022 /* a failure or timeout detected */
2023 check_notify_failure(check);
2024 }
2025 else if (check->result == CHK_RES_CONDPASS) {
2026 /* check is OK but asks for stopping mode */
2027 check_notify_stopping(check);
2028 }
2029 else if (check->result == CHK_RES_PASSED) {
2030 /* a success was detected */
2031 check_notify_success(check);
2032 }
2033 check->state &= ~CHK_ST_INPROGRESS;
2034
2035 pid_list_del(check->curpid);
2036
2037 rv = 0;
2038 if (global.spread_checks > 0) {
2039 rv = srv_getinter(check) * global.spread_checks / 100;
2040 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
2041 }
2042 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
2043 }
2044
2045 reschedule:
2046 while (tick_is_expired(t->expire, now_ms))
2047 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2048 return t;
2049}
2050
2051/*
2052 * manages a server health-check that uses a connection. Returns
2053 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2054 */
2055static struct task *process_chk_conn(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002056{
Simon Horman4a741432013-02-23 15:35:38 +09002057 struct check *check = t->context;
2058 struct server *s = check->server;
2059 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002060 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002061 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002062 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002063
Willy Tarreau2c115e52013-12-11 19:41:16 +01002064 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01002065 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002066 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01002067 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002068
Simon Horman671b6f02013-11-25 10:46:39 +09002069 /* we don't send any health-checks when the proxy is
2070 * stopped, the server should not be checked or the check
2071 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002072 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01002073 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01002074 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01002075 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002076
2077 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09002078 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002079
Willy Tarreau2c115e52013-12-11 19:41:16 +01002080 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09002081 check->bi->p = check->bi->data;
2082 check->bi->i = 0;
2083 check->bo->p = check->bo->data;
2084 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002085
Simon Hormandbf70192015-01-30 11:22:53 +09002086 ret = connect_conn_chk(t);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002087 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002088 case SF_ERR_UP:
Simon Hormanb00d17a2014-06-13 16:18:16 +09002089 return t;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002090 case SF_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002091 /* we allow up to min(inter, timeout.connect) for a connection
2092 * to establish but only when timeout.check is set
2093 * as it may be to short for a full check otherwise
2094 */
Simon Horman4a741432013-02-23 15:35:38 +09002095 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002096
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002097 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2098 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2099 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002100 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01002101
2102 if (check->type)
2103 conn_data_want_recv(conn); /* prepare for reading a possible reply */
2104
Willy Tarreau5a78f362012-11-23 12:47:05 +01002105 goto reschedule;
2106
Willy Tarreaue7dff022015-04-03 01:14:29 +02002107 case SF_ERR_SRVTO: /* ETIMEDOUT */
2108 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau4bd07de2014-01-24 16:10:57 +01002109 conn->flags |= CO_FL_ERROR;
2110 chk_report_conn_err(conn, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002111 break;
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02002112 /* should share same code than cases below */
2113 case SF_ERR_CHK_PORT:
2114 check->state |= CHK_ST_PORT_MISS;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002115 case SF_ERR_PRXCOND:
2116 case SF_ERR_RESOURCE:
2117 case SF_ERR_INTERNAL:
Willy Tarreau4bd07de2014-01-24 16:10:57 +01002118 conn->flags |= CO_FL_ERROR;
2119 chk_report_conn_err(conn, 0, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002120 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002121 }
2122
Willy Tarreau5a78f362012-11-23 12:47:05 +01002123 /* here, we have seen a synchronous error, no fd was allocated */
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) {
2156 chk_report_conn_err(conn, 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 Tarreau5ba04f62013-02-12 15:23:12 +01002163 if (conn->xprt) {
2164 /* 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 Tarreauf79c8172013-10-21 16:30:56 +02002170 conn_force_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002171 }
2172
Willy Tarreauaf549582014-05-16 17:37:50 +02002173 if (check->result == CHK_RES_FAILED) {
2174 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002175 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002176 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002177 else if (check->result == CHK_RES_CONDPASS) {
2178 /* check is OK but asks for stopping mode */
2179 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002180 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002181 else if (check->result == CHK_RES_PASSED) {
2182 /* a success was detected */
2183 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002184 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01002185 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002186
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002187 rv = 0;
2188 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002189 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002190 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002191 }
Simon Horman4a741432013-02-23 15:35:38 +09002192 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002193 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002194
2195 reschedule:
2196 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002197 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002198 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01002199 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002200}
2201
Simon Horman98637e52014-06-20 12:30:16 +09002202/*
2203 * manages a server health-check. Returns
2204 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2205 */
2206static struct task *process_chk(struct task *t)
2207{
2208 struct check *check = t->context;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002209 struct server *s = check->server;
2210 struct dns_resolution *resolution = s->resolution;
2211
2212 /* trigger name resolution */
2213 if ((s->check.state & CHK_ST_ENABLED) && (resolution)) {
2214 /* check if a no resolution is running for this server */
2215 if (resolution->step == RSLV_STEP_NONE) {
2216 /*
2217 * if there has not been any name resolution for a longer period than
2218 * hold.valid, let's trigger a new one.
2219 */
Baptiste Assmannf0d93702015-09-03 10:59:39 +02002220 if (!resolution->last_resolution || tick_is_expired(tick_add(resolution->last_resolution, resolution->resolvers->hold.valid), now_ms)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002221 trigger_resolution(s);
2222 }
2223 }
2224 }
Simon Horman98637e52014-06-20 12:30:16 +09002225
2226 if (check->type == PR_O2_EXT_CHK)
2227 return process_chk_proc(t);
2228 return process_chk_conn(t);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002229
2230}
2231
2232/*
2233 * Initiates a new name resolution:
2234 * - generates a query id
2235 * - configure the resolution structure
2236 * - startup the resolvers task if required
2237 *
2238 * returns:
2239 * - 0 in case of error or if resolution already running
2240 * - 1 if everything started properly
2241 */
2242int trigger_resolution(struct server *s)
2243{
2244 struct dns_resolution *resolution;
2245 struct dns_resolvers *resolvers;
2246 int query_id;
2247 int i;
2248
2249 resolution = s->resolution;
2250 resolvers = resolution->resolvers;
2251
2252 /*
2253 * check if a resolution has already been started for this server
2254 * return directly to avoid resolution pill up
2255 */
2256 if (resolution->step != RSLV_STEP_NONE)
2257 return 0;
2258
2259 /* generates a query id */
2260 i = 0;
2261 do {
2262 query_id = dns_rnd16();
2263 /* we do try only 100 times to find a free query id */
2264 if (i++ > 100) {
2265 chunk_printf(&trash, "could not generate a query id for %s/%s, in resolvers %s",
2266 s->proxy->id, s->id, resolvers->id);
2267
2268 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2269 return 0;
2270 }
2271 } while (eb32_lookup(&resolvers->query_ids, query_id));
2272
2273 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
2274
2275 /* now update resolution parameters */
2276 resolution->query_id = query_id;
2277 resolution->qid.key = query_id;
2278 resolution->step = RSLV_STEP_RUNNING;
Thierry Fournierada34842016-02-17 21:25:09 +01002279 resolution->opts = &s->dns_opts;
2280 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00002281 resolution->query_type = DNS_RTYPE_A;
2282 } else {
2283 resolution->query_type = DNS_RTYPE_AAAA;
2284 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002285 resolution->try = resolvers->resolve_retries;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002286 resolution->try_cname = 0;
2287 resolution->nb_responses = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002288 eb32_insert(&resolvers->query_ids, &resolution->qid);
2289
2290 dns_send_query(resolution);
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002291 resolution->try -= 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002292
2293 /* update wakeup date if this resolution is the only one in the FIFO list */
2294 if (dns_check_resolution_queue(resolvers) == 1) {
2295 /* update task timeout */
2296 dns_update_resolvers_timeout(resolvers);
2297 task_queue(resolvers->t);
2298 }
2299
2300 return 1;
Simon Horman98637e52014-06-20 12:30:16 +09002301}
2302
Simon Horman5c942422013-11-25 10:46:32 +09002303static int start_check_task(struct check *check, int mininter,
2304 int nbcheck, int srvpos)
2305{
2306 struct task *t;
2307 /* task for the check */
2308 if ((t = task_new()) == NULL) {
2309 Alert("Starting [%s:%s] check: out of memory.\n",
2310 check->server->proxy->id, check->server->id);
2311 return 0;
2312 }
2313
2314 check->task = t;
2315 t->process = process_chk;
2316 t->context = check;
2317
Willy Tarreau1746eec2014-04-25 10:46:47 +02002318 if (mininter < srv_getinter(check))
2319 mininter = srv_getinter(check);
2320
2321 if (global.max_spread_checks && mininter > global.max_spread_checks)
2322 mininter = global.max_spread_checks;
2323
Simon Horman5c942422013-11-25 10:46:32 +09002324 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002325 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002326 check->start = now;
2327 task_queue(t);
2328
2329 return 1;
2330}
2331
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002332/*
2333 * Start health-check.
2334 * Returns 0 if OK, -1 if error, and prints the error in this case.
2335 */
2336int start_checks() {
2337
2338 struct proxy *px;
2339 struct server *s;
2340 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002341 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002342
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002343 /* 1- count the checkers to run simultaneously.
2344 * We also determine the minimum interval among all of those which
2345 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2346 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002347 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002348 * too short an interval for all others.
2349 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002350 for (px = proxy; px; px = px->next) {
2351 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002352 if (s->slowstart) {
2353 if ((t = task_new()) == NULL) {
2354 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
2355 return -1;
2356 }
2357 /* We need a warmup task that will be called when the server
2358 * state switches from down to up.
2359 */
2360 s->warmup = t;
2361 t->process = server_warmup;
2362 t->context = s;
2363 t->expire = TICK_ETERNITY;
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002364 /* server can be in this state only because of */
2365 if (s->state == SRV_ST_STARTING)
2366 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 +01002367 }
2368
Willy Tarreaud8514a22013-12-11 21:10:14 +01002369 if (s->check.state & CHK_ST_CONFIGURED) {
2370 nbcheck++;
2371 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2372 (!mininter || mininter > srv_getinter(&s->check)))
2373 mininter = srv_getinter(&s->check);
2374 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002375
Willy Tarreaud8514a22013-12-11 21:10:14 +01002376 if (s->agent.state & CHK_ST_CONFIGURED) {
2377 nbcheck++;
2378 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2379 (!mininter || mininter > srv_getinter(&s->agent)))
2380 mininter = srv_getinter(&s->agent);
2381 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002382 }
2383 }
2384
Simon Horman4a741432013-02-23 15:35:38 +09002385 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002386 return 0;
2387
2388 srand((unsigned)time(NULL));
2389
2390 /*
2391 * 2- start them as far as possible from each others. For this, we will
2392 * start them after their interval set to the min interval divided by
2393 * the number of servers, weighted by the server's position in the list.
2394 */
2395 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002396 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2397 if (init_pid_list()) {
2398 Alert("Starting [%s] check: out of memory.\n", px->id);
2399 return -1;
2400 }
2401 }
2402
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002403 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002404 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002405 if (s->check.state & CHK_ST_CONFIGURED) {
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002406 if (s->check.type == PR_O2_EXT_CHK) {
2407 if (!prepare_external_check(&s->check))
2408 return -1;
2409 }
Simon Hormand60d6912013-11-25 10:46:36 +09002410 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
2411 return -1;
2412 srvpos++;
2413 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002414
Simon Hormand60d6912013-11-25 10:46:36 +09002415 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002416 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002417 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
2418 return -1;
2419 }
2420 srvpos++;
2421 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002422 }
2423 }
2424 return 0;
2425}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002426
2427/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002428 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002429 * The buffer MUST be terminated by a null byte before calling this function.
2430 * Sets server status appropriately. The caller is responsible for ensuring
2431 * that the buffer contains at least 13 characters. If <done> is zero, we may
2432 * return 0 to indicate that data is required to decide of a match.
2433 */
2434static int httpchk_expect(struct server *s, int done)
2435{
2436 static char status_msg[] = "HTTP status check returned code <000>";
2437 char status_code[] = "000";
2438 char *contentptr;
2439 int crlf;
2440 int ret;
2441
2442 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2443 case PR_O2_EXP_STS:
2444 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002445 memcpy(status_code, s->check.bi->data + 9, 3);
2446 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002447
2448 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2449 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2450 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002451 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002452
2453 /* we necessarily have the response, so there are no partial failures */
2454 if (s->proxy->options2 & PR_O2_EXP_INV)
2455 ret = !ret;
2456
Simon Horman4a741432013-02-23 15:35:38 +09002457 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002458 break;
2459
2460 case PR_O2_EXP_STR:
2461 case PR_O2_EXP_RSTR:
2462 /* very simple response parser: ignore CR and only count consecutive LFs,
2463 * stop with contentptr pointing to first char after the double CRLF or
2464 * to '\0' if crlf < 2.
2465 */
2466 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002467 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002468 if (crlf >= 2)
2469 break;
2470 if (*contentptr == '\r')
2471 continue;
2472 else if (*contentptr == '\n')
2473 crlf++;
2474 else
2475 crlf = 0;
2476 }
2477
2478 /* Check that response contains a body... */
2479 if (crlf < 2) {
2480 if (!done)
2481 return 0;
2482
Simon Horman4a741432013-02-23 15:35:38 +09002483 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002484 "HTTP content check could not find a response body");
2485 return 1;
2486 }
2487
2488 /* Check that response body is not empty... */
2489 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002490 if (!done)
2491 return 0;
2492
Simon Horman4a741432013-02-23 15:35:38 +09002493 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002494 "HTTP content check found empty response body");
2495 return 1;
2496 }
2497
2498 /* Check the response content against the supplied string
2499 * or regex... */
2500 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2501 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2502 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002503 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002504
2505 /* if we don't match, we may need to wait more */
2506 if (!ret && !done)
2507 return 0;
2508
2509 if (ret) {
2510 /* content matched */
2511 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002512 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002513 "HTTP check matched unwanted content");
2514 else
Simon Horman4a741432013-02-23 15:35:38 +09002515 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002516 "HTTP content check matched");
2517 }
2518 else {
2519 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002520 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002521 "HTTP check did not match unwanted content");
2522 else
Simon Horman4a741432013-02-23 15:35:38 +09002523 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002524 "HTTP content check did not match");
2525 }
2526 break;
2527 }
2528 return 1;
2529}
2530
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002531/*
2532 * return the id of a step in a send/expect session
2533 */
Simon Hormane16c1b32015-01-30 11:22:57 +09002534static int tcpcheck_get_step_id(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002535{
2536 struct tcpcheck_rule *cur = NULL, *next = NULL;
2537 int i = 0;
2538
Willy Tarreau213c6782014-10-02 14:51:02 +02002539 /* not even started anything yet => step 0 = initial connect */
Baptiste Assmannf95bc8e2015-04-25 16:03:06 +02002540 if (!check->current_step)
Willy Tarreau213c6782014-10-02 14:51:02 +02002541 return 0;
2542
Simon Hormane16c1b32015-01-30 11:22:57 +09002543 cur = check->last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002544
2545 /* no step => first step */
2546 if (cur == NULL)
2547 return 1;
2548
2549 /* increment i until current step */
Simon Hormane16c1b32015-01-30 11:22:57 +09002550 list_for_each_entry(next, check->tcpcheck_rules, list) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002551 if (next->list.p == &cur->list)
2552 break;
2553 ++i;
2554 }
2555
2556 return i;
2557}
2558
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002559/*
2560 * return the latest known comment before (including) the given stepid
2561 * returns NULL if no comment found
2562 */
2563static char * tcpcheck_get_step_comment(struct check *check, int stepid)
2564{
2565 struct tcpcheck_rule *cur = NULL;
2566 char *ret = NULL;
2567 int i = 0;
2568
2569 /* not even started anything yet, return latest comment found before any action */
2570 if (!check->current_step) {
2571 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2572 if (cur->action == TCPCHK_ACT_COMMENT)
2573 ret = cur->comment;
2574 else
2575 goto return_comment;
2576 }
2577 }
2578
2579 i = 1;
2580 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2581 if (cur->comment)
2582 ret = cur->comment;
2583
2584 if (i >= stepid)
2585 goto return_comment;
2586
2587 ++i;
2588 }
2589
2590 return_comment:
2591 return ret;
2592}
2593
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002594static void tcpcheck_main(struct connection *conn)
2595{
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002596 char *contentptr, *comment;
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002597 struct tcpcheck_rule *next;
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002598 int done = 0, ret = 0, step = 0;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002599 struct check *check = conn->owner;
2600 struct server *s = check->server;
2601 struct task *t = check->task;
Simon Hormane16c1b32015-01-30 11:22:57 +09002602 struct list *head = check->tcpcheck_rules;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002603
Willy Tarreauef953952014-10-02 14:30:14 +02002604 /* here, we know that the check is complete or that it failed */
2605 if (check->result != CHK_RES_UNKNOWN)
2606 goto out_end_tcpcheck;
2607
2608 /* We have 4 possibilities here :
2609 * 1. we've not yet attempted step 1, and step 1 is a connect, so no
2610 * connection attempt was made yet ;
2611 * 2. we've not yet attempted step 1, and step 1 is a not connect or
2612 * does not exist (no rule), so a connection attempt was made
2613 * before coming here.
2614 * 3. we're coming back after having started with step 1, so we may
2615 * be waiting for a connection attempt to complete.
2616 * 4. the connection + handshake are complete
2617 *
2618 * #2 and #3 are quite similar, we want both the connection and the
2619 * handshake to complete before going any further. Thus we must always
2620 * wait for a connection to complete unless we're before and existing
2621 * step 1.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002622 */
Willy Tarreau449f9522015-05-13 15:39:48 +02002623
2624 /* find first rule and skip comments */
2625 next = LIST_NEXT(head, struct tcpcheck_rule *, list);
2626 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
2627 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
2628
Willy Tarreauef953952014-10-02 14:30:14 +02002629 if ((!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE)) &&
Willy Tarreau449f9522015-05-13 15:39:48 +02002630 (check->current_step || &next->list == head)) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002631 /* we allow up to min(inter, timeout.connect) for a connection
2632 * to establish but only when timeout.check is set
2633 * as it may be to short for a full check otherwise
2634 */
2635 while (tick_is_expired(t->expire, now_ms)) {
2636 int t_con;
2637
2638 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2639 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2640
2641 if (s->proxy->timeout.check)
2642 t->expire = tick_first(t->expire, t_con);
2643 }
2644 return;
2645 }
2646
Willy Tarreauef953952014-10-02 14:30:14 +02002647 /* special case: option tcp-check with no rule, a connect is enough */
Willy Tarreau449f9522015-05-13 15:39:48 +02002648 if (&next->list == head) {
Willy Tarreauef953952014-10-02 14:30:14 +02002649 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002650 goto out_end_tcpcheck;
Willy Tarreauef953952014-10-02 14:30:14 +02002651 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002652
Willy Tarreau213c6782014-10-02 14:51:02 +02002653 /* no step means first step initialisation */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002654 if (check->current_step == NULL) {
Willy Tarreau213c6782014-10-02 14:51:02 +02002655 check->last_started_step = NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002656 check->bo->p = check->bo->data;
2657 check->bo->o = 0;
2658 check->bi->p = check->bi->data;
2659 check->bi->i = 0;
Willy Tarreau449f9522015-05-13 15:39:48 +02002660 check->current_step = next;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002661 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2662 if (s->proxy->timeout.check)
2663 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2664 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002665
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002666 /* It's only the rules which will enable send/recv */
2667 __conn_data_stop_both(conn);
2668
Willy Tarreauabca5b62013-12-06 14:19:25 +01002669 while (1) {
Willy Tarreau263013d2015-05-13 11:59:14 +02002670 /* We have to try to flush the output buffer before reading, at
2671 * the end, or if we're about to send a string that does not fit
2672 * in the remaining space. That explains why we break out of the
2673 * loop after this control.
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002674 */
2675 if (check->bo->o &&
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002676 (&check->current_step->list == head ||
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002677 check->current_step->action != TCPCHK_ACT_SEND ||
2678 check->current_step->string_len >= buffer_total_space(check->bo))) {
2679
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002680 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002681 if (conn->flags & CO_FL_ERROR) {
2682 chk_report_conn_err(conn, errno, 0);
2683 __conn_data_stop_both(conn);
2684 goto out_end_tcpcheck;
2685 }
Willy Tarreau263013d2015-05-13 11:59:14 +02002686 break;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002687 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002688 }
2689
Willy Tarreau263013d2015-05-13 11:59:14 +02002690 if (&check->current_step->list == head)
Willy Tarreauabca5b62013-12-06 14:19:25 +01002691 break;
2692
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002693 /* have 'next' point to the next rule or NULL if we're on the
2694 * last one, connect() needs this.
2695 */
Willy Tarreau5581c272015-05-13 12:24:53 +02002696 next = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002697
2698 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002699 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002700 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002701
2702 /* NULL if we're on the last rule */
Willy Tarreauf3d34822014-12-08 12:11:28 +01002703 if (&next->list == head)
2704 next = NULL;
2705
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002706 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2707 struct protocol *proto;
2708 struct xprt_ops *xprt;
2709
2710 /* mark the step as started */
2711 check->last_started_step = check->current_step;
2712 /* first, shut existing connection */
2713 conn_force_close(conn);
2714
2715 /* prepare new connection */
2716 /* initialization */
2717 conn_init(conn);
2718 conn_attach(conn, check, &check_conn_cb);
2719 conn->target = &s->obj_type;
2720
2721 /* no client address */
2722 clear_addr(&conn->addr.from);
2723
Simon Horman41f58762015-01-30 11:22:56 +09002724 if (is_addr(&check->addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002725 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09002726 conn->addr.to = check->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002727 }
2728 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002729 /* we'll connect to the addr on the server */
2730 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002731 }
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01002732 proto = protocol_by_family(conn->addr.to.ss_family);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002733
2734 /* port */
2735 if (check->current_step->port)
2736 set_host_port(&conn->addr.to, check->current_step->port);
2737 else if (check->port)
2738 set_host_port(&conn->addr.to, check->port);
2739
2740#ifdef USE_OPENSSL
2741 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
2742 xprt = &ssl_sock;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002743 }
2744 else {
2745 xprt = &raw_sock;
2746 }
2747#else /* USE_OPENSSL */
2748 xprt = &raw_sock;
2749#endif /* USE_OPENSSL */
2750 conn_prepare(conn, proto, xprt);
2751
Willy Tarreaue7dff022015-04-03 01:14:29 +02002752 ret = SF_ERR_INTERNAL;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002753 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01002754 ret = proto->connect(conn,
2755 1 /* I/O polling is always needed */,
2756 (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002757 conn->flags |= CO_FL_WAKE_DATA;
2758 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2759 conn->send_proxy_ofs = 1;
2760 conn->flags |= CO_FL_SEND_PROXY;
2761 }
2762
2763 /* It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02002764 * - SF_ERR_NONE if everything's OK
2765 * - SF_ERR_SRVTO if there are no more servers
2766 * - SF_ERR_SRVCL if the connection was refused by the server
2767 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2768 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2769 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +01002770 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002771 * Note that we try to prevent the network stack from sending the ACK during the
2772 * connect() when a pure TCP check is used (without PROXY protocol).
2773 */
2774 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002775 case SF_ERR_NONE:
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002776 /* we allow up to min(inter, timeout.connect) for a connection
2777 * to establish but only when timeout.check is set
2778 * as it may be to short for a full check otherwise
2779 */
2780 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2781
2782 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2783 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2784 t->expire = tick_first(t->expire, t_con);
2785 }
2786 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002787 case SF_ERR_SRVTO: /* ETIMEDOUT */
2788 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002789 step = tcpcheck_get_step_id(check);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002790 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002791 step, strerror(errno));
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002792 comment = tcpcheck_get_step_comment(check, step);
2793 if (comment)
2794 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002795 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2796 goto out_end_tcpcheck;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002797 case SF_ERR_PRXCOND:
2798 case SF_ERR_RESOURCE:
2799 case SF_ERR_INTERNAL:
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002800 step = tcpcheck_get_step_id(check);
2801 chunk_printf(&trash, "TCPCHK error establishing connection at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002802 comment = tcpcheck_get_step_comment(check, step);
2803 if (comment)
2804 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002805 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2806 goto out_end_tcpcheck;
2807 }
2808
2809 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002810 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002811
2812 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002813 while (&check->current_step->list != head &&
2814 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002815 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002816
Willy Tarreauf2c87352015-05-13 12:08:21 +02002817 if (&check->current_step->list == head)
2818 break;
2819
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002820 /* don't do anything until the connection is established */
2821 if (!(conn->flags & CO_FL_CONNECTED)) {
2822 /* update expire time, should be done by process_chk */
2823 /* we allow up to min(inter, timeout.connect) for a connection
2824 * to establish but only when timeout.check is set
2825 * as it may be to short for a full check otherwise
2826 */
2827 while (tick_is_expired(t->expire, now_ms)) {
2828 int t_con;
2829
2830 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2831 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2832
2833 if (s->proxy->timeout.check)
2834 t->expire = tick_first(t->expire, t_con);
2835 }
2836 return;
2837 }
2838
2839 } /* end 'connect' */
2840 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2841 /* mark the step as started */
2842 check->last_started_step = check->current_step;
2843
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002844 /* reset the read buffer */
2845 if (*check->bi->data != '\0') {
2846 *check->bi->data = '\0';
2847 check->bi->i = 0;
2848 }
2849
2850 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
2851 conn->flags |= CO_FL_ERROR;
2852 chk_report_conn_err(conn, 0, 0);
2853 goto out_end_tcpcheck;
2854 }
2855
Willy Tarreauabca5b62013-12-06 14:19:25 +01002856 if (check->current_step->string_len >= check->bo->size) {
2857 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2858 check->current_step->string_len, check->bo->size,
Simon Hormane16c1b32015-01-30 11:22:57 +09002859 tcpcheck_get_step_id(check));
Willy Tarreauabca5b62013-12-06 14:19:25 +01002860 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2861 goto out_end_tcpcheck;
2862 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002863
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002864 /* do not try to send if there is no space */
2865 if (check->current_step->string_len >= buffer_total_space(check->bo))
2866 continue;
2867
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002868 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2869 *check->bo->p = '\0'; /* to make gdb output easier to read */
2870
Willy Tarreauabca5b62013-12-06 14:19:25 +01002871 /* go to next rule and try to send */
Willy Tarreau5581c272015-05-13 12:24:53 +02002872 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002873
2874 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002875 while (&check->current_step->list != head &&
2876 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002877 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Willy Tarreauf2c87352015-05-13 12:08:21 +02002878
2879 if (&check->current_step->list == head)
2880 break;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002881 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002882 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002883 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002884 goto out_end_tcpcheck;
2885
Willy Tarreau310987a2014-01-22 19:46:33 +01002886 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002887 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
2888 done = 1;
2889 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2890 /* Report network errors only if we got no other data. Otherwise
2891 * we'll let the upper layers decide whether the response is OK
2892 * or not. It is very common that an RST sent by the server is
2893 * reported as an error just after the last data chunk.
2894 */
2895 chk_report_conn_err(conn, errno, 0);
2896 goto out_end_tcpcheck;
2897 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002898 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002899 else
Willy Tarreau263013d2015-05-13 11:59:14 +02002900 break;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002901 }
2902
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002903 /* mark the step as started */
2904 check->last_started_step = check->current_step;
2905
2906
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002907 /* Intermediate or complete response received.
2908 * Terminate string in check->bi->data buffer.
2909 */
2910 if (check->bi->i < check->bi->size) {
2911 check->bi->data[check->bi->i] = '\0';
2912 }
2913 else {
2914 check->bi->data[check->bi->i - 1] = '\0';
2915 done = 1; /* buffer full, don't wait for more data */
2916 }
2917
2918 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002919
2920 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002921 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002922 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002923 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002924
2925 /* empty response */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002926 step = tcpcheck_get_step_id(check);
2927 chunk_printf(&trash, "TCPCHK got an empty response at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002928 comment = tcpcheck_get_step_comment(check, step);
2929 if (comment)
2930 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002931 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2932
2933 goto out_end_tcpcheck;
2934 }
2935
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002936 if (!done && (check->current_step->string != NULL) && (check->bi->i < check->current_step->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002937 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002938
Willy Tarreaua970c282013-12-06 12:47:19 +01002939 tcpcheck_expect:
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002940 if (check->current_step->string != NULL)
2941 ret = my_memmem(contentptr, check->bi->i, check->current_step->string, check->current_step->string_len) != NULL;
2942 else if (check->current_step->expect_regex != NULL)
2943 ret = regex_exec(check->current_step->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002944
2945 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002946 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002947
2948 /* matched */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002949 step = tcpcheck_get_step_id(check);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002950 if (ret) {
2951 /* matched but we did not want to => ERROR */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002952 if (check->current_step->inverse) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002953 /* 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 matched unwanted 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 */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002960 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d", step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002961 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002962 comment = tcpcheck_get_step_comment(check, step);
2963 if (comment)
2964 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002965 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2966 goto out_end_tcpcheck;
2967 }
2968 /* matched and was supposed to => OK, next step */
2969 else {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002970 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002971 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002972
2973 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002974 while (&check->current_step->list != head &&
2975 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002976 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002977
Willy Tarreauf2c87352015-05-13 12:08:21 +02002978 if (&check->current_step->list == head)
2979 break;
2980
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002981 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002982 goto tcpcheck_expect;
2983 __conn_data_stop_recv(conn);
2984 }
2985 }
2986 else {
2987 /* not matched */
2988 /* not matched and was not supposed to => OK, next step */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002989 if (check->current_step->inverse) {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002990 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002991 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002992
2993 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002994 while (&check->current_step->list != head &&
2995 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002996 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002997
Willy Tarreauf2c87352015-05-13 12:08:21 +02002998 if (&check->current_step->list == head)
2999 break;
3000
Willy Tarreau98aec9f2013-12-06 16:16:41 +01003001 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003002 goto tcpcheck_expect;
3003 __conn_data_stop_recv(conn);
3004 }
3005 /* not matched but was supposed to => ERROR */
3006 else {
3007 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02003008 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003009 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02003010 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003011 }
3012 else {
3013 /* we were looking for a regex */
3014 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02003015 step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003016 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02003017 comment = tcpcheck_get_step_comment(check, step);
3018 if (comment)
3019 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003020 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
3021 goto out_end_tcpcheck;
3022 }
3023 }
3024 } /* end expect */
3025 } /* end loop over double chained step list */
3026
Willy Tarreau263013d2015-05-13 11:59:14 +02003027 /* We're waiting for some I/O to complete, we've reached the end of the
3028 * rules, or both. Do what we have to do, otherwise we're done.
3029 */
3030 if (&check->current_step->list == head && !check->bo->o) {
3031 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
3032 goto out_end_tcpcheck;
3033 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003034
Willy Tarreau53c5a042015-05-13 11:38:17 +02003035 /* warning, current_step may now point to the head */
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01003036 if (check->bo->o)
3037 __conn_data_want_send(conn);
3038
Willy Tarreau53c5a042015-05-13 11:38:17 +02003039 if (&check->current_step->list != head &&
3040 check->current_step->action == TCPCHK_ACT_EXPECT)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01003041 __conn_data_want_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003042 return;
3043
3044 out_end_tcpcheck:
3045 /* collect possible new errors */
3046 if (conn->flags & CO_FL_ERROR)
3047 chk_report_conn_err(conn, 0, 0);
3048
Baptiste Assmann69e273f2013-12-11 00:52:19 +01003049 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003050 check->current_step = NULL;
3051
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01003052 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003053 conn->flags |= CO_FL_ERROR;
3054
3055 __conn_data_stop_both(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003056 return;
3057}
3058
Simon Hormanb1900d52015-01-30 11:22:54 +09003059const char *init_check(struct check *check, int type)
3060{
3061 check->type = type;
3062
3063 /* Allocate buffer for requests... */
3064 if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3065 return "out of memory while allocating check buffer";
3066 }
3067 check->bi->size = global.tune.chksize;
3068
3069 /* Allocate buffer for responses... */
3070 if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3071 return "out of memory while allocating check buffer";
3072 }
3073 check->bo->size = global.tune.chksize;
3074
3075 /* Allocate buffer for partial results... */
3076 if ((check->conn = calloc(1, sizeof(struct connection))) == NULL) {
3077 return "out of memory while allocating check connection";
3078 }
3079
3080 check->conn->t.sock.fd = -1; /* no agent in progress yet */
3081
3082 return NULL;
3083}
3084
Simon Hormanbfb5d332015-01-30 11:22:55 +09003085void free_check(struct check *check)
3086{
3087 free(check->bi);
3088 free(check->bo);
3089 free(check->conn);
3090}
3091
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003092void email_alert_free(struct email_alert *alert)
3093{
3094 struct tcpcheck_rule *rule, *back;
3095
3096 if (!alert)
3097 return;
3098
3099 list_for_each_entry_safe(rule, back, &alert->tcpcheck_rules, list)
3100 free(rule);
3101 free(alert);
3102}
3103
3104static struct task *process_email_alert(struct task *t)
3105{
3106 struct check *check = t->context;
3107 struct email_alertq *q;
3108
3109 q = container_of(check, typeof(*q), check);
3110
3111 if (!(check->state & CHK_ST_ENABLED)) {
3112 if (LIST_ISEMPTY(&q->email_alerts)) {
3113 /* All alerts processed, delete check */
3114 task_delete(t);
3115 task_free(t);
3116 check->task = NULL;
3117 return NULL;
3118 } else {
3119 struct email_alert *alert;
3120
3121 alert = LIST_NEXT(&q->email_alerts, typeof(alert), list);
3122 check->tcpcheck_rules = &alert->tcpcheck_rules;
3123 LIST_DEL(&alert->list);
3124
3125 check->state |= CHK_ST_ENABLED;
3126 }
3127
3128 }
3129
3130 process_chk(t);
3131
3132 if (!(check->state & CHK_ST_INPROGRESS) && check->tcpcheck_rules) {
3133 struct email_alert *alert;
3134
3135 alert = container_of(check->tcpcheck_rules, typeof(*alert), tcpcheck_rules);
3136 email_alert_free(alert);
3137
3138 check->tcpcheck_rules = NULL;
3139 check->state &= ~CHK_ST_ENABLED;
3140 }
3141 return t;
3142}
3143
3144static int init_email_alert_checks(struct server *s)
3145{
3146 int i;
3147 struct mailer *mailer;
3148 const char *err_str;
3149 struct proxy *p = s->proxy;
3150
3151 if (p->email_alert.queues)
3152 /* Already initialised, nothing to do */
3153 return 1;
3154
3155 p->email_alert.queues = calloc(p->email_alert.mailers.m->count, sizeof *p->email_alert.queues);
3156 if (!p->email_alert.queues) {
3157 err_str = "out of memory while allocating checks array";
3158 goto error_alert;
3159 }
3160
3161 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3162 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
3163 struct email_alertq *q = &p->email_alert.queues[i];
3164 struct check *check = &q->check;
3165
3166
3167 LIST_INIT(&q->email_alerts);
3168
Pieter Baauw235fcfc2016-02-13 15:33:40 +01003169 check->inter = p->email_alert.mailers.m->timeout.mail;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003170 check->rise = DEF_AGENT_RISETIME;
3171 check->fall = DEF_AGENT_FALLTIME;
3172 err_str = init_check(check, PR_O2_TCPCHK_CHK);
3173 if (err_str) {
3174 goto error_free;
3175 }
3176
3177 check->xprt = mailer->xprt;
3178 if (!get_host_port(&mailer->addr))
3179 /* Default to submission port */
3180 check->port = 587;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003181 check->addr = mailer->addr;
3182 check->server = s;
3183 }
3184
3185 return 1;
3186
3187error_free:
3188 while (i-- > 1)
3189 task_free(p->email_alert.queues[i].check.task);
3190 free(p->email_alert.queues);
3191 p->email_alert.queues = NULL;
3192error_alert:
3193 Alert("Email alert [%s] could not be initialised: %s\n", p->id, err_str);
3194 return 0;
3195}
3196
3197
3198static int add_tcpcheck_expect_str(struct list *list, const char *str)
3199{
3200 struct tcpcheck_rule *tcpcheck;
3201
3202 tcpcheck = calloc(1, sizeof *tcpcheck);
3203 if (!tcpcheck)
3204 return 0;
3205
3206 tcpcheck->action = TCPCHK_ACT_EXPECT;
3207 tcpcheck->string = strdup(str);
3208 if (!tcpcheck->string) {
3209 free(tcpcheck);
3210 return 0;
3211 }
3212
3213 LIST_ADDQ(list, &tcpcheck->list);
3214 return 1;
3215}
3216
3217static int add_tcpcheck_send_strs(struct list *list, const char * const *strs)
3218{
3219 struct tcpcheck_rule *tcpcheck;
Willy Tarreau64345aa2016-08-10 19:29:09 +02003220 const char *in;
3221 char *dst;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003222 int i;
3223
3224 tcpcheck = calloc(1, sizeof *tcpcheck);
3225 if (!tcpcheck)
3226 return 0;
3227
3228 tcpcheck->action = TCPCHK_ACT_SEND;
3229
3230 tcpcheck->string_len = 0;
3231 for (i = 0; strs[i]; i++)
3232 tcpcheck->string_len += strlen(strs[i]);
3233
3234 tcpcheck->string = malloc(tcpcheck->string_len + 1);
3235 if (!tcpcheck->string) {
3236 free(tcpcheck);
3237 return 0;
3238 }
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003239
Willy Tarreau64345aa2016-08-10 19:29:09 +02003240 dst = tcpcheck->string;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003241 for (i = 0; strs[i]; i++)
Willy Tarreau64345aa2016-08-10 19:29:09 +02003242 for (in = strs[i]; (*dst = *in++); dst++);
3243 *dst = 0;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003244
3245 LIST_ADDQ(list, &tcpcheck->list);
3246 return 1;
3247}
3248
3249static int enqueue_one_email_alert(struct email_alertq *q, const char *msg)
3250{
3251 struct email_alert *alert = NULL;
3252 struct tcpcheck_rule *tcpcheck;
3253 struct check *check = &q->check;
3254 struct proxy *p = check->server->proxy;
3255
3256 alert = calloc(1, sizeof *alert);
3257 if (!alert) {
3258 goto error;
3259 }
3260 LIST_INIT(&alert->tcpcheck_rules);
3261
3262 tcpcheck = calloc(1, sizeof *tcpcheck);
3263 if (!tcpcheck)
3264 goto error;
3265 tcpcheck->action = TCPCHK_ACT_CONNECT;
3266 LIST_ADDQ(&alert->tcpcheck_rules, &tcpcheck->list);
3267
3268 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "220 "))
3269 goto error;
3270
3271 {
3272 const char * const strs[4] = { "EHLO ", p->email_alert.myhostname, "\r\n" };
3273 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3274 goto error;
3275 }
3276
3277 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3278 goto error;
3279
3280 {
3281 const char * const strs[4] = { "MAIL FROM:<", p->email_alert.from, ">\r\n" };
3282 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3283 goto error;
3284 }
3285
3286 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3287 goto error;
3288
3289 {
3290 const char * const strs[4] = { "RCPT TO:<", p->email_alert.to, ">\r\n" };
3291 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3292 goto error;
3293 }
3294
3295 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3296 goto error;
3297
3298 {
3299 const char * const strs[2] = { "DATA\r\n" };
3300 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3301 goto error;
3302 }
3303
3304 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "354 "))
3305 goto error;
3306
3307 {
3308 struct tm tm;
3309 char datestr[48];
3310 const char * const strs[18] = {
Pieter Baauw5e0964e2016-02-13 16:27:35 +01003311 "From: ", p->email_alert.from, "\r\n",
3312 "To: ", p->email_alert.to, "\r\n",
3313 "Date: ", datestr, "\r\n",
3314 "Subject: [HAproxy Alert] ", msg, "\r\n",
3315 "\r\n",
3316 msg, "\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003317 "\r\n",
Pieter Baauwed35c372015-07-22 19:51:54 +02003318 ".\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003319 NULL
3320 };
3321
3322 get_localtime(date.tv_sec, &tm);
3323
3324 if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %T %z (%Z)", &tm) == 0) {
3325 goto error;
3326 }
3327
3328 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3329 goto error;
3330 }
3331
3332 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3333 goto error;
3334
3335 {
3336 const char * const strs[2] = { "QUIT\r\n" };
3337 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3338 goto error;
3339 }
3340
3341 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "221 "))
3342 goto error;
3343
3344 if (!check->task) {
3345 struct task *t;
3346
3347 if ((t = task_new()) == NULL)
3348 goto error;
3349
3350 check->task = t;
3351 t->process = process_email_alert;
3352 t->context = check;
3353
3354 /* check this in one ms */
3355 t->expire = tick_add(now_ms, MS_TO_TICKS(1));
3356 check->start = now;
3357 task_queue(t);
3358 }
3359
3360 LIST_ADDQ(&q->email_alerts, &alert->list);
3361
3362 return 1;
3363
3364error:
3365 email_alert_free(alert);
3366 return 0;
3367}
3368
3369static void enqueue_email_alert(struct proxy *p, const char *msg)
3370{
3371 int i;
3372 struct mailer *mailer;
3373
3374 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3375 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
3376 if (!enqueue_one_email_alert(&p->email_alert.queues[i], msg)) {
3377 Alert("Email alert [%s] could not be enqueued: out of memory\n", p->id);
3378 return;
3379 }
3380 }
3381
3382 return;
3383}
3384
3385/*
3386 * Send email alert if configured.
3387 */
Simon Horman64e34162015-02-06 11:11:57 +09003388void send_email_alert(struct server *s, int level, const char *format, ...)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003389{
3390 va_list argp;
3391 char buf[1024];
3392 int len;
3393 struct proxy *p = s->proxy;
3394
Simon Horman64e34162015-02-06 11:11:57 +09003395 if (!p->email_alert.mailers.m || level > p->email_alert.level ||
3396 format == NULL || !init_email_alert_checks(s))
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003397 return;
3398
3399 va_start(argp, format);
3400 len = vsnprintf(buf, sizeof(buf), format, argp);
3401 va_end(argp);
3402
3403 if (len < 0) {
Cyril Bontéb65e0332015-12-04 03:07:08 +01003404 Alert("Email alert [%s] could not format message\n", p->id);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003405 return;
3406 }
3407
3408 enqueue_email_alert(p, buf);
3409}
3410
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003411/*
3412 * Return value:
3413 * the port to be used for the health check
3414 * 0 in case no port could be found for the check
3415 */
3416int srv_check_healthcheck_port(struct check *chk)
3417{
3418 int i = 0;
3419 struct server *srv = NULL;
3420
3421 srv = chk->server;
3422
3423 /* If neither a port nor an addr was specified and no check transport
3424 * layer is forced, then the transport layer used by the checks is the
3425 * same as for the production traffic. Otherwise we use raw_sock by
3426 * default, unless one is specified.
3427 */
3428 if (!chk->port && !is_addr(&chk->addr)) {
3429#ifdef USE_OPENSSL
3430 chk->use_ssl |= (srv->use_ssl || (srv->proxy->options & PR_O_TCPCHK_SSL));
3431#endif
3432 chk->send_proxy |= (srv->pp_opts);
3433 }
3434
3435 /* by default, we use the health check port ocnfigured */
3436 if (chk->port > 0)
3437 return chk->port;
3438
3439 /* try to get the port from check_core.addr if check.port not set */
3440 i = get_host_port(&chk->addr);
3441 if (i > 0)
3442 return i;
3443
3444 /* try to get the port from server address */
3445 /* prevent MAPPORTS from working at this point, since checks could
3446 * not be performed in such case (MAPPORTS impose a relative ports
3447 * based on live traffic)
3448 */
3449 if (srv->flags & SRV_F_MAPPORTS)
3450 return 0;
3451 i = get_host_port(&srv->addr); /* by default */
3452 if (i > 0)
3453 return i;
3454
3455 return 0;
3456}
3457
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003458
Willy Tarreaubd741542010-03-16 18:46:54 +01003459/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003460 * Local variables:
3461 * c-indent-level: 8
3462 * c-basic-offset: 8
3463 * End:
3464 */