blob: b1e45499fd77f742f131710c6de1703109804fe2 [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.
Willy Tarreau865c5142016-12-21 20:04:48 +01002334 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002335 */
Willy Tarreau865c5142016-12-21 20:04:48 +01002336static int start_checks()
2337{
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002338
2339 struct proxy *px;
2340 struct server *s;
2341 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002342 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002343
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002344 /* 1- count the checkers to run simultaneously.
2345 * We also determine the minimum interval among all of those which
2346 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2347 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002348 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002349 * too short an interval for all others.
2350 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002351 for (px = proxy; px; px = px->next) {
2352 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002353 if (s->slowstart) {
2354 if ((t = task_new()) == NULL) {
2355 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Willy Tarreau865c5142016-12-21 20:04:48 +01002356 return ERR_ALERT | ERR_FATAL;
Willy Tarreaue7b73482013-11-21 11:50:50 +01002357 }
2358 /* We need a warmup task that will be called when the server
2359 * state switches from down to up.
2360 */
2361 s->warmup = t;
2362 t->process = server_warmup;
2363 t->context = s;
2364 t->expire = TICK_ETERNITY;
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002365 /* server can be in this state only because of */
2366 if (s->state == SRV_ST_STARTING)
2367 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 +01002368 }
2369
Willy Tarreaud8514a22013-12-11 21:10:14 +01002370 if (s->check.state & CHK_ST_CONFIGURED) {
2371 nbcheck++;
2372 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2373 (!mininter || mininter > srv_getinter(&s->check)))
2374 mininter = srv_getinter(&s->check);
2375 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002376
Willy Tarreaud8514a22013-12-11 21:10:14 +01002377 if (s->agent.state & CHK_ST_CONFIGURED) {
2378 nbcheck++;
2379 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2380 (!mininter || mininter > srv_getinter(&s->agent)))
2381 mininter = srv_getinter(&s->agent);
2382 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002383 }
2384 }
2385
Simon Horman4a741432013-02-23 15:35:38 +09002386 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002387 return 0;
2388
2389 srand((unsigned)time(NULL));
2390
2391 /*
2392 * 2- start them as far as possible from each others. For this, we will
2393 * start them after their interval set to the min interval divided by
2394 * the number of servers, weighted by the server's position in the list.
2395 */
2396 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002397 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2398 if (init_pid_list()) {
2399 Alert("Starting [%s] check: out of memory.\n", px->id);
Willy Tarreau865c5142016-12-21 20:04:48 +01002400 return ERR_ALERT | ERR_FATAL;
Simon Horman98637e52014-06-20 12:30:16 +09002401 }
2402 }
2403
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002404 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002405 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002406 if (s->check.state & CHK_ST_CONFIGURED) {
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002407 if (s->check.type == PR_O2_EXT_CHK) {
2408 if (!prepare_external_check(&s->check))
Willy Tarreau865c5142016-12-21 20:04:48 +01002409 return ERR_ALERT | ERR_FATAL;
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002410 }
Simon Hormand60d6912013-11-25 10:46:36 +09002411 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
Willy Tarreau865c5142016-12-21 20:04:48 +01002412 return ERR_ALERT | ERR_FATAL;
Simon Hormand60d6912013-11-25 10:46:36 +09002413 srvpos++;
2414 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002415
Simon Hormand60d6912013-11-25 10:46:36 +09002416 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002417 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002418 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
Willy Tarreau865c5142016-12-21 20:04:48 +01002419 return ERR_ALERT | ERR_FATAL;
Simon Hormand60d6912013-11-25 10:46:36 +09002420 }
2421 srvpos++;
2422 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002423 }
2424 }
2425 return 0;
2426}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002427
2428/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002429 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002430 * The buffer MUST be terminated by a null byte before calling this function.
2431 * Sets server status appropriately. The caller is responsible for ensuring
2432 * that the buffer contains at least 13 characters. If <done> is zero, we may
2433 * return 0 to indicate that data is required to decide of a match.
2434 */
2435static int httpchk_expect(struct server *s, int done)
2436{
2437 static char status_msg[] = "HTTP status check returned code <000>";
2438 char status_code[] = "000";
2439 char *contentptr;
2440 int crlf;
2441 int ret;
2442
2443 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2444 case PR_O2_EXP_STS:
2445 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002446 memcpy(status_code, s->check.bi->data + 9, 3);
2447 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002448
2449 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2450 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2451 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002452 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002453
2454 /* we necessarily have the response, so there are no partial failures */
2455 if (s->proxy->options2 & PR_O2_EXP_INV)
2456 ret = !ret;
2457
Simon Horman4a741432013-02-23 15:35:38 +09002458 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002459 break;
2460
2461 case PR_O2_EXP_STR:
2462 case PR_O2_EXP_RSTR:
2463 /* very simple response parser: ignore CR and only count consecutive LFs,
2464 * stop with contentptr pointing to first char after the double CRLF or
2465 * to '\0' if crlf < 2.
2466 */
2467 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002468 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002469 if (crlf >= 2)
2470 break;
2471 if (*contentptr == '\r')
2472 continue;
2473 else if (*contentptr == '\n')
2474 crlf++;
2475 else
2476 crlf = 0;
2477 }
2478
2479 /* Check that response contains a body... */
2480 if (crlf < 2) {
2481 if (!done)
2482 return 0;
2483
Simon Horman4a741432013-02-23 15:35:38 +09002484 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002485 "HTTP content check could not find a response body");
2486 return 1;
2487 }
2488
2489 /* Check that response body is not empty... */
2490 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002491 if (!done)
2492 return 0;
2493
Simon Horman4a741432013-02-23 15:35:38 +09002494 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002495 "HTTP content check found empty response body");
2496 return 1;
2497 }
2498
2499 /* Check the response content against the supplied string
2500 * or regex... */
2501 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2502 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2503 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002504 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002505
2506 /* if we don't match, we may need to wait more */
2507 if (!ret && !done)
2508 return 0;
2509
2510 if (ret) {
2511 /* content matched */
2512 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002513 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002514 "HTTP check matched unwanted content");
2515 else
Simon Horman4a741432013-02-23 15:35:38 +09002516 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002517 "HTTP content check matched");
2518 }
2519 else {
2520 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002521 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002522 "HTTP check did not match unwanted content");
2523 else
Simon Horman4a741432013-02-23 15:35:38 +09002524 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002525 "HTTP content check did not match");
2526 }
2527 break;
2528 }
2529 return 1;
2530}
2531
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002532/*
2533 * return the id of a step in a send/expect session
2534 */
Simon Hormane16c1b32015-01-30 11:22:57 +09002535static int tcpcheck_get_step_id(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002536{
2537 struct tcpcheck_rule *cur = NULL, *next = NULL;
2538 int i = 0;
2539
Willy Tarreau213c6782014-10-02 14:51:02 +02002540 /* not even started anything yet => step 0 = initial connect */
Baptiste Assmannf95bc8e2015-04-25 16:03:06 +02002541 if (!check->current_step)
Willy Tarreau213c6782014-10-02 14:51:02 +02002542 return 0;
2543
Simon Hormane16c1b32015-01-30 11:22:57 +09002544 cur = check->last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002545
2546 /* no step => first step */
2547 if (cur == NULL)
2548 return 1;
2549
2550 /* increment i until current step */
Simon Hormane16c1b32015-01-30 11:22:57 +09002551 list_for_each_entry(next, check->tcpcheck_rules, list) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002552 if (next->list.p == &cur->list)
2553 break;
2554 ++i;
2555 }
2556
2557 return i;
2558}
2559
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002560/*
2561 * return the latest known comment before (including) the given stepid
2562 * returns NULL if no comment found
2563 */
2564static char * tcpcheck_get_step_comment(struct check *check, int stepid)
2565{
2566 struct tcpcheck_rule *cur = NULL;
2567 char *ret = NULL;
2568 int i = 0;
2569
2570 /* not even started anything yet, return latest comment found before any action */
2571 if (!check->current_step) {
2572 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2573 if (cur->action == TCPCHK_ACT_COMMENT)
2574 ret = cur->comment;
2575 else
2576 goto return_comment;
2577 }
2578 }
2579
2580 i = 1;
2581 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2582 if (cur->comment)
2583 ret = cur->comment;
2584
2585 if (i >= stepid)
2586 goto return_comment;
2587
2588 ++i;
2589 }
2590
2591 return_comment:
2592 return ret;
2593}
2594
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002595static void tcpcheck_main(struct connection *conn)
2596{
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002597 char *contentptr, *comment;
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002598 struct tcpcheck_rule *next;
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002599 int done = 0, ret = 0, step = 0;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002600 struct check *check = conn->owner;
2601 struct server *s = check->server;
2602 struct task *t = check->task;
Simon Hormane16c1b32015-01-30 11:22:57 +09002603 struct list *head = check->tcpcheck_rules;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002604
Willy Tarreauef953952014-10-02 14:30:14 +02002605 /* here, we know that the check is complete or that it failed */
2606 if (check->result != CHK_RES_UNKNOWN)
2607 goto out_end_tcpcheck;
2608
2609 /* We have 4 possibilities here :
2610 * 1. we've not yet attempted step 1, and step 1 is a connect, so no
2611 * connection attempt was made yet ;
2612 * 2. we've not yet attempted step 1, and step 1 is a not connect or
2613 * does not exist (no rule), so a connection attempt was made
2614 * before coming here.
2615 * 3. we're coming back after having started with step 1, so we may
2616 * be waiting for a connection attempt to complete.
2617 * 4. the connection + handshake are complete
2618 *
2619 * #2 and #3 are quite similar, we want both the connection and the
2620 * handshake to complete before going any further. Thus we must always
2621 * wait for a connection to complete unless we're before and existing
2622 * step 1.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002623 */
Willy Tarreau449f9522015-05-13 15:39:48 +02002624
2625 /* find first rule and skip comments */
2626 next = LIST_NEXT(head, struct tcpcheck_rule *, list);
2627 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
2628 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
2629
Willy Tarreauef953952014-10-02 14:30:14 +02002630 if ((!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE)) &&
Willy Tarreau449f9522015-05-13 15:39:48 +02002631 (check->current_step || &next->list == head)) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002632 /* we allow up to min(inter, timeout.connect) for a connection
2633 * to establish but only when timeout.check is set
2634 * as it may be to short for a full check otherwise
2635 */
2636 while (tick_is_expired(t->expire, now_ms)) {
2637 int t_con;
2638
2639 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2640 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2641
2642 if (s->proxy->timeout.check)
2643 t->expire = tick_first(t->expire, t_con);
2644 }
2645 return;
2646 }
2647
Willy Tarreauef953952014-10-02 14:30:14 +02002648 /* special case: option tcp-check with no rule, a connect is enough */
Willy Tarreau449f9522015-05-13 15:39:48 +02002649 if (&next->list == head) {
Willy Tarreauef953952014-10-02 14:30:14 +02002650 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002651 goto out_end_tcpcheck;
Willy Tarreauef953952014-10-02 14:30:14 +02002652 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002653
Willy Tarreau213c6782014-10-02 14:51:02 +02002654 /* no step means first step initialisation */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002655 if (check->current_step == NULL) {
Willy Tarreau213c6782014-10-02 14:51:02 +02002656 check->last_started_step = NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002657 check->bo->p = check->bo->data;
2658 check->bo->o = 0;
2659 check->bi->p = check->bi->data;
2660 check->bi->i = 0;
Willy Tarreau449f9522015-05-13 15:39:48 +02002661 check->current_step = next;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002662 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2663 if (s->proxy->timeout.check)
2664 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2665 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002666
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002667 /* It's only the rules which will enable send/recv */
2668 __conn_data_stop_both(conn);
2669
Willy Tarreauabca5b62013-12-06 14:19:25 +01002670 while (1) {
Willy Tarreau263013d2015-05-13 11:59:14 +02002671 /* We have to try to flush the output buffer before reading, at
2672 * the end, or if we're about to send a string that does not fit
2673 * in the remaining space. That explains why we break out of the
2674 * loop after this control.
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002675 */
2676 if (check->bo->o &&
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002677 (&check->current_step->list == head ||
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002678 check->current_step->action != TCPCHK_ACT_SEND ||
2679 check->current_step->string_len >= buffer_total_space(check->bo))) {
2680
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002681 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002682 if (conn->flags & CO_FL_ERROR) {
2683 chk_report_conn_err(conn, errno, 0);
2684 __conn_data_stop_both(conn);
2685 goto out_end_tcpcheck;
2686 }
Willy Tarreau263013d2015-05-13 11:59:14 +02002687 break;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002688 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002689 }
2690
Willy Tarreau263013d2015-05-13 11:59:14 +02002691 if (&check->current_step->list == head)
Willy Tarreauabca5b62013-12-06 14:19:25 +01002692 break;
2693
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002694 /* have 'next' point to the next rule or NULL if we're on the
2695 * last one, connect() needs this.
2696 */
Willy Tarreau5581c272015-05-13 12:24:53 +02002697 next = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002698
2699 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002700 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002701 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002702
2703 /* NULL if we're on the last rule */
Willy Tarreauf3d34822014-12-08 12:11:28 +01002704 if (&next->list == head)
2705 next = NULL;
2706
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002707 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2708 struct protocol *proto;
2709 struct xprt_ops *xprt;
2710
2711 /* mark the step as started */
2712 check->last_started_step = check->current_step;
2713 /* first, shut existing connection */
2714 conn_force_close(conn);
2715
2716 /* prepare new connection */
2717 /* initialization */
2718 conn_init(conn);
2719 conn_attach(conn, check, &check_conn_cb);
2720 conn->target = &s->obj_type;
2721
2722 /* no client address */
2723 clear_addr(&conn->addr.from);
2724
Simon Horman41f58762015-01-30 11:22:56 +09002725 if (is_addr(&check->addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002726 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09002727 conn->addr.to = check->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002728 }
2729 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002730 /* we'll connect to the addr on the server */
2731 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002732 }
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01002733 proto = protocol_by_family(conn->addr.to.ss_family);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002734
2735 /* port */
2736 if (check->current_step->port)
2737 set_host_port(&conn->addr.to, check->current_step->port);
2738 else if (check->port)
2739 set_host_port(&conn->addr.to, check->port);
2740
2741#ifdef USE_OPENSSL
2742 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
2743 xprt = &ssl_sock;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002744 }
2745 else {
2746 xprt = &raw_sock;
2747 }
2748#else /* USE_OPENSSL */
2749 xprt = &raw_sock;
2750#endif /* USE_OPENSSL */
2751 conn_prepare(conn, proto, xprt);
2752
Willy Tarreaue7dff022015-04-03 01:14:29 +02002753 ret = SF_ERR_INTERNAL;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002754 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01002755 ret = proto->connect(conn,
2756 1 /* I/O polling is always needed */,
2757 (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002758 conn->flags |= CO_FL_WAKE_DATA;
2759 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2760 conn->send_proxy_ofs = 1;
2761 conn->flags |= CO_FL_SEND_PROXY;
2762 }
2763
2764 /* It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02002765 * - SF_ERR_NONE if everything's OK
2766 * - SF_ERR_SRVTO if there are no more servers
2767 * - SF_ERR_SRVCL if the connection was refused by the server
2768 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2769 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2770 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +01002771 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002772 * Note that we try to prevent the network stack from sending the ACK during the
2773 * connect() when a pure TCP check is used (without PROXY protocol).
2774 */
2775 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002776 case SF_ERR_NONE:
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002777 /* we allow up to min(inter, timeout.connect) for a connection
2778 * to establish but only when timeout.check is set
2779 * as it may be to short for a full check otherwise
2780 */
2781 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2782
2783 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2784 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2785 t->expire = tick_first(t->expire, t_con);
2786 }
2787 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002788 case SF_ERR_SRVTO: /* ETIMEDOUT */
2789 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002790 step = tcpcheck_get_step_id(check);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002791 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002792 step, strerror(errno));
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002793 comment = tcpcheck_get_step_comment(check, step);
2794 if (comment)
2795 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002796 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2797 goto out_end_tcpcheck;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002798 case SF_ERR_PRXCOND:
2799 case SF_ERR_RESOURCE:
2800 case SF_ERR_INTERNAL:
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002801 step = tcpcheck_get_step_id(check);
2802 chunk_printf(&trash, "TCPCHK error establishing connection at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002803 comment = tcpcheck_get_step_comment(check, step);
2804 if (comment)
2805 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002806 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2807 goto out_end_tcpcheck;
2808 }
2809
2810 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002811 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002812
2813 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002814 while (&check->current_step->list != head &&
2815 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002816 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002817
Willy Tarreauf2c87352015-05-13 12:08:21 +02002818 if (&check->current_step->list == head)
2819 break;
2820
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002821 /* don't do anything until the connection is established */
2822 if (!(conn->flags & CO_FL_CONNECTED)) {
2823 /* update expire time, should be done by process_chk */
2824 /* we allow up to min(inter, timeout.connect) for a connection
2825 * to establish but only when timeout.check is set
2826 * as it may be to short for a full check otherwise
2827 */
2828 while (tick_is_expired(t->expire, now_ms)) {
2829 int t_con;
2830
2831 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2832 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2833
2834 if (s->proxy->timeout.check)
2835 t->expire = tick_first(t->expire, t_con);
2836 }
2837 return;
2838 }
2839
2840 } /* end 'connect' */
2841 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2842 /* mark the step as started */
2843 check->last_started_step = check->current_step;
2844
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002845 /* reset the read buffer */
2846 if (*check->bi->data != '\0') {
2847 *check->bi->data = '\0';
2848 check->bi->i = 0;
2849 }
2850
2851 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
2852 conn->flags |= CO_FL_ERROR;
2853 chk_report_conn_err(conn, 0, 0);
2854 goto out_end_tcpcheck;
2855 }
2856
Willy Tarreauabca5b62013-12-06 14:19:25 +01002857 if (check->current_step->string_len >= check->bo->size) {
2858 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2859 check->current_step->string_len, check->bo->size,
Simon Hormane16c1b32015-01-30 11:22:57 +09002860 tcpcheck_get_step_id(check));
Willy Tarreauabca5b62013-12-06 14:19:25 +01002861 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2862 goto out_end_tcpcheck;
2863 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002864
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002865 /* do not try to send if there is no space */
2866 if (check->current_step->string_len >= buffer_total_space(check->bo))
2867 continue;
2868
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002869 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2870 *check->bo->p = '\0'; /* to make gdb output easier to read */
2871
Willy Tarreauabca5b62013-12-06 14:19:25 +01002872 /* go to next rule and try to send */
Willy Tarreau5581c272015-05-13 12:24:53 +02002873 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002874
2875 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002876 while (&check->current_step->list != head &&
2877 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002878 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Willy Tarreauf2c87352015-05-13 12:08:21 +02002879
2880 if (&check->current_step->list == head)
2881 break;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002882 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002883 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002884 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002885 goto out_end_tcpcheck;
2886
Willy Tarreau310987a2014-01-22 19:46:33 +01002887 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002888 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
2889 done = 1;
2890 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2891 /* Report network errors only if we got no other data. Otherwise
2892 * we'll let the upper layers decide whether the response is OK
2893 * or not. It is very common that an RST sent by the server is
2894 * reported as an error just after the last data chunk.
2895 */
2896 chk_report_conn_err(conn, errno, 0);
2897 goto out_end_tcpcheck;
2898 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002899 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002900 else
Willy Tarreau263013d2015-05-13 11:59:14 +02002901 break;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002902 }
2903
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002904 /* mark the step as started */
2905 check->last_started_step = check->current_step;
2906
2907
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002908 /* Intermediate or complete response received.
2909 * Terminate string in check->bi->data buffer.
2910 */
2911 if (check->bi->i < check->bi->size) {
2912 check->bi->data[check->bi->i] = '\0';
2913 }
2914 else {
2915 check->bi->data[check->bi->i - 1] = '\0';
2916 done = 1; /* buffer full, don't wait for more data */
2917 }
2918
2919 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002920
2921 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002922 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002923 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002924 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002925
2926 /* empty response */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002927 step = tcpcheck_get_step_id(check);
2928 chunk_printf(&trash, "TCPCHK got an empty response at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002929 comment = tcpcheck_get_step_comment(check, step);
2930 if (comment)
2931 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002932 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2933
2934 goto out_end_tcpcheck;
2935 }
2936
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002937 if (!done && (check->current_step->string != NULL) && (check->bi->i < check->current_step->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002938 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002939
Willy Tarreaua970c282013-12-06 12:47:19 +01002940 tcpcheck_expect:
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002941 if (check->current_step->string != NULL)
2942 ret = my_memmem(contentptr, check->bi->i, check->current_step->string, check->current_step->string_len) != NULL;
2943 else if (check->current_step->expect_regex != NULL)
2944 ret = regex_exec(check->current_step->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002945
2946 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002947 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002948
2949 /* matched */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002950 step = tcpcheck_get_step_id(check);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002951 if (ret) {
2952 /* matched but we did not want to => ERROR */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002953 if (check->current_step->inverse) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002954 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002955 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002956 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002957 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002958 }
2959 else {
2960 /* we were looking for a regex */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002961 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d", step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002962 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002963 comment = tcpcheck_get_step_comment(check, step);
2964 if (comment)
2965 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002966 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2967 goto out_end_tcpcheck;
2968 }
2969 /* matched and was supposed to => OK, next step */
2970 else {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002971 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002972 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002973
2974 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002975 while (&check->current_step->list != head &&
2976 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002977 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002978
Willy Tarreauf2c87352015-05-13 12:08:21 +02002979 if (&check->current_step->list == head)
2980 break;
2981
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002982 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002983 goto tcpcheck_expect;
2984 __conn_data_stop_recv(conn);
2985 }
2986 }
2987 else {
2988 /* not matched */
2989 /* not matched and was not supposed to => OK, next step */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002990 if (check->current_step->inverse) {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002991 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002992 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002993
2994 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002995 while (&check->current_step->list != head &&
2996 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002997 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002998
Willy Tarreauf2c87352015-05-13 12:08:21 +02002999 if (&check->current_step->list == head)
3000 break;
3001
Willy Tarreau98aec9f2013-12-06 16:16:41 +01003002 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003003 goto tcpcheck_expect;
3004 __conn_data_stop_recv(conn);
3005 }
3006 /* not matched but was supposed to => ERROR */
3007 else {
3008 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02003009 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003010 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02003011 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003012 }
3013 else {
3014 /* we were looking for a regex */
3015 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02003016 step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003017 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02003018 comment = tcpcheck_get_step_comment(check, step);
3019 if (comment)
3020 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003021 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
3022 goto out_end_tcpcheck;
3023 }
3024 }
3025 } /* end expect */
3026 } /* end loop over double chained step list */
3027
Willy Tarreau263013d2015-05-13 11:59:14 +02003028 /* We're waiting for some I/O to complete, we've reached the end of the
3029 * rules, or both. Do what we have to do, otherwise we're done.
3030 */
3031 if (&check->current_step->list == head && !check->bo->o) {
3032 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
3033 goto out_end_tcpcheck;
3034 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003035
Willy Tarreau53c5a042015-05-13 11:38:17 +02003036 /* warning, current_step may now point to the head */
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01003037 if (check->bo->o)
3038 __conn_data_want_send(conn);
3039
Willy Tarreau53c5a042015-05-13 11:38:17 +02003040 if (&check->current_step->list != head &&
3041 check->current_step->action == TCPCHK_ACT_EXPECT)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01003042 __conn_data_want_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003043 return;
3044
3045 out_end_tcpcheck:
3046 /* collect possible new errors */
3047 if (conn->flags & CO_FL_ERROR)
3048 chk_report_conn_err(conn, 0, 0);
3049
Baptiste Assmann69e273f2013-12-11 00:52:19 +01003050 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003051 check->current_step = NULL;
3052
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01003053 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003054 conn->flags |= CO_FL_ERROR;
3055
3056 __conn_data_stop_both(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003057 return;
3058}
3059
Simon Hormanb1900d52015-01-30 11:22:54 +09003060const char *init_check(struct check *check, int type)
3061{
3062 check->type = type;
3063
3064 /* Allocate buffer for requests... */
3065 if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3066 return "out of memory while allocating check buffer";
3067 }
3068 check->bi->size = global.tune.chksize;
3069
3070 /* Allocate buffer for responses... */
3071 if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3072 return "out of memory while allocating check buffer";
3073 }
3074 check->bo->size = global.tune.chksize;
3075
3076 /* Allocate buffer for partial results... */
3077 if ((check->conn = calloc(1, sizeof(struct connection))) == NULL) {
3078 return "out of memory while allocating check connection";
3079 }
3080
3081 check->conn->t.sock.fd = -1; /* no agent in progress yet */
3082
3083 return NULL;
3084}
3085
Simon Hormanbfb5d332015-01-30 11:22:55 +09003086void free_check(struct check *check)
3087{
3088 free(check->bi);
3089 free(check->bo);
3090 free(check->conn);
3091}
3092
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003093void email_alert_free(struct email_alert *alert)
3094{
3095 struct tcpcheck_rule *rule, *back;
3096
3097 if (!alert)
3098 return;
3099
3100 list_for_each_entry_safe(rule, back, &alert->tcpcheck_rules, list)
3101 free(rule);
3102 free(alert);
3103}
3104
3105static struct task *process_email_alert(struct task *t)
3106{
3107 struct check *check = t->context;
3108 struct email_alertq *q;
3109
3110 q = container_of(check, typeof(*q), check);
3111
3112 if (!(check->state & CHK_ST_ENABLED)) {
3113 if (LIST_ISEMPTY(&q->email_alerts)) {
3114 /* All alerts processed, delete check */
3115 task_delete(t);
3116 task_free(t);
3117 check->task = NULL;
3118 return NULL;
3119 } else {
3120 struct email_alert *alert;
3121
3122 alert = LIST_NEXT(&q->email_alerts, typeof(alert), list);
3123 check->tcpcheck_rules = &alert->tcpcheck_rules;
3124 LIST_DEL(&alert->list);
3125
3126 check->state |= CHK_ST_ENABLED;
3127 }
3128
3129 }
3130
3131 process_chk(t);
3132
3133 if (!(check->state & CHK_ST_INPROGRESS) && check->tcpcheck_rules) {
3134 struct email_alert *alert;
3135
3136 alert = container_of(check->tcpcheck_rules, typeof(*alert), tcpcheck_rules);
3137 email_alert_free(alert);
3138
3139 check->tcpcheck_rules = NULL;
3140 check->state &= ~CHK_ST_ENABLED;
3141 }
3142 return t;
3143}
3144
3145static int init_email_alert_checks(struct server *s)
3146{
3147 int i;
3148 struct mailer *mailer;
3149 const char *err_str;
3150 struct proxy *p = s->proxy;
3151
3152 if (p->email_alert.queues)
3153 /* Already initialised, nothing to do */
3154 return 1;
3155
3156 p->email_alert.queues = calloc(p->email_alert.mailers.m->count, sizeof *p->email_alert.queues);
3157 if (!p->email_alert.queues) {
3158 err_str = "out of memory while allocating checks array";
3159 goto error_alert;
3160 }
3161
3162 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3163 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
3164 struct email_alertq *q = &p->email_alert.queues[i];
3165 struct check *check = &q->check;
3166
3167
3168 LIST_INIT(&q->email_alerts);
3169
Pieter Baauw235fcfc2016-02-13 15:33:40 +01003170 check->inter = p->email_alert.mailers.m->timeout.mail;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003171 check->rise = DEF_AGENT_RISETIME;
3172 check->fall = DEF_AGENT_FALLTIME;
3173 err_str = init_check(check, PR_O2_TCPCHK_CHK);
3174 if (err_str) {
3175 goto error_free;
3176 }
3177
3178 check->xprt = mailer->xprt;
3179 if (!get_host_port(&mailer->addr))
3180 /* Default to submission port */
3181 check->port = 587;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003182 check->addr = mailer->addr;
3183 check->server = s;
3184 }
3185
3186 return 1;
3187
3188error_free:
3189 while (i-- > 1)
3190 task_free(p->email_alert.queues[i].check.task);
3191 free(p->email_alert.queues);
3192 p->email_alert.queues = NULL;
3193error_alert:
3194 Alert("Email alert [%s] could not be initialised: %s\n", p->id, err_str);
3195 return 0;
3196}
3197
3198
3199static int add_tcpcheck_expect_str(struct list *list, const char *str)
3200{
3201 struct tcpcheck_rule *tcpcheck;
3202
3203 tcpcheck = calloc(1, sizeof *tcpcheck);
3204 if (!tcpcheck)
3205 return 0;
3206
3207 tcpcheck->action = TCPCHK_ACT_EXPECT;
3208 tcpcheck->string = strdup(str);
3209 if (!tcpcheck->string) {
3210 free(tcpcheck);
3211 return 0;
3212 }
3213
3214 LIST_ADDQ(list, &tcpcheck->list);
3215 return 1;
3216}
3217
3218static int add_tcpcheck_send_strs(struct list *list, const char * const *strs)
3219{
3220 struct tcpcheck_rule *tcpcheck;
Willy Tarreau64345aa2016-08-10 19:29:09 +02003221 const char *in;
3222 char *dst;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003223 int i;
3224
3225 tcpcheck = calloc(1, sizeof *tcpcheck);
3226 if (!tcpcheck)
3227 return 0;
3228
3229 tcpcheck->action = TCPCHK_ACT_SEND;
3230
3231 tcpcheck->string_len = 0;
3232 for (i = 0; strs[i]; i++)
3233 tcpcheck->string_len += strlen(strs[i]);
3234
3235 tcpcheck->string = malloc(tcpcheck->string_len + 1);
3236 if (!tcpcheck->string) {
3237 free(tcpcheck);
3238 return 0;
3239 }
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003240
Willy Tarreau64345aa2016-08-10 19:29:09 +02003241 dst = tcpcheck->string;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003242 for (i = 0; strs[i]; i++)
Willy Tarreau64345aa2016-08-10 19:29:09 +02003243 for (in = strs[i]; (*dst = *in++); dst++);
3244 *dst = 0;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003245
3246 LIST_ADDQ(list, &tcpcheck->list);
3247 return 1;
3248}
3249
3250static int enqueue_one_email_alert(struct email_alertq *q, const char *msg)
3251{
3252 struct email_alert *alert = NULL;
3253 struct tcpcheck_rule *tcpcheck;
3254 struct check *check = &q->check;
3255 struct proxy *p = check->server->proxy;
3256
3257 alert = calloc(1, sizeof *alert);
3258 if (!alert) {
3259 goto error;
3260 }
3261 LIST_INIT(&alert->tcpcheck_rules);
3262
3263 tcpcheck = calloc(1, sizeof *tcpcheck);
3264 if (!tcpcheck)
3265 goto error;
3266 tcpcheck->action = TCPCHK_ACT_CONNECT;
3267 LIST_ADDQ(&alert->tcpcheck_rules, &tcpcheck->list);
3268
3269 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "220 "))
3270 goto error;
3271
3272 {
3273 const char * const strs[4] = { "EHLO ", p->email_alert.myhostname, "\r\n" };
3274 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3275 goto error;
3276 }
3277
3278 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3279 goto error;
3280
3281 {
3282 const char * const strs[4] = { "MAIL FROM:<", p->email_alert.from, ">\r\n" };
3283 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3284 goto error;
3285 }
3286
3287 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3288 goto error;
3289
3290 {
3291 const char * const strs[4] = { "RCPT TO:<", p->email_alert.to, ">\r\n" };
3292 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3293 goto error;
3294 }
3295
3296 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3297 goto error;
3298
3299 {
3300 const char * const strs[2] = { "DATA\r\n" };
3301 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3302 goto error;
3303 }
3304
3305 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "354 "))
3306 goto error;
3307
3308 {
3309 struct tm tm;
3310 char datestr[48];
3311 const char * const strs[18] = {
Pieter Baauw5e0964e2016-02-13 16:27:35 +01003312 "From: ", p->email_alert.from, "\r\n",
3313 "To: ", p->email_alert.to, "\r\n",
3314 "Date: ", datestr, "\r\n",
3315 "Subject: [HAproxy Alert] ", msg, "\r\n",
3316 "\r\n",
3317 msg, "\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003318 "\r\n",
Pieter Baauwed35c372015-07-22 19:51:54 +02003319 ".\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003320 NULL
3321 };
3322
3323 get_localtime(date.tv_sec, &tm);
3324
3325 if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %T %z (%Z)", &tm) == 0) {
3326 goto error;
3327 }
3328
3329 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3330 goto error;
3331 }
3332
3333 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3334 goto error;
3335
3336 {
3337 const char * const strs[2] = { "QUIT\r\n" };
3338 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3339 goto error;
3340 }
3341
3342 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "221 "))
3343 goto error;
3344
3345 if (!check->task) {
3346 struct task *t;
3347
3348 if ((t = task_new()) == NULL)
3349 goto error;
3350
3351 check->task = t;
3352 t->process = process_email_alert;
3353 t->context = check;
3354
3355 /* check this in one ms */
3356 t->expire = tick_add(now_ms, MS_TO_TICKS(1));
3357 check->start = now;
3358 task_queue(t);
3359 }
3360
3361 LIST_ADDQ(&q->email_alerts, &alert->list);
3362
3363 return 1;
3364
3365error:
3366 email_alert_free(alert);
3367 return 0;
3368}
3369
3370static void enqueue_email_alert(struct proxy *p, const char *msg)
3371{
3372 int i;
3373 struct mailer *mailer;
3374
3375 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3376 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
3377 if (!enqueue_one_email_alert(&p->email_alert.queues[i], msg)) {
3378 Alert("Email alert [%s] could not be enqueued: out of memory\n", p->id);
3379 return;
3380 }
3381 }
3382
3383 return;
3384}
3385
3386/*
3387 * Send email alert if configured.
3388 */
Simon Horman64e34162015-02-06 11:11:57 +09003389void send_email_alert(struct server *s, int level, const char *format, ...)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003390{
3391 va_list argp;
3392 char buf[1024];
3393 int len;
3394 struct proxy *p = s->proxy;
3395
Simon Horman64e34162015-02-06 11:11:57 +09003396 if (!p->email_alert.mailers.m || level > p->email_alert.level ||
3397 format == NULL || !init_email_alert_checks(s))
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003398 return;
3399
3400 va_start(argp, format);
3401 len = vsnprintf(buf, sizeof(buf), format, argp);
3402 va_end(argp);
3403
3404 if (len < 0) {
Cyril Bontéb65e0332015-12-04 03:07:08 +01003405 Alert("Email alert [%s] could not format message\n", p->id);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003406 return;
3407 }
3408
3409 enqueue_email_alert(p, buf);
3410}
3411
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003412/*
3413 * Return value:
3414 * the port to be used for the health check
3415 * 0 in case no port could be found for the check
3416 */
3417int srv_check_healthcheck_port(struct check *chk)
3418{
3419 int i = 0;
3420 struct server *srv = NULL;
3421
3422 srv = chk->server;
3423
3424 /* If neither a port nor an addr was specified and no check transport
3425 * layer is forced, then the transport layer used by the checks is the
3426 * same as for the production traffic. Otherwise we use raw_sock by
3427 * default, unless one is specified.
3428 */
3429 if (!chk->port && !is_addr(&chk->addr)) {
3430#ifdef USE_OPENSSL
3431 chk->use_ssl |= (srv->use_ssl || (srv->proxy->options & PR_O_TCPCHK_SSL));
3432#endif
3433 chk->send_proxy |= (srv->pp_opts);
3434 }
3435
3436 /* by default, we use the health check port ocnfigured */
3437 if (chk->port > 0)
3438 return chk->port;
3439
3440 /* try to get the port from check_core.addr if check.port not set */
3441 i = get_host_port(&chk->addr);
3442 if (i > 0)
3443 return i;
3444
3445 /* try to get the port from server address */
3446 /* prevent MAPPORTS from working at this point, since checks could
3447 * not be performed in such case (MAPPORTS impose a relative ports
3448 * based on live traffic)
3449 */
3450 if (srv->flags & SRV_F_MAPPORTS)
3451 return 0;
3452 i = get_host_port(&srv->addr); /* by default */
3453 if (i > 0)
3454 return i;
3455
3456 return 0;
3457}
3458
Willy Tarreau865c5142016-12-21 20:04:48 +01003459__attribute__((constructor))
3460static void __check_init(void)
3461{
3462 hap_register_post_check(start_checks);
3463}
3464
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003465
Willy Tarreaubd741542010-03-16 18:46:54 +01003466/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003467 * Local variables:
3468 * c-indent-level: 8
3469 * c-basic-offset: 8
3470 * End:
3471 */