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