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