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