Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Health-checks functions. |
| 3 | * |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 4 | * Copyright 2000-2009 Willy Tarreau <w@1wt.eu> |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 5 | * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6 | * |
| 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 Tarreau | b881608 | 2008-01-18 12:18:15 +0100 | [diff] [blame] | 14 | #include <assert.h> |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 15 | #include <ctype.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 16 | #include <errno.h> |
| 17 | #include <fcntl.h> |
Simon Horman | 0ba0e4a | 2015-01-30 11:23:00 +0900 | [diff] [blame] | 18 | #include <stdarg.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 20 | #include <stdlib.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 21 | #include <string.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 22 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 23 | #include <unistd.h> |
Willy Tarreau | 9f6dc72 | 2019-03-01 11:15:10 +0100 | [diff] [blame] | 24 | #include <sys/resource.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 25 | #include <sys/socket.h> |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 26 | #include <sys/types.h> |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 27 | #include <sys/wait.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 28 | #include <netinet/in.h> |
Willy Tarreau | 1274bc4 | 2009-07-15 07:16:31 +0200 | [diff] [blame] | 29 | #include <netinet/tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | #include <arpa/inet.h> |
| 31 | |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 32 | #include <haproxy/action.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 33 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 34 | #include <haproxy/arg.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 35 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 4aa573d | 2020-06-04 18:21:56 +0200 | [diff] [blame] | 36 | #include <haproxy/check.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 37 | #include <haproxy/chunk.h> |
Willy Tarreau | 7c18b54 | 2020-06-11 09:23:02 +0200 | [diff] [blame] | 38 | #include <haproxy/dgram.h> |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 39 | #include <haproxy/dynbuf-t.h> |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 40 | #include <haproxy/extcheck.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 41 | #include <haproxy/fd.h> |
| 42 | #include <haproxy/global.h> |
| 43 | #include <haproxy/h1.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 44 | #include <haproxy/http.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 45 | #include <haproxy/http_htx.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 46 | #include <haproxy/htx.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 47 | #include <haproxy/istbuf.h> |
| 48 | #include <haproxy/list.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 49 | #include <haproxy/log.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 50 | #include <haproxy/mailers.h> |
| 51 | #include <haproxy/port_range.h> |
| 52 | #include <haproxy/proto_tcp.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 53 | #include <haproxy/protocol.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 54 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 55 | #include <haproxy/queue.h> |
| 56 | #include <haproxy/regex.h> |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 57 | #include <haproxy/resolvers.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 58 | #include <haproxy/sample.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 59 | #include <haproxy/server.h> |
Willy Tarreau | 209108d | 2020-06-04 20:30:20 +0200 | [diff] [blame] | 60 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 61 | #include <haproxy/stats-t.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 62 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 63 | #include <haproxy/task.h> |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 64 | #include <haproxy/tcpcheck.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 65 | #include <haproxy/thread.h> |
| 66 | #include <haproxy/time.h> |
| 67 | #include <haproxy/tools.h> |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 68 | #include <haproxy/trace.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 69 | #include <haproxy/vars.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 71 | /* trace source and events */ |
| 72 | static void check_trace(enum trace_level level, uint64_t mask, |
| 73 | const struct trace_source *src, |
| 74 | const struct ist where, const struct ist func, |
| 75 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 76 | |
| 77 | /* The event representation is split like this : |
| 78 | * check - check |
| 79 | * |
| 80 | * CHECK_EV_* macros are defined in <haproxy/check.h> |
| 81 | */ |
| 82 | static const struct trace_event check_trace_events[] = { |
| 83 | { .mask = CHK_EV_TASK_WAKE, .name = "task_wake", .desc = "Check task woken up" }, |
| 84 | { .mask = CHK_EV_HCHK_START, .name = "hchck_start", .desc = "Health-check started" }, |
| 85 | { .mask = CHK_EV_HCHK_WAKE, .name = "hchck_wake", .desc = "Health-check woken up" }, |
| 86 | { .mask = CHK_EV_HCHK_RUN, .name = "hchck_run", .desc = "Health-check running" }, |
| 87 | { .mask = CHK_EV_HCHK_END, .name = "hchck_end", .desc = "Health-check terminated" }, |
| 88 | { .mask = CHK_EV_HCHK_SUCC, .name = "hchck_succ", .desc = "Health-check success" }, |
| 89 | { .mask = CHK_EV_HCHK_ERR, .name = "hchck_err", .desc = "Health-check failure" }, |
| 90 | |
| 91 | { .mask = CHK_EV_TCPCHK_EVAL, .name = "tcp_check_eval", .desc = "tcp-check rules evaluation" }, |
| 92 | { .mask = CHK_EV_TCPCHK_ERR, .name = "tcp_check_err", .desc = "tcp-check evaluation error" }, |
| 93 | { .mask = CHK_EV_TCPCHK_CONN, .name = "tcp_check_conn", .desc = "tcp-check connection rule" }, |
| 94 | { .mask = CHK_EV_TCPCHK_SND, .name = "tcp_check_send", .desc = "tcp-check send rule" }, |
| 95 | { .mask = CHK_EV_TCPCHK_EXP, .name = "tcp_check_expect", .desc = "tcp-check expect rule" }, |
| 96 | { .mask = CHK_EV_TCPCHK_ACT, .name = "tcp_check_action", .desc = "tcp-check action rule" }, |
| 97 | |
| 98 | { .mask = CHK_EV_RX_DATA, .name = "rx_data", .desc = "receipt of data" }, |
| 99 | { .mask = CHK_EV_RX_BLK, .name = "rx_blk", .desc = "receipt blocked" }, |
| 100 | { .mask = CHK_EV_RX_ERR, .name = "rx_err", .desc = "receipt error" }, |
| 101 | |
| 102 | { .mask = CHK_EV_TX_DATA, .name = "tx_data", .desc = "transmission of data" }, |
| 103 | { .mask = CHK_EV_TX_BLK, .name = "tx_blk", .desc = "transmission blocked" }, |
| 104 | { .mask = CHK_EV_TX_ERR, .name = "tx_err", .desc = "transmission error" }, |
| 105 | |
| 106 | {} |
| 107 | }; |
| 108 | |
| 109 | static const struct name_desc check_trace_lockon_args[4] = { |
| 110 | /* arg1 */ { /* already used by the check */ }, |
| 111 | /* arg2 */ { }, |
| 112 | /* arg3 */ { }, |
| 113 | /* arg4 */ { } |
| 114 | }; |
| 115 | |
| 116 | static const struct name_desc check_trace_decoding[] = { |
| 117 | #define CHK_VERB_CLEAN 1 |
| 118 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 119 | #define CHK_VERB_MINIMAL 2 |
| 120 | { .name="minimal", .desc="report info on stream and stream-interfaces" }, |
| 121 | #define CHK_VERB_SIMPLE 3 |
| 122 | { .name="simple", .desc="add info on request and response channels" }, |
| 123 | #define CHK_VERB_ADVANCED 4 |
| 124 | { .name="advanced", .desc="add info on channel's buffer for data and developer levels only" }, |
| 125 | #define CHK_VERB_COMPLETE 5 |
| 126 | { .name="complete", .desc="add info on channel's buffer" }, |
| 127 | { /* end */ } |
| 128 | }; |
| 129 | |
| 130 | struct trace_source trace_check = { |
| 131 | .name = IST("check"), |
| 132 | .desc = "Health-check", |
| 133 | .arg_def = TRC_ARG1_CHK, // TRACE()'s first argument is always a stream |
| 134 | .default_cb = check_trace, |
| 135 | .known_events = check_trace_events, |
| 136 | .lockon_args = check_trace_lockon_args, |
| 137 | .decoding = check_trace_decoding, |
| 138 | .report_events = ~0, // report everything by default |
| 139 | }; |
| 140 | |
| 141 | #define TRACE_SOURCE &trace_check |
| 142 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 143 | |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 144 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 145 | static int wake_srv_chk(struct conn_stream *cs); |
| 146 | struct data_cb check_conn_cb = { |
| 147 | .wake = wake_srv_chk, |
| 148 | .name = "CHCK", |
| 149 | }; |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 150 | |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 151 | |
Gaetan Rivet | 05d692d | 2020-02-14 17:42:54 +0100 | [diff] [blame] | 152 | /* Dummy frontend used to create all checks sessions. */ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 153 | struct proxy checks_fe; |
Christopher Faulet | 31dff9b | 2017-10-23 15:45:20 +0200 | [diff] [blame] | 154 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 155 | |
| 156 | static inline void check_trace_buf(const struct buffer *buf, size_t ofs, size_t len) |
| 157 | { |
| 158 | size_t block1, block2; |
| 159 | int line, ptr, newptr; |
| 160 | |
| 161 | block1 = b_contig_data(buf, ofs); |
| 162 | block2 = 0; |
| 163 | if (block1 > len) |
| 164 | block1 = len; |
| 165 | block2 = len - block1; |
| 166 | |
| 167 | ofs = b_peek_ofs(buf, ofs); |
| 168 | |
| 169 | line = 0; |
| 170 | ptr = ofs; |
| 171 | while (ptr < ofs + block1) { |
| 172 | newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), ofs + block1, &line, ptr); |
| 173 | if (newptr == ptr) |
| 174 | break; |
| 175 | ptr = newptr; |
| 176 | } |
| 177 | |
| 178 | line = ptr = 0; |
| 179 | while (ptr < block2) { |
| 180 | newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), block2, &line, ptr); |
| 181 | if (newptr == ptr) |
| 182 | break; |
| 183 | ptr = newptr; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /* trace source and events */ |
| 188 | static void check_trace(enum trace_level level, uint64_t mask, |
| 189 | const struct trace_source *src, |
| 190 | const struct ist where, const struct ist func, |
| 191 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 192 | { |
| 193 | const struct check *check = a1; |
| 194 | const struct server *srv = (check ? check->server : NULL); |
| 195 | const size_t *val = a4; |
| 196 | const char *res; |
| 197 | |
| 198 | if (!check || src->verbosity < CHK_VERB_CLEAN) |
| 199 | return; |
| 200 | |
Christopher Faulet | 5f76c90 | 2022-06-08 09:12:58 +0200 | [diff] [blame] | 201 | if (srv) { |
| 202 | chunk_appendf(&trace_buf, " : [%c] SRV=%s", |
| 203 | ((check->type == PR_O2_EXT_CHK) ? 'E' : (check->state & CHK_ST_AGENT ? 'A' : 'H')), |
| 204 | srv->id); |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 205 | |
Christopher Faulet | 5f76c90 | 2022-06-08 09:12:58 +0200 | [diff] [blame] | 206 | chunk_appendf(&trace_buf, " status=%d/%d %s", |
| 207 | (check->health >= check->rise) ? check->health - check->rise + 1 : check->health, |
| 208 | (check->health >= check->rise) ? check->fall : check->rise, |
| 209 | (check->health >= check->rise) ? (srv->uweight ? "UP" : "DRAIN") : "DOWN"); |
| 210 | } |
| 211 | else |
| 212 | chunk_appendf(&trace_buf, " : [EMAIL]"); |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 213 | |
| 214 | switch (check->result) { |
| 215 | case CHK_RES_NEUTRAL: res = "-"; break; |
| 216 | case CHK_RES_FAILED: res = "FAIL"; break; |
| 217 | case CHK_RES_PASSED: res = "PASS"; break; |
| 218 | case CHK_RES_CONDPASS: res = "COND"; break; |
| 219 | default: res = "UNK"; break; |
| 220 | } |
| 221 | |
| 222 | if (src->verbosity == CHK_VERB_CLEAN) |
| 223 | return; |
| 224 | |
| 225 | chunk_appendf(&trace_buf, " - last=%s(%d)/%s(%d)", |
| 226 | get_check_status_info(check->status), check->status, |
| 227 | res, check->result); |
| 228 | |
| 229 | /* Display the value to the 4th argument (level > STATE) */ |
| 230 | if (src->level > TRACE_LEVEL_STATE && val) |
| 231 | chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val); |
| 232 | |
| 233 | chunk_appendf(&trace_buf, " check=%p(0x%08x)", check, check->state); |
| 234 | |
| 235 | if (src->verbosity == CHK_VERB_MINIMAL) |
| 236 | return; |
| 237 | |
| 238 | |
| 239 | if (check->cs) { |
| 240 | chunk_appendf(&trace_buf, " - conn=%p(0x%08x)", check->cs->conn, check->cs->conn->flags); |
| 241 | chunk_appendf(&trace_buf, " cs=%p(0x%08x)", check->cs, check->cs->flags); |
| 242 | } |
| 243 | |
| 244 | if (mask & CHK_EV_TCPCHK) { |
| 245 | const char *type; |
| 246 | |
| 247 | switch (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) { |
| 248 | case TCPCHK_RULES_PGSQL_CHK: type = "PGSQL"; break; |
| 249 | case TCPCHK_RULES_REDIS_CHK: type = "REDIS"; break; |
| 250 | case TCPCHK_RULES_SMTP_CHK: type = "SMTP"; break; |
| 251 | case TCPCHK_RULES_HTTP_CHK: type = "HTTP"; break; |
| 252 | case TCPCHK_RULES_MYSQL_CHK: type = "MYSQL"; break; |
| 253 | case TCPCHK_RULES_LDAP_CHK: type = "LDAP"; break; |
| 254 | case TCPCHK_RULES_SSL3_CHK: type = "SSL3"; break; |
| 255 | case TCPCHK_RULES_AGENT_CHK: type = "AGENT"; break; |
| 256 | case TCPCHK_RULES_SPOP_CHK: type = "SPOP"; break; |
| 257 | case TCPCHK_RULES_TCP_CHK: type = "TCP"; break; |
| 258 | default: type = "???"; break; |
| 259 | } |
| 260 | if (check->current_step) |
| 261 | chunk_appendf(&trace_buf, " - tcp-check=(%s,%d)", type, tcpcheck_get_step_id(check, NULL)); |
| 262 | else |
| 263 | chunk_appendf(&trace_buf, " - tcp-check=(%s,-)", type); |
| 264 | } |
| 265 | |
| 266 | /* Display bi and bo buffer info (level > USER & verbosity > SIMPLE) */ |
| 267 | if (src->level > TRACE_LEVEL_USER) { |
| 268 | const struct buffer *buf = NULL; |
| 269 | |
| 270 | chunk_appendf(&trace_buf, " bi=%u@%p+%u/%u", |
| 271 | (unsigned int)b_data(&check->bi), b_orig(&check->bi), |
| 272 | (unsigned int)b_head_ofs(&check->bi), (unsigned int)b_size(&check->bi)); |
| 273 | chunk_appendf(&trace_buf, " bo=%u@%p+%u/%u", |
| 274 | (unsigned int)b_data(&check->bo), b_orig(&check->bo), |
| 275 | (unsigned int)b_head_ofs(&check->bo), (unsigned int)b_size(&check->bo)); |
| 276 | |
| 277 | if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_RX))) |
| 278 | buf = (b_is_null(&check->bi) ? NULL : &check->bi); |
| 279 | else if (src->verbosity >= CHK_VERB_ADVANCED && (mask & (CHK_EV_TX))) |
| 280 | buf = (b_is_null(&check->bo) ? NULL : &check->bo); |
| 281 | |
| 282 | if (buf) { |
| 283 | if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK) { |
| 284 | int full = (src->verbosity == CHK_VERB_COMPLETE); |
| 285 | |
| 286 | chunk_memcat(&trace_buf, "\n\t", 2); |
| 287 | htx_dump(&trace_buf, htxbuf(buf), full); |
| 288 | } |
| 289 | else { |
| 290 | int max = ((src->verbosity == CHK_VERB_COMPLETE) ? 1024 : 256); |
| 291 | |
| 292 | chunk_memcat(&trace_buf, "\n", 1); |
| 293 | if (b_data(buf) > max) { |
| 294 | check_trace_buf(buf, 0, max); |
| 295 | chunk_memcat(&trace_buf, " ...\n", 6); |
| 296 | } |
| 297 | else |
| 298 | check_trace_buf(buf, 0, b_data(buf)); |
| 299 | } |
| 300 | |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | } |
| 305 | |
| 306 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 307 | /**************************************************************************/ |
| 308 | /************************ Handle check results ****************************/ |
| 309 | /**************************************************************************/ |
| 310 | struct check_status { |
| 311 | short result; /* one of SRV_CHK_* */ |
| 312 | char *info; /* human readable short info */ |
| 313 | char *desc; /* long description */ |
| 314 | }; |
| 315 | |
| 316 | struct analyze_status { |
| 317 | char *desc; /* description */ |
| 318 | unsigned char lr[HANA_OBS_SIZE]; /* result for l4/l7: 0 = ignore, 1 - error, 2 - OK */ |
| 319 | }; |
| 320 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 321 | static const struct check_status check_statuses[HCHK_STATUS_SIZE] = { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 322 | [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" }, |
| 323 | [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" }, |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 324 | [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 325 | |
Willy Tarreau | 2396418 | 2014-05-20 20:56:30 +0200 | [diff] [blame] | 326 | /* Below we have finished checks */ |
| 327 | [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" }, |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 328 | [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" }, |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 329 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 330 | [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 331 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 332 | [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" }, |
| 333 | [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" }, |
| 334 | [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 335 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 336 | [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" }, |
| 337 | [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" }, |
| 338 | [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 339 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 340 | [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" }, |
| 341 | [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 342 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 343 | [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 344 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 345 | [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" }, |
| 346 | [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" }, |
| 347 | [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" }, |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 348 | |
| 349 | [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" }, |
| 350 | [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" }, |
Cyril Bonté | 77010d8 | 2014-08-07 01:55:37 +0200 | [diff] [blame] | 351 | [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 352 | }; |
| 353 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 354 | static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */ |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 355 | [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }}, |
| 356 | |
| 357 | [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }}, |
| 358 | [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }}, |
| 359 | |
| 360 | [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }}, |
| 361 | [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }}, |
| 362 | [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }}, |
| 363 | [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }}, |
| 364 | |
| 365 | [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }}, |
| 366 | [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }}, |
| 367 | [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }}, |
| 368 | }; |
| 369 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 370 | /* checks if <err> is a real error for errno or one that can be ignored, and |
| 371 | * return 0 for these ones or <err> for real ones. |
| 372 | */ |
| 373 | static inline int unclean_errno(int err) |
| 374 | { |
| 375 | if (err == EAGAIN || err == EINPROGRESS || |
| 376 | err == EISCONN || err == EALREADY) |
| 377 | return 0; |
| 378 | return err; |
| 379 | } |
| 380 | |
Christopher Faulet | 7aa3271 | 2021-02-01 13:11:50 +0100 | [diff] [blame] | 381 | /* Converts check_status code to result code */ |
| 382 | short get_check_status_result(short check_status) |
| 383 | { |
| 384 | if (check_status < HCHK_STATUS_SIZE) |
| 385 | return check_statuses[check_status].result; |
| 386 | else |
| 387 | return check_statuses[HCHK_STATUS_UNKNOWN].result; |
| 388 | } |
| 389 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 390 | /* Converts check_status code to description */ |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 391 | const char *get_check_status_description(short check_status) { |
| 392 | |
| 393 | const char *desc; |
| 394 | |
| 395 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 396 | desc = check_statuses[check_status].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 397 | else |
| 398 | desc = NULL; |
| 399 | |
| 400 | if (desc && *desc) |
| 401 | return desc; |
| 402 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 403 | return check_statuses[HCHK_STATUS_UNKNOWN].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 406 | /* Converts check_status code to short info */ |
William Dauchy | b26122b | 2021-02-14 22:26:23 +0100 | [diff] [blame] | 407 | const char *get_check_status_info(short check_status) |
| 408 | { |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 409 | const char *info; |
| 410 | |
| 411 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 412 | info = check_statuses[check_status].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 413 | else |
| 414 | info = NULL; |
| 415 | |
| 416 | if (info && *info) |
| 417 | return info; |
| 418 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 419 | return check_statuses[HCHK_STATUS_UNKNOWN].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 420 | } |
| 421 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 422 | /* Convert analyze_status to description */ |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 423 | const char *get_analyze_status(short analyze_status) { |
| 424 | |
| 425 | const char *desc; |
| 426 | |
| 427 | if (analyze_status < HANA_STATUS_SIZE) |
| 428 | desc = analyze_statuses[analyze_status].desc; |
| 429 | else |
| 430 | desc = NULL; |
| 431 | |
| 432 | if (desc && *desc) |
| 433 | return desc; |
| 434 | else |
| 435 | return analyze_statuses[HANA_STATUS_UNKNOWN].desc; |
| 436 | } |
| 437 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 438 | /* Sets check->status, update check->duration and fill check->result with an |
| 439 | * adequate CHK_RES_* value. The new check->health is computed based on the |
| 440 | * result. |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 441 | * |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 442 | * Shows information in logs about failed health check if server is UP or |
| 443 | * succeeded health checks if server is DOWN. |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 444 | */ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 445 | void set_server_check_status(struct check *check, short status, const char *desc) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 446 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 447 | struct server *s = check->server; |
Willy Tarreau | bef1b32 | 2014-05-13 21:01:39 +0200 | [diff] [blame] | 448 | short prev_status = check->status; |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 449 | int report = 0; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 450 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 451 | TRACE_POINT(CHK_EV_HCHK_RUN, check); |
| 452 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 453 | if (status == HCHK_STATUS_START) { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 454 | check->result = CHK_RES_UNKNOWN; /* no result yet */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 455 | check->desc[0] = '\0'; |
| 456 | check->start = now; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 457 | return; |
| 458 | } |
| 459 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 460 | if (!check->status) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 461 | return; |
| 462 | |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 463 | if (desc && *desc) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 464 | strncpy(check->desc, desc, HCHK_DESC_LEN-1); |
| 465 | check->desc[HCHK_DESC_LEN-1] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 466 | } else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 467 | check->desc[0] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 468 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 469 | check->status = status; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 470 | if (check_statuses[status].result) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 471 | check->result = check_statuses[status].result; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 472 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 473 | if (status == HCHK_STATUS_HANA) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 474 | check->duration = -1; |
| 475 | else if (!tv_iszero(&check->start)) { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 476 | /* set_server_check_status() may be called more than once */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 477 | check->duration = tv_ms_elapsed(&check->start, &now); |
| 478 | tv_zero(&check->start); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 479 | } |
| 480 | |
Willy Tarreau | 2396418 | 2014-05-20 20:56:30 +0200 | [diff] [blame] | 481 | /* no change is expected if no state change occurred */ |
| 482 | if (check->result == CHK_RES_NEUTRAL) |
| 483 | return; |
| 484 | |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 485 | /* If the check was really just sending a mail, it won't have an |
| 486 | * associated server, so we're done now. |
| 487 | */ |
| 488 | if (!s) |
| 489 | return; |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 490 | report = 0; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 491 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 492 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 493 | switch (check->result) { |
| 494 | case CHK_RES_FAILED: |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 495 | /* Failure to connect to the agent as a secondary check should not |
| 496 | * cause the server to be marked down. |
| 497 | */ |
| 498 | if ((!(check->state & CHK_ST_AGENT) || |
Simon Horman | eaabd52 | 2015-02-26 11:26:17 +0900 | [diff] [blame] | 499 | (check->status >= HCHK_STATUS_L57DATA)) && |
Christopher Faulet | b119a79 | 2018-05-02 12:12:45 +0200 | [diff] [blame] | 500 | (check->health > 0)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 501 | _HA_ATOMIC_INC(&s->counters.failed_checks); |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 502 | report = 1; |
| 503 | check->health--; |
| 504 | if (check->health < check->rise) |
| 505 | check->health = 0; |
| 506 | } |
| 507 | break; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 508 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 509 | case CHK_RES_PASSED: |
Christopher Faulet | 1e527cb | 2020-11-20 18:13:02 +0100 | [diff] [blame] | 510 | case CHK_RES_CONDPASS: |
| 511 | if (check->health < check->rise + check->fall - 1) { |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 512 | report = 1; |
| 513 | check->health++; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 514 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 515 | if (check->health >= check->rise) |
| 516 | check->health = check->rise + check->fall - 1; /* OK now */ |
| 517 | } |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 518 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 519 | /* clear consecutive_errors if observing is enabled */ |
| 520 | if (s->onerror) |
| 521 | s->consecutive_errors = 0; |
| 522 | break; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 523 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 524 | default: |
| 525 | break; |
| 526 | } |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 527 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 528 | if (s->proxy->options2 & PR_O2_LOGHCHKS && |
| 529 | (status != prev_status || report)) { |
| 530 | chunk_printf(&trash, |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 531 | "%s check for %sserver %s/%s %s%s", |
| 532 | (check->state & CHK_ST_AGENT) ? "Agent" : "Health", |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 533 | s->flags & SRV_F_BACKUP ? "backup " : "", |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 534 | s->proxy->id, s->id, |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 535 | (check->result == CHK_RES_CONDPASS) ? "conditionally ":"", |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 536 | (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 537 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 538 | srv_append_status(&trash, s, check, -1, 0); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 539 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 540 | chunk_appendf(&trash, ", status: %d/%d %s", |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 541 | (check->health >= check->rise) ? check->health - check->rise + 1 : check->health, |
| 542 | (check->health >= check->rise) ? check->fall : check->rise, |
| 543 | (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 544 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 545 | ha_warning("%s.\n", trash.area); |
| 546 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
| 547 | send_email_alert(s, LOG_INFO, "%s", trash.area); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 548 | } |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 549 | } |
| 550 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 551 | /* Marks the check <check>'s server down if the current check is already failed |
| 552 | * and the server is not down yet nor in maintenance. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 553 | */ |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 554 | void check_notify_failure(struct check *check) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 555 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 556 | struct server *s = check->server; |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 557 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 558 | /* The agent secondary check should only cause a server to be marked |
| 559 | * as down if check->status is HCHK_STATUS_L7STS, which indicates |
| 560 | * that the agent returned "fail", "stopped" or "down". |
| 561 | * The implication here is that failure to connect to the agent |
| 562 | * as a secondary check should not cause the server to be marked |
| 563 | * down. */ |
| 564 | if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS) |
| 565 | return; |
| 566 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 567 | if (check->health > 0) |
| 568 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 569 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 570 | TRACE_STATE("health-check failed, set server DOWN", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 571 | /* We only report a reason for the check if we did not do so previously */ |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 572 | srv_set_stopped(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 573 | } |
| 574 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 575 | /* Marks the check <check> as valid and tries to set its server up, provided |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 576 | * it isn't in maintenance, it is not tracking a down server and other checks |
| 577 | * comply. The rule is simple : by default, a server is up, unless any of the |
| 578 | * following conditions is true : |
| 579 | * - health check failed (check->health < rise) |
| 580 | * - agent check failed (agent->health < rise) |
| 581 | * - the server tracks a down server (track && track->state == STOPPED) |
| 582 | * Note that if the server has a slowstart, it will switch to STARTING instead |
| 583 | * of RUNNING. Also, only the health checks support the nolb mode, so the |
| 584 | * agent's success may not take the server out of this mode. |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 585 | */ |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 586 | void check_notify_success(struct check *check) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 587 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 588 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 589 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 590 | if (s->next_admin & SRV_ADMF_MAINT) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 591 | return; |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 592 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 593 | if (s->track && s->track->next_state == SRV_ST_STOPPED) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 594 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 595 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 596 | if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise)) |
| 597 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 598 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 599 | if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise)) |
| 600 | return; |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 601 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 602 | if ((check->state & CHK_ST_AGENT) && s->next_state == SRV_ST_STOPPING) |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 603 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 604 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 605 | TRACE_STATE("health-check succeeded, set server RUNNING", CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 606 | srv_set_running(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 607 | } |
| 608 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 609 | /* Marks the check <check> as valid and tries to set its server into stopping mode |
| 610 | * if it was running or starting, and provided it isn't in maintenance and other |
| 611 | * checks comply. The conditions for the server to be marked in stopping mode are |
| 612 | * the same as for it to be turned up. Also, only the health checks support the |
| 613 | * nolb mode. |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 614 | */ |
Willy Tarreau | bcc6733 | 2020-06-05 15:31:31 +0200 | [diff] [blame] | 615 | void check_notify_stopping(struct check *check) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 616 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 617 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 618 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 619 | if (s->next_admin & SRV_ADMF_MAINT) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 620 | return; |
| 621 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 622 | if (check->state & CHK_ST_AGENT) |
| 623 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 624 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 625 | if (s->track && s->track->next_state == SRV_ST_STOPPED) |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 626 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 627 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 628 | if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise)) |
| 629 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 630 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 631 | if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise)) |
| 632 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 633 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 634 | TRACE_STATE("health-check condionnaly succeeded, set server STOPPING", CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check); |
Willy Tarreau | b26881a | 2017-12-23 11:16:49 +0100 | [diff] [blame] | 635 | srv_set_stopping(s, NULL, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 636 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 637 | |
Willy Tarreau | 9fe7aae | 2013-12-31 23:47:37 +0100 | [diff] [blame] | 638 | /* note: use health_adjust() only, which first checks that the observe mode is |
Willy Tarreau | 4e9df27 | 2021-02-17 15:20:19 +0100 | [diff] [blame] | 639 | * enabled. This will take the server lock if needed. |
Willy Tarreau | 9fe7aae | 2013-12-31 23:47:37 +0100 | [diff] [blame] | 640 | */ |
| 641 | void __health_adjust(struct server *s, short status) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 642 | { |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 643 | int failed; |
| 644 | int expire; |
| 645 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 646 | if (s->observe >= HANA_OBS_SIZE) |
| 647 | return; |
| 648 | |
Willy Tarreau | bb95666 | 2013-01-24 00:37:39 +0100 | [diff] [blame] | 649 | if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc) |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 650 | return; |
| 651 | |
| 652 | switch (analyze_statuses[status].lr[s->observe - 1]) { |
| 653 | case 1: |
| 654 | failed = 1; |
| 655 | break; |
| 656 | |
| 657 | case 2: |
| 658 | failed = 0; |
| 659 | break; |
| 660 | |
| 661 | default: |
| 662 | return; |
| 663 | } |
| 664 | |
| 665 | if (!failed) { |
| 666 | /* good: clear consecutive_errors */ |
| 667 | s->consecutive_errors = 0; |
| 668 | return; |
| 669 | } |
| 670 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 671 | _HA_ATOMIC_INC(&s->consecutive_errors); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 672 | |
| 673 | if (s->consecutive_errors < s->consecutive_errors_limit) |
| 674 | return; |
| 675 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 676 | chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s", |
| 677 | s->consecutive_errors, get_analyze_status(status)); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 678 | |
Willy Tarreau | 4e9df27 | 2021-02-17 15:20:19 +0100 | [diff] [blame] | 679 | if (s->check.fastinter) |
| 680 | expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter)); |
| 681 | else |
| 682 | expire = TICK_ETERNITY; |
| 683 | |
| 684 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
| 685 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 686 | switch (s->onerror) { |
| 687 | case HANA_ONERR_FASTINTER: |
| 688 | /* force fastinter - nothing to do here as all modes force it */ |
| 689 | break; |
| 690 | |
| 691 | case HANA_ONERR_SUDDTH: |
| 692 | /* simulate a pre-fatal failed health check */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 693 | if (s->check.health > s->check.rise) |
| 694 | s->check.health = s->check.rise + 1; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 695 | |
Tim Duesterhus | 588b314 | 2020-05-29 14:35:51 +0200 | [diff] [blame] | 696 | /* fall through */ |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 697 | |
| 698 | case HANA_ONERR_FAILCHK: |
| 699 | /* simulate a failed health check */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 700 | set_server_check_status(&s->check, HCHK_STATUS_HANA, |
| 701 | trash.area); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 702 | check_notify_failure(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 703 | break; |
| 704 | |
| 705 | case HANA_ONERR_MARKDWN: |
| 706 | /* mark server down */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 707 | s->check.health = s->check.rise; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 708 | set_server_check_status(&s->check, HCHK_STATUS_HANA, |
| 709 | trash.area); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 710 | check_notify_failure(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 711 | break; |
| 712 | |
| 713 | default: |
| 714 | /* write a warning? */ |
| 715 | break; |
| 716 | } |
| 717 | |
Willy Tarreau | 4e9df27 | 2021-02-17 15:20:19 +0100 | [diff] [blame] | 718 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 719 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 720 | s->consecutive_errors = 0; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 721 | _HA_ATOMIC_INC(&s->counters.failed_hana); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 722 | |
Christopher Faulet | ea86083 | 2021-05-07 11:45:26 +0200 | [diff] [blame] | 723 | if (tick_isset(expire) && tick_is_lt(expire, s->check.task->expire)) { |
Willy Tarreau | 4e9df27 | 2021-02-17 15:20:19 +0100 | [diff] [blame] | 724 | /* requeue check task with new expire */ |
| 725 | task_schedule(s->check.task, expire); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 726 | } |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 727 | } |
| 728 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 729 | /* Checks the connection. If an error has already been reported or the socket is |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 730 | * closed, keep errno intact as it is supposed to contain the valid error code. |
| 731 | * If no error is reported, check the socket's error queue using getsockopt(). |
| 732 | * Warning, this must be done only once when returning from poll, and never |
| 733 | * after an I/O error was attempted, otherwise the error queue might contain |
| 734 | * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the |
| 735 | * socket. Returns non-zero if an error was reported, zero if everything is |
| 736 | * clean (including a properly closed socket). |
| 737 | */ |
| 738 | static int retrieve_errno_from_socket(struct connection *conn) |
| 739 | { |
| 740 | int skerr; |
| 741 | socklen_t lskerr = sizeof(skerr); |
| 742 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 743 | if (conn->flags & CO_FL_ERROR && (unclean_errno(errno) || !conn->ctrl)) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 744 | return 1; |
| 745 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 746 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 747 | return 0; |
| 748 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 749 | if (getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 750 | errno = skerr; |
| 751 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 752 | errno = unclean_errno(errno); |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 753 | |
| 754 | if (!errno) { |
| 755 | /* we could not retrieve an error, that does not mean there is |
| 756 | * none. Just don't change anything and only report the prior |
| 757 | * error if any. |
| 758 | */ |
| 759 | if (conn->flags & CO_FL_ERROR) |
| 760 | return 1; |
| 761 | else |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH; |
| 766 | return 1; |
| 767 | } |
| 768 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 769 | /* Tries to collect as much information as possible on the connection status, |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 770 | * and adjust the server status accordingly. It may make use of <errno_bck> |
| 771 | * if non-null when the caller is absolutely certain of its validity (eg: |
| 772 | * checked just after a syscall). If the caller doesn't have a valid errno, |
| 773 | * it can pass zero, and retrieve_errno_from_socket() will be called to try |
| 774 | * to extract errno from the socket. If no error is reported, it will consider |
| 775 | * the <expired> flag. This is intended to be used when a connection error was |
| 776 | * reported in conn->flags or when a timeout was reported in <expired>. The |
| 777 | * function takes care of not updating a server status which was already set. |
| 778 | * All situations where at least one of <expired> or CO_FL_ERROR are set |
| 779 | * produce a status. |
| 780 | */ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 781 | void chk_report_conn_err(struct check *check, int errno_bck, int expired) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 782 | { |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 783 | struct conn_stream *cs = check->cs; |
| 784 | struct connection *conn = cs_conn(cs); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 785 | const char *err_msg; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 786 | struct buffer *chk; |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 787 | int step; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 788 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 789 | if (check->result != CHK_RES_UNKNOWN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 790 | return; |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 791 | } |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 792 | |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 793 | errno = unclean_errno(errno_bck); |
| 794 | if (conn && errno) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 795 | retrieve_errno_from_socket(conn); |
| 796 | |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 797 | if (conn && !(conn->flags & CO_FL_ERROR) && |
| 798 | !(cs->flags & CS_FL_ERROR) && !expired) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 799 | return; |
| 800 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 801 | TRACE_ENTER(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check, 0, 0, (size_t[]){expired}); |
| 802 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 803 | /* we'll try to build a meaningful error message depending on the |
| 804 | * context of the error possibly present in conn->err_code, and the |
| 805 | * socket error possibly collected above. This is useful to know the |
| 806 | * exact step of the L6 layer (eg: SSL handshake). |
| 807 | */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 808 | chk = get_trash_chunk(); |
| 809 | |
Christopher Faulet | 799f3a4 | 2020-04-07 12:06:14 +0200 | [diff] [blame] | 810 | if (check->type == PR_O2_TCPCHK_CHK && |
Christopher Faulet | d7e6396 | 2020-04-17 20:15:59 +0200 | [diff] [blame] | 811 | (check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_TCP_CHK) { |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 812 | step = tcpcheck_get_step_id(check, NULL); |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 813 | if (!step) { |
| 814 | TRACE_DEVEL("initial connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 815 | chunk_printf(chk, " at initial connection step of tcp-check"); |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 816 | } |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 817 | else { |
| 818 | chunk_printf(chk, " at step %d of tcp-check", step); |
| 819 | /* we were looking for a string */ |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 820 | if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) { |
| 821 | if (check->current_step->connect.port) |
| 822 | chunk_appendf(chk, " (connect port %d)" ,check->current_step->connect.port); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 823 | else |
| 824 | chunk_appendf(chk, " (connect)"); |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 825 | TRACE_DEVEL("connection failure", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 826 | } |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 827 | else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) { |
| 828 | struct tcpcheck_expect *expect = &check->current_step->expect; |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 829 | |
| 830 | switch (expect->type) { |
| 831 | case TCPCHK_EXPECT_STRING: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 832 | chunk_appendf(chk, " (expect string '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data)); |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 833 | break; |
| 834 | case TCPCHK_EXPECT_BINARY: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 835 | chunk_appendf(chk, " (expect binary '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data)); |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 836 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 837 | case TCPCHK_EXPECT_STRING_REGEX: |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 838 | chunk_appendf(chk, " (expect regex)"); |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 839 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 840 | case TCPCHK_EXPECT_BINARY_REGEX: |
Gaetan Rivet | efab6c6 | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 841 | chunk_appendf(chk, " (expect binary regex)"); |
| 842 | break; |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 843 | case TCPCHK_EXPECT_STRING_LF: |
| 844 | chunk_appendf(chk, " (expect log-format string)"); |
| 845 | break; |
| 846 | case TCPCHK_EXPECT_BINARY_LF: |
| 847 | chunk_appendf(chk, " (expect log-format binary)"); |
| 848 | break; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 849 | case TCPCHK_EXPECT_HTTP_STATUS: |
Christopher Faulet | 8021a5f | 2020-04-24 13:53:12 +0200 | [diff] [blame] | 850 | chunk_appendf(chk, " (expect HTTP status codes)"); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 851 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 852 | case TCPCHK_EXPECT_HTTP_STATUS_REGEX: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 853 | chunk_appendf(chk, " (expect HTTP status regex)"); |
| 854 | break; |
Christopher Faulet | 3970819 | 2020-05-05 10:47:36 +0200 | [diff] [blame] | 855 | case TCPCHK_EXPECT_HTTP_HEADER: |
| 856 | chunk_appendf(chk, " (expect HTTP header pattern)"); |
| 857 | break; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 858 | case TCPCHK_EXPECT_HTTP_BODY: |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 859 | chunk_appendf(chk, " (expect HTTP body content '%.*s')", (unsigned int)istlen(expect->data), istptr(expect->data)); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 860 | break; |
Christopher Faulet | 67a2345 | 2020-05-05 18:10:01 +0200 | [diff] [blame] | 861 | case TCPCHK_EXPECT_HTTP_BODY_REGEX: |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 862 | chunk_appendf(chk, " (expect HTTP body regex)"); |
| 863 | break; |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 864 | case TCPCHK_EXPECT_HTTP_BODY_LF: |
| 865 | chunk_appendf(chk, " (expect log-format HTTP body)"); |
| 866 | break; |
Christopher Faulet | 9e6ed15 | 2020-04-03 15:24:06 +0200 | [diff] [blame] | 867 | case TCPCHK_EXPECT_CUSTOM: |
| 868 | chunk_appendf(chk, " (expect custom function)"); |
| 869 | break; |
Gaetan Rivet | b616add | 2020-02-07 15:37:17 +0100 | [diff] [blame] | 870 | case TCPCHK_EXPECT_UNDEF: |
| 871 | chunk_appendf(chk, " (undefined expect!)"); |
| 872 | break; |
| 873 | } |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 874 | TRACE_DEVEL("expect rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 875 | } |
Christopher Faulet | b2c2e0f | 2020-03-30 11:05:10 +0200 | [diff] [blame] | 876 | else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) { |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 877 | chunk_appendf(chk, " (send)"); |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 878 | TRACE_DEVEL("send rule failed", CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check); |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 879 | } |
Baptiste Assmann | 22b09d2 | 2015-05-01 08:03:04 +0200 | [diff] [blame] | 880 | |
Christopher Faulet | 6f2a5e4 | 2020-04-01 13:11:41 +0200 | [diff] [blame] | 881 | if (check->current_step && check->current_step->comment) |
| 882 | chunk_appendf(chk, " comment: '%s'", check->current_step->comment); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 886 | if (conn && conn->err_code) { |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 887 | if (unclean_errno(errno)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 888 | chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), |
| 889 | chk->area); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 890 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 891 | chunk_printf(&trash, "%s%s", conn_err_code_str(conn), |
| 892 | chk->area); |
| 893 | err_msg = trash.area; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 894 | } |
| 895 | else { |
Willy Tarreau | c8dc20a | 2019-12-27 12:03:27 +0100 | [diff] [blame] | 896 | if (unclean_errno(errno)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 897 | chunk_printf(&trash, "%s%s", strerror(errno), |
| 898 | chk->area); |
| 899 | err_msg = trash.area; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 900 | } |
| 901 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 902 | err_msg = chk->area; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 906 | if (check->state & CHK_ST_PORT_MISS) { |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 907 | /* NOTE: this is reported after <fall> tries */ |
Baptiste Assmann | 95db2bc | 2016-06-13 14:15:41 +0200 | [diff] [blame] | 908 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 909 | } |
| 910 | |
Christopher Faulet | 5e29376 | 2020-10-26 11:10:49 +0100 | [diff] [blame] | 911 | if (!conn || !conn->ctrl) { |
| 912 | /* error before any connection attempt (connection allocation error or no control layer) */ |
Willy Tarreau | 0014912 | 2017-10-04 18:05:01 +0200 | [diff] [blame] | 913 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 914 | } |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 915 | else if (conn->flags & CO_FL_WAIT_L4_CONN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 916 | /* L4 not established (yet) */ |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 917 | if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 918 | set_server_check_status(check, HCHK_STATUS_L4CON, err_msg); |
| 919 | else if (expired) |
| 920 | set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 921 | |
| 922 | /* |
| 923 | * might be due to a server IP change. |
| 924 | * Let's trigger a DNS resolution if none are currently running. |
| 925 | */ |
Olivier Houchard | 0923fa4 | 2019-01-11 18:43:04 +0100 | [diff] [blame] | 926 | if (check->server) |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 927 | resolv_trigger_resolution(check->server->resolv_requester); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 928 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 929 | } |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 930 | else if (conn->flags & CO_FL_WAIT_L6_CONN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 931 | /* L6 not established (yet) */ |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 932 | if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 933 | set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg); |
| 934 | else if (expired) |
| 935 | set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg); |
| 936 | } |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 937 | else if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 938 | /* I/O error after connection was established and before we could diagnose */ |
| 939 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 940 | } |
| 941 | else if (expired) { |
Christopher Faulet | cf80f2f | 2020-04-01 11:04:52 +0200 | [diff] [blame] | 942 | enum healthcheck_status tout = HCHK_STATUS_L7TOUT; |
| 943 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 944 | /* connection established but expired check */ |
Christopher Faulet | 1941bab | 2020-05-05 07:55:50 +0200 | [diff] [blame] | 945 | if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT && |
| 946 | check->current_step->expect.tout_status != HCHK_STATUS_UNKNOWN) |
Christopher Faulet | 811f78c | 2020-04-01 11:10:27 +0200 | [diff] [blame] | 947 | tout = check->current_step->expect.tout_status; |
| 948 | set_server_check_status(check, tout, err_msg); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 949 | } |
| 950 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 951 | TRACE_LEAVE(CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 952 | return; |
| 953 | } |
| 954 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 955 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 956 | /* Builds the server state header used by HTTP health-checks */ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 957 | int httpchk_build_status_header(struct server *s, struct buffer *buf) |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 958 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 959 | int sv_state; |
| 960 | int ratio; |
| 961 | char addr[46]; |
| 962 | char port[6]; |
| 963 | const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d", |
| 964 | "UP %d/%d", "UP", |
| 965 | "NOLB %d/%d", "NOLB", |
| 966 | "no check" }; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 967 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 968 | if (!(s->check.state & CHK_ST_ENABLED)) |
| 969 | sv_state = 6; |
| 970 | else if (s->cur_state != SRV_ST_STOPPED) { |
| 971 | if (s->check.health == s->check.rise + s->check.fall - 1) |
| 972 | sv_state = 3; /* UP */ |
| 973 | else |
| 974 | sv_state = 2; /* going down */ |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 975 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 976 | if (s->cur_state == SRV_ST_STOPPING) |
| 977 | sv_state += 2; |
| 978 | } else { |
| 979 | if (s->check.health) |
| 980 | sv_state = 1; /* going up */ |
| 981 | else |
| 982 | sv_state = 0; /* DOWN */ |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 983 | } |
Willy Tarreau | b7b2478 | 2016-06-21 15:32:29 +0200 | [diff] [blame] | 984 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 985 | chunk_appendf(buf, srv_hlt_st[sv_state], |
| 986 | (s->cur_state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health), |
| 987 | (s->cur_state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise)); |
Willy Tarreau | b7b2478 | 2016-06-21 15:32:29 +0200 | [diff] [blame] | 988 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 989 | addr_to_str(&s->addr, addr, sizeof(addr)); |
| 990 | if (s->addr.ss_family == AF_INET || s->addr.ss_family == AF_INET6) |
| 991 | snprintf(port, sizeof(port), "%u", s->svc_port); |
| 992 | else |
| 993 | *port = 0; |
Willy Tarreau | b7b2478 | 2016-06-21 15:32:29 +0200 | [diff] [blame] | 994 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 995 | chunk_appendf(buf, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d", |
| 996 | addr, port, s->proxy->id, s->id, |
| 997 | global.node, |
| 998 | (s->cur_eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 999 | (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 1000 | s->cur_sess, s->proxy->beconn - s->proxy->nbpend, |
| 1001 | s->nbpend); |
Willy Tarreau | 9f6dc72 | 2019-03-01 11:15:10 +0100 | [diff] [blame] | 1002 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1003 | if ((s->cur_state == SRV_ST_STARTING) && |
| 1004 | now.tv_sec < s->last_change + s->slowstart && |
| 1005 | now.tv_sec >= s->last_change) { |
| 1006 | ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart); |
| 1007 | chunk_appendf(buf, "; throttle=%d%%", ratio); |
| 1008 | } |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1009 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1010 | return b_data(buf); |
| 1011 | } |
Christopher Faulet | aaae9a0 | 2020-04-26 09:50:31 +0200 | [diff] [blame] | 1012 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1013 | /**************************************************************************/ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1014 | /***************** Health-checks based on connections *********************/ |
| 1015 | /**************************************************************************/ |
| 1016 | /* This function is used only for server health-checks. It handles connection |
| 1017 | * status updates including errors. If necessary, it wakes the check task up. |
| 1018 | * It returns 0 on normal cases, <0 if at least one close() has happened on the |
| 1019 | * connection (eg: reconnect). It relies on tcpcheck_main(). |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1020 | */ |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1021 | static int wake_srv_chk(struct conn_stream *cs) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1022 | { |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1023 | struct connection *conn = cs->conn; |
| 1024 | struct check *check = cs->data; |
| 1025 | struct email_alertq *q = container_of(check, typeof(*q), check); |
| 1026 | int ret = 0; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1027 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1028 | TRACE_ENTER(CHK_EV_HCHK_WAKE, check); |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1029 | if (check->server) |
| 1030 | HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock); |
| 1031 | else |
| 1032 | HA_SPIN_LOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1033 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1034 | /* we may have to make progress on the TCP checks */ |
| 1035 | ret = tcpcheck_main(check); |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 1036 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1037 | cs = check->cs; |
| 1038 | conn = cs->conn; |
Christopher Faulet | aaab083 | 2020-05-05 15:54:22 +0200 | [diff] [blame] | 1039 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1040 | if (unlikely(conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)) { |
| 1041 | /* We may get error reports bypassing the I/O handlers, typically |
| 1042 | * the case when sending a pure TCP check which fails, then the I/O |
| 1043 | * handlers above are not called. This is completely handled by the |
| 1044 | * main processing task so let's simply wake it up. If we get here, |
| 1045 | * we expect errno to still be valid. |
| 1046 | */ |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1047 | TRACE_ERROR("report connection error", CHK_EV_HCHK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check); |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1048 | chk_report_conn_err(check, errno, 0); |
| 1049 | task_wakeup(check->task, TASK_WOKEN_IO); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1050 | } |
| 1051 | |
Christopher Faulet | 8f10042 | 2021-01-18 15:47:03 +0100 | [diff] [blame] | 1052 | if (check->result != CHK_RES_UNKNOWN || ret == -1) { |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1053 | /* Check complete or aborted. If connection not yet closed do it |
| 1054 | * now and wake the check task up to be sure the result is |
| 1055 | * handled ASAP. */ |
Willy Tarreau | 30bd4ef | 2020-12-11 11:09:29 +0100 | [diff] [blame] | 1056 | cs_drain_and_close(cs); |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1057 | ret = -1; |
Christopher Faulet | 8f10042 | 2021-01-18 15:47:03 +0100 | [diff] [blame] | 1058 | |
| 1059 | if (check->wait_list.events) |
| 1060 | cs->conn->mux->unsubscribe(cs, check->wait_list.events, &check->wait_list); |
| 1061 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1062 | /* 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 Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1068 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1069 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1070 | if (check->server) |
| 1071 | HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock); |
| 1072 | else |
| 1073 | HA_SPIN_UNLOCK(EMAIL_ALERTS_LOCK, &q->lock); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1074 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1075 | TRACE_LEAVE(CHK_EV_HCHK_WAKE, check); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1076 | return ret; |
| 1077 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1078 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1079 | /* This function checks if any I/O is wanted, and if so, attempts to do so */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1080 | struct task *event_srv_chk_io(struct task *t, void *ctx, unsigned int state) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1081 | { |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1082 | struct check *check = ctx; |
| 1083 | struct conn_stream *cs = check->cs; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1084 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1085 | wake_srv_chk(cs); |
| 1086 | return NULL; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1087 | } |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1088 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1089 | /* manages a server health-check that uses a connection. Returns |
| 1090 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1091 | * |
| 1092 | * Please do NOT place any return statement in this function and only leave |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1093 | * via the out_unlock label. |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1094 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1095 | struct task *process_chk_conn(struct task *t, void *context, unsigned int state) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1096 | { |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1097 | struct check *check = context; |
| 1098 | struct proxy *proxy = check->proxy; |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1099 | struct conn_stream *cs; |
| 1100 | struct connection *conn; |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1101 | int rv; |
| 1102 | int expired = tick_is_expired(t->expire, now_ms); |
Willy Tarreau | deccd11 | 2018-06-14 18:38:55 +0200 | [diff] [blame] | 1103 | |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1104 | TRACE_ENTER(CHK_EV_TASK_WAKE, check); |
| 1105 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1106 | if (check->server) |
| 1107 | HA_SPIN_LOCK(SERVER_LOCK, &check->server->lock); |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1108 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1109 | if (!(check->state & CHK_ST_INPROGRESS)) { |
| 1110 | /* no check currently running */ |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1111 | if (!expired) /* woke up too early */ { |
| 1112 | TRACE_STATE("health-check wake up too early", CHK_EV_TASK_WAKE, check); |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1113 | goto out_unlock; |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1114 | } |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 1115 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1116 | /* we don't send any health-checks when the proxy is |
| 1117 | * stopped, the server should not be checked or the check |
| 1118 | * is disabled. |
| 1119 | */ |
| 1120 | if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) || |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1121 | proxy->disabled) { |
| 1122 | TRACE_STATE("health-check paused or disabled", CHK_EV_TASK_WAKE, check); |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1123 | goto reschedule; |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1124 | } |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1125 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1126 | /* we'll initiate a new check */ |
| 1127 | set_server_check_status(check, HCHK_STATUS_START, NULL); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1128 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1129 | check->state |= CHK_ST_INPROGRESS; |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1130 | TRACE_STATE("init new health-check", CHK_EV_TASK_WAKE|CHK_EV_HCHK_START, check); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1131 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1132 | task_set_affinity(t, tid_bit); |
| 1133 | |
| 1134 | check->current_step = NULL; |
| 1135 | tcpcheck_main(check); |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1136 | expired = 0; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1137 | } |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1138 | |
| 1139 | cs = check->cs; |
| 1140 | conn = cs_conn(cs); |
| 1141 | |
| 1142 | /* there was a test running. |
| 1143 | * First, let's check whether there was an uncaught error, |
| 1144 | * which can happen on connect timeout or error. |
| 1145 | */ |
| 1146 | if (check->result == CHK_RES_UNKNOWN) { |
| 1147 | /* Here the connection must be defined. Otherwise the |
| 1148 | * error would have already been detected |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1149 | */ |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1150 | if ((conn && ((conn->flags & CO_FL_ERROR) || (cs->flags & CS_FL_ERROR))) || expired) { |
| 1151 | TRACE_ERROR("report connection error", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check); |
| 1152 | chk_report_conn_err(check, 0, expired); |
| 1153 | } |
| 1154 | else { |
| 1155 | if (check->state & CHK_ST_CLOSE_CONN) { |
| 1156 | TRACE_DEVEL("closing current connection", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check); |
| 1157 | cs_destroy(cs); |
| 1158 | cs = NULL; |
| 1159 | conn = NULL; |
| 1160 | check->cs = NULL; |
| 1161 | check->state &= ~CHK_ST_CLOSE_CONN; |
| 1162 | tcpcheck_main(check); |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1163 | } |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1164 | if (check->result == CHK_RES_UNKNOWN) { |
| 1165 | TRACE_DEVEL("health-check not expired", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check); |
| 1166 | goto out_unlock; /* timeout not reached, wait again */ |
Christopher Faulet | 8f10042 | 2021-01-18 15:47:03 +0100 | [diff] [blame] | 1167 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1168 | } |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1169 | } |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1170 | |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1171 | /* check complete or aborted */ |
| 1172 | TRACE_STATE("health-check complete or aborted", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END, check); |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 1173 | |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1174 | check->current_step = NULL; |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 1175 | |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1176 | if (conn && conn->xprt) { |
| 1177 | /* The check was aborted and the connection was not yet closed. |
| 1178 | * This can happen upon timeout, or when an external event such |
| 1179 | * as a failed response coupled with "observe layer7" caused the |
| 1180 | * server state to be suddenly changed. |
| 1181 | */ |
| 1182 | cs_drain_and_close(cs); |
| 1183 | } |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 1184 | |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1185 | if (cs) { |
| 1186 | if (check->wait_list.events) |
| 1187 | cs->conn->mux->unsubscribe(cs, check->wait_list.events, &check->wait_list); |
| 1188 | /* We may have been scheduled to run, and the |
| 1189 | * I/O handler expects to have a cs, so remove |
| 1190 | * the tasklet |
| 1191 | */ |
| 1192 | tasklet_remove_from_tasklet_list(check->wait_list.tasklet); |
| 1193 | cs_destroy(cs); |
| 1194 | cs = check->cs = NULL; |
| 1195 | conn = NULL; |
| 1196 | } |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1197 | |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1198 | if (check->sess != NULL) { |
| 1199 | vars_prune(&check->vars, check->sess, NULL); |
| 1200 | session_free(check->sess); |
| 1201 | check->sess = NULL; |
| 1202 | } |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1203 | |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1204 | if (check->server) { |
| 1205 | if (check->result == CHK_RES_FAILED) { |
| 1206 | /* a failure or timeout detected */ |
| 1207 | TRACE_DEVEL("report failure", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_ERR, check); |
| 1208 | check_notify_failure(check); |
| 1209 | } |
| 1210 | else if (check->result == CHK_RES_CONDPASS) { |
| 1211 | /* check is OK but asks for stopping mode */ |
| 1212 | TRACE_DEVEL("report conditional success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check); |
| 1213 | check_notify_stopping(check); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1214 | } |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1215 | else if (check->result == CHK_RES_PASSED) { |
| 1216 | /* a success was detected */ |
| 1217 | TRACE_DEVEL("report success", CHK_EV_TASK_WAKE|CHK_EV_HCHK_END|CHK_EV_HCHK_SUCC, check); |
| 1218 | check_notify_success(check); |
| 1219 | } |
| 1220 | } |
Christopher Faulet | ecc3426 | 2022-05-18 14:24:43 +0200 | [diff] [blame] | 1221 | |
| 1222 | if (LIST_INLIST(&check->buf_wait.list)) |
| 1223 | LIST_DEL_INIT(&check->buf_wait.list); |
| 1224 | |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1225 | task_set_affinity(t, MAX_THREADS_MASK); |
| 1226 | check_release_buf(check, &check->bi); |
| 1227 | check_release_buf(check, &check->bo); |
| 1228 | check->state &= ~(CHK_ST_INPROGRESS|CHK_ST_IN_ALLOC|CHK_ST_OUT_ALLOC); |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 1229 | |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1230 | if (check->server) { |
| 1231 | rv = 0; |
| 1232 | if (global.spread_checks > 0) { |
| 1233 | rv = srv_getinter(check) * global.spread_checks / 100; |
| 1234 | rv -= (int) (2 * rv * (ha_random32() / 4294967295.0)); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1235 | } |
Christopher Faulet | 92017a3 | 2021-05-06 16:01:18 +0200 | [diff] [blame] | 1236 | t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv)); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1237 | } |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1238 | |
| 1239 | reschedule: |
| 1240 | while (tick_is_expired(t->expire, now_ms)) |
| 1241 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 1242 | out_unlock: |
| 1243 | if (check->server) |
| 1244 | HA_SPIN_UNLOCK(SERVER_LOCK, &check->server->lock); |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1245 | |
| 1246 | TRACE_LEAVE(CHK_EV_TASK_WAKE, check); |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1247 | return t; |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 1248 | } |
| 1249 | |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 1250 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1251 | /**************************************************************************/ |
| 1252 | /************************** Init/deinit checks ****************************/ |
| 1253 | /**************************************************************************/ |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1254 | /* |
| 1255 | * Tries to grab a buffer and to re-enables processing on check <target>. The |
| 1256 | * check flags are used to figure what buffer was requested. It returns 1 if the |
| 1257 | * allocation succeeds, in which case the I/O tasklet is woken up, or 0 if it's |
| 1258 | * impossible to wake up and we prefer to be woken up later. |
| 1259 | */ |
| 1260 | int check_buf_available(void *target) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1261 | { |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1262 | struct check *check = target; |
| 1263 | |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 1264 | if ((check->state & CHK_ST_IN_ALLOC) && b_alloc(&check->bi)) { |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1265 | TRACE_STATE("unblocking check, input buffer allocated", CHK_EV_TCPCHK_EXP|CHK_EV_RX_BLK, check); |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1266 | check->state &= ~CHK_ST_IN_ALLOC; |
| 1267 | tasklet_wakeup(check->wait_list.tasklet); |
| 1268 | return 1; |
| 1269 | } |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 1270 | if ((check->state & CHK_ST_OUT_ALLOC) && b_alloc(&check->bo)) { |
Christopher Faulet | 147b8c9 | 2021-04-10 09:00:38 +0200 | [diff] [blame] | 1271 | TRACE_STATE("unblocking check, output buffer allocated", CHK_EV_TCPCHK_SND|CHK_EV_TX_BLK, check); |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1272 | check->state &= ~CHK_ST_OUT_ALLOC; |
| 1273 | tasklet_wakeup(check->wait_list.tasklet); |
| 1274 | return 1; |
| 1275 | } |
| 1276 | |
| 1277 | return 0; |
| 1278 | } |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 1279 | |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1280 | /* |
William Dauchy | f430090 | 2021-02-06 20:47:50 +0100 | [diff] [blame] | 1281 | * Allocate a buffer. If it fails, it adds the check in buffer wait queue. |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1282 | */ |
| 1283 | struct buffer *check_get_buf(struct check *check, struct buffer *bptr) |
| 1284 | { |
| 1285 | struct buffer *buf = NULL; |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 1286 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1287 | if (likely(!LIST_INLIST(&check->buf_wait.list)) && |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 1288 | unlikely((buf = b_alloc(bptr)) == NULL)) { |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1289 | check->buf_wait.target = check; |
| 1290 | check->buf_wait.wakeup_cb = check_buf_available; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1291 | LIST_APPEND(&ti->buffer_wq, &check->buf_wait.list); |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1292 | } |
| 1293 | return buf; |
| 1294 | } |
| 1295 | |
| 1296 | /* |
| 1297 | * Release a buffer, if any, and try to wake up entities waiting in the buffer |
| 1298 | * wait queue. |
| 1299 | */ |
| 1300 | void check_release_buf(struct check *check, struct buffer *bptr) |
| 1301 | { |
| 1302 | if (bptr->size) { |
| 1303 | b_free(bptr); |
Willy Tarreau | 4d77bbf | 2021-02-20 12:02:46 +0100 | [diff] [blame] | 1304 | offer_buffers(check->buf_wait.target, 1); |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | const char *init_check(struct check *check, int type) |
| 1309 | { |
| 1310 | check->type = type; |
Christopher Faulet | ba3c68f | 2020-04-01 16:27:05 +0200 | [diff] [blame] | 1311 | |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1312 | check->bi = BUF_NULL; |
| 1313 | check->bo = BUF_NULL; |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 1314 | LIST_INIT(&check->buf_wait.list); |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1315 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1316 | check->wait_list.tasklet = tasklet_new(); |
| 1317 | if (!check->wait_list.tasklet) |
| 1318 | return "out of memory while allocating check tasklet"; |
| 1319 | check->wait_list.events = 0; |
| 1320 | check->wait_list.tasklet->process = event_srv_chk_io; |
| 1321 | check->wait_list.tasklet->context = check; |
| 1322 | return NULL; |
| 1323 | } |
| 1324 | |
| 1325 | void free_check(struct check *check) |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1326 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1327 | task_destroy(check->task); |
| 1328 | if (check->wait_list.tasklet) |
| 1329 | tasklet_free(check->wait_list.tasklet); |
| 1330 | |
Christopher Faulet | b381a50 | 2020-11-25 13:47:00 +0100 | [diff] [blame] | 1331 | check_release_buf(check, &check->bi); |
| 1332 | check_release_buf(check, &check->bo); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1333 | if (check->cs) { |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 1334 | ha_free(&check->cs->conn); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1335 | cs_free(check->cs); |
| 1336 | check->cs = NULL; |
| 1337 | } |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1338 | } |
| 1339 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1340 | /* manages a server health-check. Returns the time the task accepts to wait, or |
| 1341 | * TIME_ETERNITY for infinity. |
| 1342 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1343 | struct task *process_chk(struct task *t, void *context, unsigned int state) |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1344 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1345 | struct check *check = context; |
| 1346 | |
| 1347 | if (check->type == PR_O2_EXT_CHK) |
| 1348 | return process_chk_proc(t, context, state); |
| 1349 | return process_chk_conn(t, context, state); |
| 1350 | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1351 | } |
| 1352 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1353 | |
| 1354 | static int start_check_task(struct check *check, int mininter, |
| 1355 | int nbcheck, int srvpos) |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1356 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1357 | struct task *t; |
| 1358 | unsigned long thread_mask = MAX_THREADS_MASK; |
Willy Tarreau | c4dc79b | 2023-05-17 09:01:22 +0200 | [diff] [blame] | 1359 | ulong boottime = tv_ms_remain(&start_date, &ready_date); |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1360 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1361 | if (check->type == PR_O2_EXT_CHK) |
| 1362 | thread_mask = 1; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1363 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1364 | /* task for the check */ |
| 1365 | if ((t = task_new(thread_mask)) == NULL) { |
| 1366 | ha_alert("Starting [%s:%s] check: out of memory.\n", |
| 1367 | check->server->proxy->id, check->server->id); |
| 1368 | return 0; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1369 | } |
| 1370 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1371 | check->task = t; |
| 1372 | t->process = process_chk; |
| 1373 | t->context = check; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1374 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1375 | if (mininter < srv_getinter(check)) |
| 1376 | mininter = srv_getinter(check); |
| 1377 | |
Willy Tarreau | 18e018a | 2023-05-17 07:39:57 +0200 | [diff] [blame] | 1378 | if (global.spread_checks > 0) { |
| 1379 | int rnd; |
| 1380 | |
| 1381 | rnd = srv_getinter(check) * global.spread_checks / 100; |
| 1382 | rnd -= (int) (2 * rnd * (ha_random32() / 4294967295.0)); |
| 1383 | mininter += rnd; |
| 1384 | } |
| 1385 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1386 | if (global.max_spread_checks && mininter > global.max_spread_checks) |
| 1387 | mininter = global.max_spread_checks; |
| 1388 | |
| 1389 | /* check this every ms */ |
Willy Tarreau | c4dc79b | 2023-05-17 09:01:22 +0200 | [diff] [blame] | 1390 | t->expire = tick_add(now_ms, MS_TO_TICKS(boottime + mininter * srvpos / nbcheck)); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1391 | check->start = now; |
| 1392 | task_queue(t); |
| 1393 | |
| 1394 | return 1; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1395 | } |
| 1396 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1397 | /* updates the server's weight during a warmup stage. Once the final weight is |
| 1398 | * reached, the task automatically stops. Note that any server status change |
| 1399 | * must have updated s->last_change accordingly. |
| 1400 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1401 | struct task *server_warmup(struct task *t, void *context, unsigned int state) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1402 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1403 | struct server *s = context; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1404 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1405 | /* by default, plan on stopping the task */ |
| 1406 | t->expire = TICK_ETERNITY; |
| 1407 | if ((s->next_admin & SRV_ADMF_MAINT) || |
| 1408 | (s->next_state != SRV_ST_STARTING)) |
| 1409 | return t; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1410 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1411 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1412 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1413 | /* recalculate the weights and update the state */ |
| 1414 | server_recalc_eweight(s, 1); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 1415 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1416 | /* probably that we can refill this server with a bit more connections */ |
| 1417 | pendconn_grab_from_px(s); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 1418 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1419 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 1420 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1421 | /* get back there in 1 second or 1/20th of the slowstart interval, |
| 1422 | * whichever is greater, resulting in small 5% steps. |
| 1423 | */ |
| 1424 | if (s->next_state == SRV_ST_STARTING) |
| 1425 | t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))); |
| 1426 | return t; |
| 1427 | } |
| 1428 | |
| 1429 | /* |
| 1430 | * Start health-check. |
| 1431 | * Returns 0 if OK, ERR_FATAL on error, and prints the error in this case. |
| 1432 | */ |
| 1433 | static int start_checks() |
| 1434 | { |
| 1435 | |
| 1436 | struct proxy *px; |
| 1437 | struct server *s; |
| 1438 | struct task *t; |
| 1439 | int nbcheck=0, mininter=0, srvpos=0; |
| 1440 | |
| 1441 | /* 0- init the dummy frontend used to create all checks sessions */ |
| 1442 | init_new_proxy(&checks_fe); |
Christopher Faulet | 0f1fc23 | 2021-04-16 10:49:07 +0200 | [diff] [blame] | 1443 | checks_fe.id = strdup("CHECKS-FE"); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1444 | checks_fe.cap = PR_CAP_FE | PR_CAP_BE; |
| 1445 | checks_fe.mode = PR_MODE_TCP; |
| 1446 | checks_fe.maxconn = 0; |
| 1447 | checks_fe.conn_retries = CONN_RETRIES; |
| 1448 | checks_fe.options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC; |
| 1449 | checks_fe.timeout.client = TICK_ETERNITY; |
| 1450 | |
| 1451 | /* 1- count the checkers to run simultaneously. |
| 1452 | * We also determine the minimum interval among all of those which |
| 1453 | * have an interval larger than SRV_CHK_INTER_THRES. This interval |
| 1454 | * will be used to spread their start-up date. Those which have |
| 1455 | * a shorter interval will start independently and will not dictate |
| 1456 | * too short an interval for all others. |
| 1457 | */ |
| 1458 | for (px = proxies_list; px; px = px->next) { |
| 1459 | for (s = px->srv; s; s = s->next) { |
| 1460 | if (s->slowstart) { |
| 1461 | if ((t = task_new(MAX_THREADS_MASK)) == NULL) { |
| 1462 | ha_alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 1463 | return ERR_ALERT | ERR_FATAL; |
| 1464 | } |
| 1465 | /* We need a warmup task that will be called when the server |
| 1466 | * state switches from down to up. |
| 1467 | */ |
| 1468 | s->warmup = t; |
| 1469 | t->process = server_warmup; |
| 1470 | t->context = s; |
| 1471 | /* server can be in this state only because of */ |
| 1472 | if (s->next_state == SRV_ST_STARTING) |
| 1473 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, (now.tv_sec - s->last_change)) / 20))); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 1474 | } |
| 1475 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1476 | if (s->check.state & CHK_ST_CONFIGURED) { |
| 1477 | nbcheck++; |
| 1478 | if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) && |
| 1479 | (!mininter || mininter > srv_getinter(&s->check))) |
| 1480 | mininter = srv_getinter(&s->check); |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 1481 | } |
| 1482 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1483 | if (s->agent.state & CHK_ST_CONFIGURED) { |
| 1484 | nbcheck++; |
| 1485 | if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) && |
| 1486 | (!mininter || mininter > srv_getinter(&s->agent))) |
| 1487 | mininter = srv_getinter(&s->agent); |
| 1488 | } |
Christopher Faulet | 5c28874 | 2020-03-31 08:15:58 +0200 | [diff] [blame] | 1489 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1490 | } |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 1491 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1492 | if (!nbcheck) |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1493 | return ERR_NONE; |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 1494 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1495 | srand((unsigned)time(NULL)); |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 1496 | |
William Dauchy | f430090 | 2021-02-06 20:47:50 +0100 | [diff] [blame] | 1497 | /* 2- start them as far as possible from each other. For this, we will |
| 1498 | * start them after their interval is set to the min interval divided |
| 1499 | * by the number of servers, weighted by the server's position in the |
| 1500 | * list. |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1501 | */ |
| 1502 | for (px = proxies_list; px; px = px->next) { |
| 1503 | if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) { |
| 1504 | if (init_pid_list()) { |
| 1505 | ha_alert("Starting [%s] check: out of memory.\n", px->id); |
| 1506 | return ERR_ALERT | ERR_FATAL; |
| 1507 | } |
| 1508 | } |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 1509 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1510 | for (s = px->srv; s; s = s->next) { |
| 1511 | /* A task for the main check */ |
| 1512 | if (s->check.state & CHK_ST_CONFIGURED) { |
| 1513 | if (s->check.type == PR_O2_EXT_CHK) { |
| 1514 | if (!prepare_external_check(&s->check)) |
| 1515 | return ERR_ALERT | ERR_FATAL; |
Christopher Faulet | b7d3009 | 2020-03-30 15:19:03 +0200 | [diff] [blame] | 1516 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1517 | if (!start_check_task(&s->check, mininter, nbcheck, srvpos)) |
| 1518 | return ERR_ALERT | ERR_FATAL; |
| 1519 | srvpos++; |
Christopher Faulet | 9857232 | 2020-03-30 13:16:44 +0200 | [diff] [blame] | 1520 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1521 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1522 | /* A task for a auxiliary agent check */ |
| 1523 | if (s->agent.state & CHK_ST_CONFIGURED) { |
| 1524 | if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) { |
| 1525 | return ERR_ALERT | ERR_FATAL; |
| 1526 | } |
| 1527 | srvpos++; |
| 1528 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1529 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1530 | } |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1531 | return ERR_NONE; |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1532 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1533 | |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1534 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1535 | /* |
| 1536 | * Return value: |
| 1537 | * the port to be used for the health check |
| 1538 | * 0 in case no port could be found for the check |
| 1539 | */ |
| 1540 | static int srv_check_healthcheck_port(struct check *chk) |
| 1541 | { |
| 1542 | int i = 0; |
| 1543 | struct server *srv = NULL; |
| 1544 | |
| 1545 | srv = chk->server; |
| 1546 | |
William Dauchy | f430090 | 2021-02-06 20:47:50 +0100 | [diff] [blame] | 1547 | /* by default, we use the health check port configured */ |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1548 | if (chk->port > 0) |
| 1549 | return chk->port; |
| 1550 | |
| 1551 | /* try to get the port from check_core.addr if check.port not set */ |
| 1552 | i = get_host_port(&chk->addr); |
| 1553 | if (i > 0) |
| 1554 | return i; |
| 1555 | |
| 1556 | /* try to get the port from server address */ |
| 1557 | /* prevent MAPPORTS from working at this point, since checks could |
| 1558 | * not be performed in such case (MAPPORTS impose a relative ports |
| 1559 | * based on live traffic) |
| 1560 | */ |
| 1561 | if (srv->flags & SRV_F_MAPPORTS) |
| 1562 | return 0; |
| 1563 | |
| 1564 | i = srv->svc_port; /* by default */ |
| 1565 | if (i > 0) |
| 1566 | return i; |
| 1567 | |
| 1568 | return 0; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1569 | } |
| 1570 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1571 | /* Initializes an health-check attached to the server <srv>. Non-zero is returned |
| 1572 | * if an error occurred. |
| 1573 | */ |
| 1574 | static int init_srv_check(struct server *srv) |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1575 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1576 | const char *err; |
| 1577 | struct tcpcheck_rule *r; |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1578 | int ret = ERR_NONE; |
Amaury Denoyelle | 0519bd4 | 2020-11-13 12:34:56 +0100 | [diff] [blame] | 1579 | int check_type; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1580 | |
Christopher Faulet | 6ecd593 | 2021-01-12 17:29:45 +0100 | [diff] [blame] | 1581 | if (!srv->do_check || !(srv->proxy->cap & PR_CAP_BE)) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1582 | goto out; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1583 | |
Amaury Denoyelle | 0519bd4 | 2020-11-13 12:34:56 +0100 | [diff] [blame] | 1584 | check_type = srv->check.tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK; |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 1585 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1586 | /* If neither a port nor an addr was specified and no check transport |
| 1587 | * layer is forced, then the transport layer used by the checks is the |
| 1588 | * same as for the production traffic. Otherwise we use raw_sock by |
| 1589 | * default, unless one is specified. |
| 1590 | */ |
| 1591 | if (!srv->check.port && !is_addr(&srv->check.addr)) { |
| 1592 | if (!srv->check.use_ssl && srv->use_ssl != -1) { |
| 1593 | srv->check.use_ssl = srv->use_ssl; |
| 1594 | srv->check.xprt = srv->xprt; |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1595 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1596 | else if (srv->check.use_ssl == 1) |
| 1597 | srv->check.xprt = xprt_get(XPRT_SSL); |
| 1598 | srv->check.send_proxy |= (srv->pp_opts); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1599 | } |
Christopher Faulet | 66163ec | 2020-05-20 22:36:24 +0200 | [diff] [blame] | 1600 | else if (srv->check.use_ssl == 1) |
| 1601 | srv->check.xprt = xprt_get(XPRT_SSL); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1602 | |
Christopher Faulet | 12882cf | 2020-04-23 15:50:18 +0200 | [diff] [blame] | 1603 | /* Inherit the mux protocol from the server if not already defined for |
| 1604 | * the check |
| 1605 | */ |
Amaury Denoyelle | 0519bd4 | 2020-11-13 12:34:56 +0100 | [diff] [blame] | 1606 | if (srv->mux_proto && !srv->check.mux_proto && |
| 1607 | ((srv->mux_proto->mode == PROTO_MODE_HTTP && check_type == TCPCHK_RULES_HTTP_CHK) || |
| 1608 | (srv->mux_proto->mode == PROTO_MODE_TCP && check_type != TCPCHK_RULES_HTTP_CHK))) { |
Christopher Faulet | 12882cf | 2020-04-23 15:50:18 +0200 | [diff] [blame] | 1609 | srv->check.mux_proto = srv->mux_proto; |
Amaury Denoyelle | 0519bd4 | 2020-11-13 12:34:56 +0100 | [diff] [blame] | 1610 | } |
Amaury Denoyelle | 7c14890 | 2020-11-13 12:34:57 +0100 | [diff] [blame] | 1611 | /* test that check proto is valid if explicitly defined */ |
| 1612 | else if (srv->check.mux_proto && |
| 1613 | ((srv->check.mux_proto->mode == PROTO_MODE_HTTP && check_type != TCPCHK_RULES_HTTP_CHK) || |
| 1614 | (srv->check.mux_proto->mode == PROTO_MODE_TCP && check_type == TCPCHK_RULES_HTTP_CHK))) { |
| 1615 | ha_alert("config: %s '%s': server '%s' uses an incompatible MUX protocol for the selected check type\n", |
| 1616 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 1617 | ret |= ERR_ALERT | ERR_FATAL; |
| 1618 | goto out; |
| 1619 | } |
Christopher Faulet | 12882cf | 2020-04-23 15:50:18 +0200 | [diff] [blame] | 1620 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1621 | /* validate <srv> server health-check settings */ |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 1622 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1623 | /* We need at least a service port, a check port or the first tcp-check |
| 1624 | * rule must be a 'connect' one when checking an IPv4/IPv6 server. |
| 1625 | */ |
| 1626 | if ((srv_check_healthcheck_port(&srv->check) != 0) || |
| 1627 | (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr)))) |
| 1628 | goto init; |
Christopher Faulet | f50f4e9 | 2020-03-30 19:52:29 +0200 | [diff] [blame] | 1629 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1630 | if (!srv->proxy->tcpcheck_rules.list || LIST_ISEMPTY(srv->proxy->tcpcheck_rules.list)) { |
| 1631 | ha_alert("config: %s '%s': server '%s' has neither service port nor check port.\n", |
| 1632 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 1633 | ret |= ERR_ALERT | ERR_ABORT; |
| 1634 | goto out; |
| 1635 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1636 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1637 | /* search the first action (connect / send / expect) in the list */ |
| 1638 | r = get_first_tcpcheck_rule(&srv->proxy->tcpcheck_rules); |
| 1639 | if (!r || (r->action != TCPCHK_ACT_CONNECT) || (!r->connect.port && !get_host_port(&r->connect.addr))) { |
| 1640 | ha_alert("config: %s '%s': server '%s' has neither service port nor check port " |
| 1641 | "nor tcp_check rule 'connect' with port information.\n", |
| 1642 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 1643 | ret |= ERR_ALERT | ERR_ABORT; |
| 1644 | goto out; |
| 1645 | } |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1646 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1647 | /* scan the tcp-check ruleset to ensure a port has been configured */ |
| 1648 | list_for_each_entry(r, srv->proxy->tcpcheck_rules.list, list) { |
Willy Tarreau | 565f333 | 2021-07-22 11:06:41 +0200 | [diff] [blame] | 1649 | if ((r->action == TCPCHK_ACT_CONNECT) && (!r->connect.port && !get_host_port(&r->connect.addr))) { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1650 | ha_alert("config: %s '%s': server '%s' has neither service port nor check port, " |
| 1651 | "and a tcp_check rule 'connect' with no port information.\n", |
| 1652 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 1653 | ret |= ERR_ALERT | ERR_ABORT; |
| 1654 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1655 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1656 | } |
| 1657 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1658 | init: |
| 1659 | if (!(srv->proxy->options2 & PR_O2_CHK_ANY)) { |
| 1660 | struct tcpcheck_ruleset *rs = NULL; |
| 1661 | struct tcpcheck_rules *rules = &srv->proxy->tcpcheck_rules; |
| 1662 | //char *errmsg = NULL; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1663 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1664 | srv->proxy->options2 &= ~PR_O2_CHK_ANY; |
| 1665 | srv->proxy->options2 |= PR_O2_TCPCHK_CHK; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1666 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1667 | rs = find_tcpcheck_ruleset("*tcp-check"); |
| 1668 | if (!rs) { |
| 1669 | rs = create_tcpcheck_ruleset("*tcp-check"); |
| 1670 | if (rs == NULL) { |
| 1671 | ha_alert("config: %s '%s': out of memory.\n", |
| 1672 | proxy_type_str(srv->proxy), srv->proxy->id); |
| 1673 | ret |= ERR_ALERT | ERR_FATAL; |
| 1674 | goto out; |
| 1675 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1676 | } |
| 1677 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1678 | free_tcpcheck_vars(&rules->preset_vars); |
| 1679 | rules->list = &rs->rules; |
| 1680 | rules->flags = 0; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1681 | } |
| 1682 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1683 | err = init_check(&srv->check, srv->proxy->options2 & PR_O2_CHK_ANY); |
| 1684 | if (err) { |
| 1685 | ha_alert("config: %s '%s': unable to init check for server '%s' (%s).\n", |
| 1686 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err); |
| 1687 | ret |= ERR_ALERT | ERR_ABORT; |
| 1688 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1689 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1690 | srv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED; |
| 1691 | global.maxsock++; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1692 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1693 | out: |
| 1694 | return ret; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1695 | } |
| 1696 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1697 | /* Initializes an agent-check attached to the server <srv>. Non-zero is returned |
| 1698 | * if an error occurred. |
| 1699 | */ |
| 1700 | static int init_srv_agent_check(struct server *srv) |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1701 | { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1702 | struct tcpcheck_rule *chk; |
| 1703 | const char *err; |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1704 | int ret = ERR_NONE; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1705 | |
Christopher Faulet | 6ecd593 | 2021-01-12 17:29:45 +0100 | [diff] [blame] | 1706 | if (!srv->do_agent || !(srv->proxy->cap & PR_CAP_BE)) |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1707 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1708 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1709 | /* If there is no connect rule preceding all send / expect rules, an |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1710 | * implicit one is inserted before all others. |
| 1711 | */ |
| 1712 | chk = get_first_tcpcheck_rule(srv->agent.tcpcheck_rules); |
| 1713 | if (!chk || chk->action != TCPCHK_ACT_CONNECT) { |
| 1714 | chk = calloc(1, sizeof(*chk)); |
| 1715 | if (!chk) { |
| 1716 | ha_alert("config : %s '%s': unable to add implicit tcp-check connect rule" |
| 1717 | " to agent-check for server '%s' (out of memory).\n", |
| 1718 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id); |
| 1719 | ret |= ERR_ALERT | ERR_FATAL; |
| 1720 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1721 | } |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1722 | chk->action = TCPCHK_ACT_CONNECT; |
| 1723 | chk->connect.options = (TCPCHK_OPT_DEFAULT_CONNECT|TCPCHK_OPT_IMPLICIT); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1724 | LIST_INSERT(srv->agent.tcpcheck_rules->list, &chk->list); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1725 | } |
| 1726 | |
Christopher Faulet | 8af4ab8 | 2022-08-24 11:38:03 +0200 | [diff] [blame] | 1727 | /* <chk> is always defined here and it is a CONNECT action. If there is |
| 1728 | * a preset variable, it means there is an agent string defined and data |
| 1729 | * will be sent after the connect. |
| 1730 | */ |
| 1731 | if (!LIST_ISEMPTY(&srv->agent.tcpcheck_rules->preset_vars)) |
| 1732 | chk->connect.options |= TCPCHK_OPT_HAS_DATA; |
| 1733 | |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1734 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1735 | err = init_check(&srv->agent, PR_O2_TCPCHK_CHK); |
| 1736 | if (err) { |
| 1737 | ha_alert("config: %s '%s': unable to init agent-check for server '%s' (%s).\n", |
| 1738 | proxy_type_str(srv->proxy), srv->proxy->id, srv->id, err); |
| 1739 | ret |= ERR_ALERT | ERR_ABORT; |
| 1740 | goto out; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1741 | } |
| 1742 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1743 | if (!srv->agent.inter) |
| 1744 | srv->agent.inter = srv->check.inter; |
| 1745 | |
| 1746 | srv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT; |
| 1747 | global.maxsock++; |
| 1748 | |
| 1749 | out: |
| 1750 | return ret; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1751 | } |
| 1752 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1753 | static void deinit_srv_check(struct server *srv) |
| 1754 | { |
| 1755 | if (srv->check.state & CHK_ST_CONFIGURED) |
| 1756 | free_check(&srv->check); |
| 1757 | srv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED; |
| 1758 | srv->do_check = 0; |
| 1759 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1760 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1761 | |
| 1762 | static void deinit_srv_agent_check(struct server *srv) |
| 1763 | { |
| 1764 | if (srv->agent.tcpcheck_rules) { |
| 1765 | free_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 1766 | ha_free(&srv->agent.tcpcheck_rules); |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1767 | } |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1768 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1769 | if (srv->agent.state & CHK_ST_CONFIGURED) |
| 1770 | free_check(&srv->agent); |
| 1771 | |
| 1772 | srv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT; |
| 1773 | srv->do_agent = 0; |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1774 | } |
| 1775 | |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 1776 | REGISTER_POST_SERVER_CHECK(init_srv_check); |
| 1777 | REGISTER_POST_SERVER_CHECK(init_srv_agent_check); |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 1778 | REGISTER_POST_CHECK(start_checks); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1779 | |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 1780 | REGISTER_SERVER_DEINIT(deinit_srv_check); |
| 1781 | REGISTER_SERVER_DEINIT(deinit_srv_agent_check); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1782 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1783 | |
| 1784 | /**************************************************************************/ |
| 1785 | /************************** Check sample fetches **************************/ |
| 1786 | /**************************************************************************/ |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 1787 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1788 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1789 | { /* END */ }, |
| 1790 | }}; |
| 1791 | |
| 1792 | INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws); |
| 1793 | |
| 1794 | |
| 1795 | /**************************************************************************/ |
| 1796 | /************************ Check's parsing functions ***********************/ |
| 1797 | /**************************************************************************/ |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 1798 | /* Parse the "addr" server keyword */ |
| 1799 | static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 1800 | char **errmsg) |
| 1801 | { |
| 1802 | struct sockaddr_storage *sk; |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 1803 | int port1, port2, err_code = 0; |
| 1804 | |
| 1805 | |
| 1806 | if (!*args[*cur_arg+1]) { |
| 1807 | memprintf(errmsg, "'%s' expects <ipv4|ipv6> as argument.", args[*cur_arg]); |
| 1808 | goto error; |
| 1809 | } |
| 1810 | |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1811 | sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, NULL, NULL, errmsg, NULL, NULL, |
| 1812 | PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT); |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 1813 | if (!sk) { |
| 1814 | memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg); |
| 1815 | goto error; |
| 1816 | } |
| 1817 | |
William Dauchy | 1c921cd | 2021-02-03 22:30:08 +0100 | [diff] [blame] | 1818 | srv->check.addr = *sk; |
| 1819 | /* if agentaddr was never set, we can use addr */ |
| 1820 | if (!(srv->flags & SRV_F_AGENTADDR)) |
| 1821 | srv->agent.addr = *sk; |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 1822 | |
| 1823 | out: |
| 1824 | return err_code; |
| 1825 | |
| 1826 | error: |
| 1827 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1828 | goto out; |
| 1829 | } |
| 1830 | |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 1831 | /* Parse the "agent-addr" server keyword */ |
| 1832 | static int srv_parse_agent_addr(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 1833 | char **errmsg) |
| 1834 | { |
William Dauchy | 1c921cd | 2021-02-03 22:30:08 +0100 | [diff] [blame] | 1835 | struct sockaddr_storage sk; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 1836 | int err_code = 0; |
| 1837 | |
| 1838 | if (!*(args[*cur_arg+1])) { |
| 1839 | memprintf(errmsg, "'%s' expects an address as argument.", args[*cur_arg]); |
| 1840 | goto error; |
| 1841 | } |
William Dauchy | 1c921cd | 2021-02-03 22:30:08 +0100 | [diff] [blame] | 1842 | memset(&sk, 0, sizeof(sk)); |
| 1843 | if (str2ip(args[*cur_arg + 1], &sk) == NULL) { |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 1844 | memprintf(errmsg, "parsing agent-addr failed. Check if '%s' is correct address.", args[*cur_arg+1]); |
| 1845 | goto error; |
| 1846 | } |
William Dauchy | 1c921cd | 2021-02-03 22:30:08 +0100 | [diff] [blame] | 1847 | set_srv_agent_addr(srv, &sk); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 1848 | |
| 1849 | out: |
| 1850 | return err_code; |
| 1851 | |
| 1852 | error: |
| 1853 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1854 | goto out; |
| 1855 | } |
| 1856 | |
| 1857 | /* Parse the "agent-check" server keyword */ |
| 1858 | static int srv_parse_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 1859 | char **errmsg) |
| 1860 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1861 | struct tcpcheck_ruleset *rs = NULL; |
| 1862 | struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules; |
| 1863 | struct tcpcheck_rule *chk; |
| 1864 | int err_code = 0; |
| 1865 | |
| 1866 | if (srv->do_agent) |
| 1867 | goto out; |
| 1868 | |
Christopher Faulet | 6ecd593 | 2021-01-12 17:29:45 +0100 | [diff] [blame] | 1869 | if (!(curpx->cap & PR_CAP_BE)) { |
| 1870 | memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability", |
| 1871 | args[*cur_arg], proxy_type_str(curpx), curpx->id); |
| 1872 | return ERR_WARN; |
| 1873 | } |
| 1874 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1875 | if (!rules) { |
| 1876 | rules = calloc(1, sizeof(*rules)); |
| 1877 | if (!rules) { |
| 1878 | memprintf(errmsg, "out of memory."); |
| 1879 | goto error; |
| 1880 | } |
| 1881 | LIST_INIT(&rules->preset_vars); |
| 1882 | srv->agent.tcpcheck_rules = rules; |
| 1883 | } |
| 1884 | rules->list = NULL; |
| 1885 | rules->flags = 0; |
| 1886 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1887 | rs = find_tcpcheck_ruleset("*agent-check"); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1888 | if (rs) |
| 1889 | goto ruleset_found; |
| 1890 | |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1891 | rs = create_tcpcheck_ruleset("*agent-check"); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1892 | if (rs == NULL) { |
| 1893 | memprintf(errmsg, "out of memory."); |
| 1894 | goto error; |
| 1895 | } |
| 1896 | |
Christopher Faulet | b50b3e6 | 2020-05-05 18:43:43 +0200 | [diff] [blame] | 1897 | chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-lf", "%[var(check.agent_string)]", ""}, |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1898 | 1, curpx, &rs->rules, srv->conf.file, srv->conf.line, errmsg); |
| 1899 | if (!chk) { |
| 1900 | memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg); |
| 1901 | goto error; |
| 1902 | } |
| 1903 | chk->index = 0; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1904 | LIST_APPEND(&rs->rules, &chk->list); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1905 | |
| 1906 | chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""}, |
Christopher Faulet | e5870d8 | 2020-04-15 11:32:03 +0200 | [diff] [blame] | 1907 | 1, curpx, &rs->rules, TCPCHK_RULES_AGENT_CHK, |
| 1908 | srv->conf.file, srv->conf.line, errmsg); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1909 | if (!chk) { |
| 1910 | memprintf(errmsg, "'%s': %s", args[*cur_arg], *errmsg); |
| 1911 | goto error; |
| 1912 | } |
| 1913 | chk->expect.custom = tcpcheck_agent_expect_reply; |
| 1914 | chk->index = 1; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1915 | LIST_APPEND(&rs->rules, &chk->list); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1916 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1917 | ruleset_found: |
| 1918 | rules->list = &rs->rules; |
Christopher Faulet | 1faf18a | 2020-11-25 16:43:12 +0100 | [diff] [blame] | 1919 | rules->flags &= ~(TCPCHK_RULES_PROTO_CHK|TCPCHK_RULES_UNUSED_RS); |
Christopher Faulet | 404f919 | 2020-04-09 23:13:54 +0200 | [diff] [blame] | 1920 | rules->flags |= TCPCHK_RULES_AGENT_CHK; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 1921 | srv->do_agent = 1; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1922 | |
| 1923 | out: |
Dirkjan Bussink | 1065e7e | 2021-06-18 19:57:49 +0000 | [diff] [blame] | 1924 | return err_code; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1925 | |
| 1926 | error: |
| 1927 | deinit_srv_agent_check(srv); |
Christopher Faulet | 61cc852 | 2020-04-20 14:54:42 +0200 | [diff] [blame] | 1928 | free_tcpcheck_ruleset(rs); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1929 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1930 | goto out; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | /* Parse the "agent-inter" server keyword */ |
| 1934 | static int srv_parse_agent_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 1935 | char **errmsg) |
| 1936 | { |
| 1937 | const char *err = NULL; |
| 1938 | unsigned int delay; |
| 1939 | int err_code = 0; |
| 1940 | |
| 1941 | if (!*(args[*cur_arg+1])) { |
| 1942 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 1943 | goto error; |
| 1944 | } |
| 1945 | |
| 1946 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 1947 | if (err == PARSE_TIME_OVER) { |
| 1948 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 1949 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 1950 | goto error; |
| 1951 | } |
| 1952 | else if (err == PARSE_TIME_UNDER) { |
| 1953 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 1954 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 1955 | goto error; |
| 1956 | } |
| 1957 | else if (err) { |
| 1958 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 1959 | *err, srv->id); |
| 1960 | goto error; |
| 1961 | } |
| 1962 | if (delay <= 0) { |
| 1963 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 1964 | delay, args[*cur_arg], srv->id); |
| 1965 | goto error; |
| 1966 | } |
| 1967 | srv->agent.inter = delay; |
| 1968 | |
| 1969 | out: |
| 1970 | return err_code; |
| 1971 | |
| 1972 | error: |
| 1973 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1974 | goto out; |
| 1975 | } |
| 1976 | |
| 1977 | /* Parse the "agent-port" server keyword */ |
| 1978 | static int srv_parse_agent_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 1979 | char **errmsg) |
| 1980 | { |
| 1981 | int err_code = 0; |
| 1982 | |
| 1983 | if (!*(args[*cur_arg+1])) { |
| 1984 | memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]); |
| 1985 | goto error; |
| 1986 | } |
| 1987 | |
| 1988 | global.maxsock++; |
William Dauchy | 4858fb2 | 2021-02-03 22:30:09 +0100 | [diff] [blame] | 1989 | set_srv_agent_port(srv, atol(args[*cur_arg + 1])); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 1990 | |
| 1991 | out: |
| 1992 | return err_code; |
| 1993 | |
| 1994 | error: |
| 1995 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1996 | goto out; |
| 1997 | } |
| 1998 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 1999 | int set_srv_agent_send(struct server *srv, const char *send) |
| 2000 | { |
| 2001 | struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules; |
| 2002 | struct tcpcheck_var *var = NULL; |
| 2003 | char *str; |
| 2004 | |
| 2005 | str = strdup(send); |
Christopher Faulet | b61caf4 | 2020-04-21 10:57:42 +0200 | [diff] [blame] | 2006 | var = create_tcpcheck_var(ist("check.agent_string")); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2007 | if (str == NULL || var == NULL) |
| 2008 | goto error; |
| 2009 | |
| 2010 | free_tcpcheck_vars(&rules->preset_vars); |
| 2011 | |
| 2012 | var->data.type = SMP_T_STR; |
| 2013 | var->data.u.str.area = str; |
| 2014 | var->data.u.str.data = strlen(str); |
| 2015 | LIST_INIT(&var->list); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2016 | LIST_APPEND(&rules->preset_vars, &var->list); |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2017 | |
| 2018 | return 1; |
| 2019 | |
| 2020 | error: |
| 2021 | free(str); |
| 2022 | free(var); |
| 2023 | return 0; |
| 2024 | } |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2025 | |
William Dauchy | f430090 | 2021-02-06 20:47:50 +0100 | [diff] [blame] | 2026 | /* set agent addr and appropriate flag */ |
William Dauchy | 1c921cd | 2021-02-03 22:30:08 +0100 | [diff] [blame] | 2027 | inline void set_srv_agent_addr(struct server *srv, struct sockaddr_storage *sk) |
| 2028 | { |
| 2029 | srv->agent.addr = *sk; |
| 2030 | srv->flags |= SRV_F_AGENTADDR; |
| 2031 | } |
| 2032 | |
William Dauchy | f430090 | 2021-02-06 20:47:50 +0100 | [diff] [blame] | 2033 | /* set agent port and appropriate flag */ |
William Dauchy | 4858fb2 | 2021-02-03 22:30:09 +0100 | [diff] [blame] | 2034 | inline void set_srv_agent_port(struct server *srv, int port) |
| 2035 | { |
| 2036 | srv->agent.port = port; |
| 2037 | srv->flags |= SRV_F_AGENTPORT; |
| 2038 | } |
| 2039 | |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2040 | /* Parse the "agent-send" server keyword */ |
| 2041 | static int srv_parse_agent_send(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2042 | char **errmsg) |
| 2043 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2044 | struct tcpcheck_rules *rules = srv->agent.tcpcheck_rules; |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2045 | int err_code = 0; |
| 2046 | |
| 2047 | if (!*(args[*cur_arg+1])) { |
| 2048 | memprintf(errmsg, "'%s' expects a string as argument.", args[*cur_arg]); |
| 2049 | goto error; |
| 2050 | } |
| 2051 | |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2052 | if (!rules) { |
| 2053 | rules = calloc(1, sizeof(*rules)); |
| 2054 | if (!rules) { |
| 2055 | memprintf(errmsg, "out of memory."); |
| 2056 | goto error; |
| 2057 | } |
| 2058 | LIST_INIT(&rules->preset_vars); |
| 2059 | srv->agent.tcpcheck_rules = rules; |
| 2060 | } |
| 2061 | |
| 2062 | if (!set_srv_agent_send(srv, args[*cur_arg+1])) { |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2063 | memprintf(errmsg, "out of memory."); |
| 2064 | goto error; |
| 2065 | } |
| 2066 | |
| 2067 | out: |
| 2068 | return err_code; |
| 2069 | |
| 2070 | error: |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2071 | deinit_srv_agent_check(srv); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2072 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2073 | goto out; |
| 2074 | } |
| 2075 | |
| 2076 | /* Parse the "no-agent-send" server keyword */ |
| 2077 | static int srv_parse_no_agent_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2078 | char **errmsg) |
| 2079 | { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2080 | deinit_srv_agent_check(srv); |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2081 | return 0; |
| 2082 | } |
| 2083 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 2084 | /* Parse the "check" server keyword */ |
| 2085 | static int srv_parse_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2086 | char **errmsg) |
| 2087 | { |
Christopher Faulet | 6ecd593 | 2021-01-12 17:29:45 +0100 | [diff] [blame] | 2088 | if (!(curpx->cap & PR_CAP_BE)) { |
| 2089 | memprintf(errmsg, "'%s' ignored because %s '%s' has no backend capability", |
| 2090 | args[*cur_arg], proxy_type_str(curpx), curpx->id); |
| 2091 | return ERR_WARN; |
| 2092 | } |
| 2093 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 2094 | srv->do_check = 1; |
| 2095 | return 0; |
| 2096 | } |
| 2097 | |
| 2098 | /* Parse the "check-send-proxy" server keyword */ |
| 2099 | static int srv_parse_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2100 | char **errmsg) |
| 2101 | { |
| 2102 | srv->check.send_proxy = 1; |
| 2103 | return 0; |
| 2104 | } |
| 2105 | |
| 2106 | /* Parse the "check-via-socks4" server keyword */ |
| 2107 | static int srv_parse_check_via_socks4(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2108 | char **errmsg) |
| 2109 | { |
| 2110 | srv->check.via_socks4 = 1; |
| 2111 | return 0; |
| 2112 | } |
| 2113 | |
| 2114 | /* Parse the "no-check" server keyword */ |
| 2115 | static int srv_parse_no_check(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2116 | char **errmsg) |
| 2117 | { |
| 2118 | deinit_srv_check(srv); |
| 2119 | return 0; |
| 2120 | } |
| 2121 | |
| 2122 | /* Parse the "no-check-send-proxy" server keyword */ |
| 2123 | static int srv_parse_no_check_send_proxy(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2124 | char **errmsg) |
| 2125 | { |
| 2126 | srv->check.send_proxy = 0; |
| 2127 | return 0; |
| 2128 | } |
| 2129 | |
Christopher Faulet | edc6ed9 | 2020-04-23 16:27:59 +0200 | [diff] [blame] | 2130 | /* parse the "check-proto" server keyword */ |
| 2131 | static int srv_parse_check_proto(char **args, int *cur_arg, |
| 2132 | struct proxy *px, struct server *newsrv, char **err) |
| 2133 | { |
| 2134 | int err_code = 0; |
| 2135 | |
| 2136 | if (!*args[*cur_arg + 1]) { |
| 2137 | memprintf(err, "'%s' : missing value", args[*cur_arg]); |
| 2138 | goto error; |
| 2139 | } |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 2140 | newsrv->check.mux_proto = get_mux_proto(ist(args[*cur_arg + 1])); |
Christopher Faulet | edc6ed9 | 2020-04-23 16:27:59 +0200 | [diff] [blame] | 2141 | if (!newsrv->check.mux_proto) { |
| 2142 | memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]); |
| 2143 | goto error; |
| 2144 | } |
| 2145 | |
| 2146 | out: |
| 2147 | return err_code; |
| 2148 | |
| 2149 | error: |
| 2150 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2151 | goto out; |
| 2152 | } |
| 2153 | |
| 2154 | |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 2155 | /* Parse the "rise" server keyword */ |
| 2156 | static int srv_parse_check_rise(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.rise = atol(args[*cur_arg+1]); |
| 2167 | if (srv->check.rise <= 0) { |
| 2168 | memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]); |
| 2169 | goto error; |
| 2170 | } |
| 2171 | |
| 2172 | if (srv->check.health) |
| 2173 | srv->check.health = srv->check.rise; |
| 2174 | |
| 2175 | out: |
| 2176 | return err_code; |
| 2177 | |
| 2178 | error: |
| 2179 | deinit_srv_agent_check(srv); |
| 2180 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2181 | goto out; |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | /* Parse the "fall" server keyword */ |
| 2185 | static int srv_parse_check_fall(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2186 | char **errmsg) |
| 2187 | { |
| 2188 | int err_code = 0; |
| 2189 | |
| 2190 | if (!*args[*cur_arg + 1]) { |
| 2191 | memprintf(errmsg, "'%s' expects an integer argument.", args[*cur_arg]); |
| 2192 | goto error; |
| 2193 | } |
| 2194 | |
| 2195 | srv->check.fall = atol(args[*cur_arg+1]); |
| 2196 | if (srv->check.fall <= 0) { |
| 2197 | memprintf(errmsg, "'%s' has to be > 0.", args[*cur_arg]); |
| 2198 | goto error; |
| 2199 | } |
| 2200 | |
| 2201 | out: |
| 2202 | return err_code; |
| 2203 | |
| 2204 | error: |
| 2205 | deinit_srv_agent_check(srv); |
| 2206 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2207 | goto out; |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | /* Parse the "inter" server keyword */ |
| 2211 | static int srv_parse_check_inter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2212 | char **errmsg) |
| 2213 | { |
| 2214 | const char *err = NULL; |
| 2215 | unsigned int delay; |
| 2216 | int err_code = 0; |
| 2217 | |
| 2218 | if (!*(args[*cur_arg+1])) { |
| 2219 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 2220 | goto error; |
| 2221 | } |
| 2222 | |
| 2223 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 2224 | if (err == PARSE_TIME_OVER) { |
| 2225 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 2226 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 2227 | goto error; |
| 2228 | } |
| 2229 | else if (err == PARSE_TIME_UNDER) { |
| 2230 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 2231 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 2232 | goto error; |
| 2233 | } |
| 2234 | else if (err) { |
| 2235 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 2236 | *err, srv->id); |
| 2237 | goto error; |
| 2238 | } |
| 2239 | if (delay <= 0) { |
| 2240 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 2241 | delay, args[*cur_arg], srv->id); |
| 2242 | goto error; |
| 2243 | } |
| 2244 | srv->check.inter = delay; |
| 2245 | |
| 2246 | out: |
| 2247 | return err_code; |
| 2248 | |
| 2249 | error: |
| 2250 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2251 | goto out; |
| 2252 | } |
| 2253 | |
| 2254 | |
| 2255 | /* Parse the "fastinter" server keyword */ |
| 2256 | static int srv_parse_check_fastinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2257 | char **errmsg) |
| 2258 | { |
| 2259 | const char *err = NULL; |
| 2260 | unsigned int delay; |
| 2261 | int err_code = 0; |
| 2262 | |
| 2263 | if (!*(args[*cur_arg+1])) { |
| 2264 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 2265 | goto error; |
| 2266 | } |
| 2267 | |
| 2268 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 2269 | if (err == PARSE_TIME_OVER) { |
| 2270 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 2271 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 2272 | goto error; |
| 2273 | } |
| 2274 | else if (err == PARSE_TIME_UNDER) { |
| 2275 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 2276 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 2277 | goto error; |
| 2278 | } |
| 2279 | else if (err) { |
| 2280 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 2281 | *err, srv->id); |
| 2282 | goto error; |
| 2283 | } |
| 2284 | if (delay <= 0) { |
| 2285 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 2286 | delay, args[*cur_arg], srv->id); |
| 2287 | goto error; |
| 2288 | } |
| 2289 | srv->check.fastinter = delay; |
| 2290 | |
| 2291 | out: |
| 2292 | return err_code; |
| 2293 | |
| 2294 | error: |
| 2295 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2296 | goto out; |
| 2297 | } |
| 2298 | |
| 2299 | |
| 2300 | /* Parse the "downinter" server keyword */ |
| 2301 | static int srv_parse_check_downinter(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2302 | char **errmsg) |
| 2303 | { |
| 2304 | const char *err = NULL; |
| 2305 | unsigned int delay; |
| 2306 | int err_code = 0; |
| 2307 | |
| 2308 | if (!*(args[*cur_arg+1])) { |
| 2309 | memprintf(errmsg, "'%s' expects a delay as argument.", args[*cur_arg]); |
| 2310 | goto error; |
| 2311 | } |
| 2312 | |
| 2313 | err = parse_time_err(args[*cur_arg+1], &delay, TIME_UNIT_MS); |
| 2314 | if (err == PARSE_TIME_OVER) { |
| 2315 | memprintf(errmsg, "timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 2316 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 2317 | goto error; |
| 2318 | } |
| 2319 | else if (err == PARSE_TIME_UNDER) { |
| 2320 | memprintf(errmsg, "timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 2321 | args[*cur_arg+1], args[*cur_arg], srv->id); |
| 2322 | goto error; |
| 2323 | } |
| 2324 | else if (err) { |
| 2325 | memprintf(errmsg, "unexpected character '%c' in 'agent-inter' argument of server %s.", |
| 2326 | *err, srv->id); |
| 2327 | goto error; |
| 2328 | } |
| 2329 | if (delay <= 0) { |
| 2330 | memprintf(errmsg, "invalid value %d for argument '%s' of server %s.", |
| 2331 | delay, args[*cur_arg], srv->id); |
| 2332 | goto error; |
| 2333 | } |
| 2334 | srv->check.downinter = delay; |
| 2335 | |
| 2336 | out: |
| 2337 | return err_code; |
| 2338 | |
| 2339 | error: |
| 2340 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2341 | goto out; |
| 2342 | } |
| 2343 | |
| 2344 | /* Parse the "port" server keyword */ |
| 2345 | static int srv_parse_check_port(char **args, int *cur_arg, struct proxy *curpx, struct server *srv, |
| 2346 | char **errmsg) |
| 2347 | { |
| 2348 | int err_code = 0; |
| 2349 | |
| 2350 | if (!*(args[*cur_arg+1])) { |
| 2351 | memprintf(errmsg, "'%s' expects a port number as argument.", args[*cur_arg]); |
| 2352 | goto error; |
| 2353 | } |
| 2354 | |
| 2355 | global.maxsock++; |
| 2356 | srv->check.port = atol(args[*cur_arg+1]); |
William Dauchy | 4858fb2 | 2021-02-03 22:30:09 +0100 | [diff] [blame] | 2357 | /* if agentport was never set, we can use port */ |
| 2358 | if (!(srv->flags & SRV_F_AGENTPORT)) |
| 2359 | srv->agent.port = srv->check.port; |
Christopher Faulet | ce8111e | 2020-04-06 15:04:11 +0200 | [diff] [blame] | 2360 | |
| 2361 | out: |
| 2362 | return err_code; |
| 2363 | |
| 2364 | error: |
| 2365 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2366 | goto out; |
| 2367 | } |
| 2368 | |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2369 | static struct srv_kw_list srv_kws = { "CHK", { }, { |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2370 | { "addr", srv_parse_addr, 1, 1, 0 }, /* IP address to send health to or to probe from agent-check */ |
| 2371 | { "agent-addr", srv_parse_agent_addr, 1, 1, 0 }, /* Enable an auxiliary agent check */ |
| 2372 | { "agent-check", srv_parse_agent_check, 0, 1, 0 }, /* Enable agent checks */ |
| 2373 | { "agent-inter", srv_parse_agent_inter, 1, 1, 0 }, /* Set the interval between two agent checks */ |
| 2374 | { "agent-port", srv_parse_agent_port, 1, 1, 0 }, /* Set the TCP port used for agent checks. */ |
| 2375 | { "agent-send", srv_parse_agent_send, 1, 1, 0 }, /* Set string to send to agent. */ |
| 2376 | { "check", srv_parse_check, 0, 1, 0 }, /* Enable health checks */ |
| 2377 | { "check-proto", srv_parse_check_proto, 1, 1, 0 }, /* Set the mux protocol for health checks */ |
| 2378 | { "check-send-proxy", srv_parse_check_send_proxy, 0, 1, 0 }, /* Enable PROXY protocol for health checks */ |
| 2379 | { "check-via-socks4", srv_parse_check_via_socks4, 0, 1, 0 }, /* Enable socks4 proxy for health checks */ |
| 2380 | { "no-agent-check", srv_parse_no_agent_check, 0, 1, 0 }, /* Do not enable any auxiliary agent check */ |
| 2381 | { "no-check", srv_parse_no_check, 0, 1, 0 }, /* Disable health checks */ |
| 2382 | { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1, 0 }, /* Disable PROXY protocol for health checks */ |
| 2383 | { "rise", srv_parse_check_rise, 1, 1, 0 }, /* Set rise value for health checks */ |
| 2384 | { "fall", srv_parse_check_fall, 1, 1, 0 }, /* Set fall value for health checks */ |
| 2385 | { "inter", srv_parse_check_inter, 1, 1, 0 }, /* Set inter value for health checks */ |
| 2386 | { "fastinter", srv_parse_check_fastinter, 1, 1, 0 }, /* Set fastinter value for health checks */ |
| 2387 | { "downinter", srv_parse_check_downinter, 1, 1, 0 }, /* Set downinter value for health checks */ |
| 2388 | { "port", srv_parse_check_port, 1, 1, 0 }, /* Set the TCP port used for health checks. */ |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2389 | { NULL, NULL, 0 }, |
| 2390 | }}; |
| 2391 | |
Christopher Faulet | cbba66c | 2020-04-06 14:26:30 +0200 | [diff] [blame] | 2392 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
Christopher Faulet | fd6c229 | 2020-03-25 18:20:15 +0100 | [diff] [blame] | 2393 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2394 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2395 | * Local variables: |
| 2396 | * c-indent-level: 8 |
| 2397 | * c-basic-offset: 8 |
| 2398 | * End: |
| 2399 | */ |