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