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