blob: 6972f9cb6e875ab9713a7b5e761bcf18fb75dd16 [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>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020040#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010041#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042
43#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020044#include <proto/checks.h>
William Lallemand9ed62032016-11-21 17:49:11 +010045#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/fd.h>
47#include <proto/log.h>
48#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020049#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010050#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010051#include <proto/proto_tcp.h>
Baptiste Assmann69e273f2013-12-11 00:52:19 +010052#include <proto/protocol.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010053#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/server.h>
Willy Tarreau48d6bf22016-06-21 16:27:34 +020055#include <proto/signal.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010056#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020058#include <proto/log.h>
59#include <proto/dns.h>
60#include <proto/proto_udp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061
Olivier Houchard9130a962017-10-17 17:33:43 +020062#ifdef USE_OPENSSL
63#include <proto/ssl_sock.h>
64#endif /* USE_OPENSSL */
65
Willy Tarreaubd741542010-03-16 18:46:54 +010066static int httpchk_expect(struct server *s, int done);
Simon Hormane16c1b32015-01-30 11:22:57 +090067static int tcpcheck_get_step_id(struct check *);
Baptiste Assmann22b09d22015-05-01 08:03:04 +020068static char * tcpcheck_get_step_comment(struct check *, int);
Willy Tarreau6bdcab02017-10-04 18:41:00 +020069static int tcpcheck_main(struct check *);
Willy Tarreaubd741542010-03-16 18:46:54 +010070
Simon Horman63a4a822012-03-19 07:24:41 +090071static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010072 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
73 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020074 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020075
Willy Tarreau23964182014-05-20 20:56:30 +020076 /* Below we have finished checks */
77 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010078 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010079
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010080 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020081
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010082 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
83 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
84 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020085
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010086 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
87 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
88 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020089
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010090 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
91 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020092
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020093 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020094
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010095 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
96 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
97 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +090098
99 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
100 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +0200101 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200102};
103
Cyril Bontéac92a062014-12-27 22:28:38 +0100104const struct extcheck_env extcheck_envs[EXTCHK_SIZE] = {
105 [EXTCHK_PATH] = { "PATH", EXTCHK_SIZE_EVAL_INIT },
106 [EXTCHK_HAPROXY_PROXY_NAME] = { "HAPROXY_PROXY_NAME", EXTCHK_SIZE_EVAL_INIT },
107 [EXTCHK_HAPROXY_PROXY_ID] = { "HAPROXY_PROXY_ID", EXTCHK_SIZE_EVAL_INIT },
108 [EXTCHK_HAPROXY_PROXY_ADDR] = { "HAPROXY_PROXY_ADDR", EXTCHK_SIZE_EVAL_INIT },
109 [EXTCHK_HAPROXY_PROXY_PORT] = { "HAPROXY_PROXY_PORT", EXTCHK_SIZE_EVAL_INIT },
110 [EXTCHK_HAPROXY_SERVER_NAME] = { "HAPROXY_SERVER_NAME", EXTCHK_SIZE_EVAL_INIT },
111 [EXTCHK_HAPROXY_SERVER_ID] = { "HAPROXY_SERVER_ID", EXTCHK_SIZE_EVAL_INIT },
112 [EXTCHK_HAPROXY_SERVER_ADDR] = { "HAPROXY_SERVER_ADDR", EXTCHK_SIZE_EVAL_INIT },
113 [EXTCHK_HAPROXY_SERVER_PORT] = { "HAPROXY_SERVER_PORT", EXTCHK_SIZE_EVAL_INIT },
114 [EXTCHK_HAPROXY_SERVER_MAXCONN] = { "HAPROXY_SERVER_MAXCONN", EXTCHK_SIZE_EVAL_INIT },
115 [EXTCHK_HAPROXY_SERVER_CURCONN] = { "HAPROXY_SERVER_CURCONN", EXTCHK_SIZE_ULONG },
116};
117
Simon Horman63a4a822012-03-19 07:24:41 +0900118static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100119 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
120
121 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
122 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
123
124 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
125 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
126 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
127 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
128
129 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
130 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
131 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
132};
133
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200134/*
135 * Convert check_status code to description
136 */
137const char *get_check_status_description(short check_status) {
138
139 const char *desc;
140
141 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200142 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200143 else
144 desc = NULL;
145
146 if (desc && *desc)
147 return desc;
148 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200149 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200150}
151
152/*
153 * Convert check_status code to short info
154 */
155const char *get_check_status_info(short check_status) {
156
157 const char *info;
158
159 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200160 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200161 else
162 info = NULL;
163
164 if (info && *info)
165 return info;
166 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200167 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200168}
169
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100170const char *get_analyze_status(short analyze_status) {
171
172 const char *desc;
173
174 if (analyze_status < HANA_STATUS_SIZE)
175 desc = analyze_statuses[analyze_status].desc;
176 else
177 desc = NULL;
178
179 if (desc && *desc)
180 return desc;
181 else
182 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
183}
184
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200185/*
Simon Horman4a741432013-02-23 15:35:38 +0900186 * Set check->status, update check->duration and fill check->result with
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200187 * an adequate CHK_RES_* value. The new check->health is computed based
188 * on the result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200189 *
190 * Show information in logs about failed health check if server is UP
191 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200192 */
Simon Horman4a741432013-02-23 15:35:38 +0900193static void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100194{
Simon Horman4a741432013-02-23 15:35:38 +0900195 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200196 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200197 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900198
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200199 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100200 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900201 check->desc[0] = '\0';
202 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200203 return;
204 }
205
Simon Horman4a741432013-02-23 15:35:38 +0900206 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200207 return;
208
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200209 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900210 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
211 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200212 } else
Simon Horman4a741432013-02-23 15:35:38 +0900213 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200214
Simon Horman4a741432013-02-23 15:35:38 +0900215 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200216 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900217 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200218
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100219 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900220 check->duration = -1;
221 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200222 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900223 check->duration = tv_ms_elapsed(&check->start, &now);
224 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200225 }
226
Willy Tarreau23964182014-05-20 20:56:30 +0200227 /* no change is expected if no state change occurred */
228 if (check->result == CHK_RES_NEUTRAL)
229 return;
230
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200231 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200232
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200233 switch (check->result) {
234 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200235 /* Failure to connect to the agent as a secondary check should not
236 * cause the server to be marked down.
237 */
238 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900239 (check->status >= HCHK_STATUS_L57DATA)) &&
Willy Tarreau12634e12014-05-23 11:32:36 +0200240 (check->health >= check->rise)) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200241 s->counters.failed_checks++;
242 report = 1;
243 check->health--;
244 if (check->health < check->rise)
245 check->health = 0;
246 }
247 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200248
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200249 case CHK_RES_PASSED:
250 case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */
251 if ((check->health < check->rise + check->fall - 1) &&
252 (check->result == CHK_RES_PASSED || check->health > 0)) {
253 report = 1;
254 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200255
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200256 if (check->health >= check->rise)
257 check->health = check->rise + check->fall - 1; /* OK now */
258 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200259
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200260 /* clear consecutive_errors if observing is enabled */
261 if (s->onerror)
262 s->consecutive_errors = 0;
263 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100264
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200265 default:
266 break;
267 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200268
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200269 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
270 (status != prev_status || report)) {
271 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200272 "%s check for %sserver %s/%s %s%s",
273 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200274 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100275 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100276 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200277 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200278
Emeric Brun5a133512017-10-19 14:42:30 +0200279 srv_append_status(&trash, s, check, -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200280
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100281 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200282 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
283 (check->health >= check->rise) ? check->fall : check->rise,
284 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200285
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100286 Warning("%s.\n", trash.str);
287 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman7ea9be02015-04-30 13:10:33 +0900288 send_email_alert(s, LOG_INFO, "%s", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200289 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200290}
291
Willy Tarreau4eec5472014-05-20 22:32:27 +0200292/* Marks the check <check>'s server down if the current check is already failed
293 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200294 */
Willy Tarreau4eec5472014-05-20 22:32:27 +0200295static void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200296{
Simon Horman4a741432013-02-23 15:35:38 +0900297 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900298
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200299 /* The agent secondary check should only cause a server to be marked
300 * as down if check->status is HCHK_STATUS_L7STS, which indicates
301 * that the agent returned "fail", "stopped" or "down".
302 * The implication here is that failure to connect to the agent
303 * as a secondary check should not cause the server to be marked
304 * down. */
305 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
306 return;
307
Willy Tarreau4eec5472014-05-20 22:32:27 +0200308 if (check->health > 0)
309 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100310
Willy Tarreau4eec5472014-05-20 22:32:27 +0200311 /* We only report a reason for the check if we did not do so previously */
Emeric Brun5a133512017-10-19 14:42:30 +0200312 srv_set_stopped(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200313}
314
Willy Tarreauaf549582014-05-16 17:37:50 +0200315/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200316 * it isn't in maintenance, it is not tracking a down server and other checks
317 * comply. The rule is simple : by default, a server is up, unless any of the
318 * following conditions is true :
319 * - health check failed (check->health < rise)
320 * - agent check failed (agent->health < rise)
321 * - the server tracks a down server (track && track->state == STOPPED)
322 * Note that if the server has a slowstart, it will switch to STARTING instead
323 * of RUNNING. Also, only the health checks support the nolb mode, so the
324 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200325 */
Willy Tarreau3e048382014-05-21 10:30:54 +0200326static void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200327{
Simon Horman4a741432013-02-23 15:35:38 +0900328 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100329
Emeric Brun52a91d32017-08-31 14:41:55 +0200330 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200331 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100332
Emeric Brun52a91d32017-08-31 14:41:55 +0200333 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200334 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100335
Willy Tarreau3e048382014-05-21 10:30:54 +0200336 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
337 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100338
Willy Tarreau3e048382014-05-21 10:30:54 +0200339 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
340 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200341
Emeric Brun52a91d32017-08-31 14:41:55 +0200342 if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING)
Willy Tarreau3e048382014-05-21 10:30:54 +0200343 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100344
Emeric Brun5a133512017-10-19 14:42:30 +0200345 srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100346}
347
Willy Tarreaudb58b792014-05-21 13:57:23 +0200348/* Marks the check <check> as valid and tries to set its server into stopping mode
349 * if it was running or starting, and provided it isn't in maintenance and other
350 * checks comply. The conditions for the server to be marked in stopping mode are
351 * the same as for it to be turned up. Also, only the health checks support the
352 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200353 */
Willy Tarreaudb58b792014-05-21 13:57:23 +0200354static void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200355{
Simon Horman4a741432013-02-23 15:35:38 +0900356 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100357
Emeric Brun52a91d32017-08-31 14:41:55 +0200358 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200359 return;
360
Willy Tarreaudb58b792014-05-21 13:57:23 +0200361 if (check->state & CHK_ST_AGENT)
362 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100363
Emeric Brun52a91d32017-08-31 14:41:55 +0200364 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreaudb58b792014-05-21 13:57:23 +0200365 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100366
Willy Tarreaudb58b792014-05-21 13:57:23 +0200367 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
368 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100369
Willy Tarreaudb58b792014-05-21 13:57:23 +0200370 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
371 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100372
Emeric Brun5a133512017-10-19 14:42:30 +0200373 srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100374}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200375
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100376/* note: use health_adjust() only, which first checks that the observe mode is
377 * enabled.
378 */
379void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100380{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100381 int failed;
382 int expire;
383
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100384 if (s->observe >= HANA_OBS_SIZE)
385 return;
386
Willy Tarreaubb956662013-01-24 00:37:39 +0100387 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100388 return;
389
390 switch (analyze_statuses[status].lr[s->observe - 1]) {
391 case 1:
392 failed = 1;
393 break;
394
395 case 2:
396 failed = 0;
397 break;
398
399 default:
400 return;
401 }
402
403 if (!failed) {
404 /* good: clear consecutive_errors */
405 s->consecutive_errors = 0;
406 return;
407 }
408
409 s->consecutive_errors++;
410
411 if (s->consecutive_errors < s->consecutive_errors_limit)
412 return;
413
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100414 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
415 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100416
417 switch (s->onerror) {
418 case HANA_ONERR_FASTINTER:
419 /* force fastinter - nothing to do here as all modes force it */
420 break;
421
422 case HANA_ONERR_SUDDTH:
423 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900424 if (s->check.health > s->check.rise)
425 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100426
427 /* no break - fall through */
428
429 case HANA_ONERR_FAILCHK:
430 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900431 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200432 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100433 break;
434
435 case HANA_ONERR_MARKDWN:
436 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900437 s->check.health = s->check.rise;
Simon Horman4a741432013-02-23 15:35:38 +0900438 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200439 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100440 break;
441
442 default:
443 /* write a warning? */
444 break;
445 }
446
447 s->consecutive_errors = 0;
448 s->counters.failed_hana++;
449
Simon Horman66183002013-02-23 10:16:43 +0900450 if (s->check.fastinter) {
451 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300452 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200453 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300454 /* requeue check task with new expire */
455 task_queue(s->check.task);
456 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100457 }
458}
459
Willy Tarreaua1dab552014-04-14 15:04:54 +0200460static int httpchk_build_status_header(struct server *s, char *buffer, int size)
Willy Tarreauef781042010-01-27 11:53:01 +0100461{
462 int sv_state;
463 int ratio;
464 int hlen = 0;
Joseph Lynch514061c2015-01-15 17:52:59 -0800465 char addr[46];
466 char port[6];
Willy Tarreauef781042010-01-27 11:53:01 +0100467 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
468 "UP %d/%d", "UP",
469 "NOLB %d/%d", "NOLB",
470 "no check" };
471
472 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
473 hlen += 24;
474
Willy Tarreauff5ae352013-12-11 20:36:34 +0100475 if (!(s->check.state & CHK_ST_ENABLED))
476 sv_state = 6;
Emeric Brun52a91d32017-08-31 14:41:55 +0200477 else if (s->cur_state != SRV_ST_STOPPED) {
Simon Horman58c32972013-11-25 10:46:38 +0900478 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100479 sv_state = 3; /* UP */
480 else
481 sv_state = 2; /* going down */
482
Emeric Brun52a91d32017-08-31 14:41:55 +0200483 if (s->cur_state == SRV_ST_STOPPING)
Willy Tarreauef781042010-01-27 11:53:01 +0100484 sv_state += 2;
485 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900486 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100487 sv_state = 1; /* going up */
488 else
489 sv_state = 0; /* DOWN */
490 }
491
Willy Tarreaua1dab552014-04-14 15:04:54 +0200492 hlen += snprintf(buffer + hlen, size - hlen,
Willy Tarreauef781042010-01-27 11:53:01 +0100493 srv_hlt_st[sv_state],
Emeric Brun52a91d32017-08-31 14:41:55 +0200494 (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
495 (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100496
Joseph Lynch514061c2015-01-15 17:52:59 -0800497 addr_to_str(&s->addr, addr, sizeof(addr));
Willy Tarreau04276f32017-01-06 17:41:29 +0100498 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
499 snprintf(port, sizeof(port), "%u", s->svc_port);
500 else
501 *port = 0;
Joseph Lynch514061c2015-01-15 17:52:59 -0800502
503 hlen += snprintf(buffer + hlen, size - hlen, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
504 addr, port, s->proxy->id, s->id,
Willy Tarreauef781042010-01-27 11:53:01 +0100505 global.node,
Emeric Brun52a91d32017-08-31 14:41:55 +0200506 (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
Willy Tarreauef781042010-01-27 11:53:01 +0100507 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
508 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
509 s->nbpend);
510
Emeric Brun52a91d32017-08-31 14:41:55 +0200511 if ((s->cur_state == SRV_ST_STARTING) &&
Willy Tarreauef781042010-01-27 11:53:01 +0100512 now.tv_sec < s->last_change + s->slowstart &&
513 now.tv_sec >= s->last_change) {
514 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
Willy Tarreaua1dab552014-04-14 15:04:54 +0200515 hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio);
Willy Tarreauef781042010-01-27 11:53:01 +0100516 }
517
518 buffer[hlen++] = '\r';
519 buffer[hlen++] = '\n';
520
521 return hlen;
522}
523
Willy Tarreau20a18342013-12-05 00:31:46 +0100524/* Check the connection. If an error has already been reported or the socket is
525 * closed, keep errno intact as it is supposed to contain the valid error code.
526 * If no error is reported, check the socket's error queue using getsockopt().
527 * Warning, this must be done only once when returning from poll, and never
528 * after an I/O error was attempted, otherwise the error queue might contain
529 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
530 * socket. Returns non-zero if an error was reported, zero if everything is
531 * clean (including a properly closed socket).
532 */
533static int retrieve_errno_from_socket(struct connection *conn)
534{
535 int skerr;
536 socklen_t lskerr = sizeof(skerr);
537
538 if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl))
539 return 1;
540
Willy Tarreau3c728722014-01-23 13:50:42 +0100541 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100542 return 0;
543
Willy Tarreau585744b2017-08-24 14:31:19 +0200544 if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
Willy Tarreau20a18342013-12-05 00:31:46 +0100545 errno = skerr;
546
547 if (errno == EAGAIN)
548 errno = 0;
549
550 if (!errno) {
551 /* we could not retrieve an error, that does not mean there is
552 * none. Just don't change anything and only report the prior
553 * error if any.
554 */
555 if (conn->flags & CO_FL_ERROR)
556 return 1;
557 else
558 return 0;
559 }
560
561 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
562 return 1;
563}
564
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100565/* Try to collect as much information as possible on the connection status,
566 * and adjust the server status accordingly. It may make use of <errno_bck>
567 * if non-null when the caller is absolutely certain of its validity (eg:
568 * checked just after a syscall). If the caller doesn't have a valid errno,
569 * it can pass zero, and retrieve_errno_from_socket() will be called to try
570 * to extract errno from the socket. If no error is reported, it will consider
571 * the <expired> flag. This is intended to be used when a connection error was
572 * reported in conn->flags or when a timeout was reported in <expired>. The
573 * function takes care of not updating a server status which was already set.
574 * All situations where at least one of <expired> or CO_FL_ERROR are set
575 * produce a status.
576 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200577static void chk_report_conn_err(struct check *check, int errno_bck, int expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100578{
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200579 struct connection *conn = check->conn;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100580 const char *err_msg;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200581 struct chunk *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200582 int step;
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200583 char *comment;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100584
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100585 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100586 return;
587
588 errno = errno_bck;
Willy Tarreau00149122017-10-04 18:05:01 +0200589 if (conn && (!errno || errno == EAGAIN))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100590 retrieve_errno_from_socket(conn);
591
Willy Tarreau00149122017-10-04 18:05:01 +0200592 if (conn && !(conn->flags & CO_FL_ERROR) && !expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100593 return;
594
595 /* we'll try to build a meaningful error message depending on the
596 * context of the error possibly present in conn->err_code, and the
597 * socket error possibly collected above. This is useful to know the
598 * exact step of the L6 layer (eg: SSL handshake).
599 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200600 chk = get_trash_chunk();
601
602 if (check->type == PR_O2_TCPCHK_CHK) {
Simon Hormane16c1b32015-01-30 11:22:57 +0900603 step = tcpcheck_get_step_id(check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200604 if (!step)
605 chunk_printf(chk, " at initial connection step of tcp-check");
606 else {
607 chunk_printf(chk, " at step %d of tcp-check", step);
608 /* we were looking for a string */
609 if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_CONNECT) {
610 if (check->last_started_step->port)
611 chunk_appendf(chk, " (connect port %d)" ,check->last_started_step->port);
612 else
613 chunk_appendf(chk, " (connect)");
614 }
615 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_EXPECT) {
616 if (check->last_started_step->string)
Baptiste Assmann96a5c9b2015-05-01 08:09:29 +0200617 chunk_appendf(chk, " (expect string '%s')", check->last_started_step->string);
Willy Tarreau213c6782014-10-02 14:51:02 +0200618 else if (check->last_started_step->expect_regex)
619 chunk_appendf(chk, " (expect regex)");
620 }
621 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_SEND) {
622 chunk_appendf(chk, " (send)");
623 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200624
625 comment = tcpcheck_get_step_comment(check, step);
626 if (comment)
627 chunk_appendf(chk, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200628 }
629 }
630
Willy Tarreau00149122017-10-04 18:05:01 +0200631 if (conn && conn->err_code) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100632 if (errno && errno != EAGAIN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200633 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100634 else
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200635 chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100636 err_msg = trash.str;
637 }
638 else {
639 if (errno && errno != EAGAIN) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200640 chunk_printf(&trash, "%s%s", strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100641 err_msg = trash.str;
642 }
643 else {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200644 err_msg = chk->str;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100645 }
646 }
647
Willy Tarreau00149122017-10-04 18:05:01 +0200648 if (check->state & CHK_ST_PORT_MISS) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200649 /* NOTE: this is reported after <fall> tries */
650 chunk_printf(chk, "No port available for the TCP connection");
651 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
652 }
653
Willy Tarreau00149122017-10-04 18:05:01 +0200654 if (!conn) {
655 /* connection allocation error before the connection was established */
656 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
657 }
658 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100659 /* L4 not established (yet) */
660 if (conn->flags & CO_FL_ERROR)
661 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
662 else if (expired)
663 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200664
665 /*
666 * might be due to a server IP change.
667 * Let's trigger a DNS resolution if none are currently running.
668 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200669 dns_trigger_resolution(check->server->dns_requester);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200670
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100671 }
672 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
673 /* L6 not established (yet) */
674 if (conn->flags & CO_FL_ERROR)
675 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
676 else if (expired)
677 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
678 }
679 else if (conn->flags & CO_FL_ERROR) {
680 /* I/O error after connection was established and before we could diagnose */
681 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
682 }
683 else if (expired) {
684 /* connection established but expired check */
685 if (check->type == PR_O2_SSL3_CHK)
686 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
687 else /* HTTP, SMTP, ... */
688 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
689 }
690
691 return;
692}
693
Willy Tarreaubaaee002006-06-26 02:48:02 +0200694/*
695 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200696 * the connection acknowledgement. If the proxy requires L7 health-checks,
697 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900698 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200699 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200700static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200701{
Simon Horman4a741432013-02-23 15:35:38 +0900702 struct check *check = conn->owner;
703 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900704 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200705
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100706 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100707 goto out_wakeup;
708
Willy Tarreau310987a2014-01-22 19:46:33 +0100709 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100710 return;
711
Willy Tarreau20a18342013-12-05 00:31:46 +0100712 if (retrieve_errno_from_socket(conn)) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200713 chk_report_conn_err(check, errno, 0);
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200714 __conn_xprt_stop_both(conn);
Willy Tarreau20a18342013-12-05 00:31:46 +0100715 goto out_wakeup;
716 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100717
Willy Tarreaubbae3f02017-08-30 09:59:52 +0200718 if (conn->flags & CO_FL_SOCK_WR_SH) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100719 /* if the output is closed, we can't do anything */
720 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200721 chk_report_conn_err(check, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100722 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200723 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200724
Willy Tarreau06559ac2013-12-05 01:53:08 +0100725 /* here, we know that the connection is established. That's enough for
726 * a pure TCP check.
727 */
728 if (!check->type)
729 goto out_wakeup;
730
Willy Tarreauc09572f2017-10-04 11:58:22 +0200731 /* wake() will take care of calling tcpcheck_main() */
732 if (check->type == PR_O2_TCPCHK_CHK)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200733 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200734
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100735 if (check->bo->o) {
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100736 conn->xprt->snd_buf(conn, check->bo, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100737 if (conn->flags & CO_FL_ERROR) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200738 chk_report_conn_err(check, errno, 0);
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200739 __conn_xprt_stop_both(conn);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100740 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200741 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100742 if (check->bo->o)
743 return;
744 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200745
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100746 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
747 if (s->proxy->timeout.check) {
748 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
749 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200750 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100751 goto out_nowake;
752
Willy Tarreau83749182007-04-15 20:56:27 +0200753 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200754 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200755 out_nowake:
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200756 __conn_xprt_stop_send(conn); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200757}
758
Willy Tarreaubaaee002006-06-26 02:48:02 +0200759/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200760 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200761 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900762 * set_server_check_status() to update check->status, check->duration
763 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200764
765 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
766 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
767 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
768 * response to an SSL HELLO (the principle is that this is enough to
769 * distinguish between an SSL server and a pure TCP relay). All other cases will
770 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
771 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200772 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200773static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200774{
Simon Horman4a741432013-02-23 15:35:38 +0900775 struct check *check = conn->owner;
776 struct server *s = check->server;
777 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200778 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100779 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200780 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200781
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100782 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +0200783 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200784
Willy Tarreau310987a2014-01-22 19:46:33 +0100785 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200786 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200787
Willy Tarreauc09572f2017-10-04 11:58:22 +0200788 /* wake() will take care of calling tcpcheck_main() */
789 if (check->type == PR_O2_TCPCHK_CHK)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200790 return;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200791
Willy Tarreau83749182007-04-15 20:56:27 +0200792 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
793 * but the connection was closed on the remote end. Fortunately, recv still
794 * works correctly and we don't need to do the getsockopt() on linux.
795 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000796
797 /* Set buffer to point to the end of the data already read, and check
798 * that there is free space remaining. If the buffer is full, proceed
799 * with running the checks without attempting another socket read.
800 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000801
Willy Tarreau03938182010-03-17 21:52:07 +0100802 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +0000803
Simon Horman4a741432013-02-23 15:35:38 +0900804 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreau54e917c2017-08-30 07:35:35 +0200805 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +0100806 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +0900807 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +0200808 /* Report network errors only if we got no other data. Otherwise
809 * we'll let the upper layers decide whether the response is OK
810 * or not. It is very common that an RST sent by the server is
811 * reported as an error just after the last data chunk.
812 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200813 chk_report_conn_err(check, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +0100814 goto out_wakeup;
815 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200816 }
817
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100818
Willy Tarreau03938182010-03-17 21:52:07 +0100819 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +0900820 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +0100821 */
Simon Horman4a741432013-02-23 15:35:38 +0900822 if (check->bi->i < check->bi->size)
823 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100824 else {
Simon Horman4a741432013-02-23 15:35:38 +0900825 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100826 done = 1; /* buffer full, don't wait for more data */
827 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200828
Nick Chalk57b1bf72010-03-16 15:50:46 +0000829 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +0900830 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +0200831 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900832 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100833 goto wait_more_data;
834
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100835 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +0900836 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
837 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
838 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
839 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
840 !isdigit((unsigned char) *(check->bi->data + 11))) {
841 cut_crlf(check->bi->data);
842 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200843
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100844 goto out_wakeup;
845 }
846
Simon Horman4a741432013-02-23 15:35:38 +0900847 check->code = str2uic(check->bi->data + 9);
848 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +0000849
Willy Tarreaubd741542010-03-16 18:46:54 +0100850 if ((s->proxy->options & PR_O_DISABLE404) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200851 (s->next_state != SRV_ST_STOPPED) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000852 /* 404 may be accepted as "stopping" only if the server was up */
853 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900854 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000855 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100856 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
857 /* Run content verification check... We know we have at least 13 chars */
858 if (!httpchk_expect(s, done))
859 goto wait_more_data;
860 }
861 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +0900862 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +0100863 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900864 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +0100865 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000866 else {
867 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900868 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000869 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200870 break;
871
872 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900873 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +0100874 goto wait_more_data;
875
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100876 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +0900877 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
878 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200879 else
Simon Horman4a741432013-02-23 15:35:38 +0900880 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200881 break;
882
883 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900884 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100885 goto wait_more_data;
886
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200887 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +0900888 if ((check->bi->i < strlen("000\r")) ||
889 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
890 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
891 !isdigit((unsigned char) *(check->bi->data + 2))) {
892 cut_crlf(check->bi->data);
893 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200894
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200895 goto out_wakeup;
896 }
897
Simon Horman4a741432013-02-23 15:35:38 +0900898 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200899
Simon Horman4a741432013-02-23 15:35:38 +0900900 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200901 cut_crlf(desc);
902
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100903 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +0900904 if (*check->bi->data == '2')
905 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200906 else
Simon Horman4a741432013-02-23 15:35:38 +0900907 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200908 break;
909
Simon Hormana2b9dad2013-02-12 10:45:54 +0900910 case PR_O2_LB_AGENT_CHK: {
Willy Tarreau81f5d942013-12-09 20:51:51 +0100911 int status = HCHK_STATUS_CHECKED;
912 const char *hs = NULL; /* health status */
913 const char *as = NULL; /* admin status */
914 const char *ps = NULL; /* performance status */
Nenad Merdanovic174dd372016-04-24 23:10:06 +0200915 const char *cs = NULL; /* maxconn */
Willy Tarreau81f5d942013-12-09 20:51:51 +0100916 const char *err = NULL; /* first error to report */
917 const char *wrn = NULL; /* first warning to report */
918 char *cmd, *p;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900919
Willy Tarreau81f5d942013-12-09 20:51:51 +0100920 /* We're getting an agent check response. The agent could
921 * have been disabled in the mean time with a long check
922 * still pending. It is important that we ignore the whole
923 * response.
924 */
925 if (!(check->server->agent.state & CHK_ST_ENABLED))
926 break;
927
928 /* The agent supports strings made of a single line ended by the
929 * first CR ('\r') or LF ('\n'). This line is composed of words
930 * delimited by spaces (' '), tabs ('\t'), or commas (','). The
931 * line may optionally contained a description of a state change
932 * after a sharp ('#'), which is only considered if a health state
933 * is announced.
934 *
935 * Words may be composed of :
936 * - a numeric weight suffixed by the percent character ('%').
937 * - a health status among "up", "down", "stopped", and "fail".
938 * - an admin status among "ready", "drain", "maint".
939 *
940 * These words may appear in any order. If multiple words of the
941 * same category appear, the last one wins.
942 */
943
Willy Tarreau9809b782013-12-11 21:40:11 +0100944 p = check->bi->data;
945 while (*p && *p != '\n' && *p != '\r')
946 p++;
947
948 if (!*p) {
949 if (!done)
950 goto wait_more_data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900951
Willy Tarreau9809b782013-12-11 21:40:11 +0100952 /* at least inform the admin that the agent is mis-behaving */
953 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
954 break;
955 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100956
Willy Tarreau9809b782013-12-11 21:40:11 +0100957 *p = 0;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100958 cmd = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900959
Willy Tarreau81f5d942013-12-09 20:51:51 +0100960 while (*cmd) {
961 /* look for next word */
962 if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') {
963 cmd++;
964 continue;
965 }
Simon Horman671b6f02013-11-25 10:46:39 +0900966
Willy Tarreau81f5d942013-12-09 20:51:51 +0100967 if (*cmd == '#') {
968 /* this is the beginning of a health status description,
969 * skip the sharp and blanks.
970 */
971 cmd++;
972 while (*cmd == '\t' || *cmd == ' ')
973 cmd++;
Simon Horman671b6f02013-11-25 10:46:39 +0900974 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900975 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100976
977 /* find the end of the word so that we have a null-terminated
978 * word between <cmd> and <p>.
979 */
980 p = cmd + 1;
981 while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',')
982 p++;
983 if (*p)
984 *p++ = 0;
985
986 /* first, health statuses */
987 if (strcasecmp(cmd, "up") == 0) {
988 check->health = check->rise + check->fall - 1;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900989 status = HCHK_STATUS_L7OKD;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100990 hs = cmd;
991 }
992 else if (strcasecmp(cmd, "down") == 0) {
993 check->health = 0;
994 status = HCHK_STATUS_L7STS;
995 hs = cmd;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900996 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100997 else if (strcasecmp(cmd, "stopped") == 0) {
998 check->health = 0;
999 status = HCHK_STATUS_L7STS;
1000 hs = cmd;
1001 }
1002 else if (strcasecmp(cmd, "fail") == 0) {
1003 check->health = 0;
1004 status = HCHK_STATUS_L7STS;
1005 hs = cmd;
1006 }
1007 /* admin statuses */
1008 else if (strcasecmp(cmd, "ready") == 0) {
1009 as = cmd;
1010 }
1011 else if (strcasecmp(cmd, "drain") == 0) {
1012 as = cmd;
1013 }
1014 else if (strcasecmp(cmd, "maint") == 0) {
1015 as = cmd;
1016 }
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001017 /* try to parse a weight here and keep the last one */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001018 else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) {
1019 ps = cmd;
1020 }
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001021 /* try to parse a maxconn here */
1022 else if (strncasecmp(cmd, "maxconn:", strlen("maxconn:")) == 0) {
1023 cs = cmd;
1024 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001025 else {
1026 /* keep a copy of the first error */
1027 if (!err)
1028 err = cmd;
1029 }
1030 /* skip to next word */
1031 cmd = p;
1032 }
1033 /* here, cmd points either to \0 or to the beginning of a
1034 * description. Skip possible leading spaces.
1035 */
1036 while (*cmd == ' ' || *cmd == '\n')
1037 cmd++;
1038
1039 /* First, update the admin status so that we avoid sending other
1040 * possibly useless warnings and can also update the health if
1041 * present after going back up.
1042 */
1043 if (as) {
1044 if (strcasecmp(as, "drain") == 0)
1045 srv_adm_set_drain(check->server);
1046 else if (strcasecmp(as, "maint") == 0)
1047 srv_adm_set_maint(check->server);
1048 else
1049 srv_adm_set_ready(check->server);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001050 }
1051
Willy Tarreau81f5d942013-12-09 20:51:51 +01001052 /* now change weights */
1053 if (ps) {
1054 const char *msg;
1055
1056 msg = server_parse_weight_change_request(s, ps);
1057 if (!wrn || !*wrn)
1058 wrn = msg;
1059 }
1060
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001061 if (cs) {
1062 const char *msg;
1063
1064 cs += strlen("maxconn:");
1065
1066 msg = server_parse_maxconn_change_request(s, cs);
1067 if (!wrn || !*wrn)
1068 wrn = msg;
1069 }
1070
Willy Tarreau81f5d942013-12-09 20:51:51 +01001071 /* and finally health status */
1072 if (hs) {
1073 /* We'll report some of the warnings and errors we have
1074 * here. Down reports are critical, we leave them untouched.
1075 * Lack of report, or report of 'UP' leaves the room for
1076 * ERR first, then WARN.
Simon Hormana2b9dad2013-02-12 10:45:54 +09001077 */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001078 const char *msg = cmd;
1079 struct chunk *t;
1080
1081 if (!*msg || status == HCHK_STATUS_L7OKD) {
1082 if (err && *err)
1083 msg = err;
1084 else if (wrn && *wrn)
1085 msg = wrn;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001086 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001087
1088 t = get_trash_chunk();
1089 chunk_printf(t, "via agent : %s%s%s%s",
1090 hs, *msg ? " (" : "",
1091 msg, *msg ? ")" : "");
1092
1093 set_server_check_status(check, status, t->str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001094 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001095 else if (err && *err) {
1096 /* No status change but we'd like to report something odd.
1097 * Just report the current state and copy the message.
1098 */
1099 chunk_printf(&trash, "agent reports an error : %s", err);
1100 set_server_check_status(check, status/*check->status*/, trash.str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001101
Willy Tarreau81f5d942013-12-09 20:51:51 +01001102 }
1103 else if (wrn && *wrn) {
1104 /* No status change but we'd like to report something odd.
1105 * Just report the current state and copy the message.
1106 */
1107 chunk_printf(&trash, "agent warns : %s", wrn);
1108 set_server_check_status(check, status/*check->status*/, trash.str);
1109 }
1110 else
1111 set_server_check_status(check, status, NULL);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001112 break;
1113 }
1114
Willy Tarreau1620ec32011-08-06 17:05:02 +02001115 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001116 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001117 goto wait_more_data;
1118
Simon Horman4a741432013-02-23 15:35:38 +09001119 if (check->bi->data[0] == 'R') {
1120 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001121 }
1122 else {
Simon Horman4a741432013-02-23 15:35:38 +09001123 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1124 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001125 else
1126 desc = "PostgreSQL unknown error";
1127
Simon Horman4a741432013-02-23 15:35:38 +09001128 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001129 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001130 break;
1131
1132 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001133 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001134 goto wait_more_data;
1135
Simon Horman4a741432013-02-23 15:35:38 +09001136 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1137 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001138 }
1139 else {
Simon Horman4a741432013-02-23 15:35:38 +09001140 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001141 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001142 break;
1143
1144 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001145 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001146 goto wait_more_data;
1147
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001148 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001149 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001150 /* We set the MySQL Version in description for information purpose
1151 * FIXME : it can be cool to use MySQL Version for other purpose,
1152 * like mark as down old MySQL server.
1153 */
Simon Horman4a741432013-02-23 15:35:38 +09001154 if (check->bi->i > 51) {
1155 desc = ltrim(check->bi->data + 5, ' ');
1156 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001157 }
1158 else {
1159 if (!done)
1160 goto wait_more_data;
1161 /* it seems we have a OK packet but without a valid length,
1162 * it must be a protocol error
1163 */
Simon Horman4a741432013-02-23 15:35:38 +09001164 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001165 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001166 }
1167 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001168 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001169 desc = ltrim(check->bi->data + 7, ' ');
1170 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001171 }
1172 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001173 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1174 (((unsigned int) *(check->bi->data + 1)) << 8) +
1175 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001176
Simon Horman4a741432013-02-23 15:35:38 +09001177 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001178 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001179 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001180 /* We have only one MySQL packet and it is a Handshake Initialization packet
1181 * but we need to have a second packet to know if it is alright
1182 */
Simon Horman4a741432013-02-23 15:35:38 +09001183 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001184 goto wait_more_data;
1185 }
1186 else {
1187 /* We have only one packet and it is an Error packet,
1188 * an error message is attached, so we can display it
1189 */
Simon Horman4a741432013-02-23 15:35:38 +09001190 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001191 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001192 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001193 }
Simon Horman4a741432013-02-23 15:35:38 +09001194 } else if (check->bi->i > first_packet_len + 4) {
1195 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1196 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1197 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001198
Simon Horman4a741432013-02-23 15:35:38 +09001199 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001200 /* We have 2 packets and that's good */
1201 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001202 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001203 /* No error packet */
1204 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001205 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001206 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001207 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001208 }
1209 else {
1210 /* An error message is attached in the Error packet
1211 * so we can display it ! :)
1212 */
Simon Horman4a741432013-02-23 15:35:38 +09001213 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001214 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001215 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001216 }
1217 }
1218 }
1219 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001220 if (!done)
1221 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001222 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001223 * it must be a protocol error
1224 */
Simon Horman4a741432013-02-23 15:35:38 +09001225 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001226 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001227 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001228 }
1229 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001230 break;
1231
1232 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001233 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001234 goto wait_more_data;
1235
1236 /* Check if the server speaks LDAP (ASN.1/BER)
1237 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1238 * http://tools.ietf.org/html/rfc4511
1239 */
1240
1241 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1242 * LDAPMessage: 0x30: SEQUENCE
1243 */
Simon Horman4a741432013-02-23 15:35:38 +09001244 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1245 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001246 }
1247 else {
1248 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001249 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001250
1251 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1252 * messageID: 0x02 0x01 0x01: INTEGER 1
1253 * protocolOp: 0x61: bindResponse
1254 */
1255 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001256 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1257 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001258
1259 goto out_wakeup;
1260 }
1261
1262 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001263 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001264
1265 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1266 * ldapResult: 0x0a 0x01: ENUMERATION
1267 */
1268 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001269 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1270 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001271
1272 goto out_wakeup;
1273 }
1274
1275 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1276 * resultCode
1277 */
Simon Horman4a741432013-02-23 15:35:38 +09001278 check->code = *(check->bi->data + msglen + 9);
1279 if (check->code) {
1280 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 +02001281 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001282 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001283 }
1284 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001285 break;
1286
Christopher Fauletba7bc162016-11-07 21:07:38 +01001287 case PR_O2_SPOP_CHK: {
1288 unsigned int framesz;
1289 char err[HCHK_DESC_LEN];
1290
1291 if (!done && check->bi->i < 4)
1292 goto wait_more_data;
1293
1294 memcpy(&framesz, check->bi->data, 4);
1295 framesz = ntohl(framesz);
1296
1297 if (!done && check->bi->i < (4+framesz))
1298 goto wait_more_data;
1299
Christopher Faulet8ef75252017-02-20 22:56:03 +01001300 if (!spoe_handle_healthcheck_response(check->bi->data+4, framesz, err, HCHK_DESC_LEN-1))
Christopher Fauletba7bc162016-11-07 21:07:38 +01001301 set_server_check_status(check, HCHK_STATUS_L7OKD, "SPOA server is ok");
1302 else
1303 set_server_check_status(check, HCHK_STATUS_L7STS, err);
1304 break;
1305 }
1306
Willy Tarreau1620ec32011-08-06 17:05:02 +02001307 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001308 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001309 break;
1310 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001311
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001312 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001313 /* collect possible new errors */
1314 if (conn->flags & CO_FL_ERROR)
Willy Tarreaub5259bf2017-10-04 14:47:29 +02001315 chk_report_conn_err(check, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001316
Nick Chalk57b1bf72010-03-16 15:50:46 +00001317 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001318 *check->bi->data = '\0';
1319 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001320
Steven Davidovitz544d4812017-03-08 11:06:20 -08001321 /* Close the connection... We still attempt to nicely close if,
1322 * for instance, SSL needs to send a "close notify." Later, we perform
1323 * a hard close and reset the connection if some data are pending,
1324 * otherwise we end up with many TIME_WAITs and eat all the source port
1325 * range quickly. To avoid sending RSTs all the time, we first try to
1326 * drain pending data.
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001327 */
Olivier Houchard1a0545f2017-09-13 18:30:23 +02001328 __conn_xprt_stop_both(conn);
1329 conn_xprt_shutw(conn);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001330
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001331 /* OK, let's not stay here forever */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001332 if (check->result == CHK_RES_FAILED)
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001333 conn->flags |= CO_FL_ERROR;
1334
Willy Tarreaufdccded2008-08-29 18:19:04 +02001335 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001336 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001337
1338 wait_more_data:
Olivier Houchard1a0545f2017-09-13 18:30:23 +02001339 __conn_xprt_want_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001340}
1341
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001342/*
1343 * This function is used only for server health-checks. It handles connection
1344 * status updates including errors. If necessary, it wakes the check task up.
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001345 * It returns 0 on normal cases, <0 if at least one close() has happened on the
1346 * connection (eg: reconnect).
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001347 */
1348static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001349{
Simon Horman4a741432013-02-23 15:35:38 +09001350 struct check *check = conn->owner;
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001351 int ret = 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001352
Willy Tarreauc09572f2017-10-04 11:58:22 +02001353 /* we may have to make progress on the TCP checks */
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001354 if (check->type == PR_O2_TCPCHK_CHK) {
1355 ret = tcpcheck_main(check);
Willy Tarreau00149122017-10-04 18:05:01 +02001356 conn = check->conn;
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001357 }
Willy Tarreauc09572f2017-10-04 11:58:22 +02001358
Willy Tarreau6c560da2012-11-24 11:14:45 +01001359 if (unlikely(conn->flags & CO_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001360 /* We may get error reports bypassing the I/O handlers, typically
1361 * the case when sending a pure TCP check which fails, then the I/O
1362 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001363 * main processing task so let's simply wake it up. If we get here,
1364 * we expect errno to still be valid.
1365 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +02001366 chk_report_conn_err(check, errno, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001367
Olivier Houchard1a0545f2017-09-13 18:30:23 +02001368 __conn_xprt_stop_both(conn);
Willy Tarreau2d351b62013-12-05 02:36:25 +01001369 task_wakeup(check->task, TASK_WOKEN_IO);
1370 }
Olivier Houchard1a0545f2017-09-13 18:30:23 +02001371 else if (!(conn->flags & (CO_FL_XPRT_RD_ENA|CO_FL_XPRT_WR_ENA|CO_FL_HANDSHAKE))) {
Willy Tarreau3be293f2014-02-05 18:31:24 +01001372 /* we may get here if only a connection probe was required : we
1373 * don't have any data to send nor anything expected in response,
1374 * so the completion of the connection establishment is enough.
1375 */
1376 task_wakeup(check->task, TASK_WOKEN_IO);
1377 }
Willy Tarreau2d351b62013-12-05 02:36:25 +01001378
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001379 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001380 /* We're here because nobody wants to handle the error, so we
1381 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001382 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01001383 conn_sock_drain(conn);
Willy Tarreau402dbc12017-10-05 17:53:13 +02001384 conn_full_close(conn);
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001385 ret = -1;
Willy Tarreau2d351b62013-12-05 02:36:25 +01001386 }
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001387
1388 /* if a connection got replaced, we must absolutely prevent the connection
1389 * handler from touching its fd, and perform the FD polling updates ourselves
1390 */
1391 if (ret < 0)
1392 conn_cond_update_polling(conn);
1393
1394 return ret;
Willy Tarreau20bea422012-07-06 12:00:49 +02001395}
1396
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001397struct data_cb check_conn_cb = {
1398 .recv = event_srv_chk_r,
1399 .send = event_srv_chk_w,
1400 .wake = wake_srv_chk,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +01001401 .name = "CHCK",
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001402};
1403
Willy Tarreaubaaee002006-06-26 02:48:02 +02001404/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001405 * updates the server's weight during a warmup stage. Once the final weight is
1406 * reached, the task automatically stops. Note that any server status change
1407 * must have updated s->last_change accordingly.
1408 */
1409static struct task *server_warmup(struct task *t)
1410{
1411 struct server *s = t->context;
1412
1413 /* by default, plan on stopping the task */
1414 t->expire = TICK_ETERNITY;
Emeric Brun52a91d32017-08-31 14:41:55 +02001415 if ((s->next_admin & SRV_ADMF_MAINT) ||
1416 (s->next_state != SRV_ST_STARTING))
Willy Tarreau2e993902011-10-31 11:53:20 +01001417 return t;
1418
Willy Tarreau892337c2014-05-13 23:41:20 +02001419 /* recalculate the weights and update the state */
Willy Tarreau004e0452013-11-21 11:22:01 +01001420 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001421
1422 /* probably that we can refill this server with a bit more connections */
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001423 pendconn_grab_from_px(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001424
1425 /* get back there in 1 second or 1/20th of the slowstart interval,
1426 * whichever is greater, resulting in small 5% steps.
1427 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001428 if (s->next_state == SRV_ST_STARTING)
Willy Tarreau2e993902011-10-31 11:53:20 +01001429 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1430 return t;
1431}
1432
Willy Tarreau894c6422017-10-04 15:58:52 +02001433/* returns the first NON-COMMENT tcp-check rule from list <list> or NULL if
1434 * none was found.
1435 */
1436static struct tcpcheck_rule *get_first_tcpcheck_rule(struct list *list)
1437{
1438 struct tcpcheck_rule *r;
1439
1440 list_for_each_entry(r, list, list) {
1441 if (r->action != TCPCHK_ACT_COMMENT)
1442 return r;
1443 }
1444 return NULL;
1445}
1446
Willy Tarreau2e993902011-10-31 11:53:20 +01001447/*
Simon Horman98637e52014-06-20 12:30:16 +09001448 * establish a server health-check that makes use of a connection.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001449 *
1450 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001451 * - SF_ERR_NONE if everything's OK and tcpcheck_main() was not called
1452 * - SF_ERR_UP if if everything's OK and tcpcheck_main() was called
1453 * - SF_ERR_SRVTO if there are no more servers
1454 * - SF_ERR_SRVCL if the connection was refused by the server
1455 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1456 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1457 * - SF_ERR_INTERNAL for any other purely internal errors
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001458 * - 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 +01001459 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001460 * Note that we try to prevent the network stack from sending the ACK during the
1461 * connect() when a pure TCP check is used (without PROXY protocol).
1462 */
Simon Horman98637e52014-06-20 12:30:16 +09001463static int connect_conn_chk(struct task *t)
Simon Hormanb00d17a2014-06-13 16:18:16 +09001464{
1465 struct check *check = t->context;
1466 struct server *s = check->server;
1467 struct connection *conn = check->conn;
1468 struct protocol *proto;
Willy Tarreauf411cce2017-10-04 16:21:19 +02001469 struct tcpcheck_rule *tcp_rule = NULL;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001470 int ret;
Willy Tarreauf3d34822014-12-08 12:11:28 +01001471 int quickack;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001472
Willy Tarreau00149122017-10-04 18:05:01 +02001473 /* we cannot have a connection here */
1474 if (conn)
1475 return SF_ERR_INTERNAL;
1476
Simon Hormanb00d17a2014-06-13 16:18:16 +09001477 /* tcpcheck send/expect initialisation */
Willy Tarreauf411cce2017-10-04 16:21:19 +02001478 if (check->type == PR_O2_TCPCHK_CHK) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001479 check->current_step = NULL;
Willy Tarreauf411cce2017-10-04 16:21:19 +02001480 tcp_rule = get_first_tcpcheck_rule(check->tcpcheck_rules);
1481 }
Simon Hormanb00d17a2014-06-13 16:18:16 +09001482
1483 /* prepare the check buffer.
1484 * This should not be used if check is the secondary agent check
1485 * of a server as s->proxy->check_req will relate to the
1486 * configuration of the primary check. Similarly, tcp-check uses
1487 * its own strings.
1488 */
1489 if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) {
1490 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
1491
1492 /* we want to check if this host replies to HTTP or SSLv3 requests
1493 * so we'll send the request, and won't wake the checker up now.
1494 */
1495 if ((check->type) == PR_O2_SSL3_CHK) {
1496 /* SSL requires that we put Unix time in the request */
1497 int gmt_time = htonl(date.tv_sec);
1498 memcpy(check->bo->data + 11, &gmt_time, 4);
1499 }
1500 else if ((check->type) == PR_O2_HTTP_CHK) {
1501 if (s->proxy->options2 & PR_O2_CHK_SNDST)
1502 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size));
Cyril Bonté32602d22015-01-30 00:07:07 +01001503 /* prevent HTTP keep-alive when "http-check expect" is used */
1504 if (s->proxy->options2 & PR_O2_EXP_TYPE)
1505 bo_putstr(check->bo, "Connection: close\r\n");
Simon Hormanb00d17a2014-06-13 16:18:16 +09001506 bo_putstr(check->bo, "\r\n");
1507 *check->bo->p = '\0'; /* to make gdb output easier to read */
1508 }
1509 }
1510
James Brown55f9ff12015-10-21 18:19:05 -07001511 if ((check->type & PR_O2_LB_AGENT_CHK) && check->send_string_len) {
1512 bo_putblk(check->bo, check->send_string, check->send_string_len);
1513 }
1514
Willy Tarreauf411cce2017-10-04 16:21:19 +02001515 /* for tcp-checks, the initial connection setup is handled separately as
1516 * it may be sent to a specific port and not to the server's.
1517 */
1518 if (tcp_rule && tcp_rule->action == TCPCHK_ACT_CONNECT) {
1519 tcpcheck_main(check);
1520 return SF_ERR_UP;
1521 }
1522
Simon Hormanb00d17a2014-06-13 16:18:16 +09001523 /* prepare a new connection */
Willy Tarreau00149122017-10-04 18:05:01 +02001524 conn = check->conn = conn_new();
1525 if (!check->conn)
1526 return SF_ERR_RESOURCE;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001527
Simon Horman41f58762015-01-30 11:22:56 +09001528 if (is_addr(&check->addr)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001529 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09001530 conn->addr.to = check->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001531 }
1532 else {
1533 /* we'll connect to the addr on the server */
1534 conn->addr.to = s->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001535 }
1536
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001537 if ((conn->addr.to.ss_family == AF_INET) || (conn->addr.to.ss_family == AF_INET6)) {
1538 int i = 0;
1539
1540 i = srv_check_healthcheck_port(check);
1541 if (i == 0) {
1542 conn->owner = check;
1543 return SF_ERR_CHK_PORT;
1544 }
1545
1546 set_host_port(&conn->addr.to, i);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001547 }
1548
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01001549 proto = protocol_by_family(conn->addr.to.ss_family);
1550
1551 conn_prepare(conn, proto, check->xprt);
1552 conn_attach(conn, check, &check_conn_cb);
1553 conn->target = &s->obj_type;
1554
1555 /* no client address */
1556 clear_addr(&conn->addr.from);
1557
Willy Tarreauf3d34822014-12-08 12:11:28 +01001558 /* only plain tcp-check supports quick ACK */
1559 quickack = check->type == 0 || check->type == PR_O2_TCPCHK_CHK;
1560
Willy Tarreauf411cce2017-10-04 16:21:19 +02001561 if (tcp_rule && tcp_rule->action == TCPCHK_ACT_EXPECT)
1562 quickack = 0;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001563
Willy Tarreaue7dff022015-04-03 01:14:29 +02001564 ret = SF_ERR_INTERNAL;
Olivier Houchardb68fda42017-08-04 18:39:01 +02001565 if (proto && proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01001566 ret = proto->connect(conn, check->type, quickack ? 2 : 0);
Olivier Houchard9130a962017-10-17 17:33:43 +02001567#ifdef USE_OPENSSL
1568 if (s->check.sni)
1569 ssl_sock_set_servername(conn, s->check.sni);
1570#endif
Willy Tarreauf4949772017-05-06 08:45:28 +02001571 if (s->check.send_proxy && !(check->state & CHK_ST_AGENT)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001572 conn->send_proxy_ofs = 1;
1573 conn->flags |= CO_FL_SEND_PROXY;
1574 }
1575
1576 return ret;
1577}
1578
Simon Horman98637e52014-06-20 12:30:16 +09001579static struct list pid_list = LIST_HEAD_INIT(pid_list);
1580static struct pool_head *pool2_pid_list;
1581
1582void block_sigchld(void)
1583{
1584 sigset_t set;
1585 sigemptyset(&set);
1586 sigaddset(&set, SIGCHLD);
Willy Tarreauebc92442016-06-21 17:29:46 +02001587 assert(sigprocmask(SIG_BLOCK, &set, NULL) == 0);
Simon Horman98637e52014-06-20 12:30:16 +09001588}
1589
1590void unblock_sigchld(void)
1591{
1592 sigset_t set;
1593 sigemptyset(&set);
Willy Tarreauebc92442016-06-21 17:29:46 +02001594 sigaddset(&set, SIGCHLD);
1595 assert(sigprocmask(SIG_UNBLOCK, &set, NULL) == 0);
Simon Horman98637e52014-06-20 12:30:16 +09001596}
1597
Simon Horman98637e52014-06-20 12:30:16 +09001598static struct pid_list *pid_list_add(pid_t pid, struct task *t)
1599{
1600 struct pid_list *elem;
1601 struct check *check = t->context;
1602
1603 elem = pool_alloc2(pool2_pid_list);
1604 if (!elem)
1605 return NULL;
1606 elem->pid = pid;
1607 elem->t = t;
1608 elem->exited = 0;
1609 check->curpid = elem;
1610 LIST_INIT(&elem->list);
1611 LIST_ADD(&pid_list, &elem->list);
1612 return elem;
1613}
1614
Simon Horman98637e52014-06-20 12:30:16 +09001615static void pid_list_del(struct pid_list *elem)
1616{
1617 struct check *check;
1618
1619 if (!elem)
1620 return;
1621
Simon Horman98637e52014-06-20 12:30:16 +09001622 LIST_DEL(&elem->list);
Simon Horman98637e52014-06-20 12:30:16 +09001623 if (!elem->exited)
1624 kill(elem->pid, SIGTERM);
1625
1626 check = elem->t->context;
1627 check->curpid = NULL;
1628 pool_free2(pool2_pid_list, elem);
1629}
1630
1631/* Called from inside SIGCHLD handler, SIGCHLD is blocked */
1632static void pid_list_expire(pid_t pid, int status)
1633{
1634 struct pid_list *elem;
1635
1636 list_for_each_entry(elem, &pid_list, list) {
1637 if (elem->pid == pid) {
1638 elem->t->expire = now_ms;
1639 elem->status = status;
1640 elem->exited = 1;
Cyril Bonté9dbcfab2014-08-07 01:55:39 +02001641 task_wakeup(elem->t, TASK_WOKEN_IO);
Simon Horman98637e52014-06-20 12:30:16 +09001642 return;
1643 }
1644 }
1645}
1646
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001647static void sigchld_handler(struct sig_handler *sh)
Simon Horman98637e52014-06-20 12:30:16 +09001648{
1649 pid_t pid;
1650 int status;
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001651
Simon Horman98637e52014-06-20 12:30:16 +09001652 while ((pid = waitpid(0, &status, WNOHANG)) > 0)
1653 pid_list_expire(pid, status);
1654}
1655
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001656static int init_pid_list(void)
1657{
Simon Horman98637e52014-06-20 12:30:16 +09001658 if (pool2_pid_list != NULL)
1659 /* Nothing to do */
1660 return 0;
1661
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001662 if (!signal_register_fct(SIGCHLD, sigchld_handler, SIGCHLD)) {
Simon Horman98637e52014-06-20 12:30:16 +09001663 Alert("Failed to set signal handler for external health checks: %s. Aborting.\n",
1664 strerror(errno));
1665 return 1;
1666 }
1667
1668 pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED);
1669 if (pool2_pid_list == NULL) {
1670 Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n",
1671 strerror(errno));
1672 return 1;
1673 }
1674
1675 return 0;
1676}
1677
Cyril Bontéac92a062014-12-27 22:28:38 +01001678/* helper macro to set an environment variable and jump to a specific label on failure. */
1679#define EXTCHK_SETENV(check, envidx, value, fail) { if (extchk_setenv(check, envidx, value)) goto fail; }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001680
1681/*
Cyril Bontéac92a062014-12-27 22:28:38 +01001682 * helper function to allocate enough memory to store an environment variable.
1683 * It will also check that the environment variable is updatable, and silently
1684 * fail if not.
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001685 */
Cyril Bontéac92a062014-12-27 22:28:38 +01001686static int extchk_setenv(struct check *check, int idx, const char *value)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001687{
1688 int len, ret;
Cyril Bontéac92a062014-12-27 22:28:38 +01001689 char *envname;
1690 int vmaxlen;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001691
Cyril Bontéac92a062014-12-27 22:28:38 +01001692 if (idx < 0 || idx >= EXTCHK_SIZE) {
1693 Alert("Illegal environment variable index %d. Aborting.\n", idx);
1694 return 1;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001695 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001696
1697 envname = extcheck_envs[idx].name;
1698 vmaxlen = extcheck_envs[idx].vmaxlen;
1699
1700 /* Check if the environment variable is already set, and silently reject
1701 * the update if this one is not updatable. */
1702 if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx]))
1703 return 0;
1704
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001705 /* Instead of sending NOT_USED, sending an empty value is preferable */
1706 if (strcmp(value, "NOT_USED") == 0) {
1707 value = "";
1708 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001709
1710 len = strlen(envname) + 1;
1711 if (vmaxlen == EXTCHK_SIZE_EVAL_INIT)
1712 len += strlen(value);
1713 else
1714 len += vmaxlen;
1715
1716 if (!check->envp[idx])
1717 check->envp[idx] = malloc(len + 1);
1718
1719 if (!check->envp[idx]) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001720 Alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname);
1721 return 1;
1722 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001723 ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001724 if (ret < 0) {
1725 Alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno));
1726 return 1;
1727 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001728 else if (ret > len) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001729 Alert("Environment variable '%s' was truncated. Aborting.\n", envname);
1730 return 1;
1731 }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001732 return 0;
1733}
Simon Horman98637e52014-06-20 12:30:16 +09001734
1735static int prepare_external_check(struct check *check)
1736{
1737 struct server *s = check->server;
1738 struct proxy *px = s->proxy;
1739 struct listener *listener = NULL, *l;
1740 int i;
Simon Horman98637e52014-06-20 12:30:16 +09001741 const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001742 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001743
1744 list_for_each_entry(l, &px->conf.listeners, by_fe)
1745 /* Use the first INET, INET6 or UNIX listener */
1746 if (l->addr.ss_family == AF_INET ||
1747 l->addr.ss_family == AF_INET6 ||
1748 l->addr.ss_family == AF_UNIX) {
1749 listener = l;
1750 break;
1751 }
1752
Simon Horman98637e52014-06-20 12:30:16 +09001753 check->curpid = NULL;
Cyril Bontéac92a062014-12-27 22:28:38 +01001754 check->envp = calloc((EXTCHK_SIZE + 1), sizeof(char *));
1755 if (!check->envp) {
1756 Alert("Failed to allocate memory for environment variables. Aborting\n");
1757 goto err;
1758 }
Simon Horman98637e52014-06-20 12:30:16 +09001759
Cyril Bontéac92a062014-12-27 22:28:38 +01001760 check->argv = calloc(6, sizeof(char *));
1761 if (!check->argv) {
1762 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001763 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001764 }
Simon Horman98637e52014-06-20 12:30:16 +09001765
1766 check->argv[0] = px->check_command;
1767
Cyril Bonté777be862014-12-02 21:21:35 +01001768 if (!listener) {
1769 check->argv[1] = strdup("NOT_USED");
1770 check->argv[2] = strdup("NOT_USED");
1771 }
1772 else if (listener->addr.ss_family == AF_INET ||
Simon Horman98637e52014-06-20 12:30:16 +09001773 listener->addr.ss_family == AF_INET6) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001774 addr_to_str(&listener->addr, buf, sizeof(buf));
1775 check->argv[1] = strdup(buf);
1776 port_to_str(&listener->addr, buf, sizeof(buf));
1777 check->argv[2] = strdup(buf);
Cyril Bonté777be862014-12-02 21:21:35 +01001778 }
1779 else if (listener->addr.ss_family == AF_UNIX) {
Simon Horman98637e52014-06-20 12:30:16 +09001780 const struct sockaddr_un *un;
1781
1782 un = (struct sockaddr_un *)&listener->addr;
1783 check->argv[1] = strdup(un->sun_path);
1784 check->argv[2] = strdup("NOT_USED");
Cyril Bonté777be862014-12-02 21:21:35 +01001785 }
1786 else {
Cyril Bontéac92a062014-12-27 22:28:38 +01001787 Alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001788 goto err;
1789 }
1790
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001791 addr_to_str(&s->addr, buf, sizeof(buf));
1792 check->argv[3] = strdup(buf);
Willy Tarreau04276f32017-01-06 17:41:29 +01001793
1794 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
1795 snprintf(buf, sizeof(buf), "%u", s->svc_port);
1796 else
1797 *buf = 0;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001798 check->argv[4] = strdup(buf);
Simon Horman98637e52014-06-20 12:30:16 +09001799
Cyril Bontéac92a062014-12-27 22:28:38 +01001800 for (i = 0; i < 5; i++) {
1801 if (!check->argv[i]) {
1802 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001803 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001804 }
1805 }
Simon Horman98637e52014-06-20 12:30:16 +09001806
Cyril Bontéac92a062014-12-27 22:28:38 +01001807 EXTCHK_SETENV(check, EXTCHK_PATH, path, err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001808 /* Add proxy environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001809 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err);
1810 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err);
1811 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err);
1812 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001813 /* Add server environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001814 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err);
1815 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err);
1816 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err);
1817 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err);
1818 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err);
1819 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), err);
1820
1821 /* Ensure that we don't leave any hole in check->envp */
1822 for (i = 0; i < EXTCHK_SIZE; i++)
1823 if (!check->envp[i])
1824 EXTCHK_SETENV(check, i, "", err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001825
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001826 return 1;
Simon Horman98637e52014-06-20 12:30:16 +09001827err:
1828 if (check->envp) {
Cyril Bontéac92a062014-12-27 22:28:38 +01001829 for (i = 0; i < EXTCHK_SIZE; i++)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001830 free(check->envp[i]);
Simon Horman98637e52014-06-20 12:30:16 +09001831 free(check->envp);
1832 check->envp = NULL;
1833 }
1834
1835 if (check->argv) {
1836 for (i = 1; i < 5; i++)
1837 free(check->argv[i]);
1838 free(check->argv);
1839 check->argv = NULL;
1840 }
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001841 return 0;
Simon Horman98637e52014-06-20 12:30:16 +09001842}
1843
Simon Hormanb00d17a2014-06-13 16:18:16 +09001844/*
Simon Horman98637e52014-06-20 12:30:16 +09001845 * establish a server health-check that makes use of a process.
1846 *
1847 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001848 * - SF_ERR_NONE if everything's OK
Willy Tarreaue7dff022015-04-03 01:14:29 +02001849 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
Tim Düsterhus4896c442016-11-29 02:15:19 +01001850 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Horman98637e52014-06-20 12:30:16 +09001851 *
1852 * Blocks and then unblocks SIGCHLD
1853 */
1854static int connect_proc_chk(struct task *t)
1855{
Cyril Bontéac92a062014-12-27 22:28:38 +01001856 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001857 struct check *check = t->context;
1858 struct server *s = check->server;
1859 struct proxy *px = s->proxy;
1860 int status;
1861 pid_t pid;
1862
Willy Tarreaue7dff022015-04-03 01:14:29 +02001863 status = SF_ERR_RESOURCE;
Simon Horman98637e52014-06-20 12:30:16 +09001864
1865 block_sigchld();
1866
1867 pid = fork();
1868 if (pid < 0) {
1869 Alert("Failed to fork process for external health check: %s. Aborting.\n",
1870 strerror(errno));
1871 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1872 goto out;
1873 }
1874 if (pid == 0) {
1875 /* Child */
1876 extern char **environ;
Willy Tarreaub7b24782016-06-21 15:32:29 +02001877 int fd;
1878
1879 /* close all FDs. Keep stdin/stdout/stderr in verbose mode */
1880 fd = (global.mode & (MODE_QUIET|MODE_VERBOSE)) == MODE_QUIET ? 0 : 3;
1881
1882 while (fd < global.rlimit_nofile)
1883 close(fd++);
1884
Simon Horman98637e52014-06-20 12:30:16 +09001885 environ = check->envp;
Cyril Bontéac92a062014-12-27 22:28:38 +01001886 extchk_setenv(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)));
Simon Horman98637e52014-06-20 12:30:16 +09001887 execvp(px->check_command, check->argv);
1888 Alert("Failed to exec process for external health check: %s. Aborting.\n",
1889 strerror(errno));
1890 exit(-1);
1891 }
1892
1893 /* Parent */
1894 if (check->result == CHK_RES_UNKNOWN) {
1895 if (pid_list_add(pid, t) != NULL) {
1896 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1897
1898 if (px->timeout.check && px->timeout.connect) {
1899 int t_con = tick_add(now_ms, px->timeout.connect);
1900 t->expire = tick_first(t->expire, t_con);
1901 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02001902 status = SF_ERR_NONE;
Simon Horman98637e52014-06-20 12:30:16 +09001903 goto out;
1904 }
1905 else {
1906 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1907 }
1908 kill(pid, SIGTERM); /* process creation error */
1909 }
1910 else
1911 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1912
1913out:
1914 unblock_sigchld();
1915 return status;
1916}
1917
1918/*
Willy Tarreau1e62e2a2017-10-04 15:07:02 +02001919 * manages a server health-check that uses an external process. Returns
Willy Tarreaubaaee002006-06-26 02:48:02 +02001920 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1921 */
Simon Horman98637e52014-06-20 12:30:16 +09001922static struct task *process_chk_proc(struct task *t)
1923{
1924 struct check *check = t->context;
1925 struct server *s = check->server;
Simon Horman98637e52014-06-20 12:30:16 +09001926 int rv;
1927 int ret;
1928 int expired = tick_is_expired(t->expire, now_ms);
1929
1930 if (!(check->state & CHK_ST_INPROGRESS)) {
1931 /* no check currently running */
1932 if (!expired) /* woke up too early */
1933 return t;
1934
1935 /* we don't send any health-checks when the proxy is
1936 * stopped, the server should not be checked or the check
1937 * is disabled.
1938 */
1939 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
1940 s->proxy->state == PR_STSTOPPED)
1941 goto reschedule;
1942
1943 /* we'll initiate a new check */
1944 set_server_check_status(check, HCHK_STATUS_START, NULL);
1945
1946 check->state |= CHK_ST_INPROGRESS;
1947
Simon Hormandbf70192015-01-30 11:22:53 +09001948 ret = connect_proc_chk(t);
Willy Tarreaud7c3fbd2017-10-04 15:19:26 +02001949 if (ret == SF_ERR_NONE) {
Willy Tarreau1e62e2a2017-10-04 15:07:02 +02001950 /* the process was forked, we allow up to min(inter,
1951 * timeout.connect) for it to report its status, but
1952 * only when timeout.check is set as it may be to short
1953 * for a full check otherwise.
Simon Horman98637e52014-06-20 12:30:16 +09001954 */
1955 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1956
1957 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1958 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1959 t->expire = tick_first(t->expire, t_con);
1960 }
1961
1962 goto reschedule;
Simon Horman98637e52014-06-20 12:30:16 +09001963 }
1964
Willy Tarreau1e62e2a2017-10-04 15:07:02 +02001965 /* here, we failed to start the check */
Simon Horman98637e52014-06-20 12:30:16 +09001966
1967 check->state &= ~CHK_ST_INPROGRESS;
1968 check_notify_failure(check);
1969
1970 /* we allow up to min(inter, timeout.connect) for a connection
1971 * to establish but only when timeout.check is set
1972 * as it may be to short for a full check otherwise
1973 */
1974 while (tick_is_expired(t->expire, now_ms)) {
1975 int t_con;
1976
1977 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1978 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1979
1980 if (s->proxy->timeout.check)
1981 t->expire = tick_first(t->expire, t_con);
1982 }
1983 }
1984 else {
1985 /* there was a test running.
1986 * First, let's check whether there was an uncaught error,
1987 * which can happen on connect timeout or error.
1988 */
1989 if (check->result == CHK_RES_UNKNOWN) {
1990 /* good connection is enough for pure TCP check */
1991 struct pid_list *elem = check->curpid;
1992 int status = HCHK_STATUS_UNKNOWN;
1993
1994 if (elem->exited) {
1995 status = elem->status; /* Save in case the process exits between use below */
1996 if (!WIFEXITED(status))
1997 check->code = -1;
1998 else
1999 check->code = WEXITSTATUS(status);
2000 if (!WIFEXITED(status) || WEXITSTATUS(status))
2001 status = HCHK_STATUS_PROCERR;
2002 else
2003 status = HCHK_STATUS_PROCOK;
2004 } else if (expired) {
2005 status = HCHK_STATUS_PROCTOUT;
Willy Tarreaudc3d1902014-07-08 00:56:27 +02002006 Warning("kill %d\n", (int)elem->pid);
Simon Horman98637e52014-06-20 12:30:16 +09002007 kill(elem->pid, SIGTERM);
2008 }
2009 set_server_check_status(check, status, NULL);
2010 }
2011
2012 if (check->result == CHK_RES_FAILED) {
2013 /* a failure or timeout detected */
2014 check_notify_failure(check);
2015 }
2016 else if (check->result == CHK_RES_CONDPASS) {
2017 /* check is OK but asks for stopping mode */
2018 check_notify_stopping(check);
2019 }
2020 else if (check->result == CHK_RES_PASSED) {
2021 /* a success was detected */
2022 check_notify_success(check);
2023 }
2024 check->state &= ~CHK_ST_INPROGRESS;
2025
2026 pid_list_del(check->curpid);
2027
2028 rv = 0;
2029 if (global.spread_checks > 0) {
2030 rv = srv_getinter(check) * global.spread_checks / 100;
2031 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
2032 }
2033 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
2034 }
2035
2036 reschedule:
2037 while (tick_is_expired(t->expire, now_ms))
2038 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2039 return t;
2040}
2041
2042/*
2043 * manages a server health-check that uses a connection. Returns
2044 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2045 */
2046static struct task *process_chk_conn(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002047{
Simon Horman4a741432013-02-23 15:35:38 +09002048 struct check *check = t->context;
2049 struct server *s = check->server;
2050 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002051 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002052 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002053 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002054
Willy Tarreau2c115e52013-12-11 19:41:16 +01002055 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01002056 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002057 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01002058 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002059
Simon Horman671b6f02013-11-25 10:46:39 +09002060 /* we don't send any health-checks when the proxy is
2061 * stopped, the server should not be checked or the check
2062 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002063 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01002064 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01002065 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01002066 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002067
2068 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09002069 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002070
Willy Tarreau2c115e52013-12-11 19:41:16 +01002071 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09002072 check->bi->p = check->bi->data;
2073 check->bi->i = 0;
2074 check->bo->p = check->bo->data;
2075 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002076
Simon Hormandbf70192015-01-30 11:22:53 +09002077 ret = connect_conn_chk(t);
Willy Tarreau00149122017-10-04 18:05:01 +02002078 conn = check->conn;
2079
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002080 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002081 case SF_ERR_UP:
Simon Hormanb00d17a2014-06-13 16:18:16 +09002082 return t;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002083 case SF_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002084 /* we allow up to min(inter, timeout.connect) for a connection
2085 * to establish but only when timeout.check is set
2086 * as it may be to short for a full check otherwise
2087 */
Simon Horman4a741432013-02-23 15:35:38 +09002088 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002089
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002090 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2091 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2092 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002093 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01002094
2095 if (check->type)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002096 conn_xprt_want_recv(conn); /* prepare for reading a possible reply */
Willy Tarreau06559ac2013-12-05 01:53:08 +01002097
Willy Tarreau5a78f362012-11-23 12:47:05 +01002098 goto reschedule;
2099
Willy Tarreaue7dff022015-04-03 01:14:29 +02002100 case SF_ERR_SRVTO: /* ETIMEDOUT */
2101 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau00149122017-10-04 18:05:01 +02002102 if (conn)
2103 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002104 chk_report_conn_err(check, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002105 break;
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02002106 /* should share same code than cases below */
2107 case SF_ERR_CHK_PORT:
2108 check->state |= CHK_ST_PORT_MISS;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002109 case SF_ERR_PRXCOND:
2110 case SF_ERR_RESOURCE:
2111 case SF_ERR_INTERNAL:
Willy Tarreau00149122017-10-04 18:05:01 +02002112 if (conn)
2113 conn->flags |= CO_FL_ERROR;
2114 chk_report_conn_err(check, conn ? 0 : ENOMEM, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002115 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002116 }
2117
Willy Tarreau5a78f362012-11-23 12:47:05 +01002118 /* here, we have seen a synchronous error, no fd was allocated */
Olivier Houchard390485a2017-10-24 19:03:30 +02002119 if (conn) {
2120 conn_free(conn);
2121 check->conn = conn = NULL;
2122 }
Willy Tarreau6b0a8502012-11-23 08:51:32 +01002123
Willy Tarreau2c115e52013-12-11 19:41:16 +01002124 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02002125 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002126
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002127 /* we allow up to min(inter, timeout.connect) for a connection
2128 * to establish but only when timeout.check is set
2129 * as it may be to short for a full check otherwise
2130 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002131 while (tick_is_expired(t->expire, now_ms)) {
2132 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002133
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002134 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09002135 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002136
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002137 if (s->proxy->timeout.check)
2138 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002139 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002140 }
2141 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02002142 /* there was a test running.
2143 * First, let's check whether there was an uncaught error,
2144 * which can happen on connect timeout or error.
2145 */
Simon Hormanccaabcd2014-06-20 12:29:47 +09002146 if (check->result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002147 /* good connection is enough for pure TCP check */
2148 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09002149 if (check->use_ssl)
2150 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02002151 else
Simon Horman4a741432013-02-23 15:35:38 +09002152 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002153 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002154 else if ((conn->flags & CO_FL_ERROR) || expired) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002155 chk_report_conn_err(check, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02002156 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002157 else
2158 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02002159 }
2160
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002161 /* check complete or aborted */
Willy Tarreau00149122017-10-04 18:05:01 +02002162 if (conn && conn->xprt) {
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002163 /* The check was aborted and the connection was not yet closed.
2164 * This can happen upon timeout, or when an external event such
2165 * as a failed response coupled with "observe layer7" caused the
2166 * server state to be suddenly changed.
2167 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01002168 conn_sock_drain(conn);
Willy Tarreau402dbc12017-10-05 17:53:13 +02002169 conn_full_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002170 }
2171
Willy Tarreau00149122017-10-04 18:05:01 +02002172 if (conn) {
2173 conn_free(conn);
2174 check->conn = conn = NULL;
2175 }
2176
Willy Tarreauaf549582014-05-16 17:37:50 +02002177 if (check->result == CHK_RES_FAILED) {
2178 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002179 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002180 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002181 else if (check->result == CHK_RES_CONDPASS) {
2182 /* check is OK but asks for stopping mode */
2183 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002184 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002185 else if (check->result == CHK_RES_PASSED) {
2186 /* a success was detected */
2187 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002188 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01002189 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002190
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002191 rv = 0;
2192 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002193 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002194 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002195 }
Simon Horman4a741432013-02-23 15:35:38 +09002196 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002197 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002198
2199 reschedule:
2200 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002201 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002202 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01002203 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002204}
2205
Simon Horman98637e52014-06-20 12:30:16 +09002206/*
2207 * manages a server health-check. Returns
2208 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2209 */
2210static struct task *process_chk(struct task *t)
2211{
2212 struct check *check = t->context;
2213
2214 if (check->type == PR_O2_EXT_CHK)
2215 return process_chk_proc(t);
2216 return process_chk_conn(t);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002217
Simon Horman98637e52014-06-20 12:30:16 +09002218}
2219
Simon Horman5c942422013-11-25 10:46:32 +09002220static int start_check_task(struct check *check, int mininter,
2221 int nbcheck, int srvpos)
2222{
2223 struct task *t;
2224 /* task for the check */
2225 if ((t = task_new()) == NULL) {
2226 Alert("Starting [%s:%s] check: out of memory.\n",
2227 check->server->proxy->id, check->server->id);
2228 return 0;
2229 }
2230
2231 check->task = t;
2232 t->process = process_chk;
2233 t->context = check;
2234
Willy Tarreau1746eec2014-04-25 10:46:47 +02002235 if (mininter < srv_getinter(check))
2236 mininter = srv_getinter(check);
2237
2238 if (global.max_spread_checks && mininter > global.max_spread_checks)
2239 mininter = global.max_spread_checks;
2240
Simon Horman5c942422013-11-25 10:46:32 +09002241 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002242 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002243 check->start = now;
2244 task_queue(t);
2245
2246 return 1;
2247}
2248
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002249/*
2250 * Start health-check.
Willy Tarreau865c5142016-12-21 20:04:48 +01002251 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002252 */
Willy Tarreau865c5142016-12-21 20:04:48 +01002253static int start_checks()
2254{
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002255
2256 struct proxy *px;
2257 struct server *s;
2258 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002259 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002260
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002261 /* 1- count the checkers to run simultaneously.
2262 * We also determine the minimum interval among all of those which
2263 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2264 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002265 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002266 * too short an interval for all others.
2267 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002268 for (px = proxy; px; px = px->next) {
2269 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002270 if (s->slowstart) {
2271 if ((t = task_new()) == NULL) {
2272 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Willy Tarreau865c5142016-12-21 20:04:48 +01002273 return ERR_ALERT | ERR_FATAL;
Willy Tarreaue7b73482013-11-21 11:50:50 +01002274 }
2275 /* We need a warmup task that will be called when the server
2276 * state switches from down to up.
2277 */
2278 s->warmup = t;
2279 t->process = server_warmup;
2280 t->context = s;
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002281 /* server can be in this state only because of */
Emeric Brun52a91d32017-08-31 14:41:55 +02002282 if (s->next_state == SRV_ST_STARTING)
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002283 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 +01002284 }
2285
Willy Tarreaud8514a22013-12-11 21:10:14 +01002286 if (s->check.state & CHK_ST_CONFIGURED) {
2287 nbcheck++;
2288 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2289 (!mininter || mininter > srv_getinter(&s->check)))
2290 mininter = srv_getinter(&s->check);
2291 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002292
Willy Tarreaud8514a22013-12-11 21:10:14 +01002293 if (s->agent.state & CHK_ST_CONFIGURED) {
2294 nbcheck++;
2295 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2296 (!mininter || mininter > srv_getinter(&s->agent)))
2297 mininter = srv_getinter(&s->agent);
2298 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002299 }
2300 }
2301
Simon Horman4a741432013-02-23 15:35:38 +09002302 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002303 return 0;
2304
2305 srand((unsigned)time(NULL));
2306
2307 /*
2308 * 2- start them as far as possible from each others. For this, we will
2309 * start them after their interval set to the min interval divided by
2310 * the number of servers, weighted by the server's position in the list.
2311 */
2312 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002313 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2314 if (init_pid_list()) {
2315 Alert("Starting [%s] check: out of memory.\n", px->id);
Willy Tarreau865c5142016-12-21 20:04:48 +01002316 return ERR_ALERT | ERR_FATAL;
Simon Horman98637e52014-06-20 12:30:16 +09002317 }
2318 }
2319
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002320 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002321 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002322 if (s->check.state & CHK_ST_CONFIGURED) {
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002323 if (s->check.type == PR_O2_EXT_CHK) {
2324 if (!prepare_external_check(&s->check))
Willy Tarreau865c5142016-12-21 20:04:48 +01002325 return ERR_ALERT | ERR_FATAL;
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002326 }
Simon Hormand60d6912013-11-25 10:46:36 +09002327 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
Willy Tarreau865c5142016-12-21 20:04:48 +01002328 return ERR_ALERT | ERR_FATAL;
Simon Hormand60d6912013-11-25 10:46:36 +09002329 srvpos++;
2330 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002331
Simon Hormand60d6912013-11-25 10:46:36 +09002332 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002333 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002334 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
Willy Tarreau865c5142016-12-21 20:04:48 +01002335 return ERR_ALERT | ERR_FATAL;
Simon Hormand60d6912013-11-25 10:46:36 +09002336 }
2337 srvpos++;
2338 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002339 }
2340 }
2341 return 0;
2342}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002343
2344/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002345 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002346 * The buffer MUST be terminated by a null byte before calling this function.
2347 * Sets server status appropriately. The caller is responsible for ensuring
2348 * that the buffer contains at least 13 characters. If <done> is zero, we may
2349 * return 0 to indicate that data is required to decide of a match.
2350 */
2351static int httpchk_expect(struct server *s, int done)
2352{
2353 static char status_msg[] = "HTTP status check returned code <000>";
2354 char status_code[] = "000";
2355 char *contentptr;
2356 int crlf;
2357 int ret;
2358
2359 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2360 case PR_O2_EXP_STS:
2361 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002362 memcpy(status_code, s->check.bi->data + 9, 3);
2363 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002364
2365 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2366 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2367 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002368 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002369
2370 /* we necessarily have the response, so there are no partial failures */
2371 if (s->proxy->options2 & PR_O2_EXP_INV)
2372 ret = !ret;
2373
Simon Horman4a741432013-02-23 15:35:38 +09002374 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002375 break;
2376
2377 case PR_O2_EXP_STR:
2378 case PR_O2_EXP_RSTR:
2379 /* very simple response parser: ignore CR and only count consecutive LFs,
2380 * stop with contentptr pointing to first char after the double CRLF or
2381 * to '\0' if crlf < 2.
2382 */
2383 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002384 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002385 if (crlf >= 2)
2386 break;
2387 if (*contentptr == '\r')
2388 continue;
2389 else if (*contentptr == '\n')
2390 crlf++;
2391 else
2392 crlf = 0;
2393 }
2394
2395 /* Check that response contains a body... */
2396 if (crlf < 2) {
2397 if (!done)
2398 return 0;
2399
Simon Horman4a741432013-02-23 15:35:38 +09002400 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002401 "HTTP content check could not find a response body");
2402 return 1;
2403 }
2404
2405 /* Check that response body is not empty... */
2406 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002407 if (!done)
2408 return 0;
2409
Simon Horman4a741432013-02-23 15:35:38 +09002410 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002411 "HTTP content check found empty response body");
2412 return 1;
2413 }
2414
2415 /* Check the response content against the supplied string
2416 * or regex... */
2417 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2418 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2419 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002420 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002421
2422 /* if we don't match, we may need to wait more */
2423 if (!ret && !done)
2424 return 0;
2425
2426 if (ret) {
2427 /* content matched */
2428 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002429 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002430 "HTTP check matched unwanted content");
2431 else
Simon Horman4a741432013-02-23 15:35:38 +09002432 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002433 "HTTP content check matched");
2434 }
2435 else {
2436 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002437 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002438 "HTTP check did not match unwanted content");
2439 else
Simon Horman4a741432013-02-23 15:35:38 +09002440 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002441 "HTTP content check did not match");
2442 }
2443 break;
2444 }
2445 return 1;
2446}
2447
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002448/*
2449 * return the id of a step in a send/expect session
2450 */
Simon Hormane16c1b32015-01-30 11:22:57 +09002451static int tcpcheck_get_step_id(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002452{
2453 struct tcpcheck_rule *cur = NULL, *next = NULL;
2454 int i = 0;
2455
Willy Tarreau213c6782014-10-02 14:51:02 +02002456 /* not even started anything yet => step 0 = initial connect */
Baptiste Assmannf95bc8e2015-04-25 16:03:06 +02002457 if (!check->current_step)
Willy Tarreau213c6782014-10-02 14:51:02 +02002458 return 0;
2459
Simon Hormane16c1b32015-01-30 11:22:57 +09002460 cur = check->last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002461
2462 /* no step => first step */
2463 if (cur == NULL)
2464 return 1;
2465
2466 /* increment i until current step */
Simon Hormane16c1b32015-01-30 11:22:57 +09002467 list_for_each_entry(next, check->tcpcheck_rules, list) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002468 if (next->list.p == &cur->list)
2469 break;
2470 ++i;
2471 }
2472
2473 return i;
2474}
2475
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002476/*
2477 * return the latest known comment before (including) the given stepid
2478 * returns NULL if no comment found
2479 */
2480static char * tcpcheck_get_step_comment(struct check *check, int stepid)
2481{
2482 struct tcpcheck_rule *cur = NULL;
2483 char *ret = NULL;
2484 int i = 0;
2485
2486 /* not even started anything yet, return latest comment found before any action */
2487 if (!check->current_step) {
2488 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2489 if (cur->action == TCPCHK_ACT_COMMENT)
2490 ret = cur->comment;
2491 else
2492 goto return_comment;
2493 }
2494 }
2495
2496 i = 1;
2497 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2498 if (cur->comment)
2499 ret = cur->comment;
2500
2501 if (i >= stepid)
2502 goto return_comment;
2503
2504 ++i;
2505 }
2506
2507 return_comment:
2508 return ret;
2509}
2510
Willy Tarreaube74b882017-10-04 16:22:49 +02002511/* proceed with next steps for the TCP checks <check>. Note that this is called
2512 * both from the connection's wake() callback and from the check scheduling task.
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002513 * It returns 0 on normal cases, or <0 if a close() has happened on an existing
2514 * connection, presenting the risk of an fd replacement.
Willy Tarreaube74b882017-10-04 16:22:49 +02002515 */
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002516static int tcpcheck_main(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002517{
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002518 char *contentptr, *comment;
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002519 struct tcpcheck_rule *next;
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002520 int done = 0, ret = 0, step = 0;
Willy Tarreaube74b882017-10-04 16:22:49 +02002521 struct connection *conn = check->conn;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002522 struct server *s = check->server;
2523 struct task *t = check->task;
Simon Hormane16c1b32015-01-30 11:22:57 +09002524 struct list *head = check->tcpcheck_rules;
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002525 int retcode = 0;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002526
Willy Tarreauef953952014-10-02 14:30:14 +02002527 /* here, we know that the check is complete or that it failed */
2528 if (check->result != CHK_RES_UNKNOWN)
2529 goto out_end_tcpcheck;
2530
2531 /* We have 4 possibilities here :
2532 * 1. we've not yet attempted step 1, and step 1 is a connect, so no
Willy Tarreau00149122017-10-04 18:05:01 +02002533 * connection attempt was made yet ; conn==NULL;current_step==NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002534 * 2. we've not yet attempted step 1, and step 1 is a not connect or
2535 * does not exist (no rule), so a connection attempt was made
Willy Tarreau00149122017-10-04 18:05:01 +02002536 * before coming here, conn!=NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002537 * 3. we're coming back after having started with step 1, so we may
Willy Tarreau00149122017-10-04 18:05:01 +02002538 * be waiting for a connection attempt to complete. conn!=NULL.
2539 * 4. the connection + handshake are complete. conn!=NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002540 *
2541 * #2 and #3 are quite similar, we want both the connection and the
2542 * handshake to complete before going any further. Thus we must always
2543 * wait for a connection to complete unless we're before and existing
2544 * step 1.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002545 */
Willy Tarreau449f9522015-05-13 15:39:48 +02002546
2547 /* find first rule and skip comments */
2548 next = LIST_NEXT(head, struct tcpcheck_rule *, list);
2549 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
2550 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
2551
Willy Tarreau00149122017-10-04 18:05:01 +02002552 if ((check->current_step || &next->list == head) &&
2553 (!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE))) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002554 /* we allow up to min(inter, timeout.connect) for a connection
2555 * to establish but only when timeout.check is set
2556 * as it may be to short for a full check otherwise
2557 */
2558 while (tick_is_expired(t->expire, now_ms)) {
2559 int t_con;
2560
2561 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2562 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2563
2564 if (s->proxy->timeout.check)
2565 t->expire = tick_first(t->expire, t_con);
2566 }
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002567 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002568 }
2569
Willy Tarreauef953952014-10-02 14:30:14 +02002570 /* special case: option tcp-check with no rule, a connect is enough */
Willy Tarreau449f9522015-05-13 15:39:48 +02002571 if (&next->list == head) {
Willy Tarreauef953952014-10-02 14:30:14 +02002572 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002573 goto out_end_tcpcheck;
Willy Tarreauef953952014-10-02 14:30:14 +02002574 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002575
Willy Tarreau213c6782014-10-02 14:51:02 +02002576 /* no step means first step initialisation */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002577 if (check->current_step == NULL) {
Willy Tarreau213c6782014-10-02 14:51:02 +02002578 check->last_started_step = NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002579 check->bo->p = check->bo->data;
2580 check->bo->o = 0;
2581 check->bi->p = check->bi->data;
2582 check->bi->i = 0;
Willy Tarreau449f9522015-05-13 15:39:48 +02002583 check->current_step = next;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002584 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2585 if (s->proxy->timeout.check)
2586 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2587 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002588
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002589 /* It's only the rules which will enable send/recv */
Willy Tarreau00149122017-10-04 18:05:01 +02002590 if (conn)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002591 __conn_xprt_stop_both(conn);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002592
Willy Tarreauabca5b62013-12-06 14:19:25 +01002593 while (1) {
Willy Tarreau263013d2015-05-13 11:59:14 +02002594 /* We have to try to flush the output buffer before reading, at
2595 * the end, or if we're about to send a string that does not fit
2596 * in the remaining space. That explains why we break out of the
Willy Tarreau00149122017-10-04 18:05:01 +02002597 * loop after this control. If we have data, conn is valid.
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002598 */
2599 if (check->bo->o &&
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002600 (&check->current_step->list == head ||
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002601 check->current_step->action != TCPCHK_ACT_SEND ||
2602 check->current_step->string_len >= buffer_total_space(check->bo))) {
2603
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002604 __conn_xprt_want_send(conn);
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002605 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002606 if (conn->flags & CO_FL_ERROR) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002607 chk_report_conn_err(check, errno, 0);
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002608 __conn_xprt_stop_both(conn);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002609 goto out_end_tcpcheck;
2610 }
Willy Tarreau263013d2015-05-13 11:59:14 +02002611 break;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002612 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002613 }
2614
Willy Tarreau263013d2015-05-13 11:59:14 +02002615 if (&check->current_step->list == head)
Willy Tarreauabca5b62013-12-06 14:19:25 +01002616 break;
2617
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002618 /* have 'next' point to the next rule or NULL if we're on the
2619 * last one, connect() needs this.
2620 */
Willy Tarreau5581c272015-05-13 12:24:53 +02002621 next = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002622
2623 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002624 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002625 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002626
2627 /* NULL if we're on the last rule */
Willy Tarreauf3d34822014-12-08 12:11:28 +01002628 if (&next->list == head)
2629 next = NULL;
2630
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002631 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2632 struct protocol *proto;
2633 struct xprt_ops *xprt;
2634
Willy Tarreau00149122017-10-04 18:05:01 +02002635 /* For a connect action we'll create a new connection.
2636 * We may also have to kill a previous one. But we don't
2637 * want to leave *without* a connection if we came here
2638 * from the connection layer, hence with a connection.
2639 * Thus we'll proceed in the following order :
2640 * 1: close but not release previous connection
2641 * 2: try to get a new connection
2642 * 3: release and replace the old one on success
2643 */
2644 if (check->conn) {
Willy Tarreau402dbc12017-10-05 17:53:13 +02002645 conn_full_close(check->conn);
Willy Tarreau00149122017-10-04 18:05:01 +02002646 retcode = -1; /* do not reuse the fd! */
2647 }
2648
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002649 /* mark the step as started */
2650 check->last_started_step = check->current_step;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002651
2652 /* prepare new connection */
Willy Tarreau00149122017-10-04 18:05:01 +02002653 conn = conn_new();
2654 if (!conn) {
2655 step = tcpcheck_get_step_id(check);
2656 chunk_printf(&trash, "TCPCHK error allocating connection at step %d", step);
2657 comment = tcpcheck_get_step_comment(check, step);
2658 if (comment)
2659 chunk_appendf(&trash, " comment: '%s'", comment);
2660 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2661 check->current_step = NULL;
2662 return retcode;
2663 }
2664
2665 if (check->conn)
2666 conn_free(check->conn);
2667 check->conn = conn;
2668
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002669 conn_attach(conn, check, &check_conn_cb);
2670 conn->target = &s->obj_type;
2671
2672 /* no client address */
2673 clear_addr(&conn->addr.from);
2674
Simon Horman41f58762015-01-30 11:22:56 +09002675 if (is_addr(&check->addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002676 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09002677 conn->addr.to = check->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002678 }
2679 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002680 /* we'll connect to the addr on the server */
2681 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002682 }
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01002683 proto = protocol_by_family(conn->addr.to.ss_family);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002684
2685 /* port */
2686 if (check->current_step->port)
2687 set_host_port(&conn->addr.to, check->current_step->port);
2688 else if (check->port)
2689 set_host_port(&conn->addr.to, check->port);
2690
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002691 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002692 xprt = xprt_get(XPRT_SSL);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002693 }
2694 else {
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002695 xprt = xprt_get(XPRT_RAW);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002696 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002697 conn_prepare(conn, proto, xprt);
2698
Willy Tarreaue7dff022015-04-03 01:14:29 +02002699 ret = SF_ERR_INTERNAL;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002700 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01002701 ret = proto->connect(conn,
2702 1 /* I/O polling is always needed */,
2703 (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002704 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2705 conn->send_proxy_ofs = 1;
2706 conn->flags |= CO_FL_SEND_PROXY;
2707 }
2708
2709 /* It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02002710 * - SF_ERR_NONE if everything's OK
2711 * - SF_ERR_SRVTO if there are no more servers
2712 * - SF_ERR_SRVCL if the connection was refused by the server
2713 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2714 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2715 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +01002716 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002717 * Note that we try to prevent the network stack from sending the ACK during the
2718 * connect() when a pure TCP check is used (without PROXY protocol).
2719 */
2720 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002721 case SF_ERR_NONE:
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002722 /* we allow up to min(inter, timeout.connect) for a connection
2723 * to establish but only when timeout.check is set
2724 * as it may be to short for a full check otherwise
2725 */
2726 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2727
2728 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2729 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2730 t->expire = tick_first(t->expire, t_con);
2731 }
2732 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002733 case SF_ERR_SRVTO: /* ETIMEDOUT */
2734 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002735 step = tcpcheck_get_step_id(check);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002736 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002737 step, strerror(errno));
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002738 comment = tcpcheck_get_step_comment(check, step);
2739 if (comment)
2740 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002741 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2742 goto out_end_tcpcheck;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002743 case SF_ERR_PRXCOND:
2744 case SF_ERR_RESOURCE:
2745 case SF_ERR_INTERNAL:
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002746 step = tcpcheck_get_step_id(check);
2747 chunk_printf(&trash, "TCPCHK error establishing connection at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002748 comment = tcpcheck_get_step_comment(check, step);
2749 if (comment)
2750 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002751 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2752 goto out_end_tcpcheck;
2753 }
2754
2755 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002756 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002757
2758 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002759 while (&check->current_step->list != head &&
2760 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002761 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002762
Willy Tarreauf2c87352015-05-13 12:08:21 +02002763 if (&check->current_step->list == head)
2764 break;
2765
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002766 /* don't do anything until the connection is established */
2767 if (!(conn->flags & CO_FL_CONNECTED)) {
2768 /* update expire time, should be done by process_chk */
2769 /* we allow up to min(inter, timeout.connect) for a connection
2770 * to establish but only when timeout.check is set
2771 * as it may be to short for a full check otherwise
2772 */
2773 while (tick_is_expired(t->expire, now_ms)) {
2774 int t_con;
2775
2776 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2777 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2778
2779 if (s->proxy->timeout.check)
2780 t->expire = tick_first(t->expire, t_con);
2781 }
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002782 return retcode;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002783 }
2784
2785 } /* end 'connect' */
2786 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2787 /* mark the step as started */
2788 check->last_started_step = check->current_step;
2789
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002790 /* reset the read buffer */
2791 if (*check->bi->data != '\0') {
2792 *check->bi->data = '\0';
2793 check->bi->i = 0;
2794 }
2795
Willy Tarreaubbae3f02017-08-30 09:59:52 +02002796 if (conn->flags & CO_FL_SOCK_WR_SH) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002797 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002798 chk_report_conn_err(check, 0, 0);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002799 goto out_end_tcpcheck;
2800 }
2801
Willy Tarreauabca5b62013-12-06 14:19:25 +01002802 if (check->current_step->string_len >= check->bo->size) {
2803 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2804 check->current_step->string_len, check->bo->size,
Simon Hormane16c1b32015-01-30 11:22:57 +09002805 tcpcheck_get_step_id(check));
Willy Tarreauabca5b62013-12-06 14:19:25 +01002806 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2807 goto out_end_tcpcheck;
2808 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002809
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002810 /* do not try to send if there is no space */
2811 if (check->current_step->string_len >= buffer_total_space(check->bo))
2812 continue;
2813
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002814 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2815 *check->bo->p = '\0'; /* to make gdb output easier to read */
2816
Willy Tarreauabca5b62013-12-06 14:19:25 +01002817 /* go to next rule and try to send */
Willy Tarreau5581c272015-05-13 12:24:53 +02002818 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002819
2820 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002821 while (&check->current_step->list != head &&
2822 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002823 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Willy Tarreauf2c87352015-05-13 12:08:21 +02002824
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002825 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002826 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002827 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002828 goto out_end_tcpcheck;
2829
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002830 __conn_xprt_want_recv(conn);
Willy Tarreau310987a2014-01-22 19:46:33 +01002831 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreau54e917c2017-08-30 07:35:35 +02002832 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002833 done = 1;
2834 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2835 /* Report network errors only if we got no other data. Otherwise
2836 * we'll let the upper layers decide whether the response is OK
2837 * or not. It is very common that an RST sent by the server is
2838 * reported as an error just after the last data chunk.
2839 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002840 chk_report_conn_err(check, errno, 0);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002841 goto out_end_tcpcheck;
2842 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002843 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002844 else
Willy Tarreau263013d2015-05-13 11:59:14 +02002845 break;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002846 }
2847
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002848 /* mark the step as started */
2849 check->last_started_step = check->current_step;
2850
2851
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002852 /* Intermediate or complete response received.
2853 * Terminate string in check->bi->data buffer.
2854 */
2855 if (check->bi->i < check->bi->size) {
2856 check->bi->data[check->bi->i] = '\0';
2857 }
2858 else {
2859 check->bi->data[check->bi->i - 1] = '\0';
2860 done = 1; /* buffer full, don't wait for more data */
2861 }
2862
2863 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002864
2865 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002866 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002867 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002868 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002869
2870 /* empty response */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002871 step = tcpcheck_get_step_id(check);
2872 chunk_printf(&trash, "TCPCHK got an empty response at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002873 comment = tcpcheck_get_step_comment(check, step);
2874 if (comment)
2875 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002876 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2877
2878 goto out_end_tcpcheck;
2879 }
2880
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002881 if (!done && (check->current_step->string != NULL) && (check->bi->i < check->current_step->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002882 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002883
Willy Tarreaua970c282013-12-06 12:47:19 +01002884 tcpcheck_expect:
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002885 if (check->current_step->string != NULL)
2886 ret = my_memmem(contentptr, check->bi->i, check->current_step->string, check->current_step->string_len) != NULL;
2887 else if (check->current_step->expect_regex != NULL)
2888 ret = regex_exec(check->current_step->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002889
2890 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002891 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002892
2893 /* matched */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002894 step = tcpcheck_get_step_id(check);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002895 if (ret) {
2896 /* matched but we did not want to => ERROR */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002897 if (check->current_step->inverse) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002898 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002899 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002900 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002901 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002902 }
2903 else {
2904 /* we were looking for a regex */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002905 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d", step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002906 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002907 comment = tcpcheck_get_step_comment(check, step);
2908 if (comment)
2909 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002910 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2911 goto out_end_tcpcheck;
2912 }
2913 /* matched and was supposed to => OK, next step */
2914 else {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002915 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002916 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002917
2918 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002919 while (&check->current_step->list != head &&
2920 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002921 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002922
Willy Tarreauf2c87352015-05-13 12:08:21 +02002923 if (&check->current_step->list == head)
2924 break;
2925
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002926 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002927 goto tcpcheck_expect;
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002928 __conn_xprt_stop_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002929 }
2930 }
2931 else {
2932 /* not matched */
2933 /* not matched and was not supposed to => OK, next step */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002934 if (check->current_step->inverse) {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002935 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002936 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002937
2938 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002939 while (&check->current_step->list != head &&
2940 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002941 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002942
Willy Tarreauf2c87352015-05-13 12:08:21 +02002943 if (&check->current_step->list == head)
2944 break;
2945
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002946 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002947 goto tcpcheck_expect;
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002948 __conn_xprt_stop_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002949 }
2950 /* not matched but was supposed to => ERROR */
2951 else {
2952 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002953 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002954 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002955 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002956 }
2957 else {
2958 /* we were looking for a regex */
2959 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002960 step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002961 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002962 comment = tcpcheck_get_step_comment(check, step);
2963 if (comment)
2964 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002965 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2966 goto out_end_tcpcheck;
2967 }
2968 }
2969 } /* end expect */
2970 } /* end loop over double chained step list */
2971
Willy Tarreau263013d2015-05-13 11:59:14 +02002972 /* We're waiting for some I/O to complete, we've reached the end of the
2973 * rules, or both. Do what we have to do, otherwise we're done.
2974 */
2975 if (&check->current_step->list == head && !check->bo->o) {
2976 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
2977 goto out_end_tcpcheck;
2978 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002979
Willy Tarreau53c5a042015-05-13 11:38:17 +02002980 /* warning, current_step may now point to the head */
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002981 if (check->bo->o)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002982 __conn_xprt_want_send(conn);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002983
Willy Tarreau53c5a042015-05-13 11:38:17 +02002984 if (&check->current_step->list != head &&
2985 check->current_step->action == TCPCHK_ACT_EXPECT)
Olivier Houchard1a0545f2017-09-13 18:30:23 +02002986 __conn_xprt_want_recv(conn);
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002987 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002988
2989 out_end_tcpcheck:
2990 /* collect possible new errors */
2991 if (conn->flags & CO_FL_ERROR)
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002992 chk_report_conn_err(check, 0, 0);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002993
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002994 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002995 check->current_step = NULL;
2996
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002997 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002998 conn->flags |= CO_FL_ERROR;
2999
Olivier Houchard1a0545f2017-09-13 18:30:23 +02003000 __conn_xprt_stop_both(conn);
Willy Tarreau6bdcab02017-10-04 18:41:00 +02003001 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003002}
3003
Simon Hormanb1900d52015-01-30 11:22:54 +09003004const char *init_check(struct check *check, int type)
3005{
3006 check->type = type;
3007
3008 /* Allocate buffer for requests... */
3009 if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3010 return "out of memory while allocating check buffer";
3011 }
3012 check->bi->size = global.tune.chksize;
3013
3014 /* Allocate buffer for responses... */
3015 if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3016 return "out of memory while allocating check buffer";
3017 }
3018 check->bo->size = global.tune.chksize;
3019
Simon Hormanb1900d52015-01-30 11:22:54 +09003020 return NULL;
3021}
3022
Simon Hormanbfb5d332015-01-30 11:22:55 +09003023void free_check(struct check *check)
3024{
3025 free(check->bi);
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01003026 check->bi = NULL;
Simon Hormanbfb5d332015-01-30 11:22:55 +09003027 free(check->bo);
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01003028 check->bo = NULL;
Simon Hormanbfb5d332015-01-30 11:22:55 +09003029 free(check->conn);
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01003030 check->conn = NULL;
Simon Hormanbfb5d332015-01-30 11:22:55 +09003031}
3032
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003033void email_alert_free(struct email_alert *alert)
3034{
3035 struct tcpcheck_rule *rule, *back;
3036
3037 if (!alert)
3038 return;
3039
Christopher Fauletde1a75b2017-10-23 15:38:19 +02003040 list_for_each_entry_safe(rule, back, &alert->tcpcheck_rules, list) {
3041 LIST_DEL(&rule->list);
3042 free(rule->comment);
3043 free(rule->string);
3044 if (rule->expect_regex)
3045 regex_free(rule->expect_regex);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003046 free(rule);
Christopher Fauletde1a75b2017-10-23 15:38:19 +02003047 }
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003048 free(alert);
3049}
3050
3051static struct task *process_email_alert(struct task *t)
3052{
Christopher Faulet0108bb32017-10-20 21:34:32 +02003053 struct check *check = t->context;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003054 struct email_alertq *q;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003055 struct email_alert *alert;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003056
3057 q = container_of(check, typeof(*q), check);
3058
Christopher Faulet0108bb32017-10-20 21:34:32 +02003059 while (1) {
3060 if (!(check->state & CHK_ST_ENABLED)) {
3061 if (LIST_ISEMPTY(&q->email_alerts)) {
3062 /* All alerts processed, queue the task */
3063 t->expire = TICK_ETERNITY;
3064 task_queue(t);
3065 return t;
3066 }
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003067
3068 alert = LIST_NEXT(&q->email_alerts, typeof(alert), list);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003069 LIST_DEL(&alert->list);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003070 t->expire = now_ms;
3071 check->server = alert->srv;
3072 check->tcpcheck_rules = &alert->tcpcheck_rules;
3073 check->status = HCHK_STATUS_INI;
3074 check->state |= CHK_ST_ENABLED;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003075 }
3076
Christopher Faulet0108bb32017-10-20 21:34:32 +02003077 process_chk(t);
3078 if (check->state & CHK_ST_INPROGRESS)
3079 break;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003080
3081 alert = container_of(check->tcpcheck_rules, typeof(*alert), tcpcheck_rules);
3082 email_alert_free(alert);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003083 check->tcpcheck_rules = NULL;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003084 check->server = NULL;
3085 check->state &= ~CHK_ST_ENABLED;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003086 }
3087 return t;
3088}
3089
Christopher Faulet0108bb32017-10-20 21:34:32 +02003090/* Initializes mailer alerts for the proxy <p> using <mls> parameters.
3091 *
3092 * The function returns 1 in success case, otherwise, it returns 0 and err is
3093 * filled.
3094 */
3095int init_email_alert(struct mailers *mls, struct proxy *p, char **err)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003096{
Christopher Faulet0108bb32017-10-20 21:34:32 +02003097 struct mailer *mailer;
3098 struct email_alertq *queues;
3099 const char *err_str;
3100 int i = 0;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003101
Christopher Faulet0108bb32017-10-20 21:34:32 +02003102 if ((queues = calloc(mls->count, sizeof(*queues))) == NULL) {
3103 memprintf(err, "out of memory while allocating mailer alerts queues");
3104 goto error;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003105 }
3106
Christopher Faulet0108bb32017-10-20 21:34:32 +02003107 for (mailer = mls->mailer_list; mailer; i++, mailer = mailer->next) {
3108 struct email_alertq *q = &queues[i];
3109 struct check *check = &q->check;
3110 struct task *t;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003111
3112 LIST_INIT(&q->email_alerts);
3113
Christopher Faulet0108bb32017-10-20 21:34:32 +02003114 check->inter = mls->timeout.mail;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003115 check->rise = DEF_AGENT_RISETIME;
3116 check->fall = DEF_AGENT_FALLTIME;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003117 if ((err_str = init_check(check, PR_O2_TCPCHK_CHK))) {
3118 memprintf(err, "%s", err_str);
3119 goto error;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003120 }
3121
3122 check->xprt = mailer->xprt;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003123 check->addr = mailer->addr;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003124 if (!get_host_port(&mailer->addr))
3125 /* Default to submission port */
3126 check->port = 587;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003127 //check->server = s;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003128
Christopher Faulet0108bb32017-10-20 21:34:32 +02003129 if ((t = task_new()) == NULL) {
3130 memprintf(err, "out of memory while allocating mailer alerts task");
3131 goto error;
3132 }
3133
3134 check->task = t;
3135 t->process = process_email_alert;
3136 t->context = check;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003137
Christopher Faulet0108bb32017-10-20 21:34:32 +02003138 /* check this in one ms */
3139 t->expire = TICK_ETERNITY;
3140 check->start = now;
3141 task_queue(t);
3142 }
3143
3144 mls->users++;
3145 free(p->email_alert.mailers.name);
3146 p->email_alert.mailers.m = mls;
3147 p->email_alert.queues = queues;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003148 return 0;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003149
3150 error:
3151 for (i = 0; i < mls->count; i++) {
3152 struct email_alertq *q = &queues[i];
3153 struct check *check = &q->check;
3154
3155 if (check->task) {
3156 task_delete(check->task);
3157 task_free(check->task);
3158 check->task = NULL;
3159 }
3160 free_check(check);
3161 }
3162 free(queues);
3163 return 1;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003164}
3165
3166
3167static int add_tcpcheck_expect_str(struct list *list, const char *str)
3168{
3169 struct tcpcheck_rule *tcpcheck;
3170
3171 tcpcheck = calloc(1, sizeof *tcpcheck);
3172 if (!tcpcheck)
3173 return 0;
3174
3175 tcpcheck->action = TCPCHK_ACT_EXPECT;
3176 tcpcheck->string = strdup(str);
3177 if (!tcpcheck->string) {
3178 free(tcpcheck);
3179 return 0;
3180 }
3181
3182 LIST_ADDQ(list, &tcpcheck->list);
3183 return 1;
3184}
3185
3186static int add_tcpcheck_send_strs(struct list *list, const char * const *strs)
3187{
3188 struct tcpcheck_rule *tcpcheck;
Willy Tarreau64345aa2016-08-10 19:29:09 +02003189 const char *in;
3190 char *dst;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003191 int i;
3192
3193 tcpcheck = calloc(1, sizeof *tcpcheck);
3194 if (!tcpcheck)
3195 return 0;
3196
3197 tcpcheck->action = TCPCHK_ACT_SEND;
3198
3199 tcpcheck->string_len = 0;
3200 for (i = 0; strs[i]; i++)
3201 tcpcheck->string_len += strlen(strs[i]);
3202
3203 tcpcheck->string = malloc(tcpcheck->string_len + 1);
3204 if (!tcpcheck->string) {
3205 free(tcpcheck);
3206 return 0;
3207 }
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003208
Willy Tarreau64345aa2016-08-10 19:29:09 +02003209 dst = tcpcheck->string;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003210 for (i = 0; strs[i]; i++)
Willy Tarreau64345aa2016-08-10 19:29:09 +02003211 for (in = strs[i]; (*dst = *in++); dst++);
3212 *dst = 0;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003213
3214 LIST_ADDQ(list, &tcpcheck->list);
3215 return 1;
3216}
3217
Christopher Faulet0108bb32017-10-20 21:34:32 +02003218static int enqueue_one_email_alert(struct proxy *p, struct server *s,
3219 struct email_alertq *q, const char *msg)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003220{
3221 struct email_alert *alert = NULL;
3222 struct tcpcheck_rule *tcpcheck;
3223 struct check *check = &q->check;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003224
3225 alert = calloc(1, sizeof *alert);
3226 if (!alert) {
3227 goto error;
3228 }
3229 LIST_INIT(&alert->tcpcheck_rules);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003230 alert->srv = s;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003231 tcpcheck = calloc(1, sizeof *tcpcheck);
3232 if (!tcpcheck)
3233 goto error;
3234 tcpcheck->action = TCPCHK_ACT_CONNECT;
3235 LIST_ADDQ(&alert->tcpcheck_rules, &tcpcheck->list);
3236
3237 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "220 "))
3238 goto error;
3239
3240 {
3241 const char * const strs[4] = { "EHLO ", p->email_alert.myhostname, "\r\n" };
3242 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3243 goto error;
3244 }
3245
3246 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3247 goto error;
3248
3249 {
3250 const char * const strs[4] = { "MAIL FROM:<", p->email_alert.from, ">\r\n" };
3251 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3252 goto error;
3253 }
3254
3255 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3256 goto error;
3257
3258 {
3259 const char * const strs[4] = { "RCPT TO:<", p->email_alert.to, ">\r\n" };
3260 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3261 goto error;
3262 }
3263
3264 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3265 goto error;
3266
3267 {
3268 const char * const strs[2] = { "DATA\r\n" };
3269 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3270 goto error;
3271 }
3272
3273 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "354 "))
3274 goto error;
3275
3276 {
3277 struct tm tm;
3278 char datestr[48];
3279 const char * const strs[18] = {
Pieter Baauw5e0964e2016-02-13 16:27:35 +01003280 "From: ", p->email_alert.from, "\r\n",
3281 "To: ", p->email_alert.to, "\r\n",
3282 "Date: ", datestr, "\r\n",
3283 "Subject: [HAproxy Alert] ", msg, "\r\n",
3284 "\r\n",
3285 msg, "\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003286 "\r\n",
Pieter Baauwed35c372015-07-22 19:51:54 +02003287 ".\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003288 NULL
3289 };
3290
3291 get_localtime(date.tv_sec, &tm);
3292
3293 if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %T %z (%Z)", &tm) == 0) {
3294 goto error;
3295 }
3296
3297 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3298 goto error;
3299 }
3300
3301 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3302 goto error;
3303
3304 {
3305 const char * const strs[2] = { "QUIT\r\n" };
3306 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3307 goto error;
3308 }
3309
3310 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "221 "))
3311 goto error;
3312
Christopher Faulet0108bb32017-10-20 21:34:32 +02003313 task_wakeup(check->task, TASK_WOKEN_MSG);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003314 LIST_ADDQ(&q->email_alerts, &alert->list);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003315 return 1;
3316
3317error:
3318 email_alert_free(alert);
3319 return 0;
3320}
3321
Christopher Faulet0108bb32017-10-20 21:34:32 +02003322static void enqueue_email_alert(struct proxy *p, struct server *s, const char *msg)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003323{
3324 int i;
3325 struct mailer *mailer;
3326
3327 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3328 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
Christopher Faulet0108bb32017-10-20 21:34:32 +02003329 if (!enqueue_one_email_alert(p, s, &p->email_alert.queues[i], msg)) {
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003330 Alert("Email alert [%s] could not be enqueued: out of memory\n", p->id);
3331 return;
3332 }
3333 }
3334
3335 return;
3336}
3337
3338/*
3339 * Send email alert if configured.
3340 */
Simon Horman64e34162015-02-06 11:11:57 +09003341void send_email_alert(struct server *s, int level, const char *format, ...)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003342{
3343 va_list argp;
3344 char buf[1024];
3345 int len;
3346 struct proxy *p = s->proxy;
3347
Christopher Faulet0108bb32017-10-20 21:34:32 +02003348 if (!p->email_alert.mailers.m || level > p->email_alert.level || format == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003349 return;
3350
3351 va_start(argp, format);
3352 len = vsnprintf(buf, sizeof(buf), format, argp);
3353 va_end(argp);
3354
Thierry FOURNIER62c8a212017-02-09 12:19:27 +01003355 if (len < 0 || len >= sizeof(buf)) {
Cyril Bontéb65e0332015-12-04 03:07:08 +01003356 Alert("Email alert [%s] could not format message\n", p->id);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003357 return;
3358 }
3359
Christopher Faulet0108bb32017-10-20 21:34:32 +02003360 enqueue_email_alert(p, s, buf);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003361}
3362
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003363/*
3364 * Return value:
3365 * the port to be used for the health check
3366 * 0 in case no port could be found for the check
3367 */
3368int srv_check_healthcheck_port(struct check *chk)
3369{
3370 int i = 0;
3371 struct server *srv = NULL;
3372
3373 srv = chk->server;
3374
3375 /* If neither a port nor an addr was specified and no check transport
3376 * layer is forced, then the transport layer used by the checks is the
3377 * same as for the production traffic. Otherwise we use raw_sock by
3378 * default, unless one is specified.
3379 */
3380 if (!chk->port && !is_addr(&chk->addr)) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003381 chk->use_ssl |= (srv->use_ssl || (srv->proxy->options & PR_O_TCPCHK_SSL));
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003382 chk->send_proxy |= (srv->pp_opts);
3383 }
3384
3385 /* by default, we use the health check port ocnfigured */
3386 if (chk->port > 0)
3387 return chk->port;
3388
3389 /* try to get the port from check_core.addr if check.port not set */
3390 i = get_host_port(&chk->addr);
3391 if (i > 0)
3392 return i;
3393
3394 /* try to get the port from server address */
3395 /* prevent MAPPORTS from working at this point, since checks could
3396 * not be performed in such case (MAPPORTS impose a relative ports
3397 * based on live traffic)
3398 */
3399 if (srv->flags & SRV_F_MAPPORTS)
3400 return 0;
Willy Tarreau04276f32017-01-06 17:41:29 +01003401
3402 i = srv->svc_port; /* by default */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003403 if (i > 0)
3404 return i;
3405
3406 return 0;
3407}
3408
Willy Tarreau865c5142016-12-21 20:04:48 +01003409__attribute__((constructor))
3410static void __check_init(void)
3411{
3412 hap_register_post_check(start_checks);
3413}
3414
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003415
Willy Tarreaubd741542010-03-16 18:46:54 +01003416/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003417 * Local variables:
3418 * c-indent-level: 8
3419 * c-basic-offset: 8
3420 * End:
3421 */