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