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