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 | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 589 | int step; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 590 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 591 | if (check->result != CHK_RES_UNKNOWN) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 592 | return; |
| 593 | |
| 594 | errno = errno_bck; |
| 595 | if (!errno || errno == EAGAIN) |
| 596 | retrieve_errno_from_socket(conn); |
| 597 | |
| 598 | if (!(conn->flags & CO_FL_ERROR) && !expired) |
| 599 | return; |
| 600 | |
| 601 | /* we'll try to build a meaningful error message depending on the |
| 602 | * context of the error possibly present in conn->err_code, and the |
| 603 | * socket error possibly collected above. This is useful to know the |
| 604 | * exact step of the L6 layer (eg: SSL handshake). |
| 605 | */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 606 | chk = get_trash_chunk(); |
| 607 | |
| 608 | if (check->type == PR_O2_TCPCHK_CHK) { |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 609 | step = tcpcheck_get_step_id(check->server); |
| 610 | if (!step) |
| 611 | chunk_printf(chk, " at initial connection step of tcp-check"); |
| 612 | else { |
| 613 | chunk_printf(chk, " at step %d of tcp-check", step); |
| 614 | /* we were looking for a string */ |
| 615 | if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_CONNECT) { |
| 616 | if (check->last_started_step->port) |
| 617 | chunk_appendf(chk, " (connect port %d)" ,check->last_started_step->port); |
| 618 | else |
| 619 | chunk_appendf(chk, " (connect)"); |
| 620 | } |
| 621 | else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_EXPECT) { |
| 622 | if (check->last_started_step->string) |
| 623 | chunk_appendf(chk, " (string '%s')", check->last_started_step->string); |
| 624 | else if (check->last_started_step->expect_regex) |
| 625 | chunk_appendf(chk, " (expect regex)"); |
| 626 | } |
| 627 | else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_SEND) { |
| 628 | chunk_appendf(chk, " (send)"); |
| 629 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 633 | if (conn->err_code) { |
| 634 | if (errno && errno != EAGAIN) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 635 | 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] | 636 | else |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 637 | chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 638 | err_msg = trash.str; |
| 639 | } |
| 640 | else { |
| 641 | if (errno && errno != EAGAIN) { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 642 | chunk_printf(&trash, "%s%s", strerror(errno), chk->str); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 643 | err_msg = trash.str; |
| 644 | } |
| 645 | else { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 646 | err_msg = chk->str; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
| 650 | if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) { |
| 651 | /* L4 not established (yet) */ |
| 652 | if (conn->flags & CO_FL_ERROR) |
| 653 | set_server_check_status(check, HCHK_STATUS_L4CON, err_msg); |
| 654 | else if (expired) |
| 655 | set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg); |
| 656 | } |
| 657 | else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) { |
| 658 | /* L6 not established (yet) */ |
| 659 | if (conn->flags & CO_FL_ERROR) |
| 660 | set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg); |
| 661 | else if (expired) |
| 662 | set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg); |
| 663 | } |
| 664 | else if (conn->flags & CO_FL_ERROR) { |
| 665 | /* I/O error after connection was established and before we could diagnose */ |
| 666 | set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg); |
| 667 | } |
| 668 | else if (expired) { |
| 669 | /* connection established but expired check */ |
| 670 | if (check->type == PR_O2_SSL3_CHK) |
| 671 | set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg); |
| 672 | else /* HTTP, SMTP, ... */ |
| 673 | set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg); |
| 674 | } |
| 675 | |
| 676 | return; |
| 677 | } |
| 678 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 679 | /* |
| 680 | * This function is used only for server health-checks. It handles |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 681 | * the connection acknowledgement. If the proxy requires L7 health-checks, |
| 682 | * 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] | 683 | * to set check->status, check->duration and check->result. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 684 | */ |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 685 | static void event_srv_chk_w(struct connection *conn) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 686 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 687 | struct check *check = conn->owner; |
| 688 | struct server *s = check->server; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 689 | struct task *t = check->task; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 690 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 691 | if (unlikely(check->result == CHK_RES_FAILED)) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 692 | goto out_wakeup; |
| 693 | |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 694 | if (conn->flags & CO_FL_HANDSHAKE) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 695 | return; |
| 696 | |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 697 | if (retrieve_errno_from_socket(conn)) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 698 | chk_report_conn_err(conn, errno, 0); |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 699 | __conn_data_stop_both(conn); |
| 700 | goto out_wakeup; |
| 701 | } |
Krzysztof Piotr Oledzki | 6492db5 | 2010-01-02 22:03:01 +0100 | [diff] [blame] | 702 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 703 | 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] | 704 | /* if the output is closed, we can't do anything */ |
| 705 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 706 | chk_report_conn_err(conn, 0, 0); |
Willy Tarreau | 20a1834 | 2013-12-05 00:31:46 +0100 | [diff] [blame] | 707 | goto out_wakeup; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 708 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 709 | |
Willy Tarreau | 06559ac | 2013-12-05 01:53:08 +0100 | [diff] [blame] | 710 | /* here, we know that the connection is established. That's enough for |
| 711 | * a pure TCP check. |
| 712 | */ |
| 713 | if (!check->type) |
| 714 | goto out_wakeup; |
| 715 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 716 | if (check->type == PR_O2_TCPCHK_CHK) { |
| 717 | tcpcheck_main(conn); |
| 718 | return; |
| 719 | } |
| 720 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 721 | if (check->bo->o) { |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 722 | conn->xprt->snd_buf(conn, check->bo, 0); |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 723 | if (conn->flags & CO_FL_ERROR) { |
| 724 | chk_report_conn_err(conn, errno, 0); |
| 725 | __conn_data_stop_both(conn); |
| 726 | goto out_wakeup; |
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 | if (check->bo->o) |
| 729 | return; |
| 730 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 731 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 732 | /* full request sent, we allow up to <timeout.check> if nonzero for a response */ |
| 733 | if (s->proxy->timeout.check) { |
| 734 | t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check); |
| 735 | task_queue(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 736 | } |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 737 | goto out_nowake; |
| 738 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 739 | out_wakeup: |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 740 | task_wakeup(t, TASK_WOKEN_IO); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 741 | out_nowake: |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 742 | __conn_data_stop_send(conn); /* nothing more to write */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 743 | } |
| 744 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 745 | /* |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 746 | * 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] | 747 | * 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] | 748 | * set_server_check_status() to update check->status, check->duration |
| 749 | * and check->result. |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 750 | |
| 751 | * The set_server_check_status function is called with HCHK_STATUS_L7OKD if |
| 752 | * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server |
| 753 | * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in |
| 754 | * response to an SSL HELLO (the principle is that this is enough to |
| 755 | * distinguish between an SSL server and a pure TCP relay). All other cases will |
| 756 | * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP, |
| 757 | * etc. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 758 | */ |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 759 | static void event_srv_chk_r(struct connection *conn) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 760 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 761 | struct check *check = conn->owner; |
| 762 | struct server *s = check->server; |
| 763 | struct task *t = check->task; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 764 | char *desc; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 765 | int done; |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 766 | unsigned short msglen; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 767 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 768 | if (unlikely(check->result == CHK_RES_FAILED)) |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 769 | goto out_wakeup; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 770 | |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 771 | if (conn->flags & CO_FL_HANDSHAKE) |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 772 | return; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 773 | |
| 774 | if (check->type == PR_O2_TCPCHK_CHK) { |
| 775 | tcpcheck_main(conn); |
| 776 | return; |
| 777 | } |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 778 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 779 | /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available |
| 780 | * but the connection was closed on the remote end. Fortunately, recv still |
| 781 | * works correctly and we don't need to do the getsockopt() on linux. |
| 782 | */ |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 783 | |
| 784 | /* Set buffer to point to the end of the data already read, and check |
| 785 | * that there is free space remaining. If the buffer is full, proceed |
| 786 | * with running the checks without attempting another socket read. |
| 787 | */ |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 788 | |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 789 | done = 0; |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 790 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 791 | conn->xprt->rcv_buf(conn, check->bi, check->bi->size); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 792 | 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] | 793 | done = 1; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 794 | if ((conn->flags & CO_FL_ERROR) && !check->bi->i) { |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 795 | /* Report network errors only if we got no other data. Otherwise |
| 796 | * we'll let the upper layers decide whether the response is OK |
| 797 | * or not. It is very common that an RST sent by the server is |
| 798 | * reported as an error just after the last data chunk. |
| 799 | */ |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 800 | chk_report_conn_err(conn, errno, 0); |
Willy Tarreau | c1a0796 | 2010-03-16 20:55:43 +0100 | [diff] [blame] | 801 | goto out_wakeup; |
| 802 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 803 | } |
| 804 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 805 | |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 806 | /* Intermediate or complete response received. |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 807 | * Terminate string in check->bi->data buffer. |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 808 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 809 | if (check->bi->i < check->bi->size) |
| 810 | check->bi->data[check->bi->i] = '\0'; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 811 | else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 812 | check->bi->data[check->bi->i - 1] = '\0'; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 813 | done = 1; /* buffer full, don't wait for more data */ |
| 814 | } |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 815 | |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 816 | /* Run the checks... */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 817 | switch (check->type) { |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 818 | case PR_O2_HTTP_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 819 | if (!done && check->bi->i < strlen("HTTP/1.0 000\r")) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 820 | goto wait_more_data; |
| 821 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 822 | /* Check if the server speaks HTTP 1.X */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 823 | if ((check->bi->i < strlen("HTTP/1.0 000\r")) || |
| 824 | (memcmp(check->bi->data, "HTTP/1.", 7) != 0 || |
| 825 | (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) || |
| 826 | !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) || |
| 827 | !isdigit((unsigned char) *(check->bi->data + 11))) { |
| 828 | cut_crlf(check->bi->data); |
| 829 | set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 830 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 831 | goto out_wakeup; |
| 832 | } |
| 833 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 834 | check->code = str2uic(check->bi->data + 9); |
| 835 | desc = ltrim(check->bi->data + 12, ' '); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 836 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 837 | if ((s->proxy->options & PR_O_DISABLE404) && |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 838 | (s->state != SRV_ST_STOPPED) && (check->code == 404)) { |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 839 | /* 404 may be accepted as "stopping" only if the server was up */ |
| 840 | cut_crlf(desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 841 | set_server_check_status(check, HCHK_STATUS_L7OKCD, desc); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 842 | } |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 843 | else if (s->proxy->options2 & PR_O2_EXP_TYPE) { |
| 844 | /* Run content verification check... We know we have at least 13 chars */ |
| 845 | if (!httpchk_expect(s, done)) |
| 846 | goto wait_more_data; |
| 847 | } |
| 848 | /* check the reply : HTTP/1.X 2xx and 3xx are OK */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 849 | else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') { |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 850 | cut_crlf(desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 851 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 852 | } |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 853 | else { |
| 854 | cut_crlf(desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 855 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 856 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 857 | break; |
| 858 | |
| 859 | case PR_O2_SSL3_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 860 | if (!done && check->bi->i < 5) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 861 | goto wait_more_data; |
| 862 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 863 | /* Check for SSLv3 alert or handshake */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 864 | if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16)) |
| 865 | set_server_check_status(check, HCHK_STATUS_L6OK, NULL); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 866 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 867 | set_server_check_status(check, HCHK_STATUS_L6RSP, NULL); |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 868 | break; |
| 869 | |
| 870 | case PR_O2_SMTP_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 871 | if (!done && check->bi->i < strlen("000\r")) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 872 | goto wait_more_data; |
| 873 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 874 | /* Check if the server speaks SMTP */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 875 | if ((check->bi->i < strlen("000\r")) || |
| 876 | (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') || |
| 877 | !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) || |
| 878 | !isdigit((unsigned char) *(check->bi->data + 2))) { |
| 879 | cut_crlf(check->bi->data); |
| 880 | set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 881 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 882 | goto out_wakeup; |
| 883 | } |
| 884 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 885 | check->code = str2uic(check->bi->data); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 886 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 887 | desc = ltrim(check->bi->data + 3, ' '); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 888 | cut_crlf(desc); |
| 889 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 890 | /* Check for SMTP code 2xx (should be 250) */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 891 | if (*check->bi->data == '2') |
| 892 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 893 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 894 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 895 | break; |
| 896 | |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 897 | case PR_O2_LB_AGENT_CHK: { |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 898 | int status = HCHK_STATUS_CHECKED; |
| 899 | const char *hs = NULL; /* health status */ |
| 900 | const char *as = NULL; /* admin status */ |
| 901 | const char *ps = NULL; /* performance status */ |
| 902 | const char *err = NULL; /* first error to report */ |
| 903 | const char *wrn = NULL; /* first warning to report */ |
| 904 | char *cmd, *p; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 905 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 906 | /* We're getting an agent check response. The agent could |
| 907 | * have been disabled in the mean time with a long check |
| 908 | * still pending. It is important that we ignore the whole |
| 909 | * response. |
| 910 | */ |
| 911 | if (!(check->server->agent.state & CHK_ST_ENABLED)) |
| 912 | break; |
| 913 | |
| 914 | /* The agent supports strings made of a single line ended by the |
| 915 | * first CR ('\r') or LF ('\n'). This line is composed of words |
| 916 | * delimited by spaces (' '), tabs ('\t'), or commas (','). The |
| 917 | * line may optionally contained a description of a state change |
| 918 | * after a sharp ('#'), which is only considered if a health state |
| 919 | * is announced. |
| 920 | * |
| 921 | * Words may be composed of : |
| 922 | * - a numeric weight suffixed by the percent character ('%'). |
| 923 | * - a health status among "up", "down", "stopped", and "fail". |
| 924 | * - an admin status among "ready", "drain", "maint". |
| 925 | * |
| 926 | * These words may appear in any order. If multiple words of the |
| 927 | * same category appear, the last one wins. |
| 928 | */ |
| 929 | |
Willy Tarreau | 9809b78 | 2013-12-11 21:40:11 +0100 | [diff] [blame] | 930 | p = check->bi->data; |
| 931 | while (*p && *p != '\n' && *p != '\r') |
| 932 | p++; |
| 933 | |
| 934 | if (!*p) { |
| 935 | if (!done) |
| 936 | goto wait_more_data; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 937 | |
Willy Tarreau | 9809b78 | 2013-12-11 21:40:11 +0100 | [diff] [blame] | 938 | /* at least inform the admin that the agent is mis-behaving */ |
| 939 | set_server_check_status(check, check->status, "Ignoring incomplete line from agent"); |
| 940 | break; |
| 941 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 942 | |
Willy Tarreau | 9809b78 | 2013-12-11 21:40:11 +0100 | [diff] [blame] | 943 | *p = 0; |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 944 | cmd = check->bi->data; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 945 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 946 | while (*cmd) { |
| 947 | /* look for next word */ |
| 948 | if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') { |
| 949 | cmd++; |
| 950 | continue; |
| 951 | } |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 952 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 953 | if (*cmd == '#') { |
| 954 | /* this is the beginning of a health status description, |
| 955 | * skip the sharp and blanks. |
| 956 | */ |
| 957 | cmd++; |
| 958 | while (*cmd == '\t' || *cmd == ' ') |
| 959 | cmd++; |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 960 | break; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 961 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 962 | |
| 963 | /* find the end of the word so that we have a null-terminated |
| 964 | * word between <cmd> and <p>. |
| 965 | */ |
| 966 | p = cmd + 1; |
| 967 | while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',') |
| 968 | p++; |
| 969 | if (*p) |
| 970 | *p++ = 0; |
| 971 | |
| 972 | /* first, health statuses */ |
| 973 | if (strcasecmp(cmd, "up") == 0) { |
| 974 | check->health = check->rise + check->fall - 1; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 975 | status = HCHK_STATUS_L7OKD; |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 976 | hs = cmd; |
| 977 | } |
| 978 | else if (strcasecmp(cmd, "down") == 0) { |
| 979 | check->health = 0; |
| 980 | status = HCHK_STATUS_L7STS; |
| 981 | hs = cmd; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 982 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 983 | else if (strcasecmp(cmd, "stopped") == 0) { |
| 984 | check->health = 0; |
| 985 | status = HCHK_STATUS_L7STS; |
| 986 | hs = cmd; |
| 987 | } |
| 988 | else if (strcasecmp(cmd, "fail") == 0) { |
| 989 | check->health = 0; |
| 990 | status = HCHK_STATUS_L7STS; |
| 991 | hs = cmd; |
| 992 | } |
| 993 | /* admin statuses */ |
| 994 | else if (strcasecmp(cmd, "ready") == 0) { |
| 995 | as = cmd; |
| 996 | } |
| 997 | else if (strcasecmp(cmd, "drain") == 0) { |
| 998 | as = cmd; |
| 999 | } |
| 1000 | else if (strcasecmp(cmd, "maint") == 0) { |
| 1001 | as = cmd; |
| 1002 | } |
| 1003 | /* else try to parse a weight here and keep the last one */ |
| 1004 | else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) { |
| 1005 | ps = cmd; |
| 1006 | } |
| 1007 | else { |
| 1008 | /* keep a copy of the first error */ |
| 1009 | if (!err) |
| 1010 | err = cmd; |
| 1011 | } |
| 1012 | /* skip to next word */ |
| 1013 | cmd = p; |
| 1014 | } |
| 1015 | /* here, cmd points either to \0 or to the beginning of a |
| 1016 | * description. Skip possible leading spaces. |
| 1017 | */ |
| 1018 | while (*cmd == ' ' || *cmd == '\n') |
| 1019 | cmd++; |
| 1020 | |
| 1021 | /* First, update the admin status so that we avoid sending other |
| 1022 | * possibly useless warnings and can also update the health if |
| 1023 | * present after going back up. |
| 1024 | */ |
| 1025 | if (as) { |
| 1026 | if (strcasecmp(as, "drain") == 0) |
| 1027 | srv_adm_set_drain(check->server); |
| 1028 | else if (strcasecmp(as, "maint") == 0) |
| 1029 | srv_adm_set_maint(check->server); |
| 1030 | else |
| 1031 | srv_adm_set_ready(check->server); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1032 | } |
| 1033 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1034 | /* now change weights */ |
| 1035 | if (ps) { |
| 1036 | const char *msg; |
| 1037 | |
| 1038 | msg = server_parse_weight_change_request(s, ps); |
| 1039 | if (!wrn || !*wrn) |
| 1040 | wrn = msg; |
| 1041 | } |
| 1042 | |
| 1043 | /* and finally health status */ |
| 1044 | if (hs) { |
| 1045 | /* We'll report some of the warnings and errors we have |
| 1046 | * here. Down reports are critical, we leave them untouched. |
| 1047 | * Lack of report, or report of 'UP' leaves the room for |
| 1048 | * ERR first, then WARN. |
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 | const char *msg = cmd; |
| 1051 | struct chunk *t; |
| 1052 | |
| 1053 | if (!*msg || status == HCHK_STATUS_L7OKD) { |
| 1054 | if (err && *err) |
| 1055 | msg = err; |
| 1056 | else if (wrn && *wrn) |
| 1057 | msg = wrn; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1058 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1059 | |
| 1060 | t = get_trash_chunk(); |
| 1061 | chunk_printf(t, "via agent : %s%s%s%s", |
| 1062 | hs, *msg ? " (" : "", |
| 1063 | msg, *msg ? ")" : ""); |
| 1064 | |
| 1065 | set_server_check_status(check, status, t->str); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1066 | } |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1067 | else if (err && *err) { |
| 1068 | /* No status change but we'd like to report something odd. |
| 1069 | * Just report the current state and copy the message. |
| 1070 | */ |
| 1071 | chunk_printf(&trash, "agent reports an error : %s", err); |
| 1072 | set_server_check_status(check, status/*check->status*/, trash.str); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1073 | |
Willy Tarreau | 81f5d94 | 2013-12-09 20:51:51 +0100 | [diff] [blame] | 1074 | } |
| 1075 | else if (wrn && *wrn) { |
| 1076 | /* No status change but we'd like to report something odd. |
| 1077 | * Just report the current state and copy the message. |
| 1078 | */ |
| 1079 | chunk_printf(&trash, "agent warns : %s", wrn); |
| 1080 | set_server_check_status(check, status/*check->status*/, trash.str); |
| 1081 | } |
| 1082 | else |
| 1083 | set_server_check_status(check, status, NULL); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1084 | break; |
| 1085 | } |
| 1086 | |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1087 | case PR_O2_PGSQL_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1088 | if (!done && check->bi->i < 9) |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1089 | goto wait_more_data; |
| 1090 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1091 | if (check->bi->data[0] == 'R') { |
| 1092 | set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok"); |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1093 | } |
| 1094 | else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1095 | if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0)) |
| 1096 | desc = &check->bi->data[6]; |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1097 | else |
| 1098 | desc = "PostgreSQL unknown error"; |
| 1099 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1100 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1101 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1102 | break; |
| 1103 | |
| 1104 | case PR_O2_REDIS_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1105 | if (!done && check->bi->i < 7) |
Hervé COMMOWICK | ec032d6 | 2011-08-05 16:23:48 +0200 | [diff] [blame] | 1106 | goto wait_more_data; |
| 1107 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1108 | if (strcmp(check->bi->data, "+PONG\r\n") == 0) { |
| 1109 | set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok"); |
Hervé COMMOWICK | ec032d6 | 2011-08-05 16:23:48 +0200 | [diff] [blame] | 1110 | } |
| 1111 | else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1112 | set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data); |
Hervé COMMOWICK | ec032d6 | 2011-08-05 16:23:48 +0200 | [diff] [blame] | 1113 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1114 | break; |
| 1115 | |
| 1116 | case PR_O2_MYSQL_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1117 | if (!done && check->bi->i < 5) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1118 | goto wait_more_data; |
| 1119 | |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1120 | if (s->proxy->check_len == 0) { // old mode |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1121 | if (*(check->bi->data + 4) != '\xff') { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1122 | /* We set the MySQL Version in description for information purpose |
| 1123 | * FIXME : it can be cool to use MySQL Version for other purpose, |
| 1124 | * like mark as down old MySQL server. |
| 1125 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1126 | if (check->bi->i > 51) { |
| 1127 | desc = ltrim(check->bi->data + 5, ' '); |
| 1128 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1129 | } |
| 1130 | else { |
| 1131 | if (!done) |
| 1132 | goto wait_more_data; |
| 1133 | /* it seems we have a OK packet but without a valid length, |
| 1134 | * it must be a protocol error |
| 1135 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1136 | set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1137 | } |
Hervé COMMOWICK | 698ae00 | 2010-01-12 09:25:13 +0100 | [diff] [blame] | 1138 | } |
| 1139 | else { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1140 | /* An error message is attached in the Error packet */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1141 | desc = ltrim(check->bi->data + 7, ' '); |
| 1142 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1143 | } |
| 1144 | } else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1145 | unsigned int first_packet_len = ((unsigned int) *check->bi->data) + |
| 1146 | (((unsigned int) *(check->bi->data + 1)) << 8) + |
| 1147 | (((unsigned int) *(check->bi->data + 2)) << 16); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1148 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1149 | if (check->bi->i == first_packet_len + 4) { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1150 | /* MySQL Error packet always begin with field_count = 0xff */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1151 | if (*(check->bi->data + 4) != '\xff') { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1152 | /* We have only one MySQL packet and it is a Handshake Initialization packet |
| 1153 | * but we need to have a second packet to know if it is alright |
| 1154 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1155 | if (!done && check->bi->i < first_packet_len + 5) |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1156 | goto wait_more_data; |
| 1157 | } |
| 1158 | else { |
| 1159 | /* We have only one packet and it is an Error packet, |
| 1160 | * an error message is attached, so we can display it |
| 1161 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1162 | desc = &check->bi->data[7]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1163 | //Warning("onlyoneERR: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1164 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1165 | } |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1166 | } else if (check->bi->i > first_packet_len + 4) { |
| 1167 | unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) + |
| 1168 | (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) + |
| 1169 | (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1170 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1171 | if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1172 | /* We have 2 packets and that's good */ |
| 1173 | /* Check if the second packet is a MySQL Error packet or not */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1174 | if (*(check->bi->data + first_packet_len + 8) != '\xff') { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1175 | /* No error packet */ |
| 1176 | /* We set the MySQL Version in description for information purpose */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1177 | desc = &check->bi->data[5]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1178 | //Warning("2packetOK: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1179 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1180 | } |
| 1181 | else { |
| 1182 | /* An error message is attached in the Error packet |
| 1183 | * so we can display it ! :) |
| 1184 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1185 | desc = &check->bi->data[first_packet_len+11]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1186 | //Warning("2packetERR: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1187 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | } |
| 1191 | else { |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1192 | if (!done) |
| 1193 | goto wait_more_data; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1194 | /* 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] | 1195 | * it must be a protocol error |
| 1196 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1197 | desc = &check->bi->data[5]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1198 | //Warning("protoerr: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1199 | set_server_check_status(check, HCHK_STATUS_L7RSP, desc); |
Hervé COMMOWICK | 698ae00 | 2010-01-12 09:25:13 +0100 | [diff] [blame] | 1200 | } |
| 1201 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1202 | break; |
| 1203 | |
| 1204 | case PR_O2_LDAP_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1205 | if (!done && check->bi->i < 14) |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1206 | goto wait_more_data; |
| 1207 | |
| 1208 | /* Check if the server speaks LDAP (ASN.1/BER) |
| 1209 | * http://en.wikipedia.org/wiki/Basic_Encoding_Rules |
| 1210 | * http://tools.ietf.org/html/rfc4511 |
| 1211 | */ |
| 1212 | |
| 1213 | /* http://tools.ietf.org/html/rfc4511#section-4.1.1 |
| 1214 | * LDAPMessage: 0x30: SEQUENCE |
| 1215 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1216 | if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) { |
| 1217 | set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1218 | } |
| 1219 | else { |
| 1220 | /* size of LDAPMessage */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1221 | msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0; |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1222 | |
| 1223 | /* http://tools.ietf.org/html/rfc4511#section-4.2.2 |
| 1224 | * messageID: 0x02 0x01 0x01: INTEGER 1 |
| 1225 | * protocolOp: 0x61: bindResponse |
| 1226 | */ |
| 1227 | if ((msglen > 2) || |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1228 | (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) { |
| 1229 | set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1230 | |
| 1231 | goto out_wakeup; |
| 1232 | } |
| 1233 | |
| 1234 | /* size of bindResponse */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1235 | 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] | 1236 | |
| 1237 | /* http://tools.ietf.org/html/rfc4511#section-4.1.9 |
| 1238 | * ldapResult: 0x0a 0x01: ENUMERATION |
| 1239 | */ |
| 1240 | if ((msglen > 4) || |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1241 | (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) { |
| 1242 | set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1243 | |
| 1244 | goto out_wakeup; |
| 1245 | } |
| 1246 | |
| 1247 | /* http://tools.ietf.org/html/rfc4511#section-4.1.9 |
| 1248 | * resultCode |
| 1249 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1250 | check->code = *(check->bi->data + msglen + 9); |
| 1251 | if (check->code) { |
| 1252 | 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] | 1253 | } else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1254 | set_server_check_status(check, HCHK_STATUS_L7OKD, "Success"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1255 | } |
| 1256 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1257 | break; |
| 1258 | |
| 1259 | default: |
Willy Tarreau | 06559ac | 2013-12-05 01:53:08 +0100 | [diff] [blame] | 1260 | /* for other checks (eg: pure TCP), delegate to the main task */ |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1261 | break; |
| 1262 | } /* switch */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 1263 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1264 | out_wakeup: |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1265 | /* collect possible new errors */ |
| 1266 | if (conn->flags & CO_FL_ERROR) |
| 1267 | chk_report_conn_err(conn, 0, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1268 | |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 1269 | /* Reset the check buffer... */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1270 | *check->bi->data = '\0'; |
| 1271 | check->bi->i = 0; |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 1272 | |
Willy Tarreau | fd29cc5 | 2012-11-23 09:18:20 +0100 | [diff] [blame] | 1273 | /* Close the connection... We absolutely want to perform a hard close |
| 1274 | * and reset the connection if some data are pending, otherwise we end |
| 1275 | * up with many TIME_WAITs and eat all the source port range quickly. |
| 1276 | * To avoid sending RSTs all the time, we first try to drain pending |
| 1277 | * data. |
| 1278 | */ |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1279 | if (conn->xprt && conn->xprt->shutw) |
| 1280 | conn->xprt->shutw(conn, 0); |
Willy Tarreau | 2b57cb8 | 2013-06-10 19:56:38 +0200 | [diff] [blame] | 1281 | |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1282 | /* OK, let's not stay here forever */ |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 1283 | if (check->result == CHK_RES_FAILED) |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1284 | conn->flags |= CO_FL_ERROR; |
| 1285 | |
Willy Tarreau | a522f80 | 2012-11-23 08:56:35 +0100 | [diff] [blame] | 1286 | __conn_data_stop_both(conn); |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 1287 | task_wakeup(t, TASK_WOKEN_IO); |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 1288 | return; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1289 | |
| 1290 | wait_more_data: |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 1291 | __conn_data_want_recv(conn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1292 | } |
| 1293 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1294 | /* |
| 1295 | * This function is used only for server health-checks. It handles connection |
| 1296 | * status updates including errors. If necessary, it wakes the check task up. |
| 1297 | * It always returns 0. |
| 1298 | */ |
| 1299 | static int wake_srv_chk(struct connection *conn) |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1300 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1301 | struct check *check = conn->owner; |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1302 | |
Willy Tarreau | 6c560da | 2012-11-24 11:14:45 +0100 | [diff] [blame] | 1303 | if (unlikely(conn->flags & CO_FL_ERROR)) { |
Willy Tarreau | 02b0f58 | 2013-12-03 15:42:33 +0100 | [diff] [blame] | 1304 | /* We may get error reports bypassing the I/O handlers, typically |
| 1305 | * the case when sending a pure TCP check which fails, then the I/O |
| 1306 | * handlers above are not called. This is completely handled by the |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1307 | * main processing task so let's simply wake it up. If we get here, |
| 1308 | * we expect errno to still be valid. |
| 1309 | */ |
| 1310 | chk_report_conn_err(conn, errno, 0); |
| 1311 | |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 1312 | __conn_data_stop_both(conn); |
| 1313 | task_wakeup(check->task, TASK_WOKEN_IO); |
| 1314 | } |
Willy Tarreau | 3be293f | 2014-02-05 18:31:24 +0100 | [diff] [blame] | 1315 | else if (!(conn->flags & (CO_FL_DATA_RD_ENA|CO_FL_DATA_WR_ENA|CO_FL_HANDSHAKE))) { |
| 1316 | /* we may get here if only a connection probe was required : we |
| 1317 | * don't have any data to send nor anything expected in response, |
| 1318 | * so the completion of the connection establishment is enough. |
| 1319 | */ |
| 1320 | task_wakeup(check->task, TASK_WOKEN_IO); |
| 1321 | } |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 1322 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 1323 | if (check->result != CHK_RES_UNKNOWN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1324 | /* We're here because nobody wants to handle the error, so we |
| 1325 | * sure want to abort the hard way. |
Willy Tarreau | 02b0f58 | 2013-12-03 15:42:33 +0100 | [diff] [blame] | 1326 | */ |
Willy Tarreau | 46be2e5 | 2014-01-20 12:10:52 +0100 | [diff] [blame] | 1327 | conn_drain(conn); |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 1328 | conn_force_close(conn); |
Willy Tarreau | 2d351b6 | 2013-12-05 02:36:25 +0100 | [diff] [blame] | 1329 | } |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 1330 | return 0; |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1331 | } |
| 1332 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1333 | struct data_cb check_conn_cb = { |
| 1334 | .recv = event_srv_chk_r, |
| 1335 | .send = event_srv_chk_w, |
| 1336 | .wake = wake_srv_chk, |
| 1337 | }; |
| 1338 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1339 | /* |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1340 | * updates the server's weight during a warmup stage. Once the final weight is |
| 1341 | * reached, the task automatically stops. Note that any server status change |
| 1342 | * must have updated s->last_change accordingly. |
| 1343 | */ |
| 1344 | static struct task *server_warmup(struct task *t) |
| 1345 | { |
| 1346 | struct server *s = t->context; |
| 1347 | |
| 1348 | /* by default, plan on stopping the task */ |
| 1349 | t->expire = TICK_ETERNITY; |
Willy Tarreau | 2012521 | 2014-05-13 19:44:56 +0200 | [diff] [blame] | 1350 | if ((s->admin & SRV_ADMF_MAINT) || |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 1351 | (s->state != SRV_ST_STARTING)) |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1352 | return t; |
| 1353 | |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 1354 | /* recalculate the weights and update the state */ |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1355 | server_recalc_eweight(s); |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1356 | |
| 1357 | /* probably that we can refill this server with a bit more connections */ |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1358 | pendconn_grab_from_px(s); |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1359 | |
| 1360 | /* get back there in 1 second or 1/20th of the slowstart interval, |
| 1361 | * whichever is greater, resulting in small 5% steps. |
| 1362 | */ |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 1363 | if (s->state == SRV_ST_STARTING) |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1364 | t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))); |
| 1365 | return t; |
| 1366 | } |
| 1367 | |
| 1368 | /* |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1369 | * establish a server health-check that makes use of a connection. |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1370 | * |
| 1371 | * It can return one of : |
| 1372 | * - SN_ERR_NONE if everything's OK and tcpcheck_main() was not called |
| 1373 | * - SN_ERR_UP if if everything's OK and tcpcheck_main() was called |
| 1374 | * - SN_ERR_SRVTO if there are no more servers |
| 1375 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 1376 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1377 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1378 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 1379 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 1380 | * Note that we try to prevent the network stack from sending the ACK during the |
| 1381 | * connect() when a pure TCP check is used (without PROXY protocol). |
| 1382 | */ |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1383 | static int connect_conn_chk(struct task *t) |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1384 | { |
| 1385 | struct check *check = t->context; |
| 1386 | struct server *s = check->server; |
| 1387 | struct connection *conn = check->conn; |
| 1388 | struct protocol *proto; |
| 1389 | int ret; |
| 1390 | |
| 1391 | /* tcpcheck send/expect initialisation */ |
| 1392 | if (check->type == PR_O2_TCPCHK_CHK) |
| 1393 | check->current_step = NULL; |
| 1394 | |
| 1395 | /* prepare the check buffer. |
| 1396 | * This should not be used if check is the secondary agent check |
| 1397 | * of a server as s->proxy->check_req will relate to the |
| 1398 | * configuration of the primary check. Similarly, tcp-check uses |
| 1399 | * its own strings. |
| 1400 | */ |
| 1401 | if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) { |
| 1402 | bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len); |
| 1403 | |
| 1404 | /* we want to check if this host replies to HTTP or SSLv3 requests |
| 1405 | * so we'll send the request, and won't wake the checker up now. |
| 1406 | */ |
| 1407 | if ((check->type) == PR_O2_SSL3_CHK) { |
| 1408 | /* SSL requires that we put Unix time in the request */ |
| 1409 | int gmt_time = htonl(date.tv_sec); |
| 1410 | memcpy(check->bo->data + 11, &gmt_time, 4); |
| 1411 | } |
| 1412 | else if ((check->type) == PR_O2_HTTP_CHK) { |
| 1413 | if (s->proxy->options2 & PR_O2_CHK_SNDST) |
| 1414 | bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size)); |
| 1415 | bo_putstr(check->bo, "\r\n"); |
| 1416 | *check->bo->p = '\0'; /* to make gdb output easier to read */ |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | /* prepare a new connection */ |
| 1421 | conn_init(conn); |
| 1422 | conn_prepare(conn, s->check_common.proto, s->check_common.xprt); |
| 1423 | conn_attach(conn, check, &check_conn_cb); |
| 1424 | conn->target = &s->obj_type; |
| 1425 | |
| 1426 | /* no client address */ |
| 1427 | clear_addr(&conn->addr.from); |
| 1428 | |
| 1429 | if (is_addr(&s->check_common.addr)) { |
| 1430 | |
| 1431 | /* we'll connect to the check addr specified on the server */ |
| 1432 | conn->addr.to = s->check_common.addr; |
| 1433 | proto = s->check_common.proto; |
| 1434 | } |
| 1435 | else { |
| 1436 | /* we'll connect to the addr on the server */ |
| 1437 | conn->addr.to = s->addr; |
| 1438 | proto = s->proto; |
| 1439 | } |
| 1440 | |
| 1441 | if (check->port) { |
| 1442 | set_host_port(&conn->addr.to, check->port); |
| 1443 | } |
| 1444 | |
| 1445 | if (check->type == PR_O2_TCPCHK_CHK) { |
| 1446 | struct tcpcheck_rule *r = (struct tcpcheck_rule *) s->proxy->tcpcheck_rules.n; |
| 1447 | /* if first step is a 'connect', then tcpcheck_main must run it */ |
| 1448 | if (r->action == TCPCHK_ACT_CONNECT) { |
| 1449 | tcpcheck_main(conn); |
| 1450 | return SN_ERR_UP; |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | ret = SN_ERR_INTERNAL; |
| 1455 | if (proto->connect) |
| 1456 | ret = proto->connect(conn, check->type, (check->type) ? 0 : 2); |
| 1457 | conn->flags |= CO_FL_WAKE_DATA; |
| 1458 | if (s->check.send_proxy) { |
| 1459 | conn->send_proxy_ofs = 1; |
| 1460 | conn->flags |= CO_FL_SEND_PROXY; |
| 1461 | } |
| 1462 | |
| 1463 | return ret; |
| 1464 | } |
| 1465 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1466 | static struct list pid_list = LIST_HEAD_INIT(pid_list); |
| 1467 | static struct pool_head *pool2_pid_list; |
| 1468 | |
| 1469 | void block_sigchld(void) |
| 1470 | { |
| 1471 | sigset_t set; |
| 1472 | sigemptyset(&set); |
| 1473 | sigaddset(&set, SIGCHLD); |
| 1474 | assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0); |
| 1475 | } |
| 1476 | |
| 1477 | void unblock_sigchld(void) |
| 1478 | { |
| 1479 | sigset_t set; |
| 1480 | sigemptyset(&set); |
| 1481 | assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0); |
| 1482 | } |
| 1483 | |
| 1484 | /* Call with SIGCHLD blocked */ |
| 1485 | static struct pid_list *pid_list_add(pid_t pid, struct task *t) |
| 1486 | { |
| 1487 | struct pid_list *elem; |
| 1488 | struct check *check = t->context; |
| 1489 | |
| 1490 | elem = pool_alloc2(pool2_pid_list); |
| 1491 | if (!elem) |
| 1492 | return NULL; |
| 1493 | elem->pid = pid; |
| 1494 | elem->t = t; |
| 1495 | elem->exited = 0; |
| 1496 | check->curpid = elem; |
| 1497 | LIST_INIT(&elem->list); |
| 1498 | LIST_ADD(&pid_list, &elem->list); |
| 1499 | return elem; |
| 1500 | } |
| 1501 | |
| 1502 | /* Blocks blocks and then unblocks SIGCHLD */ |
| 1503 | static void pid_list_del(struct pid_list *elem) |
| 1504 | { |
| 1505 | struct check *check; |
| 1506 | |
| 1507 | if (!elem) |
| 1508 | return; |
| 1509 | |
| 1510 | block_sigchld(); |
| 1511 | LIST_DEL(&elem->list); |
| 1512 | unblock_sigchld(); |
| 1513 | if (!elem->exited) |
| 1514 | kill(elem->pid, SIGTERM); |
| 1515 | |
| 1516 | check = elem->t->context; |
| 1517 | check->curpid = NULL; |
| 1518 | pool_free2(pool2_pid_list, elem); |
| 1519 | } |
| 1520 | |
| 1521 | /* Called from inside SIGCHLD handler, SIGCHLD is blocked */ |
| 1522 | static void pid_list_expire(pid_t pid, int status) |
| 1523 | { |
| 1524 | struct pid_list *elem; |
| 1525 | |
| 1526 | list_for_each_entry(elem, &pid_list, list) { |
| 1527 | if (elem->pid == pid) { |
| 1528 | elem->t->expire = now_ms; |
| 1529 | elem->status = status; |
| 1530 | elem->exited = 1; |
Cyril Bonté | 9dbcfab | 2014-08-07 01:55:39 +0200 | [diff] [blame] | 1531 | task_wakeup(elem->t, TASK_WOKEN_IO); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1532 | return; |
| 1533 | } |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | static void sigchld_handler(int signal) |
| 1538 | { |
| 1539 | pid_t pid; |
| 1540 | int status; |
| 1541 | while ((pid = waitpid(0, &status, WNOHANG)) > 0) |
| 1542 | pid_list_expire(pid, status); |
| 1543 | } |
| 1544 | |
| 1545 | static int init_pid_list(void) { |
| 1546 | struct sigaction action = { |
| 1547 | .sa_handler = sigchld_handler, |
| 1548 | .sa_flags = SA_NOCLDSTOP |
| 1549 | }; |
| 1550 | |
| 1551 | if (pool2_pid_list != NULL) |
| 1552 | /* Nothing to do */ |
| 1553 | return 0; |
| 1554 | |
| 1555 | if (sigaction(SIGCHLD, &action, NULL)) { |
| 1556 | Alert("Failed to set signal handler for external health checks: %s. Aborting.\n", |
| 1557 | strerror(errno)); |
| 1558 | return 1; |
| 1559 | } |
| 1560 | |
| 1561 | pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED); |
| 1562 | if (pool2_pid_list == NULL) { |
| 1563 | Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n", |
| 1564 | strerror(errno)); |
| 1565 | return 1; |
| 1566 | } |
| 1567 | |
| 1568 | return 0; |
| 1569 | } |
| 1570 | |
| 1571 | |
| 1572 | static int prepare_external_check(struct check *check) |
| 1573 | { |
| 1574 | struct server *s = check->server; |
| 1575 | struct proxy *px = s->proxy; |
| 1576 | struct listener *listener = NULL, *l; |
| 1577 | int i; |
| 1578 | const char *err_fmt = "Starting [%s:%s] check: out of memory.\n"; |
| 1579 | const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH; |
| 1580 | char host[46]; |
| 1581 | char serv[6]; |
| 1582 | |
| 1583 | list_for_each_entry(l, &px->conf.listeners, by_fe) |
| 1584 | /* Use the first INET, INET6 or UNIX listener */ |
| 1585 | if (l->addr.ss_family == AF_INET || |
| 1586 | l->addr.ss_family == AF_INET6 || |
| 1587 | l->addr.ss_family == AF_UNIX) { |
| 1588 | listener = l; |
| 1589 | break; |
| 1590 | } |
| 1591 | |
| 1592 | if (!listener) { |
| 1593 | err_fmt = "Starting [%s:%s] check: no listener.\n"; |
| 1594 | goto err; |
| 1595 | } |
| 1596 | |
| 1597 | check->curpid = NULL; |
| 1598 | |
| 1599 | check->envp = calloc(2, sizeof(check->argv)); |
| 1600 | if (!check->envp) |
| 1601 | goto err; |
| 1602 | check->envp[0] = malloc(strlen("PATH=") + strlen(path) + 1); |
| 1603 | if (!check->envp[0]) |
| 1604 | goto err; |
| 1605 | strcpy(check->envp[0], "PATH="); |
| 1606 | strcpy(check->envp[0] + strlen(check->envp[0]), path); |
| 1607 | check->envp[1] = NULL; |
| 1608 | |
| 1609 | check->argv = calloc(6, sizeof(check->argv)); |
| 1610 | if (!check->argv) |
| 1611 | goto err; |
| 1612 | |
| 1613 | check->argv[0] = px->check_command; |
| 1614 | |
| 1615 | if (listener->addr.ss_family == AF_INET || |
| 1616 | listener->addr.ss_family == AF_INET6) { |
| 1617 | addr_to_str(&listener->addr, host, sizeof(host)); |
| 1618 | check->argv[1] = strdup(host); |
| 1619 | port_to_str(&listener->addr, serv, sizeof(serv)); |
| 1620 | check->argv[2] = strdup(serv); |
| 1621 | } else if (listener->addr.ss_family == AF_UNIX) { |
| 1622 | const struct sockaddr_un *un; |
| 1623 | |
| 1624 | un = (struct sockaddr_un *)&listener->addr; |
| 1625 | check->argv[1] = strdup(un->sun_path); |
| 1626 | check->argv[2] = strdup("NOT_USED"); |
| 1627 | } else { |
| 1628 | goto err; |
| 1629 | } |
| 1630 | |
| 1631 | addr_to_str(&s->addr, host, sizeof(host)); |
| 1632 | check->argv[3] = strdup(host); |
| 1633 | port_to_str(&s->addr, serv, sizeof(serv)); |
| 1634 | check->argv[4] = strdup(serv); |
| 1635 | |
| 1636 | for (i = 0; i < 5; i++) |
| 1637 | if (!check->argv[i]) |
| 1638 | goto err; |
| 1639 | |
Cyril Bonté | 99c5bf5 | 2014-08-07 01:55:38 +0200 | [diff] [blame] | 1640 | return 1; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1641 | err: |
| 1642 | if (check->envp) { |
| 1643 | free(check->envp[1]); |
| 1644 | free(check->envp); |
| 1645 | check->envp = NULL; |
| 1646 | } |
| 1647 | |
| 1648 | if (check->argv) { |
| 1649 | for (i = 1; i < 5; i++) |
| 1650 | free(check->argv[i]); |
| 1651 | free(check->argv); |
| 1652 | check->argv = NULL; |
| 1653 | } |
| 1654 | Alert(err_fmt, px->id, s->id); |
Cyril Bonté | 99c5bf5 | 2014-08-07 01:55:38 +0200 | [diff] [blame] | 1655 | return 0; |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1656 | } |
| 1657 | |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1658 | /* |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1659 | * establish a server health-check that makes use of a process. |
| 1660 | * |
| 1661 | * It can return one of : |
| 1662 | * - SN_ERR_NONE if everything's OK |
| 1663 | * - SN_ERR_SRVTO if there are no more servers |
| 1664 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 1665 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1666 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1667 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 1668 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 1669 | * |
| 1670 | * Blocks and then unblocks SIGCHLD |
| 1671 | */ |
| 1672 | static int connect_proc_chk(struct task *t) |
| 1673 | { |
| 1674 | struct check *check = t->context; |
| 1675 | struct server *s = check->server; |
| 1676 | struct proxy *px = s->proxy; |
| 1677 | int status; |
| 1678 | pid_t pid; |
| 1679 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1680 | status = SN_ERR_RESOURCE; |
| 1681 | |
| 1682 | block_sigchld(); |
| 1683 | |
| 1684 | pid = fork(); |
| 1685 | if (pid < 0) { |
| 1686 | Alert("Failed to fork process for external health check: %s. Aborting.\n", |
| 1687 | strerror(errno)); |
| 1688 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 1689 | goto out; |
| 1690 | } |
| 1691 | if (pid == 0) { |
| 1692 | /* Child */ |
| 1693 | extern char **environ; |
| 1694 | environ = check->envp; |
| 1695 | execvp(px->check_command, check->argv); |
| 1696 | Alert("Failed to exec process for external health check: %s. Aborting.\n", |
| 1697 | strerror(errno)); |
| 1698 | exit(-1); |
| 1699 | } |
| 1700 | |
| 1701 | /* Parent */ |
| 1702 | if (check->result == CHK_RES_UNKNOWN) { |
| 1703 | if (pid_list_add(pid, t) != NULL) { |
| 1704 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 1705 | |
| 1706 | if (px->timeout.check && px->timeout.connect) { |
| 1707 | int t_con = tick_add(now_ms, px->timeout.connect); |
| 1708 | t->expire = tick_first(t->expire, t_con); |
| 1709 | } |
| 1710 | status = SN_ERR_NONE; |
| 1711 | goto out; |
| 1712 | } |
| 1713 | else { |
| 1714 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 1715 | } |
| 1716 | kill(pid, SIGTERM); /* process creation error */ |
| 1717 | } |
| 1718 | else |
| 1719 | set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno)); |
| 1720 | |
| 1721 | out: |
| 1722 | unblock_sigchld(); |
| 1723 | return status; |
| 1724 | } |
| 1725 | |
| 1726 | /* |
| 1727 | * establish a server health-check. |
| 1728 | * |
| 1729 | * It can return one of : |
| 1730 | * - SN_ERR_NONE if everything's OK |
| 1731 | * - SN_ERR_SRVTO if there are no more servers |
| 1732 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 1733 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1734 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1735 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 1736 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 1737 | */ |
| 1738 | static int connect_chk(struct task *t) |
| 1739 | { |
| 1740 | struct check *check = t->context; |
| 1741 | |
| 1742 | if (check->type == PR_O2_EXT_CHK) |
| 1743 | return connect_proc_chk(t); |
| 1744 | return connect_conn_chk(t); |
| 1745 | } |
| 1746 | |
| 1747 | /* |
| 1748 | * manages a server health-check that uses a process. Returns |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1749 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 1750 | */ |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1751 | static struct task *process_chk_proc(struct task *t) |
| 1752 | { |
| 1753 | struct check *check = t->context; |
| 1754 | struct server *s = check->server; |
| 1755 | struct connection *conn = check->conn; |
| 1756 | int rv; |
| 1757 | int ret; |
| 1758 | int expired = tick_is_expired(t->expire, now_ms); |
| 1759 | |
| 1760 | if (!(check->state & CHK_ST_INPROGRESS)) { |
| 1761 | /* no check currently running */ |
| 1762 | if (!expired) /* woke up too early */ |
| 1763 | return t; |
| 1764 | |
| 1765 | /* we don't send any health-checks when the proxy is |
| 1766 | * stopped, the server should not be checked or the check |
| 1767 | * is disabled. |
| 1768 | */ |
| 1769 | if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) || |
| 1770 | s->proxy->state == PR_STSTOPPED) |
| 1771 | goto reschedule; |
| 1772 | |
| 1773 | /* we'll initiate a new check */ |
| 1774 | set_server_check_status(check, HCHK_STATUS_START, NULL); |
| 1775 | |
| 1776 | check->state |= CHK_ST_INPROGRESS; |
| 1777 | |
| 1778 | ret = connect_chk(t); |
| 1779 | switch (ret) { |
| 1780 | case SN_ERR_UP: |
| 1781 | return t; |
| 1782 | case SN_ERR_NONE: |
| 1783 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1784 | * to establish but only when timeout.check is set |
| 1785 | * as it may be to short for a full check otherwise |
| 1786 | */ |
| 1787 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 1788 | |
| 1789 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 1790 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 1791 | t->expire = tick_first(t->expire, t_con); |
| 1792 | } |
| 1793 | |
| 1794 | goto reschedule; |
| 1795 | |
| 1796 | case SN_ERR_SRVTO: /* ETIMEDOUT */ |
| 1797 | case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */ |
| 1798 | conn->flags |= CO_FL_ERROR; |
| 1799 | chk_report_conn_err(conn, errno, 0); |
| 1800 | break; |
| 1801 | case SN_ERR_PRXCOND: |
| 1802 | case SN_ERR_RESOURCE: |
| 1803 | case SN_ERR_INTERNAL: |
| 1804 | conn->flags |= CO_FL_ERROR; |
| 1805 | chk_report_conn_err(conn, 0, 0); |
| 1806 | break; |
| 1807 | } |
| 1808 | |
| 1809 | /* here, we have seen a synchronous error, no fd was allocated */ |
| 1810 | |
| 1811 | check->state &= ~CHK_ST_INPROGRESS; |
| 1812 | check_notify_failure(check); |
| 1813 | |
| 1814 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1815 | * to establish but only when timeout.check is set |
| 1816 | * as it may be to short for a full check otherwise |
| 1817 | */ |
| 1818 | while (tick_is_expired(t->expire, now_ms)) { |
| 1819 | int t_con; |
| 1820 | |
| 1821 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 1822 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 1823 | |
| 1824 | if (s->proxy->timeout.check) |
| 1825 | t->expire = tick_first(t->expire, t_con); |
| 1826 | } |
| 1827 | } |
| 1828 | else { |
| 1829 | /* there was a test running. |
| 1830 | * First, let's check whether there was an uncaught error, |
| 1831 | * which can happen on connect timeout or error. |
| 1832 | */ |
| 1833 | if (check->result == CHK_RES_UNKNOWN) { |
| 1834 | /* good connection is enough for pure TCP check */ |
| 1835 | struct pid_list *elem = check->curpid; |
| 1836 | int status = HCHK_STATUS_UNKNOWN; |
| 1837 | |
| 1838 | if (elem->exited) { |
| 1839 | status = elem->status; /* Save in case the process exits between use below */ |
| 1840 | if (!WIFEXITED(status)) |
| 1841 | check->code = -1; |
| 1842 | else |
| 1843 | check->code = WEXITSTATUS(status); |
| 1844 | if (!WIFEXITED(status) || WEXITSTATUS(status)) |
| 1845 | status = HCHK_STATUS_PROCERR; |
| 1846 | else |
| 1847 | status = HCHK_STATUS_PROCOK; |
| 1848 | } else if (expired) { |
| 1849 | status = HCHK_STATUS_PROCTOUT; |
Willy Tarreau | dc3d190 | 2014-07-08 00:56:27 +0200 | [diff] [blame] | 1850 | Warning("kill %d\n", (int)elem->pid); |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 1851 | kill(elem->pid, SIGTERM); |
| 1852 | } |
| 1853 | set_server_check_status(check, status, NULL); |
| 1854 | } |
| 1855 | |
| 1856 | if (check->result == CHK_RES_FAILED) { |
| 1857 | /* a failure or timeout detected */ |
| 1858 | check_notify_failure(check); |
| 1859 | } |
| 1860 | else if (check->result == CHK_RES_CONDPASS) { |
| 1861 | /* check is OK but asks for stopping mode */ |
| 1862 | check_notify_stopping(check); |
| 1863 | } |
| 1864 | else if (check->result == CHK_RES_PASSED) { |
| 1865 | /* a success was detected */ |
| 1866 | check_notify_success(check); |
| 1867 | } |
| 1868 | check->state &= ~CHK_ST_INPROGRESS; |
| 1869 | |
| 1870 | pid_list_del(check->curpid); |
| 1871 | |
| 1872 | rv = 0; |
| 1873 | if (global.spread_checks > 0) { |
| 1874 | rv = srv_getinter(check) * global.spread_checks / 100; |
| 1875 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
| 1876 | } |
| 1877 | t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv)); |
| 1878 | } |
| 1879 | |
| 1880 | reschedule: |
| 1881 | while (tick_is_expired(t->expire, now_ms)) |
| 1882 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 1883 | return t; |
| 1884 | } |
| 1885 | |
| 1886 | /* |
| 1887 | * manages a server health-check that uses a connection. Returns |
| 1888 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 1889 | */ |
| 1890 | static struct task *process_chk_conn(struct task *t) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1891 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1892 | struct check *check = t->context; |
| 1893 | struct server *s = check->server; |
| 1894 | struct connection *conn = check->conn; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1895 | int rv; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1896 | int ret; |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1897 | int expired = tick_is_expired(t->expire, now_ms); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1898 | |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1899 | if (!(check->state & CHK_ST_INPROGRESS)) { |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1900 | /* no check currently running */ |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1901 | if (!expired) /* woke up too early */ |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1902 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1903 | |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 1904 | /* we don't send any health-checks when the proxy is |
| 1905 | * stopped, the server should not be checked or the check |
| 1906 | * is disabled. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1907 | */ |
Willy Tarreau | 0d924cc | 2013-12-11 21:26:24 +0100 | [diff] [blame] | 1908 | if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) || |
Willy Tarreau | 33a08db | 2013-12-11 21:03:31 +0100 | [diff] [blame] | 1909 | s->proxy->state == PR_STSTOPPED) |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1910 | goto reschedule; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1911 | |
| 1912 | /* we'll initiate a new check */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1913 | set_server_check_status(check, HCHK_STATUS_START, NULL); |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1914 | |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1915 | check->state |= CHK_ST_INPROGRESS; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1916 | check->bi->p = check->bi->data; |
| 1917 | check->bi->i = 0; |
| 1918 | check->bo->p = check->bo->data; |
| 1919 | check->bo->o = 0; |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1920 | |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1921 | ret = connect_chk(t); |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1922 | switch (ret) { |
Simon Horman | b00d17a | 2014-06-13 16:18:16 +0900 | [diff] [blame] | 1923 | case SN_ERR_UP: |
| 1924 | return t; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1925 | case SN_ERR_NONE: |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1926 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1927 | * to establish but only when timeout.check is set |
| 1928 | * as it may be to short for a full check otherwise |
| 1929 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1930 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1931 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1932 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 1933 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 1934 | t->expire = tick_first(t->expire, t_con); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1935 | } |
Willy Tarreau | 06559ac | 2013-12-05 01:53:08 +0100 | [diff] [blame] | 1936 | |
| 1937 | if (check->type) |
| 1938 | conn_data_want_recv(conn); /* prepare for reading a possible reply */ |
| 1939 | |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1940 | goto reschedule; |
| 1941 | |
| 1942 | case SN_ERR_SRVTO: /* ETIMEDOUT */ |
| 1943 | case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */ |
Willy Tarreau | 4bd07de | 2014-01-24 16:10:57 +0100 | [diff] [blame] | 1944 | conn->flags |= CO_FL_ERROR; |
| 1945 | chk_report_conn_err(conn, errno, 0); |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1946 | break; |
| 1947 | case SN_ERR_PRXCOND: |
| 1948 | case SN_ERR_RESOURCE: |
| 1949 | case SN_ERR_INTERNAL: |
Willy Tarreau | 4bd07de | 2014-01-24 16:10:57 +0100 | [diff] [blame] | 1950 | conn->flags |= CO_FL_ERROR; |
| 1951 | chk_report_conn_err(conn, 0, 0); |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1952 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1953 | } |
| 1954 | |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1955 | /* here, we have seen a synchronous error, no fd was allocated */ |
Willy Tarreau | 6b0a850 | 2012-11-23 08:51:32 +0100 | [diff] [blame] | 1956 | |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 1957 | check->state &= ~CHK_ST_INPROGRESS; |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 1958 | check_notify_failure(check); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1959 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1960 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1961 | * to establish but only when timeout.check is set |
| 1962 | * as it may be to short for a full check otherwise |
| 1963 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1964 | while (tick_is_expired(t->expire, now_ms)) { |
| 1965 | int t_con; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1966 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1967 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1968 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1969 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1970 | if (s->proxy->timeout.check) |
| 1971 | t->expire = tick_first(t->expire, t_con); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1972 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1973 | } |
| 1974 | else { |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1975 | /* there was a test running. |
| 1976 | * First, let's check whether there was an uncaught error, |
| 1977 | * which can happen on connect timeout or error. |
| 1978 | */ |
Simon Horman | ccaabcd | 2014-06-20 12:29:47 +0900 | [diff] [blame] | 1979 | if (check->result == CHK_RES_UNKNOWN) { |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1980 | /* good connection is enough for pure TCP check */ |
| 1981 | if ((conn->flags & CO_FL_CONNECTED) && !check->type) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1982 | if (check->use_ssl) |
| 1983 | set_server_check_status(check, HCHK_STATUS_L6OK, NULL); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1984 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1985 | set_server_check_status(check, HCHK_STATUS_L4OK, NULL); |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1986 | } |
Willy Tarreau | 25e2ab5 | 2013-12-04 11:17:05 +0100 | [diff] [blame] | 1987 | else if ((conn->flags & CO_FL_ERROR) || expired) { |
| 1988 | chk_report_conn_err(conn, 0, expired); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1989 | } |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1990 | else |
| 1991 | goto out_wait; /* timeout not reached, wait again */ |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1992 | } |
| 1993 | |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1994 | /* check complete or aborted */ |
Willy Tarreau | 5ba04f6 | 2013-02-12 15:23:12 +0100 | [diff] [blame] | 1995 | if (conn->xprt) { |
| 1996 | /* The check was aborted and the connection was not yet closed. |
| 1997 | * This can happen upon timeout, or when an external event such |
| 1998 | * as a failed response coupled with "observe layer7" caused the |
| 1999 | * server state to be suddenly changed. |
| 2000 | */ |
Willy Tarreau | 46be2e5 | 2014-01-20 12:10:52 +0100 | [diff] [blame] | 2001 | conn_drain(conn); |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 2002 | conn_force_close(conn); |
Willy Tarreau | 5ba04f6 | 2013-02-12 15:23:12 +0100 | [diff] [blame] | 2003 | } |
| 2004 | |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 2005 | if (check->result == CHK_RES_FAILED) { |
| 2006 | /* a failure or timeout detected */ |
Willy Tarreau | 4eec547 | 2014-05-20 22:32:27 +0200 | [diff] [blame] | 2007 | check_notify_failure(check); |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 2008 | } |
Willy Tarreau | db58b79 | 2014-05-21 13:57:23 +0200 | [diff] [blame] | 2009 | else if (check->result == CHK_RES_CONDPASS) { |
| 2010 | /* check is OK but asks for stopping mode */ |
| 2011 | check_notify_stopping(check); |
Willy Tarreau | af54958 | 2014-05-16 17:37:50 +0200 | [diff] [blame] | 2012 | } |
Willy Tarreau | 3e04838 | 2014-05-21 10:30:54 +0200 | [diff] [blame] | 2013 | else if (check->result == CHK_RES_PASSED) { |
| 2014 | /* a success was detected */ |
| 2015 | check_notify_success(check); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2016 | } |
Willy Tarreau | 2c115e5 | 2013-12-11 19:41:16 +0100 | [diff] [blame] | 2017 | check->state &= ~CHK_ST_INPROGRESS; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2018 | |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 2019 | rv = 0; |
| 2020 | if (global.spread_checks > 0) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2021 | rv = srv_getinter(check) * global.spread_checks / 100; |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 2022 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2023 | } |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2024 | 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] | 2025 | } |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 2026 | |
| 2027 | reschedule: |
| 2028 | while (tick_is_expired(t->expire, now_ms)) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2029 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 2030 | out_wait: |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 2031 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2032 | } |
| 2033 | |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 2034 | /* |
| 2035 | * manages a server health-check. Returns |
| 2036 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 2037 | */ |
| 2038 | static struct task *process_chk(struct task *t) |
| 2039 | { |
| 2040 | struct check *check = t->context; |
| 2041 | |
| 2042 | if (check->type == PR_O2_EXT_CHK) |
| 2043 | return process_chk_proc(t); |
| 2044 | return process_chk_conn(t); |
| 2045 | } |
| 2046 | |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 2047 | static int start_check_task(struct check *check, int mininter, |
| 2048 | int nbcheck, int srvpos) |
| 2049 | { |
| 2050 | struct task *t; |
| 2051 | /* task for the check */ |
| 2052 | if ((t = task_new()) == NULL) { |
| 2053 | Alert("Starting [%s:%s] check: out of memory.\n", |
| 2054 | check->server->proxy->id, check->server->id); |
| 2055 | return 0; |
| 2056 | } |
| 2057 | |
| 2058 | check->task = t; |
| 2059 | t->process = process_chk; |
| 2060 | t->context = check; |
| 2061 | |
Willy Tarreau | 1746eec | 2014-04-25 10:46:47 +0200 | [diff] [blame] | 2062 | if (mininter < srv_getinter(check)) |
| 2063 | mininter = srv_getinter(check); |
| 2064 | |
| 2065 | if (global.max_spread_checks && mininter > global.max_spread_checks) |
| 2066 | mininter = global.max_spread_checks; |
| 2067 | |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 2068 | /* check this every ms */ |
Willy Tarreau | 1746eec | 2014-04-25 10:46:47 +0200 | [diff] [blame] | 2069 | t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck)); |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 2070 | check->start = now; |
| 2071 | task_queue(t); |
| 2072 | |
| 2073 | return 1; |
| 2074 | } |
| 2075 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2076 | /* |
| 2077 | * Start health-check. |
| 2078 | * Returns 0 if OK, -1 if error, and prints the error in this case. |
| 2079 | */ |
| 2080 | int start_checks() { |
| 2081 | |
| 2082 | struct proxy *px; |
| 2083 | struct server *s; |
| 2084 | struct task *t; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2085 | int nbcheck=0, mininter=0, srvpos=0; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2086 | |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 2087 | /* 1- count the checkers to run simultaneously. |
| 2088 | * We also determine the minimum interval among all of those which |
| 2089 | * have an interval larger than SRV_CHK_INTER_THRES. This interval |
| 2090 | * will be used to spread their start-up date. Those which have |
Jamie Gloudon | 801a0a3 | 2012-08-25 00:18:33 -0400 | [diff] [blame] | 2091 | * a shorter interval will start independently and will not dictate |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 2092 | * too short an interval for all others. |
| 2093 | */ |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2094 | for (px = proxy; px; px = px->next) { |
| 2095 | for (s = px->srv; s; s = s->next) { |
Willy Tarreau | e7b7348 | 2013-11-21 11:50:50 +0100 | [diff] [blame] | 2096 | if (s->slowstart) { |
| 2097 | if ((t = task_new()) == NULL) { |
| 2098 | Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 2099 | return -1; |
| 2100 | } |
| 2101 | /* We need a warmup task that will be called when the server |
| 2102 | * state switches from down to up. |
| 2103 | */ |
| 2104 | s->warmup = t; |
| 2105 | t->process = server_warmup; |
| 2106 | t->context = s; |
| 2107 | t->expire = TICK_ETERNITY; |
| 2108 | } |
| 2109 | |
Willy Tarreau | d8514a2 | 2013-12-11 21:10:14 +0100 | [diff] [blame] | 2110 | if (s->check.state & CHK_ST_CONFIGURED) { |
| 2111 | nbcheck++; |
| 2112 | if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) && |
| 2113 | (!mininter || mininter > srv_getinter(&s->check))) |
| 2114 | mininter = srv_getinter(&s->check); |
| 2115 | } |
Willy Tarreau | 15f3910 | 2013-12-11 20:41:18 +0100 | [diff] [blame] | 2116 | |
Willy Tarreau | d8514a2 | 2013-12-11 21:10:14 +0100 | [diff] [blame] | 2117 | if (s->agent.state & CHK_ST_CONFIGURED) { |
| 2118 | nbcheck++; |
| 2119 | if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) && |
| 2120 | (!mininter || mininter > srv_getinter(&s->agent))) |
| 2121 | mininter = srv_getinter(&s->agent); |
| 2122 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2123 | } |
| 2124 | } |
| 2125 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2126 | if (!nbcheck) |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2127 | return 0; |
| 2128 | |
| 2129 | srand((unsigned)time(NULL)); |
| 2130 | |
| 2131 | /* |
| 2132 | * 2- start them as far as possible from each others. For this, we will |
| 2133 | * start them after their interval set to the min interval divided by |
| 2134 | * the number of servers, weighted by the server's position in the list. |
| 2135 | */ |
| 2136 | for (px = proxy; px; px = px->next) { |
Simon Horman | 98637e5 | 2014-06-20 12:30:16 +0900 | [diff] [blame] | 2137 | if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) { |
| 2138 | if (init_pid_list()) { |
| 2139 | Alert("Starting [%s] check: out of memory.\n", px->id); |
| 2140 | return -1; |
| 2141 | } |
| 2142 | } |
| 2143 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2144 | for (s = px->srv; s; s = s->next) { |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2145 | /* A task for the main check */ |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 2146 | if (s->check.state & CHK_ST_CONFIGURED) { |
Cyril Bonté | 99c5bf5 | 2014-08-07 01:55:38 +0200 | [diff] [blame] | 2147 | if (s->check.type == PR_O2_EXT_CHK) { |
| 2148 | if (!prepare_external_check(&s->check)) |
| 2149 | return -1; |
| 2150 | } |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2151 | if (!start_check_task(&s->check, mininter, nbcheck, srvpos)) |
| 2152 | return -1; |
| 2153 | srvpos++; |
| 2154 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2155 | |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2156 | /* A task for a auxiliary agent check */ |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 2157 | if (s->agent.state & CHK_ST_CONFIGURED) { |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 2158 | if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) { |
| 2159 | return -1; |
| 2160 | } |
| 2161 | srvpos++; |
| 2162 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 2163 | } |
| 2164 | } |
| 2165 | return 0; |
| 2166 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2167 | |
| 2168 | /* |
Willy Tarreau | 5b3a202 | 2012-09-28 15:01:02 +0200 | [diff] [blame] | 2169 | * Perform content verification check on data in s->check.buffer buffer. |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2170 | * The buffer MUST be terminated by a null byte before calling this function. |
| 2171 | * Sets server status appropriately. The caller is responsible for ensuring |
| 2172 | * that the buffer contains at least 13 characters. If <done> is zero, we may |
| 2173 | * return 0 to indicate that data is required to decide of a match. |
| 2174 | */ |
| 2175 | static int httpchk_expect(struct server *s, int done) |
| 2176 | { |
| 2177 | static char status_msg[] = "HTTP status check returned code <000>"; |
| 2178 | char status_code[] = "000"; |
| 2179 | char *contentptr; |
| 2180 | int crlf; |
| 2181 | int ret; |
| 2182 | |
| 2183 | switch (s->proxy->options2 & PR_O2_EXP_TYPE) { |
| 2184 | case PR_O2_EXP_STS: |
| 2185 | case PR_O2_EXP_RSTS: |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 2186 | memcpy(status_code, s->check.bi->data + 9, 3); |
| 2187 | 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] | 2188 | |
| 2189 | if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS) |
| 2190 | ret = strncmp(s->proxy->expect_str, status_code, 3) == 0; |
| 2191 | else |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 2192 | ret = regex_exec(s->proxy->expect_regex, status_code); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2193 | |
| 2194 | /* we necessarily have the response, so there are no partial failures */ |
| 2195 | if (s->proxy->options2 & PR_O2_EXP_INV) |
| 2196 | ret = !ret; |
| 2197 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2198 | 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] | 2199 | break; |
| 2200 | |
| 2201 | case PR_O2_EXP_STR: |
| 2202 | case PR_O2_EXP_RSTR: |
| 2203 | /* very simple response parser: ignore CR and only count consecutive LFs, |
| 2204 | * stop with contentptr pointing to first char after the double CRLF or |
| 2205 | * to '\0' if crlf < 2. |
| 2206 | */ |
| 2207 | crlf = 0; |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 2208 | for (contentptr = s->check.bi->data; *contentptr; contentptr++) { |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2209 | if (crlf >= 2) |
| 2210 | break; |
| 2211 | if (*contentptr == '\r') |
| 2212 | continue; |
| 2213 | else if (*contentptr == '\n') |
| 2214 | crlf++; |
| 2215 | else |
| 2216 | crlf = 0; |
| 2217 | } |
| 2218 | |
| 2219 | /* Check that response contains a body... */ |
| 2220 | if (crlf < 2) { |
| 2221 | if (!done) |
| 2222 | return 0; |
| 2223 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2224 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2225 | "HTTP content check could not find a response body"); |
| 2226 | return 1; |
| 2227 | } |
| 2228 | |
| 2229 | /* Check that response body is not empty... */ |
| 2230 | if (*contentptr == '\0') { |
Willy Tarreau | a164fb5 | 2011-04-13 09:32:41 +0200 | [diff] [blame] | 2231 | if (!done) |
| 2232 | return 0; |
| 2233 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2234 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2235 | "HTTP content check found empty response body"); |
| 2236 | return 1; |
| 2237 | } |
| 2238 | |
| 2239 | /* Check the response content against the supplied string |
| 2240 | * or regex... */ |
| 2241 | if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR) |
| 2242 | ret = strstr(contentptr, s->proxy->expect_str) != NULL; |
| 2243 | else |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 2244 | ret = regex_exec(s->proxy->expect_regex, contentptr); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2245 | |
| 2246 | /* if we don't match, we may need to wait more */ |
| 2247 | if (!ret && !done) |
| 2248 | return 0; |
| 2249 | |
| 2250 | if (ret) { |
| 2251 | /* content matched */ |
| 2252 | if (s->proxy->options2 & PR_O2_EXP_INV) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2253 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2254 | "HTTP check matched unwanted content"); |
| 2255 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2256 | set_server_check_status(&s->check, HCHK_STATUS_L7OKD, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2257 | "HTTP content check matched"); |
| 2258 | } |
| 2259 | else { |
| 2260 | if (s->proxy->options2 & PR_O2_EXP_INV) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2261 | set_server_check_status(&s->check, HCHK_STATUS_L7OKD, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2262 | "HTTP check did not match unwanted content"); |
| 2263 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2264 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2265 | "HTTP content check did not match"); |
| 2266 | } |
| 2267 | break; |
| 2268 | } |
| 2269 | return 1; |
| 2270 | } |
| 2271 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2272 | /* |
| 2273 | * return the id of a step in a send/expect session |
| 2274 | */ |
| 2275 | static int tcpcheck_get_step_id(struct server *s) |
| 2276 | { |
| 2277 | struct tcpcheck_rule *cur = NULL, *next = NULL; |
| 2278 | int i = 0; |
| 2279 | |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 2280 | /* not even started anything yet => step 0 = initial connect */ |
| 2281 | if (!s->check.current_step) |
| 2282 | return 0; |
| 2283 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2284 | cur = s->check.last_started_step; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2285 | |
| 2286 | /* no step => first step */ |
| 2287 | if (cur == NULL) |
| 2288 | return 1; |
| 2289 | |
| 2290 | /* increment i until current step */ |
| 2291 | list_for_each_entry(next, &s->proxy->tcpcheck_rules, list) { |
| 2292 | if (next->list.p == &cur->list) |
| 2293 | break; |
| 2294 | ++i; |
| 2295 | } |
| 2296 | |
| 2297 | return i; |
| 2298 | } |
| 2299 | |
| 2300 | static void tcpcheck_main(struct connection *conn) |
| 2301 | { |
| 2302 | char *contentptr; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2303 | struct tcpcheck_rule *cur = NULL; |
| 2304 | int done = 0, ret = 0; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2305 | struct check *check = conn->owner; |
| 2306 | struct server *s = check->server; |
| 2307 | struct task *t = check->task; |
Willy Tarreau | ef95395 | 2014-10-02 14:30:14 +0200 | [diff] [blame] | 2308 | struct list *head = &s->proxy->tcpcheck_rules; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2309 | |
Willy Tarreau | ef95395 | 2014-10-02 14:30:14 +0200 | [diff] [blame] | 2310 | /* here, we know that the check is complete or that it failed */ |
| 2311 | if (check->result != CHK_RES_UNKNOWN) |
| 2312 | goto out_end_tcpcheck; |
| 2313 | |
| 2314 | /* We have 4 possibilities here : |
| 2315 | * 1. we've not yet attempted step 1, and step 1 is a connect, so no |
| 2316 | * connection attempt was made yet ; |
| 2317 | * 2. we've not yet attempted step 1, and step 1 is a not connect or |
| 2318 | * does not exist (no rule), so a connection attempt was made |
| 2319 | * before coming here. |
| 2320 | * 3. we're coming back after having started with step 1, so we may |
| 2321 | * be waiting for a connection attempt to complete. |
| 2322 | * 4. the connection + handshake are complete |
| 2323 | * |
| 2324 | * #2 and #3 are quite similar, we want both the connection and the |
| 2325 | * handshake to complete before going any further. Thus we must always |
| 2326 | * wait for a connection to complete unless we're before and existing |
| 2327 | * step 1. |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2328 | */ |
Willy Tarreau | ef95395 | 2014-10-02 14:30:14 +0200 | [diff] [blame] | 2329 | if ((!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE)) && |
| 2330 | (check->current_step || LIST_ISEMPTY(head))) { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2331 | /* we allow up to min(inter, timeout.connect) for a connection |
| 2332 | * to establish but only when timeout.check is set |
| 2333 | * as it may be to short for a full check otherwise |
| 2334 | */ |
| 2335 | while (tick_is_expired(t->expire, now_ms)) { |
| 2336 | int t_con; |
| 2337 | |
| 2338 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 2339 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 2340 | |
| 2341 | if (s->proxy->timeout.check) |
| 2342 | t->expire = tick_first(t->expire, t_con); |
| 2343 | } |
| 2344 | return; |
| 2345 | } |
| 2346 | |
Willy Tarreau | ef95395 | 2014-10-02 14:30:14 +0200 | [diff] [blame] | 2347 | /* special case: option tcp-check with no rule, a connect is enough */ |
| 2348 | if (LIST_ISEMPTY(head)) { |
| 2349 | set_server_check_status(check, HCHK_STATUS_L4OK, NULL); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2350 | goto out_end_tcpcheck; |
Willy Tarreau | ef95395 | 2014-10-02 14:30:14 +0200 | [diff] [blame] | 2351 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2352 | |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 2353 | /* no step means first step initialisation */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2354 | if (check->current_step == NULL) { |
Willy Tarreau | 213c678 | 2014-10-02 14:51:02 +0200 | [diff] [blame] | 2355 | check->last_started_step = NULL; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2356 | check->bo->p = check->bo->data; |
| 2357 | check->bo->o = 0; |
| 2358 | check->bi->p = check->bi->data; |
| 2359 | check->bi->i = 0; |
| 2360 | cur = check->current_step = LIST_ELEM(head->n, struct tcpcheck_rule *, list); |
| 2361 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 2362 | if (s->proxy->timeout.check) |
| 2363 | t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check); |
| 2364 | } |
| 2365 | /* keep on processing step */ |
| 2366 | else { |
| 2367 | cur = check->current_step; |
| 2368 | } |
| 2369 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2370 | /* It's only the rules which will enable send/recv */ |
| 2371 | __conn_data_stop_both(conn); |
| 2372 | |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2373 | while (1) { |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2374 | /* we have to try to flush the output buffer before reading, at the end, |
| 2375 | * or if we're about to send a string that does not fit in the remaining space. |
| 2376 | */ |
| 2377 | if (check->bo->o && |
| 2378 | (&cur->list == head || |
| 2379 | check->current_step->action != TCPCHK_ACT_SEND || |
| 2380 | check->current_step->string_len >= buffer_total_space(check->bo))) { |
| 2381 | |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 2382 | if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) { |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2383 | if (conn->flags & CO_FL_ERROR) { |
| 2384 | chk_report_conn_err(conn, errno, 0); |
| 2385 | __conn_data_stop_both(conn); |
| 2386 | goto out_end_tcpcheck; |
| 2387 | } |
| 2388 | goto out_need_io; |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2389 | } |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2390 | } |
| 2391 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2392 | /* did we reach the end ? If so, let's check that everything was sent */ |
| 2393 | if (&cur->list == head) { |
| 2394 | if (check->bo->o) |
| 2395 | goto out_need_io; |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2396 | break; |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2397 | } |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2398 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2399 | if (check->current_step->action == TCPCHK_ACT_CONNECT) { |
| 2400 | struct protocol *proto; |
| 2401 | struct xprt_ops *xprt; |
| 2402 | |
| 2403 | /* mark the step as started */ |
| 2404 | check->last_started_step = check->current_step; |
| 2405 | /* first, shut existing connection */ |
| 2406 | conn_force_close(conn); |
| 2407 | |
| 2408 | /* prepare new connection */ |
| 2409 | /* initialization */ |
| 2410 | conn_init(conn); |
| 2411 | conn_attach(conn, check, &check_conn_cb); |
| 2412 | conn->target = &s->obj_type; |
| 2413 | |
| 2414 | /* no client address */ |
| 2415 | clear_addr(&conn->addr.from); |
| 2416 | |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 2417 | if (is_addr(&s->check_common.addr)) { |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2418 | /* we'll connect to the check addr specified on the server */ |
| 2419 | conn->addr.to = s->check_common.addr; |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 2420 | proto = s->check_common.proto; |
| 2421 | } |
| 2422 | else { |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2423 | /* we'll connect to the addr on the server */ |
| 2424 | conn->addr.to = s->addr; |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 2425 | proto = s->proto; |
| 2426 | } |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2427 | |
| 2428 | /* port */ |
| 2429 | if (check->current_step->port) |
| 2430 | set_host_port(&conn->addr.to, check->current_step->port); |
| 2431 | else if (check->port) |
| 2432 | set_host_port(&conn->addr.to, check->port); |
| 2433 | |
| 2434 | #ifdef USE_OPENSSL |
| 2435 | if (check->current_step->conn_opts & TCPCHK_OPT_SSL) { |
| 2436 | xprt = &ssl_sock; |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2437 | } |
| 2438 | else { |
| 2439 | xprt = &raw_sock; |
| 2440 | } |
| 2441 | #else /* USE_OPENSSL */ |
| 2442 | xprt = &raw_sock; |
| 2443 | #endif /* USE_OPENSSL */ |
| 2444 | conn_prepare(conn, proto, xprt); |
| 2445 | |
| 2446 | ret = SN_ERR_INTERNAL; |
| 2447 | if (proto->connect) |
| 2448 | ret = proto->connect(conn, check->type, (check->type) ? 0 : 2); |
| 2449 | conn->flags |= CO_FL_WAKE_DATA; |
| 2450 | if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) { |
| 2451 | conn->send_proxy_ofs = 1; |
| 2452 | conn->flags |= CO_FL_SEND_PROXY; |
| 2453 | } |
| 2454 | |
| 2455 | /* It can return one of : |
| 2456 | * - SN_ERR_NONE if everything's OK |
| 2457 | * - SN_ERR_SRVTO if there are no more servers |
| 2458 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 2459 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 2460 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 2461 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 2462 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 2463 | * Note that we try to prevent the network stack from sending the ACK during the |
| 2464 | * connect() when a pure TCP check is used (without PROXY protocol). |
| 2465 | */ |
| 2466 | switch (ret) { |
| 2467 | case SN_ERR_NONE: |
| 2468 | /* we allow up to min(inter, timeout.connect) for a connection |
| 2469 | * to establish but only when timeout.check is set |
| 2470 | * as it may be to short for a full check otherwise |
| 2471 | */ |
| 2472 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
| 2473 | |
| 2474 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 2475 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 2476 | t->expire = tick_first(t->expire, t_con); |
| 2477 | } |
| 2478 | break; |
| 2479 | case SN_ERR_SRVTO: /* ETIMEDOUT */ |
| 2480 | case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */ |
| 2481 | chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s", |
| 2482 | tcpcheck_get_step_id(s), strerror(errno)); |
| 2483 | set_server_check_status(check, HCHK_STATUS_L4CON, trash.str); |
| 2484 | goto out_end_tcpcheck; |
| 2485 | case SN_ERR_PRXCOND: |
| 2486 | case SN_ERR_RESOURCE: |
| 2487 | case SN_ERR_INTERNAL: |
| 2488 | chunk_printf(&trash, "TCPCHK error establishing connection at step %d", |
| 2489 | tcpcheck_get_step_id(s)); |
| 2490 | set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str); |
| 2491 | goto out_end_tcpcheck; |
| 2492 | } |
| 2493 | |
| 2494 | /* allow next rule */ |
| 2495 | cur = (struct tcpcheck_rule *)cur->list.n; |
| 2496 | check->current_step = cur; |
| 2497 | |
| 2498 | /* don't do anything until the connection is established */ |
| 2499 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 2500 | /* update expire time, should be done by process_chk */ |
| 2501 | /* we allow up to min(inter, timeout.connect) for a connection |
| 2502 | * to establish but only when timeout.check is set |
| 2503 | * as it may be to short for a full check otherwise |
| 2504 | */ |
| 2505 | while (tick_is_expired(t->expire, now_ms)) { |
| 2506 | int t_con; |
| 2507 | |
| 2508 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
| 2509 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
| 2510 | |
| 2511 | if (s->proxy->timeout.check) |
| 2512 | t->expire = tick_first(t->expire, t_con); |
| 2513 | } |
| 2514 | return; |
| 2515 | } |
| 2516 | |
| 2517 | } /* end 'connect' */ |
| 2518 | else if (check->current_step->action == TCPCHK_ACT_SEND) { |
| 2519 | /* mark the step as started */ |
| 2520 | check->last_started_step = check->current_step; |
| 2521 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2522 | /* reset the read buffer */ |
| 2523 | if (*check->bi->data != '\0') { |
| 2524 | *check->bi->data = '\0'; |
| 2525 | check->bi->i = 0; |
| 2526 | } |
| 2527 | |
| 2528 | if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) { |
| 2529 | conn->flags |= CO_FL_ERROR; |
| 2530 | chk_report_conn_err(conn, 0, 0); |
| 2531 | goto out_end_tcpcheck; |
| 2532 | } |
| 2533 | |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2534 | if (check->current_step->string_len >= check->bo->size) { |
| 2535 | chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d", |
| 2536 | check->current_step->string_len, check->bo->size, |
| 2537 | tcpcheck_get_step_id(s)); |
| 2538 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2539 | goto out_end_tcpcheck; |
| 2540 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2541 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2542 | /* do not try to send if there is no space */ |
| 2543 | if (check->current_step->string_len >= buffer_total_space(check->bo)) |
| 2544 | continue; |
| 2545 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2546 | bo_putblk(check->bo, check->current_step->string, check->current_step->string_len); |
| 2547 | *check->bo->p = '\0'; /* to make gdb output easier to read */ |
| 2548 | |
Willy Tarreau | abca5b6 | 2013-12-06 14:19:25 +0100 | [diff] [blame] | 2549 | /* go to next rule and try to send */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2550 | cur = (struct tcpcheck_rule *)cur->list.n; |
| 2551 | check->current_step = cur; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2552 | } /* end 'send' */ |
Willy Tarreau | 98aec9f | 2013-12-06 16:16:41 +0100 | [diff] [blame] | 2553 | else if (check->current_step->action == TCPCHK_ACT_EXPECT) { |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 2554 | if (unlikely(check->result == CHK_RES_FAILED)) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2555 | goto out_end_tcpcheck; |
| 2556 | |
Willy Tarreau | 310987a | 2014-01-22 19:46:33 +0100 | [diff] [blame] | 2557 | if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) { |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2558 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) { |
| 2559 | done = 1; |
| 2560 | if ((conn->flags & CO_FL_ERROR) && !check->bi->i) { |
| 2561 | /* Report network errors only if we got no other data. Otherwise |
| 2562 | * we'll let the upper layers decide whether the response is OK |
| 2563 | * or not. It is very common that an RST sent by the server is |
| 2564 | * reported as an error just after the last data chunk. |
| 2565 | */ |
| 2566 | chk_report_conn_err(conn, errno, 0); |
| 2567 | goto out_end_tcpcheck; |
| 2568 | } |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2569 | } |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2570 | else |
| 2571 | goto out_need_io; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2572 | } |
| 2573 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2574 | /* mark the step as started */ |
| 2575 | check->last_started_step = check->current_step; |
| 2576 | |
| 2577 | |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2578 | /* Intermediate or complete response received. |
| 2579 | * Terminate string in check->bi->data buffer. |
| 2580 | */ |
| 2581 | if (check->bi->i < check->bi->size) { |
| 2582 | check->bi->data[check->bi->i] = '\0'; |
| 2583 | } |
| 2584 | else { |
| 2585 | check->bi->data[check->bi->i - 1] = '\0'; |
| 2586 | done = 1; /* buffer full, don't wait for more data */ |
| 2587 | } |
| 2588 | |
| 2589 | contentptr = check->bi->data; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2590 | |
| 2591 | /* Check that response body is not empty... */ |
Willy Tarreau | ec6b012 | 2014-05-13 17:57:29 +0200 | [diff] [blame] | 2592 | if (!check->bi->i) { |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2593 | if (!done) |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2594 | continue; |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2595 | |
| 2596 | /* empty response */ |
| 2597 | chunk_printf(&trash, "TCPCHK got an empty response at step %d", |
| 2598 | tcpcheck_get_step_id(s)); |
| 2599 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2600 | |
| 2601 | goto out_end_tcpcheck; |
| 2602 | } |
| 2603 | |
| 2604 | if (!done && (cur->string != NULL) && (check->bi->i < cur->string_len) ) |
Willy Tarreau | a970c28 | 2013-12-06 12:47:19 +0100 | [diff] [blame] | 2605 | continue; /* try to read more */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2606 | |
Willy Tarreau | a970c28 | 2013-12-06 12:47:19 +0100 | [diff] [blame] | 2607 | tcpcheck_expect: |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2608 | if (cur->string != NULL) |
Willy Tarreau | ec6b012 | 2014-05-13 17:57:29 +0200 | [diff] [blame] | 2609 | 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] | 2610 | else if (cur->expect_regex != NULL) |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 2611 | ret = regex_exec(cur->expect_regex, contentptr); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2612 | |
| 2613 | if (!ret && !done) |
Willy Tarreau | a970c28 | 2013-12-06 12:47:19 +0100 | [diff] [blame] | 2614 | continue; /* try to read more */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2615 | |
| 2616 | /* matched */ |
| 2617 | if (ret) { |
| 2618 | /* matched but we did not want to => ERROR */ |
| 2619 | if (cur->inverse) { |
| 2620 | /* we were looking for a string */ |
| 2621 | if (cur->string != NULL) { |
| 2622 | chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d", |
| 2623 | cur->string, tcpcheck_get_step_id(s)); |
| 2624 | } |
| 2625 | else { |
| 2626 | /* we were looking for a regex */ |
| 2627 | chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d", |
| 2628 | tcpcheck_get_step_id(s)); |
| 2629 | } |
| 2630 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2631 | goto out_end_tcpcheck; |
| 2632 | } |
| 2633 | /* matched and was supposed to => OK, next step */ |
| 2634 | else { |
| 2635 | cur = (struct tcpcheck_rule*)cur->list.n; |
| 2636 | check->current_step = cur; |
Willy Tarreau | 98aec9f | 2013-12-06 16:16:41 +0100 | [diff] [blame] | 2637 | if (check->current_step->action == TCPCHK_ACT_EXPECT) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2638 | goto tcpcheck_expect; |
| 2639 | __conn_data_stop_recv(conn); |
| 2640 | } |
| 2641 | } |
| 2642 | else { |
| 2643 | /* not matched */ |
| 2644 | /* not matched and was not supposed to => OK, next step */ |
| 2645 | if (cur->inverse) { |
| 2646 | cur = (struct tcpcheck_rule*)cur->list.n; |
| 2647 | check->current_step = cur; |
Willy Tarreau | 98aec9f | 2013-12-06 16:16:41 +0100 | [diff] [blame] | 2648 | if (check->current_step->action == TCPCHK_ACT_EXPECT) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2649 | goto tcpcheck_expect; |
| 2650 | __conn_data_stop_recv(conn); |
| 2651 | } |
| 2652 | /* not matched but was supposed to => ERROR */ |
| 2653 | else { |
| 2654 | /* we were looking for a string */ |
| 2655 | if (cur->string != NULL) { |
| 2656 | chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d", |
| 2657 | cur->string, tcpcheck_get_step_id(s)); |
| 2658 | } |
| 2659 | else { |
| 2660 | /* we were looking for a regex */ |
| 2661 | chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d", |
| 2662 | tcpcheck_get_step_id(s)); |
| 2663 | } |
| 2664 | set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str); |
| 2665 | goto out_end_tcpcheck; |
| 2666 | } |
| 2667 | } |
| 2668 | } /* end expect */ |
| 2669 | } /* end loop over double chained step list */ |
| 2670 | |
| 2671 | set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)"); |
| 2672 | goto out_end_tcpcheck; |
| 2673 | |
Willy Tarreau | fbe0edf | 2013-12-06 16:54:31 +0100 | [diff] [blame] | 2674 | out_need_io: |
| 2675 | if (check->bo->o) |
| 2676 | __conn_data_want_send(conn); |
| 2677 | |
| 2678 | if (check->current_step->action == TCPCHK_ACT_EXPECT) |
| 2679 | __conn_data_want_recv(conn); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2680 | return; |
| 2681 | |
| 2682 | out_end_tcpcheck: |
| 2683 | /* collect possible new errors */ |
| 2684 | if (conn->flags & CO_FL_ERROR) |
| 2685 | chk_report_conn_err(conn, 0, 0); |
| 2686 | |
Baptiste Assmann | 69e273f | 2013-12-11 00:52:19 +0100 | [diff] [blame] | 2687 | /* cleanup before leaving */ |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2688 | check->current_step = NULL; |
| 2689 | |
Willy Tarreau | 6aaa1b8 | 2013-12-11 17:09:34 +0100 | [diff] [blame] | 2690 | if (check->result == CHK_RES_FAILED) |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2691 | conn->flags |= CO_FL_ERROR; |
| 2692 | |
| 2693 | __conn_data_stop_both(conn); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2694 | |
| 2695 | return; |
| 2696 | } |
| 2697 | |
| 2698 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 2699 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2700 | * Local variables: |
| 2701 | * c-indent-level: 8 |
| 2702 | * c-basic-offset: 8 |
| 2703 | * End: |
| 2704 | */ |