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" }, |
Cyril Bonté | 77010d8 | 2014-08-07 01:55:37 +0200 | [diff] [blame] | 96 | [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "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; |
Cyril Bonté | 9dbcfab | 2014-08-07 01:55:39 +0200 | [diff] [blame] | 1522 | task_wakeup(elem->t, TASK_WOKEN_IO); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1523 | return; |
| 1524 | } |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | static void sigchld_handler(int signal) |
| 1529 | { |
| 1530 | pid_t pid; |
| 1531 | int status; |
| 1532 | while ((pid = waitpid(0, &status, WNOHANG)) > 0) |
| 1533 | pid_list_expire(pid, status); |
| 1534 | } |
| 1535 | |
| 1536 | static int init_pid_list(void) { |
| 1537 | struct sigaction action = { |
| 1538 | .sa_handler = sigchld_handler, |
| 1539 | .sa_flags = SA_NOCLDSTOP |
| 1540 | }; |
| 1541 | |
| 1542 | if (pool2_pid_list != NULL) |
| 1543 | /* Nothing to do */ |
| 1544 | return 0; |
| 1545 | |
| 1546 | if (sigaction(SIGCHLD, &action, NULL)) { |
| 1547 | Alert("Failed to set signal handler for external health checks: %s. Aborting.\n", |
| 1548 | strerror(errno)); |
| 1549 | return 1; |
| 1550 | } |
| 1551 | |
| 1552 | pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED); |
| 1553 | if (pool2_pid_list == NULL) { |
| 1554 | Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n", |
| 1555 | strerror(errno)); |
| 1556 | return 1; |
| 1557 | } |
| 1558 | |
| 1559 | return 0; |
| 1560 | } |
| 1561 | |
| 1562 | |
| 1563 | static int prepare_external_check(struct check *check) |
| 1564 | { |
| 1565 | struct server *s = check->server; |
| 1566 | struct proxy *px = s->proxy; |
| 1567 | struct listener *listener = NULL, *l; |
| 1568 | int i; |
| 1569 | const char *err_fmt = "Starting [%s:%s] check: out of memory.\n"; |
| 1570 | const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH; |
| 1571 | char host[46]; |
| 1572 | char serv[6]; |
| 1573 | |
| 1574 | list_for_each_entry(l, &px->conf.listeners, by_fe) |
| 1575 | /* Use the first INET, INET6 or UNIX listener */ |
| 1576 | if (l->addr.ss_family == AF_INET || |
| 1577 | l->addr.ss_family == AF_INET6 || |
| 1578 | l->addr.ss_family == AF_UNIX) { |
| 1579 | listener = l; |
| 1580 | break; |
| 1581 | } |
| 1582 | |
| 1583 | if (!listener) { |
| 1584 | err_fmt = "Starting [%s:%s] check: no listener.\n"; |
| 1585 | goto err; |
| 1586 | } |
| 1587 | |
| 1588 | check->curpid = NULL; |
| 1589 | |
| 1590 | check->envp = calloc(2, sizeof(check->argv)); |
| 1591 | if (!check->envp) |
| 1592 | goto err; |
| 1593 | check->envp[0] = malloc(strlen("PATH=") + strlen(path) + 1); |
| 1594 | if (!check->envp[0]) |
| 1595 | goto err; |
| 1596 | strcpy(check->envp[0], "PATH="); |
| 1597 | strcpy(check->envp[0] + strlen(check->envp[0]), path); |
| 1598 | check->envp[1] = NULL; |
| 1599 | |
| 1600 | check->argv = calloc(6, sizeof(check->argv)); |
| 1601 | if (!check->argv) |
| 1602 | goto err; |
| 1603 | |
| 1604 | check->argv[0] = px->check_command; |
| 1605 | |
| 1606 | if (listener->addr.ss_family == AF_INET || |
| 1607 | listener->addr.ss_family == AF_INET6) { |
| 1608 | addr_to_str(&listener->addr, host, sizeof(host)); |
| 1609 | check->argv[1] = strdup(host); |
| 1610 | port_to_str(&listener->addr, serv, sizeof(serv)); |
| 1611 | check->argv[2] = strdup(serv); |
| 1612 | } else if (listener->addr.ss_family == AF_UNIX) { |
| 1613 | const struct sockaddr_un *un; |
| 1614 | |
| 1615 | un = (struct sockaddr_un *)&listener->addr; |
| 1616 | check->argv[1] = strdup(un->sun_path); |
| 1617 | check->argv[2] = strdup("NOT_USED"); |
| 1618 | } else { |
| 1619 | goto err; |
| 1620 | } |
| 1621 | |
| 1622 | addr_to_str(&s->addr, host, sizeof(host)); |
| 1623 | check->argv[3] = strdup(host); |
| 1624 | port_to_str(&s->addr, serv, sizeof(serv)); |
| 1625 | check->argv[4] = strdup(serv); |
| 1626 | |
| 1627 | for (i = 0; i < 5; i++) |
| 1628 | if (!check->argv[i]) |
| 1629 | goto err; |
| 1630 | |
Cyril Bonté | 99c5bf5 | 2014-08-07 01:55:38 +0200 | [diff] [blame] | 1631 | return 1; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1632 | err: |
| 1633 | if (check->envp) { |
| 1634 | free(check->envp[1]); |
| 1635 | free(check->envp); |
| 1636 | check->envp = NULL; |
| 1637 | } |
| 1638 | |
| 1639 | if (check->argv) { |
| 1640 | for (i = 1; i < 5; i++) |
| 1641 | free(check->argv[i]); |
| 1642 | free(check->argv); |
| 1643 | check->argv = NULL; |
| 1644 | } |
| 1645 | Alert(err_fmt, px->id, s->id); |
Cyril Bonté | 99c5bf5 | 2014-08-07 01:55:38 +0200 | [diff] [blame] | 1646 | return 0; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1647 | } |
| 1648 | |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1649 | /* |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1650 | * establish a server health-check that makes use of a process. |
| 1651 | * |
| 1652 | * It can return one of : |
| 1653 | * - SN_ERR_NONE if everything's OK |
| 1654 | * - SN_ERR_SRVTO if there are no more servers |
| 1655 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 1656 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1657 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1658 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 1659 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 1660 | * |
| 1661 | * Blocks and then unblocks SIGCHLD |
| 1662 | */ |
| 1663 | static int connect_proc_chk(struct task *t) |
| 1664 | { |
| 1665 | struct check *check = t->context; |
| 1666 | struct server *s = check->server; |
| 1667 | struct proxy *px = s->proxy; |
| 1668 | int status; |
| 1669 | pid_t pid; |
| 1670 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1671 | status = SN_ERR_RESOURCE; |
| 1672 | |
| 1673 | block_sigchld(); |
| 1674 | |
| 1675 | pid = fork(); |
| 1676 | if (pid < 0) { |
| 1677 | Alert("Failed to fork process for external health check: %s. Aborting.\n", |
| 1678 | strerror(errno)); |
| 1679 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 1680 | goto out; |
| 1681 | } |
| 1682 | if (pid == 0) { |
| 1683 | /* Child */ |
| 1684 | extern char **environ; |
| 1685 | environ = check->envp; |
| 1686 | execvp(px->check_command, check->argv); |
| 1687 | Alert("Failed to exec process for external health check: %s. Aborting.\n", |
| 1688 | strerror(errno)); |
| 1689 | exit(-1); |
| 1690 | } |
| 1691 | |
| 1692 | /* Parent */ |
| 1693 | if (check->result == CHK_RES_UNKNOWN) { |
| 1694 | if (pid_list_add(pid, t) != NULL) { |
| 1695 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 1696 | |
| 1697 | if (px->timeout.check && px->timeout.connect) { |
| 1698 | int t_con = tick_add(now_ms, px->timeout.connect); |
| 1699 | t->expire = tick_first(t->expire, t_con); |
| 1700 | } |
| 1701 | status = SN_ERR_NONE; |
| 1702 | goto out; |
| 1703 | } |
| 1704 | else { |
| 1705 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 1706 | } |
| 1707 | kill(pid, SIGTERM); /* process creation error */ |
| 1708 | } |
| 1709 | else |
| 1710 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 1711 | |
| 1712 | out: |
| 1713 | unblock_sigchld(); |
| 1714 | return status; |
| 1715 | } |
| 1716 | |
| 1717 | /* |
| 1718 | * establish a server health-check. |
| 1719 | * |
| 1720 | * It can return one of : |
| 1721 | * - SN_ERR_NONE if everything's OK |
| 1722 | * - SN_ERR_SRVTO if there are no more servers |
| 1723 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 1724 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1725 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1726 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 1727 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 1728 | */ |
| 1729 | static int connect_chk(struct task *t) |
| 1730 | { |
| 1731 | struct check *check = t->context; |
| 1732 | |
| 1733 | if (check->type == PR_O2_EXT_CHK) |
| 1734 | return connect_proc_chk(t); |
| 1735 | return connect_conn_chk(t); |
| 1736 | } |
| 1737 | |
| 1738 | /* |
| 1739 | * manages a server health-check that uses a process. Returns |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1740 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 1741 | */ |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1742 | static struct task *process_chk_proc(struct task *t) |
| 1743 | { |
| 1744 | struct check *check = t->context; |
| 1745 | struct server *s = check->server; |
| 1746 | struct connection *conn = check->conn; |
| 1747 | int rv; |
| 1748 | int ret; |
| 1749 | int expired = tick_is_expired(t->expire, now_ms); |
| 1750 | |
| 1751 | if (!(check->state & CHK_ST_INPROGRESS)) { |
| 1752 | /* no check currently running */ |
| 1753 | if (!expired) /* woke up too early */ |
| 1754 | return t; |
| 1755 | |
| 1756 | /* we don't send any health-checks when the proxy is |
| 1757 | * stopped, the server should not be checked or the check |
| 1758 | * is disabled. |
| 1759 | */ |
| 1760 | if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) || |
| 1761 | s->proxy->state == PR_STSTOPPED) |
| 1762 | goto reschedule; |
| 1763 | |
| 1764 | /* we'll initiate a new check */ |
| 1765 | set_server_check_status(check, HCHK_STATUS_START, NULL); |
| 1766 | |
| 1767 | check->state |= CHK_ST_INPROGRESS; |
| 1768 | |
| 1769 | ret = connect_chk(t); |
| 1770 | switch (ret) { |
| 1771 | case SN_ERR_UP: |
| 1772 | return t; |
| 1773 | case SN_ERR_NONE: |
| 1774 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1775 | * to establish but only when timeout.check is set |
| 1776 | * as it may be to short for a full check otherwise |
| 1777 | */ |
| 1778 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 1779 | |
| 1780 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 1781 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 1782 | t->expire = tick_first(t->expire, t_con); |
| 1783 | } |
| 1784 | |
| 1785 | goto reschedule; |
| 1786 | |
| 1787 | case SN_ERR_SRVTO: /* ETIMEDOUT */ |
| 1788 | case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */ |
| 1789 | conn->flags |= CO_FL_ERROR; |
| 1790 | chk_report_conn_err(conn, errno, 0); |
| 1791 | break; |
| 1792 | case SN_ERR_PRXCOND: |
| 1793 | case SN_ERR_RESOURCE: |
| 1794 | case SN_ERR_INTERNAL: |
| 1795 | conn->flags |= CO_FL_ERROR; |
| 1796 | chk_report_conn_err(conn, 0, 0); |
| 1797 | break; |
| 1798 | } |
| 1799 | |
| 1800 | /* here, we have seen a synchronous error, no fd was allocated */ |
| 1801 | |
| 1802 | check->state &= ~CHK_ST_INPROGRESS; |
| 1803 | check_notify_failure(check); |
| 1804 | |
| 1805 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1806 | * to establish but only when timeout.check is set |
| 1807 | * as it may be to short for a full check otherwise |
| 1808 | */ |
| 1809 | while (tick_is_expired(t->expire, now_ms)) { |
| 1810 | int t_con; |
| 1811 | |
| 1812 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 1813 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 1814 | |
| 1815 | if (s->proxy->timeout.check) |
| 1816 | t->expire = tick_first(t->expire, t_con); |
| 1817 | } |
| 1818 | } |
| 1819 | else { |
| 1820 | /* there was a test running. |
| 1821 | * First, let's check whether there was an uncaught error, |
| 1822 | * which can happen on connect timeout or error. |
| 1823 | */ |
| 1824 | if (check->result == CHK_RES_UNKNOWN) { |
| 1825 | /* good connection is enough for pure TCP check */ |
| 1826 | struct pid_list *elem = check->curpid; |
| 1827 | int status = HCHK_STATUS_UNKNOWN; |
| 1828 | |
| 1829 | if (elem->exited) { |
| 1830 | status = elem->status; /* Save in case the process exits between use below */ |
| 1831 | if (!WIFEXITED(status)) |
| 1832 | check->code = -1; |
| 1833 | else |
| 1834 | check->code = WEXITSTATUS(status); |
| 1835 | if (!WIFEXITED(status) || WEXITSTATUS(status)) |
| 1836 | status = HCHK_STATUS_PROCERR; |
| 1837 | else |
| 1838 | status = HCHK_STATUS_PROCOK; |
| 1839 | } else if (expired) { |
| 1840 | status = HCHK_STATUS_PROCTOUT; |
Willy Tarreau | dc3d190 | 2014-07-08 00:56:27 +0200 | [diff] [blame] | 1841 | Warning("kill %d\n", (int)elem->pid); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1842 | kill(elem->pid, SIGTERM); |
| 1843 | } |
| 1844 | set_server_check_status(check, status, NULL); |
| 1845 | } |
| 1846 | |
| 1847 | if (check->result == CHK_RES_FAILED) { |
| 1848 | /* a failure or timeout detected */ |
| 1849 | check_notify_failure(check); |
| 1850 | } |
| 1851 | else if (check->result == CHK_RES_CONDPASS) { |
| 1852 | /* check is OK but asks for stopping mode */ |
| 1853 | check_notify_stopping(check); |
| 1854 | } |
| 1855 | else if (check->result == CHK_RES_PASSED) { |
| 1856 | /* a success was detected */ |
| 1857 | check_notify_success(check); |
| 1858 | } |
| 1859 | check->state &= ~CHK_ST_INPROGRESS; |
| 1860 | |
| 1861 | pid_list_del(check->curpid); |
| 1862 | |
| 1863 | rv = 0; |
| 1864 | if (global.spread_checks > 0) { |
| 1865 | rv = srv_getinter(check) * global.spread_checks / 100; |
| 1866 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
| 1867 | } |
| 1868 | t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv)); |
| 1869 | } |
| 1870 | |
| 1871 | reschedule: |
| 1872 | while (tick_is_expired(t->expire, now_ms)) |
| 1873 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 1874 | return t; |
| 1875 | } |
| 1876 | |
| 1877 | /* |
| 1878 | * manages a server health-check that uses a connection. Returns |
| 1879 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 1880 | */ |
| 1881 | static struct task *process_chk_conn(struct task *t) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1882 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1883 | struct check *check = t->context; |
| 1884 | struct server *s = check->server; |
| 1885 | struct connection *conn = check->conn; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1886 | int rv; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1887 | int ret; |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1888 | int expired = tick_is_expired(t->expire, now_ms); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1889 | |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1890 | if (!(check->state & CHK_ST_INPROGRESS)) { |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1891 | /* no check currently running */ |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1892 | if (!expired) /* woke up too early */ |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1893 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1894 | |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 1895 | /* we don't send any health-checks when the proxy is |
| 1896 | * stopped, the server should not be checked or the check |
| 1897 | * is disabled. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1898 | */ |
Willy Tarreau | 0d924cc | 2013-12-11 21:26:24 +0100 | [diff] [blame] | 1899 | if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) || |
Willy Tarreau | 33a08db | 2013-12-11 21:03:31 +0100 | [diff] [blame] | 1900 | s->proxy->state == PR_STSTOPPED) |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1901 | goto reschedule; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1902 | |
| 1903 | /* we'll initiate a new check */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1904 | set_server_check_status(check, HCHK_STATUS_START, NULL); |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1905 | |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1906 | check->state |= CHK_ST_INPROGRESS; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1907 | check->bi->p = check->bi->data; |
| 1908 | check->bi->i = 0; |
| 1909 | check->bo->p = check->bo->data; |
| 1910 | check->bo->o = 0; |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1911 | |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1912 | ret = connect_chk(t); |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1913 | switch (ret) { |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1914 | case SN_ERR_UP: |
| 1915 | return t; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1916 | case SN_ERR_NONE: |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1917 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1918 | * to establish but only when timeout.check is set |
| 1919 | * as it may be to short for a full check otherwise |
| 1920 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1921 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1922 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1923 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 1924 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 1925 | t->expire = tick_first(t->expire, t_con); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1926 | } |
Willy Tarreau | 06559ac | 2013-12-05 01:53:08 +0100 | [diff] [blame] | 1927 | |
| 1928 | if (check->type) |
| 1929 | conn_data_want_recv(conn); /* prepare for reading a possible reply */ |
| 1930 | |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1931 | goto reschedule; |
| 1932 | |
| 1933 | case SN_ERR_SRVTO: /* ETIMEDOUT */ |
| 1934 | case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */ |
Willy Tarreau | 4bd07de | 2014-01-24 16:10:57 +0100 | [diff] [blame] | 1935 | conn->flags |= CO_FL_ERROR; |
| 1936 | chk_report_conn_err(conn, errno, 0); |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1937 | break; |
| 1938 | case SN_ERR_PRXCOND: |
| 1939 | case SN_ERR_RESOURCE: |
| 1940 | case SN_ERR_INTERNAL: |
Willy Tarreau | 4bd07de | 2014-01-24 16:10:57 +0100 | [diff] [blame] | 1941 | conn->flags |= CO_FL_ERROR; |
| 1942 | chk_report_conn_err(conn, 0, 0); |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1943 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1944 | } |
| 1945 | |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1946 | /* here, we have seen a synchronous error, no fd was allocated */ |
Willy Tarreau | 6b0a850 | 2012-11-23 08:51:32 +0100 | [diff] [blame] | 1947 | |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1948 | check->state &= ~CHK_ST_INPROGRESS; |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 1949 | check_notify_failure(check); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1950 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1951 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1952 | * to establish but only when timeout.check is set |
| 1953 | * as it may be to short for a full check otherwise |
| 1954 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1955 | while (tick_is_expired(t->expire, now_ms)) { |
| 1956 | int t_con; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1957 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1958 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1959 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1960 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1961 | if (s->proxy->timeout.check) |
| 1962 | t->expire = tick_first(t->expire, t_con); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1963 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1964 | } |
| 1965 | else { |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1966 | /* there was a test running. |
| 1967 | * First, let's check whether there was an uncaught error, |
| 1968 | * which can happen on connect timeout or error. |
| 1969 | */ |
Simon Horman | ccaabcd | 2014-06-20 12:29:47 +0900 | [diff] [blame] | 1970 | if (check->result == CHK_RES_UNKNOWN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1971 | /* good connection is enough for pure TCP check */ |
| 1972 | if ((conn->flags & CO_FL_CONNECTED) && !check->type) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1973 | if (check->use_ssl) |
| 1974 | set_server_check_status(check, HCHK_STATUS_L6OK, NULL); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1975 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1976 | set_server_check_status(check, HCHK_STATUS_L4OK, NULL); |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1977 | } |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1978 | else if ((conn->flags & CO_FL_ERROR) || expired) { |
| 1979 | chk_report_conn_err(conn, 0, expired); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1980 | } |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1981 | else |
| 1982 | goto out_wait; /* timeout not reached, wait again */ |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1983 | } |
| 1984 | |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1985 | /* check complete or aborted */ |
Willy Tarreau | 5ba04f6 | 2013-02-12 15:23:12 +0100 | [diff] [blame] | 1986 | if (conn->xprt) { |
| 1987 | /* The check was aborted and the connection was not yet closed. |
| 1988 | * This can happen upon timeout, or when an external event such |
| 1989 | * as a failed response coupled with "observe layer7" caused the |
| 1990 | * server state to be suddenly changed. |
| 1991 | */ |
Willy Tarreau | 46be2e5 | 2014-01-20 12:10:52 +0100 | [diff] [blame] | 1992 | conn_drain(conn); |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 1993 | conn_force_close(conn); |
Willy Tarreau | 5ba04f6 | 2013-02-12 15:23:12 +0100 | [diff] [blame] | 1994 | } |
| 1995 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 1996 | if (check->result == CHK_RES_FAILED) { |
| 1997 | /* a failure or timeout detected */ |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 1998 | check_notify_failure(check); |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 1999 | } |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 2000 | else if (check->result == CHK_RES_CONDPASS) { |
| 2001 | /* check is OK but asks for stopping mode */ |
| 2002 | check_notify_stopping(check); |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 2003 | } |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 2004 | else if (check->result == CHK_RES_PASSED) { |
| 2005 | /* a success was detected */ |
| 2006 | check_notify_success(check); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2007 | } |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 2008 | check->state &= ~CHK_ST_INPROGRESS; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2009 | |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 2010 | rv = 0; |
| 2011 | if (global.spread_checks > 0) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2012 | rv = srv_getinter(check) * global.spread_checks / 100; |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 2013 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2014 | } |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2015 | 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] | 2016 | } |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 2017 | |
| 2018 | reschedule: |
| 2019 | while (tick_is_expired(t->expire, now_ms)) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2020 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 2021 | out_wait: |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 2022 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2023 | } |
| 2024 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 2025 | /* |
| 2026 | * manages a server health-check. Returns |
| 2027 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 2028 | */ |
| 2029 | static struct task *process_chk(struct task *t) |
| 2030 | { |
| 2031 | struct check *check = t->context; |
| 2032 | |
| 2033 | if (check->type == PR_O2_EXT_CHK) |
| 2034 | return process_chk_proc(t); |
| 2035 | return process_chk_conn(t); |
| 2036 | } |
| 2037 | |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 2038 | static int start_check_task(struct check *check, int mininter, |
| 2039 | int nbcheck, int srvpos) |
| 2040 | { |
| 2041 | struct task *t; |
| 2042 | /* task for the check */ |
| 2043 | if ((t = task_new()) == NULL) { |
| 2044 | Alert("Starting [%s:%s] check: out of memory.\n", |
| 2045 | check->server->proxy->id, check->server->id); |
| 2046 | return 0; |
| 2047 | } |
| 2048 | |
| 2049 | check->task = t; |
| 2050 | t->process = process_chk; |
| 2051 | t->context = check; |
| 2052 | |
Willy Tarreau | 1746eec | 2014-04-25 10:46:47 +0200 | [diff] [blame] | 2053 | if (mininter < srv_getinter(check)) |
| 2054 | mininter = srv_getinter(check); |
| 2055 | |
| 2056 | if (global.max_spread_checks && mininter > global.max_spread_checks) |
| 2057 | mininter = global.max_spread_checks; |
| 2058 | |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 2059 | /* check this every ms */ |
Willy Tarreau | 1746eec | 2014-04-25 10:46:47 +0200 | [diff] [blame] | 2060 | t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck)); |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 2061 | check->start = now; |
| 2062 | task_queue(t); |
| 2063 | |
| 2064 | return 1; |
| 2065 | } |
| 2066 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2067 | /* |
| 2068 | * Start health-check. |
| 2069 | * Returns 0 if OK, -1 if error, and prints the error in this case. |
| 2070 | */ |
| 2071 | int start_checks() { |
| 2072 | |
| 2073 | struct proxy *px; |
| 2074 | struct server *s; |
| 2075 | struct task *t; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2076 | int nbcheck=0, mininter=0, srvpos=0; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2077 | |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 2078 | /* 1- count the checkers to run simultaneously. |
| 2079 | * We also determine the minimum interval among all of those which |
| 2080 | * have an interval larger than SRV_CHK_INTER_THRES. This interval |
| 2081 | * will be used to spread their start-up date. Those which have |
Jamie Gloudon | 801a0a3 | 2012-08-25 00:18:33 -0400 | [diff] [blame] | 2082 | * a shorter interval will start independently and will not dictate |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 2083 | * too short an interval for all others. |
| 2084 | */ |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2085 | for (px = proxy; px; px = px->next) { |
| 2086 | for (s = px->srv; s; s = s->next) { |
Willy Tarreau | e7b7348 | 2013-11-21 11:50:50 +0100 | [diff] [blame] | 2087 | if (s->slowstart) { |
| 2088 | if ((t = task_new()) == NULL) { |
| 2089 | Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 2090 | return -1; |
| 2091 | } |
| 2092 | /* We need a warmup task that will be called when the server |
| 2093 | * state switches from down to up. |
| 2094 | */ |
| 2095 | s->warmup = t; |
| 2096 | t->process = server_warmup; |
| 2097 | t->context = s; |
| 2098 | t->expire = TICK_ETERNITY; |
| 2099 | } |
| 2100 | |
Willy Tarreau | d8514a2 | 2013-12-11 21:10:14 +0100 | [diff] [blame] | 2101 | if (s->check.state & CHK_ST_CONFIGURED) { |
| 2102 | nbcheck++; |
| 2103 | if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) && |
| 2104 | (!mininter || mininter > srv_getinter(&s->check))) |
| 2105 | mininter = srv_getinter(&s->check); |
| 2106 | } |
Willy Tarreau | 15f3910 | 2013-12-11 20:41:18 +0100 | [diff] [blame] | 2107 | |
Willy Tarreau | d8514a2 | 2013-12-11 21:10:14 +0100 | [diff] [blame] | 2108 | if (s->agent.state & CHK_ST_CONFIGURED) { |
| 2109 | nbcheck++; |
| 2110 | if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) && |
| 2111 | (!mininter || mininter > srv_getinter(&s->agent))) |
| 2112 | mininter = srv_getinter(&s->agent); |
| 2113 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2114 | } |
| 2115 | } |
| 2116 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2117 | if (!nbcheck) |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2118 | return 0; |
| 2119 | |
| 2120 | srand((unsigned)time(NULL)); |
| 2121 | |
| 2122 | /* |
| 2123 | * 2- start them as far as possible from each others. For this, we will |
| 2124 | * start them after their interval set to the min interval divided by |
| 2125 | * the number of servers, weighted by the server's position in the list. |
| 2126 | */ |
| 2127 | for (px = proxy; px; px = px->next) { |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 2128 | if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) { |
| 2129 | if (init_pid_list()) { |
| 2130 | Alert("Starting [%s] check: out of memory.\n", px->id); |
| 2131 | return -1; |
| 2132 | } |
| 2133 | } |
| 2134 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2135 | for (s = px->srv; s; s = s->next) { |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2136 | /* A task for the main check */ |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 2137 | if (s->check.state & CHK_ST_CONFIGURED) { |
Cyril Bonté | 99c5bf5 | 2014-08-07 01:55:38 +0200 | [diff] [blame] | 2138 | if (s->check.type == PR_O2_EXT_CHK) { |
| 2139 | if (!prepare_external_check(&s->check)) |
| 2140 | return -1; |
| 2141 | } |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2142 | if (!start_check_task(&s->check, mininter, nbcheck, srvpos)) |
| 2143 | return -1; |
| 2144 | srvpos++; |
| 2145 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2146 | |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2147 | /* A task for a auxiliary agent check */ |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 2148 | if (s->agent.state & CHK_ST_CONFIGURED) { |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2149 | if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) { |
| 2150 | return -1; |
| 2151 | } |
| 2152 | srvpos++; |
| 2153 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2154 | } |
| 2155 | } |
| 2156 | return 0; |
| 2157 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2158 | |
| 2159 | /* |
Willy Tarreau | 5b3a202 | 2012-09-28 15:01:02 +0200 | [diff] [blame] | 2160 | * Perform content verification check on data in s->check.buffer buffer. |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2161 | * The buffer MUST be terminated by a null byte before calling this function. |
| 2162 | * Sets server status appropriately. The caller is responsible for ensuring |
| 2163 | * that the buffer contains at least 13 characters. If <done> is zero, we may |
| 2164 | * return 0 to indicate that data is required to decide of a match. |
| 2165 | */ |
| 2166 | static int httpchk_expect(struct server *s, int done) |
| 2167 | { |
| 2168 | static char status_msg[] = "HTTP status check returned code <000>"; |
| 2169 | char status_code[] = "000"; |
| 2170 | char *contentptr; |
| 2171 | int crlf; |
| 2172 | int ret; |
| 2173 | |
| 2174 | switch (s->proxy->options2 & PR_O2_EXP_TYPE) { |
| 2175 | case PR_O2_EXP_STS: |
| 2176 | case PR_O2_EXP_RSTS: |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 2177 | memcpy(status_code, s->check.bi->data + 9, 3); |
| 2178 | 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] | 2179 | |
| 2180 | if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS) |
| 2181 | ret = strncmp(s->proxy->expect_str, status_code, 3) == 0; |
| 2182 | else |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 2183 | ret = regex_exec(s->proxy->expect_regex, status_code); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2184 | |
| 2185 | /* we necessarily have the response, so there are no partial failures */ |
| 2186 | if (s->proxy->options2 & PR_O2_EXP_INV) |
| 2187 | ret = !ret; |
| 2188 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2189 | 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] | 2190 | break; |
| 2191 | |
| 2192 | case PR_O2_EXP_STR: |
| 2193 | case PR_O2_EXP_RSTR: |
| 2194 | /* very simple response parser: ignore CR and only count consecutive LFs, |
| 2195 | * stop with contentptr pointing to first char after the double CRLF or |
| 2196 | * to '\0' if crlf < 2. |
| 2197 | */ |
| 2198 | crlf = 0; |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 2199 | for (contentptr = s->check.bi->data; *contentptr; contentptr++) { |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2200 | if (crlf >= 2) |
| 2201 | break; |
| 2202 | if (*contentptr == '\r') |
| 2203 | continue; |
| 2204 | else if (*contentptr == '\n') |
| 2205 | crlf++; |
| 2206 | else |
| 2207 | crlf = 0; |
| 2208 | } |
| 2209 | |
| 2210 | /* Check that response contains a body... */ |
| 2211 | if (crlf < 2) { |
| 2212 | if (!done) |
| 2213 | return 0; |
| 2214 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2215 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2216 | "HTTP content check could not find a response body"); |
| 2217 | return 1; |
| 2218 | } |
| 2219 | |
| 2220 | /* Check that response body is not empty... */ |
| 2221 | if (*contentptr == '\0') { |
Willy Tarreau | a164fb5 | 2011-04-13 09:32:41 +0200 | [diff] [blame] | 2222 | if (!done) |
| 2223 | return 0; |
| 2224 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2225 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2226 | "HTTP content check found empty response body"); |
| 2227 | return 1; |
| 2228 | } |
| 2229 | |
| 2230 | /* Check the response content against the supplied string |
| 2231 | * or regex... */ |
| 2232 | if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR) |
| 2233 | ret = strstr(contentptr, s->proxy->expect_str) != NULL; |
| 2234 | else |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 2235 | ret = regex_exec(s->proxy->expect_regex, contentptr); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2236 | |
| 2237 | /* if we don't match, we may need to wait more */ |
| 2238 | if (!ret && !done) |
| 2239 | return 0; |
| 2240 | |
| 2241 | if (ret) { |
| 2242 | /* content matched */ |
| 2243 | if (s->proxy->options2 & PR_O2_EXP_INV) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2244 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2245 | "HTTP check matched unwanted content"); |
| 2246 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2247 | set_server_check_status(&s->check, HCHK_STATUS_L7OKD, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2248 | "HTTP content check matched"); |
| 2249 | } |
| 2250 | else { |
| 2251 | if (s->proxy->options2 & PR_O2_EXP_INV) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2252 | set_server_check_status(&s->check, HCHK_STATUS_L7OKD, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2253 | "HTTP check did not match unwanted content"); |
| 2254 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2255 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2256 | "HTTP content check did not match"); |
| 2257 | } |
| 2258 | break; |
| 2259 | } |
| 2260 | return 1; |
| 2261 | } |
| 2262 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2263 | /* |
| 2264 | * return the id of a step in a send/expect session |
| 2265 | */ |
| 2266 | static int tcpcheck_get_step_id(struct server *s) |
| 2267 | { |
| 2268 | struct tcpcheck_rule *cur = NULL, *next = NULL; |
| 2269 | int i = 0; |
| 2270 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2271 | cur = s->check.last_started_step; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2272 | |
| 2273 | /* no step => first step */ |
| 2274 | if (cur == NULL) |
| 2275 | return 1; |
| 2276 | |
| 2277 | /* increment i until current step */ |
| 2278 | list_for_each_entry(next, &s->proxy->tcpcheck_rules, list) { |
| 2279 | if (next->list.p == &cur->list) |
| 2280 | break; |
| 2281 | ++i; |
| 2282 | } |
| 2283 | |
| 2284 | return i; |
| 2285 | } |
| 2286 | |
| 2287 | static void tcpcheck_main(struct connection *conn) |
| 2288 | { |
| 2289 | char *contentptr; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2290 | struct list *head = NULL; |
| 2291 | struct tcpcheck_rule *cur = NULL; |
| 2292 | int done = 0, ret = 0; |
| 2293 | |
| 2294 | struct check *check = conn->owner; |
| 2295 | struct server *s = check->server; |
| 2296 | struct task *t = check->task; |
| 2297 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2298 | /* |
| 2299 | * don't do anything until the connection is established but if we're running |
| 2300 | * first step which must be a connect |
| 2301 | */ |
| 2302 | if (check->current_step && (!(conn->flags & CO_FL_CONNECTED))) { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2303 | /* update expire time, should be done by process_chk */ |
| 2304 | /* we allow up to min(inter, timeout.connect) for a connection |
| 2305 | * to establish but only when timeout.check is set |
| 2306 | * as it may be to short for a full check otherwise |
| 2307 | */ |
| 2308 | while (tick_is_expired(t->expire, now_ms)) { |
| 2309 | int t_con; |
| 2310 | |
| 2311 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 2312 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 2313 | |
| 2314 | if (s->proxy->timeout.check) |
| 2315 | t->expire = tick_first(t->expire, t_con); |
| 2316 | } |
| 2317 | return; |
| 2318 | } |
| 2319 | |
| 2320 | /* here, we know that the connection is established */ |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 2321 | if (check->result != CHK_RES_UNKNOWN) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2322 | goto out_end_tcpcheck; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2323 | |
| 2324 | /* head is be the first element of the double chained list */ |
| 2325 | head = &s->proxy->tcpcheck_rules; |
| 2326 | |
| 2327 | /* no step means first step |
| 2328 | * initialisation */ |
| 2329 | if (check->current_step == NULL) { |
| 2330 | check->bo->p = check->bo->data; |
| 2331 | check->bo->o = 0; |
| 2332 | check->bi->p = check->bi->data; |
| 2333 | check->bi->i = 0; |
| 2334 | cur = check->current_step = LIST_ELEM(head->n, struct tcpcheck_rule *, list); |
| 2335 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 2336 | if (s->proxy->timeout.check) |
| 2337 | t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check); |
| 2338 | } |
| 2339 | /* keep on processing step */ |
| 2340 | else { |
| 2341 | cur = check->current_step; |
| 2342 | } |
| 2343 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2344 | if (conn->flags & CO_FL_HANDSHAKE) |
| 2345 | return; |
| 2346 | |
| 2347 | /* It's only the rules which will enable send/recv */ |
| 2348 | __conn_data_stop_both(conn); |
| 2349 | |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2350 | while (1) { |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2351 | /* we have to try to flush the output buffer before reading, at the end, |
| 2352 | * or if we're about to send a string that does not fit in the remaining space. |
| 2353 | */ |
| 2354 | if (check->bo->o && |
| 2355 | (&cur->list == head || |
| 2356 | check->current_step->action != TCPCHK_ACT_SEND || |
| 2357 | check->current_step->string_len >= buffer_total_space(check->bo))) { |
| 2358 | |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 2359 | if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) { |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2360 | if (conn->flags & CO_FL_ERROR) { |
| 2361 | chk_report_conn_err(conn, errno, 0); |
| 2362 | __conn_data_stop_both(conn); |
| 2363 | goto out_end_tcpcheck; |
| 2364 | } |
| 2365 | goto out_need_io; |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2366 | } |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2367 | } |
| 2368 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2369 | /* did we reach the end ? If so, let's check that everything was sent */ |
| 2370 | if (&cur->list == head) { |
| 2371 | if (check->bo->o) |
| 2372 | goto out_need_io; |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2373 | break; |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2374 | } |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2375 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2376 | if (check->current_step->action == TCPCHK_ACT_CONNECT) { |
| 2377 | struct protocol *proto; |
| 2378 | struct xprt_ops *xprt; |
| 2379 | |
| 2380 | /* mark the step as started */ |
| 2381 | check->last_started_step = check->current_step; |
| 2382 | /* first, shut existing connection */ |
| 2383 | conn_force_close(conn); |
| 2384 | |
| 2385 | /* prepare new connection */ |
| 2386 | /* initialization */ |
| 2387 | conn_init(conn); |
| 2388 | conn_attach(conn, check, &check_conn_cb); |
| 2389 | conn->target = &s->obj_type; |
| 2390 | |
| 2391 | /* no client address */ |
| 2392 | clear_addr(&conn->addr.from); |
| 2393 | |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 2394 | if (is_addr(&s->check_common.addr)) { |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2395 | /* we'll connect to the check addr specified on the server */ |
| 2396 | conn->addr.to = s->check_common.addr; |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 2397 | proto = s->check_common.proto; |
| 2398 | } |
| 2399 | else { |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2400 | /* we'll connect to the addr on the server */ |
| 2401 | conn->addr.to = s->addr; |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 2402 | proto = s->proto; |
| 2403 | } |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2404 | |
| 2405 | /* port */ |
| 2406 | if (check->current_step->port) |
| 2407 | set_host_port(&conn->addr.to, check->current_step->port); |
| 2408 | else if (check->port) |
| 2409 | set_host_port(&conn->addr.to, check->port); |
| 2410 | |
| 2411 | #ifdef USE_OPENSSL |
| 2412 | if (check->current_step->conn_opts & TCPCHK_OPT_SSL) { |
| 2413 | xprt = &ssl_sock; |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2414 | } |
| 2415 | else { |
| 2416 | xprt = &raw_sock; |
| 2417 | } |
| 2418 | #else /* USE_OPENSSL */ |
| 2419 | xprt = &raw_sock; |
| 2420 | #endif /* USE_OPENSSL */ |
| 2421 | conn_prepare(conn, proto, xprt); |
| 2422 | |
| 2423 | ret = SN_ERR_INTERNAL; |
| 2424 | if (proto->connect) |
| 2425 | ret = proto->connect(conn, check->type, (check->type) ? 0 : 2); |
| 2426 | conn->flags |= CO_FL_WAKE_DATA; |
| 2427 | if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) { |
| 2428 | conn->send_proxy_ofs = 1; |
| 2429 | conn->flags |= CO_FL_SEND_PROXY; |
| 2430 | } |
| 2431 | |
| 2432 | /* It can return one of : |
| 2433 | * - SN_ERR_NONE if everything's OK |
| 2434 | * - SN_ERR_SRVTO if there are no more servers |
| 2435 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 2436 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 2437 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 2438 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 2439 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 2440 | * Note that we try to prevent the network stack from sending the ACK during the |
| 2441 | * connect() when a pure TCP check is used (without PROXY protocol). |
| 2442 | */ |
| 2443 | switch (ret) { |
| 2444 | case SN_ERR_NONE: |
| 2445 | /* we allow up to min(inter, timeout.connect) for a connection |
| 2446 | * to establish but only when timeout.check is set |
| 2447 | * as it may be to short for a full check otherwise |
| 2448 | */ |
| 2449 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 2450 | |
| 2451 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 2452 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 2453 | t->expire = tick_first(t->expire, t_con); |
| 2454 | } |
| 2455 | break; |
| 2456 | case SN_ERR_SRVTO: /* ETIMEDOUT */ |
| 2457 | case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */ |
| 2458 | chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s", |
| 2459 | tcpcheck_get_step_id(s), strerror(errno)); |
| 2460 | set_server_check_status(check, HCHK_STATUS_L4CON, trash.str); |
| 2461 | goto out_end_tcpcheck; |
| 2462 | case SN_ERR_PRXCOND: |
| 2463 | case SN_ERR_RESOURCE: |
| 2464 | case SN_ERR_INTERNAL: |
| 2465 | chunk_printf(&trash, "TCPCHK error establishing connection at step %d", |
| 2466 | tcpcheck_get_step_id(s)); |
| 2467 | set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str); |
| 2468 | goto out_end_tcpcheck; |
| 2469 | } |
| 2470 | |
| 2471 | /* allow next rule */ |
| 2472 | cur = (struct tcpcheck_rule *)cur->list.n; |
| 2473 | check->current_step = cur; |
| 2474 | |
| 2475 | /* don't do anything until the connection is established */ |
| 2476 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 2477 | /* update expire time, should be done by process_chk */ |
| 2478 | /* we allow up to min(inter, timeout.connect) for a connection |
| 2479 | * to establish but only when timeout.check is set |
| 2480 | * as it may be to short for a full check otherwise |
| 2481 | */ |
| 2482 | while (tick_is_expired(t->expire, now_ms)) { |
| 2483 | int t_con; |
| 2484 | |
| 2485 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 2486 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 2487 | |
| 2488 | if (s->proxy->timeout.check) |
| 2489 | t->expire = tick_first(t->expire, t_con); |
| 2490 | } |
| 2491 | return; |
| 2492 | } |
| 2493 | |
| 2494 | } /* end 'connect' */ |
| 2495 | else if (check->current_step->action == TCPCHK_ACT_SEND) { |
| 2496 | /* mark the step as started */ |
| 2497 | check->last_started_step = check->current_step; |
| 2498 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2499 | /* reset the read buffer */ |
| 2500 | if (*check->bi->data != '\0') { |
| 2501 | *check->bi->data = '\0'; |
| 2502 | check->bi->i = 0; |
| 2503 | } |
| 2504 | |
| 2505 | if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) { |
| 2506 | conn->flags |= CO_FL_ERROR; |
| 2507 | chk_report_conn_err(conn, 0, 0); |
| 2508 | goto out_end_tcpcheck; |
| 2509 | } |
| 2510 | |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2511 | if (check->current_step->string_len >= check->bo->size) { |
| 2512 | chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d", |
| 2513 | check->current_step->string_len, check->bo->size, |
| 2514 | tcpcheck_get_step_id(s)); |
| 2515 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2516 | goto out_end_tcpcheck; |
| 2517 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2518 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2519 | /* do not try to send if there is no space */ |
| 2520 | if (check->current_step->string_len >= buffer_total_space(check->bo)) |
| 2521 | continue; |
| 2522 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2523 | bo_putblk(check->bo, check->current_step->string, check->current_step->string_len); |
| 2524 | *check->bo->p = '\0'; /* to make gdb output easier to read */ |
| 2525 | |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2526 | /* go to next rule and try to send */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2527 | cur = (struct tcpcheck_rule *)cur->list.n; |
| 2528 | check->current_step = cur; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2529 | } /* end 'send' */ |
Willy Tarreau | 98aec9f | 2013-12-06 16:16:41 +0100 | [diff] [blame] | 2530 | else if (check->current_step->action == TCPCHK_ACT_EXPECT) { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 2531 | if (unlikely(check->result == CHK_RES_FAILED)) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2532 | goto out_end_tcpcheck; |
| 2533 | |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 2534 | if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) { |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2535 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) { |
| 2536 | done = 1; |
| 2537 | if ((conn->flags & CO_FL_ERROR) && !check->bi->i) { |
| 2538 | /* Report network errors only if we got no other data. Otherwise |
| 2539 | * we'll let the upper layers decide whether the response is OK |
| 2540 | * or not. It is very common that an RST sent by the server is |
| 2541 | * reported as an error just after the last data chunk. |
| 2542 | */ |
| 2543 | chk_report_conn_err(conn, errno, 0); |
| 2544 | goto out_end_tcpcheck; |
| 2545 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2546 | } |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2547 | else |
| 2548 | goto out_need_io; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2549 | } |
| 2550 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2551 | /* mark the step as started */ |
| 2552 | check->last_started_step = check->current_step; |
| 2553 | |
| 2554 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2555 | /* Intermediate or complete response received. |
| 2556 | * Terminate string in check->bi->data buffer. |
| 2557 | */ |
| 2558 | if (check->bi->i < check->bi->size) { |
| 2559 | check->bi->data[check->bi->i] = '\0'; |
| 2560 | } |
| 2561 | else { |
| 2562 | check->bi->data[check->bi->i - 1] = '\0'; |
| 2563 | done = 1; /* buffer full, don't wait for more data */ |
| 2564 | } |
| 2565 | |
| 2566 | contentptr = check->bi->data; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2567 | |
| 2568 | /* Check that response body is not empty... */ |
Willy Tarreau | ec6b012 | 2014-05-13 17:57:29 +0200 | [diff] [blame] | 2569 | if (!check->bi->i) { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2570 | if (!done) |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2571 | continue; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2572 | |
| 2573 | /* empty response */ |
| 2574 | chunk_printf(&trash, "TCPCHK got an empty response at step %d", |
| 2575 | tcpcheck_get_step_id(s)); |
| 2576 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2577 | |
| 2578 | goto out_end_tcpcheck; |
| 2579 | } |
| 2580 | |
| 2581 | if (!done && (cur->string != NULL) && (check->bi->i < cur->string_len) ) |
Willy Tarreau | a970c28 | 2013-12-06 12:47:19 +0100 | [diff] [blame] | 2582 | continue; /* try to read more */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2583 | |
Willy Tarreau | a970c28 | 2013-12-06 12:47:19 +0100 | [diff] [blame] | 2584 | tcpcheck_expect: |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2585 | if (cur->string != NULL) |
Willy Tarreau | ec6b012 | 2014-05-13 17:57:29 +0200 | [diff] [blame] | 2586 | 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] | 2587 | else if (cur->expect_regex != NULL) |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 2588 | ret = regex_exec(cur->expect_regex, contentptr); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2589 | |
| 2590 | if (!ret && !done) |
Willy Tarreau | a970c28 | 2013-12-06 12:47:19 +0100 | [diff] [blame] | 2591 | continue; /* try to read more */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2592 | |
| 2593 | /* matched */ |
| 2594 | if (ret) { |
| 2595 | /* matched but we did not want to => ERROR */ |
| 2596 | if (cur->inverse) { |
| 2597 | /* we were looking for a string */ |
| 2598 | if (cur->string != NULL) { |
| 2599 | chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d", |
| 2600 | cur->string, tcpcheck_get_step_id(s)); |
| 2601 | } |
| 2602 | else { |
| 2603 | /* we were looking for a regex */ |
| 2604 | chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d", |
| 2605 | tcpcheck_get_step_id(s)); |
| 2606 | } |
| 2607 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2608 | goto out_end_tcpcheck; |
| 2609 | } |
| 2610 | /* matched and was supposed to => OK, next step */ |
| 2611 | else { |
| 2612 | cur = (struct tcpcheck_rule*)cur->list.n; |
| 2613 | check->current_step = cur; |
Willy Tarreau | 98aec9f | 2013-12-06 16:16:41 +0100 | [diff] [blame] | 2614 | if (check->current_step->action == TCPCHK_ACT_EXPECT) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2615 | goto tcpcheck_expect; |
| 2616 | __conn_data_stop_recv(conn); |
| 2617 | } |
| 2618 | } |
| 2619 | else { |
| 2620 | /* not matched */ |
| 2621 | /* not matched and was not supposed to => OK, next step */ |
| 2622 | if (cur->inverse) { |
| 2623 | cur = (struct tcpcheck_rule*)cur->list.n; |
| 2624 | check->current_step = cur; |
Willy Tarreau | 98aec9f | 2013-12-06 16:16:41 +0100 | [diff] [blame] | 2625 | if (check->current_step->action == TCPCHK_ACT_EXPECT) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2626 | goto tcpcheck_expect; |
| 2627 | __conn_data_stop_recv(conn); |
| 2628 | } |
| 2629 | /* not matched but was supposed to => ERROR */ |
| 2630 | else { |
| 2631 | /* we were looking for a string */ |
| 2632 | if (cur->string != NULL) { |
| 2633 | chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d", |
| 2634 | cur->string, tcpcheck_get_step_id(s)); |
| 2635 | } |
| 2636 | else { |
| 2637 | /* we were looking for a regex */ |
| 2638 | chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d", |
| 2639 | tcpcheck_get_step_id(s)); |
| 2640 | } |
| 2641 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2642 | goto out_end_tcpcheck; |
| 2643 | } |
| 2644 | } |
| 2645 | } /* end expect */ |
| 2646 | } /* end loop over double chained step list */ |
| 2647 | |
| 2648 | set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)"); |
| 2649 | goto out_end_tcpcheck; |
| 2650 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2651 | out_need_io: |
| 2652 | if (check->bo->o) |
| 2653 | __conn_data_want_send(conn); |
| 2654 | |
| 2655 | if (check->current_step->action == TCPCHK_ACT_EXPECT) |
| 2656 | __conn_data_want_recv(conn); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2657 | return; |
| 2658 | |
| 2659 | out_end_tcpcheck: |
| 2660 | /* collect possible new errors */ |
| 2661 | if (conn->flags & CO_FL_ERROR) |
| 2662 | chk_report_conn_err(conn, 0, 0); |
| 2663 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2664 | /* cleanup before leaving */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2665 | check->current_step = NULL; |
| 2666 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 2667 | if (check->result == CHK_RES_FAILED) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2668 | conn->flags |= CO_FL_ERROR; |
| 2669 | |
| 2670 | __conn_data_stop_both(conn); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2671 | |
| 2672 | return; |
| 2673 | } |
| 2674 | |
| 2675 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2676 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2677 | * Local variables: |
| 2678 | * c-indent-level: 8 |
| 2679 | * c-basic-offset: 8 |
| 2680 | * End: |
| 2681 | */ |