blob: 71d3f3cdd5d90266592cf9b8d957db2831ca3f8f [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Health-checks functions.
3 *
Willy Tarreau26c25062009-03-08 09:38:41 +01004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreaub8816082008-01-18 12:18:15 +010014#include <assert.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020015#include <ctype.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <errno.h>
17#include <fcntl.h>
Simon Horman0ba0e4a2015-01-30 11:23:00 +090018#include <stdarg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020019#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020020#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020021#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020022#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023#include <unistd.h>
Willy Tarreau9f6dc722019-03-01 11:15:10 +010024#include <sys/resource.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020025#include <sys/socket.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040026#include <sys/types.h>
Simon Horman98637e52014-06-20 12:30:16 +090027#include <sys/wait.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028#include <netinet/in.h>
Willy Tarreau1274bc42009-07-15 07:16:31 +020029#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <arpa/inet.h>
31
Willy Tarreau122eba92020-06-04 10:15:32 +020032#include <haproxy/action.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020033#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020034#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020035#include <haproxy/cfgparse.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020036#include <haproxy/check.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020037#include <haproxy/chunk.h>
Willy Tarreau7c18b542020-06-11 09:23:02 +020038#include <haproxy/dgram.h>
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +020039#include <haproxy/dynbuf.h>
Willy Tarreaubcc67332020-06-05 15:31:31 +020040#include <haproxy/extcheck.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/fd.h>
42#include <haproxy/global.h>
43#include <haproxy/h1.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020044#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020045#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020046#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020047#include <haproxy/istbuf.h>
48#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020049#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020050#include <haproxy/mailers.h>
51#include <haproxy/port_range.h>
52#include <haproxy/proto_tcp.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020053#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020054#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020055#include <haproxy/queue.h>
56#include <haproxy/regex.h>
Emeric Brunc9437992021-02-12 19:42:55 +010057#include <haproxy/resolvers.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020058#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020059#include <haproxy/server.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020060#include <haproxy/ssl_sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020061#include <haproxy/stats-t.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020062#include <haproxy/task.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020063#include <haproxy/tcpcheck.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020064#include <haproxy/thread.h>
65#include <haproxy/time.h>
66#include <haproxy/tools.h>
Christopher Faulet147b8c92021-04-10 09:00:38 +020067#include <haproxy/trace.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020068#include <haproxy/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020069
Christopher Faulet147b8c92021-04-10 09:00:38 +020070/* trace source and events */
71static void check_trace(enum trace_level level, uint64_t mask,
72 const struct trace_source *src,
73 const struct ist where, const struct ist func,
74 const void *a1, const void *a2, const void *a3, const void *a4);
75
76/* The event representation is split like this :
77 * check - check
78 *
79 * CHECK_EV_* macros are defined in <haproxy/check.h>
80 */
81static const struct trace_event check_trace_events[] = {
82 { .mask = CHK_EV_TASK_WAKE, .name = "task_wake", .desc = "Check task woken up" },
83 { .mask = CHK_EV_HCHK_START, .name = "hchck_start", .desc = "Health-check started" },
84 { .mask = CHK_EV_HCHK_WAKE, .name = "hchck_wake", .desc = "Health-check woken up" },
85 { .mask = CHK_EV_HCHK_RUN, .name = "hchck_run", .desc = "Health-check running" },
86 { .mask = CHK_EV_HCHK_END, .name = "hchck_end", .desc = "Health-check terminated" },
87 { .mask = CHK_EV_HCHK_SUCC, .name = "hchck_succ", .desc = "Health-check success" },
88 { .mask = CHK_EV_HCHK_ERR, .name = "hchck_err", .desc = "Health-check failure" },
89
90 { .mask = CHK_EV_TCPCHK_EVAL, .name = "tcp_check_eval", .desc = "tcp-check rules evaluation" },
91 { .mask = CHK_EV_TCPCHK_ERR, .name = "tcp_check_err", .desc = "tcp-check evaluation error" },
92 { .mask = CHK_EV_TCPCHK_CONN, .name = "tcp_check_conn", .desc = "tcp-check connection rule" },
93 { .mask = CHK_EV_TCPCHK_SND, .name = "tcp_check_send", .desc = "tcp-check send rule" },
94 { .mask = CHK_EV_TCPCHK_EXP, .name = "tcp_check_expect", .desc = "tcp-check expect rule" },
95 { .mask = CHK_EV_TCPCHK_ACT, .name = "tcp_check_action", .desc = "tcp-check action rule" },
96
97 { .mask = CHK_EV_RX_DATA, .name = "rx_data", .desc = "receipt of data" },
98 { .mask = CHK_EV_RX_BLK, .name = "rx_blk", .desc = "receipt blocked" },
99 { .mask = CHK_EV_RX_ERR, .name = "rx_err", .desc = "receipt error" },
100
101 { .mask = CHK_EV_TX_DATA, .name = "tx_data", .desc = "transmission of data" },
102 { .mask = CHK_EV_TX_BLK, .name = "tx_blk", .desc = "transmission blocked" },
103 { .mask = CHK_EV_TX_ERR, .name = "tx_err", .desc = "transmission error" },
104
105 {}
106};
107
108static const struct name_desc check_trace_lockon_args[4] = {
109 /* arg1 */ { /* already used by the check */ },
110 /* arg2 */ { },
111 /* arg3 */ { },
112 /* arg4 */ { }
113};
114
115static const struct name_desc check_trace_decoding[] = {
116#define CHK_VERB_CLEAN 1
117 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
118#define CHK_VERB_MINIMAL 2
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200119 { .name="minimal", .desc="report info on stream and conn-streams" },
Christopher Faulet147b8c92021-04-10 09:00:38 +0200120#define CHK_VERB_SIMPLE 3
121 { .name="simple", .desc="add info on request and response channels" },
122#define CHK_VERB_ADVANCED 4
123 { .name="advanced", .desc="add info on channel's buffer for data and developer levels only" },
124#define CHK_VERB_COMPLETE 5
125 { .name="complete", .desc="add info on channel's buffer" },
126 { /* end */ }
127};
128
129struct trace_source trace_check = {
130 .name = IST("check"),
131 .desc = "Health-check",
132 .arg_def = TRC_ARG1_CHK, // TRACE()'s first argument is always a stream
133 .default_cb = check_trace,
134 .known_events = check_trace_events,
135 .lockon_args = check_trace_lockon_args,
136 .decoding = check_trace_decoding,
137 .report_events = ~0, // report everything by default
138};
139
140#define TRACE_SOURCE &trace_check
141INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
142
Olivier Houchard9130a962017-10-17 17:33:43 +0200143
Christopher Faulet61cc8522020-04-20 14:54:42 +0200144static int wake_srv_chk(struct conn_stream *cs);
145struct data_cb check_conn_cb = {
146 .wake = wake_srv_chk,
147 .name = "CHCK",
148};
Christopher Fauletd7e63962020-04-17 20:15:59 +0200149
Christopher Faulet5d503fc2020-03-30 20:34:34 +0200150
Gaetan Rivet05d692d2020-02-14 17:42:54 +0100151/* Dummy frontend used to create all checks sessions. */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200152struct proxy checks_fe;
Christopher Faulet31dff9b2017-10-23 15:45:20 +0200153
Christopher Faulet147b8c92021-04-10 09:00:38 +0200154
155static inline void check_trace_buf(const struct buffer *buf, size_t ofs, size_t len)
156{
157 size_t block1, block2;
158 int line, ptr, newptr;
159
160 block1 = b_contig_data(buf, ofs);
161 block2 = 0;
162 if (block1 > len)
163 block1 = len;
164 block2 = len - block1;
165
166 ofs = b_peek_ofs(buf, ofs);
167
168 line = 0;
169 ptr = ofs;
170 while (ptr < ofs + block1) {
171 newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), ofs + block1, &line, ptr);
172 if (newptr == ptr)
173 break;
174 ptr = newptr;
175 }
176
177 line = ptr = 0;
178 while (ptr < block2) {
179 newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), block2, &line, ptr);
180 if (newptr == ptr)
181 break;
182 ptr = newptr;
183 }
184}
185
186/* trace source and events */
187static void check_trace(enum trace_level level, uint64_t mask,
188 const struct trace_source *src,
189 const struct ist where, const struct ist func,
190 const void *a1, const void *a2, const void *a3, const void *a4)
191{
192 const struct check *check = a1;
193 const struct server *srv = (check ? check->server : NULL);
194 const size_t *val = a4;
195 const char *res;
196
197 if (!check || src->verbosity < CHK_VERB_CLEAN)
198 return;
199
200 chunk_appendf(&trace_buf, " : [%c] SRV=%s",
201 ((check->type == PR_O2_EXT_CHK) ? 'E' : (check->state & CHK_ST_AGENT ? 'A' : 'H')),
202 srv->id);
203
204 chunk_appendf(&trace_buf, " status=%d/%d %s",
205 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
206 (check->health >= check->rise) ? check->fall : check->rise,
207 (check->health >= check->rise) ? (srv->uweight ? "UP" : "DRAIN") : "DOWN");
208
209 switch (check->result) {
210 case CHK_RES_NEUTRAL: res = "-"; break;
211 case CHK_RES_FAILED: res = "FAIL"; break;
212 case CHK_RES_PASSED: res = "PASS"; break;
213 case CHK_RES_CONDPASS: res = "COND"; break;
214 default: res = "UNK"; break;
215 }
216
217 if (src->verbosity == CHK_VERB_CLEAN)
218 return;
219
220 chunk_appendf(&trace_buf, " - last=%s(%d)/%s(%d)",
221 get_check_status_info(check->status), check->status,
222 res, check->result);
223
224 /* Display the value to the 4th argument (level > STATE) */
225 if (src->level > TRACE_LEVEL_STATE && val)
226 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
227
228 chunk_appendf(&trace_buf, " check=%p(0x%08x)", check, check->state);
229
230 if (src->verbosity == CHK_VERB_MINIMAL)
231 return;
232
233
234 if (check->cs) {
Christopher Faulet0256da12021-12-15 09:50:17 +0100235 struct connection *conn = cs_conn(check->cs);
236
237 chunk_appendf(&trace_buf, " - conn=%p(0x%08x)", conn, conn ? conn->flags : 0);
Christopher Faulet147b8c92021-04-10 09:00:38 +0200238 chunk_appendf(&trace_buf, " cs=%p(0x%08x)", check->cs, check->cs->flags);
239 }
240
241 if (mask & CHK_EV_TCPCHK) {
242 const char *type;
243
244 switch (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) {
245 case TCPCHK_RULES_PGSQL_CHK: type = "PGSQL"; break;
246 case TCPCHK_RULES_REDIS_CHK: type = "REDIS"; break;
247 case TCPCHK_RULES_SMTP_CHK: type = "SMTP"; break;
248 case TCPCHK_RULES_HTTP_CHK: type = "HTTP"; break;
249 case TCPCHK_RULES_MYSQL_CHK: type = "MYSQL"; break;
250 case TCPCHK_RULES_LDAP_CHK: type = "LDAP"; break;
251 case TCPCHK_RULES_SSL3_CHK: type = "SSL3"; break;
252 case TCPCHK_RULES_AGENT_CHK: type = "AGENT"; break;
253 case TCPCHK_RULES_SPOP_CHK: type = "SPOP"; break;
254 case TCPCHK_RULES_TCP_CHK: type = "TCP"; break;
255 default: type = "???"; break;
256 }
257 if (check->current_step)
258 chunk_appendf(&trace_buf, " - tcp-check=(%s,%d)", type, tcpcheck_get_step_id(check, NULL));
259 else
260 chunk_appendf(&trace_buf, " - tcp-check=(%s,-)", type);
261 }
262
263 /* Display bi and bo buffer info (level > USER & verbosity > SIMPLE) */
264 if (src->level > TRACE_LEVEL_USER) {
265 const struct buffer *buf = NULL;
266
267 chunk_appendf(&trace_buf, " bi=%u@%p+%u/%u",
268 (unsigned int)b_data(&check->bi), b_orig(&check->bi),
269 (unsigned int)b_head_ofs(&check->bi), (unsigned int)b_size(&check->bi));
270 chunk_appendf(&trace_buf, " bo=%u@%p+%u/%u",
271 (unsigned int)b_data(&check->bo), b_orig(&check->bo),
272 (unsigned int)b_head_ofs(&check->bo), (unsigned int)b_size(&check->bo));
273
274 if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_RX)))
275 buf = (b_is_null(&check->bi) ? NULL : &check->bi);
276 else if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_TX)))
277 buf = (b_is_null(&check->bo) ? NULL : &check->bo);
278
279 if (buf) {
280 if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) {
281 int full = (src->verbosity == CHK_VERB_COMPLETE);
282
283 chunk_memcat(&trace_buf, "\n\t", 2);
284 htx_dump(&trace_buf, htxbuf(buf), full);
285 }
286 else {
287 int max = ((src->verbosity == CHK_VERB_COMPLETE) ? 1024 : 256);
288
289 chunk_memcat(&trace_buf, "\n", 1);
290 if (b_data(buf) > max) {
291 check_trace_buf(buf, 0, max);
292 chunk_memcat(&trace_buf, " ...\n", 6);
293 }
294 else
295 check_trace_buf(buf, 0, b_data(buf));
296 }
297
298 }
299 }
300
301}
302
303
Christopher Faulet61cc8522020-04-20 14:54:42 +0200304/**************************************************************************/
305/************************ Handle check results ****************************/
306/**************************************************************************/
307struct check_status {
308 short result; /* one of SRV_CHK_* */
309 char *info; /* human readable short info */
310 char *desc; /* long description */
311};
312
313struct analyze_status {
314 char *desc; /* description */
315 unsigned char lr[HANA_OBS_SIZE]; /* result for l4/l7: 0 = ignore, 1 - error, 2 - OK */
316};
317
Simon Horman63a4a822012-03-19 07:24:41 +0900318static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100319 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
320 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200321 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200322
Willy Tarreau23964182014-05-20 20:56:30 +0200323 /* Below we have finished checks */
324 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100325 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100326
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100327 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200328
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100329 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
330 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
331 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200332
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100333 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
334 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
335 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200336
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100337 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
338 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200339
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200340 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200341
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100342 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
343 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
344 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +0900345
346 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
347 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +0200348 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200349};
350
Simon Horman63a4a822012-03-19 07:24:41 +0900351static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100352 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
353
354 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
355 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
356
357 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
358 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
359 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
360 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
361
362 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
363 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
364 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
365};
366
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100367/* checks if <err> is a real error for errno or one that can be ignored, and
368 * return 0 for these ones or <err> for real ones.
369 */
370static inline int unclean_errno(int err)
371{
372 if (err == EAGAIN || err == EINPROGRESS ||
373 err == EISCONN || err == EALREADY)
374 return 0;
375 return err;
376}
377
Christopher Faulet7aa32712021-02-01 13:11:50 +0100378/* Converts check_status code to result code */
379short get_check_status_result(short check_status)
380{
381 if (check_status < HCHK_STATUS_SIZE)
382 return check_statuses[check_status].result;
383 else
384 return check_statuses[HCHK_STATUS_UNKNOWN].result;
385}
386
Christopher Faulet61cc8522020-04-20 14:54:42 +0200387/* Converts check_status code to description */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200388const char *get_check_status_description(short check_status) {
389
390 const char *desc;
391
392 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200393 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200394 else
395 desc = NULL;
396
397 if (desc && *desc)
398 return desc;
399 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200400 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200401}
402
Christopher Faulet61cc8522020-04-20 14:54:42 +0200403/* Converts check_status code to short info */
William Dauchyb26122b2021-02-14 22:26:23 +0100404const char *get_check_status_info(short check_status)
405{
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200406 const char *info;
407
408 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200409 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200410 else
411 info = NULL;
412
413 if (info && *info)
414 return info;
415 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200416 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200417}
418
Christopher Faulet61cc8522020-04-20 14:54:42 +0200419/* Convert analyze_status to description */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100420const char *get_analyze_status(short analyze_status) {
421
422 const char *desc;
423
424 if (analyze_status < HANA_STATUS_SIZE)
425 desc = analyze_statuses[analyze_status].desc;
426 else
427 desc = NULL;
428
429 if (desc && *desc)
430 return desc;
431 else
432 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
433}
434
Christopher Faulet61cc8522020-04-20 14:54:42 +0200435/* Sets check->status, update check->duration and fill check->result with an
436 * adequate CHK_RES_* value. The new check->health is computed based on the
437 * result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200438 *
Christopher Faulet61cc8522020-04-20 14:54:42 +0200439 * Shows information in logs about failed health check if server is UP or
440 * succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200441 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200442void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100443{
Simon Horman4a741432013-02-23 15:35:38 +0900444 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200445 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200446 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900447
Christopher Faulet147b8c92021-04-10 09:00:38 +0200448 TRACE_POINT(CHK_EV_HCHK_RUN, check);
449
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200450 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100451 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900452 check->desc[0] = '\0';
453 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200454 return;
455 }
456
Simon Horman4a741432013-02-23 15:35:38 +0900457 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200458 return;
459
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200460 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900461 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
462 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200463 } else
Simon Horman4a741432013-02-23 15:35:38 +0900464 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200465
Simon Horman4a741432013-02-23 15:35:38 +0900466 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200467 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900468 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200469
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100470 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900471 check->duration = -1;
472 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200473 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900474 check->duration = tv_ms_elapsed(&check->start, &now);
475 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200476 }
477
Willy Tarreau23964182014-05-20 20:56:30 +0200478 /* no change is expected if no state change occurred */
479 if (check->result == CHK_RES_NEUTRAL)
480 return;
481
Olivier Houchard0923fa42019-01-11 18:43:04 +0100482 /* If the check was really just sending a mail, it won't have an
483 * associated server, so we're done now.
484 */
485 if (!s)
486 return;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200487 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200488
Christopher Faulet147b8c92021-04-10 09:00:38 +0200489
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200490 switch (check->result) {
491 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200492 /* Failure to connect to the agent as a secondary check should not
493 * cause the server to be marked down.
494 */
495 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900496 (check->status >= HCHK_STATUS_L57DATA)) &&
Christopher Fauletb119a792018-05-02 12:12:45 +0200497 (check->health > 0)) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200498 _HA_ATOMIC_INC(&s->counters.failed_checks);
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200499 report = 1;
500 check->health--;
501 if (check->health < check->rise)
502 check->health = 0;
503 }
504 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200505
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200506 case CHK_RES_PASSED:
Christopher Faulet1e527cb2020-11-20 18:13:02 +0100507 case CHK_RES_CONDPASS:
508 if (check->health < check->rise + check->fall - 1) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200509 report = 1;
510 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200511
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200512 if (check->health >= check->rise)
513 check->health = check->rise + check->fall - 1; /* OK now */
514 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200515
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200516 /* clear consecutive_errors if observing is enabled */
517 if (s->onerror)
518 s->consecutive_errors = 0;
519 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100520
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200521 default:
522 break;
523 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200524
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200525 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
526 (status != prev_status || report)) {
527 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200528 "%s check for %sserver %s/%s %s%s",
529 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200530 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100531 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100532 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200533 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200534
Emeric Brun5a133512017-10-19 14:42:30 +0200535 srv_append_status(&trash, s, check, -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200536
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100537 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200538 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
539 (check->health >= check->rise) ? check->fall : check->rise,
540 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200541
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200542 ha_warning("%s.\n", trash.area);
543 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
544 send_email_alert(s, LOG_INFO, "%s", trash.area);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200545 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200546}
547
Willy Tarreau4eec5472014-05-20 22:32:27 +0200548/* Marks the check <check>'s server down if the current check is already failed
549 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200550 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200551void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200552{
Simon Horman4a741432013-02-23 15:35:38 +0900553 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900554
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200555 /* The agent secondary check should only cause a server to be marked
556 * as down if check->status is HCHK_STATUS_L7STS, which indicates
557 * that the agent returned "fail", "stopped" or "down".
558 * The implication here is that failure to connect to the agent
559 * as a secondary check should not cause the server to be marked
560 * down. */
561 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
562 return;
563
Willy Tarreau4eec5472014-05-20 22:32:27 +0200564 if (check->health > 0)
565 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100566
Christopher Faulet147b8c92021-04-10 09:00:38 +0200567 TRACE_STATE("health-check failed, set server DOWN", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200568 /* We only report a reason for the check if we did not do so previously */
Emeric Brun5a133512017-10-19 14:42:30 +0200569 srv_set_stopped(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200570}
571
Willy Tarreauaf549582014-05-16 17:37:50 +0200572/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200573 * it isn't in maintenance, it is not tracking a down server and other checks
574 * comply. The rule is simple : by default, a server is up, unless any of the
575 * following conditions is true :
576 * - health check failed (check->health < rise)
577 * - agent check failed (agent->health < rise)
578 * - the server tracks a down server (track && track->state == STOPPED)
579 * Note that if the server has a slowstart, it will switch to STARTING instead
580 * of RUNNING. Also, only the health checks support the nolb mode, so the
581 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200582 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200583void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200584{
Simon Horman4a741432013-02-23 15:35:38 +0900585 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100586
Emeric Brun52a91d32017-08-31 14:41:55 +0200587 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200588 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100589
Emeric Brun52a91d32017-08-31 14:41:55 +0200590 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200591 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100592
Willy Tarreau3e048382014-05-21 10:30:54 +0200593 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
594 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100595
Willy Tarreau3e048382014-05-21 10:30:54 +0200596 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
597 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200598
Emeric Brun52a91d32017-08-31 14:41:55 +0200599 if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING)
Willy Tarreau3e048382014-05-21 10:30:54 +0200600 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100601
Christopher Faulet147b8c92021-04-10 09:00:38 +0200602 TRACE_STATE("health-check succeeded, set server RUNNING", CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
Emeric Brun5a133512017-10-19 14:42:30 +0200603 srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100604}
605
Willy Tarreaudb58b792014-05-21 13:57:23 +0200606/* Marks the check <check> as valid and tries to set its server into stopping mode
607 * if it was running or starting, and provided it isn't in maintenance and other
608 * checks comply. The conditions for the server to be marked in stopping mode are
609 * the same as for it to be turned up. Also, only the health checks support the
610 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200611 */
Willy Tarreaubcc67332020-06-05 15:31:31 +0200612void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200613{
Simon Horman4a741432013-02-23 15:35:38 +0900614 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100615
Emeric Brun52a91d32017-08-31 14:41:55 +0200616 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreauaf549582014-05-16 17:37:50 +0200617 return;
618
Willy Tarreaudb58b792014-05-21 13:57:23 +0200619 if (check->state & CHK_ST_AGENT)
620 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100621
Emeric Brun52a91d32017-08-31 14:41:55 +0200622 if (s->track && s->track->next_state == SRV_ST_STOPPED)
Willy Tarreaudb58b792014-05-21 13:57:23 +0200623 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100624
Willy Tarreaudb58b792014-05-21 13:57:23 +0200625 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
626 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100627
Willy Tarreaudb58b792014-05-21 13:57:23 +0200628 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
629 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100630
Christopher Faulet147b8c92021-04-10 09:00:38 +0200631 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 +0100632 srv_set_stopping(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100633}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100635/* note: use health_adjust() only, which first checks that the observe mode is
Willy Tarreau4e9df272021-02-17 15:20:19 +0100636 * enabled. This will take the server lock if needed.
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100637 */
638void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100639{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100640 int failed;
641 int expire;
642
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100643 if (s->observe >= HANA_OBS_SIZE)
644 return;
645
Willy Tarreaubb956662013-01-24 00:37:39 +0100646 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100647 return;
648
649 switch (analyze_statuses[status].lr[s->observe - 1]) {
650 case 1:
651 failed = 1;
652 break;
653
654 case 2:
655 failed = 0;
656 break;
657
658 default:
659 return;
660 }
661
662 if (!failed) {
663 /* good: clear consecutive_errors */
664 s->consecutive_errors = 0;
665 return;
666 }
667
Willy Tarreau4781b152021-04-06 13:53:36 +0200668 _HA_ATOMIC_INC(&s->consecutive_errors);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100669
670 if (s->consecutive_errors < s->consecutive_errors_limit)
671 return;
672
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100673 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
674 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100675
Willy Tarreau4e9df272021-02-17 15:20:19 +0100676 if (s->check.fastinter)
677 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
678 else
679 expire = TICK_ETERNITY;
680
681 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
682
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100683 switch (s->onerror) {
684 case HANA_ONERR_FASTINTER:
685 /* force fastinter - nothing to do here as all modes force it */
686 break;
687
688 case HANA_ONERR_SUDDTH:
689 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900690 if (s->check.health > s->check.rise)
691 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100692
Tim Duesterhus588b3142020-05-29 14:35:51 +0200693 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100694
695 case HANA_ONERR_FAILCHK:
696 /* simulate a failed health check */
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 case HANA_ONERR_MARKDWN:
703 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900704 s->check.health = s->check.rise;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200705 set_server_check_status(&s->check, HCHK_STATUS_HANA,
706 trash.area);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200707 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100708 break;
709
710 default:
711 /* write a warning? */
712 break;
713 }
714
Willy Tarreau4e9df272021-02-17 15:20:19 +0100715 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
716
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100717 s->consecutive_errors = 0;
Willy Tarreau4781b152021-04-06 13:53:36 +0200718 _HA_ATOMIC_INC(&s->counters.failed_hana);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100719
Christopher Fauletea860832021-05-07 11:45:26 +0200720 if (tick_isset(expire) && tick_is_lt(expire, s->check.task->expire)) {
Willy Tarreau4e9df272021-02-17 15:20:19 +0100721 /* requeue check task with new expire */
722 task_schedule(s->check.task, expire);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100723 }
Willy Tarreauef781042010-01-27 11:53:01 +0100724}
725
Christopher Faulet61cc8522020-04-20 14:54:42 +0200726/* Checks the connection. If an error has already been reported or the socket is
Willy Tarreau20a18342013-12-05 00:31:46 +0100727 * closed, keep errno intact as it is supposed to contain the valid error code.
728 * If no error is reported, check the socket's error queue using getsockopt().
729 * Warning, this must be done only once when returning from poll, and never
730 * after an I/O error was attempted, otherwise the error queue might contain
731 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
732 * socket. Returns non-zero if an error was reported, zero if everything is
733 * clean (including a properly closed socket).
734 */
735static int retrieve_errno_from_socket(struct connection *conn)
736{
737 int skerr;
738 socklen_t lskerr = sizeof(skerr);
739
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100740 if (conn->flags & CO_FL_ERROR && (unclean_errno(errno) || !conn->ctrl))
Willy Tarreau20a18342013-12-05 00:31:46 +0100741 return 1;
742
Willy Tarreau3c728722014-01-23 13:50:42 +0100743 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100744 return 0;
745
Willy Tarreau07ecfc52022-04-11 18:07:03 +0200746 BUG_ON(conn->flags & CO_FL_FDLESS);
747
Willy Tarreau585744b2017-08-24 14:31:19 +0200748 if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
Willy Tarreau20a18342013-12-05 00:31:46 +0100749 errno = skerr;
750
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100751 errno = unclean_errno(errno);
Willy Tarreau20a18342013-12-05 00:31:46 +0100752
753 if (!errno) {
754 /* we could not retrieve an error, that does not mean there is
755 * none. Just don't change anything and only report the prior
756 * error if any.
757 */
758 if (conn->flags & CO_FL_ERROR)
759 return 1;
760 else
761 return 0;
762 }
763
764 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
765 return 1;
766}
767
Christopher Faulet61cc8522020-04-20 14:54:42 +0200768/* Tries to collect as much information as possible on the connection status,
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100769 * and adjust the server status accordingly. It may make use of <errno_bck>
770 * if non-null when the caller is absolutely certain of its validity (eg:
771 * checked just after a syscall). If the caller doesn't have a valid errno,
772 * it can pass zero, and retrieve_errno_from_socket() will be called to try
773 * to extract errno from the socket. If no error is reported, it will consider
774 * the <expired> flag. This is intended to be used when a connection error was
775 * reported in conn->flags or when a timeout was reported in <expired>. The
776 * function takes care of not updating a server status which was already set.
777 * All situations where at least one of <expired> or CO_FL_ERROR are set
778 * produce a status.
779 */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200780void chk_report_conn_err(struct check *check, int errno_bck, int expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100781{
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200782 struct conn_stream *cs = check->cs;
783 struct connection *conn = cs_conn(cs);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100784 const char *err_msg;
Willy Tarreau83061a82018-07-13 11:56:34 +0200785 struct buffer *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200786 int step;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100787
Christopher Faulet147b8c92021-04-10 09:00:38 +0200788 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100789 return;
Christopher Faulet147b8c92021-04-10 09:00:38 +0200790 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100791
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100792 errno = unclean_errno(errno_bck);
793 if (conn && errno)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100794 retrieve_errno_from_socket(conn);
795
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200796 if (conn && !(conn->flags & CO_FL_ERROR) &&
Christopher Fauletb041b232022-03-24 10:27:02 +0100797 cs && !(cs->endp->flags & CS_EP_ERROR) && !expired)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100798 return;
799
Christopher Faulet147b8c92021-04-10 09:00:38 +0200800 TRACE_ENTER(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check, 0, 0, (size_t[]){expired});
801
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100802 /* we'll try to build a meaningful error message depending on the
803 * context of the error possibly present in conn->err_code, and the
804 * socket error possibly collected above. This is useful to know the
805 * exact step of the L6 layer (eg: SSL handshake).
806 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200807 chk = get_trash_chunk();
808
Christopher Faulet799f3a42020-04-07 12:06:14 +0200809 if (check->type == PR_O2_TCPCHK_CHK &&
Christopher Fauletd7e63962020-04-17 20:15:59 +0200810 (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) {
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200811 step = tcpcheck_get_step_id(check, NULL);
Christopher Faulet147b8c92021-04-10 09:00:38 +0200812 if (!step) {
813 TRACE_DEVEL("initial connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200814 chunk_printf(chk, " at initial connection step of tcp-check");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200815 }
Willy Tarreau213c6782014-10-02 14:51:02 +0200816 else {
817 chunk_printf(chk, " at step %d of tcp-check", step);
818 /* we were looking for a string */
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200819 if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) {
820 if (check->current_step->connect.port)
821 chunk_appendf(chk, " (connect port %d)" ,check->current_step->connect.port);
Willy Tarreau213c6782014-10-02 14:51:02 +0200822 else
823 chunk_appendf(chk, " (connect)");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200824 TRACE_DEVEL("connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200825 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200826 else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) {
827 struct tcpcheck_expect *expect = &check->current_step->expect;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100828
829 switch (expect->type) {
830 case TCPCHK_EXPECT_STRING:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200831 chunk_appendf(chk, " (expect string '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100832 break;
833 case TCPCHK_EXPECT_BINARY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200834 chunk_appendf(chk, " (expect binary '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Gaetan Rivetb616add2020-02-07 15:37:17 +0100835 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200836 case TCPCHK_EXPECT_STRING_REGEX:
Willy Tarreau213c6782014-10-02 14:51:02 +0200837 chunk_appendf(chk, " (expect regex)");
Gaetan Rivetb616add2020-02-07 15:37:17 +0100838 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200839 case TCPCHK_EXPECT_BINARY_REGEX:
Gaetan Rivetefab6c62020-02-07 15:37:17 +0100840 chunk_appendf(chk, " (expect binary regex)");
841 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200842 case TCPCHK_EXPECT_STRING_LF:
843 chunk_appendf(chk, " (expect log-format string)");
844 break;
845 case TCPCHK_EXPECT_BINARY_LF:
846 chunk_appendf(chk, " (expect log-format binary)");
847 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200848 case TCPCHK_EXPECT_HTTP_STATUS:
Christopher Faulet8021a5f2020-04-24 13:53:12 +0200849 chunk_appendf(chk, " (expect HTTP status codes)");
Christopher Faulete5870d82020-04-15 11:32:03 +0200850 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200851 case TCPCHK_EXPECT_HTTP_STATUS_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200852 chunk_appendf(chk, " (expect HTTP status regex)");
853 break;
Christopher Faulet39708192020-05-05 10:47:36 +0200854 case TCPCHK_EXPECT_HTTP_HEADER:
855 chunk_appendf(chk, " (expect HTTP header pattern)");
856 break;
Christopher Faulete5870d82020-04-15 11:32:03 +0200857 case TCPCHK_EXPECT_HTTP_BODY:
Christopher Fauletb61caf42020-04-21 10:57:42 +0200858 chunk_appendf(chk, " (expect HTTP body content '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data));
Christopher Faulete5870d82020-04-15 11:32:03 +0200859 break;
Christopher Faulet67a23452020-05-05 18:10:01 +0200860 case TCPCHK_EXPECT_HTTP_BODY_REGEX:
Christopher Faulete5870d82020-04-15 11:32:03 +0200861 chunk_appendf(chk, " (expect HTTP body regex)");
862 break;
Christopher Fauletaaab0832020-05-05 15:54:22 +0200863 case TCPCHK_EXPECT_HTTP_BODY_LF:
864 chunk_appendf(chk, " (expect log-format HTTP body)");
865 break;
Christopher Faulet9e6ed152020-04-03 15:24:06 +0200866 case TCPCHK_EXPECT_CUSTOM:
867 chunk_appendf(chk, " (expect custom function)");
868 break;
Gaetan Rivetb616add2020-02-07 15:37:17 +0100869 case TCPCHK_EXPECT_UNDEF:
870 chunk_appendf(chk, " (undefined expect!)");
871 break;
872 }
Christopher Faulet147b8c92021-04-10 09:00:38 +0200873 TRACE_DEVEL("expect rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200874 }
Christopher Fauletb2c2e0f2020-03-30 11:05:10 +0200875 else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) {
Willy Tarreau213c6782014-10-02 14:51:02 +0200876 chunk_appendf(chk, " (send)");
Christopher Faulet147b8c92021-04-10 09:00:38 +0200877 TRACE_DEVEL("send rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200878 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200879
Christopher Faulet6f2a5e42020-04-01 13:11:41 +0200880 if (check->current_step && check->current_step->comment)
881 chunk_appendf(chk, " comment: '%s'", check->current_step->comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200882 }
883 }
884
Willy Tarreau00149122017-10-04 18:05:01 +0200885 if (conn && conn->err_code) {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100886 if (unclean_errno(errno))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200887 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno),
888 chk->area);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100889 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200890 chunk_printf(&trash, "%s%s", conn_err_code_str(conn),
891 chk->area);
892 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100893 }
894 else {
Willy Tarreauc8dc20a2019-12-27 12:03:27 +0100895 if (unclean_errno(errno)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200896 chunk_printf(&trash, "%s%s", strerror(errno),
897 chk->area);
898 err_msg = trash.area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100899 }
900 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200901 err_msg = chk->area;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100902 }
903 }
904
Willy Tarreau00149122017-10-04 18:05:01 +0200905 if (check->state & CHK_ST_PORT_MISS) {
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200906 /* NOTE: this is reported after <fall> tries */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +0200907 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
908 }
909
Christopher Faulet0256da12021-12-15 09:50:17 +0100910 if (!cs || !conn || !conn->ctrl) {
Christopher Faulet5e293762020-10-26 11:10:49 +0100911 /* error before any connection attempt (connection allocation error or no control layer) */
Willy Tarreau00149122017-10-04 18:05:01 +0200912 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
913 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100914 else if (conn->flags & CO_FL_WAIT_L4_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100915 /* L4 not established (yet) */
Christopher Fauletb041b232022-03-24 10:27:02 +0100916 if (conn->flags & CO_FL_ERROR || cs->endp->flags & CS_EP_ERROR)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100917 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
918 else if (expired)
919 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200920
921 /*
922 * might be due to a server IP change.
923 * Let's trigger a DNS resolution if none are currently running.
924 */
Olivier Houchard0923fa42019-01-11 18:43:04 +0100925 if (check->server)
Emeric Brund30e9a12020-12-23 18:49:16 +0100926 resolv_trigger_resolution(check->server->resolv_requester);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200927
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100928 }
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100929 else if (conn->flags & CO_FL_WAIT_L6_CONN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100930 /* L6 not established (yet) */
Christopher Fauletb041b232022-03-24 10:27:02 +0100931 if (conn->flags & CO_FL_ERROR || cs->endp->flags & CS_EP_ERROR)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100932 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
933 else if (expired)
934 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
935 }
Christopher Fauletb041b232022-03-24 10:27:02 +0100936 else if (conn->flags & CO_FL_ERROR || cs->endp->flags & CS_EP_ERROR) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100937 /* I/O error after connection was established and before we could diagnose */
938 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
939 }
940 else if (expired) {
Christopher Fauletcf80f2f2020-04-01 11:04:52 +0200941 enum healthcheck_status tout = HCHK_STATUS_L7TOUT;
942
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100943 /* connection established but expired check */
Christopher Faulet1941bab2020-05-05 07:55:50 +0200944 if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT &&
945 check->current_step->expect.tout_status != HCHK_STATUS_UNKNOWN)
Christopher Faulet811f78c2020-04-01 11:10:27 +0200946 tout = check->current_step->expect.tout_status;
947 set_server_check_status(check, tout, err_msg);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100948 }
949
Christopher Faulet147b8c92021-04-10 09:00:38 +0200950 TRACE_LEAVE(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100951 return;
952}
953
Simon Horman98637e52014-06-20 12:30:16 +0900954
Christopher Faulet61cc8522020-04-20 14:54:42 +0200955/* Builds the server state header used by HTTP health-checks */
Willy Tarreau51cd5952020-06-05 12:25:38 +0200956int httpchk_build_status_header(struct server *s, struct buffer *buf)
Simon Horman98637e52014-06-20 12:30:16 +0900957{
Christopher Faulet61cc8522020-04-20 14:54:42 +0200958 int sv_state;
959 int ratio;
960 char addr[46];
961 char port[6];
962 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
963 "UP %d/%d", "UP",
964 "NOLB %d/%d", "NOLB",
965 "no check" };
Simon Horman98637e52014-06-20 12:30:16 +0900966
Christopher Faulet61cc8522020-04-20 14:54:42 +0200967 if (!(s->check.state & CHK_ST_ENABLED))
968 sv_state = 6;
969 else if (s->cur_state != SRV_ST_STOPPED) {
970 if (s->check.health == s->check.rise + s->check.fall - 1)
971 sv_state = 3; /* UP */
972 else
973 sv_state = 2; /* going down */
Simon Horman98637e52014-06-20 12:30:16 +0900974
Christopher Faulet61cc8522020-04-20 14:54:42 +0200975 if (s->cur_state == SRV_ST_STOPPING)
976 sv_state += 2;
977 } else {
978 if (s->check.health)
979 sv_state = 1; /* going up */
980 else
981 sv_state = 0; /* DOWN */
Simon Horman98637e52014-06-20 12:30:16 +0900982 }
Willy Tarreaub7b24782016-06-21 15:32:29 +0200983
Christopher Faulet61cc8522020-04-20 14:54:42 +0200984 chunk_appendf(buf, srv_hlt_st[sv_state],
985 (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
986 (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreaub7b24782016-06-21 15:32:29 +0200987
Christopher Faulet61cc8522020-04-20 14:54:42 +0200988 addr_to_str(&s->addr, addr, sizeof(addr));
989 if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6)
990 snprintf(port, sizeof(port), "%u", s->svc_port);
991 else
992 *port = 0;
Willy Tarreaub7b24782016-06-21 15:32:29 +0200993
Christopher Faulet61cc8522020-04-20 14:54:42 +0200994 chunk_appendf(buf, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
995 addr, port, s->proxy->id, s->id,
996 global.node,
997 (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
998 (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 +0200999 s->cur_sess, s->proxy->beconn - s->proxy->queue.length,
Willy Tarreaua0570452021-06-18 09:30:30 +02001000 s->queue.length);
Willy Tarreau9f6dc722019-03-01 11:15:10 +01001001
Christopher Faulet61cc8522020-04-20 14:54:42 +02001002 if ((s->cur_state == SRV_ST_STARTING) &&
1003 now.tv_sec < s->last_change + s->slowstart &&
1004 now.tv_sec >= s->last_change) {
1005 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
1006 chunk_appendf(buf, "; throttle=%d%%", ratio);
1007 }
Christopher Fauletaaae9a02020-04-26 09:50:31 +02001008
Christopher Faulet61cc8522020-04-20 14:54:42 +02001009 return b_data(buf);
1010}
Christopher Fauletaaae9a02020-04-26 09:50:31 +02001011
Willy Tarreau51cd5952020-06-05 12:25:38 +02001012/**************************************************************************/
Willy Tarreau51cd5952020-06-05 12:25:38 +02001013/***************** Health-checks based on connections *********************/
1014/**************************************************************************/
1015/* This function is used only for server health-checks. It handles connection
1016 * status updates including errors. If necessary, it wakes the check task up.
1017 * It returns 0 on normal cases, <0 if at least one close() has happened on the
1018 * connection (eg: reconnect). It relies on tcpcheck_main().
Christopher Faulet61cc8522020-04-20 14:54:42 +02001019 */
Willy Tarreau51cd5952020-06-05 12:25:38 +02001020static int wake_srv_chk(struct conn_stream *cs)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001021{
Christopher Faulet0256da12021-12-15 09:50:17 +01001022 struct connection *conn;
Christopher Faulet693b23b2022-02-28 09:09:05 +01001023 struct check *check = __cs_check(cs);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001024 struct email_alertq *q = container_of(check, typeof(*q), check);
1025 int ret = 0;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001026
Christopher Faulet147b8c92021-04-10 09:00:38 +02001027 TRACE_ENTER(CHK_EV_HCHK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001028 if (check->server)
1029 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
1030 else
1031 HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001032
Willy Tarreau51cd5952020-06-05 12:25:38 +02001033 /* we may have to make progress on the TCP checks */
1034 ret = tcpcheck_main(check);
Christopher Fauletaaab0832020-05-05 15:54:22 +02001035
Willy Tarreau51cd5952020-06-05 12:25:38 +02001036 cs = check->cs;
Christopher Faulet0256da12021-12-15 09:50:17 +01001037 conn = cs_conn(cs);
Christopher Fauletaaab0832020-05-05 15:54:22 +02001038
Christopher Fauletb041b232022-03-24 10:27:02 +01001039 if (unlikely(!conn || !cs || conn->flags & CO_FL_ERROR || cs->endp->flags & CS_EP_ERROR)) {
Willy Tarreau51cd5952020-06-05 12:25:38 +02001040 /* We may get error reports bypassing the I/O handlers, typically
1041 * the case when sending a pure TCP check which fails, then the I/O
1042 * handlers above are not called. This is completely handled by the
1043 * main processing task so let's simply wake it up. If we get here,
1044 * we expect errno to still be valid.
1045 */
Christopher Faulet147b8c92021-04-10 09:00:38 +02001046 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 +02001047 chk_report_conn_err(check, errno, 0);
1048 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001049 }
1050
Christopher Faulet8f100422021-01-18 15:47:03 +01001051 if (check->result != CHK_RES_UNKNOWN || ret == -1) {
Willy Tarreau51cd5952020-06-05 12:25:38 +02001052 /* Check complete or aborted. If connection not yet closed do it
1053 * now and wake the check task up to be sure the result is
1054 * handled ASAP. */
Willy Tarreau51cd5952020-06-05 12:25:38 +02001055 ret = -1;
Christopher Faulet69ef6c92022-03-31 14:20:00 +02001056 if (conn) {
Christopher Fauletff022a22022-04-21 08:38:54 +02001057 cs_conn_drain_and_shut(cs);
Christopher Faulet69ef6c92022-03-31 14:20:00 +02001058 if (check->wait_list.events)
1059 conn->mux->unsubscribe(cs, check->wait_list.events, &check->wait_list);
1060 }
Christopher Faulet8f100422021-01-18 15:47:03 +01001061
Willy Tarreau51cd5952020-06-05 12:25:38 +02001062 /* We may have been scheduled to run, and the
1063 * I/O handler expects to have a cs, so remove
1064 * the tasklet
1065 */
1066 tasklet_remove_from_tasklet_list(check->wait_list.tasklet);
1067 task_wakeup(check->task, TASK_WOKEN_IO);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001068 }
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001069
Willy Tarreau51cd5952020-06-05 12:25:38 +02001070 if (check->server)
1071 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
1072 else
1073 HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001074
Christopher Faulet147b8c92021-04-10 09:00:38 +02001075 TRACE_LEAVE(CHK_EV_HCHK_WAKE, check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001076 return ret;
1077}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001078
Willy Tarreau51cd5952020-06-05 12:25:38 +02001079/* This function checks if any I/O is wanted, and if so, attempts to do so */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001080struct task *event_srv_chk_io(struct task *t, void *ctx, unsigned int state)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001081{
Willy Tarreau51cd5952020-06-05 12:25:38 +02001082 struct check *check = ctx;
1083 struct conn_stream *cs = check->cs;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001084
Willy Tarreau51cd5952020-06-05 12:25:38 +02001085 wake_srv_chk(cs);
1086 return NULL;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001087}
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001088
Willy Tarreau51cd5952020-06-05 12:25:38 +02001089/* manages a server health-check that uses a connection. Returns
1090 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001091 *
1092 * Please do NOT place any return statement in this function and only leave
Willy Tarreau51cd5952020-06-05 12:25:38 +02001093 * via the out_unlock label.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001094 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001095struct task *process_chk_conn(struct task *t, void *context, unsigned int state)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001096{
Willy Tarreau51cd5952020-06-05 12:25:38 +02001097 struct check *check = context;
1098 struct proxy *proxy = check->proxy;
Christopher Faulet177a0e62022-04-12 17:47:07 +02001099 struct conn_stream *cs;
Christopher Faulet92017a32021-05-06 16:01:18 +02001100 struct connection *conn;
Willy Tarreau51cd5952020-06-05 12:25:38 +02001101 int rv;
1102 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaudeccd112018-06-14 18:38:55 +02001103
Christopher Faulet147b8c92021-04-10 09:00:38 +02001104 TRACE_ENTER(CHK_EV_TASK_WAKE, check);
1105
Willy Tarreau51cd5952020-06-05 12:25:38 +02001106 if (check->server)
1107 HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock);
Christopher Faulet92017a32021-05-06 16:01:18 +02001108
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001109 if (unlikely(check->state & CHK_ST_PURGE)) {
1110 TRACE_STATE("health-check state to purge", CHK_EV_TASK_WAKE, check);
1111 }
1112 else if (!(check->state & (CHK_ST_INPROGRESS))) {
Willy Tarreau51cd5952020-06-05 12:25:38 +02001113 /* no check currently running */
Christopher Faulet147b8c92021-04-10 09:00:38 +02001114 if (!expired) /* woke up too early */ {
1115 TRACE_STATE("health-check wake up too early", CHK_EV_TASK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001116 goto out_unlock;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001117 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01001118
Willy Tarreau51cd5952020-06-05 12:25:38 +02001119 /* we don't send any health-checks when the proxy is
1120 * stopped, the server should not be checked or the check
1121 * is disabled.
1122 */
1123 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001124 (proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001125 TRACE_STATE("health-check paused or disabled", CHK_EV_TASK_WAKE, check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001126 goto reschedule;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001127 }
Christopher Faulet404f9192020-04-09 23:13:54 +02001128
Willy Tarreau51cd5952020-06-05 12:25:38 +02001129 /* we'll initiate a new check */
1130 set_server_check_status(check, HCHK_STATUS_START, NULL);
Christopher Faulet404f9192020-04-09 23:13:54 +02001131
Willy Tarreau51cd5952020-06-05 12:25:38 +02001132 check->state |= CHK_ST_INPROGRESS;
Christopher Faulet147b8c92021-04-10 09:00:38 +02001133 TRACE_STATE("init new health-check", CHK_EV_TASK_WAKE|CHK_EV_HCHK_START, check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001134
Willy Tarreau51cd5952020-06-05 12:25:38 +02001135 task_set_affinity(t, tid_bit);
1136
1137 check->current_step = NULL;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001138
Christopher Faulet177a0e62022-04-12 17:47:07 +02001139 check->cs = cs_new_from_check(check, CS_FL_NONE);
1140 if (!check->cs) {
1141 set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL);
1142 goto end;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001143 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001144 tcpcheck_main(check);
Christopher Faulet92017a32021-05-06 16:01:18 +02001145 expired = 0;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001146 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001147
Christopher Faulet92017a32021-05-06 16:01:18 +02001148 /* there was a test running.
1149 * First, let's check whether there was an uncaught error,
1150 * which can happen on connect timeout or error.
1151 */
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001152 if (check->result == CHK_RES_UNKNOWN && likely(!(check->state & CHK_ST_PURGE))) {
Christopher Faulet177a0e62022-04-12 17:47:07 +02001153 cs = check->cs;
1154 conn = (cs ? cs_conn(cs) : NULL);
1155
Christopher Faulet92017a32021-05-06 16:01:18 +02001156 /* Here the connection must be defined. Otherwise the
1157 * error would have already been detected
Willy Tarreau51cd5952020-06-05 12:25:38 +02001158 */
Christopher Faulet177a0e62022-04-12 17:47:07 +02001159 if ((conn && ((conn->flags & CO_FL_ERROR) || (cs->endp->flags & CS_EP_ERROR))) || expired) {
Christopher Faulet92017a32021-05-06 16:01:18 +02001160 TRACE_ERROR("report connection error", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1161 chk_report_conn_err(check, 0, expired);
1162 }
1163 else {
1164 if (check->state & CHK_ST_CLOSE_CONN) {
1165 TRACE_DEVEL("closing current connection", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
Christopher Faulet92017a32021-05-06 16:01:18 +02001166 check->state &= ~CHK_ST_CLOSE_CONN;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001167 conn = NULL;
Christopher Fauletb041b232022-03-24 10:27:02 +01001168 cs_reset_endp(check->cs); /* error will be handled by tcpcheck_main() */
Christopher Faulet92017a32021-05-06 16:01:18 +02001169 tcpcheck_main(check);
Willy Tarreau51cd5952020-06-05 12:25:38 +02001170 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001171 if (check->result == CHK_RES_UNKNOWN) {
1172 TRACE_DEVEL("health-check not expired", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
1173 goto out_unlock; /* timeout not reached, wait again */
Christopher Faulet8f100422021-01-18 15:47:03 +01001174 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001175 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001176 }
Christopher Faulet404f9192020-04-09 23:13:54 +02001177
Christopher Faulet92017a32021-05-06 16:01:18 +02001178 /* check complete or aborted */
1179 TRACE_STATE("health-check complete or aborted", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END, check);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001180
Christopher Faulet92017a32021-05-06 16:01:18 +02001181 check->current_step = NULL;
Christopher Faulet177a0e62022-04-12 17:47:07 +02001182 cs = check->cs;
1183 conn = (cs ? cs_conn(cs) : NULL);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001184
Christopher Faulet92017a32021-05-06 16:01:18 +02001185 if (conn && conn->xprt) {
1186 /* The check was aborted and the connection was not yet closed.
1187 * This can happen upon timeout, or when an external event such
1188 * as a failed response coupled with "observe layer7" caused the
1189 * server state to be suddenly changed.
1190 */
Christopher Fauletff022a22022-04-21 08:38:54 +02001191 cs_conn_drain_and_shut(cs);
Christopher Faulet92017a32021-05-06 16:01:18 +02001192 }
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001193
Christopher Faulet177a0e62022-04-12 17:47:07 +02001194 if (cs) {
1195 if (conn && check->wait_list.events)
1196 conn->mux->unsubscribe(cs, check->wait_list.events, &check->wait_list);
1197 /* We may have been scheduled to run, and the
1198 * I/O handler expects to have a cs, so remove
1199 * the tasklet
1200 */
1201 tasklet_remove_from_tasklet_list(check->wait_list.tasklet);
1202 cs_detach_endp(cs);
1203 cs_detach_app(cs);
1204 cs = check->cs = NULL;
1205 conn = NULL;
1206 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001207
Christopher Faulet92017a32021-05-06 16:01:18 +02001208 if (check->sess != NULL) {
1209 vars_prune(&check->vars, check->sess, NULL);
1210 session_free(check->sess);
1211 check->sess = NULL;
1212 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001213
Christopher Fauletb041b232022-03-24 10:27:02 +01001214 end:
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001215 if (check->server && likely(!(check->state & CHK_ST_PURGE))) {
Christopher Faulet92017a32021-05-06 16:01:18 +02001216 if (check->result == CHK_RES_FAILED) {
1217 /* a failure or timeout detected */
1218 TRACE_DEVEL("report failure", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check);
1219 check_notify_failure(check);
1220 }
1221 else if (check->result == CHK_RES_CONDPASS) {
1222 /* check is OK but asks for stopping mode */
1223 TRACE_DEVEL("report conditional success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
1224 check_notify_stopping(check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001225 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001226 else if (check->result == CHK_RES_PASSED) {
1227 /* a success was detected */
1228 TRACE_DEVEL("report success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check);
1229 check_notify_success(check);
1230 }
1231 }
1232 task_set_affinity(t, MAX_THREADS_MASK);
1233 check_release_buf(check, &check->bi);
1234 check_release_buf(check, &check->bo);
1235 check->state &= ~(CHK_ST_INPROGRESS|CHK_ST_IN_ALLOC|CHK_ST_OUT_ALLOC);
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001236
Christopher Faulet92017a32021-05-06 16:01:18 +02001237 if (check->server) {
1238 rv = 0;
1239 if (global.spread_checks > 0) {
1240 rv = srv_getinter(check) * global.spread_checks / 100;
1241 rv -= (int) (2 * rv * (ha_random32() / 4294967295.0));
Christopher Faulet61cc8522020-04-20 14:54:42 +02001242 }
Christopher Faulet92017a32021-05-06 16:01:18 +02001243 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Christopher Faulet61cc8522020-04-20 14:54:42 +02001244 }
Willy Tarreau51cd5952020-06-05 12:25:38 +02001245
1246 reschedule:
1247 while (tick_is_expired(t->expire, now_ms))
1248 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1249 out_unlock:
1250 if (check->server)
1251 HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock);
Christopher Faulet147b8c92021-04-10 09:00:38 +02001252
1253 TRACE_LEAVE(CHK_EV_TASK_WAKE, check);
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001254
1255 /* Free the check if set to PURGE. After this, the check instance may be
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001256 * freed via the srv_drop invocation, so it must not be accessed after
1257 * this point.
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001258 */
1259 if (unlikely(check->state & CHK_ST_PURGE)) {
Amaury Denoyelle26cb8342021-08-10 16:23:49 +02001260 free_check(check);
Amaury Denoyelle9ba34ae2021-08-09 15:09:17 +02001261 if (check->server)
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001262 srv_drop(check->server);
Amaury Denoyelle26cb8342021-08-10 16:23:49 +02001263
1264 t = NULL;
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001265 }
1266
Willy Tarreau51cd5952020-06-05 12:25:38 +02001267 return t;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001268}
1269
Willy Tarreau51cd5952020-06-05 12:25:38 +02001270
Christopher Faulet61cc8522020-04-20 14:54:42 +02001271/**************************************************************************/
1272/************************** Init/deinit checks ****************************/
1273/**************************************************************************/
Christopher Fauletb381a502020-11-25 13:47:00 +01001274/*
1275 * Tries to grab a buffer and to re-enables processing on check <target>. The
1276 * check flags are used to figure what buffer was requested. It returns 1 if the
1277 * allocation succeeds, in which case the I/O tasklet is woken up, or 0 if it's
1278 * impossible to wake up and we prefer to be woken up later.
1279 */
1280int check_buf_available(void *target)
Christopher Faulet61cc8522020-04-20 14:54:42 +02001281{
Christopher Fauletb381a502020-11-25 13:47:00 +01001282 struct check *check = target;
1283
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001284 if ((check->state & CHK_ST_IN_ALLOC) && b_alloc(&check->bi)) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001285 TRACE_STATE("unblocking check, input buffer allocated", CHK_EV_TCPCHK_EXP|CHK_EV_RX_BLK, check);
Christopher Fauletb381a502020-11-25 13:47:00 +01001286 check->state &= ~CHK_ST_IN_ALLOC;
1287 tasklet_wakeup(check->wait_list.tasklet);
1288 return 1;
1289 }
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001290 if ((check->state & CHK_ST_OUT_ALLOC) && b_alloc(&check->bo)) {
Christopher Faulet147b8c92021-04-10 09:00:38 +02001291 TRACE_STATE("unblocking check, output buffer allocated", CHK_EV_TCPCHK_SND|CHK_EV_TX_BLK, check);
Christopher Fauletb381a502020-11-25 13:47:00 +01001292 check->state &= ~CHK_ST_OUT_ALLOC;
1293 tasklet_wakeup(check->wait_list.tasklet);
1294 return 1;
1295 }
1296
1297 return 0;
1298}
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001299
Christopher Fauletb381a502020-11-25 13:47:00 +01001300/*
William Dauchyf4300902021-02-06 20:47:50 +01001301 * Allocate a buffer. If it fails, it adds the check in buffer wait queue.
Christopher Fauletb381a502020-11-25 13:47:00 +01001302 */
1303struct buffer *check_get_buf(struct check *check, struct buffer *bptr)
1304{
1305 struct buffer *buf = NULL;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001306
Willy Tarreau2b718102021-04-21 07:32:39 +02001307 if (likely(!LIST_INLIST(&check->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +01001308 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Fauletb381a502020-11-25 13:47:00 +01001309 check->buf_wait.target = check;
1310 check->buf_wait.wakeup_cb = check_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +02001311 LIST_APPEND(&th_ctx->buffer_wq, &check->buf_wait.list);
Christopher Fauletb381a502020-11-25 13:47:00 +01001312 }
1313 return buf;
1314}
1315
1316/*
1317 * Release a buffer, if any, and try to wake up entities waiting in the buffer
1318 * wait queue.
1319 */
1320void check_release_buf(struct check *check, struct buffer *bptr)
1321{
1322 if (bptr->size) {
1323 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01001324 offer_buffers(check->buf_wait.target, 1);
Christopher Fauletb381a502020-11-25 13:47:00 +01001325 }
1326}
1327
1328const char *init_check(struct check *check, int type)
1329{
1330 check->type = type;
Christopher Fauletba3c68f2020-04-01 16:27:05 +02001331
Christopher Fauletb381a502020-11-25 13:47:00 +01001332 check->bi = BUF_NULL;
1333 check->bo = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01001334 LIST_INIT(&check->buf_wait.list);
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001335
Christopher Faulet61cc8522020-04-20 14:54:42 +02001336 check->wait_list.tasklet = tasklet_new();
1337 if (!check->wait_list.tasklet)
1338 return "out of memory while allocating check tasklet";
1339 check->wait_list.events = 0;
1340 check->wait_list.tasklet->process = event_srv_chk_io;
1341 check->wait_list.tasklet->context = check;
1342 return NULL;
1343}
1344
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001345/* Liberates the resources allocated for a check.
1346 *
Amaury Denoyelle6d7fc442021-08-10 16:22:51 +02001347 * This function must only be run by the thread owning the check.
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001348 */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001349void free_check(struct check *check)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001350{
Amaury Denoyelle6d7fc442021-08-10 16:22:51 +02001351 /* For agent-check, free the rules / vars from the server. This is not
1352 * done for health-check : the proxy is the owner of the rules / vars
1353 * in this case.
1354 */
1355 if (check->state & CHK_ST_AGENT) {
1356 free_tcpcheck_vars(&check->tcpcheck_rules->preset_vars);
1357 ha_free(&check->tcpcheck_rules);
1358 }
1359
Christopher Faulet61cc8522020-04-20 14:54:42 +02001360 task_destroy(check->task);
1361 if (check->wait_list.tasklet)
1362 tasklet_free(check->wait_list.tasklet);
1363
Christopher Fauletb381a502020-11-25 13:47:00 +01001364 check_release_buf(check, &check->bi);
1365 check_release_buf(check, &check->bo);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001366 if (check->cs) {
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001367 cs_detach_endp(check->cs);
1368 cs_detach_app(check->cs);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001369 check->cs = NULL;
1370 }
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001371}
1372
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001373/* This function must be used in order to free a started check. The check will
1374 * be scheduled for a next execution in order to properly close and free all
1375 * check elements.
1376 *
1377 * Non thread-safe.
1378 */
1379void check_purge(struct check *check)
1380{
Amaury Denoyelle25fe1032021-08-10 16:21:55 +02001381 check->state |= CHK_ST_PURGE;
Amaury Denoyelleb33a0ab2021-07-29 15:51:45 +02001382 task_wakeup(check->task, TASK_WOKEN_OTHER);
1383}
1384
Christopher Faulet61cc8522020-04-20 14:54:42 +02001385/* manages a server health-check. Returns the time the task accepts to wait, or
1386 * TIME_ETERNITY for infinity.
1387 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001388struct task *process_chk(struct task *t, void *context, unsigned int state)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001389{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001390 struct check *check = context;
1391
1392 if (check->type == PR_O2_EXT_CHK)
1393 return process_chk_proc(t, context, state);
1394 return process_chk_conn(t, context, state);
1395
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001396}
1397
Christopher Faulet61cc8522020-04-20 14:54:42 +02001398
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001399int start_check_task(struct check *check, int mininter,
Christopher Faulet61cc8522020-04-20 14:54:42 +02001400 int nbcheck, int srvpos)
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001401{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001402 struct task *t;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001403
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001404 /* task for the check. Process-based checks exclusively run on thread 1. */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001405 if (check->type == PR_O2_EXT_CHK)
Willy Tarreaua89c1912021-10-20 18:43:30 +02001406 t = task_new_on(0);
Christopher Faulet177a0e62022-04-12 17:47:07 +02001407 else
Willy Tarreaubeeabf52021-10-01 18:23:30 +02001408 t = task_new_anywhere();
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001409
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001410 if (!t)
1411 goto fail_alloc_task;
1412
Christopher Faulet61cc8522020-04-20 14:54:42 +02001413 check->task = t;
1414 t->process = process_chk;
1415 t->context = check;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001416
Christopher Faulet61cc8522020-04-20 14:54:42 +02001417 if (mininter < srv_getinter(check))
1418 mininter = srv_getinter(check);
1419
1420 if (global.max_spread_checks && mininter > global.max_spread_checks)
1421 mininter = global.max_spread_checks;
1422
1423 /* check this every ms */
1424 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
1425 check->start = now;
1426 task_queue(t);
1427
1428 return 1;
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001429
1430 fail_alloc_task:
Christopher Faulet54e85cb2022-01-06 08:46:56 +01001431 ha_alert("Starting [%s:%s] check: out of memory.\n",
1432 check->server->proxy->id, check->server->id);
1433 return 0;
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001434}
1435
Christopher Faulet61cc8522020-04-20 14:54:42 +02001436/*
1437 * Start health-check.
1438 * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case.
1439 */
1440static int start_checks()
1441{
1442
1443 struct proxy *px;
1444 struct server *s;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001445 int nbcheck=0, mininter=0, srvpos=0;
1446
1447 /* 0- init the dummy frontend used to create all checks sessions */
1448 init_new_proxy(&checks_fe);
Christopher Faulet0f1fc232021-04-16 10:49:07 +02001449 checks_fe.id = strdup("CHECKS-FE");
Christopher Faulet61cc8522020-04-20 14:54:42 +02001450 checks_fe.cap = PR_CAP_FE | PR_CAP_BE;
1451 checks_fe.mode = PR_MODE_TCP;
1452 checks_fe.maxconn = 0;
1453 checks_fe.conn_retries = CONN_RETRIES;
1454 checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
1455 checks_fe.timeout.client = TICK_ETERNITY;
1456
1457 /* 1- count the checkers to run simultaneously.
1458 * We also determine the minimum interval among all of those which
1459 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1460 * will be used to spread their start-up date. Those which have
1461 * a shorter interval will start independently and will not dictate
1462 * too short an interval for all others.
1463 */
1464 for (px = proxies_list; px; px = px->next) {
1465 for (s = px->srv; s; s = s->next) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001466 if (s->check.state & CHK_ST_CONFIGURED) {
1467 nbcheck++;
1468 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1469 (!mininter || mininter > srv_getinter(&s->check)))
1470 mininter = srv_getinter(&s->check);
Christopher Faulet5c288742020-03-31 08:15:58 +02001471 }
1472
Christopher Faulet61cc8522020-04-20 14:54:42 +02001473 if (s->agent.state & CHK_ST_CONFIGURED) {
1474 nbcheck++;
1475 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
1476 (!mininter || mininter > srv_getinter(&s->agent)))
1477 mininter = srv_getinter(&s->agent);
1478 }
Christopher Faulet5c288742020-03-31 08:15:58 +02001479 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001480 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001481
Christopher Faulet61cc8522020-04-20 14:54:42 +02001482 if (!nbcheck)
Christopher Fauletfc633b62020-11-06 15:24:23 +01001483 return ERR_NONE;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001484
Christopher Faulet61cc8522020-04-20 14:54:42 +02001485 srand((unsigned)time(NULL));
Christopher Fauletb7d30092020-03-30 15:19:03 +02001486
William Dauchyf4300902021-02-06 20:47:50 +01001487 /* 2- start them as far as possible from each other. For this, we will
1488 * start them after their interval is set to the min interval divided
1489 * by the number of servers, weighted by the server's position in the
1490 * list.
Christopher Faulet61cc8522020-04-20 14:54:42 +02001491 */
1492 for (px = proxies_list; px; px = px->next) {
1493 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
1494 if (init_pid_list()) {
1495 ha_alert("Starting [%s] check: out of memory.\n", px->id);
1496 return ERR_ALERT | ERR_FATAL;
1497 }
1498 }
Christopher Fauletb7d30092020-03-30 15:19:03 +02001499
Christopher Faulet61cc8522020-04-20 14:54:42 +02001500 for (s = px->srv; s; s = s->next) {
1501 /* A task for the main check */
1502 if (s->check.state & CHK_ST_CONFIGURED) {
1503 if (s->check.type == PR_O2_EXT_CHK) {
1504 if (!prepare_external_check(&s->check))
1505 return ERR_ALERT | ERR_FATAL;
Christopher Fauletb7d30092020-03-30 15:19:03 +02001506 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001507 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1508 return ERR_ALERT | ERR_FATAL;
1509 srvpos++;
Christopher Faulet98572322020-03-30 13:16:44 +02001510 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001511
Christopher Faulet61cc8522020-04-20 14:54:42 +02001512 /* A task for a auxiliary agent check */
1513 if (s->agent.state & CHK_ST_CONFIGURED) {
1514 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1515 return ERR_ALERT | ERR_FATAL;
1516 }
1517 srvpos++;
1518 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001519 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001520 }
Christopher Fauletfc633b62020-11-06 15:24:23 +01001521 return ERR_NONE;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001522}
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001523
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001524
Christopher Faulet61cc8522020-04-20 14:54:42 +02001525/*
1526 * Return value:
1527 * the port to be used for the health check
1528 * 0 in case no port could be found for the check
1529 */
1530static int srv_check_healthcheck_port(struct check *chk)
1531{
1532 int i = 0;
1533 struct server *srv = NULL;
1534
1535 srv = chk->server;
1536
William Dauchyf4300902021-02-06 20:47:50 +01001537 /* by default, we use the health check port configured */
Christopher Faulet61cc8522020-04-20 14:54:42 +02001538 if (chk->port > 0)
1539 return chk->port;
1540
1541 /* try to get the port from check_core.addr if check.port not set */
1542 i = get_host_port(&chk->addr);
1543 if (i > 0)
1544 return i;
1545
1546 /* try to get the port from server address */
1547 /* prevent MAPPORTS from working at this point, since checks could
1548 * not be performed in such case (MAPPORTS impose a relative ports
1549 * based on live traffic)
1550 */
1551 if (srv->flags & SRV_F_MAPPORTS)
1552 return 0;
1553
1554 i = srv->svc_port; /* by default */
1555 if (i > 0)
1556 return i;
1557
1558 return 0;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001559}
1560
Christopher Faulet61cc8522020-04-20 14:54:42 +02001561/* Initializes an health-check attached to the server <srv>. Non-zero is returned
1562 * if an error occurred.
1563 */
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001564int init_srv_check(struct server *srv)
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001565{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001566 const char *err;
1567 struct tcpcheck_rule *r;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001568 int ret = ERR_NONE;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001569 int check_type;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001570
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001571 if (!srv->do_check || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001572 goto out;
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001573
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001574 check_type = srv->check.tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001575
Amaury Denoyelle7d098be2022-03-09 14:20:10 +01001576 if (!(srv->flags & SRV_F_DYNAMIC)) {
1577 /* If neither a port nor an addr was specified and no check
1578 * transport layer is forced, then the transport layer used by
1579 * the checks is the same as for the production traffic.
1580 * Otherwise we use raw_sock by default, unless one is
1581 * specified.
1582 */
1583 if (!srv->check.port && !is_addr(&srv->check.addr)) {
1584 if (!srv->check.use_ssl && srv->use_ssl != -1) {
1585 srv->check.use_ssl = srv->use_ssl;
1586 srv->check.xprt = srv->xprt;
1587 }
1588 else if (srv->check.use_ssl == 1)
1589 srv->check.xprt = xprt_get(XPRT_SSL);
1590 srv->check.send_proxy |= (srv->pp_opts);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001591 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001592 else if (srv->check.use_ssl == 1)
1593 srv->check.xprt = xprt_get(XPRT_SSL);
Amaury Denoyelle7d098be2022-03-09 14:20:10 +01001594 }
1595 else {
1596 /* For dynamic servers, check-ssl and check-send-proxy must be
1597 * explicitely defined even if the check port was not
1598 * overridden.
1599 */
1600 if (srv->check.use_ssl == 1)
1601 srv->check.xprt = xprt_get(XPRT_SSL);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001602 }
1603
Christopher Faulet12882cf2020-04-23 15:50:18 +02001604 /* Inherit the mux protocol from the server if not already defined for
1605 * the check
1606 */
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001607 if (srv->mux_proto && !srv->check.mux_proto &&
1608 ((srv->mux_proto->mode == PROTO_MODE_HTTP && check_type == TCPCHK_RULES_HTTP_CHK) ||
1609 (srv->mux_proto->mode == PROTO_MODE_TCP && check_type != TCPCHK_RULES_HTTP_CHK))) {
Christopher Faulet12882cf2020-04-23 15:50:18 +02001610 srv->check.mux_proto = srv->mux_proto;
Amaury Denoyelle0519bd42020-11-13 12:34:56 +01001611 }
Amaury Denoyelle7c148902020-11-13 12:34:57 +01001612 /* test that check proto is valid if explicitly defined */
1613 else if (srv->check.mux_proto &&
1614 ((srv->check.mux_proto->mode == PROTO_MODE_HTTP && check_type != TCPCHK_RULES_HTTP_CHK) ||
1615 (srv->check.mux_proto->mode == PROTO_MODE_TCP && check_type == TCPCHK_RULES_HTTP_CHK))) {
1616 ha_alert("config: %s '%s': server '%s' uses an incompatible MUX protocol for the selected check type\n",
1617 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1618 ret |= ERR_ALERT | ERR_FATAL;
1619 goto out;
1620 }
Christopher Faulet12882cf2020-04-23 15:50:18 +02001621
Christopher Faulet61cc8522020-04-20 14:54:42 +02001622 /* validate <srv> server health-check settings */
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001623
Christopher Faulet61cc8522020-04-20 14:54:42 +02001624 /* We need at least a service port, a check port or the first tcp-check
1625 * rule must be a 'connect' one when checking an IPv4/IPv6 server.
1626 */
1627 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1628 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1629 goto init;
Christopher Fauletf50f4e92020-03-30 19:52:29 +02001630
Christopher Faulet61cc8522020-04-20 14:54:42 +02001631 if (!srv->proxy->tcpcheck_rules.list || LIST_ISEMPTY(srv->proxy->tcpcheck_rules.list)) {
1632 ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n",
1633 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1634 ret |= ERR_ALERT | ERR_ABORT;
1635 goto out;
1636 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001637
Christopher Faulet61cc8522020-04-20 14:54:42 +02001638 /* search the first action (connect / send / expect) in the list */
1639 r = get_first_tcpcheck_rule(&srv->proxy->tcpcheck_rules);
1640 if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) {
1641 ha_alert("config: %s '%s': server '%s' has neither service port nor check port "
1642 "nor tcp_check rule 'connect' with port information.\n",
1643 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1644 ret |= ERR_ALERT | ERR_ABORT;
1645 goto out;
1646 }
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001647
Christopher Faulet61cc8522020-04-20 14:54:42 +02001648 /* scan the tcp-check ruleset to ensure a port has been configured */
1649 list_for_each_entry(r, srv->proxy->tcpcheck_rules.list, list) {
Willy Tarreauacff3092021-07-22 11:06:41 +02001650 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port && !get_host_port(&r->connect.addr))) {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001651 ha_alert("config: %s '%s': server '%s' has neither service port nor check port, "
1652 "and a tcp_check rule 'connect' with no port information.\n",
1653 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1654 ret |= ERR_ALERT | ERR_ABORT;
1655 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001656 }
Christopher Faulete5870d82020-04-15 11:32:03 +02001657 }
1658
Christopher Faulet61cc8522020-04-20 14:54:42 +02001659 init:
Christopher Faulet61cc8522020-04-20 14:54:42 +02001660 err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY);
1661 if (err) {
1662 ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n",
1663 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1664 ret |= ERR_ALERT | ERR_ABORT;
1665 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001666 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001667 srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001668 srv_take(srv);
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001669
1670 /* Only increment maxsock for servers from the configuration. Dynamic
1671 * servers at the moment are not taken into account for the estimation
1672 * of the resources limits.
1673 */
1674 if (global.mode & MODE_STARTING)
1675 global.maxsock++;
Christopher Faulete5870d82020-04-15 11:32:03 +02001676
Christopher Faulet61cc8522020-04-20 14:54:42 +02001677 out:
1678 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001679}
1680
Christopher Faulet61cc8522020-04-20 14:54:42 +02001681/* Initializes an agent-check attached to the server <srv>. Non-zero is returned
1682 * if an error occurred.
1683 */
Amaury Denoyelle3c2ab1a2021-07-22 16:04:40 +02001684int init_srv_agent_check(struct server *srv)
Christopher Faulete5870d82020-04-15 11:32:03 +02001685{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001686 struct tcpcheck_rule *chk;
1687 const char *err;
Christopher Fauletfc633b62020-11-06 15:24:23 +01001688 int ret = ERR_NONE;
Christopher Faulete5870d82020-04-15 11:32:03 +02001689
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001690 if (!srv->do_agent || !(srv->proxy->cap & PR_CAP_BE))
Christopher Faulet61cc8522020-04-20 14:54:42 +02001691 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001692
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001693 /* If there is no connect rule preceding all send / expect rules, an
Christopher Faulet61cc8522020-04-20 14:54:42 +02001694 * implicit one is inserted before all others.
1695 */
1696 chk = get_first_tcpcheck_rule(srv->agent.tcpcheck_rules);
1697 if (!chk || chk->action != TCPCHK_ACT_CONNECT) {
1698 chk = calloc(1, sizeof(*chk));
1699 if (!chk) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02001700 ha_alert("%s '%s': unable to add implicit tcp-check connect rule"
Christopher Faulet61cc8522020-04-20 14:54:42 +02001701 " to agent-check for server '%s' (out of memory).\n",
1702 proxy_type_str(srv->proxy), srv->proxy->id, srv->id);
1703 ret |= ERR_ALERT | ERR_FATAL;
1704 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001705 }
Christopher Faulet61cc8522020-04-20 14:54:42 +02001706 chk->action = TCPCHK_ACT_CONNECT;
1707 chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT);
Willy Tarreau2b718102021-04-21 07:32:39 +02001708 LIST_INSERT(srv->agent.tcpcheck_rules->list, &chk->list);
Christopher Faulete5870d82020-04-15 11:32:03 +02001709 }
1710
Christopher Faulete5870d82020-04-15 11:32:03 +02001711
Christopher Faulet61cc8522020-04-20 14:54:42 +02001712 err = init_check(&srv->agent, PR_O2_TCPCHK_CHK);
1713 if (err) {
1714 ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n",
1715 proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err);
1716 ret |= ERR_ALERT | ERR_ABORT;
1717 goto out;
Christopher Faulete5870d82020-04-15 11:32:03 +02001718 }
1719
Christopher Faulet61cc8522020-04-20 14:54:42 +02001720 if (!srv->agent.inter)
1721 srv->agent.inter = srv->check.inter;
1722
1723 srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02001724 srv_take(srv);
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001725
1726 /* Only increment maxsock for servers from the configuration. Dynamic
1727 * servers at the moment are not taken into account for the estimation
1728 * of the resources limits.
1729 */
1730 if (global.mode & MODE_STARTING)
1731 global.maxsock++;
Christopher Faulet61cc8522020-04-20 14:54:42 +02001732
1733 out:
1734 return ret;
Christopher Faulete5870d82020-04-15 11:32:03 +02001735}
1736
Christopher Faulet61cc8522020-04-20 14:54:42 +02001737static void deinit_srv_check(struct server *srv)
1738{
1739 if (srv->check.state & CHK_ST_CONFIGURED)
1740 free_check(&srv->check);
1741 srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
1742 srv->do_check = 0;
1743}
Christopher Faulete5870d82020-04-15 11:32:03 +02001744
Christopher Faulet61cc8522020-04-20 14:54:42 +02001745
1746static void deinit_srv_agent_check(struct server *srv)
1747{
Christopher Faulet61cc8522020-04-20 14:54:42 +02001748 if (srv->agent.state & CHK_ST_CONFIGURED)
1749 free_check(&srv->agent);
1750
1751 srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
1752 srv->do_agent = 0;
Christopher Faulete5870d82020-04-15 11:32:03 +02001753}
1754
Willy Tarreaucee013e2020-06-05 11:40:38 +02001755REGISTER_POST_SERVER_CHECK(init_srv_check);
1756REGISTER_POST_SERVER_CHECK(init_srv_agent_check);
Willy Tarreaucee013e2020-06-05 11:40:38 +02001757REGISTER_POST_CHECK(start_checks);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001758
Willy Tarreaucee013e2020-06-05 11:40:38 +02001759REGISTER_SERVER_DEINIT(deinit_srv_check);
1760REGISTER_SERVER_DEINIT(deinit_srv_agent_check);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001761
Christopher Faulet61cc8522020-04-20 14:54:42 +02001762
1763/**************************************************************************/
1764/************************** Check sample fetches **************************/
1765/**************************************************************************/
Christopher Fauletfd6c2292020-03-25 18:20:15 +01001766
Christopher Faulet61cc8522020-04-20 14:54:42 +02001767static struct sample_fetch_kw_list smp_kws = {ILH, {
Christopher Faulet61cc8522020-04-20 14:54:42 +02001768 { /* END */ },
1769}};
1770
1771INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
1772
1773
1774/**************************************************************************/
1775/************************ Check's parsing functions ***********************/
1776/**************************************************************************/
Christopher Fauletce8111e2020-04-06 15:04:11 +02001777/* Parse the "addr" server keyword */
1778static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1779 char **errmsg)
1780{
1781 struct sockaddr_storage *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001782 int port1, port2, err_code = 0;
1783
1784
1785 if (!*args[*cur_arg+1]) {
1786 memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]);
1787 goto error;
1788 }
1789
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001790 sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, NULL, NULL, errmsg, NULL, NULL,
1791 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Christopher Fauletce8111e2020-04-06 15:04:11 +02001792 if (!sk) {
1793 memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg);
1794 goto error;
1795 }
1796
William Dauchy1c921cd2021-02-03 22:30:08 +01001797 srv->check.addr = *sk;
1798 /* if agentaddr was never set, we can use addr */
1799 if (!(srv->flags & SRV_F_AGENTADDR))
1800 srv->agent.addr = *sk;
Christopher Fauletce8111e2020-04-06 15:04:11 +02001801
1802 out:
1803 return err_code;
1804
1805 error:
1806 err_code |= ERR_ALERT | ERR_FATAL;
1807 goto out;
1808}
1809
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001810/* Parse the "agent-addr" server keyword */
1811static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1812 char **errmsg)
1813{
William Dauchy1c921cd2021-02-03 22:30:08 +01001814 struct sockaddr_storage sk;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001815 int err_code = 0;
1816
1817 if (!*(args[*cur_arg+1])) {
1818 memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]);
1819 goto error;
1820 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001821 memset(&sk, 0, sizeof(sk));
1822 if (str2ip(args[*cur_arg + 1], &sk) == NULL) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001823 memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]);
1824 goto error;
1825 }
William Dauchy1c921cd2021-02-03 22:30:08 +01001826 set_srv_agent_addr(srv, &sk);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001827
1828 out:
1829 return err_code;
1830
1831 error:
1832 err_code |= ERR_ALERT | ERR_FATAL;
1833 goto out;
1834}
1835
1836/* Parse the "agent-check" server keyword */
1837static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1838 char **errmsg)
1839{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001840 struct tcpcheck_ruleset *rs = NULL;
1841 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1842 struct tcpcheck_rule *chk;
1843 int err_code = 0;
1844
1845 if (srv->do_agent)
1846 goto out;
1847
Christopher Faulet6ecd5932021-01-12 17:29:45 +01001848 if (!(curpx->cap & PR_CAP_BE)) {
1849 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
1850 args[*cur_arg], proxy_type_str(curpx), curpx->id);
1851 return ERR_WARN;
1852 }
1853
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001854 if (!rules) {
1855 rules = calloc(1, sizeof(*rules));
1856 if (!rules) {
1857 memprintf(errmsg, "out of memory.");
1858 goto error;
1859 }
1860 LIST_INIT(&rules->preset_vars);
1861 srv->agent.tcpcheck_rules = rules;
1862 }
1863 rules->list = NULL;
1864 rules->flags = 0;
1865
Christopher Faulet61cc8522020-04-20 14:54:42 +02001866 rs = find_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001867 if (rs)
1868 goto ruleset_found;
1869
Christopher Faulet61cc8522020-04-20 14:54:42 +02001870 rs = create_tcpcheck_ruleset("*agent-check");
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001871 if (rs == NULL) {
1872 memprintf(errmsg, "out of memory.");
1873 goto error;
1874 }
1875
Christopher Fauletb50b3e62020-05-05 18:43:43 +02001876 chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", "%[var(check.agent_string)]", ""},
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001877 1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg);
1878 if (!chk) {
1879 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1880 goto error;
1881 }
1882 chk->index = 0;
Willy Tarreau2b718102021-04-21 07:32:39 +02001883 LIST_APPEND(&rs->rules, &chk->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001884
1885 chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
Christopher Faulete5870d82020-04-15 11:32:03 +02001886 1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK,
1887 srv->conf.file, srv->conf.line, errmsg);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001888 if (!chk) {
1889 memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg);
1890 goto error;
1891 }
1892 chk->expect.custom = tcpcheck_agent_expect_reply;
1893 chk->index = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02001894 LIST_APPEND(&rs->rules, &chk->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001895
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001896 ruleset_found:
1897 rules->list = &rs->rules;
Christopher Faulet1faf18a2020-11-25 16:43:12 +01001898 rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS);
Christopher Faulet404f9192020-04-09 23:13:54 +02001899 rules->flags |= TCPCHK_RULES_AGENT_CHK;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001900 srv->do_agent = 1;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001901
1902 out:
Dirkjan Bussinkdfee2172021-06-18 19:57:49 +00001903 return err_code;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001904
1905 error:
1906 deinit_srv_agent_check(srv);
Christopher Faulet61cc8522020-04-20 14:54:42 +02001907 free_tcpcheck_ruleset(rs);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001908 err_code |= ERR_ALERT | ERR_FATAL;
1909 goto out;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001910}
1911
1912/* Parse the "agent-inter" server keyword */
1913static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1914 char **errmsg)
1915{
1916 const char *err = NULL;
1917 unsigned int delay;
1918 int err_code = 0;
1919
1920 if (!*(args[*cur_arg+1])) {
1921 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
1922 goto error;
1923 }
1924
1925 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
1926 if (err == PARSE_TIME_OVER) {
1927 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1928 args[*cur_arg+1], args[*cur_arg], srv->id);
1929 goto error;
1930 }
1931 else if (err == PARSE_TIME_UNDER) {
1932 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1933 args[*cur_arg+1], args[*cur_arg], srv->id);
1934 goto error;
1935 }
1936 else if (err) {
1937 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
1938 *err, srv->id);
1939 goto error;
1940 }
1941 if (delay <= 0) {
1942 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
1943 delay, args[*cur_arg], srv->id);
1944 goto error;
1945 }
1946 srv->agent.inter = delay;
1947
1948 out:
1949 return err_code;
1950
1951 error:
1952 err_code |= ERR_ALERT | ERR_FATAL;
1953 goto out;
1954}
1955
1956/* Parse the "agent-port" server keyword */
1957static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
1958 char **errmsg)
1959{
1960 int err_code = 0;
1961
1962 if (!*(args[*cur_arg+1])) {
1963 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
1964 goto error;
1965 }
1966
Amaury Denoyelle403dce82021-07-29 15:39:43 +02001967 /* Only increment maxsock for servers from the configuration. Dynamic
1968 * servers at the moment are not taken into account for the estimation
1969 * of the resources limits.
1970 */
1971 if (global.mode & MODE_STARTING)
1972 global.maxsock++;
1973
William Dauchy4858fb22021-02-03 22:30:09 +01001974 set_srv_agent_port(srv, atol(args[*cur_arg + 1]));
Christopher Fauletcbba66c2020-04-06 14:26:30 +02001975
1976 out:
1977 return err_code;
1978
1979 error:
1980 err_code |= ERR_ALERT | ERR_FATAL;
1981 goto out;
1982}
1983
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001984int set_srv_agent_send(struct server *srv, const char *send)
1985{
1986 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
1987 struct tcpcheck_var *var = NULL;
1988 char *str;
1989
1990 str = strdup(send);
Christopher Fauletb61caf42020-04-21 10:57:42 +02001991 var = create_tcpcheck_var(ist("check.agent_string"));
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001992 if (str == NULL || var == NULL)
1993 goto error;
1994
1995 free_tcpcheck_vars(&rules->preset_vars);
1996
1997 var->data.type = SMP_T_STR;
1998 var->data.u.str.area = str;
1999 var->data.u.str.data = strlen(str);
2000 LIST_INIT(&var->list);
Willy Tarreau2b718102021-04-21 07:32:39 +02002001 LIST_APPEND(&rules->preset_vars, &var->list);
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002002
2003 return 1;
2004
2005 error:
2006 free(str);
2007 free(var);
2008 return 0;
2009}
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002010
2011/* Parse the "agent-send" server keyword */
2012static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2013 char **errmsg)
2014{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002015 struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules;
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002016 int err_code = 0;
2017
2018 if (!*(args[*cur_arg+1])) {
2019 memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]);
2020 goto error;
2021 }
2022
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002023 if (!rules) {
2024 rules = calloc(1, sizeof(*rules));
2025 if (!rules) {
2026 memprintf(errmsg, "out of memory.");
2027 goto error;
2028 }
2029 LIST_INIT(&rules->preset_vars);
2030 srv->agent.tcpcheck_rules = rules;
2031 }
2032
2033 if (!set_srv_agent_send(srv, args[*cur_arg+1])) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002034 memprintf(errmsg, "out of memory.");
2035 goto error;
2036 }
2037
2038 out:
2039 return err_code;
2040
2041 error:
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002042 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002043 err_code |= ERR_ALERT | ERR_FATAL;
2044 goto out;
2045}
2046
2047/* Parse the "no-agent-send" server keyword */
2048static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2049 char **errmsg)
2050{
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002051 deinit_srv_agent_check(srv);
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002052 return 0;
2053}
2054
Christopher Fauletce8111e2020-04-06 15:04:11 +02002055/* Parse the "check" server keyword */
2056static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2057 char **errmsg)
2058{
Christopher Faulet6ecd5932021-01-12 17:29:45 +01002059 if (!(curpx->cap & PR_CAP_BE)) {
2060 memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability",
2061 args[*cur_arg], proxy_type_str(curpx), curpx->id);
2062 return ERR_WARN;
2063 }
2064
Christopher Fauletce8111e2020-04-06 15:04:11 +02002065 srv->do_check = 1;
2066 return 0;
2067}
2068
2069/* Parse the "check-send-proxy" server keyword */
2070static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2071 char **errmsg)
2072{
2073 srv->check.send_proxy = 1;
2074 return 0;
2075}
2076
2077/* Parse the "check-via-socks4" server keyword */
2078static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2079 char **errmsg)
2080{
2081 srv->check.via_socks4 = 1;
2082 return 0;
2083}
2084
2085/* Parse the "no-check" server keyword */
2086static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2087 char **errmsg)
2088{
2089 deinit_srv_check(srv);
2090 return 0;
2091}
2092
2093/* Parse the "no-check-send-proxy" server keyword */
2094static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2095 char **errmsg)
2096{
2097 srv->check.send_proxy = 0;
2098 return 0;
2099}
2100
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002101/* parse the "check-proto" server keyword */
2102static int srv_parse_check_proto(char **args, int *cur_arg,
2103 struct proxy *px, struct server *newsrv, char **err)
2104{
2105 int err_code = 0;
2106
2107 if (!*args[*cur_arg + 1]) {
2108 memprintf(err, "'%s' : missing value", args[*cur_arg]);
2109 goto error;
2110 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002111 newsrv->check.mux_proto = get_mux_proto(ist(args[*cur_arg + 1]));
Christopher Fauletedc6ed92020-04-23 16:27:59 +02002112 if (!newsrv->check.mux_proto) {
2113 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
2114 goto error;
2115 }
2116
2117 out:
2118 return err_code;
2119
2120 error:
2121 err_code |= ERR_ALERT | ERR_FATAL;
2122 goto out;
2123}
2124
2125
Christopher Fauletce8111e2020-04-06 15:04:11 +02002126/* Parse the "rise" server keyword */
2127static int srv_parse_check_rise(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2128 char **errmsg)
2129{
2130 int err_code = 0;
2131
2132 if (!*args[*cur_arg + 1]) {
2133 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2134 goto error;
2135 }
2136
2137 srv->check.rise = atol(args[*cur_arg+1]);
2138 if (srv->check.rise <= 0) {
2139 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2140 goto error;
2141 }
2142
2143 if (srv->check.health)
2144 srv->check.health = srv->check.rise;
2145
2146 out:
2147 return err_code;
2148
2149 error:
2150 deinit_srv_agent_check(srv);
2151 err_code |= ERR_ALERT | ERR_FATAL;
2152 goto out;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002153}
2154
2155/* Parse the "fall" server keyword */
2156static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2157 char **errmsg)
2158{
2159 int err_code = 0;
2160
2161 if (!*args[*cur_arg + 1]) {
2162 memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]);
2163 goto error;
2164 }
2165
2166 srv->check.fall = atol(args[*cur_arg+1]);
2167 if (srv->check.fall <= 0) {
2168 memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]);
2169 goto error;
2170 }
2171
2172 out:
2173 return err_code;
2174
2175 error:
2176 deinit_srv_agent_check(srv);
2177 err_code |= ERR_ALERT | ERR_FATAL;
2178 goto out;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002179}
2180
2181/* Parse the "inter" server keyword */
2182static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2183 char **errmsg)
2184{
2185 const char *err = NULL;
2186 unsigned int delay;
2187 int err_code = 0;
2188
2189 if (!*(args[*cur_arg+1])) {
2190 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2191 goto error;
2192 }
2193
2194 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2195 if (err == PARSE_TIME_OVER) {
2196 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2197 args[*cur_arg+1], args[*cur_arg], srv->id);
2198 goto error;
2199 }
2200 else if (err == PARSE_TIME_UNDER) {
2201 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2202 args[*cur_arg+1], args[*cur_arg], srv->id);
2203 goto error;
2204 }
2205 else if (err) {
2206 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2207 *err, srv->id);
2208 goto error;
2209 }
2210 if (delay <= 0) {
2211 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2212 delay, args[*cur_arg], srv->id);
2213 goto error;
2214 }
2215 srv->check.inter = delay;
2216
2217 out:
2218 return err_code;
2219
2220 error:
2221 err_code |= ERR_ALERT | ERR_FATAL;
2222 goto out;
2223}
2224
2225
2226/* Parse the "fastinter" server keyword */
2227static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2228 char **errmsg)
2229{
2230 const char *err = NULL;
2231 unsigned int delay;
2232 int err_code = 0;
2233
2234 if (!*(args[*cur_arg+1])) {
2235 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2236 goto error;
2237 }
2238
2239 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2240 if (err == PARSE_TIME_OVER) {
2241 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2242 args[*cur_arg+1], args[*cur_arg], srv->id);
2243 goto error;
2244 }
2245 else if (err == PARSE_TIME_UNDER) {
2246 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2247 args[*cur_arg+1], args[*cur_arg], srv->id);
2248 goto error;
2249 }
2250 else if (err) {
2251 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2252 *err, srv->id);
2253 goto error;
2254 }
2255 if (delay <= 0) {
2256 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2257 delay, args[*cur_arg], srv->id);
2258 goto error;
2259 }
2260 srv->check.fastinter = delay;
2261
2262 out:
2263 return err_code;
2264
2265 error:
2266 err_code |= ERR_ALERT | ERR_FATAL;
2267 goto out;
2268}
2269
2270
2271/* Parse the "downinter" server keyword */
2272static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2273 char **errmsg)
2274{
2275 const char *err = NULL;
2276 unsigned int delay;
2277 int err_code = 0;
2278
2279 if (!*(args[*cur_arg+1])) {
2280 memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]);
2281 goto error;
2282 }
2283
2284 err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS);
2285 if (err == PARSE_TIME_OVER) {
2286 memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
2287 args[*cur_arg+1], args[*cur_arg], srv->id);
2288 goto error;
2289 }
2290 else if (err == PARSE_TIME_UNDER) {
2291 memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
2292 args[*cur_arg+1], args[*cur_arg], srv->id);
2293 goto error;
2294 }
2295 else if (err) {
2296 memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.",
2297 *err, srv->id);
2298 goto error;
2299 }
2300 if (delay <= 0) {
2301 memprintf(errmsg, "invalid value %d for argument '%s' of server %s.",
2302 delay, args[*cur_arg], srv->id);
2303 goto error;
2304 }
2305 srv->check.downinter = delay;
2306
2307 out:
2308 return err_code;
2309
2310 error:
2311 err_code |= ERR_ALERT | ERR_FATAL;
2312 goto out;
2313}
2314
2315/* Parse the "port" server keyword */
2316static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv,
2317 char **errmsg)
2318{
2319 int err_code = 0;
2320
2321 if (!*(args[*cur_arg+1])) {
2322 memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]);
2323 goto error;
2324 }
2325
Amaury Denoyelle403dce82021-07-29 15:39:43 +02002326 /* Only increment maxsock for servers from the configuration. Dynamic
2327 * servers at the moment are not taken into account for the estimation
2328 * of the resources limits.
2329 */
2330 if (global.mode & MODE_STARTING)
2331 global.maxsock++;
2332
Christopher Fauletce8111e2020-04-06 15:04:11 +02002333 srv->check.port = atol(args[*cur_arg+1]);
William Dauchy4858fb22021-02-03 22:30:09 +01002334 /* if agentport was never set, we can use port */
2335 if (!(srv->flags & SRV_F_AGENTPORT))
2336 srv->agent.port = srv->check.port;
Christopher Fauletce8111e2020-04-06 15:04:11 +02002337
2338 out:
2339 return err_code;
2340
2341 error:
2342 err_code |= ERR_ALERT | ERR_FATAL;
2343 goto out;
2344}
2345
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002346static struct srv_kw_list srv_kws = { "CHK", { }, {
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002347 { "addr", srv_parse_addr, 1, 1, 1 }, /* IP address to send health to or to probe from agent-check */
2348 { "agent-addr", srv_parse_agent_addr, 1, 1, 1 }, /* Enable an auxiliary agent check */
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02002349 { "agent-check", srv_parse_agent_check, 0, 1, 1 }, /* Enable agent checks */
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002350 { "agent-inter", srv_parse_agent_inter, 1, 1, 1 }, /* Set the interval between two agent checks */
2351 { "agent-port", srv_parse_agent_port, 1, 1, 1 }, /* Set the TCP port used for agent checks. */
2352 { "agent-send", srv_parse_agent_send, 1, 1, 1 }, /* Set string to send to agent. */
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02002353 { "check", srv_parse_check, 0, 1, 1 }, /* Enable health checks */
Amaury Denoyelle9ecee0f2021-07-23 16:34:58 +02002354 { "check-proto", srv_parse_check_proto, 1, 1, 1 }, /* Set the mux protocol for health checks */
2355 { "check-send-proxy", srv_parse_check_send_proxy, 0, 1, 1 }, /* Enable PROXY protocol for health checks */
2356 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1, 1 }, /* Enable socks4 proxy for health checks */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002357 { "no-agent-check", srv_parse_no_agent_check, 0, 1, 0 }, /* Do not enable any auxiliary agent check */
2358 { "no-check", srv_parse_no_check, 0, 1, 0 }, /* Disable health checks */
2359 { "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 +02002360 { "rise", srv_parse_check_rise, 1, 1, 1 }, /* Set rise value for health checks */
2361 { "fall", srv_parse_check_fall, 1, 1, 1 }, /* Set fall value for health checks */
2362 { "inter", srv_parse_check_inter, 1, 1, 1 }, /* Set inter value for health checks */
2363 { "fastinter", srv_parse_check_fastinter, 1, 1, 1 }, /* Set fastinter value for health checks */
2364 { "downinter", srv_parse_check_downinter, 1, 1, 1 }, /* Set downinter value for health checks */
2365 { "port", srv_parse_check_port, 1, 1, 1 }, /* Set the TCP port used for health checks. */
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002366 { NULL, NULL, 0 },
2367}};
2368
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002369INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Christopher Fauletfd6c2292020-03-25 18:20:15 +01002370
Willy Tarreaubd741542010-03-16 18:46:54 +01002371/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02002372 * Local variables:
2373 * c-indent-level: 8
2374 * c-basic-offset: 8
2375 * End:
2376 */