blob: 20ed5b4faf6e70181dc85e388df2504210bb3cec [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>
Simon Horman0ba0e4a2015-01-30 11:23:00 +090017#include <stdarg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020018#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020019#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020021#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#include <unistd.h>
Willy Tarreau9f6dc722019-03-01 11:15:10 +010023#include <sys/resource.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024#include <sys/socket.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040025#include <sys/types.h>
Simon Horman98637e52014-06-20 12:30:16 +090026#include <sys/wait.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027#include <netinet/in.h>
Willy Tarreau1274bc42009-07-15 07:16:31 +020028#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029#include <arpa/inet.h>
30
Willy Tarreau122eba92020-06-04 10:15:32 +020031#include <haproxy/action.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020032#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020034#include <haproxy/cfgparse.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020035#include <haproxy/check.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020036#include <haproxy/chunk.h>
Willy Tarreau7c18b542020-06-11 09:23:02 +020037#include <haproxy/dgram.h>
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +020038#include <haproxy/dynbuf.h>
Willy Tarreaubcc67332020-06-05 15:31:31 +020039#include <haproxy/extcheck.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/fd.h>
41#include <haproxy/global.h>
42#include <haproxy/h1.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020043#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020044#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020045#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020046#include <haproxy/istbuf.h>
47#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020048#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020049#include <haproxy/mailers.h>
50#include <haproxy/port_range.h>
51#include <haproxy/proto_tcp.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020052#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020053#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020054#include <haproxy/queue.h>
55#include <haproxy/regex.h>
Emeric Brunc9437992021-02-12 19:42:55 +010056#include <haproxy/resolvers.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020057#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020058#include <haproxy/server.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020059#include <haproxy/ssl_sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020060#include <haproxy/stats-t.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020061#include <haproxy/task.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020062#include <haproxy/tcpcheck.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020063#include <haproxy/thread.h>
64#include <haproxy/time.h>
65#include <haproxy/tools.h>
Christopher Faulet147b8c92021-04-10 09:00:38 +020066#include <haproxy/trace.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020067#include <haproxy/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Christopher Faulet147b8c92021-04-10 09:00:38 +020069/* trace source and events */
70static void check_trace(enum trace_level level, uint64_t mask,
71 const struct trace_source *src,
72 const struct ist where, const struct ist func,
73 const void *a1, const void *a2, const void *a3, const void *a4);
74
75/* The event representation is split like this :
76 * check - check
77 *
78 * CHECK_EV_* macros are defined in <haproxy/check.h>
79 */
80static const struct trace_event check_trace_events[] = {
81 { .mask = CHK_EV_TASK_WAKE, .name = "task_wake", .desc = "Check task woken up" },
82 { .mask = CHK_EV_HCHK_START, .name = "hchck_start", .desc = "Health-check started" },
83 { .mask = CHK_EV_HCHK_WAKE, .name = "hchck_wake", .desc = "Health-check woken up" },
84 { .mask = CHK_EV_HCHK_RUN, .name = "hchck_run", .desc = "Health-check running" },
85 { .mask = CHK_EV_HCHK_END, .name = "hchck_end", .desc = "Health-check terminated" },
86 { .mask = CHK_EV_HCHK_SUCC, .name = "hchck_succ", .desc = "Health-check success" },
87 { .mask = CHK_EV_HCHK_ERR, .name = "hchck_err", .desc = "Health-check failure" },
88
89 { .mask = CHK_EV_TCPCHK_EVAL, .name = "tcp_check_eval", .desc = "tcp-check rules evaluation" },
90 { .mask = CHK_EV_TCPCHK_ERR, .name = "tcp_check_err", .desc = "tcp-check evaluation error" },
91 { .mask = CHK_EV_TCPCHK_CONN, .name = "tcp_check_conn", .desc = "tcp-check connection rule" },
92 { .mask = CHK_EV_TCPCHK_SND, .name = "tcp_check_send", .desc = "tcp-check send rule" },
93 { .mask = CHK_EV_TCPCHK_EXP, .name = "tcp_check_expect", .desc = "tcp-check expect rule" },
94 { .mask = CHK_EV_TCPCHK_ACT, .name = "tcp_check_action", .desc = "tcp-check action rule" },
95
96 { .mask = CHK_EV_RX_DATA, .name = "rx_data", .desc = "receipt of data" },
97 { .mask = CHK_EV_RX_BLK, .name = "rx_blk", .desc = "receipt blocked" },
98 { .mask = CHK_EV_RX_ERR, .name = "rx_err", .desc = "receipt error" },
99
100 { .mask = CHK_EV_TX_DATA, .name = "tx_data", .desc = "transmission of data" },
101 { .mask = CHK_EV_TX_BLK, .name = "tx_blk", .desc = "transmission blocked" },
102 { .mask = CHK_EV_TX_ERR, .name = "tx_err", .desc = "transmission error" },
103
104 {}
105};
106
107static const struct name_desc check_trace_lockon_args[4] = {
108 /* arg1 */ { /* already used by the check */ },
109 /* arg2 */ { },
110 /* arg3 */ { },
111 /* arg4 */ { }
112};
113
114static const struct name_desc check_trace_decoding[] = {
115#define CHK_VERB_CLEAN 1
116 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
117#define CHK_VERB_MINIMAL 2
Willy Tarreau4596fe22022-05-17 19:07:51 +0200118 { .name="minimal", .desc="report info on streams and connectors" },
Christopher Faulet147b8c92021-04-10 09:00:38 +0200119#define CHK_VERB_SIMPLE 3
120 { .name="simple", .desc="add info on request and response channels" },
121#define CHK_VERB_ADVANCED 4
122 { .name="advanced", .desc="add info on channel's buffer for data and developer levels only" },
123#define CHK_VERB_COMPLETE 5
124 { .name="complete", .desc="add info on channel's buffer" },
125 { /* end */ }
126};
127
128struct trace_source trace_check = {
129 .name = IST("check"),
130 .desc = "Health-check",
131 .arg_def = TRC_ARG1_CHK, // TRACE()'s first argument is always a stream
132 .default_cb = check_trace,
133 .known_events = check_trace_events,
134 .lockon_args = check_trace_lockon_args,
135 .decoding = check_trace_decoding,
136 .report_events = ~0, // report everything by default
137};
138
139#define TRACE_SOURCE &trace_check
140INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
141
Olivier Houchard9130a962017-10-17 17:33:43 +0200142
Gaetan Rivet05d692d2020-02-14 17:42:54 +0100143/* Dummy frontend used to create all checks sessions. */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200144struct proxy checks_fe;
Christopher Faulet31dff9b2017-10-23 15:45:20 +0200145
Christopher Faulet147b8c92021-04-10 09:00:38 +0200146
147static inline void check_trace_buf(const struct buffer *buf, size_t ofs, size_t len)
148{
149 size_t block1, block2;
150 int line, ptr, newptr;
151
152 block1 = b_contig_data(buf, ofs);
153 block2 = 0;
154 if (block1 > len)
155 block1 = len;
156 block2 = len - block1;
157
158 ofs = b_peek_ofs(buf, ofs);
159
160 line = 0;
161 ptr = ofs;
162 while (ptr < ofs + block1) {
163 newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), ofs + block1, &line, ptr);
164 if (newptr == ptr)
165 break;
166 ptr = newptr;
167 }
168
169 line = ptr = 0;
170 while (ptr < block2) {
171 newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), block2, &line, ptr);
172 if (newptr == ptr)
173 break;
174 ptr = newptr;
175 }
176}
177
178/* trace source and events */
179static void check_trace(enum trace_level level, uint64_t mask,
180 const struct trace_source *src,
181 const struct ist where, const struct ist func,
182 const void *a1, const void *a2, const void *a3, const void *a4)
183{
184 const struct check *check = a1;
185 const struct server *srv = (check ? check->server : NULL);
186 const size_t *val = a4;
187 const char *res;
188
189 if (!check || src->verbosity < CHK_VERB_CLEAN)
190 return;
191
192 chunk_appendf(&trace_buf, " : [%c] SRV=%s",
193 ((check->type == PR_O2_EXT_CHK) ? 'E' : (check->state & CHK_ST_AGENT ? 'A' : 'H')),
194 srv->id);
195
196 chunk_appendf(&trace_buf, " status=%d/%d %s",
197 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
198 (check->health >= check->rise) ? check->fall : check->rise,
199 (check->health >= check->rise) ? (srv->uweight ? "UP" : "DRAIN") : "DOWN");
200
201 switch (check->result) {
202 case CHK_RES_NEUTRAL: res = "-"; break;
203 case CHK_RES_FAILED: res = "FAIL"; break;
204 case CHK_RES_PASSED: res = "PASS"; break;
205 case CHK_RES_CONDPASS: res = "COND"; break;
206 default: res = "UNK"; break;
207 }
208
209 if (src->verbosity == CHK_VERB_CLEAN)
210 return;
211
212 chunk_appendf(&trace_buf, " - last=%s(%d)/%s(%d)",
213 get_check_status_info(check->status), check->status,
214 res, check->result);
215
216 /* Display the value to the 4th argument (level > STATE) */
217 if (src->level > TRACE_LEVEL_STATE && val)
218 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
219
220 chunk_appendf(&trace_buf, " check=%p(0x%08x)", check, check->state);
221
222 if (src->verbosity == CHK_VERB_MINIMAL)
223 return;
224
225
226 if (check->cs) {
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200227 struct connection *conn = sc_conn(check->cs);
Christopher Faulet0256da12021-12-15 09:50:17 +0100228
229 chunk_appendf(&trace_buf, " - conn=%p(0x%08x)", conn, conn ? conn->flags : 0);
Christopher Faulet147b8c92021-04-10 09:00:38 +0200230 chunk_appendf(&trace_buf, " cs=%p(0x%08x)", check->cs, check->cs->flags);
231 }
232
233 if (mask & CHK_EV_TCPCHK) {
234 const char *type;
235
236 switch (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) {
237 case TCPCHK_RULES_PGSQL_CHK: type = "PGSQL"; break;
238 case TCPCHK_RULES_REDIS_CHK: type = "REDIS"; break;
239 case TCPCHK_RULES_SMTP_CHK: type = "SMTP"; break;
240 case TCPCHK_RULES_HTTP_CHK: type = "HTTP"; break;
241 case TCPCHK_RULES_MYSQL_CHK: type = "MYSQL"; break;
242 case TCPCHK_RULES_LDAP_CHK: type = "LDAP"; break;
243 case TCPCHK_RULES_SSL3_CHK: type = "SSL3"; break;
244 case TCPCHK_RULES_AGENT_CHK: type = "AGENT"; break;
245 case TCPCHK_RULES_SPOP_CHK: type = "SPOP"; break;
246 case TCPCHK_RULES_TCP_CHK: type = "TCP"; break;
247 default: type = "???"; break;
248 }
249 if (check->current_step)
250 chunk_appendf(&trace_buf, " - tcp-check=(%s,%d)", type, tcpcheck_get_step_id(check, NULL));
251 else
252 chunk_appendf(&trace_buf, " - tcp-check=(%s,-)", type);
253 }
254
255 /* Display bi and bo buffer info (level > USER & verbosity > SIMPLE) */
256 if (src->level > TRACE_LEVEL_USER) {
257 const struct buffer *buf = NULL;
258
259 chunk_appendf(&trace_buf, " bi=%u@%p+%u/%u",
260 (unsigned int)b_data(&check->bi), b_orig(&check->bi),
261 (unsigned int)b_head_ofs(&check->bi), (unsigned int)b_size(&check->bi));
262 chunk_appendf(&trace_buf, " bo=%u@%p+%u/%u",
263 (unsigned int)b_data(&check->bo), b_orig(&check->bo),
264 (unsigned int)b_head_ofs(&check->bo), (unsigned int)b_size(&check->bo));
265
266 if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_RX)))
267 buf = (b_is_null(&check->bi) ? NULL : &check->bi);
268 else if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_TX)))
269 buf = (b_is_null(&check->bo) ? NULL : &check->bo);
270
271 if (buf) {
272 if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) {
273 int full = (src->verbosity == CHK_VERB_COMPLETE);
274
275 chunk_memcat(&trace_buf, "\n\t", 2);
276 htx_dump(&trace_buf, htxbuf(buf), full);
277 }
278 else {
279 int max = ((src->verbosity == CHK_VERB_COMPLETE) ? 1024 : 256);
280
281 chunk_memcat(&trace_buf, "\n", 1);
282 if (b_data(buf) > max) {
283 check_trace_buf(buf, 0, max);
284 chunk_memcat(&trace_buf, " ...\n", 6);
285 }
286 else
287 check_trace_buf(buf, 0, b_data(buf));
288 }
289
290 }
291 }
292
293}
294
295
Christopher Faulet61cc8522020-04-20 14:54:42 +0200296/**************************************************************************/
297/************************ Handle check results ****************************/
298/**************************************************************************/
299struct check_status {
300 short result; /* one of SRV_CHK_* */
301 char *info; /* human readable short info */
302 char *desc; /* long description */
303};
304
305struct analyze_status {
306 char *desc; /* description */
307 unsigned char lr[HANA_OBS_SIZE]; /* result for l4/l7: 0 = ignore, 1 - error, 2 - OK */
308};
309
Simon Horman63a4a822012-03-19 07:24:41 +0900310static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100311 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
312 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200313 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200314
Willy Tarreau23964182014-05-20 20:56:30 +0200315 /* Below we have finished checks */
316 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100317 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100318
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100319 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200320
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100321 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
322 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
323 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200324
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100325 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
326 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
327 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200328
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100329 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
330 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200331
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200332 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200333
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100334 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
335 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
336 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +0900337
338 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
339 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +0200340 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200341};
342
Simon Horman63a4a822012-03-19 07:24:41 +0900343static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100344 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
345
346 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
347 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
348
349 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
350 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
351 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
352 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
353
354 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
355 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
356 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
357};
358
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100359/* checks if <err> is a real error for errno or one that can be ignored, and
360 * return 0 for these ones or <err> for real ones.
361 */
362static inline int unclean_errno(int err)
363{
Willy Tarreauacef5e22022-04-25 20:32:15 +0200364 if (err == EAGAIN || err == EWOULDBLOCK || err == EINPROGRESS ||
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100365 err == EISCONN || err == EALREADY)
366 return 0;
367 return err;
368}
369
Christopher Faulet7aa32712021-02-01 13:11:50 +0100370/* Converts check_status code to result code */
371short get_check_status_result(short check_status)
372{
373 if (check_status < HCHK_STATUS_SIZE)
374 return check_statuses[check_status].result;
375 else
376 return check_statuses[HCHK_STATUS_UNKNOWN].result;
377}
378
Christopher Faulet61cc8522020-04-20 14:54:42 +0200379/* Converts check_status code to description */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200380const char *get_check_status_description(short check_status) {
381
382 const char *desc;
383
384 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200385 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200386 else
387 desc = NULL;
388
389 if (desc && *desc)
390 return desc;
391 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200392 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200393}
394
Christopher Faulet61cc8522020-04-20 14:54:42 +0200395/* Converts check_status code to short info */
William Dauchyb26122b2021-02-14 22:26:23 +0100396const char *get_check_status_info(short check_status)
397{
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200398 const char *info;
399
400 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200401 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200402 else
403 info = NULL;
404
405 if (info && *info)
406 return info;
407 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200408 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200409}
410
Christopher Faulet61cc8522020-04-20 14:54:42 +0200411/* Convert analyze_status to description */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100412const char *get_analyze_status(short analyze_status) {
413
414 const char *desc;
415
416 if (analyze_status < HANA_STATUS_SIZE)
417 desc = analyze_statuses[analyze_status].desc;
418 else
419 desc = NULL;
420
421 if (desc && *desc)
422 return desc;
423 else
424 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
425}
426
Christopher Faulet61cc8522020-04-20 14:54:42 +0200427/* Sets check->status, update check->duration and fill check->result with an
428 * adequate CHK_RES_* value. The new check->health is computed based on the
429 * result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200430 *
Christopher Faulet61cc8522020-04-20 14:54:42 +0200431 * Shows information in logs about failed health check if server is UP or
432 * succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200433 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200434void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100435{
Simon Horman4a741432013-02-23 15:35:38 +0900436 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200437 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200438 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900439
Christopher Faulet147b8c92021-04-10 09:00:38 +0200440 TRACE_POINT(CHK_EV_HCHK_RUN, check);
441
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200442 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100443 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900444 check->desc[0] = '\0';
445 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200446 return;
447 }
448
Simon Horman4a741432013-02-23 15:35:38 +0900449 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200450 return;
451
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200452 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900453 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
454 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200455 } else
Simon Horman4a741432013-02-23 15:35:38 +0900456 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200457
Simon Horman4a741432013-02-23 15:35:38 +0900458 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200459 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900460 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200461
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100462 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900463 check->duration = -1;
464 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200465 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900466 check->duration = tv_ms_elapsed(&check->start, &now);
467 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200468 }
469
Willy Tarreau23964182014-05-20 20:56:30 +0200470 /* no change is expected if no state change occurred */
471 if (check->result == CHK_RES_NEUTRAL)
472 return;
473
Olivier Houchard0923fa42019-01-11 18:43:04 +0100474 /* If the check was really just sending a mail, it won't have an
475 * associated server, so we're done now.
476 */
477 if (!s)
478 return;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200479 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200480
Christopher Faulet147b8c92021-04-10 09:00:38 +0200481
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200482 switch (check->result) {
483 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200484 /* Failure to connect to the agent as a secondary check should not
485 * cause the server to be marked down.
486 */
487 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900488 (check->status >= HCHK_STATUS_L57DATA)) &&
Christopher Fauletb119a792018-05-02 12:12:45 +0200489 (check->health > 0)) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200490 _HA_ATOMIC_INC(&s->counters.failed_checks);
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200491 report = 1;
492 check->health--;
493 if (check->health < check->rise)
494 check->health = 0;
495 }
496 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200497
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200498 case CHK_RES_PASSED:
Christopher Faulet1e527cb2020-11-20 18:13:02 +0100499 case CHK_RES_CONDPASS:
500 if (check->health < check->rise + check->fall - 1) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200501 report = 1;
502 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200503
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200504 if (check->health >= check->rise)
505 check->health = check->rise + check->fall - 1; /* OK now */
506 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200507
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200508 /* clear consecutive_errors if observing is enabled */
509 if (s->onerror)
510 s->consecutive_errors = 0;
511 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100512
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200513 default:
514 break;
515 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200516
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200517 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
518 (status != prev_status || report)) {
519 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200520 "%s check for %sserver %s/%s %s%s",
521 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200522 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100523 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100524 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200525 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200526
Emeric Brun5a133512017-10-19 14:42:30 +0200527 srv_append_status(&trash, s, check, -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200528
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100529 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200530 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
531 (check->health >= check->rise) ? check->fall : check->rise,
532 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200533
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200534 ha_warning("%s.\n", trash.area);
535 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
536 send_email_alert(s, LOG_INFO, "%s", trash.area);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200537 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200538}
539
Willy Tarreau4eec5472014-05-20 22:32:27 +0200540/* Marks the check <check>'s server down if the current check is already failed
541 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200542 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200543void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544{
Simon Horman4a741432013-02-23 15:35:38 +0900545 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900546
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200547 /* The agent secondary check should only cause a server to be marked
548 * as down if check->status is HCHK_STATUS_L7STS, which indicates
549 * that the agent returned "fail", "stopped" or "down".
550 * The implication here is that failure to connect to the agent
551 * as a secondary check should not cause the server to be marked
552 * down. */
553 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
554 return;
555
Willy Tarreau4eec5472014-05-20 22:32:27 +0200556 if (check->health > 0)
557 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100558
Christopher Faulet147b8c92021-04-10 09:00:38 +0200559 TRACE_STATE("health-check failed, set server DOWN", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200560 /* We only report a reason for the check if we did not do so previously */
Emeric Brun5a133512017-10-19 14:42:30 +0200561 srv_set_stopped(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200562}
563
Willy Tarreauaf549582014-05-16 17:37:50 +0200564/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200565 * it isn't in maintenance, it is not tracking a down server and other checks
566 * comply. The rule is simple : by default, a server is up, unless any of the
567 * following conditions is true :
568 * - health check failed (check->health < rise)
569 * - agent check failed (agent->health < rise)
570 * - the server tracks a down server (track && track->state == STOPPED)
571 * Note that if the server has a slowstart, it will switch to STARTING instead
572 * of RUNNING. Also, only the health checks support the nolb mode, so the
573 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200574 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200575void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200576{
Simon Horman4a741432013-02-23 15:35:38 +0900577 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100578
Emeric Brun52a91d32017-08-31 14:41:55 +0200579 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200580 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100581
Emeric Brun52a91d32017-08-31 14:41:55 +0200582 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200583 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100584
Willy Tarreau3e048382014-05-21 10:30:54 +0200585 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
586 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100587
Willy Tarreau3e048382014-05-21 10:30:54 +0200588 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
589 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200590
Emeric Brun52a91d32017-08-31 14:41:55 +0200591 if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING)
Willy Tarreau3e048382014-05-21 10:30:54 +0200592 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100593
Christopher Faulet147b8c92021-04-10 09:00:38 +0200594 TRACE_STATE("health-check succeeded, set server RUNNING", CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
Emeric Brun5a133512017-10-19 14:42:30 +0200595 srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100596}
597
Willy Tarreaudb58b792014-05-21 13:57:23 +0200598/* Marks the check <check> as valid and tries to set its server into stopping mode
599 * if it was running or starting, and provided it isn't in maintenance and other
600 * checks comply. The conditions for the server to be marked in stopping mode are
601 * the same as for it to be turned up. Also, only the health checks support the
602 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200603 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200604void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200605{
Simon Horman4a741432013-02-23 15:35:38 +0900606 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100607
Emeric Brun52a91d32017-08-31 14:41:55 +0200608 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200609 return;
610
Willy Tarreaudb58b792014-05-21 13:57:23 +0200611 if (check->state & CHK_ST_AGENT)
612 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100613
Emeric Brun52a91d32017-08-31 14:41:55 +0200614 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreaudb58b792014-05-21 13:57:23 +0200615 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100616
Willy Tarreaudb58b792014-05-21 13:57:23 +0200617 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
618 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100619
Willy Tarreaudb58b792014-05-21 13:57:23 +0200620 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
621 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100622
Christopher Faulet147b8c92021-04-10 09:00:38 +0200623 TRACE_STATE("health-check condionnaly succeeded, set server STOPPING", CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
Willy Tarreaub26881a2017-12-23 11:16:49 +0100624 srv_set_stopping(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100625}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200626
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100627/* note: use health_adjust() only, which first checks that the observe mode is
Willy Tarreau4e9df272021-02-17 15:20:19 +0100628 * enabled. This will take the server lock if needed.
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100629 */
630void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100631{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100632 int failed;
633 int expire;
634
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100635 if (s->observe >= HANA_OBS_SIZE)
636 return;
637
Willy Tarreaubb956662013-01-24 00:37:39 +0100638 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100639 return;
640
641 switch (analyze_statuses[status].lr[s->observe - 1]) {
642 case 1:
643 failed = 1;
644 break;
645
646 case 2:
647 failed = 0;
648 break;
649
650 default:
651 return;
652 }
653
654 if (!failed) {
655 /* good: clear consecutive_errors */
656 s->consecutive_errors = 0;
657 return;
658 }
659
Willy Tarreau4781b152021-04-06 13:53:36 +0200660 _HA_ATOMIC_INC(&s->consecutive_errors);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100661
662 if (s->consecutive_errors < s->consecutive_errors_limit)
663 return;
664
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100665 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
666 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100667
Willy Tarreau4e9df272021-02-17 15:20:19 +0100668 if (s->check.fastinter)
669 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
670 else
671 expire = TICK_ETERNITY;
672
673 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
674
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100675 switch (s->onerror) {
676 case HANA_ONERR_FASTINTER:
677 /* force fastinter - nothing to do here as all modes force it */
678 break;
679
680 case HANA_ONERR_SUDDTH:
681 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900682 if (s->check.health > s->check.rise)
683 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100684
Tim Duesterhus588b3142020-05-29 14:35:51 +0200685 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100686
687 case HANA_ONERR_FAILCHK:
688 /* simulate a failed health check */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200689 set_server_check_status(&s->check, HCHK_STATUS_HANA,
690 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200691 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100692 break;
693
694 case HANA_ONERR_MARKDWN:
695 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900696 s->check.health = s->check.rise;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200697 set_server_check_status(&s->check, HCHK_STATUS_HANA,
698 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200699 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100700 break;
701
702 default:
703 /* write a warning? */
704 break;
705 }
706
Willy Tarreau4e9df272021-02-17 15:20:19 +0100707 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
708
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100709 s->consecutive_errors = 0;
Willy Tarreau4781b152021-04-06 13:53:36 +0200710 _HA_ATOMIC_INC(&s->counters.failed_hana);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100711
Christopher Fauletea860832021-05-07 11:45:26 +0200712 if (tick_isset(expire) && tick_is_lt(expire, s->check.task->expire)) {
Willy Tarreau4e9df272021-02-17 15:20:19 +0100713 /* requeue check task with new expire */
714 task_schedule(s->check.task, expire);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100715 }
Willy Tarreauef781042010-01-27 11:53:01 +0100716}
717
Christopher Faulet61cc8522020-04-20 14:54:42 +0200718/* Checks the connection. If an error has already been reported or the socket is
Willy Tarreau20a18342013-12-05 00:31:46 +0100719 * closed, keep errno intact as it is supposed to contain the valid error code.
720 * If no error is reported, check the socket's error queue using getsockopt().
721 * Warning, this must be done only once when returning from poll, and never
722 * after an I/O error was attempted, otherwise the error queue might contain
723 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
724 * socket. Returns non-zero if an error was reported, zero if everything is
725 * clean (including a properly closed socket).
726 */
727static int retrieve_errno_from_socket(struct connection *conn)
728{
729 int skerr;
730 socklen_t lskerr = sizeof(skerr);
731
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100732 if (conn->flags & CO_FL_ERROR && (unclean_errno(errno) || !conn->ctrl))
Willy Tarreau20a18342013-12-05 00:31:46 +0100733 return 1;
734
Willy Tarreau3c728722014-01-23 13:50:42 +0100735 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100736 return 0;
737
Willy Tarreau07ecfc52022-04-11 18:07:03 +0200738 BUG_ON(conn->flags & CO_FL_FDLESS);
739
Willy Tarreau585744b2017-08-24 14:31:19 +0200740 if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
Willy Tarreau20a18342013-12-05 00:31:46 +0100741 errno = skerr;
742
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100743 errno = unclean_errno(errno);
Willy Tarreau20a18342013-12-05 00:31:46 +0100744
745 if (!errno) {
746 /* we could not retrieve an error, that does not mean there is
747 * none. Just don't change anything and only report the prior
748 * error if any.
749 */
750 if (conn->flags & CO_FL_ERROR)
751 return 1;
752 else
753 return 0;
754 }
755
756 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
757 return 1;
758}
759
Christopher Faulet61cc8522020-04-20 14:54:42 +0200760/* Tries to collect as much information as possible on the connection status,
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100761 * and adjust the server status accordingly. It may make use of <errno_bck>
762 * if non-null when the caller is absolutely certain of its validity (eg:
763 * checked just after a syscall). If the caller doesn't have a valid errno,
764 * it can pass zero, and retrieve_errno_from_socket() will be called to try
765 * to extract errno from the socket. If no error is reported, it will consider
766 * the <expired> flag. This is intended to be used when a connection error was
767 * reported in conn->flags or when a timeout was reported in <expired>. The
768 * function takes care of not updating a server status which was already set.
769 * All situations where at least one of <expired> or CO_FL_ERROR are set
770 * produce a status.
771 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200772void chk_report_conn_err(struct check *check, int errno_bck, int expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100773{
Willy Tarreau4596fe22022-05-17 19:07:51 +0200774 struct stconn *cs = check->cs;
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200775 struct connection *conn = sc_conn(cs);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100776 const char *err_msg;
Willy Tarreau83061a82018-07-13 11:56:34 +0200777 struct buffer *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200778 int step;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100779
Christopher Faulet147b8c92021-04-10 09:00:38 +0200780 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100781 return;
Christopher Faulet147b8c92021-04-10 09:00:38 +0200782 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100783
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100784 errno = unclean_errno(errno_bck);
785 if (conn && errno)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100786 retrieve_errno_from_socket(conn);
787
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200788 if (conn && !(conn->flags & CO_FL_ERROR) &&
Willy Tarreaub605c422022-05-17 17:04:55 +0200789 cs && !sc_ep_test(cs, SE_FL_ERROR) && !expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100790 return;
791
Christopher Faulet147b8c92021-04-10 09:00:38 +0200792 TRACE_ENTER(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check, 0, 0, (size_t[]){expired});
793
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100794 /* we'll try to build a meaningful error message depending on the
795 * context of the error possibly present in conn->err_code, and the
796 * socket error possibly collected above. This is useful to know the
797 * exact step of the L6 layer (eg: SSL handshake).
798 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200799 chk = get_trash_chunk();
800
Christopher Faulet799f3a42020-04-07 12:06:14 +0200801 if (check->type == PR_O2_TCPCHK_CHK &&
Christopher Fauletd7e63962020-04-17 20:15:59 +0200802 (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) {
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200803 step = tcpcheck_get_step_id(check, NULL);
Christopher Faulet147b8c92021-04-10 09:00:38 +0200804 if (!step) {
805 TRACE_DEVEL("initial connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200806 chunk_printf(chk, " at initial connection step of tcp-check");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200807 }
Willy Tarreau213c6782014-10-02 14:51:02 +0200808 else {
809 chunk_printf(chk, " at step %d of tcp-check", step);
810 /* we were looking for a string */
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200811 if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) {
812 if (check->current_step->connect.port)
813 chunk_appendf(chk, " (connect port %d)" ,check->current_step->connect.port);
Willy Tarreau213c6782014-10-02 14:51:02 +0200814 else
815 chunk_appendf(chk, " (connect)");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200816 TRACE_DEVEL("connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200817 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200818 else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) {
819 struct tcpcheck_expect *expect = &check->current_step->expect;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100820
821 switch (expect->type) {
822 case TCPCHK_EXPECT_STRING:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200823 chunk_appendf(chk, " (expect string '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100824 break;
825 case TCPCHK_EXPECT_BINARY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200826 chunk_appendf(chk, " (expect binary '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100827 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200828 case TCPCHK_EXPECT_STRING_REGEX:
Willy Tarreau213c6782014-10-02 14:51:02 +0200829 chunk_appendf(chk, " (expect regex)");
Gaetan Rivetb616add2020-02-07 15:37:17 +0100830 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200831 case TCPCHK_EXPECT_BINARY_REGEX:
Gaetan Rivetefab6c62020-02-07 15:37:17 +0100832 chunk_appendf(chk, " (expect binary regex)");
833 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200834 case TCPCHK_EXPECT_STRING_LF:
835 chunk_appendf(chk, " (expect log-format string)");
836 break;
837 case TCPCHK_EXPECT_BINARY_LF:
838 chunk_appendf(chk, " (expect log-format binary)");
839 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200840 case TCPCHK_EXPECT_HTTP_STATUS:
Christopher Faulet8021a5f2020-04-24 13:53:12 +0200841 chunk_appendf(chk, " (expect HTTP status codes)");
Christopher Faulete5870d82020-04-15 11:32:03 +0200842 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200843 case TCPCHK_EXPECT_HTTP_STATUS_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200844 chunk_appendf(chk, " (expect HTTP status regex)");
845 break;
Christopher Faulet39708192020-05-05 10:47:36 +0200846 case TCPCHK_EXPECT_HTTP_HEADER:
847 chunk_appendf(chk, " (expect HTTP header pattern)");
848 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200849 case TCPCHK_EXPECT_HTTP_BODY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200850 chunk_appendf(chk, " (expect HTTP body content '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Christopher Faulete5870d82020-04-15 11:32:03 +0200851 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200852 case TCPCHK_EXPECT_HTTP_BODY_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200853 chunk_appendf(chk, " (expect HTTP body regex)");
854 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200855 case TCPCHK_EXPECT_HTTP_BODY_LF:
856 chunk_appendf(chk, " (expect log-format HTTP body)");
857 break;
Christopher Faulet9e6ed152020-04-03 15:24:06 +0200858 case TCPCHK_EXPECT_CUSTOM:
859 chunk_appendf(chk, " (expect custom function)");
860 break;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100861 case TCPCHK_EXPECT_UNDEF:
862 chunk_appendf(chk, " (undefined expect!)");
863 break;
864 }
Christopher Faulet147b8c92021-04-10 09:00:38 +0200865 TRACE_DEVEL("expect rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200866 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200867 else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) {
Willy Tarreau213c6782014-10-02 14:51:02 +0200868 chunk_appendf(chk, " (send)");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200869 TRACE_DEVEL("send rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200870 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200871
Christopher Faulet6f2a5e42020-04-01 13:11:41 +0200872 if (check->current_step && check->current_step->comment)
873 chunk_appendf(chk, " comment: '%s'", check->current_step->comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200874 }
875 }
876
Willy Tarreau00149122017-10-04 18:05:01 +0200877 if (conn && conn->err_code) {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100878 if (unclean_errno(errno))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200879 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno),
880 chk->area);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100881 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200882 chunk_printf(&trash, "%s%s", conn_err_code_str(conn),
883 chk->area);
884 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100885 }
886 else {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100887 if (unclean_errno(errno)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200888 chunk_printf(&trash, "%s%s", strerror(errno),
889 chk->area);
890 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100891 }
892 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200893 err_msg = chk->area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100894 }
895 }
896
Willy Tarreau00149122017-10-04 18:05:01 +0200897 if (check->state & CHK_ST_PORT_MISS) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200898 /* NOTE: this is reported after <fall> tries */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200899 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
900 }
901
Christopher Faulet0256da12021-12-15 09:50:17 +0100902 if (!cs || !conn || !conn->ctrl) {
Christopher Faulet5e293762020-10-26 11:10:49 +0100903 /* error before any connection attempt (connection allocation error or no control layer) */
Willy Tarreau00149122017-10-04 18:05:01 +0200904 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
905 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100906 else if (conn->flags & CO_FL_WAIT_L4_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100907 /* L4 not established (yet) */
Willy Tarreaub605c422022-05-17 17:04:55 +0200908 if (conn->flags & CO_FL_ERROR || sc_ep_test(cs, SE_FL_ERROR))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100909 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
910 else if (expired)
911 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200912
913 /*
914 * might be due to a server IP change.
915 * Let's trigger a DNS resolution if none are currently running.
916 */
Olivier Houchard0923fa42019-01-11 18:43:04 +0100917 if (check->server)
Emeric Brund30e9a12020-12-23 18:49:16 +0100918 resolv_trigger_resolution(check->server->resolv_requester);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200919
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100920 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100921 else if (conn->flags & CO_FL_WAIT_L6_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100922 /* L6 not established (yet) */
Willy Tarreaub605c422022-05-17 17:04:55 +0200923 if (conn->flags & CO_FL_ERROR || sc_ep_test(cs, SE_FL_ERROR))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100924 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
925 else if (expired)
926 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
927 }
Willy Tarreaub605c422022-05-17 17:04:55 +0200928 else if (conn->flags & CO_FL_ERROR || sc_ep_test(cs, SE_FL_ERROR)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100929 /* I/O error after connection was established and before we could diagnose */
930 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
931 }
932 else if (expired) {
Christopher Fauletcf80f2f2020-04-01 11:04:52 +0200933 enum healthcheck_status tout = HCHK_STATUS_L7TOUT;
934
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100935 /* connection established but expired check */
Christopher Faulet1941bab2020-05-05 07:55:50 +0200936 if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT &&
937 check->current_step->expect.tout_status != HCHK_STATUS_UNKNOWN)
Christopher Faulet811f78c2020-04-01 11:10:27 +0200938 tout = check->current_step->expect.tout_status;
939 set_server_check_status(check, tout, err_msg);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100940 }
941
Christopher Faulet147b8c92021-04-10 09:00:38 +0200942 TRACE_LEAVE(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100943 return;
944}
945
Simon Horman98637e52014-06-20 12:30:16 +0900946
Christopher Faulet61cc8522020-04-20 14:54:42 +0200947/* Builds the server state header used by HTTP health-checks */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200948int httpchk_build_status_header(struct server *s, struct buffer *buf)
Simon Horman98637e52014-06-20 12:30:16 +0900949{
Christopher Faulet61cc8522020-04-20 14:54:42 +0200950 int sv_state;
951 int ratio;
952 char addr[46];
953 char port[6];
954 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
955 "UP %d/%d", "UP",
956 "NOLB %d/%d", "NOLB",
957 "no check" };
Simon Horman98637e52014-06-20 12:30:16 +0900958
Christopher Faulet61cc8522020-04-20 14:54:42 +0200959 if (!(s->check.state & CHK_ST_ENABLED))
960 sv_state = 6;
961 else if (s->cur_state != SRV_ST_STOPPED) {
962 if (s->check.health == s->check.rise + s->check.fall - 1)
963 sv_state = 3; /* UP */
964 else
965 sv_state = 2; /* going down */
Simon Horman98637e52014-06-20 12:30:16 +0900966
Christopher Faulet61cc8522020-04-20 14:54:42 +0200967 if (s->cur_state == SRV_ST_STOPPING)
968 sv_state += 2;
969 } else {
970 if (s->check.health)
971 sv_state = 1; /* going up */
972 else
973 sv_state = 0; /* DOWN */
Simon Horman98637e52014-06-20 12:30:16 +0900974 }
Willy Tarreaub7b24782016-06-21 15:32:29 +0200975
Christopher Faulet61cc8522020-04-20 14:54:42 +0200976 chunk_appendf(buf, srv_hlt_st[sv_state],
977 (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
978 (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreaub7b24782016-06-21 15:32:29 +0200979
Christopher Faulet61cc8522020-04-20 14:54:42 +0200980 addr_to_str(&s->addr, addr, sizeof(addr));
981 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
982 snprintf(port, sizeof(port), "%u", s->svc_port);
983 else
984 *port = 0;
Willy Tarreaub7b24782016-06-21 15:32:29 +0200985
Christopher Faulet61cc8522020-04-20 14:54:42 +0200986 chunk_appendf(buf, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
987 addr, port, s->proxy->id, s->id,
988 global.node,
989 (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
990 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
Willy Tarreau7f3c1df2021-06-18 09:22:21 +0200991 s->cur_sess, s->proxy->beconn - s->proxy->queue.length,
Willy Tarreaua0570452021-06-18 09:30:30 +0200992 s->queue.length);
Willy Tarreau9f6dc722019-03-01 11:15:10 +0100993
Christopher Faulet61cc8522020-04-20 14:54:42 +0200994 if ((s->cur_state == SRV_ST_STARTING) &&
995 now.tv_sec < s->last_change + s->slowstart &&
996 now.tv_sec >= s->last_change) {
997 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
998 chunk_appendf(buf, "; throttle=%d%%", ratio);
999 }
Christopher Fauletaaae9a02020-04-26 09:50:31 +02001000
Christopher Faulet61cc8522020-04-20 14:54:42 +02001001 return b_data(buf);
1002}
Christopher Fauletaaae9a02020-04-26 09:50:31 +02001003
Willy Tarreau51cd5952020-06-05 12:25:38 +02001004/**************************************************************************/
Willy Tarreau51cd5952020-06-05 12:25:38 +02001005/***************** Health-checks based on connections *********************/
1006/**************************************************************************/
1007/* This function is used only for server health-checks. It handles connection
1008 * status updates including errors. If necessary, it wakes the check task up.
1009 * It returns 0 on normal cases, <0 if at least one close() has happened on the
1010 * connection (eg: reconnect). It relies on tcpcheck_main().
Christopher Faulet61cc8522020-04-20 14:54:42 +02001011 */
Willy Tarreauf3ae34b2022-05-18 09:11:52 +02001012int wake_srv_chk(struct stconn *cs)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001013{
Christopher Faulet0256da12021-12-15 09:50:17 +01001014 struct connection *conn;
Willy Tarreauea27f482022-05-18 16:10:52 +02001015 struct check *check = __sc_check(cs);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001016 struct email_alertq *q = container_of(check, typeof(*q), check);
1017 int ret = 0;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001018
Christopher Faulet147b8c92021-04-10 09:00:38 +02001019 TRACE_ENTER(CHK_EV_HCHK_WAKE, check);
Christopher Faulet08c8f8e2022-05-18 14:35:49 +02001020 if (check->result != CHK_RES_UNKNOWN)
1021 goto end;
1022
Willy Tarreau51cd5952020-06-05 12:25:38 +02001023 if (check->server)
1024 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
1025 else
1026 HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001027
Willy Tarreau51cd5952020-06-05 12:25:38 +02001028 /* we may have to make progress on the TCP checks */
1029 ret = tcpcheck_main(check);
Christopher Fauletaaab0832020-05-05 15:54:22 +02001030
Willy Tarreau51cd5952020-06-05 12:25:38 +02001031 cs = check->cs;
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001032 conn = sc_conn(cs);
Christopher Fauletaaab0832020-05-05 15:54:22 +02001033
Willy Tarreaub605c422022-05-17 17:04:55 +02001034 if (unlikely(!conn || !cs || conn->flags & CO_FL_ERROR || sc_ep_test(cs, SE_FL_ERROR))) {
Willy Tarreau51cd5952020-06-05 12:25:38 +02001035 /* We may get error reports bypassing the I/O handlers, typically
1036 * the case when sending a pure TCP check which fails, then the I/O
1037 * handlers above are not called. This is completely handled by the
1038 * main processing task so let's simply wake it up. If we get here,
1039 * we expect errno to still be valid.
1040 */
Christopher Faulet147b8c92021-04-10 09:00:38 +02001041 TRACE_ERROR("report connection error", CHK_EV_HCHK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001042 chk_report_conn_err(check, errno, 0);
1043 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001044 }
1045
Christopher Faulet8f100422021-01-18 15:47:03 +01001046 if (check->result != CHK_RES_UNKNOWN || ret == -1) {
Christopher Faulet08c8f8e2022-05-18 14:35:49 +02001047 /* Check complete or aborted. Wake the check task up to be sure
1048 * the result is handled ASAP. */
Willy Tarreau51cd5952020-06-05 12:25:38 +02001049 ret = -1;
Willy Tarreau51cd5952020-06-05 12:25:38 +02001050 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001051 }
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001052
Willy Tarreau51cd5952020-06-05 12:25:38 +02001053 if (check->server)
1054 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
1055 else
1056 HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001057
Christopher Faulet08c8f8e2022-05-18 14:35:49 +02001058 end:
Christopher Faulet147b8c92021-04-10 09:00:38 +02001059 TRACE_LEAVE(CHK_EV_HCHK_WAKE, check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001060 return ret;
1061}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001062
Willy Tarreau51cd5952020-06-05 12:25:38 +02001063/* This function checks if any I/O is wanted, and if so, attempts to do so */
Christopher Faulet361417f2022-05-18 14:50:30 +02001064struct task *srv_chk_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001065{
Willy Tarreau4596fe22022-05-17 19:07:51 +02001066 struct stconn *cs = ctx;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001067
Willy Tarreau51cd5952020-06-05 12:25:38 +02001068 wake_srv_chk(cs);
1069 return NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001070}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001071
Willy Tarreau51cd5952020-06-05 12:25:38 +02001072/* manages a server health-check that uses a connection. Returns
1073 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001074 *
1075 * Please do NOT place any return statement in this function and only leave
Willy Tarreau51cd5952020-06-05 12:25:38 +02001076 * via the out_unlock label.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001077 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001078struct task *process_chk_conn(struct task *t, void *context, unsigned int state)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001079{
Willy Tarreau51cd5952020-06-05 12:25:38 +02001080 struct check *check = context;
1081 struct proxy *proxy = check->proxy;
Willy Tarreau4596fe22022-05-17 19:07:51 +02001082 struct stconn *cs;
Christopher Faulet92017a32021-05-06 16:01:18 +02001083 struct connection *conn;
Willy Tarreau51cd5952020-06-05 12:25:38 +02001084 int rv;
1085 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaudeccd112018-06-14 18:38:55 +02001086
Christopher Faulet147b8c92021-04-10 09:00:38 +02001087 TRACE_ENTER(CHK_EV_TASK_WAKE, check);
1088
Willy Tarreau51cd5952020-06-05 12:25:38 +02001089 if (check->server)
1090 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
Christopher Faulet92017a32021-05-06 16:01:18 +02001091
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001092 if (unlikely(check->state & CHK_ST_PURGE)) {
1093 TRACE_STATE("health-check state to purge", CHK_EV_TASK_WAKE, check);
1094 }
1095 else if (!(check->state & (CHK_ST_INPROGRESS))) {
Willy Tarreau51cd5952020-06-05 12:25:38 +02001096 /* no check currently running */
Christopher Faulet147b8c92021-04-10 09:00:38 +02001097 if (!expired) /* woke up too early */ {
1098 TRACE_STATE("health-check wake up too early", CHK_EV_TASK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001099 goto out_unlock;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001100 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01001101
Willy Tarreau51cd5952020-06-05 12:25:38 +02001102 /* we don't send any health-checks when the proxy is
1103 * stopped, the server should not be checked or the check
1104 * is disabled.
1105 */
1106 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001107 (proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001108 TRACE_STATE("health-check paused or disabled", CHK_EV_TASK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001109 goto reschedule;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001110 }
Christopher Faulet404f9192020-04-09 23:13:54 +02001111
Willy Tarreau51cd5952020-06-05 12:25:38 +02001112 /* we'll initiate a new check */
1113 set_server_check_status(check, HCHK_STATUS_START, NULL);
Christopher Faulet404f9192020-04-09 23:13:54 +02001114
Willy Tarreau51cd5952020-06-05 12:25:38 +02001115 check->state |= CHK_ST_INPROGRESS;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001116 TRACE_STATE("init new health-check", CHK_EV_TASK_WAKE|CHK_EV_HCHK_START, check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001117
Willy Tarreau51cd5952020-06-05 12:25:38 +02001118 task_set_affinity(t, tid_bit);
1119
1120 check->current_step = NULL;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001121
Willy Tarreaucb041662022-05-17 19:44:42 +02001122 check->cs = cs_new_from_check(check, SC_FL_NONE);
Christopher Faulet177a0e62022-04-12 17:47:07 +02001123 if (!check->cs) {
1124 set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL);
1125 goto end;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001126 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001127 tcpcheck_main(check);
Christopher Faulet92017a32021-05-06 16:01:18 +02001128 expired = 0;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001129 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001130
Christopher Faulet92017a32021-05-06 16:01:18 +02001131 /* there was a test running.
1132 * First, let's check whether there was an uncaught error,
1133 * which can happen on connect timeout or error.
1134 */
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001135 if (check->result == CHK_RES_UNKNOWN && likely(!(check->state & CHK_ST_PURGE))) {
Christopher Faulet177a0e62022-04-12 17:47:07 +02001136 cs = check->cs;
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001137 conn = (cs ? sc_conn(cs) : NULL);
Christopher Faulet177a0e62022-04-12 17:47:07 +02001138
Christopher Faulet92017a32021-05-06 16:01:18 +02001139 /* Here the connection must be defined. Otherwise the
1140 * error would have already been detected
Willy Tarreau51cd5952020-06-05 12:25:38 +02001141 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001142 if ((conn && ((conn->flags & CO_FL_ERROR) || sc_ep_test(cs, SE_FL_ERROR))) || expired) {
Christopher Faulet92017a32021-05-06 16:01:18 +02001143 TRACE_ERROR("report connection error", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1144 chk_report_conn_err(check, 0, expired);
1145 }
1146 else {
1147 if (check->state & CHK_ST_CLOSE_CONN) {
1148 TRACE_DEVEL("closing current connection", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
Christopher Faulet92017a32021-05-06 16:01:18 +02001149 check->state &= ~CHK_ST_CLOSE_CONN;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001150 conn = NULL;
Christopher Fauleta6c4a482022-04-28 18:25:24 +02001151 if (!cs_reset_endp(check->cs)) {
1152 /* error will be handled by tcpcheck_main().
Willy Tarreaub605c422022-05-17 17:04:55 +02001153 * On success, remove all flags except SE_FL_DETACHED
Christopher Fauleta6c4a482022-04-28 18:25:24 +02001154 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001155 sc_ep_clr(check->cs, ~SE_FL_DETACHED);
Christopher Fauleta6c4a482022-04-28 18:25:24 +02001156 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001157 tcpcheck_main(check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001158 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001159 if (check->result == CHK_RES_UNKNOWN) {
1160 TRACE_DEVEL("health-check not expired", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
1161 goto out_unlock; /* timeout not reached, wait again */
Christopher Faulet8f100422021-01-18 15:47:03 +01001162 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001163 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001164 }
Christopher Faulet404f9192020-04-09 23:13:54 +02001165
Christopher Faulet92017a32021-05-06 16:01:18 +02001166 /* check complete or aborted */
1167 TRACE_STATE("health-check complete or aborted", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END, check);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001168
Christopher Faulet92017a32021-05-06 16:01:18 +02001169 check->current_step = NULL;
Christopher Faulet177a0e62022-04-12 17:47:07 +02001170 cs = check->cs;
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001171 conn = (cs ? sc_conn(cs) : NULL);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001172
Christopher Faulet92017a32021-05-06 16:01:18 +02001173 if (conn && conn->xprt) {
1174 /* The check was aborted and the connection was not yet closed.
1175 * This can happen upon timeout, or when an external event such
1176 * as a failed response coupled with "observe layer7" caused the
1177 * server state to be suddenly changed.
1178 */
Christopher Fauletff022a22022-04-21 08:38:54 +02001179 cs_conn_drain_and_shut(cs);
Christopher Faulet92017a32021-05-06 16:01:18 +02001180 }
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001181
Christopher Faulet177a0e62022-04-12 17:47:07 +02001182 if (cs) {
Christopher Fauleteb50c012022-04-21 14:22:53 +02001183 cs_destroy(cs);
Christopher Faulet177a0e62022-04-12 17:47:07 +02001184 cs = check->cs = NULL;
1185 conn = NULL;
1186 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001187
Christopher Faulet92017a32021-05-06 16:01:18 +02001188 if (check->sess != NULL) {
1189 vars_prune(&check->vars, check->sess, NULL);
1190 session_free(check->sess);
1191 check->sess = NULL;
1192 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001193
Christopher Fauletb041b232022-03-24 10:27:02 +01001194 end:
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001195 if (check->server && likely(!(check->state & CHK_ST_PURGE))) {
Christopher Faulet92017a32021-05-06 16:01:18 +02001196 if (check->result == CHK_RES_FAILED) {
1197 /* a failure or timeout detected */
1198 TRACE_DEVEL("report failure", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1199 check_notify_failure(check);
1200 }
1201 else if (check->result == CHK_RES_CONDPASS) {
1202 /* check is OK but asks for stopping mode */
1203 TRACE_DEVEL("report conditional success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
1204 check_notify_stopping(check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001205 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001206 else if (check->result == CHK_RES_PASSED) {
1207 /* a success was detected */
1208 TRACE_DEVEL("report success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
1209 check_notify_success(check);
1210 }
1211 }
Christopher Faulet6d781f62022-05-18 14:24:43 +02001212
1213 if (LIST_INLIST(&check->buf_wait.list))
1214 LIST_DEL_INIT(&check->buf_wait.list);
1215
Christopher Faulet92017a32021-05-06 16:01:18 +02001216 task_set_affinity(t, MAX_THREADS_MASK);
1217 check_release_buf(check, &check->bi);
1218 check_release_buf(check, &check->bo);
1219 check->state &= ~(CHK_ST_INPROGRESS|CHK_ST_IN_ALLOC|CHK_ST_OUT_ALLOC);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001220
Christopher Faulet92017a32021-05-06 16:01:18 +02001221 if (check->server) {
1222 rv = 0;
1223 if (global.spread_checks > 0) {
1224 rv = srv_getinter(check) * global.spread_checks / 100;
1225 rv -= (int) (2 * rv * (ha_random32() / 4294967295.0));
Christopher Faulet61cc8522020-04-20 14:54:42 +02001226 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001227 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Christopher Faulet61cc8522020-04-20 14:54:42 +02001228 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001229
1230 reschedule:
1231 while (tick_is_expired(t->expire, now_ms))
1232 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1233 out_unlock:
1234 if (check->server)
1235 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
Christopher Faulet147b8c92021-04-10 09:00:38 +02001236
1237 TRACE_LEAVE(CHK_EV_TASK_WAKE, check);
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001238
1239 /* Free the check if set to PURGE. After this, the check instance may be
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001240 * freed via the srv_drop invocation, so it must not be accessed after
1241 * this point.
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001242 */
1243 if (unlikely(check->state & CHK_ST_PURGE)) {
Amaury Denoyelle26cb8342021-08-10 16:23:49 +02001244 free_check(check);
Amaury Denoyelle9ba34ae2021-08-09 15:09:17 +02001245 if (check->server)
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001246 srv_drop(check->server);
Amaury Denoyelle26cb8342021-08-10 16:23:49 +02001247
1248 t = NULL;
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001249 }
1250
Willy Tarreau51cd5952020-06-05 12:25:38 +02001251 return t;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001252}
1253
Willy Tarreau51cd5952020-06-05 12:25:38 +02001254
Christopher Faulet61cc8522020-04-20 14:54:42 +02001255/**************************************************************************/
1256/************************** Init/deinit checks ****************************/
1257/**************************************************************************/
Christopher Fauletb381a502020-11-25 13:47:00 +01001258/*
1259 * Tries to grab a buffer and to re-enables processing on check <target>. The
1260 * check flags are used to figure what buffer was requested. It returns 1 if the
1261 * allocation succeeds, in which case the I/O tasklet is woken up, or 0 if it's
1262 * impossible to wake up and we prefer to be woken up later.
1263 */
1264int check_buf_available(void *target)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001265{
Christopher Fauletb381a502020-11-25 13:47:00 +01001266 struct check *check = target;
1267
Christopher Fauletc95eaef2022-05-18 15:57:15 +02001268 BUG_ON(!check->cs);
1269
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001270 if ((check->state & CHK_ST_IN_ALLOC) && b_alloc(&check->bi)) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001271 TRACE_STATE("unblocking check, input buffer allocated", CHK_EV_TCPCHK_EXP|CHK_EV_RX_BLK, check);
Christopher Fauletb381a502020-11-25 13:47:00 +01001272 check->state &= ~CHK_ST_IN_ALLOC;
Christopher Fauletc95eaef2022-05-18 15:57:15 +02001273 tasklet_wakeup(check->cs->wait_event.tasklet);
Christopher Fauletb381a502020-11-25 13:47:00 +01001274 return 1;
1275 }
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001276 if ((check->state & CHK_ST_OUT_ALLOC) && b_alloc(&check->bo)) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001277 TRACE_STATE("unblocking check, output buffer allocated", CHK_EV_TCPCHK_SND|CHK_EV_TX_BLK, check);
Christopher Fauletb381a502020-11-25 13:47:00 +01001278 check->state &= ~CHK_ST_OUT_ALLOC;
Christopher Fauletc95eaef2022-05-18 15:57:15 +02001279 tasklet_wakeup(check->cs->wait_event.tasklet);
Christopher Fauletb381a502020-11-25 13:47:00 +01001280 return 1;
1281 }
1282
1283 return 0;
1284}
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001285
Christopher Fauletb381a502020-11-25 13:47:00 +01001286/*
William Dauchyf4300902021-02-06 20:47:50 +01001287 * Allocate a buffer. If it fails, it adds the check in buffer wait queue.
Christopher Fauletb381a502020-11-25 13:47:00 +01001288 */
1289struct buffer *check_get_buf(struct check *check, struct buffer *bptr)
1290{
1291 struct buffer *buf = NULL;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001292
Willy Tarreau2b718102021-04-21 07:32:39 +02001293 if (likely(!LIST_INLIST(&check->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001294 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Fauletb381a502020-11-25 13:47:00 +01001295 check->buf_wait.target = check;
1296 check->buf_wait.wakeup_cb = check_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +02001297 LIST_APPEND(&th_ctx->buffer_wq, &check->buf_wait.list);
Christopher Fauletb381a502020-11-25 13:47:00 +01001298 }
1299 return buf;
1300}
1301
1302/*
1303 * Release a buffer, if any, and try to wake up entities waiting in the buffer
1304 * wait queue.
1305 */
1306void check_release_buf(struct check *check, struct buffer *bptr)
1307{
1308 if (bptr->size) {
1309 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01001310 offer_buffers(check->buf_wait.target, 1);
Christopher Fauletb381a502020-11-25 13:47:00 +01001311 }
1312}
1313
1314const char *init_check(struct check *check, int type)
1315{
1316 check->type = type;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001317
Christopher Fauletb381a502020-11-25 13:47:00 +01001318 check->bi = BUF_NULL;
1319 check->bo = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01001320 LIST_INIT(&check->buf_wait.list);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001321 return NULL;
1322}
1323
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001324/* Liberates the resources allocated for a check.
1325 *
Amaury Denoyelle6d7fc442021-08-10 16:22:51 +02001326 * This function must only be run by the thread owning the check.
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001327 */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001328void free_check(struct check *check)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001329{
Amaury Denoyelle6d7fc442021-08-10 16:22:51 +02001330 /* For agent-check, free the rules / vars from the server. This is not
1331 * done for health-check : the proxy is the owner of the rules / vars
1332 * in this case.
1333 */
1334 if (check->state & CHK_ST_AGENT) {
1335 free_tcpcheck_vars(&check->tcpcheck_rules->preset_vars);
1336 ha_free(&check->tcpcheck_rules);
1337 }
1338
Christopher Faulet61cc8522020-04-20 14:54:42 +02001339 task_destroy(check->task);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001340
Christopher Fauletb381a502020-11-25 13:47:00 +01001341 check_release_buf(check, &check->bi);
1342 check_release_buf(check, &check->bo);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001343 if (check->cs) {
Christopher Fauleteb50c012022-04-21 14:22:53 +02001344 cs_destroy(check->cs);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001345 check->cs = NULL;
1346 }
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001347}
1348
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001349/* This function must be used in order to free a started check. The check will
1350 * be scheduled for a next execution in order to properly close and free all
1351 * check elements.
1352 *
1353 * Non thread-safe.
1354 */
1355void check_purge(struct check *check)
1356{
Amaury Denoyelle25fe1032021-08-10 16:21:55 +02001357 check->state |= CHK_ST_PURGE;
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001358 task_wakeup(check->task, TASK_WOKEN_OTHER);
1359}
1360
Christopher Faulet61cc8522020-04-20 14:54:42 +02001361/* manages a server health-check. Returns the time the task accepts to wait, or
1362 * TIME_ETERNITY for infinity.
1363 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001364struct task *process_chk(struct task *t, void *context, unsigned int state)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001365{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001366 struct check *check = context;
1367
1368 if (check->type == PR_O2_EXT_CHK)
1369 return process_chk_proc(t, context, state);
1370 return process_chk_conn(t, context, state);
1371
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001372}
1373
Christopher Faulet61cc8522020-04-20 14:54:42 +02001374
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001375int start_check_task(struct check *check, int mininter,
Christopher Faulet61cc8522020-04-20 14:54:42 +02001376 int nbcheck, int srvpos)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001377{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001378 struct task *t;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001379
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001380 /* task for the check. Process-based checks exclusively run on thread 1. */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001381 if (check->type == PR_O2_EXT_CHK)
Willy Tarreaua89c1912021-10-20 18:43:30 +02001382 t = task_new_on(0);
Christopher Faulet177a0e62022-04-12 17:47:07 +02001383 else
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001384 t = task_new_anywhere();
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001385
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001386 if (!t)
1387 goto fail_alloc_task;
1388
Christopher Faulet61cc8522020-04-20 14:54:42 +02001389 check->task = t;
1390 t->process = process_chk;
1391 t->context = check;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001392
Christopher Faulet61cc8522020-04-20 14:54:42 +02001393 if (mininter < srv_getinter(check))
1394 mininter = srv_getinter(check);
1395
1396 if (global.max_spread_checks && mininter > global.max_spread_checks)
1397 mininter = global.max_spread_checks;
1398
1399 /* check this every ms */
1400 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
1401 check->start = now;
1402 task_queue(t);
1403
1404 return 1;
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001405
1406 fail_alloc_task:
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001407 ha_alert("Starting [%s:%s] check: out of memory.\n",
1408 check->server->proxy->id, check->server->id);
1409 return 0;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001410}
1411
Christopher Faulet61cc8522020-04-20 14:54:42 +02001412/*
1413 * Start health-check.
1414 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
1415 */
1416static int start_checks()
1417{
1418
1419 struct proxy *px;
1420 struct server *s;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001421 int nbcheck=0, mininter=0, srvpos=0;
1422
1423 /* 0- init the dummy frontend used to create all checks sessions */
1424 init_new_proxy(&checks_fe);
Christopher Faulet0f1fc232021-04-16 10:49:07 +02001425 checks_fe.id = strdup("CHECKS-FE");
Christopher Faulet61cc8522020-04-20 14:54:42 +02001426 checks_fe.cap = PR_CAP_FE | PR_CAP_BE;
1427 checks_fe.mode = PR_MODE_TCP;
1428 checks_fe.maxconn = 0;
1429 checks_fe.conn_retries = CONN_RETRIES;
1430 checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
1431 checks_fe.timeout.client = TICK_ETERNITY;
1432
1433 /* 1- count the checkers to run simultaneously.
1434 * We also determine the minimum interval among all of those which
1435 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1436 * will be used to spread their start-up date. Those which have
1437 * a shorter interval will start independently and will not dictate
1438 * too short an interval for all others.
1439 */
1440 for (px = proxies_list; px; px = px->next) {
1441 for (s = px->srv; s; s = s->next) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001442 if (s->check.state & CHK_ST_CONFIGURED) {
1443 nbcheck++;
1444 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1445 (!mininter || mininter > srv_getinter(&s->check)))
1446 mininter = srv_getinter(&s->check);
Christopher Faulet5c288742020-03-31 08:15:58 +02001447 }
1448
Christopher Faulet61cc8522020-04-20 14:54:42 +02001449 if (s->agent.state & CHK_ST_CONFIGURED) {
1450 nbcheck++;
1451 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
1452 (!mininter || mininter > srv_getinter(&s->agent)))
1453 mininter = srv_getinter(&s->agent);
1454 }
Christopher Faulet5c288742020-03-31 08:15:58 +02001455 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001456 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001457
Christopher Faulet61cc8522020-04-20 14:54:42 +02001458 if (!nbcheck)
Christopher Fauletfc633b62020-11-06 15:24:23 +01001459 return ERR_NONE;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001460
Christopher Faulet61cc8522020-04-20 14:54:42 +02001461 srand((unsigned)time(NULL));
Christopher Fauletb7d30092020-03-30 15:19:03 +02001462
William Dauchyf4300902021-02-06 20:47:50 +01001463 /* 2- start them as far as possible from each other. For this, we will
1464 * start them after their interval is set to the min interval divided
1465 * by the number of servers, weighted by the server's position in the
1466 * list.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001467 */
1468 for (px = proxies_list; px; px = px->next) {
1469 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
1470 if (init_pid_list()) {
1471 ha_alert("Starting [%s] check: out of memory.\n", px->id);
1472 return ERR_ALERT | ERR_FATAL;
1473 }
1474 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001475
Christopher Faulet61cc8522020-04-20 14:54:42 +02001476 for (s = px->srv; s; s = s->next) {
1477 /* A task for the main check */
1478 if (s->check.state & CHK_ST_CONFIGURED) {
1479 if (s->check.type == PR_O2_EXT_CHK) {
1480 if (!prepare_external_check(&s->check))
1481 return ERR_ALERT | ERR_FATAL;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001482 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001483 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1484 return ERR_ALERT | ERR_FATAL;
1485 srvpos++;
Christopher Faulet98572322020-03-30 13:16:44 +02001486 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001487
Christopher Faulet61cc8522020-04-20 14:54:42 +02001488 /* A task for a auxiliary agent check */
1489 if (s->agent.state & CHK_ST_CONFIGURED) {
1490 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1491 return ERR_ALERT | ERR_FATAL;
1492 }
1493 srvpos++;
1494 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001495 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001496 }
Christopher Fauletfc633b62020-11-06 15:24:23 +01001497 return ERR_NONE;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001498}
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001499
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001500
Christopher Faulet61cc8522020-04-20 14:54:42 +02001501/*
1502 * Return value:
1503 * the port to be used for the health check
1504 * 0 in case no port could be found for the check
1505 */
1506static int srv_check_healthcheck_port(struct check *chk)
1507{
1508 int i = 0;
1509 struct server *srv = NULL;
1510
1511 srv = chk->server;
1512
William Dauchyf4300902021-02-06 20:47:50 +01001513 /* by default, we use the health check port configured */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001514 if (chk->port > 0)
1515 return chk->port;
1516
1517 /* try to get the port from check_core.addr if check.port not set */
1518 i = get_host_port(&chk->addr);
1519 if (i > 0)
1520 return i;
1521
1522 /* try to get the port from server address */
1523 /* prevent MAPPORTS from working at this point, since checks could
1524 * not be performed in such case (MAPPORTS impose a relative ports
1525 * based on live traffic)
1526 */
1527 if (srv->flags & SRV_F_MAPPORTS)
1528 return 0;
1529
1530 i = srv->svc_port; /* by default */
1531 if (i > 0)
1532 return i;
1533
1534 return 0;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001535}
1536
Christopher Faulet61cc8522020-04-20 14:54:42 +02001537/* Initializes an health-check attached to the server <srv>. Non-zero is returned
1538 * if an error occurred.
1539 */
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001540int init_srv_check(struct server *srv)
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001541{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001542 const char *err;
1543 struct tcpcheck_rule *r;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001544 int ret = ERR_NONE;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001545 int check_type;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001546
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001547 if (!srv->do_check || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001548 goto out;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001549
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001550 check_type = srv->check.tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001551
Amaury Denoyelle7d098be2022-03-09 14:20:10 +01001552 if (!(srv->flags & SRV_F_DYNAMIC)) {
1553 /* If neither a port nor an addr was specified and no check
1554 * transport layer is forced, then the transport layer used by
1555 * the checks is the same as for the production traffic.
1556 * Otherwise we use raw_sock by default, unless one is
1557 * specified.
1558 */
1559 if (!srv->check.port && !is_addr(&srv->check.addr)) {
1560 if (!srv->check.use_ssl && srv->use_ssl != -1) {
1561 srv->check.use_ssl = srv->use_ssl;
1562 srv->check.xprt = srv->xprt;
1563 }
1564 else if (srv->check.use_ssl == 1)
1565 srv->check.xprt = xprt_get(XPRT_SSL);
1566 srv->check.send_proxy |= (srv->pp_opts);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001567 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001568 else if (srv->check.use_ssl == 1)
1569 srv->check.xprt = xprt_get(XPRT_SSL);
Amaury Denoyelle7d098be2022-03-09 14:20:10 +01001570 }
1571 else {
1572 /* For dynamic servers, check-ssl and check-send-proxy must be
1573 * explicitely defined even if the check port was not
1574 * overridden.
1575 */
1576 if (srv->check.use_ssl == 1)
1577 srv->check.xprt = xprt_get(XPRT_SSL);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001578 }
1579
Christopher Faulet12882cf2020-04-23 15:50:18 +02001580 /* Inherit the mux protocol from the server if not already defined for
1581 * the check
1582 */
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001583 if (srv->mux_proto && !srv->check.mux_proto &&
1584 ((srv->mux_proto->mode == PROTO_MODE_HTTP && check_type == TCPCHK_RULES_HTTP_CHK) ||
1585 (srv->mux_proto->mode == PROTO_MODE_TCP && check_type != TCPCHK_RULES_HTTP_CHK))) {
Christopher Faulet12882cf2020-04-23 15:50:18 +02001586 srv->check.mux_proto = srv->mux_proto;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001587 }
Amaury Denoyelle7c148902020-11-13 12:34:57 +01001588 /* test that check proto is valid if explicitly defined */
1589 else if (srv->check.mux_proto &&
1590 ((srv->check.mux_proto->mode == PROTO_MODE_HTTP && check_type != TCPCHK_RULES_HTTP_CHK) ||
1591 (srv->check.mux_proto->mode == PROTO_MODE_TCP && check_type == TCPCHK_RULES_HTTP_CHK))) {
1592 ha_alert("config: %s '%s': server '%s' uses an incompatible MUX protocol for the selected check type\n",
1593 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1594 ret |= ERR_ALERT | ERR_FATAL;
1595 goto out;
1596 }
Christopher Faulet12882cf2020-04-23 15:50:18 +02001597
Christopher Faulet61cc8522020-04-20 14:54:42 +02001598 /* validate <srv> server health-check settings */
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001599
Christopher Faulet61cc8522020-04-20 14:54:42 +02001600 /* We need at least a service port, a check port or the first tcp-check
1601 * rule must be a 'connect' one when checking an IPv4/IPv6 server.
1602 */
1603 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1604 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1605 goto init;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001606
Christopher Faulet61cc8522020-04-20 14:54:42 +02001607 if (!srv->proxy->tcpcheck_rules.list || LIST_ISEMPTY(srv->proxy->tcpcheck_rules.list)) {
1608 ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n",
1609 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1610 ret |= ERR_ALERT | ERR_ABORT;
1611 goto out;
1612 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001613
Christopher Faulet61cc8522020-04-20 14:54:42 +02001614 /* search the first action (connect / send / expect) in the list */
1615 r = get_first_tcpcheck_rule(&srv->proxy->tcpcheck_rules);
1616 if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) {
1617 ha_alert("config: %s '%s': server '%s' has neither service port nor check port "
1618 "nor tcp_check rule 'connect' with port information.\n",
1619 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1620 ret |= ERR_ALERT | ERR_ABORT;
1621 goto out;
1622 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001623
Christopher Faulet61cc8522020-04-20 14:54:42 +02001624 /* scan the tcp-check ruleset to ensure a port has been configured */
1625 list_for_each_entry(r, srv->proxy->tcpcheck_rules.list, list) {
Willy Tarreauacff3092021-07-22 11:06:41 +02001626 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port && !get_host_port(&r->connect.addr))) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001627 ha_alert("config: %s '%s': server '%s' has neither service port nor check port, "
1628 "and a tcp_check rule 'connect' with no port information.\n",
1629 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1630 ret |= ERR_ALERT | ERR_ABORT;
1631 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001632 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001633 }
1634
Christopher Faulet61cc8522020-04-20 14:54:42 +02001635 init:
Christopher Faulet61cc8522020-04-20 14:54:42 +02001636 err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY);
1637 if (err) {
1638 ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n",
1639 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1640 ret |= ERR_ALERT | ERR_ABORT;
1641 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001642 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001643 srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001644 srv_take(srv);
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001645
1646 /* Only increment maxsock for servers from the configuration. Dynamic
1647 * servers at the moment are not taken into account for the estimation
1648 * of the resources limits.
1649 */
1650 if (global.mode & MODE_STARTING)
1651 global.maxsock++;
Christopher Faulete5870d82020-04-15 11:32:03 +02001652
Christopher Faulet61cc8522020-04-20 14:54:42 +02001653 out:
1654 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001655}
1656
Christopher Faulet61cc8522020-04-20 14:54:42 +02001657/* Initializes an agent-check attached to the server <srv>. Non-zero is returned
1658 * if an error occurred.
1659 */
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001660int init_srv_agent_check(struct server *srv)
Christopher Faulete5870d82020-04-15 11:32:03 +02001661{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001662 struct tcpcheck_rule *chk;
1663 const char *err;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001664 int ret = ERR_NONE;
Christopher Faulete5870d82020-04-15 11:32:03 +02001665
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001666 if (!srv->do_agent || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001667 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001668
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001669 /* If there is no connect rule preceding all send / expect rules, an
Christopher Faulet61cc8522020-04-20 14:54:42 +02001670 * implicit one is inserted before all others.
1671 */
1672 chk = get_first_tcpcheck_rule(srv->agent.tcpcheck_rules);
1673 if (!chk || chk->action != TCPCHK_ACT_CONNECT) {
1674 chk = calloc(1, sizeof(*chk));
1675 if (!chk) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02001676 ha_alert("%s '%s': unable to add implicit tcp-check connect rule"
Christopher Faulet61cc8522020-04-20 14:54:42 +02001677 " to agent-check for server '%s' (out of memory).\n",
1678 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1679 ret |= ERR_ALERT | ERR_FATAL;
1680 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001681 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001682 chk->action = TCPCHK_ACT_CONNECT;
1683 chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT);
Willy Tarreau2b718102021-04-21 07:32:39 +02001684 LIST_INSERT(srv->agent.tcpcheck_rules->list, &chk->list);
Christopher Faulete5870d82020-04-15 11:32:03 +02001685 }
1686
Christopher Faulete5870d82020-04-15 11:32:03 +02001687
Christopher Faulet61cc8522020-04-20 14:54:42 +02001688 err = init_check(&srv->agent, PR_O2_TCPCHK_CHK);
1689 if (err) {
1690 ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n",
1691 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1692 ret |= ERR_ALERT | ERR_ABORT;
1693 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001694 }
1695
Christopher Faulet61cc8522020-04-20 14:54:42 +02001696 if (!srv->agent.inter)
1697 srv->agent.inter = srv->check.inter;
1698
1699 srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001700 srv_take(srv);
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001701
1702 /* Only increment maxsock for servers from the configuration. Dynamic
1703 * servers at the moment are not taken into account for the estimation
1704 * of the resources limits.
1705 */
1706 if (global.mode & MODE_STARTING)
1707 global.maxsock++;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001708
1709 out:
1710 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001711}
1712
Christopher Faulet61cc8522020-04-20 14:54:42 +02001713static void deinit_srv_check(struct server *srv)
1714{
1715 if (srv->check.state & CHK_ST_CONFIGURED)
1716 free_check(&srv->check);
1717 srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
1718 srv->do_check = 0;
1719}
Christopher Faulete5870d82020-04-15 11:32:03 +02001720
Christopher Faulet61cc8522020-04-20 14:54:42 +02001721
1722static void deinit_srv_agent_check(struct server *srv)
1723{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001724 if (srv->agent.state & CHK_ST_CONFIGURED)
1725 free_check(&srv->agent);
1726
1727 srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
1728 srv->do_agent = 0;
Christopher Faulete5870d82020-04-15 11:32:03 +02001729}
1730
Willy Tarreaucee013e2020-06-05 11:40:38 +02001731REGISTER_POST_SERVER_CHECK(init_srv_check);
1732REGISTER_POST_SERVER_CHECK(init_srv_agent_check);
Willy Tarreaucee013e2020-06-05 11:40:38 +02001733REGISTER_POST_CHECK(start_checks);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001734
Willy Tarreaucee013e2020-06-05 11:40:38 +02001735REGISTER_SERVER_DEINIT(deinit_srv_check);
1736REGISTER_SERVER_DEINIT(deinit_srv_agent_check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001737
Christopher Faulet61cc8522020-04-20 14:54:42 +02001738
1739/**************************************************************************/
1740/************************** Check sample fetches **************************/
1741/**************************************************************************/
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001742
Christopher Faulet61cc8522020-04-20 14:54:42 +02001743static struct sample_fetch_kw_list smp_kws = {ILH, {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001744 { /* END */ },
1745}};
1746
1747INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
1748
1749
1750/**************************************************************************/
1751/************************ Check's parsing functions ***********************/
1752/**************************************************************************/
Christopher Fauletce8111e2020-04-06 15:04:11 +02001753/* Parse the "addr" server keyword */
1754static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1755 char **errmsg)
1756{
1757 struct sockaddr_storage *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001758 int port1, port2, err_code = 0;
1759
1760
1761 if (!*args[*cur_arg+1]) {
1762 memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]);
1763 goto error;
1764 }
1765
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001766 sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, NULL, NULL, errmsg, NULL, NULL,
1767 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Christopher Fauletce8111e2020-04-06 15:04:11 +02001768 if (!sk) {
1769 memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg);
1770 goto error;
1771 }
1772
William Dauchy1c921cd2021-02-03 22:30:08 +01001773 srv->check.addr = *sk;
1774 /* if agentaddr was never set, we can use addr */
1775 if (!(srv->flags & SRV_F_AGENTADDR))
1776 srv->agent.addr = *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001777
1778 out:
1779 return err_code;
1780
1781 error:
1782 err_code |= ERR_ALERT | ERR_FATAL;
1783 goto out;
1784}
1785
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001786/* Parse the "agent-addr" server keyword */
1787static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1788 char **errmsg)
1789{
William Dauchy1c921cd2021-02-03 22:30:08 +01001790 struct sockaddr_storage sk;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001791 int err_code = 0;
1792
1793 if (!*(args[*cur_arg+1])) {
1794 memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]);
1795 goto error;
1796 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001797 memset(&sk, 0, sizeof(sk));
1798 if (str2ip(args[*cur_arg + 1], &sk) == NULL) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001799 memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]);
1800 goto error;
1801 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001802 set_srv_agent_addr(srv, &sk);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001803
1804 out:
1805 return err_code;
1806
1807 error:
1808 err_code |= ERR_ALERT | ERR_FATAL;
1809 goto out;
1810}
1811
1812/* Parse the "agent-check" server keyword */
1813static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1814 char **errmsg)
1815{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001816 struct tcpcheck_ruleset *rs = NULL;
1817 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1818 struct tcpcheck_rule *chk;
1819 int err_code = 0;
1820
1821 if (srv->do_agent)
1822 goto out;
1823
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001824 if (!(curpx->cap & PR_CAP_BE)) {
1825 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
1826 args[*cur_arg], proxy_type_str(curpx), curpx->id);
1827 return ERR_WARN;
1828 }
1829
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001830 if (!rules) {
1831 rules = calloc(1, sizeof(*rules));
1832 if (!rules) {
1833 memprintf(errmsg, "out of memory.");
1834 goto error;
1835 }
1836 LIST_INIT(&rules->preset_vars);
1837 srv->agent.tcpcheck_rules = rules;
1838 }
1839 rules->list = NULL;
1840 rules->flags = 0;
1841
Christopher Faulet61cc8522020-04-20 14:54:42 +02001842 rs = find_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001843 if (rs)
1844 goto ruleset_found;
1845
Christopher Faulet61cc8522020-04-20 14:54:42 +02001846 rs = create_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001847 if (rs == NULL) {
1848 memprintf(errmsg, "out of memory.");
1849 goto error;
1850 }
1851
Christopher Fauletb50b3e62020-05-05 18:43:43 +02001852 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", "%[var(check.agent_string)]", ""},
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001853 1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg);
1854 if (!chk) {
1855 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1856 goto error;
1857 }
1858 chk->index = 0;
Willy Tarreau2b718102021-04-21 07:32:39 +02001859 LIST_APPEND(&rs->rules, &chk->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001860
1861 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02001862 1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK,
1863 srv->conf.file, srv->conf.line, errmsg);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001864 if (!chk) {
1865 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1866 goto error;
1867 }
1868 chk->expect.custom = tcpcheck_agent_expect_reply;
1869 chk->index = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02001870 LIST_APPEND(&rs->rules, &chk->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001871
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001872 ruleset_found:
1873 rules->list = &rs->rules;
Christopher Faulet1faf18a2020-11-25 16:43:12 +01001874 rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS);
Christopher Faulet404f9192020-04-09 23:13:54 +02001875 rules->flags |= TCPCHK_RULES_AGENT_CHK;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001876 srv->do_agent = 1;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001877
1878 out:
Dirkjan Bussinkdfee2172021-06-18 19:57:49 +00001879 return err_code;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001880
1881 error:
1882 deinit_srv_agent_check(srv);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001883 free_tcpcheck_ruleset(rs);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001884 err_code |= ERR_ALERT | ERR_FATAL;
1885 goto out;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001886}
1887
1888/* Parse the "agent-inter" server keyword */
1889static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1890 char **errmsg)
1891{
1892 const char *err = NULL;
1893 unsigned int delay;
1894 int err_code = 0;
1895
1896 if (!*(args[*cur_arg+1])) {
1897 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
1898 goto error;
1899 }
1900
1901 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
1902 if (err == PARSE_TIME_OVER) {
1903 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1904 args[*cur_arg+1], args[*cur_arg], srv->id);
1905 goto error;
1906 }
1907 else if (err == PARSE_TIME_UNDER) {
1908 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1909 args[*cur_arg+1], args[*cur_arg], srv->id);
1910 goto error;
1911 }
1912 else if (err) {
1913 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
1914 *err, srv->id);
1915 goto error;
1916 }
1917 if (delay <= 0) {
1918 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
1919 delay, args[*cur_arg], srv->id);
1920 goto error;
1921 }
1922 srv->agent.inter = delay;
1923
1924 out:
1925 return err_code;
1926
1927 error:
1928 err_code |= ERR_ALERT | ERR_FATAL;
1929 goto out;
1930}
1931
1932/* Parse the "agent-port" server keyword */
1933static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1934 char **errmsg)
1935{
1936 int err_code = 0;
1937
1938 if (!*(args[*cur_arg+1])) {
1939 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
1940 goto error;
1941 }
1942
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001943 /* Only increment maxsock for servers from the configuration. Dynamic
1944 * servers at the moment are not taken into account for the estimation
1945 * of the resources limits.
1946 */
1947 if (global.mode & MODE_STARTING)
1948 global.maxsock++;
1949
William Dauchy4858fb22021-02-03 22:30:09 +01001950 set_srv_agent_port(srv, atol(args[*cur_arg + 1]));
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001951
1952 out:
1953 return err_code;
1954
1955 error:
1956 err_code |= ERR_ALERT | ERR_FATAL;
1957 goto out;
1958}
1959
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001960int set_srv_agent_send(struct server *srv, const char *send)
1961{
1962 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1963 struct tcpcheck_var *var = NULL;
1964 char *str;
1965
1966 str = strdup(send);
Christopher Fauletb61caf42020-04-21 10:57:42 +02001967 var = create_tcpcheck_var(ist("check.agent_string"));
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001968 if (str == NULL || var == NULL)
1969 goto error;
1970
1971 free_tcpcheck_vars(&rules->preset_vars);
1972
1973 var->data.type = SMP_T_STR;
1974 var->data.u.str.area = str;
1975 var->data.u.str.data = strlen(str);
1976 LIST_INIT(&var->list);
Willy Tarreau2b718102021-04-21 07:32:39 +02001977 LIST_APPEND(&rules->preset_vars, &var->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001978
1979 return 1;
1980
1981 error:
1982 free(str);
1983 free(var);
1984 return 0;
1985}
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001986
1987/* Parse the "agent-send" server keyword */
1988static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1989 char **errmsg)
1990{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001991 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001992 int err_code = 0;
1993
1994 if (!*(args[*cur_arg+1])) {
1995 memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]);
1996 goto error;
1997 }
1998
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001999 if (!rules) {
2000 rules = calloc(1, sizeof(*rules));
2001 if (!rules) {
2002 memprintf(errmsg, "out of memory.");
2003 goto error;
2004 }
2005 LIST_INIT(&rules->preset_vars);
2006 srv->agent.tcpcheck_rules = rules;
2007 }
2008
2009 if (!set_srv_agent_send(srv, args[*cur_arg+1])) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002010 memprintf(errmsg, "out of memory.");
2011 goto error;
2012 }
2013
2014 out:
2015 return err_code;
2016
2017 error:
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002018 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002019 err_code |= ERR_ALERT | ERR_FATAL;
2020 goto out;
2021}
2022
2023/* Parse the "no-agent-send" server keyword */
2024static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2025 char **errmsg)
2026{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002027 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002028 return 0;
2029}
2030
Christopher Fauletce8111e2020-04-06 15:04:11 +02002031/* Parse the "check" server keyword */
2032static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2033 char **errmsg)
2034{
Christopher Faulet6ecd5932021-01-12 17:29:45 +01002035 if (!(curpx->cap & PR_CAP_BE)) {
2036 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
2037 args[*cur_arg], proxy_type_str(curpx), curpx->id);
2038 return ERR_WARN;
2039 }
2040
Christopher Fauletce8111e2020-04-06 15:04:11 +02002041 srv->do_check = 1;
2042 return 0;
2043}
2044
2045/* Parse the "check-send-proxy" server keyword */
2046static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2047 char **errmsg)
2048{
2049 srv->check.send_proxy = 1;
2050 return 0;
2051}
2052
2053/* Parse the "check-via-socks4" server keyword */
2054static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2055 char **errmsg)
2056{
2057 srv->check.via_socks4 = 1;
2058 return 0;
2059}
2060
2061/* Parse the "no-check" server keyword */
2062static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2063 char **errmsg)
2064{
2065 deinit_srv_check(srv);
2066 return 0;
2067}
2068
2069/* Parse the "no-check-send-proxy" server keyword */
2070static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2071 char **errmsg)
2072{
2073 srv->check.send_proxy = 0;
2074 return 0;
2075}
2076
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002077/* parse the "check-proto" server keyword */
2078static int srv_parse_check_proto(char **args, int *cur_arg,
2079 struct proxy *px, struct server *newsrv, char **err)
2080{
2081 int err_code = 0;
2082
2083 if (!*args[*cur_arg + 1]) {
2084 memprintf(err, "'%s' : missing value", args[*cur_arg]);
2085 goto error;
2086 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002087 newsrv->check.mux_proto = get_mux_proto(ist(args[*cur_arg + 1]));
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002088 if (!newsrv->check.mux_proto) {
2089 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
2090 goto error;
2091 }
2092
2093 out:
2094 return err_code;
2095
2096 error:
2097 err_code |= ERR_ALERT | ERR_FATAL;
2098 goto out;
2099}
2100
2101
Christopher Fauletce8111e2020-04-06 15:04:11 +02002102/* Parse the "rise" server keyword */
2103static int srv_parse_check_rise(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2104 char **errmsg)
2105{
2106 int err_code = 0;
2107
2108 if (!*args[*cur_arg + 1]) {
2109 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2110 goto error;
2111 }
2112
2113 srv->check.rise = atol(args[*cur_arg+1]);
2114 if (srv->check.rise <= 0) {
2115 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2116 goto error;
2117 }
2118
2119 if (srv->check.health)
2120 srv->check.health = srv->check.rise;
2121
2122 out:
2123 return err_code;
2124
2125 error:
2126 deinit_srv_agent_check(srv);
2127 err_code |= ERR_ALERT | ERR_FATAL;
2128 goto out;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002129}
2130
2131/* Parse the "fall" server keyword */
2132static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2133 char **errmsg)
2134{
2135 int err_code = 0;
2136
2137 if (!*args[*cur_arg + 1]) {
2138 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2139 goto error;
2140 }
2141
2142 srv->check.fall = atol(args[*cur_arg+1]);
2143 if (srv->check.fall <= 0) {
2144 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2145 goto error;
2146 }
2147
2148 out:
2149 return err_code;
2150
2151 error:
2152 deinit_srv_agent_check(srv);
2153 err_code |= ERR_ALERT | ERR_FATAL;
2154 goto out;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002155}
2156
2157/* Parse the "inter" server keyword */
2158static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2159 char **errmsg)
2160{
2161 const char *err = NULL;
2162 unsigned int delay;
2163 int err_code = 0;
2164
2165 if (!*(args[*cur_arg+1])) {
2166 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2167 goto error;
2168 }
2169
2170 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2171 if (err == PARSE_TIME_OVER) {
2172 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2173 args[*cur_arg+1], args[*cur_arg], srv->id);
2174 goto error;
2175 }
2176 else if (err == PARSE_TIME_UNDER) {
2177 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2178 args[*cur_arg+1], args[*cur_arg], srv->id);
2179 goto error;
2180 }
2181 else if (err) {
2182 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2183 *err, srv->id);
2184 goto error;
2185 }
2186 if (delay <= 0) {
2187 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2188 delay, args[*cur_arg], srv->id);
2189 goto error;
2190 }
2191 srv->check.inter = delay;
2192
2193 out:
2194 return err_code;
2195
2196 error:
2197 err_code |= ERR_ALERT | ERR_FATAL;
2198 goto out;
2199}
2200
2201
2202/* Parse the "fastinter" server keyword */
2203static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2204 char **errmsg)
2205{
2206 const char *err = NULL;
2207 unsigned int delay;
2208 int err_code = 0;
2209
2210 if (!*(args[*cur_arg+1])) {
2211 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2212 goto error;
2213 }
2214
2215 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2216 if (err == PARSE_TIME_OVER) {
2217 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2218 args[*cur_arg+1], args[*cur_arg], srv->id);
2219 goto error;
2220 }
2221 else if (err == PARSE_TIME_UNDER) {
2222 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2223 args[*cur_arg+1], args[*cur_arg], srv->id);
2224 goto error;
2225 }
2226 else if (err) {
2227 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2228 *err, srv->id);
2229 goto error;
2230 }
2231 if (delay <= 0) {
2232 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2233 delay, args[*cur_arg], srv->id);
2234 goto error;
2235 }
2236 srv->check.fastinter = delay;
2237
2238 out:
2239 return err_code;
2240
2241 error:
2242 err_code |= ERR_ALERT | ERR_FATAL;
2243 goto out;
2244}
2245
2246
2247/* Parse the "downinter" server keyword */
2248static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2249 char **errmsg)
2250{
2251 const char *err = NULL;
2252 unsigned int delay;
2253 int err_code = 0;
2254
2255 if (!*(args[*cur_arg+1])) {
2256 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2257 goto error;
2258 }
2259
2260 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2261 if (err == PARSE_TIME_OVER) {
2262 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2263 args[*cur_arg+1], args[*cur_arg], srv->id);
2264 goto error;
2265 }
2266 else if (err == PARSE_TIME_UNDER) {
2267 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2268 args[*cur_arg+1], args[*cur_arg], srv->id);
2269 goto error;
2270 }
2271 else if (err) {
2272 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2273 *err, srv->id);
2274 goto error;
2275 }
2276 if (delay <= 0) {
2277 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2278 delay, args[*cur_arg], srv->id);
2279 goto error;
2280 }
2281 srv->check.downinter = delay;
2282
2283 out:
2284 return err_code;
2285
2286 error:
2287 err_code |= ERR_ALERT | ERR_FATAL;
2288 goto out;
2289}
2290
2291/* Parse the "port" server keyword */
2292static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2293 char **errmsg)
2294{
2295 int err_code = 0;
2296
2297 if (!*(args[*cur_arg+1])) {
2298 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
2299 goto error;
2300 }
2301
Amaury Denoyelle403dce82021-07-29 15:39:43 +02002302 /* Only increment maxsock for servers from the configuration. Dynamic
2303 * servers at the moment are not taken into account for the estimation
2304 * of the resources limits.
2305 */
2306 if (global.mode & MODE_STARTING)
2307 global.maxsock++;
2308
Christopher Fauletce8111e2020-04-06 15:04:11 +02002309 srv->check.port = atol(args[*cur_arg+1]);
William Dauchy4858fb22021-02-03 22:30:09 +01002310 /* if agentport was never set, we can use port */
2311 if (!(srv->flags & SRV_F_AGENTPORT))
2312 srv->agent.port = srv->check.port;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002313
2314 out:
2315 return err_code;
2316
2317 error:
2318 err_code |= ERR_ALERT | ERR_FATAL;
2319 goto out;
2320}
2321
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002322static struct srv_kw_list srv_kws = { "CHK", { }, {
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002323 { "addr", srv_parse_addr, 1, 1, 1 }, /* IP address to send health to or to probe from agent-check */
2324 { "agent-addr", srv_parse_agent_addr, 1, 1, 1 }, /* Enable an auxiliary agent check */
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02002325 { "agent-check", srv_parse_agent_check, 0, 1, 1 }, /* Enable agent checks */
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002326 { "agent-inter", srv_parse_agent_inter, 1, 1, 1 }, /* Set the interval between two agent checks */
2327 { "agent-port", srv_parse_agent_port, 1, 1, 1 }, /* Set the TCP port used for agent checks. */
2328 { "agent-send", srv_parse_agent_send, 1, 1, 1 }, /* Set string to send to agent. */
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02002329 { "check", srv_parse_check, 0, 1, 1 }, /* Enable health checks */
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002330 { "check-proto", srv_parse_check_proto, 1, 1, 1 }, /* Set the mux protocol for health checks */
2331 { "check-send-proxy", srv_parse_check_send_proxy, 0, 1, 1 }, /* Enable PROXY protocol for health checks */
2332 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1, 1 }, /* Enable socks4 proxy for health checks */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002333 { "no-agent-check", srv_parse_no_agent_check, 0, 1, 0 }, /* Do not enable any auxiliary agent check */
2334 { "no-check", srv_parse_no_check, 0, 1, 0 }, /* Disable health checks */
2335 { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1, 0 }, /* Disable PROXY protocol for health checks */
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002336 { "rise", srv_parse_check_rise, 1, 1, 1 }, /* Set rise value for health checks */
2337 { "fall", srv_parse_check_fall, 1, 1, 1 }, /* Set fall value for health checks */
2338 { "inter", srv_parse_check_inter, 1, 1, 1 }, /* Set inter value for health checks */
2339 { "fastinter", srv_parse_check_fastinter, 1, 1, 1 }, /* Set fastinter value for health checks */
2340 { "downinter", srv_parse_check_downinter, 1, 1, 1 }, /* Set downinter value for health checks */
2341 { "port", srv_parse_check_port, 1, 1, 1 }, /* Set the TCP port used for health checks. */
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002342 { NULL, NULL, 0 },
2343}};
2344
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002345INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01002346
Willy Tarreaubd741542010-03-16 18:46:54 +01002347/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002348 * Local variables:
2349 * c-indent-level: 8
2350 * c-basic-offset: 8
2351 * End:
2352 */