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