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> |
| 18 | #include <stdio.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 19 | #include <stdlib.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 20 | #include <string.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 21 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | #include <sys/socket.h> |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 24 | #include <sys/types.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 25 | #include <netinet/in.h> |
Willy Tarreau | 1274bc4 | 2009-07-15 07:16:31 +0200 | [diff] [blame] | 26 | #include <netinet/tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | #include <arpa/inet.h> |
| 28 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 29 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/compat.h> |
| 31 | #include <common/config.h> |
| 32 | #include <common/mini-clist.h> |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 33 | #include <common/standard.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 34 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | |
| 36 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 38 | #ifdef USE_OPENSSL |
| 39 | #include <types/ssl_sock.h> |
| 40 | #include <proto/ssl_sock.h> |
| 41 | #endif /* USE_OPENSSL */ |
| 42 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | #include <proto/backend.h> |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 44 | #include <proto/checks.h> |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 45 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | #include <proto/fd.h> |
| 47 | #include <proto/log.h> |
| 48 | #include <proto/queue.h> |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 49 | #include <proto/port_range.h> |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 50 | #include <proto/proto_http.h> |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 51 | #include <proto/proto_tcp.h> |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 52 | #include <proto/protocol.h> |
Willy Tarreau | 2b5652f | 2006-12-31 17:46:05 +0100 | [diff] [blame] | 53 | #include <proto/proxy.h> |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 54 | #include <proto/raw_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | #include <proto/server.h> |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 56 | #include <proto/session.h> |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 57 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/task.h> |
| 59 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 60 | static int httpchk_expect(struct server *s, int done); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 61 | static int tcpcheck_get_step_id(struct server *); |
| 62 | static void tcpcheck_main(struct connection *); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 63 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 64 | static const struct check_status check_statuses[HCHK_STATUS_SIZE] = { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 65 | [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" }, |
| 66 | [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" }, |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 67 | [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 68 | |
Willy Tarreau | 2396418 | 2014-05-20 20:56:30 +0200 | [diff] [blame] | 69 | /* Below we have finished checks */ |
| 70 | [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" }, |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 71 | [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" }, |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 72 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 73 | [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 74 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 75 | [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" }, |
| 76 | [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" }, |
| 77 | [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 78 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 79 | [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" }, |
| 80 | [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" }, |
| 81 | [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 82 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 83 | [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" }, |
| 84 | [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 85 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 86 | [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 87 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 88 | [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" }, |
| 89 | [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" }, |
| 90 | [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 93 | 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] | 94 | [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }}, |
| 95 | |
| 96 | [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }}, |
| 97 | [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }}, |
| 98 | |
| 99 | [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }}, |
| 100 | [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }}, |
| 101 | [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }}, |
| 102 | [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }}, |
| 103 | |
| 104 | [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }}, |
| 105 | [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }}, |
| 106 | [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }}, |
| 107 | }; |
| 108 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 109 | /* |
| 110 | * Convert check_status code to description |
| 111 | */ |
| 112 | const char *get_check_status_description(short check_status) { |
| 113 | |
| 114 | const char *desc; |
| 115 | |
| 116 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 117 | desc = check_statuses[check_status].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 118 | else |
| 119 | desc = NULL; |
| 120 | |
| 121 | if (desc && *desc) |
| 122 | return desc; |
| 123 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 124 | return check_statuses[HCHK_STATUS_UNKNOWN].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Convert check_status code to short info |
| 129 | */ |
| 130 | const char *get_check_status_info(short check_status) { |
| 131 | |
| 132 | const char *info; |
| 133 | |
| 134 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 135 | info = check_statuses[check_status].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 136 | else |
| 137 | info = NULL; |
| 138 | |
| 139 | if (info && *info) |
| 140 | return info; |
| 141 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 142 | return check_statuses[HCHK_STATUS_UNKNOWN].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 145 | const char *get_analyze_status(short analyze_status) { |
| 146 | |
| 147 | const char *desc; |
| 148 | |
| 149 | if (analyze_status < HANA_STATUS_SIZE) |
| 150 | desc = analyze_statuses[analyze_status].desc; |
| 151 | else |
| 152 | desc = NULL; |
| 153 | |
| 154 | if (desc && *desc) |
| 155 | return desc; |
| 156 | else |
| 157 | return analyze_statuses[HANA_STATUS_UNKNOWN].desc; |
| 158 | } |
| 159 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 160 | /* Builds a string containing some information about the health check's result. |
| 161 | * The output string is allocated from the trash chunks. If the check is NULL, |
| 162 | * NULL is returned. This is designed to be used when emitting logs about health |
| 163 | * checks. |
Willy Tarreau | ddd329c | 2014-05-16 16:46:12 +0200 | [diff] [blame] | 164 | */ |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 165 | static const char *check_reason_string(struct check *check) |
Willy Tarreau | ddd329c | 2014-05-16 16:46:12 +0200 | [diff] [blame] | 166 | { |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 167 | struct chunk *msg; |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 168 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 169 | if (!check) |
| 170 | return NULL; |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 171 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 172 | msg = get_trash_chunk(); |
| 173 | chunk_printf(msg, "reason: %s", get_check_status_description(check->status)); |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 174 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 175 | if (check->status >= HCHK_STATUS_L57DATA) |
| 176 | chunk_appendf(msg, ", code: %d", check->code); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 177 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 178 | if (*check->desc) { |
| 179 | struct chunk src; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 180 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 181 | chunk_appendf(msg, ", info: \""); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 182 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 183 | chunk_initlen(&src, check->desc, 0, strlen(check->desc)); |
| 184 | chunk_asciiencode(msg, &src, '"'); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 185 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 186 | chunk_appendf(msg, "\""); |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 189 | if (check->duration >= 0) |
| 190 | chunk_appendf(msg, ", check duration: %ldms", check->duration); |
| 191 | |
| 192 | return msg->str; |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 195 | /* |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 196 | * Set check->status, update check->duration and fill check->result with |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 197 | * an adequate CHK_RES_* value. The new check->health is computed based |
| 198 | * on the result. |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 199 | * |
| 200 | * Show information in logs about failed health check if server is UP |
| 201 | * or succeeded health checks if server is DOWN. |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 202 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 203 | static 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] | 204 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 205 | struct server *s = check->server; |
Willy Tarreau | bef1b32 | 2014-05-13 21:01:39 +0200 | [diff] [blame] | 206 | short prev_status = check->status; |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 207 | int report = 0; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 208 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 209 | if (status == HCHK_STATUS_START) { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 210 | check->result = CHK_RES_UNKNOWN; /* no result yet */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 211 | check->desc[0] = '\0'; |
| 212 | check->start = now; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 213 | return; |
| 214 | } |
| 215 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 216 | if (!check->status) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 217 | return; |
| 218 | |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 219 | if (desc && *desc) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 220 | strncpy(check->desc, desc, HCHK_DESC_LEN-1); |
| 221 | check->desc[HCHK_DESC_LEN-1] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 222 | } else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 223 | check->desc[0] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 224 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 225 | check->status = status; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 226 | if (check_statuses[status].result) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 227 | check->result = check_statuses[status].result; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 228 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 229 | if (status == HCHK_STATUS_HANA) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 230 | check->duration = -1; |
| 231 | else if (!tv_iszero(&check->start)) { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 232 | /* set_server_check_status() may be called more than once */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 233 | check->duration = tv_ms_elapsed(&check->start, &now); |
| 234 | tv_zero(&check->start); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Willy Tarreau | 2396418 | 2014-05-20 20:56:30 +0200 | [diff] [blame] | 237 | /* no change is expected if no state change occurred */ |
| 238 | if (check->result == CHK_RES_NEUTRAL) |
| 239 | return; |
| 240 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 241 | report = 0; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 242 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 243 | switch (check->result) { |
| 244 | case CHK_RES_FAILED: |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 245 | /* Failure to connect to the agent as a secondary check should not |
| 246 | * cause the server to be marked down. |
| 247 | */ |
| 248 | if ((!(check->state & CHK_ST_AGENT) || |
| 249 | (check->status >= HCHK_STATUS_L7TOUT)) && |
| 250 | (check->health >= check->rise)) { |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 251 | s->counters.failed_checks++; |
| 252 | report = 1; |
| 253 | check->health--; |
| 254 | if (check->health < check->rise) |
| 255 | check->health = 0; |
| 256 | } |
| 257 | break; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 258 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 259 | case CHK_RES_PASSED: |
| 260 | case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */ |
| 261 | if ((check->health < check->rise + check->fall - 1) && |
| 262 | (check->result == CHK_RES_PASSED || check->health > 0)) { |
| 263 | report = 1; |
| 264 | check->health++; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 265 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 266 | if (check->health >= check->rise) |
| 267 | check->health = check->rise + check->fall - 1; /* OK now */ |
| 268 | } |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 269 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 270 | /* clear consecutive_errors if observing is enabled */ |
| 271 | if (s->onerror) |
| 272 | s->consecutive_errors = 0; |
| 273 | break; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 274 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 275 | default: |
| 276 | break; |
| 277 | } |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 278 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 279 | if (s->proxy->options2 & PR_O2_LOGHCHKS && |
| 280 | (status != prev_status || report)) { |
| 281 | chunk_printf(&trash, |
Willy Tarreau | 12634e1 | 2014-05-23 11:32:36 +0200 | [diff] [blame] | 282 | "%s check for %sserver %s/%s %s%s", |
| 283 | (check->state & CHK_ST_AGENT) ? "Agent" : "Health", |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 284 | s->flags & SRV_F_BACKUP ? "backup " : "", |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 285 | s->proxy->id, s->id, |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 286 | (check->result == CHK_RES_CONDPASS) ? "conditionally ":"", |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 287 | (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 288 | |
Willy Tarreau | a150cf1 | 2014-05-20 21:57:23 +0200 | [diff] [blame] | 289 | srv_append_status(&trash, s, check_reason_string(check), -1, 0); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 290 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 291 | chunk_appendf(&trash, ", status: %d/%d %s", |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 292 | (check->health >= check->rise) ? check->health - check->rise + 1 : check->health, |
| 293 | (check->health >= check->rise) ? check->fall : check->rise, |
| 294 | (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 295 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 296 | Warning("%s.\n", trash.str); |
| 297 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 298 | } |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 301 | /* Marks the check <check>'s server down if the current check is already failed |
| 302 | * and the server is not down yet nor in maintenance. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 303 | */ |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 304 | static void check_notify_failure(struct check *check) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 305 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 306 | struct server *s = check->server; |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 307 | |
Willy Tarreau | 7b1d47c | 2014-05-20 14:55:13 +0200 | [diff] [blame] | 308 | /* The agent secondary check should only cause a server to be marked |
| 309 | * as down if check->status is HCHK_STATUS_L7STS, which indicates |
| 310 | * that the agent returned "fail", "stopped" or "down". |
| 311 | * The implication here is that failure to connect to the agent |
| 312 | * as a secondary check should not cause the server to be marked |
| 313 | * down. */ |
| 314 | if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS) |
| 315 | return; |
| 316 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 317 | if (check->health > 0) |
| 318 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 319 | |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 320 | /* We only report a reason for the check if we did not do so previously */ |
| 321 | srv_set_stopped(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 324 | /* 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] | 325 | * it isn't in maintenance, it is not tracking a down server and other checks |
| 326 | * comply. The rule is simple : by default, a server is up, unless any of the |
| 327 | * following conditions is true : |
| 328 | * - health check failed (check->health < rise) |
| 329 | * - agent check failed (agent->health < rise) |
| 330 | * - the server tracks a down server (track && track->state == STOPPED) |
| 331 | * Note that if the server has a slowstart, it will switch to STARTING instead |
| 332 | * of RUNNING. Also, only the health checks support the nolb mode, so the |
| 333 | * agent's success may not take the server out of this mode. |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 334 | */ |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 335 | static void check_notify_success(struct check *check) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 336 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 337 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 338 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 339 | if (s->admin & SRV_ADMF_MAINT) |
| 340 | return; |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 341 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 342 | if (s->track && s->track->state == SRV_ST_STOPPED) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 343 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 344 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 345 | if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise)) |
| 346 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 347 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 348 | if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise)) |
| 349 | return; |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 350 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 351 | if ((check->state & CHK_ST_AGENT) && s->state == SRV_ST_STOPPING) |
| 352 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 353 | |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 354 | srv_set_running(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 355 | } |
| 356 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 357 | /* Marks the check <check> as valid and tries to set its server into stopping mode |
| 358 | * if it was running or starting, and provided it isn't in maintenance and other |
| 359 | * checks comply. The conditions for the server to be marked in stopping mode are |
| 360 | * the same as for it to be turned up. Also, only the health checks support the |
| 361 | * nolb mode. |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 362 | */ |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 363 | static void check_notify_stopping(struct check *check) |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 364 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 365 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 366 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 367 | if (s->admin & SRV_ADMF_MAINT) |
| 368 | return; |
| 369 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 370 | if (check->state & CHK_ST_AGENT) |
| 371 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 372 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 373 | if (s->track && s->track->state == SRV_ST_STOPPED) |
| 374 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 375 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 376 | if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise)) |
| 377 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 378 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 379 | if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise)) |
| 380 | return; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 381 | |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 382 | srv_set_stopping(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 383 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 384 | |
Willy Tarreau | 9fe7aae | 2013-12-31 23:47:37 +0100 | [diff] [blame] | 385 | /* note: use health_adjust() only, which first checks that the observe mode is |
| 386 | * enabled. |
| 387 | */ |
| 388 | void __health_adjust(struct server *s, short status) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 389 | { |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 390 | int failed; |
| 391 | int expire; |
| 392 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 393 | if (s->observe >= HANA_OBS_SIZE) |
| 394 | return; |
| 395 | |
Willy Tarreau | bb95666 | 2013-01-24 00:37:39 +0100 | [diff] [blame] | 396 | if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc) |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 397 | return; |
| 398 | |
| 399 | switch (analyze_statuses[status].lr[s->observe - 1]) { |
| 400 | case 1: |
| 401 | failed = 1; |
| 402 | break; |
| 403 | |
| 404 | case 2: |
| 405 | failed = 0; |
| 406 | break; |
| 407 | |
| 408 | default: |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | if (!failed) { |
| 413 | /* good: clear consecutive_errors */ |
| 414 | s->consecutive_errors = 0; |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | s->consecutive_errors++; |
| 419 | |
| 420 | if (s->consecutive_errors < s->consecutive_errors_limit) |
| 421 | return; |
| 422 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 423 | chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s", |
| 424 | s->consecutive_errors, get_analyze_status(status)); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 425 | |
| 426 | switch (s->onerror) { |
| 427 | case HANA_ONERR_FASTINTER: |
| 428 | /* force fastinter - nothing to do here as all modes force it */ |
| 429 | break; |
| 430 | |
| 431 | case HANA_ONERR_SUDDTH: |
| 432 | /* simulate a pre-fatal failed health check */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 433 | if (s->check.health > s->check.rise) |
| 434 | s->check.health = s->check.rise + 1; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 435 | |
| 436 | /* no break - fall through */ |
| 437 | |
| 438 | case HANA_ONERR_FAILCHK: |
| 439 | /* simulate a failed health check */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 440 | set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 441 | check_notify_failure(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 442 | break; |
| 443 | |
| 444 | case HANA_ONERR_MARKDWN: |
| 445 | /* mark server down */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 446 | s->check.health = s->check.rise; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 447 | set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str); |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 448 | check_notify_failure(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 449 | break; |
| 450 | |
| 451 | default: |
| 452 | /* write a warning? */ |
| 453 | break; |
| 454 | } |
| 455 | |
| 456 | s->consecutive_errors = 0; |
| 457 | s->counters.failed_hana++; |
| 458 | |
Simon Horman | 6618300 | 2013-02-23 10:16:43 +0900 | [diff] [blame] | 459 | if (s->check.fastinter) { |
| 460 | expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter)); |
Sergiy Prykhodko | 1d57e50 | 2013-09-21 12:05:00 +0300 | [diff] [blame] | 461 | if (s->check.task->expire > expire) { |
Willy Tarreau | 5b3a202 | 2012-09-28 15:01:02 +0200 | [diff] [blame] | 462 | s->check.task->expire = expire; |
Sergiy Prykhodko | 1d57e50 | 2013-09-21 12:05:00 +0300 | [diff] [blame] | 463 | /* requeue check task with new expire */ |
| 464 | task_queue(s->check.task); |
| 465 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
Willy Tarreau | a1dab55 | 2014-04-14 15:04:54 +0200 | [diff] [blame] | 469 | static int httpchk_build_status_header(struct server *s, char *buffer, int size) |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 470 | { |
| 471 | int sv_state; |
| 472 | int ratio; |
| 473 | int hlen = 0; |
| 474 | const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d", |
| 475 | "UP %d/%d", "UP", |
| 476 | "NOLB %d/%d", "NOLB", |
| 477 | "no check" }; |
| 478 | |
| 479 | memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24); |
| 480 | hlen += 24; |
| 481 | |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 482 | if (!(s->check.state & CHK_ST_ENABLED)) |
| 483 | sv_state = 6; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 484 | else if (s->state != SRV_ST_STOPPED) { |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 485 | if (s->check.health == s->check.rise + s->check.fall - 1) |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 486 | sv_state = 3; /* UP */ |
| 487 | else |
| 488 | sv_state = 2; /* going down */ |
| 489 | |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 490 | if (s->state == SRV_ST_STOPPING) |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 491 | sv_state += 2; |
| 492 | } else { |
Simon Horman | 125d099 | 2013-02-24 17:23:38 +0900 | [diff] [blame] | 493 | if (s->check.health) |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 494 | sv_state = 1; /* going up */ |
| 495 | else |
| 496 | sv_state = 0; /* DOWN */ |
| 497 | } |
| 498 | |
Willy Tarreau | a1dab55 | 2014-04-14 15:04:54 +0200 | [diff] [blame] | 499 | hlen += snprintf(buffer + hlen, size - hlen, |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 500 | srv_hlt_st[sv_state], |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 501 | (s->state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health), |
| 502 | (s->state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise)); |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 503 | |
Willy Tarreau | a1dab55 | 2014-04-14 15:04:54 +0200 | [diff] [blame] | 504 | hlen += snprintf(buffer + hlen, size - hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d", |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 505 | s->proxy->id, s->id, |
| 506 | global.node, |
| 507 | (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 508 | (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 509 | s->cur_sess, s->proxy->beconn - s->proxy->nbpend, |
| 510 | s->nbpend); |
| 511 | |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 512 | if ((s->state == SRV_ST_STARTING) && |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 513 | now.tv_sec < s->last_change + s->slowstart && |
| 514 | now.tv_sec >= s->last_change) { |
| 515 | ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart); |
Willy Tarreau | a1dab55 | 2014-04-14 15:04:54 +0200 | [diff] [blame] | 516 | hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio); |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | buffer[hlen++] = '\r'; |
| 520 | buffer[hlen++] = '\n'; |
| 521 | |
| 522 | return hlen; |
| 523 | } |
| 524 | |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 525 | /* Check the connection. If an error has already been reported or the socket is |
| 526 | * closed, keep errno intact as it is supposed to contain the valid error code. |
| 527 | * If no error is reported, check the socket's error queue using getsockopt(). |
| 528 | * Warning, this must be done only once when returning from poll, and never |
| 529 | * after an I/O error was attempted, otherwise the error queue might contain |
| 530 | * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the |
| 531 | * socket. Returns non-zero if an error was reported, zero if everything is |
| 532 | * clean (including a properly closed socket). |
| 533 | */ |
| 534 | static int retrieve_errno_from_socket(struct connection *conn) |
| 535 | { |
| 536 | int skerr; |
| 537 | socklen_t lskerr = sizeof(skerr); |
| 538 | |
| 539 | if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl)) |
| 540 | return 1; |
| 541 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 542 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 543 | return 0; |
| 544 | |
| 545 | if (getsockopt(conn->t.sock.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0) |
| 546 | errno = skerr; |
| 547 | |
| 548 | if (errno == EAGAIN) |
| 549 | errno = 0; |
| 550 | |
| 551 | if (!errno) { |
| 552 | /* we could not retrieve an error, that does not mean there is |
| 553 | * none. Just don't change anything and only report the prior |
| 554 | * error if any. |
| 555 | */ |
| 556 | if (conn->flags & CO_FL_ERROR) |
| 557 | return 1; |
| 558 | else |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH; |
| 563 | return 1; |
| 564 | } |
| 565 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 566 | /* Try to collect as much information as possible on the connection status, |
| 567 | * and adjust the server status accordingly. It may make use of <errno_bck> |
| 568 | * if non-null when the caller is absolutely certain of its validity (eg: |
| 569 | * checked just after a syscall). If the caller doesn't have a valid errno, |
| 570 | * it can pass zero, and retrieve_errno_from_socket() will be called to try |
| 571 | * to extract errno from the socket. If no error is reported, it will consider |
| 572 | * the <expired> flag. This is intended to be used when a connection error was |
| 573 | * reported in conn->flags or when a timeout was reported in <expired>. The |
| 574 | * function takes care of not updating a server status which was already set. |
| 575 | * All situations where at least one of <expired> or CO_FL_ERROR are set |
| 576 | * produce a status. |
| 577 | */ |
| 578 | static void chk_report_conn_err(struct connection *conn, int errno_bck, int expired) |
| 579 | { |
| 580 | struct check *check = conn->owner; |
| 581 | const char *err_msg; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 582 | struct chunk *chk; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 583 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 584 | if (check->result != CHK_RES_UNKNOWN) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 585 | return; |
| 586 | |
| 587 | errno = errno_bck; |
| 588 | if (!errno || errno == EAGAIN) |
| 589 | retrieve_errno_from_socket(conn); |
| 590 | |
| 591 | if (!(conn->flags & CO_FL_ERROR) && !expired) |
| 592 | return; |
| 593 | |
| 594 | /* we'll try to build a meaningful error message depending on the |
| 595 | * context of the error possibly present in conn->err_code, and the |
| 596 | * socket error possibly collected above. This is useful to know the |
| 597 | * exact step of the L6 layer (eg: SSL handshake). |
| 598 | */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 599 | chk = get_trash_chunk(); |
| 600 | |
| 601 | if (check->type == PR_O2_TCPCHK_CHK) { |
| 602 | chunk_printf(chk, " at step %d of tcp-check", tcpcheck_get_step_id(check->server)); |
| 603 | /* we were looking for a string */ |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 604 | if (check->current_step && check->current_step->action == TCPCHK_ACT_CONNECT) { |
| 605 | chunk_appendf(chk, " (connect)"); |
| 606 | } |
| 607 | else if (check->current_step && check->current_step->action == TCPCHK_ACT_EXPECT) { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 608 | if (check->current_step->string) |
| 609 | chunk_appendf(chk, " (string '%s')", check->current_step->string); |
| 610 | else if (check->current_step->expect_regex) |
| 611 | chunk_appendf(chk, " (expect regex)"); |
| 612 | } |
| 613 | else if (check->current_step && check->current_step->action == TCPCHK_ACT_SEND) { |
| 614 | chunk_appendf(chk, " (send)"); |
| 615 | } |
| 616 | } |
| 617 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 618 | if (conn->err_code) { |
| 619 | if (errno && errno != EAGAIN) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 620 | chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 621 | else |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 622 | chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 623 | err_msg = trash.str; |
| 624 | } |
| 625 | else { |
| 626 | if (errno && errno != EAGAIN) { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 627 | chunk_printf(&trash, "%s%s", strerror(errno), chk->str); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 628 | err_msg = trash.str; |
| 629 | } |
| 630 | else { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 631 | err_msg = chk->str; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
| 635 | if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) { |
| 636 | /* L4 not established (yet) */ |
| 637 | if (conn->flags & CO_FL_ERROR) |
| 638 | set_server_check_status(check, HCHK_STATUS_L4CON, err_msg); |
| 639 | else if (expired) |
| 640 | set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg); |
| 641 | } |
| 642 | else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) { |
| 643 | /* L6 not established (yet) */ |
| 644 | if (conn->flags & CO_FL_ERROR) |
| 645 | set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg); |
| 646 | else if (expired) |
| 647 | set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg); |
| 648 | } |
| 649 | else if (conn->flags & CO_FL_ERROR) { |
| 650 | /* I/O error after connection was established and before we could diagnose */ |
| 651 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 652 | } |
| 653 | else if (expired) { |
| 654 | /* connection established but expired check */ |
| 655 | if (check->type == PR_O2_SSL3_CHK) |
| 656 | set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg); |
| 657 | else /* HTTP, SMTP, ... */ |
| 658 | set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg); |
| 659 | } |
| 660 | |
| 661 | return; |
| 662 | } |
| 663 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 664 | /* |
| 665 | * This function is used only for server health-checks. It handles |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 666 | * the connection acknowledgement. If the proxy requires L7 health-checks, |
| 667 | * it sends the request. In other cases, it calls set_server_check_status() |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 668 | * to set check->status, check->duration and check->result. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 669 | */ |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 670 | static void event_srv_chk_w(struct connection *conn) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 671 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 672 | struct check *check = conn->owner; |
| 673 | struct server *s = check->server; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 674 | struct task *t = check->task; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 675 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 676 | if (unlikely(check->result == CHK_RES_FAILED)) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 677 | goto out_wakeup; |
| 678 | |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 679 | if (conn->flags & CO_FL_HANDSHAKE) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 680 | return; |
| 681 | |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 682 | if (retrieve_errno_from_socket(conn)) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 683 | chk_report_conn_err(conn, errno, 0); |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 684 | __conn_data_stop_both(conn); |
| 685 | goto out_wakeup; |
| 686 | } |
Krzysztof Piotr Oledzki | 6492db5 | 2010-01-02 22:03:01 +0100 | [diff] [blame] | 687 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 688 | if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) { |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 689 | /* if the output is closed, we can't do anything */ |
| 690 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 691 | chk_report_conn_err(conn, 0, 0); |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 692 | goto out_wakeup; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 693 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 694 | |
Willy Tarreau | 06559ac | 2013-12-05 01:53:08 +0100 | [diff] [blame] | 695 | /* here, we know that the connection is established. That's enough for |
| 696 | * a pure TCP check. |
| 697 | */ |
| 698 | if (!check->type) |
| 699 | goto out_wakeup; |
| 700 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 701 | if (check->type == PR_O2_TCPCHK_CHK) { |
| 702 | tcpcheck_main(conn); |
| 703 | return; |
| 704 | } |
| 705 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 706 | if (check->bo->o) { |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 707 | conn->xprt->snd_buf(conn, check->bo, 0); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 708 | if (conn->flags & CO_FL_ERROR) { |
| 709 | chk_report_conn_err(conn, errno, 0); |
| 710 | __conn_data_stop_both(conn); |
| 711 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 712 | } |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 713 | if (check->bo->o) |
| 714 | return; |
| 715 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 716 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 717 | /* full request sent, we allow up to <timeout.check> if nonzero for a response */ |
| 718 | if (s->proxy->timeout.check) { |
| 719 | t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check); |
| 720 | task_queue(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 721 | } |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 722 | goto out_nowake; |
| 723 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 724 | out_wakeup: |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 725 | task_wakeup(t, TASK_WOKEN_IO); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 726 | out_nowake: |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 727 | __conn_data_stop_send(conn); /* nothing more to write */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 728 | } |
| 729 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 730 | /* |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 731 | * This function is used only for server health-checks. It handles the server's |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 732 | * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 733 | * set_server_check_status() to update check->status, check->duration |
| 734 | * and check->result. |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 735 | |
| 736 | * The set_server_check_status function is called with HCHK_STATUS_L7OKD if |
| 737 | * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server |
| 738 | * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in |
| 739 | * response to an SSL HELLO (the principle is that this is enough to |
| 740 | * distinguish between an SSL server and a pure TCP relay). All other cases will |
| 741 | * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP, |
| 742 | * etc. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 743 | */ |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 744 | static void event_srv_chk_r(struct connection *conn) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 745 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 746 | struct check *check = conn->owner; |
| 747 | struct server *s = check->server; |
| 748 | struct task *t = check->task; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 749 | char *desc; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 750 | int done; |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 751 | unsigned short msglen; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 752 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 753 | if (unlikely(check->result == CHK_RES_FAILED)) |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 754 | goto out_wakeup; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 755 | |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 756 | if (conn->flags & CO_FL_HANDSHAKE) |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 757 | return; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 758 | |
| 759 | if (check->type == PR_O2_TCPCHK_CHK) { |
| 760 | tcpcheck_main(conn); |
| 761 | return; |
| 762 | } |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 763 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 764 | /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available |
| 765 | * but the connection was closed on the remote end. Fortunately, recv still |
| 766 | * works correctly and we don't need to do the getsockopt() on linux. |
| 767 | */ |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 768 | |
| 769 | /* Set buffer to point to the end of the data already read, and check |
| 770 | * that there is free space remaining. If the buffer is full, proceed |
| 771 | * with running the checks without attempting another socket read. |
| 772 | */ |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 773 | |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 774 | done = 0; |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 775 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 776 | conn->xprt->rcv_buf(conn, check->bi, check->bi->size); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 777 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) { |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 778 | done = 1; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 779 | if ((conn->flags & CO_FL_ERROR) && !check->bi->i) { |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 780 | /* Report network errors only if we got no other data. Otherwise |
| 781 | * we'll let the upper layers decide whether the response is OK |
| 782 | * or not. It is very common that an RST sent by the server is |
| 783 | * reported as an error just after the last data chunk. |
| 784 | */ |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 785 | chk_report_conn_err(conn, errno, 0); |
Willy Tarreau | c1a0796 | 2010-03-16 20:55:43 +0100 | [diff] [blame] | 786 | goto out_wakeup; |
| 787 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 788 | } |
| 789 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 790 | |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 791 | /* Intermediate or complete response received. |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 792 | * Terminate string in check->bi->data buffer. |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 793 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 794 | if (check->bi->i < check->bi->size) |
| 795 | check->bi->data[check->bi->i] = '\0'; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 796 | else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 797 | check->bi->data[check->bi->i - 1] = '\0'; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 798 | done = 1; /* buffer full, don't wait for more data */ |
| 799 | } |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 800 | |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 801 | /* Run the checks... */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 802 | switch (check->type) { |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 803 | case PR_O2_HTTP_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 804 | if (!done && check->bi->i < strlen("HTTP/1.0 000\r")) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 805 | goto wait_more_data; |
| 806 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 807 | /* Check if the server speaks HTTP 1.X */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 808 | if ((check->bi->i < strlen("HTTP/1.0 000\r")) || |
| 809 | (memcmp(check->bi->data, "HTTP/1.", 7) != 0 || |
| 810 | (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) || |
| 811 | !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) || |
| 812 | !isdigit((unsigned char) *(check->bi->data + 11))) { |
| 813 | cut_crlf(check->bi->data); |
| 814 | set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 815 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 816 | goto out_wakeup; |
| 817 | } |
| 818 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 819 | check->code = str2uic(check->bi->data + 9); |
| 820 | desc = ltrim(check->bi->data + 12, ' '); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 821 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 822 | if ((s->proxy->options & PR_O_DISABLE404) && |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 823 | (s->state != SRV_ST_STOPPED) && (check->code == 404)) { |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 824 | /* 404 may be accepted as "stopping" only if the server was up */ |
| 825 | cut_crlf(desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 826 | set_server_check_status(check, HCHK_STATUS_L7OKCD, desc); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 827 | } |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 828 | else if (s->proxy->options2 & PR_O2_EXP_TYPE) { |
| 829 | /* Run content verification check... We know we have at least 13 chars */ |
| 830 | if (!httpchk_expect(s, done)) |
| 831 | goto wait_more_data; |
| 832 | } |
| 833 | /* check the reply : HTTP/1.X 2xx and 3xx are OK */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 834 | else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') { |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 835 | cut_crlf(desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 836 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 837 | } |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 838 | else { |
| 839 | cut_crlf(desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 840 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 841 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 842 | break; |
| 843 | |
| 844 | case PR_O2_SSL3_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 845 | if (!done && check->bi->i < 5) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 846 | goto wait_more_data; |
| 847 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 848 | /* Check for SSLv3 alert or handshake */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 849 | if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16)) |
| 850 | set_server_check_status(check, HCHK_STATUS_L6OK, NULL); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 851 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 852 | set_server_check_status(check, HCHK_STATUS_L6RSP, NULL); |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 853 | break; |
| 854 | |
| 855 | case PR_O2_SMTP_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 856 | if (!done && check->bi->i < strlen("000\r")) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 857 | goto wait_more_data; |
| 858 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 859 | /* Check if the server speaks SMTP */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 860 | if ((check->bi->i < strlen("000\r")) || |
| 861 | (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') || |
| 862 | !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) || |
| 863 | !isdigit((unsigned char) *(check->bi->data + 2))) { |
| 864 | cut_crlf(check->bi->data); |
| 865 | set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 866 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 867 | goto out_wakeup; |
| 868 | } |
| 869 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 870 | check->code = str2uic(check->bi->data); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 871 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 872 | desc = ltrim(check->bi->data + 3, ' '); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 873 | cut_crlf(desc); |
| 874 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 875 | /* Check for SMTP code 2xx (should be 250) */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 876 | if (*check->bi->data == '2') |
| 877 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 878 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 879 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 880 | break; |
| 881 | |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 882 | case PR_O2_LB_AGENT_CHK: { |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 883 | int status = HCHK_STATUS_CHECKED; |
| 884 | const char *hs = NULL; /* health status */ |
| 885 | const char *as = NULL; /* admin status */ |
| 886 | const char *ps = NULL; /* performance status */ |
| 887 | const char *err = NULL; /* first error to report */ |
| 888 | const char *wrn = NULL; /* first warning to report */ |
| 889 | char *cmd, *p; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 890 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 891 | /* We're getting an agent check response. The agent could |
| 892 | * have been disabled in the mean time with a long check |
| 893 | * still pending. It is important that we ignore the whole |
| 894 | * response. |
| 895 | */ |
| 896 | if (!(check->server->agent.state & CHK_ST_ENABLED)) |
| 897 | break; |
| 898 | |
| 899 | /* The agent supports strings made of a single line ended by the |
| 900 | * first CR ('\r') or LF ('\n'). This line is composed of words |
| 901 | * delimited by spaces (' '), tabs ('\t'), or commas (','). The |
| 902 | * line may optionally contained a description of a state change |
| 903 | * after a sharp ('#'), which is only considered if a health state |
| 904 | * is announced. |
| 905 | * |
| 906 | * Words may be composed of : |
| 907 | * - a numeric weight suffixed by the percent character ('%'). |
| 908 | * - a health status among "up", "down", "stopped", and "fail". |
| 909 | * - an admin status among "ready", "drain", "maint". |
| 910 | * |
| 911 | * These words may appear in any order. If multiple words of the |
| 912 | * same category appear, the last one wins. |
| 913 | */ |
| 914 | |
Willy Tarreau | 9809b78 | 2013-12-11 21:40:11 +0100 | [diff] [blame] | 915 | p = check->bi->data; |
| 916 | while (*p && *p != '\n' && *p != '\r') |
| 917 | p++; |
| 918 | |
| 919 | if (!*p) { |
| 920 | if (!done) |
| 921 | goto wait_more_data; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 922 | |
Willy Tarreau | 9809b78 | 2013-12-11 21:40:11 +0100 | [diff] [blame] | 923 | /* at least inform the admin that the agent is mis-behaving */ |
| 924 | set_server_check_status(check, check->status, "Ignoring incomplete line from agent"); |
| 925 | break; |
| 926 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 927 | |
Willy Tarreau | 9809b78 | 2013-12-11 21:40:11 +0100 | [diff] [blame] | 928 | *p = 0; |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 929 | cmd = check->bi->data; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 930 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 931 | while (*cmd) { |
| 932 | /* look for next word */ |
| 933 | if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') { |
| 934 | cmd++; |
| 935 | continue; |
| 936 | } |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 937 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 938 | if (*cmd == '#') { |
| 939 | /* this is the beginning of a health status description, |
| 940 | * skip the sharp and blanks. |
| 941 | */ |
| 942 | cmd++; |
| 943 | while (*cmd == '\t' || *cmd == ' ') |
| 944 | cmd++; |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 945 | break; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 946 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 947 | |
| 948 | /* find the end of the word so that we have a null-terminated |
| 949 | * word between <cmd> and <p>. |
| 950 | */ |
| 951 | p = cmd + 1; |
| 952 | while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',') |
| 953 | p++; |
| 954 | if (*p) |
| 955 | *p++ = 0; |
| 956 | |
| 957 | /* first, health statuses */ |
| 958 | if (strcasecmp(cmd, "up") == 0) { |
| 959 | check->health = check->rise + check->fall - 1; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 960 | status = HCHK_STATUS_L7OKD; |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 961 | hs = cmd; |
| 962 | } |
| 963 | else if (strcasecmp(cmd, "down") == 0) { |
| 964 | check->health = 0; |
| 965 | status = HCHK_STATUS_L7STS; |
| 966 | hs = cmd; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 967 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 968 | else if (strcasecmp(cmd, "stopped") == 0) { |
| 969 | check->health = 0; |
| 970 | status = HCHK_STATUS_L7STS; |
| 971 | hs = cmd; |
| 972 | } |
| 973 | else if (strcasecmp(cmd, "fail") == 0) { |
| 974 | check->health = 0; |
| 975 | status = HCHK_STATUS_L7STS; |
| 976 | hs = cmd; |
| 977 | } |
| 978 | /* admin statuses */ |
| 979 | else if (strcasecmp(cmd, "ready") == 0) { |
| 980 | as = cmd; |
| 981 | } |
| 982 | else if (strcasecmp(cmd, "drain") == 0) { |
| 983 | as = cmd; |
| 984 | } |
| 985 | else if (strcasecmp(cmd, "maint") == 0) { |
| 986 | as = cmd; |
| 987 | } |
| 988 | /* else try to parse a weight here and keep the last one */ |
| 989 | else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) { |
| 990 | ps = cmd; |
| 991 | } |
| 992 | else { |
| 993 | /* keep a copy of the first error */ |
| 994 | if (!err) |
| 995 | err = cmd; |
| 996 | } |
| 997 | /* skip to next word */ |
| 998 | cmd = p; |
| 999 | } |
| 1000 | /* here, cmd points either to \0 or to the beginning of a |
| 1001 | * description. Skip possible leading spaces. |
| 1002 | */ |
| 1003 | while (*cmd == ' ' || *cmd == '\n') |
| 1004 | cmd++; |
| 1005 | |
| 1006 | /* First, update the admin status so that we avoid sending other |
| 1007 | * possibly useless warnings and can also update the health if |
| 1008 | * present after going back up. |
| 1009 | */ |
| 1010 | if (as) { |
| 1011 | if (strcasecmp(as, "drain") == 0) |
| 1012 | srv_adm_set_drain(check->server); |
| 1013 | else if (strcasecmp(as, "maint") == 0) |
| 1014 | srv_adm_set_maint(check->server); |
| 1015 | else |
| 1016 | srv_adm_set_ready(check->server); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1017 | } |
| 1018 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1019 | /* now change weights */ |
| 1020 | if (ps) { |
| 1021 | const char *msg; |
| 1022 | |
| 1023 | msg = server_parse_weight_change_request(s, ps); |
| 1024 | if (!wrn || !*wrn) |
| 1025 | wrn = msg; |
| 1026 | } |
| 1027 | |
| 1028 | /* and finally health status */ |
| 1029 | if (hs) { |
| 1030 | /* We'll report some of the warnings and errors we have |
| 1031 | * here. Down reports are critical, we leave them untouched. |
| 1032 | * Lack of report, or report of 'UP' leaves the room for |
| 1033 | * ERR first, then WARN. |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1034 | */ |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1035 | const char *msg = cmd; |
| 1036 | struct chunk *t; |
| 1037 | |
| 1038 | if (!*msg || status == HCHK_STATUS_L7OKD) { |
| 1039 | if (err && *err) |
| 1040 | msg = err; |
| 1041 | else if (wrn && *wrn) |
| 1042 | msg = wrn; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1043 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1044 | |
| 1045 | t = get_trash_chunk(); |
| 1046 | chunk_printf(t, "via agent : %s%s%s%s", |
| 1047 | hs, *msg ? " (" : "", |
| 1048 | msg, *msg ? ")" : ""); |
| 1049 | |
| 1050 | set_server_check_status(check, status, t->str); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1051 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1052 | else if (err && *err) { |
| 1053 | /* No status change but we'd like to report something odd. |
| 1054 | * Just report the current state and copy the message. |
| 1055 | */ |
| 1056 | chunk_printf(&trash, "agent reports an error : %s", err); |
| 1057 | set_server_check_status(check, status/*check->status*/, trash.str); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1058 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1059 | } |
| 1060 | else if (wrn && *wrn) { |
| 1061 | /* No status change but we'd like to report something odd. |
| 1062 | * Just report the current state and copy the message. |
| 1063 | */ |
| 1064 | chunk_printf(&trash, "agent warns : %s", wrn); |
| 1065 | set_server_check_status(check, status/*check->status*/, trash.str); |
| 1066 | } |
| 1067 | else |
| 1068 | set_server_check_status(check, status, NULL); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1069 | break; |
| 1070 | } |
| 1071 | |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1072 | case PR_O2_PGSQL_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1073 | if (!done && check->bi->i < 9) |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1074 | goto wait_more_data; |
| 1075 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1076 | if (check->bi->data[0] == 'R') { |
| 1077 | set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok"); |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1078 | } |
| 1079 | else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1080 | if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0)) |
| 1081 | desc = &check->bi->data[6]; |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1082 | else |
| 1083 | desc = "PostgreSQL unknown error"; |
| 1084 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1085 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1086 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1087 | break; |
| 1088 | |
| 1089 | case PR_O2_REDIS_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1090 | if (!done && check->bi->i < 7) |
Hervé COMMOWICK | ec032d6 | 2011-08-05 16:23:48 +0200 | [diff] [blame] | 1091 | goto wait_more_data; |
| 1092 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1093 | if (strcmp(check->bi->data, "+PONG\r\n") == 0) { |
| 1094 | set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok"); |
Hervé COMMOWICK | ec032d6 | 2011-08-05 16:23:48 +0200 | [diff] [blame] | 1095 | } |
| 1096 | else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1097 | set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data); |
Hervé COMMOWICK | ec032d6 | 2011-08-05 16:23:48 +0200 | [diff] [blame] | 1098 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1099 | break; |
| 1100 | |
| 1101 | case PR_O2_MYSQL_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1102 | if (!done && check->bi->i < 5) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1103 | goto wait_more_data; |
| 1104 | |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1105 | if (s->proxy->check_len == 0) { // old mode |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1106 | if (*(check->bi->data + 4) != '\xff') { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1107 | /* We set the MySQL Version in description for information purpose |
| 1108 | * FIXME : it can be cool to use MySQL Version for other purpose, |
| 1109 | * like mark as down old MySQL server. |
| 1110 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1111 | if (check->bi->i > 51) { |
| 1112 | desc = ltrim(check->bi->data + 5, ' '); |
| 1113 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1114 | } |
| 1115 | else { |
| 1116 | if (!done) |
| 1117 | goto wait_more_data; |
| 1118 | /* it seems we have a OK packet but without a valid length, |
| 1119 | * it must be a protocol error |
| 1120 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1121 | set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1122 | } |
Hervé COMMOWICK | 698ae00 | 2010-01-12 09:25:13 +0100 | [diff] [blame] | 1123 | } |
| 1124 | else { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1125 | /* An error message is attached in the Error packet */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1126 | desc = ltrim(check->bi->data + 7, ' '); |
| 1127 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1128 | } |
| 1129 | } else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1130 | unsigned int first_packet_len = ((unsigned int) *check->bi->data) + |
| 1131 | (((unsigned int) *(check->bi->data + 1)) << 8) + |
| 1132 | (((unsigned int) *(check->bi->data + 2)) << 16); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1133 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1134 | if (check->bi->i == first_packet_len + 4) { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1135 | /* MySQL Error packet always begin with field_count = 0xff */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1136 | if (*(check->bi->data + 4) != '\xff') { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1137 | /* We have only one MySQL packet and it is a Handshake Initialization packet |
| 1138 | * but we need to have a second packet to know if it is alright |
| 1139 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1140 | if (!done && check->bi->i < first_packet_len + 5) |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1141 | goto wait_more_data; |
| 1142 | } |
| 1143 | else { |
| 1144 | /* We have only one packet and it is an Error packet, |
| 1145 | * an error message is attached, so we can display it |
| 1146 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1147 | desc = &check->bi->data[7]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1148 | //Warning("onlyoneERR: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1149 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1150 | } |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1151 | } else if (check->bi->i > first_packet_len + 4) { |
| 1152 | unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) + |
| 1153 | (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) + |
| 1154 | (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1155 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1156 | if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1157 | /* We have 2 packets and that's good */ |
| 1158 | /* Check if the second packet is a MySQL Error packet or not */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1159 | if (*(check->bi->data + first_packet_len + 8) != '\xff') { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1160 | /* No error packet */ |
| 1161 | /* We set the MySQL Version in description for information purpose */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1162 | desc = &check->bi->data[5]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1163 | //Warning("2packetOK: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1164 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1165 | } |
| 1166 | else { |
| 1167 | /* An error message is attached in the Error packet |
| 1168 | * so we can display it ! :) |
| 1169 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1170 | desc = &check->bi->data[first_packet_len+11]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1171 | //Warning("2packetERR: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1172 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1173 | } |
| 1174 | } |
| 1175 | } |
| 1176 | else { |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1177 | if (!done) |
| 1178 | goto wait_more_data; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1179 | /* it seems we have a Handshake Initialization packet but without a valid length, |
Hervé COMMOWICK | 698ae00 | 2010-01-12 09:25:13 +0100 | [diff] [blame] | 1180 | * it must be a protocol error |
| 1181 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1182 | desc = &check->bi->data[5]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1183 | //Warning("protoerr: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1184 | set_server_check_status(check, HCHK_STATUS_L7RSP, desc); |
Hervé COMMOWICK | 698ae00 | 2010-01-12 09:25:13 +0100 | [diff] [blame] | 1185 | } |
| 1186 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1187 | break; |
| 1188 | |
| 1189 | case PR_O2_LDAP_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1190 | if (!done && check->bi->i < 14) |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1191 | goto wait_more_data; |
| 1192 | |
| 1193 | /* Check if the server speaks LDAP (ASN.1/BER) |
| 1194 | * http://en.wikipedia.org/wiki/Basic_Encoding_Rules |
| 1195 | * http://tools.ietf.org/html/rfc4511 |
| 1196 | */ |
| 1197 | |
| 1198 | /* http://tools.ietf.org/html/rfc4511#section-4.1.1 |
| 1199 | * LDAPMessage: 0x30: SEQUENCE |
| 1200 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1201 | if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) { |
| 1202 | set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1203 | } |
| 1204 | else { |
| 1205 | /* size of LDAPMessage */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1206 | msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0; |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1207 | |
| 1208 | /* http://tools.ietf.org/html/rfc4511#section-4.2.2 |
| 1209 | * messageID: 0x02 0x01 0x01: INTEGER 1 |
| 1210 | * protocolOp: 0x61: bindResponse |
| 1211 | */ |
| 1212 | if ((msglen > 2) || |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1213 | (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) { |
| 1214 | set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1215 | |
| 1216 | goto out_wakeup; |
| 1217 | } |
| 1218 | |
| 1219 | /* size of bindResponse */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1220 | msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0; |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1221 | |
| 1222 | /* http://tools.ietf.org/html/rfc4511#section-4.1.9 |
| 1223 | * ldapResult: 0x0a 0x01: ENUMERATION |
| 1224 | */ |
| 1225 | if ((msglen > 4) || |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1226 | (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) { |
| 1227 | set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1228 | |
| 1229 | goto out_wakeup; |
| 1230 | } |
| 1231 | |
| 1232 | /* http://tools.ietf.org/html/rfc4511#section-4.1.9 |
| 1233 | * resultCode |
| 1234 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1235 | check->code = *(check->bi->data + msglen + 9); |
| 1236 | if (check->code) { |
| 1237 | set_server_check_status(check, HCHK_STATUS_L7STS, "See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1238 | } else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1239 | set_server_check_status(check, HCHK_STATUS_L7OKD, "Success"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1240 | } |
| 1241 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1242 | break; |
| 1243 | |
| 1244 | default: |
Willy Tarreau | 06559ac | 2013-12-05 01:53:08 +0100 | [diff] [blame] | 1245 | /* for other checks (eg: pure TCP), delegate to the main task */ |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1246 | break; |
| 1247 | } /* switch */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 1248 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1249 | out_wakeup: |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1250 | /* collect possible new errors */ |
| 1251 | if (conn->flags & CO_FL_ERROR) |
| 1252 | chk_report_conn_err(conn, 0, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1253 | |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 1254 | /* Reset the check buffer... */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1255 | *check->bi->data = '\0'; |
| 1256 | check->bi->i = 0; |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 1257 | |
Willy Tarreau | fd29cc5 | 2012-11-23 09:18:20 +0100 | [diff] [blame] | 1258 | /* Close the connection... We absolutely want to perform a hard close |
| 1259 | * and reset the connection if some data are pending, otherwise we end |
| 1260 | * up with many TIME_WAITs and eat all the source port range quickly. |
| 1261 | * To avoid sending RSTs all the time, we first try to drain pending |
| 1262 | * data. |
| 1263 | */ |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1264 | if (conn->xprt && conn->xprt->shutw) |
| 1265 | conn->xprt->shutw(conn, 0); |
Willy Tarreau | 2b57cb8 | 2013-06-10 19:56:38 +0200 | [diff] [blame] | 1266 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1267 | /* OK, let's not stay here forever */ |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 1268 | if (check->result == CHK_RES_FAILED) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1269 | conn->flags |= CO_FL_ERROR; |
| 1270 | |
Willy Tarreau | a522f80 | 2012-11-23 08:56:35 +0100 | [diff] [blame] | 1271 | __conn_data_stop_both(conn); |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 1272 | task_wakeup(t, TASK_WOKEN_IO); |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 1273 | return; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1274 | |
| 1275 | wait_more_data: |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 1276 | __conn_data_want_recv(conn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1277 | } |
| 1278 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1279 | /* |
| 1280 | * This function is used only for server health-checks. It handles connection |
| 1281 | * status updates including errors. If necessary, it wakes the check task up. |
| 1282 | * It always returns 0. |
| 1283 | */ |
| 1284 | static int wake_srv_chk(struct connection *conn) |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1285 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1286 | struct check *check = conn->owner; |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1287 | |
Willy Tarreau | 6c560da | 2012-11-24 11:14:45 +0100 | [diff] [blame] | 1288 | if (unlikely(conn->flags & CO_FL_ERROR)) { |
Willy Tarreau | 02b0f58 | 2013-12-03 15:42:33 +0100 | [diff] [blame] | 1289 | /* We may get error reports bypassing the I/O handlers, typically |
| 1290 | * the case when sending a pure TCP check which fails, then the I/O |
| 1291 | * handlers above are not called. This is completely handled by the |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1292 | * main processing task so let's simply wake it up. If we get here, |
| 1293 | * we expect errno to still be valid. |
| 1294 | */ |
| 1295 | chk_report_conn_err(conn, errno, 0); |
| 1296 | |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 1297 | __conn_data_stop_both(conn); |
| 1298 | task_wakeup(check->task, TASK_WOKEN_IO); |
| 1299 | } |
Willy Tarreau | 3be293f | 2014-02-05 18:31:24 +0100 | [diff] [blame] | 1300 | else if (!(conn->flags & (CO_FL_DATA_RD_ENA|CO_FL_DATA_WR_ENA|CO_FL_HANDSHAKE))) { |
| 1301 | /* we may get here if only a connection probe was required : we |
| 1302 | * don't have any data to send nor anything expected in response, |
| 1303 | * so the completion of the connection establishment is enough. |
| 1304 | */ |
| 1305 | task_wakeup(check->task, TASK_WOKEN_IO); |
| 1306 | } |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 1307 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 1308 | if (check->result != CHK_RES_UNKNOWN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1309 | /* We're here because nobody wants to handle the error, so we |
| 1310 | * sure want to abort the hard way. |
Willy Tarreau | 02b0f58 | 2013-12-03 15:42:33 +0100 | [diff] [blame] | 1311 | */ |
Willy Tarreau | 46be2e5 | 2014-01-20 12:10:52 +0100 | [diff] [blame] | 1312 | conn_drain(conn); |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 1313 | conn_force_close(conn); |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 1314 | } |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 1315 | return 0; |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1316 | } |
| 1317 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1318 | struct data_cb check_conn_cb = { |
| 1319 | .recv = event_srv_chk_r, |
| 1320 | .send = event_srv_chk_w, |
| 1321 | .wake = wake_srv_chk, |
| 1322 | }; |
| 1323 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1324 | /* |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1325 | * updates the server's weight during a warmup stage. Once the final weight is |
| 1326 | * reached, the task automatically stops. Note that any server status change |
| 1327 | * must have updated s->last_change accordingly. |
| 1328 | */ |
| 1329 | static struct task *server_warmup(struct task *t) |
| 1330 | { |
| 1331 | struct server *s = t->context; |
| 1332 | |
| 1333 | /* by default, plan on stopping the task */ |
| 1334 | t->expire = TICK_ETERNITY; |
Willy Tarreau | 2012521 | 2014-05-13 19:44:56 +0200 | [diff] [blame] | 1335 | if ((s->admin & SRV_ADMF_MAINT) || |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 1336 | (s->state != SRV_ST_STARTING)) |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1337 | return t; |
| 1338 | |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 1339 | /* recalculate the weights and update the state */ |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1340 | server_recalc_eweight(s); |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1341 | |
| 1342 | /* probably that we can refill this server with a bit more connections */ |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1343 | pendconn_grab_from_px(s); |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1344 | |
| 1345 | /* get back there in 1 second or 1/20th of the slowstart interval, |
| 1346 | * whichever is greater, resulting in small 5% steps. |
| 1347 | */ |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 1348 | if (s->state == SRV_ST_STARTING) |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1349 | t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))); |
| 1350 | return t; |
| 1351 | } |
| 1352 | |
| 1353 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1354 | * manages a server health-check. Returns |
| 1355 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 1356 | */ |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 1357 | static struct task *process_chk(struct task *t) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1358 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1359 | struct check *check = t->context; |
| 1360 | struct server *s = check->server; |
| 1361 | struct connection *conn = check->conn; |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 1362 | struct protocol *proto; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1363 | int rv; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1364 | int ret; |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1365 | int expired = tick_is_expired(t->expire, now_ms); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1366 | |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1367 | if (!(check->state & CHK_ST_INPROGRESS)) { |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1368 | /* no check currently running */ |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1369 | if (!expired) /* woke up too early */ |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1370 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1371 | |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 1372 | /* we don't send any health-checks when the proxy is |
| 1373 | * stopped, the server should not be checked or the check |
| 1374 | * is disabled. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1375 | */ |
Willy Tarreau | 0d924cc | 2013-12-11 21:26:24 +0100 | [diff] [blame] | 1376 | if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) || |
Willy Tarreau | 33a08db | 2013-12-11 21:03:31 +0100 | [diff] [blame] | 1377 | s->proxy->state == PR_STSTOPPED) |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1378 | goto reschedule; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1379 | |
| 1380 | /* we'll initiate a new check */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1381 | set_server_check_status(check, HCHK_STATUS_START, NULL); |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1382 | |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1383 | check->state |= CHK_ST_INPROGRESS; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1384 | check->bi->p = check->bi->data; |
| 1385 | check->bi->i = 0; |
| 1386 | check->bo->p = check->bo->data; |
| 1387 | check->bo->o = 0; |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1388 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1389 | /* tcpcheck send/expect initialisation */ |
| 1390 | if (check->type == PR_O2_TCPCHK_CHK) |
| 1391 | check->current_step = NULL; |
| 1392 | |
| 1393 | /* prepare the check buffer. |
| 1394 | * This should not be used if check is the secondary agent check |
| 1395 | * of a server as s->proxy->check_req will relate to the |
| 1396 | * configuration of the primary check. Similarly, tcp-check uses |
| 1397 | * its own strings. |
| 1398 | */ |
Willy Tarreau | 3343432 | 2013-12-11 21:15:19 +0100 | [diff] [blame] | 1399 | if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1400 | bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len); |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1401 | |
| 1402 | /* we want to check if this host replies to HTTP or SSLv3 requests |
| 1403 | * so we'll send the request, and won't wake the checker up now. |
| 1404 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1405 | if ((check->type) == PR_O2_SSL3_CHK) { |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1406 | /* SSL requires that we put Unix time in the request */ |
| 1407 | int gmt_time = htonl(date.tv_sec); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1408 | memcpy(check->bo->data + 11, &gmt_time, 4); |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1409 | } |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1410 | else if ((check->type) == PR_O2_HTTP_CHK) { |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1411 | if (s->proxy->options2 & PR_O2_CHK_SNDST) |
Willy Tarreau | a1dab55 | 2014-04-14 15:04:54 +0200 | [diff] [blame] | 1412 | bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size)); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1413 | bo_putstr(check->bo, "\r\n"); |
| 1414 | *check->bo->p = '\0'; /* to make gdb output easier to read */ |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | /* prepare a new connection */ |
Willy Tarreau | 4bdae8a | 2013-10-14 17:29:15 +0200 | [diff] [blame] | 1419 | conn_init(conn); |
Willy Tarreau | 910c6aa | 2013-10-24 15:08:37 +0200 | [diff] [blame] | 1420 | conn_prepare(conn, s->check_common.proto, s->check_common.xprt); |
Willy Tarreau | 7abddb5 | 2013-10-24 15:31:04 +0200 | [diff] [blame] | 1421 | conn_attach(conn, check, &check_conn_cb); |
Willy Tarreau | 4bdae8a | 2013-10-14 17:29:15 +0200 | [diff] [blame] | 1422 | conn->target = &s->obj_type; |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 1423 | |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 1424 | /* no client address */ |
| 1425 | clear_addr(&conn->addr.from); |
| 1426 | |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 1427 | if (is_addr(&s->check_common.addr)) { |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1428 | /* we'll connect to the check addr specified on the server */ |
Simon Horman | 6618300 | 2013-02-23 10:16:43 +0900 | [diff] [blame] | 1429 | conn->addr.to = s->check_common.addr; |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 1430 | proto = s->check_common.proto; |
| 1431 | } |
| 1432 | else { |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1433 | /* we'll connect to the addr on the server */ |
| 1434 | conn->addr.to = s->addr; |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 1435 | proto = s->proto; |
| 1436 | } |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 1437 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 1438 | if (check->port) { |
| 1439 | set_host_port(&conn->addr.to, check->port); |
| 1440 | } |
| 1441 | |
| 1442 | if (check->type == PR_O2_TCPCHK_CHK) { |
Baptiste Assmann | f621bea | 2014-02-03 22:38:15 +0100 | [diff] [blame] | 1443 | struct tcpcheck_rule *r = (struct tcpcheck_rule *) s->proxy->tcpcheck_rules.n; |
| 1444 | /* if first step is a 'connect', then tcpcheck_main must run it */ |
| 1445 | if (r->action == TCPCHK_ACT_CONNECT) { |
| 1446 | tcpcheck_main(conn); |
| 1447 | return t; |
| 1448 | } |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 1449 | } |
| 1450 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1451 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1452 | /* It can return one of : |
| 1453 | * - SN_ERR_NONE if everything's OK |
| 1454 | * - SN_ERR_SRVTO if there are no more servers |
| 1455 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 1456 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1457 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1458 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 1459 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
Willy Tarreau | 24db47e | 2012-11-23 14:16:39 +0100 | [diff] [blame] | 1460 | * Note that we try to prevent the network stack from sending the ACK during the |
Willy Tarreau | f0837b2 | 2012-11-24 10:24:27 +0100 | [diff] [blame] | 1461 | * connect() when a pure TCP check is used (without PROXY protocol). |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1462 | */ |
Willy Tarreau | 8f46cca | 2013-03-04 20:07:44 +0100 | [diff] [blame] | 1463 | ret = SN_ERR_INTERNAL; |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 1464 | if (proto->connect) |
| 1465 | ret = proto->connect(conn, check->type, (check->type) ? 0 : 2); |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1466 | conn->flags |= CO_FL_WAKE_DATA; |
Willy Tarreau | 57cd3e4 | 2013-10-24 22:01:26 +0200 | [diff] [blame] | 1467 | if (s->check.send_proxy) { |
| 1468 | conn->send_proxy_ofs = 1; |
| 1469 | conn->flags |= CO_FL_SEND_PROXY; |
| 1470 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1471 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1472 | switch (ret) { |
| 1473 | case SN_ERR_NONE: |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1474 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1475 | * to establish but only when timeout.check is set |
| 1476 | * as it may be to short for a full check otherwise |
| 1477 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1478 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1479 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1480 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 1481 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 1482 | t->expire = tick_first(t->expire, t_con); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1483 | } |
Willy Tarreau | 06559ac | 2013-12-05 01:53:08 +0100 | [diff] [blame] | 1484 | |
| 1485 | if (check->type) |
| 1486 | conn_data_want_recv(conn); /* prepare for reading a possible reply */ |
| 1487 | |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1488 | goto reschedule; |
| 1489 | |
| 1490 | case SN_ERR_SRVTO: /* ETIMEDOUT */ |
| 1491 | case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */ |
Willy Tarreau | 4bd07de | 2014-01-24 16:10:57 +0100 | [diff] [blame] | 1492 | conn->flags |= CO_FL_ERROR; |
| 1493 | chk_report_conn_err(conn, errno, 0); |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1494 | break; |
| 1495 | case SN_ERR_PRXCOND: |
| 1496 | case SN_ERR_RESOURCE: |
| 1497 | case SN_ERR_INTERNAL: |
Willy Tarreau | 4bd07de | 2014-01-24 16:10:57 +0100 | [diff] [blame] | 1498 | conn->flags |= CO_FL_ERROR; |
| 1499 | chk_report_conn_err(conn, 0, 0); |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1500 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1501 | } |
| 1502 | |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1503 | /* here, we have seen a synchronous error, no fd was allocated */ |
Willy Tarreau | 6b0a850 | 2012-11-23 08:51:32 +0100 | [diff] [blame] | 1504 | |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1505 | check->state &= ~CHK_ST_INPROGRESS; |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 1506 | check_notify_failure(check); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1507 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1508 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1509 | * to establish but only when timeout.check is set |
| 1510 | * as it may be to short for a full check otherwise |
| 1511 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1512 | while (tick_is_expired(t->expire, now_ms)) { |
| 1513 | int t_con; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1514 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1515 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1516 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1517 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1518 | if (s->proxy->timeout.check) |
| 1519 | t->expire = tick_first(t->expire, t_con); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1520 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1521 | } |
| 1522 | else { |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1523 | /* there was a test running. |
| 1524 | * First, let's check whether there was an uncaught error, |
| 1525 | * which can happen on connect timeout or error. |
| 1526 | */ |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 1527 | if (s->check.result == CHK_RES_UNKNOWN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1528 | /* good connection is enough for pure TCP check */ |
| 1529 | if ((conn->flags & CO_FL_CONNECTED) && !check->type) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1530 | if (check->use_ssl) |
| 1531 | set_server_check_status(check, HCHK_STATUS_L6OK, NULL); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1532 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1533 | set_server_check_status(check, HCHK_STATUS_L4OK, NULL); |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1534 | } |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1535 | else if ((conn->flags & CO_FL_ERROR) || expired) { |
| 1536 | chk_report_conn_err(conn, 0, expired); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1537 | } |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1538 | else |
| 1539 | goto out_wait; /* timeout not reached, wait again */ |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1540 | } |
| 1541 | |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1542 | /* check complete or aborted */ |
Willy Tarreau | 5ba04f6 | 2013-02-12 15:23:12 +0100 | [diff] [blame] | 1543 | if (conn->xprt) { |
| 1544 | /* The check was aborted and the connection was not yet closed. |
| 1545 | * This can happen upon timeout, or when an external event such |
| 1546 | * as a failed response coupled with "observe layer7" caused the |
| 1547 | * server state to be suddenly changed. |
| 1548 | */ |
Willy Tarreau | 46be2e5 | 2014-01-20 12:10:52 +0100 | [diff] [blame] | 1549 | conn_drain(conn); |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 1550 | conn_force_close(conn); |
Willy Tarreau | 5ba04f6 | 2013-02-12 15:23:12 +0100 | [diff] [blame] | 1551 | } |
| 1552 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 1553 | if (check->result == CHK_RES_FAILED) { |
| 1554 | /* a failure or timeout detected */ |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 1555 | check_notify_failure(check); |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 1556 | } |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 1557 | else if (check->result == CHK_RES_CONDPASS) { |
| 1558 | /* check is OK but asks for stopping mode */ |
| 1559 | check_notify_stopping(check); |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 1560 | } |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 1561 | else if (check->result == CHK_RES_PASSED) { |
| 1562 | /* a success was detected */ |
| 1563 | check_notify_success(check); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1564 | } |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1565 | check->state &= ~CHK_ST_INPROGRESS; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1566 | |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1567 | rv = 0; |
| 1568 | if (global.spread_checks > 0) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1569 | rv = srv_getinter(check) * global.spread_checks / 100; |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1570 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1571 | } |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1572 | t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1573 | } |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1574 | |
| 1575 | reschedule: |
| 1576 | while (tick_is_expired(t->expire, now_ms)) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1577 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1578 | out_wait: |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1579 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1580 | } |
| 1581 | |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 1582 | static int start_check_task(struct check *check, int mininter, |
| 1583 | int nbcheck, int srvpos) |
| 1584 | { |
| 1585 | struct task *t; |
| 1586 | /* task for the check */ |
| 1587 | if ((t = task_new()) == NULL) { |
| 1588 | Alert("Starting [%s:%s] check: out of memory.\n", |
| 1589 | check->server->proxy->id, check->server->id); |
| 1590 | return 0; |
| 1591 | } |
| 1592 | |
| 1593 | check->task = t; |
| 1594 | t->process = process_chk; |
| 1595 | t->context = check; |
| 1596 | |
Willy Tarreau | 1746eec | 2014-04-25 10:46:47 +0200 | [diff] [blame] | 1597 | if (mininter < srv_getinter(check)) |
| 1598 | mininter = srv_getinter(check); |
| 1599 | |
| 1600 | if (global.max_spread_checks && mininter > global.max_spread_checks) |
| 1601 | mininter = global.max_spread_checks; |
| 1602 | |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 1603 | /* check this every ms */ |
Willy Tarreau | 1746eec | 2014-04-25 10:46:47 +0200 | [diff] [blame] | 1604 | t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck)); |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 1605 | check->start = now; |
| 1606 | task_queue(t); |
| 1607 | |
| 1608 | return 1; |
| 1609 | } |
| 1610 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1611 | /* |
| 1612 | * Start health-check. |
| 1613 | * Returns 0 if OK, -1 if error, and prints the error in this case. |
| 1614 | */ |
| 1615 | int start_checks() { |
| 1616 | |
| 1617 | struct proxy *px; |
| 1618 | struct server *s; |
| 1619 | struct task *t; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1620 | int nbcheck=0, mininter=0, srvpos=0; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1621 | |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 1622 | /* 1- count the checkers to run simultaneously. |
| 1623 | * We also determine the minimum interval among all of those which |
| 1624 | * have an interval larger than SRV_CHK_INTER_THRES. This interval |
| 1625 | * will be used to spread their start-up date. Those which have |
Jamie Gloudon | 801a0a3 | 2012-08-25 00:18:33 -0400 | [diff] [blame] | 1626 | * a shorter interval will start independently and will not dictate |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 1627 | * too short an interval for all others. |
| 1628 | */ |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1629 | for (px = proxy; px; px = px->next) { |
| 1630 | for (s = px->srv; s; s = s->next) { |
Willy Tarreau | e7b7348 | 2013-11-21 11:50:50 +0100 | [diff] [blame] | 1631 | if (s->slowstart) { |
| 1632 | if ((t = task_new()) == NULL) { |
| 1633 | Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 1634 | return -1; |
| 1635 | } |
| 1636 | /* We need a warmup task that will be called when the server |
| 1637 | * state switches from down to up. |
| 1638 | */ |
| 1639 | s->warmup = t; |
| 1640 | t->process = server_warmup; |
| 1641 | t->context = s; |
| 1642 | t->expire = TICK_ETERNITY; |
| 1643 | } |
| 1644 | |
Willy Tarreau | d8514a2 | 2013-12-11 21:10:14 +0100 | [diff] [blame] | 1645 | if (s->check.state & CHK_ST_CONFIGURED) { |
| 1646 | nbcheck++; |
| 1647 | if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) && |
| 1648 | (!mininter || mininter > srv_getinter(&s->check))) |
| 1649 | mininter = srv_getinter(&s->check); |
| 1650 | } |
Willy Tarreau | 15f3910 | 2013-12-11 20:41:18 +0100 | [diff] [blame] | 1651 | |
Willy Tarreau | d8514a2 | 2013-12-11 21:10:14 +0100 | [diff] [blame] | 1652 | if (s->agent.state & CHK_ST_CONFIGURED) { |
| 1653 | nbcheck++; |
| 1654 | if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) && |
| 1655 | (!mininter || mininter > srv_getinter(&s->agent))) |
| 1656 | mininter = srv_getinter(&s->agent); |
| 1657 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1658 | } |
| 1659 | } |
| 1660 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1661 | if (!nbcheck) |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1662 | return 0; |
| 1663 | |
| 1664 | srand((unsigned)time(NULL)); |
| 1665 | |
| 1666 | /* |
| 1667 | * 2- start them as far as possible from each others. For this, we will |
| 1668 | * start them after their interval set to the min interval divided by |
| 1669 | * the number of servers, weighted by the server's position in the list. |
| 1670 | */ |
| 1671 | for (px = proxy; px; px = px->next) { |
| 1672 | for (s = px->srv; s; s = s->next) { |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 1673 | /* A task for the main check */ |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 1674 | if (s->check.state & CHK_ST_CONFIGURED) { |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 1675 | if (!start_check_task(&s->check, mininter, nbcheck, srvpos)) |
| 1676 | return -1; |
| 1677 | srvpos++; |
| 1678 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1679 | |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 1680 | /* A task for a auxiliary agent check */ |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 1681 | if (s->agent.state & CHK_ST_CONFIGURED) { |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 1682 | if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) { |
| 1683 | return -1; |
| 1684 | } |
| 1685 | srvpos++; |
| 1686 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1687 | } |
| 1688 | } |
| 1689 | return 0; |
| 1690 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1691 | |
| 1692 | /* |
Willy Tarreau | 5b3a202 | 2012-09-28 15:01:02 +0200 | [diff] [blame] | 1693 | * Perform content verification check on data in s->check.buffer buffer. |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1694 | * The buffer MUST be terminated by a null byte before calling this function. |
| 1695 | * Sets server status appropriately. The caller is responsible for ensuring |
| 1696 | * that the buffer contains at least 13 characters. If <done> is zero, we may |
| 1697 | * return 0 to indicate that data is required to decide of a match. |
| 1698 | */ |
| 1699 | static int httpchk_expect(struct server *s, int done) |
| 1700 | { |
| 1701 | static char status_msg[] = "HTTP status check returned code <000>"; |
| 1702 | char status_code[] = "000"; |
| 1703 | char *contentptr; |
| 1704 | int crlf; |
| 1705 | int ret; |
| 1706 | |
| 1707 | switch (s->proxy->options2 & PR_O2_EXP_TYPE) { |
| 1708 | case PR_O2_EXP_STS: |
| 1709 | case PR_O2_EXP_RSTS: |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1710 | memcpy(status_code, s->check.bi->data + 9, 3); |
| 1711 | memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1712 | |
| 1713 | if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS) |
| 1714 | ret = strncmp(s->proxy->expect_str, status_code, 3) == 0; |
| 1715 | else |
| 1716 | ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0; |
| 1717 | |
| 1718 | /* we necessarily have the response, so there are no partial failures */ |
| 1719 | if (s->proxy->options2 & PR_O2_EXP_INV) |
| 1720 | ret = !ret; |
| 1721 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1722 | set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1723 | break; |
| 1724 | |
| 1725 | case PR_O2_EXP_STR: |
| 1726 | case PR_O2_EXP_RSTR: |
| 1727 | /* very simple response parser: ignore CR and only count consecutive LFs, |
| 1728 | * stop with contentptr pointing to first char after the double CRLF or |
| 1729 | * to '\0' if crlf < 2. |
| 1730 | */ |
| 1731 | crlf = 0; |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1732 | for (contentptr = s->check.bi->data; *contentptr; contentptr++) { |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1733 | if (crlf >= 2) |
| 1734 | break; |
| 1735 | if (*contentptr == '\r') |
| 1736 | continue; |
| 1737 | else if (*contentptr == '\n') |
| 1738 | crlf++; |
| 1739 | else |
| 1740 | crlf = 0; |
| 1741 | } |
| 1742 | |
| 1743 | /* Check that response contains a body... */ |
| 1744 | if (crlf < 2) { |
| 1745 | if (!done) |
| 1746 | return 0; |
| 1747 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1748 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1749 | "HTTP content check could not find a response body"); |
| 1750 | return 1; |
| 1751 | } |
| 1752 | |
| 1753 | /* Check that response body is not empty... */ |
| 1754 | if (*contentptr == '\0') { |
Willy Tarreau | a164fb5 | 2011-04-13 09:32:41 +0200 | [diff] [blame] | 1755 | if (!done) |
| 1756 | return 0; |
| 1757 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1758 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1759 | "HTTP content check found empty response body"); |
| 1760 | return 1; |
| 1761 | } |
| 1762 | |
| 1763 | /* Check the response content against the supplied string |
| 1764 | * or regex... */ |
| 1765 | if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR) |
| 1766 | ret = strstr(contentptr, s->proxy->expect_str) != NULL; |
| 1767 | else |
| 1768 | ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0; |
| 1769 | |
| 1770 | /* if we don't match, we may need to wait more */ |
| 1771 | if (!ret && !done) |
| 1772 | return 0; |
| 1773 | |
| 1774 | if (ret) { |
| 1775 | /* content matched */ |
| 1776 | if (s->proxy->options2 & PR_O2_EXP_INV) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1777 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1778 | "HTTP check matched unwanted content"); |
| 1779 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1780 | set_server_check_status(&s->check, HCHK_STATUS_L7OKD, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1781 | "HTTP content check matched"); |
| 1782 | } |
| 1783 | else { |
| 1784 | if (s->proxy->options2 & PR_O2_EXP_INV) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1785 | set_server_check_status(&s->check, HCHK_STATUS_L7OKD, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1786 | "HTTP check did not match unwanted content"); |
| 1787 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1788 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1789 | "HTTP content check did not match"); |
| 1790 | } |
| 1791 | break; |
| 1792 | } |
| 1793 | return 1; |
| 1794 | } |
| 1795 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1796 | /* |
| 1797 | * return the id of a step in a send/expect session |
| 1798 | */ |
| 1799 | static int tcpcheck_get_step_id(struct server *s) |
| 1800 | { |
| 1801 | struct tcpcheck_rule *cur = NULL, *next = NULL; |
| 1802 | int i = 0; |
| 1803 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 1804 | cur = s->check.last_started_step; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1805 | |
| 1806 | /* no step => first step */ |
| 1807 | if (cur == NULL) |
| 1808 | return 1; |
| 1809 | |
| 1810 | /* increment i until current step */ |
| 1811 | list_for_each_entry(next, &s->proxy->tcpcheck_rules, list) { |
| 1812 | if (next->list.p == &cur->list) |
| 1813 | break; |
| 1814 | ++i; |
| 1815 | } |
| 1816 | |
| 1817 | return i; |
| 1818 | } |
| 1819 | |
| 1820 | static void tcpcheck_main(struct connection *conn) |
| 1821 | { |
| 1822 | char *contentptr; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1823 | struct list *head = NULL; |
| 1824 | struct tcpcheck_rule *cur = NULL; |
| 1825 | int done = 0, ret = 0; |
| 1826 | |
| 1827 | struct check *check = conn->owner; |
| 1828 | struct server *s = check->server; |
| 1829 | struct task *t = check->task; |
| 1830 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 1831 | /* |
| 1832 | * don't do anything until the connection is established but if we're running |
| 1833 | * first step which must be a connect |
| 1834 | */ |
| 1835 | if (check->current_step && (!(conn->flags & CO_FL_CONNECTED))) { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1836 | /* update expire time, should be done by process_chk */ |
| 1837 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1838 | * to establish but only when timeout.check is set |
| 1839 | * as it may be to short for a full check otherwise |
| 1840 | */ |
| 1841 | while (tick_is_expired(t->expire, now_ms)) { |
| 1842 | int t_con; |
| 1843 | |
| 1844 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 1845 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 1846 | |
| 1847 | if (s->proxy->timeout.check) |
| 1848 | t->expire = tick_first(t->expire, t_con); |
| 1849 | } |
| 1850 | return; |
| 1851 | } |
| 1852 | |
| 1853 | /* here, we know that the connection is established */ |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 1854 | if (check->result != CHK_RES_UNKNOWN) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1855 | goto out_end_tcpcheck; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1856 | |
| 1857 | /* head is be the first element of the double chained list */ |
| 1858 | head = &s->proxy->tcpcheck_rules; |
| 1859 | |
| 1860 | /* no step means first step |
| 1861 | * initialisation */ |
| 1862 | if (check->current_step == NULL) { |
| 1863 | check->bo->p = check->bo->data; |
| 1864 | check->bo->o = 0; |
| 1865 | check->bi->p = check->bi->data; |
| 1866 | check->bi->i = 0; |
| 1867 | cur = check->current_step = LIST_ELEM(head->n, struct tcpcheck_rule *, list); |
| 1868 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 1869 | if (s->proxy->timeout.check) |
| 1870 | t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check); |
| 1871 | } |
| 1872 | /* keep on processing step */ |
| 1873 | else { |
| 1874 | cur = check->current_step; |
| 1875 | } |
| 1876 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 1877 | if (conn->flags & CO_FL_HANDSHAKE) |
| 1878 | return; |
| 1879 | |
| 1880 | /* It's only the rules which will enable send/recv */ |
| 1881 | __conn_data_stop_both(conn); |
| 1882 | |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 1883 | while (1) { |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 1884 | /* we have to try to flush the output buffer before reading, at the end, |
| 1885 | * or if we're about to send a string that does not fit in the remaining space. |
| 1886 | */ |
| 1887 | if (check->bo->o && |
| 1888 | (&cur->list == head || |
| 1889 | check->current_step->action != TCPCHK_ACT_SEND || |
| 1890 | check->current_step->string_len >= buffer_total_space(check->bo))) { |
| 1891 | |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 1892 | if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) { |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 1893 | if (conn->flags & CO_FL_ERROR) { |
| 1894 | chk_report_conn_err(conn, errno, 0); |
| 1895 | __conn_data_stop_both(conn); |
| 1896 | goto out_end_tcpcheck; |
| 1897 | } |
| 1898 | goto out_need_io; |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 1899 | } |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 1900 | } |
| 1901 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 1902 | /* did we reach the end ? If so, let's check that everything was sent */ |
| 1903 | if (&cur->list == head) { |
| 1904 | if (check->bo->o) |
| 1905 | goto out_need_io; |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 1906 | break; |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 1907 | } |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 1908 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 1909 | if (check->current_step->action == TCPCHK_ACT_CONNECT) { |
| 1910 | struct protocol *proto; |
| 1911 | struct xprt_ops *xprt; |
| 1912 | |
| 1913 | /* mark the step as started */ |
| 1914 | check->last_started_step = check->current_step; |
| 1915 | /* first, shut existing connection */ |
| 1916 | conn_force_close(conn); |
| 1917 | |
| 1918 | /* prepare new connection */ |
| 1919 | /* initialization */ |
| 1920 | conn_init(conn); |
| 1921 | conn_attach(conn, check, &check_conn_cb); |
| 1922 | conn->target = &s->obj_type; |
| 1923 | |
| 1924 | /* no client address */ |
| 1925 | clear_addr(&conn->addr.from); |
| 1926 | |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 1927 | if (is_addr(&s->check_common.addr)) { |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 1928 | /* we'll connect to the check addr specified on the server */ |
| 1929 | conn->addr.to = s->check_common.addr; |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 1930 | proto = s->check_common.proto; |
| 1931 | } |
| 1932 | else { |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 1933 | /* we'll connect to the addr on the server */ |
| 1934 | conn->addr.to = s->addr; |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 1935 | proto = s->proto; |
| 1936 | } |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 1937 | |
| 1938 | /* port */ |
| 1939 | if (check->current_step->port) |
| 1940 | set_host_port(&conn->addr.to, check->current_step->port); |
| 1941 | else if (check->port) |
| 1942 | set_host_port(&conn->addr.to, check->port); |
| 1943 | |
| 1944 | #ifdef USE_OPENSSL |
| 1945 | if (check->current_step->conn_opts & TCPCHK_OPT_SSL) { |
| 1946 | xprt = &ssl_sock; |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 1947 | } |
| 1948 | else { |
| 1949 | xprt = &raw_sock; |
| 1950 | } |
| 1951 | #else /* USE_OPENSSL */ |
| 1952 | xprt = &raw_sock; |
| 1953 | #endif /* USE_OPENSSL */ |
| 1954 | conn_prepare(conn, proto, xprt); |
| 1955 | |
| 1956 | ret = SN_ERR_INTERNAL; |
| 1957 | if (proto->connect) |
| 1958 | ret = proto->connect(conn, check->type, (check->type) ? 0 : 2); |
| 1959 | conn->flags |= CO_FL_WAKE_DATA; |
| 1960 | if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) { |
| 1961 | conn->send_proxy_ofs = 1; |
| 1962 | conn->flags |= CO_FL_SEND_PROXY; |
| 1963 | } |
| 1964 | |
| 1965 | /* It can return one of : |
| 1966 | * - SN_ERR_NONE if everything's OK |
| 1967 | * - SN_ERR_SRVTO if there are no more servers |
| 1968 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 1969 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1970 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1971 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 1972 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 1973 | * Note that we try to prevent the network stack from sending the ACK during the |
| 1974 | * connect() when a pure TCP check is used (without PROXY protocol). |
| 1975 | */ |
| 1976 | switch (ret) { |
| 1977 | case SN_ERR_NONE: |
| 1978 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1979 | * to establish but only when timeout.check is set |
| 1980 | * as it may be to short for a full check otherwise |
| 1981 | */ |
| 1982 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 1983 | |
| 1984 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 1985 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 1986 | t->expire = tick_first(t->expire, t_con); |
| 1987 | } |
| 1988 | break; |
| 1989 | case SN_ERR_SRVTO: /* ETIMEDOUT */ |
| 1990 | case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */ |
| 1991 | chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s", |
| 1992 | tcpcheck_get_step_id(s), strerror(errno)); |
| 1993 | set_server_check_status(check, HCHK_STATUS_L4CON, trash.str); |
| 1994 | goto out_end_tcpcheck; |
| 1995 | case SN_ERR_PRXCOND: |
| 1996 | case SN_ERR_RESOURCE: |
| 1997 | case SN_ERR_INTERNAL: |
| 1998 | chunk_printf(&trash, "TCPCHK error establishing connection at step %d", |
| 1999 | tcpcheck_get_step_id(s)); |
| 2000 | set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str); |
| 2001 | goto out_end_tcpcheck; |
| 2002 | } |
| 2003 | |
| 2004 | /* allow next rule */ |
| 2005 | cur = (struct tcpcheck_rule *)cur->list.n; |
| 2006 | check->current_step = cur; |
| 2007 | |
| 2008 | /* don't do anything until the connection is established */ |
| 2009 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 2010 | /* update expire time, should be done by process_chk */ |
| 2011 | /* we allow up to min(inter, timeout.connect) for a connection |
| 2012 | * to establish but only when timeout.check is set |
| 2013 | * as it may be to short for a full check otherwise |
| 2014 | */ |
| 2015 | while (tick_is_expired(t->expire, now_ms)) { |
| 2016 | int t_con; |
| 2017 | |
| 2018 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 2019 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 2020 | |
| 2021 | if (s->proxy->timeout.check) |
| 2022 | t->expire = tick_first(t->expire, t_con); |
| 2023 | } |
| 2024 | return; |
| 2025 | } |
| 2026 | |
| 2027 | } /* end 'connect' */ |
| 2028 | else if (check->current_step->action == TCPCHK_ACT_SEND) { |
| 2029 | /* mark the step as started */ |
| 2030 | check->last_started_step = check->current_step; |
| 2031 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2032 | /* reset the read buffer */ |
| 2033 | if (*check->bi->data != '\0') { |
| 2034 | *check->bi->data = '\0'; |
| 2035 | check->bi->i = 0; |
| 2036 | } |
| 2037 | |
| 2038 | if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) { |
| 2039 | conn->flags |= CO_FL_ERROR; |
| 2040 | chk_report_conn_err(conn, 0, 0); |
| 2041 | goto out_end_tcpcheck; |
| 2042 | } |
| 2043 | |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2044 | if (check->current_step->string_len >= check->bo->size) { |
| 2045 | chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d", |
| 2046 | check->current_step->string_len, check->bo->size, |
| 2047 | tcpcheck_get_step_id(s)); |
| 2048 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2049 | goto out_end_tcpcheck; |
| 2050 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2051 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2052 | /* do not try to send if there is no space */ |
| 2053 | if (check->current_step->string_len >= buffer_total_space(check->bo)) |
| 2054 | continue; |
| 2055 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2056 | bo_putblk(check->bo, check->current_step->string, check->current_step->string_len); |
| 2057 | *check->bo->p = '\0'; /* to make gdb output easier to read */ |
| 2058 | |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2059 | /* go to next rule and try to send */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2060 | cur = (struct tcpcheck_rule *)cur->list.n; |
| 2061 | check->current_step = cur; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2062 | } /* end 'send' */ |
Willy Tarreau | 98aec9f | 2013-12-06 16:16:41 +0100 | [diff] [blame] | 2063 | else if (check->current_step->action == TCPCHK_ACT_EXPECT) { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 2064 | if (unlikely(check->result == CHK_RES_FAILED)) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2065 | goto out_end_tcpcheck; |
| 2066 | |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 2067 | if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) { |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2068 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) { |
| 2069 | done = 1; |
| 2070 | if ((conn->flags & CO_FL_ERROR) && !check->bi->i) { |
| 2071 | /* Report network errors only if we got no other data. Otherwise |
| 2072 | * we'll let the upper layers decide whether the response is OK |
| 2073 | * or not. It is very common that an RST sent by the server is |
| 2074 | * reported as an error just after the last data chunk. |
| 2075 | */ |
| 2076 | chk_report_conn_err(conn, errno, 0); |
| 2077 | goto out_end_tcpcheck; |
| 2078 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2079 | } |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2080 | else |
| 2081 | goto out_need_io; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2082 | } |
| 2083 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2084 | /* mark the step as started */ |
| 2085 | check->last_started_step = check->current_step; |
| 2086 | |
| 2087 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2088 | /* Intermediate or complete response received. |
| 2089 | * Terminate string in check->bi->data buffer. |
| 2090 | */ |
| 2091 | if (check->bi->i < check->bi->size) { |
| 2092 | check->bi->data[check->bi->i] = '\0'; |
| 2093 | } |
| 2094 | else { |
| 2095 | check->bi->data[check->bi->i - 1] = '\0'; |
| 2096 | done = 1; /* buffer full, don't wait for more data */ |
| 2097 | } |
| 2098 | |
| 2099 | contentptr = check->bi->data; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2100 | |
| 2101 | /* Check that response body is not empty... */ |
Willy Tarreau | ec6b012 | 2014-05-13 17:57:29 +0200 | [diff] [blame] | 2102 | if (!check->bi->i) { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2103 | if (!done) |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2104 | continue; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2105 | |
| 2106 | /* empty response */ |
| 2107 | chunk_printf(&trash, "TCPCHK got an empty response at step %d", |
| 2108 | tcpcheck_get_step_id(s)); |
| 2109 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2110 | |
| 2111 | goto out_end_tcpcheck; |
| 2112 | } |
| 2113 | |
| 2114 | if (!done && (cur->string != NULL) && (check->bi->i < cur->string_len) ) |
Willy Tarreau | a970c28 | 2013-12-06 12:47:19 +0100 | [diff] [blame] | 2115 | continue; /* try to read more */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2116 | |
Willy Tarreau | a970c28 | 2013-12-06 12:47:19 +0100 | [diff] [blame] | 2117 | tcpcheck_expect: |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2118 | if (cur->string != NULL) |
Willy Tarreau | ec6b012 | 2014-05-13 17:57:29 +0200 | [diff] [blame] | 2119 | ret = my_memmem(contentptr, check->bi->i, cur->string, cur->string_len) != NULL; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2120 | else if (cur->expect_regex != NULL) |
| 2121 | ret = regexec(cur->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0; |
| 2122 | |
| 2123 | if (!ret && !done) |
Willy Tarreau | a970c28 | 2013-12-06 12:47:19 +0100 | [diff] [blame] | 2124 | continue; /* try to read more */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2125 | |
| 2126 | /* matched */ |
| 2127 | if (ret) { |
| 2128 | /* matched but we did not want to => ERROR */ |
| 2129 | if (cur->inverse) { |
| 2130 | /* we were looking for a string */ |
| 2131 | if (cur->string != NULL) { |
| 2132 | chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d", |
| 2133 | cur->string, tcpcheck_get_step_id(s)); |
| 2134 | } |
| 2135 | else { |
| 2136 | /* we were looking for a regex */ |
| 2137 | chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d", |
| 2138 | tcpcheck_get_step_id(s)); |
| 2139 | } |
| 2140 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2141 | goto out_end_tcpcheck; |
| 2142 | } |
| 2143 | /* matched and was supposed to => OK, next step */ |
| 2144 | else { |
| 2145 | cur = (struct tcpcheck_rule*)cur->list.n; |
| 2146 | check->current_step = cur; |
Willy Tarreau | 98aec9f | 2013-12-06 16:16:41 +0100 | [diff] [blame] | 2147 | if (check->current_step->action == TCPCHK_ACT_EXPECT) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2148 | goto tcpcheck_expect; |
| 2149 | __conn_data_stop_recv(conn); |
| 2150 | } |
| 2151 | } |
| 2152 | else { |
| 2153 | /* not matched */ |
| 2154 | /* not matched and was not supposed to => OK, next step */ |
| 2155 | if (cur->inverse) { |
| 2156 | cur = (struct tcpcheck_rule*)cur->list.n; |
| 2157 | check->current_step = cur; |
Willy Tarreau | 98aec9f | 2013-12-06 16:16:41 +0100 | [diff] [blame] | 2158 | if (check->current_step->action == TCPCHK_ACT_EXPECT) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2159 | goto tcpcheck_expect; |
| 2160 | __conn_data_stop_recv(conn); |
| 2161 | } |
| 2162 | /* not matched but was supposed to => ERROR */ |
| 2163 | else { |
| 2164 | /* we were looking for a string */ |
| 2165 | if (cur->string != NULL) { |
| 2166 | chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d", |
| 2167 | cur->string, tcpcheck_get_step_id(s)); |
| 2168 | } |
| 2169 | else { |
| 2170 | /* we were looking for a regex */ |
| 2171 | chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d", |
| 2172 | tcpcheck_get_step_id(s)); |
| 2173 | } |
| 2174 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2175 | goto out_end_tcpcheck; |
| 2176 | } |
| 2177 | } |
| 2178 | } /* end expect */ |
| 2179 | } /* end loop over double chained step list */ |
| 2180 | |
| 2181 | set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)"); |
| 2182 | goto out_end_tcpcheck; |
| 2183 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2184 | out_need_io: |
| 2185 | if (check->bo->o) |
| 2186 | __conn_data_want_send(conn); |
| 2187 | |
| 2188 | if (check->current_step->action == TCPCHK_ACT_EXPECT) |
| 2189 | __conn_data_want_recv(conn); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2190 | return; |
| 2191 | |
| 2192 | out_end_tcpcheck: |
| 2193 | /* collect possible new errors */ |
| 2194 | if (conn->flags & CO_FL_ERROR) |
| 2195 | chk_report_conn_err(conn, 0, 0); |
| 2196 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2197 | /* cleanup before leaving */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2198 | check->current_step = NULL; |
| 2199 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 2200 | if (check->result == CHK_RES_FAILED) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2201 | conn->flags |= CO_FL_ERROR; |
| 2202 | |
| 2203 | __conn_data_stop_both(conn); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2204 | |
| 2205 | return; |
| 2206 | } |
| 2207 | |
| 2208 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2209 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2210 | * Local variables: |
| 2211 | * c-indent-level: 8 |
| 2212 | * c-basic-offset: 8 |
| 2213 | * End: |
| 2214 | */ |