blob: 624a06594001cbcc82a797f481975d0cab535569 [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>
Christopher Fauletcfda8472017-10-20 15:40:23 +020038#include <common/hathreads.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039
40#include <types/global.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020041#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010042#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
44#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020045#include <proto/checks.h>
William Lallemand9ed62032016-11-21 17:49:11 +010046#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/fd.h>
48#include <proto/log.h>
Willy Tarreau53a47662017-08-28 10:53:00 +020049#include <proto/mux_pt.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020051#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010052#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010053#include <proto/proto_tcp.h>
Baptiste Assmann69e273f2013-12-11 00:52:19 +010054#include <proto/protocol.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010055#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/server.h>
Willy Tarreau48d6bf22016-06-21 16:27:34 +020057#include <proto/signal.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010058#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020060#include <proto/log.h>
61#include <proto/dns.h>
62#include <proto/proto_udp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063
Olivier Houchard9130a962017-10-17 17:33:43 +020064#ifdef USE_OPENSSL
65#include <proto/ssl_sock.h>
66#endif /* USE_OPENSSL */
67
Willy Tarreaubd741542010-03-16 18:46:54 +010068static int httpchk_expect(struct server *s, int done);
Simon Hormane16c1b32015-01-30 11:22:57 +090069static int tcpcheck_get_step_id(struct check *);
Baptiste Assmann22b09d22015-05-01 08:03:04 +020070static char * tcpcheck_get_step_comment(struct check *, int);
Willy Tarreau6bdcab02017-10-04 18:41:00 +020071static int tcpcheck_main(struct check *);
Olivier Houchard910b2bc2018-07-17 18:49:38 +020072static void __event_srv_chk_w(struct conn_stream *cs);
Willy Tarreaubd741542010-03-16 18:46:54 +010073
Willy Tarreaubafbe012017-11-24 17:34:44 +010074static struct pool_head *pool_head_email_alert = NULL;
75static struct pool_head *pool_head_tcpcheck_rule = NULL;
Christopher Faulet31dff9b2017-10-23 15:45:20 +020076
77
Simon Horman63a4a822012-03-19 07:24:41 +090078static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010079 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
80 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020081 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020082
Willy Tarreau23964182014-05-20 20:56:30 +020083 /* Below we have finished checks */
84 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010085 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010086
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010087 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020088
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010089 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
90 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
91 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020092
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010093 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
94 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
95 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020096
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010097 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
98 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020099
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200100 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200101
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100102 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
103 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
104 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +0900105
106 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
107 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +0200108 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200109};
110
Cyril Bontéac92a062014-12-27 22:28:38 +0100111const struct extcheck_env extcheck_envs[EXTCHK_SIZE] = {
112 [EXTCHK_PATH] = { "PATH", EXTCHK_SIZE_EVAL_INIT },
113 [EXTCHK_HAPROXY_PROXY_NAME] = { "HAPROXY_PROXY_NAME", EXTCHK_SIZE_EVAL_INIT },
114 [EXTCHK_HAPROXY_PROXY_ID] = { "HAPROXY_PROXY_ID", EXTCHK_SIZE_EVAL_INIT },
115 [EXTCHK_HAPROXY_PROXY_ADDR] = { "HAPROXY_PROXY_ADDR", EXTCHK_SIZE_EVAL_INIT },
116 [EXTCHK_HAPROXY_PROXY_PORT] = { "HAPROXY_PROXY_PORT", EXTCHK_SIZE_EVAL_INIT },
117 [EXTCHK_HAPROXY_SERVER_NAME] = { "HAPROXY_SERVER_NAME", EXTCHK_SIZE_EVAL_INIT },
118 [EXTCHK_HAPROXY_SERVER_ID] = { "HAPROXY_SERVER_ID", EXTCHK_SIZE_EVAL_INIT },
119 [EXTCHK_HAPROXY_SERVER_ADDR] = { "HAPROXY_SERVER_ADDR", EXTCHK_SIZE_EVAL_INIT },
120 [EXTCHK_HAPROXY_SERVER_PORT] = { "HAPROXY_SERVER_PORT", EXTCHK_SIZE_EVAL_INIT },
121 [EXTCHK_HAPROXY_SERVER_MAXCONN] = { "HAPROXY_SERVER_MAXCONN", EXTCHK_SIZE_EVAL_INIT },
122 [EXTCHK_HAPROXY_SERVER_CURCONN] = { "HAPROXY_SERVER_CURCONN", EXTCHK_SIZE_ULONG },
123};
124
Simon Horman63a4a822012-03-19 07:24:41 +0900125static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100126 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
127
128 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
129 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
130
131 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
132 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
133 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
134 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
135
136 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
137 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
138 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
139};
140
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200141/*
142 * Convert check_status code to description
143 */
144const char *get_check_status_description(short check_status) {
145
146 const char *desc;
147
148 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200149 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200150 else
151 desc = NULL;
152
153 if (desc && *desc)
154 return desc;
155 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200156 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200157}
158
159/*
160 * Convert check_status code to short info
161 */
162const char *get_check_status_info(short check_status) {
163
164 const char *info;
165
166 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200167 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200168 else
169 info = NULL;
170
171 if (info && *info)
172 return info;
173 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200174 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200175}
176
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100177const char *get_analyze_status(short analyze_status) {
178
179 const char *desc;
180
181 if (analyze_status < HANA_STATUS_SIZE)
182 desc = analyze_statuses[analyze_status].desc;
183 else
184 desc = NULL;
185
186 if (desc && *desc)
187 return desc;
188 else
189 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
190}
191
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200192/*
Simon Horman4a741432013-02-23 15:35:38 +0900193 * Set check->status, update check->duration and fill check->result with
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200194 * an adequate CHK_RES_* value. The new check->health is computed based
195 * on the result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200196 *
197 * Show information in logs about failed health check if server is UP
198 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200199 */
Simon Horman4a741432013-02-23 15:35:38 +0900200static void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100201{
Simon Horman4a741432013-02-23 15:35:38 +0900202 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200203 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200204 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900205
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200206 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100207 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900208 check->desc[0] = '\0';
209 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200210 return;
211 }
212
Simon Horman4a741432013-02-23 15:35:38 +0900213 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200214 return;
215
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200216 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900217 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
218 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200219 } else
Simon Horman4a741432013-02-23 15:35:38 +0900220 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200221
Simon Horman4a741432013-02-23 15:35:38 +0900222 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200223 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900224 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200225
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100226 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900227 check->duration = -1;
228 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200229 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900230 check->duration = tv_ms_elapsed(&check->start, &now);
231 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200232 }
233
Willy Tarreau23964182014-05-20 20:56:30 +0200234 /* no change is expected if no state change occurred */
235 if (check->result == CHK_RES_NEUTRAL)
236 return;
237
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200238 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200239
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200240 switch (check->result) {
241 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200242 /* Failure to connect to the agent as a secondary check should not
243 * cause the server to be marked down.
244 */
245 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900246 (check->status >= HCHK_STATUS_L57DATA)) &&
Christopher Fauletb119a792018-05-02 12:12:45 +0200247 (check->health > 0)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +0200248 HA_ATOMIC_ADD(&s->counters.failed_checks, 1);
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200249 report = 1;
250 check->health--;
251 if (check->health < check->rise)
252 check->health = 0;
253 }
254 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200255
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200256 case CHK_RES_PASSED:
257 case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */
258 if ((check->health < check->rise + check->fall - 1) &&
259 (check->result == CHK_RES_PASSED || check->health > 0)) {
260 report = 1;
261 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200262
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200263 if (check->health >= check->rise)
264 check->health = check->rise + check->fall - 1; /* OK now */
265 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200266
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200267 /* clear consecutive_errors if observing is enabled */
268 if (s->onerror)
269 s->consecutive_errors = 0;
270 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100271
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200272 default:
273 break;
274 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200275
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200276 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
277 (status != prev_status || report)) {
278 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200279 "%s check for %sserver %s/%s %s%s",
280 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200281 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100282 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100283 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200284 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200285
Emeric Brun5a133512017-10-19 14:42:30 +0200286 srv_append_status(&trash, s, check, -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200287
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100288 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200289 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
290 (check->health >= check->rise) ? check->fall : check->rise,
291 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200292
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200293 ha_warning("%s.\n", trash.area);
294 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
295 send_email_alert(s, LOG_INFO, "%s", trash.area);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200296 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200297}
298
Willy Tarreau4eec5472014-05-20 22:32:27 +0200299/* Marks the check <check>'s server down if the current check is already failed
300 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301 */
Willy Tarreau4eec5472014-05-20 22:32:27 +0200302static void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200303{
Simon Horman4a741432013-02-23 15:35:38 +0900304 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900305
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200306 /* The agent secondary check should only cause a server to be marked
307 * as down if check->status is HCHK_STATUS_L7STS, which indicates
308 * that the agent returned "fail", "stopped" or "down".
309 * The implication here is that failure to connect to the agent
310 * as a secondary check should not cause the server to be marked
311 * down. */
312 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
313 return;
314
Willy Tarreau4eec5472014-05-20 22:32:27 +0200315 if (check->health > 0)
316 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100317
Willy Tarreau4eec5472014-05-20 22:32:27 +0200318 /* We only report a reason for the check if we did not do so previously */
Emeric Brun5a133512017-10-19 14:42:30 +0200319 srv_set_stopped(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200320}
321
Willy Tarreauaf549582014-05-16 17:37:50 +0200322/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200323 * it isn't in maintenance, it is not tracking a down server and other checks
324 * comply. The rule is simple : by default, a server is up, unless any of the
325 * following conditions is true :
326 * - health check failed (check->health < rise)
327 * - agent check failed (agent->health < rise)
328 * - the server tracks a down server (track && track->state == STOPPED)
329 * Note that if the server has a slowstart, it will switch to STARTING instead
330 * of RUNNING. Also, only the health checks support the nolb mode, so the
331 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200332 */
Willy Tarreau3e048382014-05-21 10:30:54 +0200333static void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200334{
Simon Horman4a741432013-02-23 15:35:38 +0900335 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100336
Emeric Brun52a91d32017-08-31 14:41:55 +0200337 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200338 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100339
Emeric Brun52a91d32017-08-31 14:41:55 +0200340 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200341 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100342
Willy Tarreau3e048382014-05-21 10:30:54 +0200343 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
344 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100345
Willy Tarreau3e048382014-05-21 10:30:54 +0200346 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
347 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200348
Emeric Brun52a91d32017-08-31 14:41:55 +0200349 if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING)
Willy Tarreau3e048382014-05-21 10:30:54 +0200350 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100351
Emeric Brun5a133512017-10-19 14:42:30 +0200352 srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100353}
354
Willy Tarreaudb58b792014-05-21 13:57:23 +0200355/* Marks the check <check> as valid and tries to set its server into stopping mode
356 * if it was running or starting, and provided it isn't in maintenance and other
357 * checks comply. The conditions for the server to be marked in stopping mode are
358 * the same as for it to be turned up. Also, only the health checks support the
359 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200360 */
Willy Tarreaudb58b792014-05-21 13:57:23 +0200361static void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200362{
Simon Horman4a741432013-02-23 15:35:38 +0900363 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100364
Emeric Brun52a91d32017-08-31 14:41:55 +0200365 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200366 return;
367
Willy Tarreaudb58b792014-05-21 13:57:23 +0200368 if (check->state & CHK_ST_AGENT)
369 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100370
Emeric Brun52a91d32017-08-31 14:41:55 +0200371 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreaudb58b792014-05-21 13:57:23 +0200372 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100373
Willy Tarreaudb58b792014-05-21 13:57:23 +0200374 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
375 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100376
Willy Tarreaudb58b792014-05-21 13:57:23 +0200377 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
378 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100379
Willy Tarreaub26881a2017-12-23 11:16:49 +0100380 srv_set_stopping(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100381}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200382
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100383/* note: use health_adjust() only, which first checks that the observe mode is
384 * enabled.
385 */
386void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100387{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100388 int failed;
389 int expire;
390
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100391 if (s->observe >= HANA_OBS_SIZE)
392 return;
393
Willy Tarreaubb956662013-01-24 00:37:39 +0100394 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100395 return;
396
397 switch (analyze_statuses[status].lr[s->observe - 1]) {
398 case 1:
399 failed = 1;
400 break;
401
402 case 2:
403 failed = 0;
404 break;
405
406 default:
407 return;
408 }
409
410 if (!failed) {
411 /* good: clear consecutive_errors */
412 s->consecutive_errors = 0;
413 return;
414 }
415
Christopher Faulet29f77e82017-06-08 14:04:45 +0200416 HA_ATOMIC_ADD(&s->consecutive_errors, 1);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100417
418 if (s->consecutive_errors < s->consecutive_errors_limit)
419 return;
420
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100421 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
422 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100423
424 switch (s->onerror) {
425 case HANA_ONERR_FASTINTER:
426 /* force fastinter - nothing to do here as all modes force it */
427 break;
428
429 case HANA_ONERR_SUDDTH:
430 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900431 if (s->check.health > s->check.rise)
432 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100433
434 /* no break - fall through */
435
436 case HANA_ONERR_FAILCHK:
437 /* simulate a failed health check */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200438 set_server_check_status(&s->check, HCHK_STATUS_HANA,
439 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200440 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100441 break;
442
443 case HANA_ONERR_MARKDWN:
444 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900445 s->check.health = s->check.rise;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200446 set_server_check_status(&s->check, HCHK_STATUS_HANA,
447 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200448 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100449 break;
450
451 default:
452 /* write a warning? */
453 break;
454 }
455
456 s->consecutive_errors = 0;
Christopher Faulet29f77e82017-06-08 14:04:45 +0200457 HA_ATOMIC_ADD(&s->counters.failed_hana, 1);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100458
Simon Horman66183002013-02-23 10:16:43 +0900459 if (s->check.fastinter) {
460 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300461 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200462 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300463 /* requeue check task with new expire */
464 task_queue(s->check.task);
465 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100466 }
467}
468
Willy Tarreaua1dab552014-04-14 15:04:54 +0200469static int httpchk_build_status_header(struct server *s, char *buffer, int size)
Willy Tarreauef781042010-01-27 11:53:01 +0100470{
471 int sv_state;
472 int ratio;
473 int hlen = 0;
Joseph Lynch514061c2015-01-15 17:52:59 -0800474 char addr[46];
475 char port[6];
Willy Tarreauef781042010-01-27 11:53:01 +0100476 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
477 "UP %d/%d", "UP",
478 "NOLB %d/%d", "NOLB",
479 "no check" };
480
481 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
482 hlen += 24;
483
Willy Tarreauff5ae352013-12-11 20:36:34 +0100484 if (!(s->check.state & CHK_ST_ENABLED))
485 sv_state = 6;
Emeric Brun52a91d32017-08-31 14:41:55 +0200486 else if (s->cur_state != SRV_ST_STOPPED) {
Simon Horman58c32972013-11-25 10:46:38 +0900487 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100488 sv_state = 3; /* UP */
489 else
490 sv_state = 2; /* going down */
491
Emeric Brun52a91d32017-08-31 14:41:55 +0200492 if (s->cur_state == SRV_ST_STOPPING)
Willy Tarreauef781042010-01-27 11:53:01 +0100493 sv_state += 2;
494 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900495 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100496 sv_state = 1; /* going up */
497 else
498 sv_state = 0; /* DOWN */
499 }
500
Willy Tarreaua1dab552014-04-14 15:04:54 +0200501 hlen += snprintf(buffer + hlen, size - hlen,
Willy Tarreauef781042010-01-27 11:53:01 +0100502 srv_hlt_st[sv_state],
Emeric Brun52a91d32017-08-31 14:41:55 +0200503 (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
504 (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100505
Joseph Lynch514061c2015-01-15 17:52:59 -0800506 addr_to_str(&s->addr, addr, sizeof(addr));
Willy Tarreau04276f32017-01-06 17:41:29 +0100507 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
508 snprintf(port, sizeof(port), "%u", s->svc_port);
509 else
510 *port = 0;
Joseph Lynch514061c2015-01-15 17:52:59 -0800511
512 hlen += snprintf(buffer + hlen, size - hlen, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
513 addr, port, s->proxy->id, s->id,
Willy Tarreauef781042010-01-27 11:53:01 +0100514 global.node,
Emeric Brun52a91d32017-08-31 14:41:55 +0200515 (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
Willy Tarreauef781042010-01-27 11:53:01 +0100516 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
517 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
518 s->nbpend);
519
Emeric Brun52a91d32017-08-31 14:41:55 +0200520 if ((s->cur_state == SRV_ST_STARTING) &&
Willy Tarreauef781042010-01-27 11:53:01 +0100521 now.tv_sec < s->last_change + s->slowstart &&
522 now.tv_sec >= s->last_change) {
523 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
Willy Tarreaua1dab552014-04-14 15:04:54 +0200524 hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio);
Willy Tarreauef781042010-01-27 11:53:01 +0100525 }
526
527 buffer[hlen++] = '\r';
528 buffer[hlen++] = '\n';
529
530 return hlen;
531}
532
Willy Tarreau20a18342013-12-05 00:31:46 +0100533/* Check the connection. If an error has already been reported or the socket is
534 * closed, keep errno intact as it is supposed to contain the valid error code.
535 * If no error is reported, check the socket's error queue using getsockopt().
536 * Warning, this must be done only once when returning from poll, and never
537 * after an I/O error was attempted, otherwise the error queue might contain
538 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
539 * socket. Returns non-zero if an error was reported, zero if everything is
540 * clean (including a properly closed socket).
541 */
542static int retrieve_errno_from_socket(struct connection *conn)
543{
544 int skerr;
545 socklen_t lskerr = sizeof(skerr);
546
547 if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl))
548 return 1;
549
Willy Tarreau3c728722014-01-23 13:50:42 +0100550 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100551 return 0;
552
Willy Tarreau585744b2017-08-24 14:31:19 +0200553 if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
Willy Tarreau20a18342013-12-05 00:31:46 +0100554 errno = skerr;
555
556 if (errno == EAGAIN)
557 errno = 0;
558
559 if (!errno) {
560 /* we could not retrieve an error, that does not mean there is
561 * none. Just don't change anything and only report the prior
562 * error if any.
563 */
564 if (conn->flags & CO_FL_ERROR)
565 return 1;
566 else
567 return 0;
568 }
569
570 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
571 return 1;
572}
573
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100574/* Try to collect as much information as possible on the connection status,
575 * and adjust the server status accordingly. It may make use of <errno_bck>
576 * if non-null when the caller is absolutely certain of its validity (eg:
577 * checked just after a syscall). If the caller doesn't have a valid errno,
578 * it can pass zero, and retrieve_errno_from_socket() will be called to try
579 * to extract errno from the socket. If no error is reported, it will consider
580 * the <expired> flag. This is intended to be used when a connection error was
581 * reported in conn->flags or when a timeout was reported in <expired>. The
582 * function takes care of not updating a server status which was already set.
583 * All situations where at least one of <expired> or CO_FL_ERROR are set
584 * produce a status.
585 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200586static void chk_report_conn_err(struct check *check, int errno_bck, int expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100587{
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200588 struct conn_stream *cs = check->cs;
589 struct connection *conn = cs_conn(cs);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100590 const char *err_msg;
Willy Tarreau83061a82018-07-13 11:56:34 +0200591 struct buffer *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200592 int step;
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200593 char *comment;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100594
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100595 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100596 return;
597
598 errno = errno_bck;
Willy Tarreau00149122017-10-04 18:05:01 +0200599 if (conn && (!errno || errno == EAGAIN))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100600 retrieve_errno_from_socket(conn);
601
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200602 if (conn && !(conn->flags & CO_FL_ERROR) &&
603 !(cs->flags & CS_FL_ERROR) && !expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100604 return;
605
606 /* we'll try to build a meaningful error message depending on the
607 * context of the error possibly present in conn->err_code, and the
608 * socket error possibly collected above. This is useful to know the
609 * exact step of the L6 layer (eg: SSL handshake).
610 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200611 chk = get_trash_chunk();
612
613 if (check->type == PR_O2_TCPCHK_CHK) {
Simon Hormane16c1b32015-01-30 11:22:57 +0900614 step = tcpcheck_get_step_id(check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200615 if (!step)
616 chunk_printf(chk, " at initial connection step of tcp-check");
617 else {
618 chunk_printf(chk, " at step %d of tcp-check", step);
619 /* we were looking for a string */
620 if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_CONNECT) {
621 if (check->last_started_step->port)
622 chunk_appendf(chk, " (connect port %d)" ,check->last_started_step->port);
623 else
624 chunk_appendf(chk, " (connect)");
625 }
626 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_EXPECT) {
627 if (check->last_started_step->string)
Baptiste Assmann96a5c9b2015-05-01 08:09:29 +0200628 chunk_appendf(chk, " (expect string '%s')", check->last_started_step->string);
Willy Tarreau213c6782014-10-02 14:51:02 +0200629 else if (check->last_started_step->expect_regex)
630 chunk_appendf(chk, " (expect regex)");
631 }
632 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_SEND) {
633 chunk_appendf(chk, " (send)");
634 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200635
636 comment = tcpcheck_get_step_comment(check, step);
637 if (comment)
638 chunk_appendf(chk, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200639 }
640 }
641
Willy Tarreau00149122017-10-04 18:05:01 +0200642 if (conn && conn->err_code) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100643 if (errno && errno != EAGAIN)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200644 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno),
645 chk->area);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100646 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200647 chunk_printf(&trash, "%s%s", conn_err_code_str(conn),
648 chk->area);
649 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100650 }
651 else {
652 if (errno && errno != EAGAIN) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200653 chunk_printf(&trash, "%s%s", strerror(errno),
654 chk->area);
655 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100656 }
657 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200658 err_msg = chk->area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100659 }
660 }
661
Willy Tarreau00149122017-10-04 18:05:01 +0200662 if (check->state & CHK_ST_PORT_MISS) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200663 /* NOTE: this is reported after <fall> tries */
664 chunk_printf(chk, "No port available for the TCP connection");
665 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
666 }
667
Willy Tarreau00149122017-10-04 18:05:01 +0200668 if (!conn) {
669 /* connection allocation error before the connection was established */
670 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
671 }
672 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100673 /* L4 not established (yet) */
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200674 if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100675 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
676 else if (expired)
677 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200678
679 /*
680 * might be due to a server IP change.
681 * Let's trigger a DNS resolution if none are currently running.
682 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200683 dns_trigger_resolution(check->server->dns_requester);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200684
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100685 }
686 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
687 /* L6 not established (yet) */
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200688 if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100689 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
690 else if (expired)
691 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
692 }
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200693 else if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100694 /* I/O error after connection was established and before we could diagnose */
695 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
696 }
697 else if (expired) {
698 /* connection established but expired check */
699 if (check->type == PR_O2_SSL3_CHK)
700 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
701 else /* HTTP, SMTP, ... */
702 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
703 }
704
705 return;
706}
707
Willy Tarreaubaaee002006-06-26 02:48:02 +0200708/*
709 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200710 * the connection acknowledgement. If the proxy requires L7 health-checks,
711 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900712 * to set check->status, check->duration and check->result.
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200713 */
714static struct task *event_srv_chk_w(struct task *task, void *ctx, unsigned short state)
715{
716 struct conn_stream *cs = ctx;
717 struct check __maybe_unused *check = cs->data;
718
719 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
720 __event_srv_chk_w(cs);
721 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
722 return NULL;
723}
724
725/* same as above but protected by the server lock.
Willy Tarreau62ac84f2017-11-05 10:11:13 +0100726 *
727 * Please do NOT place any return statement in this function and only leave
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200728 * via the out label. NOTE THAT THIS FUNCTION DOESN'T LOCK, YOU PROBABLY WANT
729 * TO USE event_srv_chk_w() instead.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200730 */
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200731static void __event_srv_chk_w(struct conn_stream *cs)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200732{
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200733 struct connection *conn = cs->conn;
734 struct check *check = cs->data;
Simon Horman4a741432013-02-23 15:35:38 +0900735 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900736 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200737
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100738 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100739 goto out_wakeup;
740
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200741 if (conn->flags & CO_FL_HANDSHAKE) {
742 if (cs->wait_list.task->process != event_srv_chk_w) {
743 cs->wait_list.task->process = event_srv_chk_w;
744 cs->wait_list.task->context = cs;
745 }
746 LIST_ADDQ(&conn->send_wait_list, &cs->wait_list.list);
747 goto out;
748 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100749
Willy Tarreau20a18342013-12-05 00:31:46 +0100750 if (retrieve_errno_from_socket(conn)) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200751 chk_report_conn_err(check, errno, 0);
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200752 __cs_stop_both(cs);
Willy Tarreau20a18342013-12-05 00:31:46 +0100753 goto out_wakeup;
754 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100755
Willy Tarreaubbae3f02017-08-30 09:59:52 +0200756 if (conn->flags & CO_FL_SOCK_WR_SH) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100757 /* if the output is closed, we can't do anything */
758 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200759 chk_report_conn_err(check, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100760 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200761 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200762
Willy Tarreau06559ac2013-12-05 01:53:08 +0100763 /* here, we know that the connection is established. That's enough for
764 * a pure TCP check.
765 */
766 if (!check->type)
767 goto out_wakeup;
768
Willy Tarreauc09572f2017-10-04 11:58:22 +0200769 /* wake() will take care of calling tcpcheck_main() */
Willy Tarreau62ac84f2017-11-05 10:11:13 +0100770 if (check->type == PR_O2_TCPCHK_CHK)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200771 goto out;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200772
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200773 if (b_data(&check->bo)) {
774 b_del(&check->bo, conn->mux->snd_buf(cs, &check->bo, b_data(&check->bo), 0));
775 b_realign_if_empty(&check->bo);
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200776 if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200777 chk_report_conn_err(check, errno, 0);
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200778 __cs_stop_both(cs);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100779 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200780 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200781 if (b_data(&check->bo)) {
782 if (!cs->wait_list.task->process) {
783 cs->wait_list.task->process = event_srv_chk_w;
784 cs->wait_list.task->context = cs;
785 }
786 conn->mux->subscribe(cs, SUB_CAN_SEND, &cs->wait_list);
787 goto out;
788 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100789 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200790
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100791 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
792 if (s->proxy->timeout.check) {
793 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
794 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200795 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100796 goto out_nowake;
797
Willy Tarreau83749182007-04-15 20:56:27 +0200798 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200799 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200800 out_nowake:
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200801 __cs_stop_send(cs); /* nothing more to write */
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200802 out:
803 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200804}
805
Willy Tarreaubaaee002006-06-26 02:48:02 +0200806/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200807 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200808 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900809 * set_server_check_status() to update check->status, check->duration
810 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200811
812 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
813 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
814 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
815 * response to an SSL HELLO (the principle is that this is enough to
816 * distinguish between an SSL server and a pure TCP relay). All other cases will
817 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
818 * etc.
Willy Tarreau62ac84f2017-11-05 10:11:13 +0100819 *
820 * Please do NOT place any return statement in this function and only leave
821 * via the out_unlock label.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200822 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200823static void event_srv_chk_r(struct conn_stream *cs)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200824{
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200825 struct connection *conn = cs->conn;
826 struct check *check = cs->data;
Simon Horman4a741432013-02-23 15:35:38 +0900827 struct server *s = check->server;
828 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200829 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100830 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200831 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200832
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100833 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200834
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100835 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +0200836 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200837
Willy Tarreau62ac84f2017-11-05 10:11:13 +0100838 if (conn->flags & CO_FL_HANDSHAKE)
839 goto out_unlock;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200840
Willy Tarreauc09572f2017-10-04 11:58:22 +0200841 /* wake() will take care of calling tcpcheck_main() */
Willy Tarreau62ac84f2017-11-05 10:11:13 +0100842 if (check->type == PR_O2_TCPCHK_CHK)
843 goto out_unlock;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200844
Willy Tarreau83749182007-04-15 20:56:27 +0200845 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
846 * but the connection was closed on the remote end. Fortunately, recv still
847 * works correctly and we don't need to do the getsockopt() on linux.
848 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000849
850 /* Set buffer to point to the end of the data already read, and check
851 * that there is free space remaining. If the buffer is full, proceed
852 * with running the checks without attempting another socket read.
853 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000854
Willy Tarreau03938182010-03-17 21:52:07 +0100855 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +0000856
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200857 conn->mux->rcv_buf(cs, &check->bi, b_size(&check->bi), 0);
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200858 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH) || cs->flags & CS_FL_ERROR) {
Willy Tarreau03938182010-03-17 21:52:07 +0100859 done = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200860 if ((conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) && !b_data(&check->bi)) {
Willy Tarreauf1503172012-09-28 19:39:36 +0200861 /* Report network errors only if we got no other data. Otherwise
862 * we'll let the upper layers decide whether the response is OK
863 * or not. It is very common that an RST sent by the server is
864 * reported as an error just after the last data chunk.
865 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +0200866 chk_report_conn_err(check, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +0100867 goto out_wakeup;
868 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200869 }
870
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100871
Willy Tarreau03938182010-03-17 21:52:07 +0100872 /* Intermediate or complete response received.
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200873 * Terminate string in b_head(&check->bi) buffer.
Willy Tarreau03938182010-03-17 21:52:07 +0100874 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200875 if (b_data(&check->bi) < b_size(&check->bi))
876 b_head(&check->bi)[b_data(&check->bi)] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100877 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200878 b_head(&check->bi)[b_data(&check->bi) - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100879 done = 1; /* buffer full, don't wait for more data */
880 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200881
Nick Chalk57b1bf72010-03-16 15:50:46 +0000882 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +0900883 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +0200884 case PR_O2_HTTP_CHK:
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200885 if (!done && b_data(&check->bi) < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100886 goto wait_more_data;
887
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100888 /* Check if the server speaks HTTP 1.X */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200889 if ((b_data(&check->bi) < strlen("HTTP/1.0 000\r")) ||
890 (memcmp(b_head(&check->bi), "HTTP/1.", 7) != 0 ||
891 (*(b_head(&check->bi) + 12) != ' ' && *(b_head(&check->bi) + 12) != '\r')) ||
892 !isdigit((unsigned char) *(b_head(&check->bi) + 9)) || !isdigit((unsigned char) *(b_head(&check->bi) + 10)) ||
893 !isdigit((unsigned char) *(b_head(&check->bi) + 11))) {
894 cut_crlf(b_head(&check->bi));
895 set_server_check_status(check, HCHK_STATUS_L7RSP, b_head(&check->bi));
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200896
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100897 goto out_wakeup;
898 }
899
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200900 check->code = str2uic(b_head(&check->bi) + 9);
901 desc = ltrim(b_head(&check->bi) + 12, ' ');
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200902
Willy Tarreaubd741542010-03-16 18:46:54 +0100903 if ((s->proxy->options & PR_O_DISABLE404) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200904 (s->next_state != SRV_ST_STOPPED) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000905 /* 404 may be accepted as "stopping" only if the server was up */
906 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900907 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000908 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100909 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
910 /* Run content verification check... We know we have at least 13 chars */
911 if (!httpchk_expect(s, done))
912 goto wait_more_data;
913 }
914 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200915 else if (*(b_head(&check->bi) + 9) == '2' || *(b_head(&check->bi) + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +0100916 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900917 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +0100918 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000919 else {
920 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900921 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000922 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200923 break;
924
925 case PR_O2_SSL3_CHK:
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200926 if (!done && b_data(&check->bi) < 5)
Willy Tarreau03938182010-03-17 21:52:07 +0100927 goto wait_more_data;
928
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100929 /* Check for SSLv3 alert or handshake */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200930 if ((b_data(&check->bi) >= 5) && (*b_head(&check->bi) == 0x15 || *b_head(&check->bi) == 0x16))
Simon Horman4a741432013-02-23 15:35:38 +0900931 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200932 else
Simon Horman4a741432013-02-23 15:35:38 +0900933 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200934 break;
935
936 case PR_O2_SMTP_CHK:
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200937 if (!done && b_data(&check->bi) < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100938 goto wait_more_data;
939
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200940 /* Check if the server speaks SMTP */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200941 if ((b_data(&check->bi) < strlen("000\r")) ||
942 (*(b_head(&check->bi) + 3) != ' ' && *(b_head(&check->bi) + 3) != '\r') ||
943 !isdigit((unsigned char) *b_head(&check->bi)) || !isdigit((unsigned char) *(b_head(&check->bi) + 1)) ||
944 !isdigit((unsigned char) *(b_head(&check->bi) + 2))) {
945 cut_crlf(b_head(&check->bi));
946 set_server_check_status(check, HCHK_STATUS_L7RSP, b_head(&check->bi));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200947 goto out_wakeup;
948 }
949
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200950 check->code = str2uic(b_head(&check->bi));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200951
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200952 desc = ltrim(b_head(&check->bi) + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200953 cut_crlf(desc);
954
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100955 /* Check for SMTP code 2xx (should be 250) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200956 if (*b_head(&check->bi) == '2')
Simon Horman4a741432013-02-23 15:35:38 +0900957 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200958 else
Simon Horman4a741432013-02-23 15:35:38 +0900959 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200960 break;
961
Simon Hormana2b9dad2013-02-12 10:45:54 +0900962 case PR_O2_LB_AGENT_CHK: {
Willy Tarreau81f5d942013-12-09 20:51:51 +0100963 int status = HCHK_STATUS_CHECKED;
964 const char *hs = NULL; /* health status */
965 const char *as = NULL; /* admin status */
966 const char *ps = NULL; /* performance status */
Nenad Merdanovic174dd372016-04-24 23:10:06 +0200967 const char *cs = NULL; /* maxconn */
Willy Tarreau81f5d942013-12-09 20:51:51 +0100968 const char *err = NULL; /* first error to report */
969 const char *wrn = NULL; /* first warning to report */
970 char *cmd, *p;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900971
Willy Tarreau81f5d942013-12-09 20:51:51 +0100972 /* We're getting an agent check response. The agent could
973 * have been disabled in the mean time with a long check
974 * still pending. It is important that we ignore the whole
975 * response.
976 */
977 if (!(check->server->agent.state & CHK_ST_ENABLED))
978 break;
979
980 /* The agent supports strings made of a single line ended by the
981 * first CR ('\r') or LF ('\n'). This line is composed of words
982 * delimited by spaces (' '), tabs ('\t'), or commas (','). The
983 * line may optionally contained a description of a state change
984 * after a sharp ('#'), which is only considered if a health state
985 * is announced.
986 *
987 * Words may be composed of :
988 * - a numeric weight suffixed by the percent character ('%').
989 * - a health status among "up", "down", "stopped", and "fail".
990 * - an admin status among "ready", "drain", "maint".
991 *
992 * These words may appear in any order. If multiple words of the
993 * same category appear, the last one wins.
994 */
995
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200996 p = b_head(&check->bi);
Willy Tarreau9809b782013-12-11 21:40:11 +0100997 while (*p && *p != '\n' && *p != '\r')
998 p++;
999
1000 if (!*p) {
1001 if (!done)
1002 goto wait_more_data;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001003
Willy Tarreau9809b782013-12-11 21:40:11 +01001004 /* at least inform the admin that the agent is mis-behaving */
1005 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
1006 break;
1007 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001008
Willy Tarreau9809b782013-12-11 21:40:11 +01001009 *p = 0;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001010 cmd = b_head(&check->bi);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001011
Willy Tarreau81f5d942013-12-09 20:51:51 +01001012 while (*cmd) {
1013 /* look for next word */
1014 if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') {
1015 cmd++;
1016 continue;
1017 }
Simon Horman671b6f02013-11-25 10:46:39 +09001018
Willy Tarreau81f5d942013-12-09 20:51:51 +01001019 if (*cmd == '#') {
1020 /* this is the beginning of a health status description,
1021 * skip the sharp and blanks.
1022 */
1023 cmd++;
1024 while (*cmd == '\t' || *cmd == ' ')
1025 cmd++;
Simon Horman671b6f02013-11-25 10:46:39 +09001026 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001027 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001028
1029 /* find the end of the word so that we have a null-terminated
1030 * word between <cmd> and <p>.
1031 */
1032 p = cmd + 1;
1033 while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',')
1034 p++;
1035 if (*p)
1036 *p++ = 0;
1037
1038 /* first, health statuses */
1039 if (strcasecmp(cmd, "up") == 0) {
1040 check->health = check->rise + check->fall - 1;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001041 status = HCHK_STATUS_L7OKD;
Willy Tarreau81f5d942013-12-09 20:51:51 +01001042 hs = cmd;
1043 }
1044 else if (strcasecmp(cmd, "down") == 0) {
1045 check->health = 0;
1046 status = HCHK_STATUS_L7STS;
1047 hs = cmd;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001048 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001049 else if (strcasecmp(cmd, "stopped") == 0) {
1050 check->health = 0;
1051 status = HCHK_STATUS_L7STS;
1052 hs = cmd;
1053 }
1054 else if (strcasecmp(cmd, "fail") == 0) {
1055 check->health = 0;
1056 status = HCHK_STATUS_L7STS;
1057 hs = cmd;
1058 }
1059 /* admin statuses */
1060 else if (strcasecmp(cmd, "ready") == 0) {
1061 as = cmd;
1062 }
1063 else if (strcasecmp(cmd, "drain") == 0) {
1064 as = cmd;
1065 }
1066 else if (strcasecmp(cmd, "maint") == 0) {
1067 as = cmd;
1068 }
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001069 /* try to parse a weight here and keep the last one */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001070 else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) {
1071 ps = cmd;
1072 }
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001073 /* try to parse a maxconn here */
1074 else if (strncasecmp(cmd, "maxconn:", strlen("maxconn:")) == 0) {
1075 cs = cmd;
1076 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001077 else {
1078 /* keep a copy of the first error */
1079 if (!err)
1080 err = cmd;
1081 }
1082 /* skip to next word */
1083 cmd = p;
1084 }
1085 /* here, cmd points either to \0 or to the beginning of a
1086 * description. Skip possible leading spaces.
1087 */
1088 while (*cmd == ' ' || *cmd == '\n')
1089 cmd++;
1090
1091 /* First, update the admin status so that we avoid sending other
1092 * possibly useless warnings and can also update the health if
1093 * present after going back up.
1094 */
1095 if (as) {
1096 if (strcasecmp(as, "drain") == 0)
1097 srv_adm_set_drain(check->server);
1098 else if (strcasecmp(as, "maint") == 0)
1099 srv_adm_set_maint(check->server);
1100 else
1101 srv_adm_set_ready(check->server);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001102 }
1103
Willy Tarreau81f5d942013-12-09 20:51:51 +01001104 /* now change weights */
1105 if (ps) {
1106 const char *msg;
1107
1108 msg = server_parse_weight_change_request(s, ps);
1109 if (!wrn || !*wrn)
1110 wrn = msg;
1111 }
1112
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001113 if (cs) {
1114 const char *msg;
1115
1116 cs += strlen("maxconn:");
1117
1118 msg = server_parse_maxconn_change_request(s, cs);
1119 if (!wrn || !*wrn)
1120 wrn = msg;
1121 }
1122
Willy Tarreau81f5d942013-12-09 20:51:51 +01001123 /* and finally health status */
1124 if (hs) {
1125 /* We'll report some of the warnings and errors we have
1126 * here. Down reports are critical, we leave them untouched.
1127 * Lack of report, or report of 'UP' leaves the room for
1128 * ERR first, then WARN.
Simon Hormana2b9dad2013-02-12 10:45:54 +09001129 */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001130 const char *msg = cmd;
Willy Tarreau83061a82018-07-13 11:56:34 +02001131 struct buffer *t;
Willy Tarreau81f5d942013-12-09 20:51:51 +01001132
1133 if (!*msg || status == HCHK_STATUS_L7OKD) {
1134 if (err && *err)
1135 msg = err;
1136 else if (wrn && *wrn)
1137 msg = wrn;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001138 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001139
1140 t = get_trash_chunk();
1141 chunk_printf(t, "via agent : %s%s%s%s",
1142 hs, *msg ? " (" : "",
1143 msg, *msg ? ")" : "");
1144
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001145 set_server_check_status(check, status, t->area);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001146 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001147 else if (err && *err) {
1148 /* No status change but we'd like to report something odd.
1149 * Just report the current state and copy the message.
1150 */
1151 chunk_printf(&trash, "agent reports an error : %s", err);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001152 set_server_check_status(check, status/*check->status*/,
1153 trash.area);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001154
Willy Tarreau81f5d942013-12-09 20:51:51 +01001155 }
1156 else if (wrn && *wrn) {
1157 /* No status change but we'd like to report something odd.
1158 * Just report the current state and copy the message.
1159 */
1160 chunk_printf(&trash, "agent warns : %s", wrn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001161 set_server_check_status(check, status/*check->status*/,
1162 trash.area);
Willy Tarreau81f5d942013-12-09 20:51:51 +01001163 }
1164 else
1165 set_server_check_status(check, status, NULL);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001166 break;
1167 }
1168
Willy Tarreau1620ec32011-08-06 17:05:02 +02001169 case PR_O2_PGSQL_CHK:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001170 if (!done && b_data(&check->bi) < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001171 goto wait_more_data;
1172
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001173 if (b_head(&check->bi)[0] == 'R') {
Simon Horman4a741432013-02-23 15:35:38 +09001174 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001175 }
1176 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001177 if ((b_head(&check->bi)[0] == 'E') && (b_head(&check->bi)[5]!=0) && (b_head(&check->bi)[6]!=0))
1178 desc = &b_head(&check->bi)[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001179 else
1180 desc = "PostgreSQL unknown error";
1181
Simon Horman4a741432013-02-23 15:35:38 +09001182 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001183 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001184 break;
1185
1186 case PR_O2_REDIS_CHK:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001187 if (!done && b_data(&check->bi) < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001188 goto wait_more_data;
1189
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001190 if (strcmp(b_head(&check->bi), "+PONG\r\n") == 0) {
Simon Horman4a741432013-02-23 15:35:38 +09001191 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001192 }
1193 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001194 set_server_check_status(check, HCHK_STATUS_L7STS, b_head(&check->bi));
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001195 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001196 break;
1197
1198 case PR_O2_MYSQL_CHK:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001199 if (!done && b_data(&check->bi) < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001200 goto wait_more_data;
1201
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001202 if (s->proxy->check_len == 0) { // old mode
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001203 if (*(b_head(&check->bi) + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001204 /* We set the MySQL Version in description for information purpose
1205 * FIXME : it can be cool to use MySQL Version for other purpose,
1206 * like mark as down old MySQL server.
1207 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001208 if (b_data(&check->bi) > 51) {
1209 desc = ltrim(b_head(&check->bi) + 5, ' ');
Simon Horman4a741432013-02-23 15:35:38 +09001210 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001211 }
1212 else {
1213 if (!done)
1214 goto wait_more_data;
Emeric Brun9f0b4582017-10-23 14:39:51 +02001215
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001216 /* it seems we have a OK packet but without a valid length,
1217 * it must be a protocol error
1218 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001219 set_server_check_status(check, HCHK_STATUS_L7RSP, b_head(&check->bi));
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001220 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001221 }
1222 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001223 /* An error message is attached in the Error packet */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001224 desc = ltrim(b_head(&check->bi) + 7, ' ');
Simon Horman4a741432013-02-23 15:35:38 +09001225 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001226 }
1227 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001228 unsigned int first_packet_len = ((unsigned int) *b_head(&check->bi)) +
1229 (((unsigned int) *(b_head(&check->bi) + 1)) << 8) +
1230 (((unsigned int) *(b_head(&check->bi) + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001231
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001232 if (b_data(&check->bi) == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001233 /* MySQL Error packet always begin with field_count = 0xff */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001234 if (*(b_head(&check->bi) + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001235 /* We have only one MySQL packet and it is a Handshake Initialization packet
1236 * but we need to have a second packet to know if it is alright
1237 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001238 if (!done && b_data(&check->bi) < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001239 goto wait_more_data;
1240 }
1241 else {
1242 /* We have only one packet and it is an Error packet,
1243 * an error message is attached, so we can display it
1244 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001245 desc = &b_head(&check->bi)[7];
Christopher Faulet767a84b2017-11-24 16:50:31 +01001246 //ha_warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001247 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001248 }
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001249 } else if (b_data(&check->bi) > first_packet_len + 4) {
1250 unsigned int second_packet_len = ((unsigned int) *(b_head(&check->bi) + first_packet_len + 4)) +
1251 (((unsigned int) *(b_head(&check->bi) + first_packet_len + 5)) << 8) +
1252 (((unsigned int) *(b_head(&check->bi) + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001253
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001254 if (b_data(&check->bi) == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001255 /* We have 2 packets and that's good */
1256 /* Check if the second packet is a MySQL Error packet or not */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001257 if (*(b_head(&check->bi) + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001258 /* No error packet */
1259 /* We set the MySQL Version in description for information purpose */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001260 desc = &b_head(&check->bi)[5];
Christopher Faulet767a84b2017-11-24 16:50:31 +01001261 //ha_warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001262 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001263 }
1264 else {
1265 /* An error message is attached in the Error packet
1266 * so we can display it ! :)
1267 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001268 desc = &b_head(&check->bi)[first_packet_len+11];
Christopher Faulet767a84b2017-11-24 16:50:31 +01001269 //ha_warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001270 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001271 }
1272 }
1273 }
1274 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001275 if (!done)
1276 goto wait_more_data;
Emeric Brun9f0b4582017-10-23 14:39:51 +02001277
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001278 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001279 * it must be a protocol error
1280 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001281 desc = &b_head(&check->bi)[5];
Christopher Faulet767a84b2017-11-24 16:50:31 +01001282 //ha_warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001283 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001284 }
1285 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001286 break;
1287
1288 case PR_O2_LDAP_CHK:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001289 if (!done && b_data(&check->bi) < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001290 goto wait_more_data;
1291
1292 /* Check if the server speaks LDAP (ASN.1/BER)
1293 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1294 * http://tools.ietf.org/html/rfc4511
1295 */
1296
1297 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1298 * LDAPMessage: 0x30: SEQUENCE
1299 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001300 if ((b_data(&check->bi) < 14) || (*(b_head(&check->bi)) != '\x30')) {
Simon Horman4a741432013-02-23 15:35:38 +09001301 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001302 }
1303 else {
1304 /* size of LDAPMessage */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001305 msglen = (*(b_head(&check->bi) + 1) & 0x80) ? (*(b_head(&check->bi) + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001306
1307 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1308 * messageID: 0x02 0x01 0x01: INTEGER 1
1309 * protocolOp: 0x61: bindResponse
1310 */
1311 if ((msglen > 2) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001312 (memcmp(b_head(&check->bi) + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
Simon Horman4a741432013-02-23 15:35:38 +09001313 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001314 goto out_wakeup;
1315 }
1316
1317 /* size of bindResponse */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001318 msglen += (*(b_head(&check->bi) + msglen + 6) & 0x80) ? (*(b_head(&check->bi) + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001319
1320 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1321 * ldapResult: 0x0a 0x01: ENUMERATION
1322 */
1323 if ((msglen > 4) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001324 (memcmp(b_head(&check->bi) + 7 + msglen, "\x0a\x01", 2) != 0)) {
Simon Horman4a741432013-02-23 15:35:38 +09001325 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001326 goto out_wakeup;
1327 }
1328
1329 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1330 * resultCode
1331 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001332 check->code = *(b_head(&check->bi) + msglen + 9);
Simon Horman4a741432013-02-23 15:35:38 +09001333 if (check->code) {
1334 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 +02001335 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001336 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001337 }
1338 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001339 break;
1340
Christopher Fauletba7bc162016-11-07 21:07:38 +01001341 case PR_O2_SPOP_CHK: {
1342 unsigned int framesz;
1343 char err[HCHK_DESC_LEN];
1344
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001345 if (!done && b_data(&check->bi) < 4)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001346 goto wait_more_data;
1347
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001348 memcpy(&framesz, b_head(&check->bi), 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001349 framesz = ntohl(framesz);
1350
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001351 if (!done && b_data(&check->bi) < (4+framesz))
Christopher Fauletba7bc162016-11-07 21:07:38 +01001352 goto wait_more_data;
1353
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001354 if (!spoe_handle_healthcheck_response(b_head(&check->bi)+4, framesz, err, HCHK_DESC_LEN-1))
Christopher Fauletba7bc162016-11-07 21:07:38 +01001355 set_server_check_status(check, HCHK_STATUS_L7OKD, "SPOA server is ok");
1356 else
1357 set_server_check_status(check, HCHK_STATUS_L7STS, err);
1358 break;
1359 }
1360
Willy Tarreau1620ec32011-08-06 17:05:02 +02001361 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001362 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001363 break;
1364 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001365
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001366 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001367 /* collect possible new errors */
Willy Tarreau4ff3b892017-10-16 15:17:17 +02001368 if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)
Willy Tarreaub5259bf2017-10-04 14:47:29 +02001369 chk_report_conn_err(check, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001370
Nick Chalk57b1bf72010-03-16 15:50:46 +00001371 /* Reset the check buffer... */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001372 *b_head(&check->bi) = '\0';
1373 b_reset(&check->bi);
Nick Chalk57b1bf72010-03-16 15:50:46 +00001374
Steven Davidovitz544d4812017-03-08 11:06:20 -08001375 /* Close the connection... We still attempt to nicely close if,
1376 * for instance, SSL needs to send a "close notify." Later, we perform
1377 * a hard close and reset the connection if some data are pending,
1378 * otherwise we end up with many TIME_WAITs and eat all the source port
1379 * range quickly. To avoid sending RSTs all the time, we first try to
1380 * drain pending data.
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001381 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001382 __cs_stop_both(cs);
Willy Tarreauecdb3fe2017-10-05 15:25:48 +02001383 cs_shutw(cs, CS_SHW_NORMAL);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001384
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001385 /* OK, let's not stay here forever */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001386 if (check->result == CHK_RES_FAILED)
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001387 conn->flags |= CO_FL_ERROR;
1388
Willy Tarreaufdccded2008-08-29 18:19:04 +02001389 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau62ac84f2017-11-05 10:11:13 +01001390 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001391 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
Willy Tarreau3267d362012-08-17 23:53:56 +02001392 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001393
1394 wait_more_data:
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001395 __cs_want_recv(cs);
Willy Tarreau62ac84f2017-11-05 10:11:13 +01001396 goto out_unlock;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001397}
1398
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001399/*
1400 * This function is used only for server health-checks. It handles connection
1401 * status updates including errors. If necessary, it wakes the check task up.
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001402 * It returns 0 on normal cases, <0 if at least one close() has happened on the
1403 * connection (eg: reconnect).
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001404 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001405static int wake_srv_chk(struct conn_stream *cs)
Willy Tarreau20bea422012-07-06 12:00:49 +02001406{
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001407 struct connection *conn = cs->conn;
1408 struct check *check = cs->data;
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001409 int ret = 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001410
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001411 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
Emeric Brunf6ba17d2017-11-02 14:35:27 +01001412
Willy Tarreauc09572f2017-10-04 11:58:22 +02001413 /* we may have to make progress on the TCP checks */
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001414 if (check->type == PR_O2_TCPCHK_CHK) {
1415 ret = tcpcheck_main(check);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001416 cs = check->cs;
1417 conn = cs_conn(cs);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02001418 } else if (LIST_ISEMPTY(&cs->wait_list.list))
1419 __event_srv_chk_w(cs);
Willy Tarreauc09572f2017-10-04 11:58:22 +02001420
Willy Tarreau4ff3b892017-10-16 15:17:17 +02001421 if (unlikely(conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001422 /* We may get error reports bypassing the I/O handlers, typically
1423 * the case when sending a pure TCP check which fails, then the I/O
1424 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001425 * main processing task so let's simply wake it up. If we get here,
1426 * we expect errno to still be valid.
1427 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +02001428 chk_report_conn_err(check, errno, 0);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001429 __cs_stop_both(cs);
Willy Tarreau2d351b62013-12-05 02:36:25 +01001430 task_wakeup(check->task, TASK_WOKEN_IO);
1431 }
Willy Tarreau2500fc22018-04-03 19:31:38 +02001432 else if (!(conn->flags & CO_FL_HANDSHAKE) && !(cs->flags & (CS_FL_DATA_RD_ENA|CS_FL_DATA_WR_ENA))) {
Willy Tarreau3be293f2014-02-05 18:31:24 +01001433 /* we may get here if only a connection probe was required : we
1434 * don't have any data to send nor anything expected in response,
1435 * so the completion of the connection establishment is enough.
1436 */
1437 task_wakeup(check->task, TASK_WOKEN_IO);
1438 }
Willy Tarreau2d351b62013-12-05 02:36:25 +01001439
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001440 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001441 /* We're here because nobody wants to handle the error, so we
1442 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001443 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01001444 conn_sock_drain(conn);
Willy Tarreaua553ae92017-10-05 18:52:17 +02001445 cs_close(cs);
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001446 ret = -1;
Willy Tarreau2d351b62013-12-05 02:36:25 +01001447 }
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001448
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001449 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
Emeric Brunf6ba17d2017-11-02 14:35:27 +01001450
Willy Tarreau6bdcab02017-10-04 18:41:00 +02001451 /* if a connection got replaced, we must absolutely prevent the connection
1452 * handler from touching its fd, and perform the FD polling updates ourselves
1453 */
1454 if (ret < 0)
1455 conn_cond_update_polling(conn);
1456
1457 return ret;
Willy Tarreau20bea422012-07-06 12:00:49 +02001458}
1459
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001460struct data_cb check_conn_cb = {
1461 .recv = event_srv_chk_r,
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001462 .wake = wake_srv_chk,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +01001463 .name = "CHCK",
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001464};
1465
Willy Tarreaubaaee002006-06-26 02:48:02 +02001466/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001467 * updates the server's weight during a warmup stage. Once the final weight is
1468 * reached, the task automatically stops. Note that any server status change
1469 * must have updated s->last_change accordingly.
1470 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001471static struct task *server_warmup(struct task *t, void *context, unsigned short state)
Willy Tarreau2e993902011-10-31 11:53:20 +01001472{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001473 struct server *s = context;
Willy Tarreau2e993902011-10-31 11:53:20 +01001474
1475 /* by default, plan on stopping the task */
1476 t->expire = TICK_ETERNITY;
Emeric Brun52a91d32017-08-31 14:41:55 +02001477 if ((s->next_admin & SRV_ADMF_MAINT) ||
1478 (s->next_state != SRV_ST_STARTING))
Willy Tarreau2e993902011-10-31 11:53:20 +01001479 return t;
1480
Willy Tarreau892337c2014-05-13 23:41:20 +02001481 /* recalculate the weights and update the state */
Willy Tarreau004e0452013-11-21 11:22:01 +01001482 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001483
1484 /* probably that we can refill this server with a bit more connections */
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001485 pendconn_grab_from_px(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001486
1487 /* get back there in 1 second or 1/20th of the slowstart interval,
1488 * whichever is greater, resulting in small 5% steps.
1489 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001490 if (s->next_state == SRV_ST_STARTING)
Willy Tarreau2e993902011-10-31 11:53:20 +01001491 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1492 return t;
1493}
1494
Willy Tarreau894c6422017-10-04 15:58:52 +02001495/* returns the first NON-COMMENT tcp-check rule from list <list> or NULL if
1496 * none was found.
1497 */
1498static struct tcpcheck_rule *get_first_tcpcheck_rule(struct list *list)
1499{
1500 struct tcpcheck_rule *r;
1501
1502 list_for_each_entry(r, list, list) {
1503 if (r->action != TCPCHK_ACT_COMMENT)
1504 return r;
1505 }
1506 return NULL;
1507}
1508
Willy Tarreau2e993902011-10-31 11:53:20 +01001509/*
Simon Horman98637e52014-06-20 12:30:16 +09001510 * establish a server health-check that makes use of a connection.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001511 *
1512 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001513 * - SF_ERR_NONE if everything's OK and tcpcheck_main() was not called
1514 * - SF_ERR_UP if if everything's OK and tcpcheck_main() was called
1515 * - SF_ERR_SRVTO if there are no more servers
1516 * - SF_ERR_SRVCL if the connection was refused by the server
1517 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1518 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1519 * - SF_ERR_INTERNAL for any other purely internal errors
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001520 * - 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 +01001521 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001522 * Note that we try to prevent the network stack from sending the ACK during the
1523 * connect() when a pure TCP check is used (without PROXY protocol).
1524 */
Simon Horman98637e52014-06-20 12:30:16 +09001525static int connect_conn_chk(struct task *t)
Simon Hormanb00d17a2014-06-13 16:18:16 +09001526{
1527 struct check *check = t->context;
1528 struct server *s = check->server;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001529 struct conn_stream *cs = check->cs;
1530 struct connection *conn = cs_conn(cs);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001531 struct protocol *proto;
Willy Tarreauf411cce2017-10-04 16:21:19 +02001532 struct tcpcheck_rule *tcp_rule = NULL;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001533 int ret;
Willy Tarreauf3d34822014-12-08 12:11:28 +01001534 int quickack;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001535
Willy Tarreau00149122017-10-04 18:05:01 +02001536 /* we cannot have a connection here */
1537 if (conn)
1538 return SF_ERR_INTERNAL;
1539
Simon Hormanb00d17a2014-06-13 16:18:16 +09001540 /* tcpcheck send/expect initialisation */
Willy Tarreauf411cce2017-10-04 16:21:19 +02001541 if (check->type == PR_O2_TCPCHK_CHK) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001542 check->current_step = NULL;
Willy Tarreauf411cce2017-10-04 16:21:19 +02001543 tcp_rule = get_first_tcpcheck_rule(check->tcpcheck_rules);
1544 }
Simon Hormanb00d17a2014-06-13 16:18:16 +09001545
1546 /* prepare the check buffer.
1547 * This should not be used if check is the secondary agent check
1548 * of a server as s->proxy->check_req will relate to the
1549 * configuration of the primary check. Similarly, tcp-check uses
1550 * its own strings.
1551 */
1552 if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001553 b_putblk(&check->bo, s->proxy->check_req, s->proxy->check_len);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001554
1555 /* we want to check if this host replies to HTTP or SSLv3 requests
1556 * so we'll send the request, and won't wake the checker up now.
1557 */
1558 if ((check->type) == PR_O2_SSL3_CHK) {
1559 /* SSL requires that we put Unix time in the request */
1560 int gmt_time = htonl(date.tv_sec);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001561 memcpy(b_head(&check->bo) + 11, &gmt_time, 4);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001562 }
1563 else if ((check->type) == PR_O2_HTTP_CHK) {
1564 if (s->proxy->options2 & PR_O2_CHK_SNDST)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001565 b_putblk(&check->bo, trash.area,
1566 httpchk_build_status_header(s, trash.area, trash.size));
Cyril Bonté32602d22015-01-30 00:07:07 +01001567 /* prevent HTTP keep-alive when "http-check expect" is used */
1568 if (s->proxy->options2 & PR_O2_EXP_TYPE)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001569 b_putist(&check->bo, ist("Connection: close\r\n"));
1570 b_putist(&check->bo, ist("\r\n"));
1571 *b_tail(&check->bo) = '\0'; /* to make gdb output easier to read */
Simon Hormanb00d17a2014-06-13 16:18:16 +09001572 }
1573 }
1574
James Brown55f9ff12015-10-21 18:19:05 -07001575 if ((check->type & PR_O2_LB_AGENT_CHK) && check->send_string_len) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001576 b_putblk(&check->bo, check->send_string, check->send_string_len);
James Brown55f9ff12015-10-21 18:19:05 -07001577 }
1578
Willy Tarreauf411cce2017-10-04 16:21:19 +02001579 /* for tcp-checks, the initial connection setup is handled separately as
1580 * it may be sent to a specific port and not to the server's.
1581 */
1582 if (tcp_rule && tcp_rule->action == TCPCHK_ACT_CONNECT) {
1583 tcpcheck_main(check);
1584 return SF_ERR_UP;
1585 }
1586
Simon Hormanb00d17a2014-06-13 16:18:16 +09001587 /* prepare a new connection */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001588 cs = check->cs = cs_new(NULL);
1589 if (!check->cs)
Willy Tarreau00149122017-10-04 18:05:01 +02001590 return SF_ERR_RESOURCE;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001591 conn = cs->conn;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001592
Simon Horman41f58762015-01-30 11:22:56 +09001593 if (is_addr(&check->addr)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001594 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09001595 conn->addr.to = check->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001596 }
1597 else {
1598 /* we'll connect to the addr on the server */
1599 conn->addr.to = s->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001600 }
1601
Olivier Houchard6377a002017-12-01 22:04:05 +01001602 proto = protocol_by_family(conn->addr.to.ss_family);
1603
1604 conn_prepare(conn, proto, check->xprt);
1605 conn_install_mux(conn, &mux_pt_ops, cs);
1606 cs_attach(cs, check, &check_conn_cb);
1607 conn->target = &s->obj_type;
1608
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001609 if ((conn->addr.to.ss_family == AF_INET) || (conn->addr.to.ss_family == AF_INET6)) {
1610 int i = 0;
1611
1612 i = srv_check_healthcheck_port(check);
Olivier Houchard6377a002017-12-01 22:04:05 +01001613 if (i == 0)
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001614 return SF_ERR_CHK_PORT;
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02001615
1616 set_host_port(&conn->addr.to, i);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001617 }
1618
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01001619 /* no client address */
1620 clear_addr(&conn->addr.from);
1621
Willy Tarreauf3d34822014-12-08 12:11:28 +01001622 /* only plain tcp-check supports quick ACK */
1623 quickack = check->type == 0 || check->type == PR_O2_TCPCHK_CHK;
1624
Willy Tarreauf411cce2017-10-04 16:21:19 +02001625 if (tcp_rule && tcp_rule->action == TCPCHK_ACT_EXPECT)
1626 quickack = 0;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001627
Willy Tarreaue7dff022015-04-03 01:14:29 +02001628 ret = SF_ERR_INTERNAL;
Olivier Houchardb68fda42017-08-04 18:39:01 +02001629 if (proto && proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01001630 ret = proto->connect(conn, check->type, quickack ? 2 : 0);
Willy Tarreau16257f62017-11-02 15:45:00 +01001631
1632 if (check->type)
1633 cs_want_send(cs);
1634
Olivier Houchard9130a962017-10-17 17:33:43 +02001635#ifdef USE_OPENSSL
1636 if (s->check.sni)
1637 ssl_sock_set_servername(conn, s->check.sni);
1638#endif
Willy Tarreauf4949772017-05-06 08:45:28 +02001639 if (s->check.send_proxy && !(check->state & CHK_ST_AGENT)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001640 conn->send_proxy_ofs = 1;
1641 conn->flags |= CO_FL_SEND_PROXY;
1642 }
1643
1644 return ret;
1645}
1646
Simon Horman98637e52014-06-20 12:30:16 +09001647static struct list pid_list = LIST_HEAD_INIT(pid_list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001648static struct pool_head *pool_head_pid_list;
Christopher Faulet9dcf9b62017-11-13 10:34:01 +01001649__decl_hathreads(HA_SPINLOCK_T pid_list_lock);
Simon Horman98637e52014-06-20 12:30:16 +09001650
1651void block_sigchld(void)
1652{
1653 sigset_t set;
1654 sigemptyset(&set);
1655 sigaddset(&set, SIGCHLD);
William Lallemand6e1796e2018-06-07 11:23:40 +02001656 assert(ha_sigmask(SIG_BLOCK, &set, NULL) == 0);
Simon Horman98637e52014-06-20 12:30:16 +09001657}
1658
1659void unblock_sigchld(void)
1660{
1661 sigset_t set;
1662 sigemptyset(&set);
Willy Tarreauebc92442016-06-21 17:29:46 +02001663 sigaddset(&set, SIGCHLD);
William Lallemand6e1796e2018-06-07 11:23:40 +02001664 assert(ha_sigmask(SIG_UNBLOCK, &set, NULL) == 0);
Simon Horman98637e52014-06-20 12:30:16 +09001665}
1666
Simon Horman98637e52014-06-20 12:30:16 +09001667static struct pid_list *pid_list_add(pid_t pid, struct task *t)
1668{
1669 struct pid_list *elem;
1670 struct check *check = t->context;
1671
Willy Tarreaubafbe012017-11-24 17:34:44 +01001672 elem = pool_alloc(pool_head_pid_list);
Simon Horman98637e52014-06-20 12:30:16 +09001673 if (!elem)
1674 return NULL;
1675 elem->pid = pid;
1676 elem->t = t;
1677 elem->exited = 0;
1678 check->curpid = elem;
1679 LIST_INIT(&elem->list);
Christopher Fauletcfda8472017-10-20 15:40:23 +02001680
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001681 HA_SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock);
Simon Horman98637e52014-06-20 12:30:16 +09001682 LIST_ADD(&pid_list, &elem->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001683 HA_SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock);
Christopher Fauletcfda8472017-10-20 15:40:23 +02001684
Simon Horman98637e52014-06-20 12:30:16 +09001685 return elem;
1686}
1687
Simon Horman98637e52014-06-20 12:30:16 +09001688static void pid_list_del(struct pid_list *elem)
1689{
1690 struct check *check;
1691
1692 if (!elem)
1693 return;
1694
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001695 HA_SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock);
Simon Horman98637e52014-06-20 12:30:16 +09001696 LIST_DEL(&elem->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001697 HA_SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock);
Christopher Fauletcfda8472017-10-20 15:40:23 +02001698
Simon Horman98637e52014-06-20 12:30:16 +09001699 if (!elem->exited)
1700 kill(elem->pid, SIGTERM);
1701
1702 check = elem->t->context;
1703 check->curpid = NULL;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001704 pool_free(pool_head_pid_list, elem);
Simon Horman98637e52014-06-20 12:30:16 +09001705}
1706
1707/* Called from inside SIGCHLD handler, SIGCHLD is blocked */
1708static void pid_list_expire(pid_t pid, int status)
1709{
1710 struct pid_list *elem;
1711
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001712 HA_SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock);
Simon Horman98637e52014-06-20 12:30:16 +09001713 list_for_each_entry(elem, &pid_list, list) {
1714 if (elem->pid == pid) {
1715 elem->t->expire = now_ms;
1716 elem->status = status;
1717 elem->exited = 1;
Cyril Bonté9dbcfab2014-08-07 01:55:39 +02001718 task_wakeup(elem->t, TASK_WOKEN_IO);
Christopher Fauletcfda8472017-10-20 15:40:23 +02001719 break;
Simon Horman98637e52014-06-20 12:30:16 +09001720 }
1721 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001722 HA_SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock);
Simon Horman98637e52014-06-20 12:30:16 +09001723}
1724
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001725static void sigchld_handler(struct sig_handler *sh)
Simon Horman98637e52014-06-20 12:30:16 +09001726{
1727 pid_t pid;
1728 int status;
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001729
Simon Horman98637e52014-06-20 12:30:16 +09001730 while ((pid = waitpid(0, &status, WNOHANG)) > 0)
1731 pid_list_expire(pid, status);
1732}
1733
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001734static int init_pid_list(void)
1735{
Willy Tarreaubafbe012017-11-24 17:34:44 +01001736 if (pool_head_pid_list != NULL)
Simon Horman98637e52014-06-20 12:30:16 +09001737 /* Nothing to do */
1738 return 0;
1739
Willy Tarreau48d6bf22016-06-21 16:27:34 +02001740 if (!signal_register_fct(SIGCHLD, sigchld_handler, SIGCHLD)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001741 ha_alert("Failed to set signal handler for external health checks: %s. Aborting.\n",
1742 strerror(errno));
Simon Horman98637e52014-06-20 12:30:16 +09001743 return 1;
1744 }
1745
Willy Tarreaubafbe012017-11-24 17:34:44 +01001746 pool_head_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED);
1747 if (pool_head_pid_list == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001748 ha_alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n",
1749 strerror(errno));
Simon Horman98637e52014-06-20 12:30:16 +09001750 return 1;
1751 }
1752
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001753 HA_SPIN_INIT(&pid_list_lock);
Christopher Fauletcfda8472017-10-20 15:40:23 +02001754
Simon Horman98637e52014-06-20 12:30:16 +09001755 return 0;
1756}
1757
Cyril Bontéac92a062014-12-27 22:28:38 +01001758/* helper macro to set an environment variable and jump to a specific label on failure. */
1759#define EXTCHK_SETENV(check, envidx, value, fail) { if (extchk_setenv(check, envidx, value)) goto fail; }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001760
1761/*
Cyril Bontéac92a062014-12-27 22:28:38 +01001762 * helper function to allocate enough memory to store an environment variable.
1763 * It will also check that the environment variable is updatable, and silently
1764 * fail if not.
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001765 */
Cyril Bontéac92a062014-12-27 22:28:38 +01001766static int extchk_setenv(struct check *check, int idx, const char *value)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001767{
1768 int len, ret;
Cyril Bontéac92a062014-12-27 22:28:38 +01001769 char *envname;
1770 int vmaxlen;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001771
Cyril Bontéac92a062014-12-27 22:28:38 +01001772 if (idx < 0 || idx >= EXTCHK_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001773 ha_alert("Illegal environment variable index %d. Aborting.\n", idx);
Cyril Bontéac92a062014-12-27 22:28:38 +01001774 return 1;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001775 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001776
1777 envname = extcheck_envs[idx].name;
1778 vmaxlen = extcheck_envs[idx].vmaxlen;
1779
1780 /* Check if the environment variable is already set, and silently reject
1781 * the update if this one is not updatable. */
1782 if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx]))
1783 return 0;
1784
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001785 /* Instead of sending NOT_USED, sending an empty value is preferable */
1786 if (strcmp(value, "NOT_USED") == 0) {
1787 value = "";
1788 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001789
1790 len = strlen(envname) + 1;
1791 if (vmaxlen == EXTCHK_SIZE_EVAL_INIT)
1792 len += strlen(value);
1793 else
1794 len += vmaxlen;
1795
1796 if (!check->envp[idx])
1797 check->envp[idx] = malloc(len + 1);
1798
1799 if (!check->envp[idx]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001800 ha_alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001801 return 1;
1802 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001803 ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001804 if (ret < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001805 ha_alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno));
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001806 return 1;
1807 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001808 else if (ret > len) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001809 ha_alert("Environment variable '%s' was truncated. Aborting.\n", envname);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001810 return 1;
1811 }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001812 return 0;
1813}
Simon Horman98637e52014-06-20 12:30:16 +09001814
1815static int prepare_external_check(struct check *check)
1816{
1817 struct server *s = check->server;
1818 struct proxy *px = s->proxy;
1819 struct listener *listener = NULL, *l;
1820 int i;
Simon Horman98637e52014-06-20 12:30:16 +09001821 const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001822 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001823
1824 list_for_each_entry(l, &px->conf.listeners, by_fe)
1825 /* Use the first INET, INET6 or UNIX listener */
1826 if (l->addr.ss_family == AF_INET ||
1827 l->addr.ss_family == AF_INET6 ||
1828 l->addr.ss_family == AF_UNIX) {
1829 listener = l;
1830 break;
1831 }
1832
Simon Horman98637e52014-06-20 12:30:16 +09001833 check->curpid = NULL;
Cyril Bontéac92a062014-12-27 22:28:38 +01001834 check->envp = calloc((EXTCHK_SIZE + 1), sizeof(char *));
1835 if (!check->envp) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001836 ha_alert("Failed to allocate memory for environment variables. Aborting\n");
Cyril Bontéac92a062014-12-27 22:28:38 +01001837 goto err;
1838 }
Simon Horman98637e52014-06-20 12:30:16 +09001839
Cyril Bontéac92a062014-12-27 22:28:38 +01001840 check->argv = calloc(6, sizeof(char *));
1841 if (!check->argv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001842 ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001843 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001844 }
Simon Horman98637e52014-06-20 12:30:16 +09001845
1846 check->argv[0] = px->check_command;
1847
Cyril Bonté777be862014-12-02 21:21:35 +01001848 if (!listener) {
1849 check->argv[1] = strdup("NOT_USED");
1850 check->argv[2] = strdup("NOT_USED");
1851 }
1852 else if (listener->addr.ss_family == AF_INET ||
Simon Horman98637e52014-06-20 12:30:16 +09001853 listener->addr.ss_family == AF_INET6) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001854 addr_to_str(&listener->addr, buf, sizeof(buf));
1855 check->argv[1] = strdup(buf);
1856 port_to_str(&listener->addr, buf, sizeof(buf));
1857 check->argv[2] = strdup(buf);
Cyril Bonté777be862014-12-02 21:21:35 +01001858 }
1859 else if (listener->addr.ss_family == AF_UNIX) {
Simon Horman98637e52014-06-20 12:30:16 +09001860 const struct sockaddr_un *un;
1861
1862 un = (struct sockaddr_un *)&listener->addr;
1863 check->argv[1] = strdup(un->sun_path);
1864 check->argv[2] = strdup("NOT_USED");
Cyril Bonté777be862014-12-02 21:21:35 +01001865 }
1866 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001867 ha_alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001868 goto err;
1869 }
1870
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001871 addr_to_str(&s->addr, buf, sizeof(buf));
1872 check->argv[3] = strdup(buf);
Willy Tarreau04276f32017-01-06 17:41:29 +01001873
1874 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
1875 snprintf(buf, sizeof(buf), "%u", s->svc_port);
1876 else
1877 *buf = 0;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001878 check->argv[4] = strdup(buf);
Simon Horman98637e52014-06-20 12:30:16 +09001879
Cyril Bontéac92a062014-12-27 22:28:38 +01001880 for (i = 0; i < 5; i++) {
1881 if (!check->argv[i]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001882 ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001883 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001884 }
1885 }
Simon Horman98637e52014-06-20 12:30:16 +09001886
Cyril Bontéac92a062014-12-27 22:28:38 +01001887 EXTCHK_SETENV(check, EXTCHK_PATH, path, err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001888 /* Add proxy environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001889 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err);
1890 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err);
1891 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err);
1892 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001893 /* Add server environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001894 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err);
1895 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err);
1896 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err);
1897 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err);
1898 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err);
1899 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), err);
1900
1901 /* Ensure that we don't leave any hole in check->envp */
1902 for (i = 0; i < EXTCHK_SIZE; i++)
1903 if (!check->envp[i])
1904 EXTCHK_SETENV(check, i, "", err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001905
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001906 return 1;
Simon Horman98637e52014-06-20 12:30:16 +09001907err:
1908 if (check->envp) {
Cyril Bontéac92a062014-12-27 22:28:38 +01001909 for (i = 0; i < EXTCHK_SIZE; i++)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001910 free(check->envp[i]);
Simon Horman98637e52014-06-20 12:30:16 +09001911 free(check->envp);
1912 check->envp = NULL;
1913 }
1914
1915 if (check->argv) {
1916 for (i = 1; i < 5; i++)
1917 free(check->argv[i]);
1918 free(check->argv);
1919 check->argv = NULL;
1920 }
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001921 return 0;
Simon Horman98637e52014-06-20 12:30:16 +09001922}
1923
Simon Hormanb00d17a2014-06-13 16:18:16 +09001924/*
Simon Horman98637e52014-06-20 12:30:16 +09001925 * establish a server health-check that makes use of a process.
1926 *
1927 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001928 * - SF_ERR_NONE if everything's OK
Willy Tarreaue7dff022015-04-03 01:14:29 +02001929 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
Tim Düsterhus4896c442016-11-29 02:15:19 +01001930 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Horman98637e52014-06-20 12:30:16 +09001931 *
1932 * Blocks and then unblocks SIGCHLD
1933 */
1934static int connect_proc_chk(struct task *t)
1935{
Cyril Bontéac92a062014-12-27 22:28:38 +01001936 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001937 struct check *check = t->context;
1938 struct server *s = check->server;
1939 struct proxy *px = s->proxy;
1940 int status;
1941 pid_t pid;
1942
Willy Tarreaue7dff022015-04-03 01:14:29 +02001943 status = SF_ERR_RESOURCE;
Simon Horman98637e52014-06-20 12:30:16 +09001944
1945 block_sigchld();
1946
1947 pid = fork();
1948 if (pid < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001949 ha_alert("Failed to fork process for external health check: %s. Aborting.\n",
1950 strerror(errno));
Simon Horman98637e52014-06-20 12:30:16 +09001951 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1952 goto out;
1953 }
1954 if (pid == 0) {
1955 /* Child */
1956 extern char **environ;
Willy Tarreaub7b24782016-06-21 15:32:29 +02001957 int fd;
1958
1959 /* close all FDs. Keep stdin/stdout/stderr in verbose mode */
1960 fd = (global.mode & (MODE_QUIET|MODE_VERBOSE)) == MODE_QUIET ? 0 : 3;
1961
1962 while (fd < global.rlimit_nofile)
1963 close(fd++);
1964
Simon Horman98637e52014-06-20 12:30:16 +09001965 environ = check->envp;
Cyril Bontéac92a062014-12-27 22:28:38 +01001966 extchk_setenv(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)));
Simon Horman98637e52014-06-20 12:30:16 +09001967 execvp(px->check_command, check->argv);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001968 ha_alert("Failed to exec process for external health check: %s. Aborting.\n",
1969 strerror(errno));
Simon Horman98637e52014-06-20 12:30:16 +09001970 exit(-1);
1971 }
1972
1973 /* Parent */
1974 if (check->result == CHK_RES_UNKNOWN) {
1975 if (pid_list_add(pid, t) != NULL) {
1976 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1977
1978 if (px->timeout.check && px->timeout.connect) {
1979 int t_con = tick_add(now_ms, px->timeout.connect);
1980 t->expire = tick_first(t->expire, t_con);
1981 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02001982 status = SF_ERR_NONE;
Simon Horman98637e52014-06-20 12:30:16 +09001983 goto out;
1984 }
1985 else {
1986 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1987 }
1988 kill(pid, SIGTERM); /* process creation error */
1989 }
1990 else
1991 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1992
1993out:
1994 unblock_sigchld();
1995 return status;
1996}
1997
1998/*
Willy Tarreau1e62e2a2017-10-04 15:07:02 +02001999 * manages a server health-check that uses an external process. Returns
Willy Tarreaubaaee002006-06-26 02:48:02 +02002000 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
Willy Tarreau62ac84f2017-11-05 10:11:13 +01002001 *
2002 * Please do NOT place any return statement in this function and only leave
2003 * via the out_unlock label.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002004 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002005static struct task *process_chk_proc(struct task *t, void *context, unsigned short state)
Simon Horman98637e52014-06-20 12:30:16 +09002006{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002007 struct check *check = context;
Simon Horman98637e52014-06-20 12:30:16 +09002008 struct server *s = check->server;
Simon Horman98637e52014-06-20 12:30:16 +09002009 int rv;
2010 int ret;
2011 int expired = tick_is_expired(t->expire, now_ms);
2012
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002013 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
Simon Horman98637e52014-06-20 12:30:16 +09002014 if (!(check->state & CHK_ST_INPROGRESS)) {
2015 /* no check currently running */
Willy Tarreau62ac84f2017-11-05 10:11:13 +01002016 if (!expired) /* woke up too early */
2017 goto out_unlock;
Simon Horman98637e52014-06-20 12:30:16 +09002018
2019 /* we don't send any health-checks when the proxy is
2020 * stopped, the server should not be checked or the check
2021 * is disabled.
2022 */
2023 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
2024 s->proxy->state == PR_STSTOPPED)
2025 goto reschedule;
2026
2027 /* we'll initiate a new check */
2028 set_server_check_status(check, HCHK_STATUS_START, NULL);
2029
2030 check->state |= CHK_ST_INPROGRESS;
2031
Simon Hormandbf70192015-01-30 11:22:53 +09002032 ret = connect_proc_chk(t);
Willy Tarreaud7c3fbd2017-10-04 15:19:26 +02002033 if (ret == SF_ERR_NONE) {
Willy Tarreau1e62e2a2017-10-04 15:07:02 +02002034 /* the process was forked, we allow up to min(inter,
2035 * timeout.connect) for it to report its status, but
2036 * only when timeout.check is set as it may be to short
2037 * for a full check otherwise.
Simon Horman98637e52014-06-20 12:30:16 +09002038 */
2039 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2040
2041 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2042 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2043 t->expire = tick_first(t->expire, t_con);
2044 }
Christopher Faulet88ce5d12017-10-20 15:41:18 +02002045 task_set_affinity(t, tid_bit);
Simon Horman98637e52014-06-20 12:30:16 +09002046 goto reschedule;
Simon Horman98637e52014-06-20 12:30:16 +09002047 }
2048
Willy Tarreau1e62e2a2017-10-04 15:07:02 +02002049 /* here, we failed to start the check */
Simon Horman98637e52014-06-20 12:30:16 +09002050
2051 check->state &= ~CHK_ST_INPROGRESS;
2052 check_notify_failure(check);
2053
2054 /* we allow up to min(inter, timeout.connect) for a connection
2055 * to establish but only when timeout.check is set
2056 * as it may be to short for a full check otherwise
2057 */
2058 while (tick_is_expired(t->expire, now_ms)) {
2059 int t_con;
2060
2061 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2062 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2063
2064 if (s->proxy->timeout.check)
2065 t->expire = tick_first(t->expire, t_con);
2066 }
2067 }
2068 else {
2069 /* there was a test running.
2070 * First, let's check whether there was an uncaught error,
2071 * which can happen on connect timeout or error.
2072 */
2073 if (check->result == CHK_RES_UNKNOWN) {
2074 /* good connection is enough for pure TCP check */
2075 struct pid_list *elem = check->curpid;
2076 int status = HCHK_STATUS_UNKNOWN;
2077
2078 if (elem->exited) {
2079 status = elem->status; /* Save in case the process exits between use below */
2080 if (!WIFEXITED(status))
2081 check->code = -1;
2082 else
2083 check->code = WEXITSTATUS(status);
2084 if (!WIFEXITED(status) || WEXITSTATUS(status))
2085 status = HCHK_STATUS_PROCERR;
2086 else
2087 status = HCHK_STATUS_PROCOK;
2088 } else if (expired) {
2089 status = HCHK_STATUS_PROCTOUT;
Christopher Faulet767a84b2017-11-24 16:50:31 +01002090 ha_warning("kill %d\n", (int)elem->pid);
Simon Horman98637e52014-06-20 12:30:16 +09002091 kill(elem->pid, SIGTERM);
2092 }
2093 set_server_check_status(check, status, NULL);
2094 }
2095
2096 if (check->result == CHK_RES_FAILED) {
2097 /* a failure or timeout detected */
2098 check_notify_failure(check);
2099 }
2100 else if (check->result == CHK_RES_CONDPASS) {
2101 /* check is OK but asks for stopping mode */
2102 check_notify_stopping(check);
2103 }
2104 else if (check->result == CHK_RES_PASSED) {
2105 /* a success was detected */
2106 check_notify_success(check);
2107 }
Christopher Faulet88ce5d12017-10-20 15:41:18 +02002108 task_set_affinity(t, MAX_THREADS_MASK);
Simon Horman98637e52014-06-20 12:30:16 +09002109 check->state &= ~CHK_ST_INPROGRESS;
2110
2111 pid_list_del(check->curpid);
2112
2113 rv = 0;
2114 if (global.spread_checks > 0) {
2115 rv = srv_getinter(check) * global.spread_checks / 100;
2116 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
2117 }
2118 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
2119 }
2120
2121 reschedule:
2122 while (tick_is_expired(t->expire, now_ms))
2123 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau62ac84f2017-11-05 10:11:13 +01002124
2125 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002126 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
Simon Horman98637e52014-06-20 12:30:16 +09002127 return t;
2128}
2129
2130/*
2131 * manages a server health-check that uses a connection. Returns
2132 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
Willy Tarreau62ac84f2017-11-05 10:11:13 +01002133 *
2134 * Please do NOT place any return statement in this function and only leave
2135 * via the out_unlock label.
Simon Horman98637e52014-06-20 12:30:16 +09002136 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002137static struct task *process_chk_conn(struct task *t, void *context, unsigned short state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002138{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002139 struct check *check = context;
Simon Horman4a741432013-02-23 15:35:38 +09002140 struct server *s = check->server;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002141 struct conn_stream *cs = check->cs;
2142 struct connection *conn = cs_conn(cs);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002143 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002144 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002145 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002146
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002147 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
Willy Tarreau2c115e52013-12-11 19:41:16 +01002148 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01002149 /* no check currently running */
Willy Tarreau62ac84f2017-11-05 10:11:13 +01002150 if (!expired) /* woke up too early */
2151 goto out_unlock;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002152
Simon Horman671b6f02013-11-25 10:46:39 +09002153 /* we don't send any health-checks when the proxy is
2154 * stopped, the server should not be checked or the check
2155 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002156 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01002157 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01002158 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01002159 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002160
2161 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09002162 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002163
Willy Tarreau2c115e52013-12-11 19:41:16 +01002164 check->state |= CHK_ST_INPROGRESS;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002165 b_reset(&check->bi);
2166 b_reset(&check->bo);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002167
Simon Hormandbf70192015-01-30 11:22:53 +09002168 ret = connect_conn_chk(t);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002169 cs = check->cs;
2170 conn = cs_conn(cs);
Willy Tarreau00149122017-10-04 18:05:01 +02002171
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002172 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002173 case SF_ERR_UP:
Willy Tarreau62ac84f2017-11-05 10:11:13 +01002174 goto out_unlock;
2175
Willy Tarreaue7dff022015-04-03 01:14:29 +02002176 case SF_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002177 /* we allow up to min(inter, timeout.connect) for a connection
2178 * to establish but only when timeout.check is set
2179 * as it may be to short for a full check otherwise
2180 */
Simon Horman4a741432013-02-23 15:35:38 +09002181 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002182
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002183 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2184 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2185 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002186 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01002187
2188 if (check->type)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002189 cs_want_recv(cs); /* prepare for reading a possible reply */
Willy Tarreau06559ac2013-12-05 01:53:08 +01002190
Christopher Faulet88ce5d12017-10-20 15:41:18 +02002191 task_set_affinity(t, tid_bit);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002192 goto reschedule;
2193
Willy Tarreaue7dff022015-04-03 01:14:29 +02002194 case SF_ERR_SRVTO: /* ETIMEDOUT */
2195 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau00149122017-10-04 18:05:01 +02002196 if (conn)
2197 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002198 chk_report_conn_err(check, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002199 break;
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02002200 /* should share same code than cases below */
2201 case SF_ERR_CHK_PORT:
2202 check->state |= CHK_ST_PORT_MISS;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002203 case SF_ERR_PRXCOND:
2204 case SF_ERR_RESOURCE:
2205 case SF_ERR_INTERNAL:
Willy Tarreau00149122017-10-04 18:05:01 +02002206 if (conn)
2207 conn->flags |= CO_FL_ERROR;
2208 chk_report_conn_err(check, conn ? 0 : ENOMEM, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002209 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002210 }
2211
Willy Tarreau5a78f362012-11-23 12:47:05 +01002212 /* here, we have seen a synchronous error, no fd was allocated */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002213 if (cs) {
2214 cs_destroy(cs);
2215 cs = check->cs = NULL;
2216 conn = NULL;
Olivier Houchard390485a2017-10-24 19:03:30 +02002217 }
Willy Tarreau6b0a8502012-11-23 08:51:32 +01002218
Willy Tarreau2c115e52013-12-11 19:41:16 +01002219 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02002220 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002221
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002222 /* we allow up to min(inter, timeout.connect) for a connection
2223 * to establish but only when timeout.check is set
2224 * as it may be to short for a full check otherwise
2225 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002226 while (tick_is_expired(t->expire, now_ms)) {
2227 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002228
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002229 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09002230 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002231
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002232 if (s->proxy->timeout.check)
2233 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002234 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002235 }
2236 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02002237 /* there was a test running.
2238 * First, let's check whether there was an uncaught error,
2239 * which can happen on connect timeout or error.
2240 */
Simon Hormanccaabcd2014-06-20 12:29:47 +09002241 if (check->result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002242 /* good connection is enough for pure TCP check */
2243 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09002244 if (check->use_ssl)
2245 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02002246 else
Simon Horman4a741432013-02-23 15:35:38 +09002247 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002248 }
Willy Tarreau4ff3b892017-10-16 15:17:17 +02002249 else if ((conn->flags & CO_FL_ERROR) || cs->flags & CS_FL_ERROR || expired) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002250 chk_report_conn_err(check, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02002251 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002252 else
Willy Tarreau62ac84f2017-11-05 10:11:13 +01002253 goto out_unlock; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02002254 }
2255
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002256 /* check complete or aborted */
Willy Tarreau00149122017-10-04 18:05:01 +02002257 if (conn && conn->xprt) {
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002258 /* The check was aborted and the connection was not yet closed.
2259 * This can happen upon timeout, or when an external event such
2260 * as a failed response coupled with "observe layer7" caused the
2261 * server state to be suddenly changed.
2262 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01002263 conn_sock_drain(conn);
Willy Tarreaua553ae92017-10-05 18:52:17 +02002264 cs_close(cs);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002265 }
2266
Willy Tarreauac59f362017-10-08 11:10:19 +02002267 if (cs) {
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002268 cs_destroy(cs);
2269 cs = check->cs = NULL;
2270 conn = NULL;
Willy Tarreau00149122017-10-04 18:05:01 +02002271 }
2272
Willy Tarreauaf549582014-05-16 17:37:50 +02002273 if (check->result == CHK_RES_FAILED) {
2274 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002275 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002276 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002277 else if (check->result == CHK_RES_CONDPASS) {
2278 /* check is OK but asks for stopping mode */
2279 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002280 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002281 else if (check->result == CHK_RES_PASSED) {
2282 /* a success was detected */
2283 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002284 }
Christopher Faulet88ce5d12017-10-20 15:41:18 +02002285 task_set_affinity(t, MAX_THREADS_MASK);
Willy Tarreau2c115e52013-12-11 19:41:16 +01002286 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002287
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002288 rv = 0;
2289 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002290 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002291 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002292 }
Simon Horman4a741432013-02-23 15:35:38 +09002293 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002294 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002295
2296 reschedule:
2297 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002298 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau62ac84f2017-11-05 10:11:13 +01002299 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002300 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
Willy Tarreau26c25062009-03-08 09:38:41 +01002301 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002302}
2303
Simon Horman98637e52014-06-20 12:30:16 +09002304/*
2305 * manages a server health-check. Returns
2306 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2307 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002308static struct task *process_chk(struct task *t, void *context, unsigned short state)
Simon Horman98637e52014-06-20 12:30:16 +09002309{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002310 struct check *check = context;
Simon Horman98637e52014-06-20 12:30:16 +09002311
2312 if (check->type == PR_O2_EXT_CHK)
Olivier Houchard9f6af332018-05-25 14:04:04 +02002313 return process_chk_proc(t, context, state);
2314 return process_chk_conn(t, context, state);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002315
Simon Horman98637e52014-06-20 12:30:16 +09002316}
2317
Simon Horman5c942422013-11-25 10:46:32 +09002318static int start_check_task(struct check *check, int mininter,
2319 int nbcheck, int srvpos)
2320{
2321 struct task *t;
2322 /* task for the check */
Emeric Brunc60def82017-09-27 14:59:38 +02002323 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002324 ha_alert("Starting [%s:%s] check: out of memory.\n",
2325 check->server->proxy->id, check->server->id);
Simon Horman5c942422013-11-25 10:46:32 +09002326 return 0;
2327 }
2328
2329 check->task = t;
2330 t->process = process_chk;
2331 t->context = check;
2332
Willy Tarreau1746eec2014-04-25 10:46:47 +02002333 if (mininter < srv_getinter(check))
2334 mininter = srv_getinter(check);
2335
2336 if (global.max_spread_checks && mininter > global.max_spread_checks)
2337 mininter = global.max_spread_checks;
2338
Simon Horman5c942422013-11-25 10:46:32 +09002339 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002340 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002341 check->start = now;
2342 task_queue(t);
2343
2344 return 1;
2345}
2346
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002347/*
2348 * Start health-check.
Willy Tarreau865c5142016-12-21 20:04:48 +01002349 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002350 */
Willy Tarreau865c5142016-12-21 20:04:48 +01002351static int start_checks()
2352{
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002353
2354 struct proxy *px;
2355 struct server *s;
2356 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002357 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002358
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002359 /* 1- count the checkers to run simultaneously.
2360 * We also determine the minimum interval among all of those which
2361 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2362 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002363 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002364 * too short an interval for all others.
2365 */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002366 for (px = proxies_list; px; px = px->next) {
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002367 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002368 if (s->slowstart) {
Emeric Brunc60def82017-09-27 14:59:38 +02002369 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002370 ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Willy Tarreau865c5142016-12-21 20:04:48 +01002371 return ERR_ALERT | ERR_FATAL;
Willy Tarreaue7b73482013-11-21 11:50:50 +01002372 }
2373 /* We need a warmup task that will be called when the server
2374 * state switches from down to up.
2375 */
2376 s->warmup = t;
2377 t->process = server_warmup;
2378 t->context = s;
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002379 /* server can be in this state only because of */
Emeric Brun52a91d32017-08-31 14:41:55 +02002380 if (s->next_state == SRV_ST_STARTING)
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002381 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 +01002382 }
2383
Willy Tarreaud8514a22013-12-11 21:10:14 +01002384 if (s->check.state & CHK_ST_CONFIGURED) {
2385 nbcheck++;
2386 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2387 (!mininter || mininter > srv_getinter(&s->check)))
2388 mininter = srv_getinter(&s->check);
2389 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002390
Willy Tarreaud8514a22013-12-11 21:10:14 +01002391 if (s->agent.state & CHK_ST_CONFIGURED) {
2392 nbcheck++;
2393 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2394 (!mininter || mininter > srv_getinter(&s->agent)))
2395 mininter = srv_getinter(&s->agent);
2396 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002397 }
2398 }
2399
Simon Horman4a741432013-02-23 15:35:38 +09002400 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002401 return 0;
2402
2403 srand((unsigned)time(NULL));
2404
2405 /*
2406 * 2- start them as far as possible from each others. For this, we will
2407 * start them after their interval set to the min interval divided by
2408 * the number of servers, weighted by the server's position in the list.
2409 */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002410 for (px = proxies_list; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002411 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2412 if (init_pid_list()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002413 ha_alert("Starting [%s] check: out of memory.\n", px->id);
Willy Tarreau865c5142016-12-21 20:04:48 +01002414 return ERR_ALERT | ERR_FATAL;
Simon Horman98637e52014-06-20 12:30:16 +09002415 }
2416 }
2417
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002418 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002419 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002420 if (s->check.state & CHK_ST_CONFIGURED) {
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002421 if (s->check.type == PR_O2_EXT_CHK) {
2422 if (!prepare_external_check(&s->check))
Willy Tarreau865c5142016-12-21 20:04:48 +01002423 return ERR_ALERT | ERR_FATAL;
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002424 }
Simon Hormand60d6912013-11-25 10:46:36 +09002425 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
Willy Tarreau865c5142016-12-21 20:04:48 +01002426 return ERR_ALERT | ERR_FATAL;
Simon Hormand60d6912013-11-25 10:46:36 +09002427 srvpos++;
2428 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002429
Simon Hormand60d6912013-11-25 10:46:36 +09002430 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002431 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002432 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
Willy Tarreau865c5142016-12-21 20:04:48 +01002433 return ERR_ALERT | ERR_FATAL;
Simon Hormand60d6912013-11-25 10:46:36 +09002434 }
2435 srvpos++;
2436 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002437 }
2438 }
2439 return 0;
2440}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002441
2442/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002443 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002444 * The buffer MUST be terminated by a null byte before calling this function.
2445 * Sets server status appropriately. The caller is responsible for ensuring
2446 * that the buffer contains at least 13 characters. If <done> is zero, we may
2447 * return 0 to indicate that data is required to decide of a match.
2448 */
2449static int httpchk_expect(struct server *s, int done)
2450{
Christopher Faulet1bc04c72017-10-29 20:14:08 +01002451 static THREAD_LOCAL char status_msg[] = "HTTP status check returned code <000>";
Willy Tarreaubd741542010-03-16 18:46:54 +01002452 char status_code[] = "000";
2453 char *contentptr;
2454 int crlf;
2455 int ret;
2456
2457 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2458 case PR_O2_EXP_STS:
2459 case PR_O2_EXP_RSTS:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002460 memcpy(status_code, b_head(&s->check.bi) + 9, 3);
2461 memcpy(status_msg + strlen(status_msg) - 4, b_head(&s->check.bi) + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002462
2463 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2464 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2465 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002466 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002467
2468 /* we necessarily have the response, so there are no partial failures */
2469 if (s->proxy->options2 & PR_O2_EXP_INV)
2470 ret = !ret;
2471
Simon Horman4a741432013-02-23 15:35:38 +09002472 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002473 break;
2474
2475 case PR_O2_EXP_STR:
2476 case PR_O2_EXP_RSTR:
2477 /* very simple response parser: ignore CR and only count consecutive LFs,
2478 * stop with contentptr pointing to first char after the double CRLF or
2479 * to '\0' if crlf < 2.
2480 */
2481 crlf = 0;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002482 for (contentptr = b_head(&s->check.bi); *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002483 if (crlf >= 2)
2484 break;
2485 if (*contentptr == '\r')
2486 continue;
2487 else if (*contentptr == '\n')
2488 crlf++;
2489 else
2490 crlf = 0;
2491 }
2492
2493 /* Check that response contains a body... */
2494 if (crlf < 2) {
2495 if (!done)
2496 return 0;
2497
Simon Horman4a741432013-02-23 15:35:38 +09002498 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002499 "HTTP content check could not find a response body");
2500 return 1;
2501 }
2502
2503 /* Check that response body is not empty... */
2504 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002505 if (!done)
2506 return 0;
2507
Simon Horman4a741432013-02-23 15:35:38 +09002508 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002509 "HTTP content check found empty response body");
2510 return 1;
2511 }
2512
2513 /* Check the response content against the supplied string
2514 * or regex... */
2515 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2516 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2517 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002518 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002519
2520 /* if we don't match, we may need to wait more */
2521 if (!ret && !done)
2522 return 0;
2523
2524 if (ret) {
2525 /* content matched */
2526 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002527 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002528 "HTTP check matched unwanted content");
2529 else
Simon Horman4a741432013-02-23 15:35:38 +09002530 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002531 "HTTP content check matched");
2532 }
2533 else {
2534 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002535 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002536 "HTTP check did not match unwanted content");
2537 else
Simon Horman4a741432013-02-23 15:35:38 +09002538 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002539 "HTTP content check did not match");
2540 }
2541 break;
2542 }
2543 return 1;
2544}
2545
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002546/*
2547 * return the id of a step in a send/expect session
2548 */
Simon Hormane16c1b32015-01-30 11:22:57 +09002549static int tcpcheck_get_step_id(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002550{
2551 struct tcpcheck_rule *cur = NULL, *next = NULL;
2552 int i = 0;
2553
Willy Tarreau213c6782014-10-02 14:51:02 +02002554 /* not even started anything yet => step 0 = initial connect */
Baptiste Assmannf95bc8e2015-04-25 16:03:06 +02002555 if (!check->current_step)
Willy Tarreau213c6782014-10-02 14:51:02 +02002556 return 0;
2557
Simon Hormane16c1b32015-01-30 11:22:57 +09002558 cur = check->last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002559
2560 /* no step => first step */
2561 if (cur == NULL)
2562 return 1;
2563
2564 /* increment i until current step */
Simon Hormane16c1b32015-01-30 11:22:57 +09002565 list_for_each_entry(next, check->tcpcheck_rules, list) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002566 if (next->list.p == &cur->list)
2567 break;
2568 ++i;
2569 }
2570
2571 return i;
2572}
2573
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002574/*
2575 * return the latest known comment before (including) the given stepid
2576 * returns NULL if no comment found
2577 */
2578static char * tcpcheck_get_step_comment(struct check *check, int stepid)
2579{
2580 struct tcpcheck_rule *cur = NULL;
2581 char *ret = NULL;
2582 int i = 0;
2583
2584 /* not even started anything yet, return latest comment found before any action */
2585 if (!check->current_step) {
2586 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2587 if (cur->action == TCPCHK_ACT_COMMENT)
2588 ret = cur->comment;
2589 else
2590 goto return_comment;
2591 }
2592 }
2593
2594 i = 1;
2595 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2596 if (cur->comment)
2597 ret = cur->comment;
2598
2599 if (i >= stepid)
2600 goto return_comment;
2601
2602 ++i;
2603 }
2604
2605 return_comment:
2606 return ret;
2607}
2608
Willy Tarreaube74b882017-10-04 16:22:49 +02002609/* proceed with next steps for the TCP checks <check>. Note that this is called
2610 * both from the connection's wake() callback and from the check scheduling task.
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002611 * It returns 0 on normal cases, or <0 if a close() has happened on an existing
2612 * connection, presenting the risk of an fd replacement.
Willy Tarreau62ac84f2017-11-05 10:11:13 +01002613 *
2614 * Please do NOT place any return statement in this function and only leave
2615 * via the out_unlock label after setting retcode.
Willy Tarreaube74b882017-10-04 16:22:49 +02002616 */
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002617static int tcpcheck_main(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002618{
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002619 char *contentptr, *comment;
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002620 struct tcpcheck_rule *next;
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002621 int done = 0, ret = 0, step = 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002622 struct conn_stream *cs = check->cs;
2623 struct connection *conn = cs_conn(cs);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002624 struct server *s = check->server;
2625 struct task *t = check->task;
Simon Hormane16c1b32015-01-30 11:22:57 +09002626 struct list *head = check->tcpcheck_rules;
Willy Tarreau6bdcab02017-10-04 18:41:00 +02002627 int retcode = 0;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002628
Willy Tarreauef953952014-10-02 14:30:14 +02002629 /* here, we know that the check is complete or that it failed */
2630 if (check->result != CHK_RES_UNKNOWN)
2631 goto out_end_tcpcheck;
2632
2633 /* We have 4 possibilities here :
2634 * 1. we've not yet attempted step 1, and step 1 is a connect, so no
Willy Tarreau00149122017-10-04 18:05:01 +02002635 * connection attempt was made yet ; conn==NULL;current_step==NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002636 * 2. we've not yet attempted step 1, and step 1 is a not connect or
2637 * does not exist (no rule), so a connection attempt was made
Willy Tarreau00149122017-10-04 18:05:01 +02002638 * before coming here, conn!=NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002639 * 3. we're coming back after having started with step 1, so we may
Willy Tarreau00149122017-10-04 18:05:01 +02002640 * be waiting for a connection attempt to complete. conn!=NULL.
2641 * 4. the connection + handshake are complete. conn!=NULL.
Willy Tarreauef953952014-10-02 14:30:14 +02002642 *
2643 * #2 and #3 are quite similar, we want both the connection and the
2644 * handshake to complete before going any further. Thus we must always
2645 * wait for a connection to complete unless we're before and existing
2646 * step 1.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002647 */
Willy Tarreau449f9522015-05-13 15:39:48 +02002648
2649 /* find first rule and skip comments */
2650 next = LIST_NEXT(head, struct tcpcheck_rule *, list);
2651 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
2652 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
2653
Willy Tarreau00149122017-10-04 18:05:01 +02002654 if ((check->current_step || &next->list == head) &&
2655 (!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE))) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002656 /* we allow up to min(inter, timeout.connect) for a connection
2657 * to establish but only when timeout.check is set
2658 * as it may be to short for a full check otherwise
2659 */
2660 while (tick_is_expired(t->expire, now_ms)) {
2661 int t_con;
2662
2663 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2664 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2665
2666 if (s->proxy->timeout.check)
2667 t->expire = tick_first(t->expire, t_con);
2668 }
Christopher Fauletb6102852017-11-28 10:06:29 +01002669 goto out;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002670 }
2671
Willy Tarreauef953952014-10-02 14:30:14 +02002672 /* special case: option tcp-check with no rule, a connect is enough */
Willy Tarreau449f9522015-05-13 15:39:48 +02002673 if (&next->list == head) {
Willy Tarreauef953952014-10-02 14:30:14 +02002674 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002675 goto out_end_tcpcheck;
Willy Tarreauef953952014-10-02 14:30:14 +02002676 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002677
Willy Tarreau213c6782014-10-02 14:51:02 +02002678 /* no step means first step initialisation */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002679 if (check->current_step == NULL) {
Willy Tarreau213c6782014-10-02 14:51:02 +02002680 check->last_started_step = NULL;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002681 b_reset(&check->bo);
2682 b_reset(&check->bi);
Willy Tarreau449f9522015-05-13 15:39:48 +02002683 check->current_step = next;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002684 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2685 if (s->proxy->timeout.check)
2686 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2687 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002688
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002689 /* It's only the rules which will enable send/recv */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002690 if (cs)
2691 cs_stop_both(cs);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002692
Willy Tarreauabca5b62013-12-06 14:19:25 +01002693 while (1) {
Willy Tarreau263013d2015-05-13 11:59:14 +02002694 /* We have to try to flush the output buffer before reading, at
2695 * the end, or if we're about to send a string that does not fit
2696 * in the remaining space. That explains why we break out of the
Willy Tarreau00149122017-10-04 18:05:01 +02002697 * loop after this control. If we have data, conn is valid.
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002698 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002699 if (b_data(&check->bo) &&
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002700 (&check->current_step->list == head ||
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002701 check->current_step->action != TCPCHK_ACT_SEND ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002702 check->current_step->string_len >= b_room(&check->bo))) {
Willy Tarreaudeccd112018-06-14 18:38:55 +02002703 int ret;
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002704
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002705 __cs_want_send(cs);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002706 ret = conn->mux->snd_buf(cs, &check->bo, b_data(&check->bo), 0);
2707 b_del(&check->bo, ret);
2708 b_realign_if_empty(&check->bo);
Willy Tarreaudeccd112018-06-14 18:38:55 +02002709
2710 if (ret <= 0) {
Willy Tarreau4ff3b892017-10-16 15:17:17 +02002711 if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) {
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002712 chk_report_conn_err(check, errno, 0);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002713 __cs_stop_both(cs);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002714 goto out_end_tcpcheck;
2715 }
Willy Tarreau263013d2015-05-13 11:59:14 +02002716 break;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002717 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002718 }
2719
Willy Tarreau263013d2015-05-13 11:59:14 +02002720 if (&check->current_step->list == head)
Willy Tarreauabca5b62013-12-06 14:19:25 +01002721 break;
2722
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002723 /* have 'next' point to the next rule or NULL if we're on the
2724 * last one, connect() needs this.
2725 */
Willy Tarreau5581c272015-05-13 12:24:53 +02002726 next = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002727
2728 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002729 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002730 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002731
2732 /* NULL if we're on the last rule */
Willy Tarreauf3d34822014-12-08 12:11:28 +01002733 if (&next->list == head)
2734 next = NULL;
2735
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002736 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2737 struct protocol *proto;
2738 struct xprt_ops *xprt;
2739
Willy Tarreau00149122017-10-04 18:05:01 +02002740 /* For a connect action we'll create a new connection.
2741 * We may also have to kill a previous one. But we don't
2742 * want to leave *without* a connection if we came here
2743 * from the connection layer, hence with a connection.
2744 * Thus we'll proceed in the following order :
2745 * 1: close but not release previous connection
2746 * 2: try to get a new connection
2747 * 3: release and replace the old one on success
2748 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002749 if (check->cs) {
Willy Tarreaua553ae92017-10-05 18:52:17 +02002750 cs_close(check->cs);
2751 retcode = -1; /* do not reuse the fd in the caller! */
Willy Tarreau00149122017-10-04 18:05:01 +02002752 }
2753
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002754 /* mark the step as started */
2755 check->last_started_step = check->current_step;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002756
2757 /* prepare new connection */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002758 cs = cs_new(NULL);
2759 if (!cs) {
Willy Tarreau00149122017-10-04 18:05:01 +02002760 step = tcpcheck_get_step_id(check);
2761 chunk_printf(&trash, "TCPCHK error allocating connection at step %d", step);
2762 comment = tcpcheck_get_step_comment(check, step);
2763 if (comment)
2764 chunk_appendf(&trash, " comment: '%s'", comment);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002765 set_server_check_status(check, HCHK_STATUS_SOCKERR,
2766 trash.area);
Willy Tarreau00149122017-10-04 18:05:01 +02002767 check->current_step = NULL;
Christopher Fauletb6102852017-11-28 10:06:29 +01002768 goto out;
Willy Tarreau00149122017-10-04 18:05:01 +02002769 }
2770
Willy Tarreauac59f362017-10-08 11:10:19 +02002771 if (check->cs)
2772 cs_destroy(check->cs);
Willy Tarreau00149122017-10-04 18:05:01 +02002773
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002774 check->cs = cs;
2775 conn = cs->conn;
2776 cs_attach(cs, check, &check_conn_cb);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002777 conn->target = &s->obj_type;
2778
2779 /* no client address */
2780 clear_addr(&conn->addr.from);
2781
Simon Horman41f58762015-01-30 11:22:56 +09002782 if (is_addr(&check->addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002783 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09002784 conn->addr.to = check->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002785 }
2786 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002787 /* we'll connect to the addr on the server */
2788 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002789 }
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01002790 proto = protocol_by_family(conn->addr.to.ss_family);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002791
2792 /* port */
2793 if (check->current_step->port)
2794 set_host_port(&conn->addr.to, check->current_step->port);
2795 else if (check->port)
2796 set_host_port(&conn->addr.to, check->port);
Baptiste Assmann2f3a56b2018-03-19 12:22:41 +01002797 else if (s->svc_port)
2798 set_host_port(&conn->addr.to, s->svc_port);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002799
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002800 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002801 xprt = xprt_get(XPRT_SSL);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002802 }
2803 else {
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002804 xprt = xprt_get(XPRT_RAW);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002805 }
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002806 conn_prepare(conn, proto, xprt);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002807 conn_install_mux(conn, &mux_pt_ops, cs);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002808
Willy Tarreaue7dff022015-04-03 01:14:29 +02002809 ret = SF_ERR_INTERNAL;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002810 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01002811 ret = proto->connect(conn,
2812 1 /* I/O polling is always needed */,
2813 (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002814 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2815 conn->send_proxy_ofs = 1;
2816 conn->flags |= CO_FL_SEND_PROXY;
2817 }
2818
2819 /* It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02002820 * - SF_ERR_NONE if everything's OK
2821 * - SF_ERR_SRVTO if there are no more servers
2822 * - SF_ERR_SRVCL if the connection was refused by the server
2823 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2824 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2825 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +01002826 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002827 * Note that we try to prevent the network stack from sending the ACK during the
2828 * connect() when a pure TCP check is used (without PROXY protocol).
2829 */
2830 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002831 case SF_ERR_NONE:
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002832 /* we allow up to min(inter, timeout.connect) for a connection
2833 * to establish but only when timeout.check is set
2834 * as it may be to short for a full check otherwise
2835 */
2836 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2837
2838 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2839 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2840 t->expire = tick_first(t->expire, t_con);
2841 }
2842 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002843 case SF_ERR_SRVTO: /* ETIMEDOUT */
2844 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002845 step = tcpcheck_get_step_id(check);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002846 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002847 step, strerror(errno));
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002848 comment = tcpcheck_get_step_comment(check, step);
2849 if (comment)
2850 chunk_appendf(&trash, " comment: '%s'", comment);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002851 set_server_check_status(check, HCHK_STATUS_L4CON,
2852 trash.area);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002853 goto out_end_tcpcheck;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002854 case SF_ERR_PRXCOND:
2855 case SF_ERR_RESOURCE:
2856 case SF_ERR_INTERNAL:
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002857 step = tcpcheck_get_step_id(check);
2858 chunk_printf(&trash, "TCPCHK error establishing connection at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002859 comment = tcpcheck_get_step_comment(check, step);
2860 if (comment)
2861 chunk_appendf(&trash, " comment: '%s'", comment);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002862 set_server_check_status(check, HCHK_STATUS_SOCKERR,
2863 trash.area);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002864 goto out_end_tcpcheck;
2865 }
2866
2867 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002868 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002869
2870 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002871 while (&check->current_step->list != head &&
2872 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002873 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002874
Willy Tarreauf2c87352015-05-13 12:08:21 +02002875 if (&check->current_step->list == head)
2876 break;
2877
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002878 } /* end 'connect' */
2879 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2880 /* mark the step as started */
2881 check->last_started_step = check->current_step;
2882
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002883 /* reset the read buffer */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002884 if (*b_head(&check->bi) != '\0') {
2885 *b_head(&check->bi) = '\0';
2886 b_reset(&check->bi);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002887 }
2888
Willy Tarreaubbae3f02017-08-30 09:59:52 +02002889 if (conn->flags & CO_FL_SOCK_WR_SH) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002890 conn->flags |= CO_FL_ERROR;
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002891 chk_report_conn_err(check, 0, 0);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002892 goto out_end_tcpcheck;
2893 }
2894
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002895 if (check->current_step->string_len >= b_size(&check->bo)) {
Willy Tarreau506a29a2018-07-18 10:07:58 +02002896 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%u) at step %d",
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002897 check->current_step->string_len, (unsigned int)b_size(&check->bo),
Simon Hormane16c1b32015-01-30 11:22:57 +09002898 tcpcheck_get_step_id(check));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002899 set_server_check_status(check, HCHK_STATUS_L7RSP,
2900 trash.area);
Willy Tarreauabca5b62013-12-06 14:19:25 +01002901 goto out_end_tcpcheck;
2902 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002903
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002904 /* do not try to send if there is no space */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002905 if (check->current_step->string_len >= b_room(&check->bo))
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002906 continue;
2907
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002908 b_putblk(&check->bo, check->current_step->string, check->current_step->string_len);
2909 *b_tail(&check->bo) = '\0'; /* to make gdb output easier to read */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002910
Willy Tarreauabca5b62013-12-06 14:19:25 +01002911 /* go to next rule and try to send */
Willy Tarreau5581c272015-05-13 12:24:53 +02002912 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002913
2914 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002915 while (&check->current_step->list != head &&
2916 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002917 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Willy Tarreauf2c87352015-05-13 12:08:21 +02002918
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002919 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002920 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002921 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002922 goto out_end_tcpcheck;
2923
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002924 __cs_want_recv(cs);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002925 if (conn->mux->rcv_buf(cs, &check->bi, b_size(&check->bi), 0) <= 0) {
Willy Tarreau4ff3b892017-10-16 15:17:17 +02002926 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH) || cs->flags & CS_FL_ERROR) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002927 done = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002928 if ((conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) && !b_data(&check->bi)) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002929 /* Report network errors only if we got no other data. Otherwise
2930 * we'll let the upper layers decide whether the response is OK
2931 * or not. It is very common that an RST sent by the server is
2932 * reported as an error just after the last data chunk.
2933 */
Willy Tarreaub5259bf2017-10-04 14:47:29 +02002934 chk_report_conn_err(check, errno, 0);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002935 goto out_end_tcpcheck;
2936 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002937 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002938 else
Willy Tarreau263013d2015-05-13 11:59:14 +02002939 break;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002940 }
2941
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002942 /* mark the step as started */
2943 check->last_started_step = check->current_step;
2944
2945
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002946 /* Intermediate or complete response received.
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002947 * Terminate string in b_head(&check->bi) buffer.
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002948 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002949 if (b_data(&check->bi) < b_size(&check->bi)) {
2950 b_head(&check->bi)[b_data(&check->bi)] = '\0';
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002951 }
2952 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002953 b_head(&check->bi)[b_data(&check->bi) - 1] = '\0';
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002954 done = 1; /* buffer full, don't wait for more data */
2955 }
2956
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002957 contentptr = b_head(&check->bi);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002958
2959 /* Check that response body is not empty... */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002960 if (!b_data(&check->bi)) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002961 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002962 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002963
2964 /* empty response */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002965 step = tcpcheck_get_step_id(check);
2966 chunk_printf(&trash, "TCPCHK got an empty response at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002967 comment = tcpcheck_get_step_comment(check, step);
2968 if (comment)
2969 chunk_appendf(&trash, " comment: '%s'", comment);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002970 set_server_check_status(check, HCHK_STATUS_L7RSP,
2971 trash.area);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002972
2973 goto out_end_tcpcheck;
2974 }
2975
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002976 if (!done && (check->current_step->string != NULL) && (b_data(&check->bi) < check->current_step->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002977 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002978
Willy Tarreaua970c282013-12-06 12:47:19 +01002979 tcpcheck_expect:
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002980 if (check->current_step->string != NULL)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002981 ret = my_memmem(contentptr, b_data(&check->bi), check->current_step->string, check->current_step->string_len) != NULL;
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002982 else if (check->current_step->expect_regex != NULL)
2983 ret = regex_exec(check->current_step->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002984
2985 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002986 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002987
2988 /* matched */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002989 step = tcpcheck_get_step_id(check);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002990 if (ret) {
2991 /* matched but we did not want to => ERROR */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002992 if (check->current_step->inverse) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002993 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002994 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002995 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002996 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002997 }
2998 else {
2999 /* we were looking for a regex */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02003000 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d", step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003001 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02003002 comment = tcpcheck_get_step_comment(check, step);
3003 if (comment)
3004 chunk_appendf(&trash, " comment: '%s'", comment);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003005 set_server_check_status(check, HCHK_STATUS_L7RSP,
3006 trash.area);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003007 goto out_end_tcpcheck;
3008 }
3009 /* matched and was supposed to => OK, next step */
3010 else {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02003011 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02003012 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02003013
3014 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02003015 while (&check->current_step->list != head &&
3016 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02003017 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02003018
Willy Tarreauf2c87352015-05-13 12:08:21 +02003019 if (&check->current_step->list == head)
3020 break;
3021
Willy Tarreau98aec9f2013-12-06 16:16:41 +01003022 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003023 goto tcpcheck_expect;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003024 __cs_stop_recv(cs);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003025 }
3026 }
3027 else {
3028 /* not matched */
3029 /* not matched and was not supposed to => OK, next step */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02003030 if (check->current_step->inverse) {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02003031 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02003032 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02003033
3034 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02003035 while (&check->current_step->list != head &&
3036 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02003037 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02003038
Willy Tarreauf2c87352015-05-13 12:08:21 +02003039 if (&check->current_step->list == head)
3040 break;
3041
Willy Tarreau98aec9f2013-12-06 16:16:41 +01003042 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003043 goto tcpcheck_expect;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003044 __cs_stop_recv(cs);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003045 }
3046 /* not matched but was supposed to => ERROR */
3047 else {
3048 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02003049 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003050 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02003051 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003052 }
3053 else {
3054 /* we were looking for a regex */
3055 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02003056 step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003057 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02003058 comment = tcpcheck_get_step_comment(check, step);
3059 if (comment)
3060 chunk_appendf(&trash, " comment: '%s'", comment);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003061 set_server_check_status(check, HCHK_STATUS_L7RSP,
3062 trash.area);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003063 goto out_end_tcpcheck;
3064 }
3065 }
3066 } /* end expect */
3067 } /* end loop over double chained step list */
3068
Baptiste Assmann248f1172018-03-01 21:49:01 +01003069 /* don't do anything until the connection is established */
3070 if (!(conn->flags & CO_FL_CONNECTED)) {
3071 /* update expire time, should be done by process_chk */
3072 /* we allow up to min(inter, timeout.connect) for a connection
3073 * to establish but only when timeout.check is set
3074 * as it may be to short for a full check otherwise
3075 */
3076 while (tick_is_expired(t->expire, now_ms)) {
3077 int t_con;
3078
3079 t_con = tick_add(t->expire, s->proxy->timeout.connect);
3080 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
3081
3082 if (s->proxy->timeout.check)
3083 t->expire = tick_first(t->expire, t_con);
3084 }
3085 goto out;
3086 }
3087
Willy Tarreau263013d2015-05-13 11:59:14 +02003088 /* We're waiting for some I/O to complete, we've reached the end of the
3089 * rules, or both. Do what we have to do, otherwise we're done.
3090 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003091 if (&check->current_step->list == head && !b_data(&check->bo)) {
Willy Tarreau263013d2015-05-13 11:59:14 +02003092 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
3093 goto out_end_tcpcheck;
3094 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003095
Willy Tarreau53c5a042015-05-13 11:38:17 +02003096 /* warning, current_step may now point to the head */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003097 if (b_data(&check->bo))
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003098 __cs_want_send(cs);
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01003099
Willy Tarreau53c5a042015-05-13 11:38:17 +02003100 if (&check->current_step->list != head &&
3101 check->current_step->action == TCPCHK_ACT_EXPECT)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003102 __cs_want_recv(cs);
Christopher Fauletb6102852017-11-28 10:06:29 +01003103 goto out;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003104
3105 out_end_tcpcheck:
3106 /* collect possible new errors */
Willy Tarreau4ff3b892017-10-16 15:17:17 +02003107 if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)
Willy Tarreaub5259bf2017-10-04 14:47:29 +02003108 chk_report_conn_err(check, 0, 0);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003109
Baptiste Assmann69e273f2013-12-11 00:52:19 +01003110 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003111 check->current_step = NULL;
3112
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01003113 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003114 conn->flags |= CO_FL_ERROR;
3115
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003116 __cs_stop_both(cs);
Willy Tarreau62ac84f2017-11-05 10:11:13 +01003117
Christopher Fauletb6102852017-11-28 10:06:29 +01003118 out:
Willy Tarreau6bdcab02017-10-04 18:41:00 +02003119 return retcode;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003120}
3121
Simon Hormanb1900d52015-01-30 11:22:54 +09003122const char *init_check(struct check *check, int type)
3123{
3124 check->type = type;
3125
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003126 b_reset(&check->bi); check->bi.size = global.tune.chksize;
3127 b_reset(&check->bo); check->bo.size = global.tune.chksize;
Simon Hormanb1900d52015-01-30 11:22:54 +09003128
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003129 check->bi.area = calloc(check->bi.size, sizeof(char));
3130 check->bo.area = calloc(check->bo.size, sizeof(char));
3131
3132 if (!check->bi.area || !check->bo.area)
Simon Hormanb1900d52015-01-30 11:22:54 +09003133 return "out of memory while allocating check buffer";
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003134
Simon Hormanb1900d52015-01-30 11:22:54 +09003135 return NULL;
3136}
3137
Simon Hormanbfb5d332015-01-30 11:22:55 +09003138void free_check(struct check *check)
3139{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003140 free(check->bi.area);
3141 free(check->bo.area);
Christopher Faulet23d86d12018-01-25 11:36:35 +01003142 if (check->cs) {
3143 free(check->cs->conn);
3144 check->cs->conn = NULL;
3145 cs_free(check->cs);
3146 check->cs = NULL;
3147 }
Simon Hormanbfb5d332015-01-30 11:22:55 +09003148}
3149
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003150void email_alert_free(struct email_alert *alert)
3151{
3152 struct tcpcheck_rule *rule, *back;
3153
3154 if (!alert)
3155 return;
3156
Christopher Fauletde1a75b2017-10-23 15:38:19 +02003157 list_for_each_entry_safe(rule, back, &alert->tcpcheck_rules, list) {
3158 LIST_DEL(&rule->list);
3159 free(rule->comment);
3160 free(rule->string);
3161 if (rule->expect_regex)
3162 regex_free(rule->expect_regex);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003163 pool_free(pool_head_tcpcheck_rule, rule);
Christopher Fauletde1a75b2017-10-23 15:38:19 +02003164 }
Willy Tarreaubafbe012017-11-24 17:34:44 +01003165 pool_free(pool_head_email_alert, alert);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003166}
3167
Olivier Houchard9f6af332018-05-25 14:04:04 +02003168static struct task *process_email_alert(struct task *t, void *context, unsigned short state)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003169{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003170 struct check *check = context;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003171 struct email_alertq *q;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003172 struct email_alert *alert;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003173
3174 q = container_of(check, typeof(*q), check);
3175
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003176 HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003177 while (1) {
3178 if (!(check->state & CHK_ST_ENABLED)) {
3179 if (LIST_ISEMPTY(&q->email_alerts)) {
3180 /* All alerts processed, queue the task */
3181 t->expire = TICK_ETERNITY;
3182 task_queue(t);
Christopher Fauletc2a89a62017-10-23 15:54:24 +02003183 goto end;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003184 }
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003185
3186 alert = LIST_NEXT(&q->email_alerts, typeof(alert), list);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003187 LIST_DEL(&alert->list);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003188 t->expire = now_ms;
3189 check->server = alert->srv;
3190 check->tcpcheck_rules = &alert->tcpcheck_rules;
PiBa-NL1714b9f2017-12-06 01:35:43 +01003191 check->status = HCHK_STATUS_UNKNOWN; // the UNKNOWN status is used to exit set_server_check_status(.) early
Christopher Faulet0108bb32017-10-20 21:34:32 +02003192 check->state |= CHK_ST_ENABLED;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003193 }
3194
Olivier Houchard9f6af332018-05-25 14:04:04 +02003195 process_chk(t, context, state);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003196 if (check->state & CHK_ST_INPROGRESS)
3197 break;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003198
3199 alert = container_of(check->tcpcheck_rules, typeof(*alert), tcpcheck_rules);
3200 email_alert_free(alert);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003201 check->tcpcheck_rules = NULL;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003202 check->server = NULL;
3203 check->state &= ~CHK_ST_ENABLED;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003204 }
Christopher Fauletc2a89a62017-10-23 15:54:24 +02003205 end:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003206 HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003207 return t;
3208}
3209
Christopher Faulet0108bb32017-10-20 21:34:32 +02003210/* Initializes mailer alerts for the proxy <p> using <mls> parameters.
3211 *
3212 * The function returns 1 in success case, otherwise, it returns 0 and err is
3213 * filled.
3214 */
3215int init_email_alert(struct mailers *mls, struct proxy *p, char **err)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003216{
Christopher Faulet0108bb32017-10-20 21:34:32 +02003217 struct mailer *mailer;
3218 struct email_alertq *queues;
3219 const char *err_str;
3220 int i = 0;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003221
Christopher Faulet0108bb32017-10-20 21:34:32 +02003222 if ((queues = calloc(mls->count, sizeof(*queues))) == NULL) {
3223 memprintf(err, "out of memory while allocating mailer alerts queues");
3224 goto error;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003225 }
3226
Christopher Faulet0108bb32017-10-20 21:34:32 +02003227 for (mailer = mls->mailer_list; mailer; i++, mailer = mailer->next) {
3228 struct email_alertq *q = &queues[i];
3229 struct check *check = &q->check;
3230 struct task *t;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003231
3232 LIST_INIT(&q->email_alerts);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003233 HA_SPIN_INIT(&q->lock);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003234 check->inter = mls->timeout.mail;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003235 check->rise = DEF_AGENT_RISETIME;
3236 check->fall = DEF_AGENT_FALLTIME;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003237 if ((err_str = init_check(check, PR_O2_TCPCHK_CHK))) {
3238 memprintf(err, "%s", err_str);
3239 goto error;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003240 }
3241
3242 check->xprt = mailer->xprt;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003243 check->addr = mailer->addr;
Christopher Fauletb797ae12018-03-27 15:35:35 +02003244 check->port = get_host_port(&mailer->addr);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003245 //check->server = s;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003246
Emeric Brunc60def82017-09-27 14:59:38 +02003247 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet0108bb32017-10-20 21:34:32 +02003248 memprintf(err, "out of memory while allocating mailer alerts task");
3249 goto error;
3250 }
3251
3252 check->task = t;
3253 t->process = process_email_alert;
3254 t->context = check;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003255
Christopher Faulet0108bb32017-10-20 21:34:32 +02003256 /* check this in one ms */
3257 t->expire = TICK_ETERNITY;
3258 check->start = now;
3259 task_queue(t);
3260 }
3261
3262 mls->users++;
3263 free(p->email_alert.mailers.name);
3264 p->email_alert.mailers.m = mls;
3265 p->email_alert.queues = queues;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003266 return 0;
Christopher Faulet0108bb32017-10-20 21:34:32 +02003267
3268 error:
3269 for (i = 0; i < mls->count; i++) {
3270 struct email_alertq *q = &queues[i];
3271 struct check *check = &q->check;
3272
3273 if (check->task) {
3274 task_delete(check->task);
3275 task_free(check->task);
3276 check->task = NULL;
3277 }
3278 free_check(check);
3279 }
3280 free(queues);
3281 return 1;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003282}
3283
3284
3285static int add_tcpcheck_expect_str(struct list *list, const char *str)
3286{
3287 struct tcpcheck_rule *tcpcheck;
3288
Willy Tarreaubafbe012017-11-24 17:34:44 +01003289 if ((tcpcheck = pool_alloc(pool_head_tcpcheck_rule)) == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003290 return 0;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003291 memset(tcpcheck, 0, sizeof(*tcpcheck));
3292 tcpcheck->action = TCPCHK_ACT_EXPECT;
3293 tcpcheck->string = strdup(str);
3294 tcpcheck->expect_regex = NULL;
3295 tcpcheck->comment = NULL;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003296 if (!tcpcheck->string) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003297 pool_free(pool_head_tcpcheck_rule, tcpcheck);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003298 return 0;
3299 }
3300
3301 LIST_ADDQ(list, &tcpcheck->list);
3302 return 1;
3303}
3304
3305static int add_tcpcheck_send_strs(struct list *list, const char * const *strs)
3306{
3307 struct tcpcheck_rule *tcpcheck;
Willy Tarreau64345aa2016-08-10 19:29:09 +02003308 const char *in;
3309 char *dst;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003310 int i;
3311
Willy Tarreaubafbe012017-11-24 17:34:44 +01003312 if ((tcpcheck = pool_alloc(pool_head_tcpcheck_rule)) == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003313 return 0;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003314 memset(tcpcheck, 0, sizeof(*tcpcheck));
3315 tcpcheck->action = TCPCHK_ACT_SEND;
3316 tcpcheck->expect_regex = NULL;
3317 tcpcheck->comment = NULL;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003318 tcpcheck->string_len = 0;
3319 for (i = 0; strs[i]; i++)
3320 tcpcheck->string_len += strlen(strs[i]);
3321
3322 tcpcheck->string = malloc(tcpcheck->string_len + 1);
3323 if (!tcpcheck->string) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003324 pool_free(pool_head_tcpcheck_rule, tcpcheck);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003325 return 0;
3326 }
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003327
Willy Tarreau64345aa2016-08-10 19:29:09 +02003328 dst = tcpcheck->string;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003329 for (i = 0; strs[i]; i++)
Willy Tarreau64345aa2016-08-10 19:29:09 +02003330 for (in = strs[i]; (*dst = *in++); dst++);
3331 *dst = 0;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003332
3333 LIST_ADDQ(list, &tcpcheck->list);
3334 return 1;
3335}
3336
Christopher Faulet0108bb32017-10-20 21:34:32 +02003337static int enqueue_one_email_alert(struct proxy *p, struct server *s,
3338 struct email_alertq *q, const char *msg)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003339{
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003340 struct email_alert *alert;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003341 struct tcpcheck_rule *tcpcheck;
3342 struct check *check = &q->check;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003343
Willy Tarreaubafbe012017-11-24 17:34:44 +01003344 if ((alert = pool_alloc(pool_head_email_alert)) == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003345 goto error;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003346 LIST_INIT(&alert->list);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003347 LIST_INIT(&alert->tcpcheck_rules);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003348 alert->srv = s;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003349
Willy Tarreaubafbe012017-11-24 17:34:44 +01003350 if ((tcpcheck = pool_alloc(pool_head_tcpcheck_rule)) == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003351 goto error;
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003352 memset(tcpcheck, 0, sizeof(*tcpcheck));
3353 tcpcheck->action = TCPCHK_ACT_CONNECT;
3354 tcpcheck->comment = NULL;
3355 tcpcheck->string = NULL;
3356 tcpcheck->expect_regex = NULL;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003357 LIST_ADDQ(&alert->tcpcheck_rules, &tcpcheck->list);
3358
3359 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "220 "))
3360 goto error;
3361
3362 {
3363 const char * const strs[4] = { "EHLO ", p->email_alert.myhostname, "\r\n" };
3364 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3365 goto error;
3366 }
3367
3368 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3369 goto error;
3370
3371 {
3372 const char * const strs[4] = { "MAIL FROM:<", p->email_alert.from, ">\r\n" };
3373 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3374 goto error;
3375 }
3376
3377 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3378 goto error;
3379
3380 {
3381 const char * const strs[4] = { "RCPT TO:<", p->email_alert.to, ">\r\n" };
3382 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3383 goto error;
3384 }
3385
3386 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3387 goto error;
3388
3389 {
3390 const char * const strs[2] = { "DATA\r\n" };
3391 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3392 goto error;
3393 }
3394
3395 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "354 "))
3396 goto error;
3397
3398 {
3399 struct tm tm;
3400 char datestr[48];
3401 const char * const strs[18] = {
Pieter Baauw5e0964e2016-02-13 16:27:35 +01003402 "From: ", p->email_alert.from, "\r\n",
3403 "To: ", p->email_alert.to, "\r\n",
3404 "Date: ", datestr, "\r\n",
3405 "Subject: [HAproxy Alert] ", msg, "\r\n",
3406 "\r\n",
3407 msg, "\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003408 "\r\n",
Pieter Baauwed35c372015-07-22 19:51:54 +02003409 ".\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003410 NULL
3411 };
3412
3413 get_localtime(date.tv_sec, &tm);
3414
3415 if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %T %z (%Z)", &tm) == 0) {
3416 goto error;
3417 }
3418
3419 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3420 goto error;
3421 }
3422
3423 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3424 goto error;
3425
3426 {
3427 const char * const strs[2] = { "QUIT\r\n" };
3428 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3429 goto error;
3430 }
3431
3432 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "221 "))
3433 goto error;
3434
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003435 HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0108bb32017-10-20 21:34:32 +02003436 task_wakeup(check->task, TASK_WOKEN_MSG);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003437 LIST_ADDQ(&q->email_alerts, &alert->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003438 HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003439 return 1;
3440
3441error:
3442 email_alert_free(alert);
3443 return 0;
3444}
3445
Christopher Faulet0108bb32017-10-20 21:34:32 +02003446static void enqueue_email_alert(struct proxy *p, struct server *s, const char *msg)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003447{
3448 int i;
3449 struct mailer *mailer;
3450
3451 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3452 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
Christopher Faulet0108bb32017-10-20 21:34:32 +02003453 if (!enqueue_one_email_alert(p, s, &p->email_alert.queues[i], msg)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003454 ha_alert("Email alert [%s] could not be enqueued: out of memory\n", p->id);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003455 return;
3456 }
3457 }
3458
3459 return;
3460}
3461
3462/*
3463 * Send email alert if configured.
3464 */
Simon Horman64e34162015-02-06 11:11:57 +09003465void send_email_alert(struct server *s, int level, const char *format, ...)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003466{
3467 va_list argp;
3468 char buf[1024];
3469 int len;
3470 struct proxy *p = s->proxy;
3471
Christopher Faulet0108bb32017-10-20 21:34:32 +02003472 if (!p->email_alert.mailers.m || level > p->email_alert.level || format == NULL)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003473 return;
3474
3475 va_start(argp, format);
3476 len = vsnprintf(buf, sizeof(buf), format, argp);
3477 va_end(argp);
3478
Thierry FOURNIER62c8a212017-02-09 12:19:27 +01003479 if (len < 0 || len >= sizeof(buf)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003480 ha_alert("Email alert [%s] could not format message\n", p->id);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003481 return;
3482 }
3483
Christopher Faulet0108bb32017-10-20 21:34:32 +02003484 enqueue_email_alert(p, s, buf);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003485}
3486
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003487/*
3488 * Return value:
3489 * the port to be used for the health check
3490 * 0 in case no port could be found for the check
3491 */
3492int srv_check_healthcheck_port(struct check *chk)
3493{
3494 int i = 0;
3495 struct server *srv = NULL;
3496
3497 srv = chk->server;
3498
3499 /* If neither a port nor an addr was specified and no check transport
3500 * layer is forced, then the transport layer used by the checks is the
3501 * same as for the production traffic. Otherwise we use raw_sock by
3502 * default, unless one is specified.
3503 */
3504 if (!chk->port && !is_addr(&chk->addr)) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003505 chk->use_ssl |= (srv->use_ssl || (srv->proxy->options & PR_O_TCPCHK_SSL));
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003506 chk->send_proxy |= (srv->pp_opts);
3507 }
3508
3509 /* by default, we use the health check port ocnfigured */
3510 if (chk->port > 0)
3511 return chk->port;
3512
3513 /* try to get the port from check_core.addr if check.port not set */
3514 i = get_host_port(&chk->addr);
3515 if (i > 0)
3516 return i;
3517
3518 /* try to get the port from server address */
3519 /* prevent MAPPORTS from working at this point, since checks could
3520 * not be performed in such case (MAPPORTS impose a relative ports
3521 * based on live traffic)
3522 */
3523 if (srv->flags & SRV_F_MAPPORTS)
3524 return 0;
Willy Tarreau04276f32017-01-06 17:41:29 +01003525
3526 i = srv->svc_port; /* by default */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02003527 if (i > 0)
3528 return i;
3529
3530 return 0;
3531}
3532
Willy Tarreau865c5142016-12-21 20:04:48 +01003533__attribute__((constructor))
3534static void __check_init(void)
3535{
3536 hap_register_post_check(start_checks);
Christopher Faulet31dff9b2017-10-23 15:45:20 +02003537
Willy Tarreaubafbe012017-11-24 17:34:44 +01003538 pool_head_email_alert = create_pool("email_alert", sizeof(struct email_alert), MEM_F_SHARED);
3539 pool_head_tcpcheck_rule = create_pool("tcpcheck_rule", sizeof(struct tcpcheck_rule), MEM_F_SHARED);
Willy Tarreau865c5142016-12-21 20:04:48 +01003540}
3541
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003542
Willy Tarreaubd741542010-03-16 18:46:54 +01003543/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003544 * Local variables:
3545 * c-indent-level: 8
3546 * c-basic-offset: 8
3547 * End:
3548 */