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