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> |
| 18 | #include <stdio.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 19 | #include <stdlib.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 20 | #include <string.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 21 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | #include <sys/socket.h> |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 24 | #include <sys/types.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 25 | #include <netinet/in.h> |
Willy Tarreau | 1274bc4 | 2009-07-15 07:16:31 +0200 | [diff] [blame] | 26 | #include <netinet/tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | #include <arpa/inet.h> |
| 28 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 29 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/compat.h> |
| 31 | #include <common/config.h> |
| 32 | #include <common/mini-clist.h> |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 33 | #include <common/standard.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 34 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | |
| 36 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | |
| 38 | #include <proto/backend.h> |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 39 | #include <proto/checks.h> |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 40 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | #include <proto/fd.h> |
| 42 | #include <proto/log.h> |
| 43 | #include <proto/queue.h> |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 44 | #include <proto/port_range.h> |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 45 | #include <proto/proto_http.h> |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 46 | #include <proto/proto_tcp.h> |
Willy Tarreau | 2b5652f | 2006-12-31 17:46:05 +0100 | [diff] [blame] | 47 | #include <proto/proxy.h> |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 48 | #include <proto/raw_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 49 | #include <proto/server.h> |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 50 | #include <proto/session.h> |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 51 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | #include <proto/task.h> |
| 53 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 54 | static int httpchk_expect(struct server *s, int done); |
| 55 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 56 | static const struct check_status check_statuses[HCHK_STATUS_SIZE] = { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 57 | [HCHK_STATUS_UNKNOWN] = { SRV_CHK_UNKNOWN, "UNK", "Unknown" }, |
| 58 | [HCHK_STATUS_INI] = { SRV_CHK_UNKNOWN, "INI", "Initializing" }, |
| 59 | [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 60 | |
Willy Tarreau | d3aac70 | 2012-11-23 11:32:12 +0100 | [diff] [blame] | 61 | [HCHK_STATUS_HANA] = { SRV_CHK_FAILED, "HANA", "Health analyze" }, |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 62 | |
Willy Tarreau | d3aac70 | 2012-11-23 11:32:12 +0100 | [diff] [blame] | 63 | [HCHK_STATUS_SOCKERR] = { SRV_CHK_FAILED, "SOCKERR", "Socket error" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 64 | |
Willy Tarreau | d3aac70 | 2012-11-23 11:32:12 +0100 | [diff] [blame] | 65 | [HCHK_STATUS_L4OK] = { SRV_CHK_PASSED, "L4OK", "Layer4 check passed" }, |
| 66 | [HCHK_STATUS_L4TOUT] = { SRV_CHK_FAILED, "L4TOUT", "Layer4 timeout" }, |
| 67 | [HCHK_STATUS_L4CON] = { SRV_CHK_FAILED, "L4CON", "Layer4 connection problem" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 68 | |
Willy Tarreau | d3aac70 | 2012-11-23 11:32:12 +0100 | [diff] [blame] | 69 | [HCHK_STATUS_L6OK] = { SRV_CHK_PASSED, "L6OK", "Layer6 check passed" }, |
| 70 | [HCHK_STATUS_L6TOUT] = { SRV_CHK_FAILED, "L6TOUT", "Layer6 timeout" }, |
| 71 | [HCHK_STATUS_L6RSP] = { SRV_CHK_FAILED, "L6RSP", "Layer6 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 72 | |
Willy Tarreau | d3aac70 | 2012-11-23 11:32:12 +0100 | [diff] [blame] | 73 | [HCHK_STATUS_L7TOUT] = { SRV_CHK_FAILED, "L7TOUT", "Layer7 timeout" }, |
| 74 | [HCHK_STATUS_L7RSP] = { SRV_CHK_FAILED, "L7RSP", "Layer7 invalid response" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 75 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 76 | [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 77 | |
Willy Tarreau | d3aac70 | 2012-11-23 11:32:12 +0100 | [diff] [blame] | 78 | [HCHK_STATUS_L7OKD] = { SRV_CHK_PASSED, "L7OK", "Layer7 check passed" }, |
| 79 | [HCHK_STATUS_L7OKCD] = { SRV_CHK_PASSED | SRV_CHK_DISABLE, "L7OKC", "Layer7 check conditionally passed" }, |
| 80 | [HCHK_STATUS_L7STS] = { SRV_CHK_FAILED, "L7STS", "Layer7 wrong status" }, |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 83 | 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] | 84 | [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }}, |
| 85 | |
| 86 | [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }}, |
| 87 | [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }}, |
| 88 | |
| 89 | [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }}, |
| 90 | [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }}, |
| 91 | [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }}, |
| 92 | [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }}, |
| 93 | |
| 94 | [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }}, |
| 95 | [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }}, |
| 96 | [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }}, |
| 97 | }; |
| 98 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 99 | /* |
| 100 | * Convert check_status code to description |
| 101 | */ |
| 102 | const char *get_check_status_description(short check_status) { |
| 103 | |
| 104 | const char *desc; |
| 105 | |
| 106 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 107 | desc = check_statuses[check_status].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 108 | else |
| 109 | desc = NULL; |
| 110 | |
| 111 | if (desc && *desc) |
| 112 | return desc; |
| 113 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 114 | return check_statuses[HCHK_STATUS_UNKNOWN].desc; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Convert check_status code to short info |
| 119 | */ |
| 120 | const char *get_check_status_info(short check_status) { |
| 121 | |
| 122 | const char *info; |
| 123 | |
| 124 | if (check_status < HCHK_STATUS_SIZE) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 125 | info = check_statuses[check_status].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 126 | else |
| 127 | info = NULL; |
| 128 | |
| 129 | if (info && *info) |
| 130 | return info; |
| 131 | else |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 132 | return check_statuses[HCHK_STATUS_UNKNOWN].info; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 135 | const char *get_analyze_status(short analyze_status) { |
| 136 | |
| 137 | const char *desc; |
| 138 | |
| 139 | if (analyze_status < HANA_STATUS_SIZE) |
| 140 | desc = analyze_statuses[analyze_status].desc; |
| 141 | else |
| 142 | desc = NULL; |
| 143 | |
| 144 | if (desc && *desc) |
| 145 | return desc; |
| 146 | else |
| 147 | return analyze_statuses[HANA_STATUS_UNKNOWN].desc; |
| 148 | } |
| 149 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 150 | static void server_status_printf(struct chunk *msg, struct server *s, struct check *check, int xferred) { |
Willy Tarreau | 4426770 | 2011-10-28 15:35:33 +0200 | [diff] [blame] | 151 | if (s->track) |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 152 | chunk_appendf(msg, " via %s/%s", |
Willy Tarreau | 4426770 | 2011-10-28 15:35:33 +0200 | [diff] [blame] | 153 | s->track->proxy->id, s->track->id); |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 154 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 155 | if (check) { |
| 156 | chunk_appendf(msg, ", reason: %s", get_check_status_description(check->status)); |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 157 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 158 | if (check->status >= HCHK_STATUS_L57DATA) |
| 159 | chunk_appendf(msg, ", code: %d", check->code); |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 160 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 161 | if (*check->desc) { |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 162 | struct chunk src; |
| 163 | |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 164 | chunk_appendf(msg, ", info: \""); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 165 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 166 | chunk_initlen(&src, check->desc, 0, strlen(check->desc)); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 167 | chunk_asciiencode(msg, &src, '"'); |
| 168 | |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 169 | chunk_appendf(msg, "\""); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 172 | if (check->duration >= 0) |
| 173 | chunk_appendf(msg, ", check duration: %ldms", check->duration); |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Krzysztof Piotr Oledzki | 3bb0571 | 2010-09-27 13:10:50 +0200 | [diff] [blame] | 176 | if (xferred >= 0) { |
Krzysztof Piotr Oledzki | b16a607 | 2010-01-10 21:12:58 +0100 | [diff] [blame] | 177 | if (!(s->state & SRV_RUNNING)) |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 178 | chunk_appendf(msg, ". %d active and %d backup servers left.%s" |
Krzysztof Piotr Oledzki | 9f2b9d5 | 2010-01-11 13:16:27 +0100 | [diff] [blame] | 179 | " %d sessions active, %d requeued, %d remaining in queue", |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 180 | s->proxy->srv_act, s->proxy->srv_bck, |
| 181 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 182 | s->cur_sess, xferred, s->nbpend); |
| 183 | else |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 184 | chunk_appendf(msg, ". %d active and %d backup servers online.%s" |
Krzysztof Piotr Oledzki | 9f2b9d5 | 2010-01-11 13:16:27 +0100 | [diff] [blame] | 185 | " %d sessions requeued, %d total in queue", |
Krzysztof Piotr Oledzki | 99ab5f8 | 2009-09-27 17:28:21 +0200 | [diff] [blame] | 186 | s->proxy->srv_act, s->proxy->srv_bck, |
| 187 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 188 | xferred, s->nbpend); |
| 189 | } |
| 190 | } |
| 191 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 192 | /* |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 193 | * Set check->status, update check->duration and fill check->result with |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 194 | * an adequate SRV_CHK_* value. |
| 195 | * |
| 196 | * Show information in logs about failed health check if server is UP |
| 197 | * or succeeded health checks if server is DOWN. |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 198 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 199 | 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] | 200 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 201 | struct server *s = check->server; |
| 202 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 203 | if (status == HCHK_STATUS_START) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 204 | check->result = SRV_CHK_UNKNOWN; /* no result yet */ |
| 205 | check->desc[0] = '\0'; |
| 206 | check->start = now; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 207 | return; |
| 208 | } |
| 209 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 210 | if (!check->status) |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 211 | return; |
| 212 | |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 213 | if (desc && *desc) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 214 | strncpy(check->desc, desc, HCHK_DESC_LEN-1); |
| 215 | check->desc[HCHK_DESC_LEN-1] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 216 | } else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 217 | check->desc[0] = '\0'; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 218 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 219 | check->status = status; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 220 | if (check_statuses[status].result) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 221 | check->result = check_statuses[status].result; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 222 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 223 | if (status == HCHK_STATUS_HANA) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 224 | check->duration = -1; |
| 225 | else if (!tv_iszero(&check->start)) { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 226 | /* set_server_check_status() may be called more than once */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 227 | check->duration = tv_ms_elapsed(&check->start, &now); |
| 228 | tv_zero(&check->start); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Simon Horman | 2f1f955 | 2013-11-25 10:46:37 +0900 | [diff] [blame] | 231 | /* Failure to connect to the agent as a secondary check should not |
| 232 | * cause the server to be marked down. So only log status changes |
| 233 | * for HCHK_STATUS_* statuses */ |
| 234 | if (check == &s->agent && check->status < HCHK_STATUS_L7TOUT) |
| 235 | return; |
| 236 | |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 237 | if (s->proxy->options2 & PR_O2_LOGHCHKS && |
Simon Horman | 125d099 | 2013-02-24 17:23:38 +0900 | [diff] [blame] | 238 | (((check->health != 0) && (check->result & SRV_CHK_FAILED)) || |
Simon Horman | 8c3d0be | 2013-11-25 10:46:40 +0900 | [diff] [blame] | 239 | (((check->health != check->rise + check->fall - 1) || |
| 240 | (!s->uweight && !(s->state & SRV_DRAIN)) || |
| 241 | (s->uweight && (s->state & SRV_DRAIN))) && |
| 242 | (check->result & SRV_CHK_PASSED)) || |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 243 | ((s->state & SRV_GOINGDOWN) && !(check->result & SRV_CHK_DISABLE)) || |
| 244 | (!(s->state & SRV_GOINGDOWN) && (check->result & SRV_CHK_DISABLE)))) { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 245 | |
| 246 | int health, rise, fall, state; |
| 247 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 248 | chunk_reset(&trash); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 249 | |
| 250 | /* FIXME begin: calculate local version of the health/rise/fall/state */ |
Simon Horman | 125d099 | 2013-02-24 17:23:38 +0900 | [diff] [blame] | 251 | health = check->health; |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 252 | rise = check->rise; |
| 253 | fall = check->fall; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 254 | state = s->state; |
| 255 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 256 | if (check->result & SRV_CHK_FAILED) { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 257 | if (health > rise) { |
| 258 | health--; /* still good */ |
| 259 | } else { |
| 260 | if (health == rise) |
| 261 | state &= ~(SRV_RUNNING | SRV_GOINGDOWN); |
| 262 | |
| 263 | health = 0; |
| 264 | } |
| 265 | } |
| 266 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 267 | if (check->result & SRV_CHK_PASSED) { |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 268 | if (health < rise + fall - 1) { |
| 269 | health++; /* was bad, stays for a while */ |
| 270 | |
| 271 | if (health == rise) |
| 272 | state |= SRV_RUNNING; |
| 273 | |
| 274 | if (health >= rise) |
| 275 | health = rise + fall - 1; /* OK now */ |
| 276 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 277 | |
| 278 | /* clear consecutive_errors if observing is enabled */ |
| 279 | if (s->onerror) |
| 280 | s->consecutive_errors = 0; |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 281 | } |
| 282 | /* FIXME end: calculate local version of the health/rise/fall/state */ |
| 283 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 284 | chunk_appendf(&trash, |
| 285 | "Health check for %sserver %s/%s %s%s", |
| 286 | s->state & SRV_BACKUP ? "backup " : "", |
| 287 | s->proxy->id, s->id, |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 288 | (check->result & SRV_CHK_DISABLE)?"conditionally ":"", |
| 289 | (check->result & SRV_CHK_PASSED)?"succeeded":"failed"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 290 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 291 | server_status_printf(&trash, s, check, -1); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 292 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 293 | chunk_appendf(&trash, ", status: %d/%d %s", |
| 294 | (state & SRV_RUNNING) ? (health - rise + 1) : (health), |
| 295 | (state & SRV_RUNNING) ? (fall) : (rise), |
Simon Horman | 8c3d0be | 2013-11-25 10:46:40 +0900 | [diff] [blame] | 296 | (state & SRV_RUNNING)?(s->eweight?"UP":"DRAIN"):"DOWN"); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 297 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 298 | Warning("%s.\n", trash.str); |
| 299 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 300 | } |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 301 | } |
| 302 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 303 | /* sends a log message when a backend goes down, and also sets last |
| 304 | * change date. |
| 305 | */ |
| 306 | static void set_backend_down(struct proxy *be) |
| 307 | { |
| 308 | be->last_change = now.tv_sec; |
| 309 | be->down_trans++; |
| 310 | |
| 311 | Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id); |
| 312 | send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id); |
| 313 | } |
| 314 | |
| 315 | /* Redistribute pending connections when a server goes down. The number of |
| 316 | * connections redistributed is returned. |
| 317 | */ |
| 318 | static int redistribute_pending(struct server *s) |
| 319 | { |
| 320 | struct pendconn *pc, *pc_bck, *pc_end; |
| 321 | int xferred = 0; |
| 322 | |
| 323 | FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) { |
| 324 | struct session *sess = pc->sess; |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 325 | if ((sess->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP && |
| 326 | !(sess->flags & SN_FORCE_PRST)) { |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 327 | /* The REDISP option was specified. We will ignore |
| 328 | * cookie and force to balance or use the dispatcher. |
| 329 | */ |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 330 | |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 331 | /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 332 | sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 333 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 334 | pendconn_free(pc); |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 335 | task_wakeup(sess->task, TASK_WOKEN_RES); |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 336 | xferred++; |
| 337 | } |
| 338 | } |
| 339 | return xferred; |
| 340 | } |
| 341 | |
| 342 | /* Check for pending connections at the backend, and assign some of them to |
| 343 | * the server coming up. The server's weight is checked before being assigned |
| 344 | * connections it may not be able to handle. The total number of transferred |
| 345 | * connections is returned. |
| 346 | */ |
| 347 | static int check_for_pending(struct server *s) |
| 348 | { |
| 349 | int xferred; |
| 350 | |
| 351 | if (!s->eweight) |
| 352 | return 0; |
| 353 | |
| 354 | for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) { |
| 355 | struct session *sess; |
| 356 | struct pendconn *p; |
| 357 | |
| 358 | p = pendconn_from_px(s->proxy); |
| 359 | if (!p) |
| 360 | break; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 361 | p->sess->target = &s->obj_type; |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 362 | sess = p->sess; |
| 363 | pendconn_free(p); |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 364 | task_wakeup(sess->task, TASK_WOKEN_RES); |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 365 | } |
| 366 | return xferred; |
| 367 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 368 | |
Justin Karneges | eb2c24a | 2012-05-24 15:28:52 -0700 | [diff] [blame] | 369 | /* Shutdown all connections of a server. The caller must pass a termination |
| 370 | * code in <why>, which must be one of SN_ERR_* indicating the reason for the |
| 371 | * shutdown. |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 372 | */ |
Justin Karneges | eb2c24a | 2012-05-24 15:28:52 -0700 | [diff] [blame] | 373 | static void shutdown_sessions(struct server *srv, int why) |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 374 | { |
| 375 | struct session *session, *session_bck; |
| 376 | |
Willy Tarreau | a2a64e9 | 2011-09-07 23:01:56 +0200 | [diff] [blame] | 377 | list_for_each_entry_safe(session, session_bck, &srv->actconns, by_srv) |
| 378 | if (session->srv_conn == srv) |
Justin Karneges | eb2c24a | 2012-05-24 15:28:52 -0700 | [diff] [blame] | 379 | session_shutdown(session, why); |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 380 | } |
| 381 | |
Justin Karneges | eb2c24a | 2012-05-24 15:28:52 -0700 | [diff] [blame] | 382 | /* Shutdown all connections of all backup servers of a proxy. The caller must |
| 383 | * pass a termination code in <why>, which must be one of SN_ERR_* indicating |
| 384 | * the reason for the shutdown. |
| 385 | */ |
| 386 | static void shutdown_backup_sessions(struct proxy *px, int why) |
| 387 | { |
| 388 | struct server *srv; |
| 389 | |
| 390 | for (srv = px->srv; srv != NULL; srv = srv->next) |
| 391 | if (srv->state & SRV_BACKUP) |
| 392 | shutdown_sessions(srv, why); |
| 393 | } |
| 394 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 395 | /* Sets server <s> down, notifies by all available means, recounts the |
| 396 | * remaining servers on the proxy and transfers queued sessions whenever |
Willy Tarreau | 5af3a69 | 2007-07-24 23:32:33 +0200 | [diff] [blame] | 397 | * possible to other servers. It automatically recomputes the number of |
| 398 | * servers, but not the map. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 399 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 400 | void set_server_down(struct check *check) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 401 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 402 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 403 | struct server *srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 404 | int xferred; |
| 405 | |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 406 | if (s->state & SRV_MAINTAIN) { |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 407 | check->health = check->rise; |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 408 | } |
| 409 | |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 410 | if ((s->state & SRV_RUNNING && check->health == check->rise) || s->track) { |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 411 | int srv_was_paused = s->state & SRV_GOINGDOWN; |
Willy Tarreau | d64d225 | 2010-10-17 17:16:42 +0200 | [diff] [blame] | 412 | int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act; |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 413 | |
| 414 | s->last_change = now.tv_sec; |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 415 | s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN); |
Willy Tarreau | 9580d16 | 2012-05-19 19:07:40 +0200 | [diff] [blame] | 416 | if (s->proxy->lbprm.set_server_status_down) |
| 417 | s->proxy->lbprm.set_server_status_down(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 418 | |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 419 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
Justin Karneges | eb2c24a | 2012-05-24 15:28:52 -0700 | [diff] [blame] | 420 | shutdown_sessions(s, SN_ERR_DOWN); |
Simon Horman | e0d1bfb | 2011-06-21 14:34:58 +0900 | [diff] [blame] | 421 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 422 | /* we might have sessions queued on this server and waiting for |
| 423 | * a connection. Those which are redispatchable will be queued |
| 424 | * to another server or to the proxy itself. |
| 425 | */ |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 426 | xferred = redistribute_pending(s); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 427 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 428 | chunk_reset(&trash); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 429 | |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 430 | if (s->state & SRV_MAINTAIN) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 431 | chunk_appendf(&trash, |
| 432 | "%sServer %s/%s is DOWN for maintenance", s->state & SRV_BACKUP ? "Backup " : "", |
| 433 | s->proxy->id, s->id); |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 434 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 435 | chunk_appendf(&trash, |
| 436 | "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "", |
| 437 | s->proxy->id, s->id); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 438 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 439 | server_status_printf(&trash, s, |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 440 | ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : 0), |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 441 | xferred); |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 442 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 443 | Warning("%s.\n", trash.str); |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 444 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 445 | /* we don't send an alert if the server was previously paused */ |
| 446 | if (srv_was_paused) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 447 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 448 | else |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 449 | send_log(s->proxy, LOG_ALERT, "%s.\n", trash.str); |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 450 | |
Willy Tarreau | d64d225 | 2010-10-17 17:16:42 +0200 | [diff] [blame] | 451 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 452 | set_backend_down(s->proxy); |
| 453 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 454 | s->counters.down_trans++; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 455 | |
Krzysztof Piotr Oledzki | f39c71c | 2009-01-30 00:52:49 +0100 | [diff] [blame] | 456 | if (s->state & SRV_CHECKED) |
Willy Tarreau | bc16cd8 | 2013-11-27 16:52:23 +0100 | [diff] [blame^] | 457 | for (srv = s->tracknext; srv; srv = srv->tracknext) |
| 458 | if (!(srv->state & SRV_MAINTAIN)) |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 459 | /* Only notify tracking servers that are not already in maintenance. */ |
Willy Tarreau | bc16cd8 | 2013-11-27 16:52:23 +0100 | [diff] [blame^] | 460 | set_server_down(&srv->check); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 461 | } |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 462 | |
Simon Horman | 125d099 | 2013-02-24 17:23:38 +0900 | [diff] [blame] | 463 | check->health = 0; /* failure */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 464 | } |
| 465 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 466 | void set_server_up(struct check *check) { |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 467 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 468 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 469 | struct server *srv; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 470 | int xferred; |
Willy Tarreau | 4544678 | 2012-03-09 17:16:09 +0100 | [diff] [blame] | 471 | unsigned int old_state = s->state; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 472 | |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 473 | if (s->state & SRV_MAINTAIN) { |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 474 | check->health = check->rise; |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 475 | } |
| 476 | |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 477 | if ((s->check.health >= s->check.rise && s->agent.health >= s->agent.rise && |
| 478 | check->health == check->rise) || s->track) { |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 479 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 480 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 481 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 482 | s->proxy->last_change = now.tv_sec; |
| 483 | } |
| 484 | |
| 485 | if (s->last_change < now.tv_sec) // ignore negative times |
| 486 | s->down_time += now.tv_sec - s->last_change; |
| 487 | |
| 488 | s->last_change = now.tv_sec; |
| 489 | s->state |= SRV_RUNNING; |
Willy Tarreau | 4544678 | 2012-03-09 17:16:09 +0100 | [diff] [blame] | 490 | s->state &= ~SRV_MAINTAIN; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 491 | |
| 492 | if (s->slowstart > 0) { |
| 493 | s->state |= SRV_WARMINGUP; |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 494 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)))); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 495 | } |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 496 | |
| 497 | server_recalc_eweight(s); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 498 | |
Justin Karneges | eb2c24a | 2012-05-24 15:28:52 -0700 | [diff] [blame] | 499 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 500 | * and it's not a backup server and its effective weight is > 0, |
| 501 | * then it can accept new connections, so we shut down all sessions |
| 502 | * on all backup servers. |
| 503 | */ |
| 504 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 505 | !(s->state & SRV_BACKUP) && s->eweight) |
| 506 | shutdown_backup_sessions(s->proxy, SN_ERR_UP); |
| 507 | |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 508 | /* check if we can handle some connections queued at the proxy. We |
| 509 | * will take as many as we can handle. |
| 510 | */ |
| 511 | xferred = check_for_pending(s); |
| 512 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 513 | chunk_reset(&trash); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 514 | |
Willy Tarreau | 4544678 | 2012-03-09 17:16:09 +0100 | [diff] [blame] | 515 | if (old_state & SRV_MAINTAIN) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 516 | chunk_appendf(&trash, |
| 517 | "%sServer %s/%s is UP (leaving maintenance)", s->state & SRV_BACKUP ? "Backup " : "", |
| 518 | s->proxy->id, s->id); |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 519 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 520 | chunk_appendf(&trash, |
| 521 | "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "", |
| 522 | s->proxy->id, s->id); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 523 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 524 | server_status_printf(&trash, s, |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 525 | ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL), |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 526 | xferred); |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 527 | } |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 528 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 529 | Warning("%s.\n", trash.str); |
| 530 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 531 | |
Krzysztof Piotr Oledzki | f39c71c | 2009-01-30 00:52:49 +0100 | [diff] [blame] | 532 | if (s->state & SRV_CHECKED) |
Willy Tarreau | bc16cd8 | 2013-11-27 16:52:23 +0100 | [diff] [blame^] | 533 | for (srv = s->tracknext; srv; srv = srv->tracknext) |
| 534 | if (!(srv->state & SRV_MAINTAIN)) |
Cyril Bonté | cd19e51 | 2010-01-31 22:34:03 +0100 | [diff] [blame] | 535 | /* Only notify tracking servers if they're not in maintenance. */ |
Willy Tarreau | bc16cd8 | 2013-11-27 16:52:23 +0100 | [diff] [blame^] | 536 | set_server_up(&srv->check); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 537 | } |
| 538 | |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 539 | if (check->health >= check->rise) |
| 540 | check->health = check->rise + check->fall - 1; /* OK now */ |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 541 | |
| 542 | } |
| 543 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 544 | static void set_server_disabled(struct check *check) { |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 545 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 546 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 547 | struct server *srv; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 548 | int xferred; |
| 549 | |
| 550 | s->state |= SRV_GOINGDOWN; |
Willy Tarreau | 9580d16 | 2012-05-19 19:07:40 +0200 | [diff] [blame] | 551 | if (s->proxy->lbprm.set_server_status_down) |
| 552 | s->proxy->lbprm.set_server_status_down(s); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 553 | |
| 554 | /* we might have sessions queued on this server and waiting for |
| 555 | * a connection. Those which are redispatchable will be queued |
| 556 | * to another server or to the proxy itself. |
| 557 | */ |
| 558 | xferred = redistribute_pending(s); |
| 559 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 560 | chunk_reset(&trash); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 561 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 562 | chunk_appendf(&trash, |
| 563 | "Load-balancing on %sServer %s/%s is disabled", |
| 564 | s->state & SRV_BACKUP ? "Backup " : "", |
| 565 | s->proxy->id, s->id); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 566 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 567 | server_status_printf(&trash, s, |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 568 | ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL), |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 569 | xferred); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 570 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 571 | Warning("%s.\n", trash.str); |
| 572 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 573 | |
| 574 | if (!s->proxy->srv_bck && !s->proxy->srv_act) |
| 575 | set_backend_down(s->proxy); |
| 576 | |
Krzysztof Piotr Oledzki | f39c71c | 2009-01-30 00:52:49 +0100 | [diff] [blame] | 577 | if (s->state & SRV_CHECKED) |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 578 | for(srv = s->tracknext; srv; srv = srv->tracknext) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 579 | set_server_disabled(check); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 580 | } |
| 581 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 582 | static void set_server_enabled(struct check *check) { |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 583 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 584 | struct server *s = check->server; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 585 | struct server *srv; |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 586 | int xferred; |
| 587 | |
| 588 | s->state &= ~SRV_GOINGDOWN; |
Willy Tarreau | 9580d16 | 2012-05-19 19:07:40 +0200 | [diff] [blame] | 589 | if (s->proxy->lbprm.set_server_status_up) |
| 590 | s->proxy->lbprm.set_server_status_up(s); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 591 | |
| 592 | /* check if we can handle some connections queued at the proxy. We |
| 593 | * will take as many as we can handle. |
| 594 | */ |
| 595 | xferred = check_for_pending(s); |
| 596 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 597 | chunk_reset(&trash); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 598 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 599 | chunk_appendf(&trash, |
| 600 | "Load-balancing on %sServer %s/%s is enabled again", |
| 601 | s->state & SRV_BACKUP ? "Backup " : "", |
| 602 | s->proxy->id, s->id); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 603 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 604 | server_status_printf(&trash, s, |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 605 | ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL), |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 606 | xferred); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 607 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 608 | Warning("%s.\n", trash.str); |
| 609 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 610 | |
Krzysztof Piotr Oledzki | f39c71c | 2009-01-30 00:52:49 +0100 | [diff] [blame] | 611 | if (s->state & SRV_CHECKED) |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 612 | for(srv = s->tracknext; srv; srv = srv->tracknext) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 613 | set_server_enabled(check); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 614 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 615 | |
Simon Horman | d858306 | 2013-11-25 10:46:33 +0900 | [diff] [blame] | 616 | static void check_failed(struct check *check) |
| 617 | { |
| 618 | struct server *s = check->server; |
| 619 | |
Simon Horman | 2f1f955 | 2013-11-25 10:46:37 +0900 | [diff] [blame] | 620 | /* The agent secondary check should only cause a server to be marked |
| 621 | * as down if check->status is HCHK_STATUS_L7STS, which indicates |
| 622 | * that the agent returned "fail", "stopped" or "down". |
| 623 | * The implication here is that failure to connect to the agent |
| 624 | * as a secondary check should not cause the server to be marked |
| 625 | * down. */ |
| 626 | if (check == &s->agent && check->status != HCHK_STATUS_L7STS) |
| 627 | return; |
| 628 | |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 629 | if (check->health > check->rise) { |
Simon Horman | d858306 | 2013-11-25 10:46:33 +0900 | [diff] [blame] | 630 | check->health--; /* still good */ |
| 631 | s->counters.failed_checks++; |
| 632 | } |
| 633 | else |
| 634 | set_server_down(check); |
| 635 | } |
| 636 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 637 | void health_adjust(struct server *s, short status) |
| 638 | { |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 639 | int failed; |
| 640 | int expire; |
| 641 | |
| 642 | /* return now if observing nor health check is not enabled */ |
Willy Tarreau | 5b3a202 | 2012-09-28 15:01:02 +0200 | [diff] [blame] | 643 | if (!s->observe || !s->check.task) |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 644 | return; |
| 645 | |
| 646 | if (s->observe >= HANA_OBS_SIZE) |
| 647 | return; |
| 648 | |
Willy Tarreau | bb95666 | 2013-01-24 00:37:39 +0100 | [diff] [blame] | 649 | if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc) |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 650 | return; |
| 651 | |
| 652 | switch (analyze_statuses[status].lr[s->observe - 1]) { |
| 653 | case 1: |
| 654 | failed = 1; |
| 655 | break; |
| 656 | |
| 657 | case 2: |
| 658 | failed = 0; |
| 659 | break; |
| 660 | |
| 661 | default: |
| 662 | return; |
| 663 | } |
| 664 | |
| 665 | if (!failed) { |
| 666 | /* good: clear consecutive_errors */ |
| 667 | s->consecutive_errors = 0; |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | s->consecutive_errors++; |
| 672 | |
| 673 | if (s->consecutive_errors < s->consecutive_errors_limit) |
| 674 | return; |
| 675 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 676 | chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s", |
| 677 | s->consecutive_errors, get_analyze_status(status)); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 678 | |
| 679 | switch (s->onerror) { |
| 680 | case HANA_ONERR_FASTINTER: |
| 681 | /* force fastinter - nothing to do here as all modes force it */ |
| 682 | break; |
| 683 | |
| 684 | case HANA_ONERR_SUDDTH: |
| 685 | /* simulate a pre-fatal failed health check */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 686 | if (s->check.health > s->check.rise) |
| 687 | s->check.health = s->check.rise + 1; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 688 | |
| 689 | /* no break - fall through */ |
| 690 | |
| 691 | case HANA_ONERR_FAILCHK: |
| 692 | /* simulate a failed health check */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 693 | set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str); |
Simon Horman | d858306 | 2013-11-25 10:46:33 +0900 | [diff] [blame] | 694 | check_failed(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 695 | |
| 696 | break; |
| 697 | |
| 698 | case HANA_ONERR_MARKDWN: |
| 699 | /* mark server down */ |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 700 | s->check.health = s->check.rise; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 701 | set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str); |
| 702 | set_server_down(&s->check); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 703 | |
| 704 | break; |
| 705 | |
| 706 | default: |
| 707 | /* write a warning? */ |
| 708 | break; |
| 709 | } |
| 710 | |
| 711 | s->consecutive_errors = 0; |
| 712 | s->counters.failed_hana++; |
| 713 | |
Simon Horman | 6618300 | 2013-02-23 10:16:43 +0900 | [diff] [blame] | 714 | if (s->check.fastinter) { |
| 715 | expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter)); |
Sergiy Prykhodko | 1d57e50 | 2013-09-21 12:05:00 +0300 | [diff] [blame] | 716 | if (s->check.task->expire > expire) { |
Willy Tarreau | 5b3a202 | 2012-09-28 15:01:02 +0200 | [diff] [blame] | 717 | s->check.task->expire = expire; |
Sergiy Prykhodko | 1d57e50 | 2013-09-21 12:05:00 +0300 | [diff] [blame] | 718 | /* requeue check task with new expire */ |
| 719 | task_queue(s->check.task); |
| 720 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 724 | static int httpchk_build_status_header(struct server *s, char *buffer) |
| 725 | { |
| 726 | int sv_state; |
| 727 | int ratio; |
| 728 | int hlen = 0; |
| 729 | const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d", |
| 730 | "UP %d/%d", "UP", |
| 731 | "NOLB %d/%d", "NOLB", |
| 732 | "no check" }; |
| 733 | |
| 734 | memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24); |
| 735 | hlen += 24; |
| 736 | |
| 737 | if (!(s->state & SRV_CHECKED)) |
| 738 | sv_state = 6; /* should obviously never happen */ |
| 739 | else if (s->state & SRV_RUNNING) { |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 740 | if (s->check.health == s->check.rise + s->check.fall - 1) |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 741 | sv_state = 3; /* UP */ |
| 742 | else |
| 743 | sv_state = 2; /* going down */ |
| 744 | |
| 745 | if (s->state & SRV_GOINGDOWN) |
| 746 | sv_state += 2; |
| 747 | } else { |
Simon Horman | 125d099 | 2013-02-24 17:23:38 +0900 | [diff] [blame] | 748 | if (s->check.health) |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 749 | sv_state = 1; /* going up */ |
| 750 | else |
| 751 | sv_state = 0; /* DOWN */ |
| 752 | } |
| 753 | |
| 754 | hlen += sprintf(buffer + hlen, |
| 755 | srv_hlt_st[sv_state], |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 756 | (s->state & SRV_RUNNING) ? (s->check.health - s->check.rise + 1) : (s->check.health), |
| 757 | (s->state & SRV_RUNNING) ? (s->check.fall) : (s->check.rise)); |
Willy Tarreau | ef78104 | 2010-01-27 11:53:01 +0100 | [diff] [blame] | 758 | |
| 759 | hlen += sprintf(buffer + hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d", |
| 760 | s->proxy->id, s->id, |
| 761 | global.node, |
| 762 | (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 763 | (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, |
| 764 | s->cur_sess, s->proxy->beconn - s->proxy->nbpend, |
| 765 | s->nbpend); |
| 766 | |
| 767 | if ((s->state & SRV_WARMINGUP) && |
| 768 | now.tv_sec < s->last_change + s->slowstart && |
| 769 | now.tv_sec >= s->last_change) { |
| 770 | ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart); |
| 771 | hlen += sprintf(buffer + hlen, "; throttle=%d%%", ratio); |
| 772 | } |
| 773 | |
| 774 | buffer[hlen++] = '\r'; |
| 775 | buffer[hlen++] = '\n'; |
| 776 | |
| 777 | return hlen; |
| 778 | } |
| 779 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 780 | /* |
| 781 | * This function is used only for server health-checks. It handles |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 782 | * the connection acknowledgement. If the proxy requires L7 health-checks, |
| 783 | * 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] | 784 | * to set check->status, check->duration and check->result. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 785 | */ |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 786 | static void event_srv_chk_w(struct connection *conn) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 787 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 788 | struct check *check = conn->owner; |
| 789 | struct server *s = check->server; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 790 | int fd = conn->t.sock.fd; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 791 | struct task *t = check->task; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 792 | |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 793 | if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 794 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 795 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 796 | if (unlikely(conn->flags & CO_FL_ERROR)) { |
Krzysztof Piotr Oledzki | 6492db5 | 2010-01-02 22:03:01 +0100 | [diff] [blame] | 797 | int skerr, err = errno; |
| 798 | socklen_t lskerr = sizeof(skerr); |
| 799 | |
| 800 | if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) && skerr) |
| 801 | err = skerr; |
| 802 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 803 | set_server_check_status(check, HCHK_STATUS_L4CON, strerror(err)); |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 804 | goto out_error; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 805 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 806 | |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 807 | if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_WR)) |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 808 | return; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 809 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 810 | /* here, we know that the connection is established */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 811 | if (!(check->result & SRV_CHK_FAILED)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 812 | /* we don't want to mark 'UP' a server on which we detected an error earlier */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 813 | if (check->bo->o) { |
| 814 | conn->xprt->snd_buf(conn, check->bo, MSG_DONTWAIT | MSG_NOSIGNAL); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 815 | if (conn->flags & CO_FL_ERROR) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 816 | set_server_check_status(check, HCHK_STATUS_L4CON, strerror(errno)); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 817 | goto out_wakeup; |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 818 | } |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 819 | if (check->bo->o) { |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 820 | goto out_incomplete; |
| 821 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 822 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 823 | |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 824 | /* full request sent, we allow up to <timeout.check> if nonzero for a response */ |
| 825 | if (s->proxy->timeout.check) { |
| 826 | t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check); |
| 827 | task_queue(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 828 | } |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 829 | goto out_nowake; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 830 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 831 | out_wakeup: |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 832 | task_wakeup(t, TASK_WOKEN_IO); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 833 | out_nowake: |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 834 | __conn_data_stop_send(conn); /* nothing more to write */ |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 835 | out_incomplete: |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 836 | return; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 837 | out_error: |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 838 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 839 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 840 | } |
| 841 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 842 | /* |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 843 | * 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] | 844 | * 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] | 845 | * set_server_check_status() to update check->status, check->duration |
| 846 | * and check->result. |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 847 | |
| 848 | * The set_server_check_status function is called with HCHK_STATUS_L7OKD if |
| 849 | * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server |
| 850 | * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in |
| 851 | * response to an SSL HELLO (the principle is that this is enough to |
| 852 | * distinguish between an SSL server and a pure TCP relay). All other cases will |
| 853 | * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP, |
| 854 | * etc. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 855 | */ |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 856 | static void event_srv_chk_r(struct connection *conn) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 857 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 858 | struct check *check = conn->owner; |
| 859 | struct server *s = check->server; |
| 860 | struct task *t = check->task; |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 861 | char *desc; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 862 | int done; |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 863 | unsigned short msglen; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 864 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 865 | if (unlikely((check->result & SRV_CHK_FAILED) || (conn->flags & CO_FL_ERROR))) { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 866 | /* in case of TCP only, this tells us if the connection failed */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 867 | if (!(check->result & SRV_CHK_FAILED)) |
| 868 | set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 869 | goto out_wakeup; |
| 870 | } |
| 871 | |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 872 | if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_RD)) |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 873 | return; |
| 874 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 875 | /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available |
| 876 | * but the connection was closed on the remote end. Fortunately, recv still |
| 877 | * works correctly and we don't need to do the getsockopt() on linux. |
| 878 | */ |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 879 | |
| 880 | /* Set buffer to point to the end of the data already read, and check |
| 881 | * that there is free space remaining. If the buffer is full, proceed |
| 882 | * with running the checks without attempting another socket read. |
| 883 | */ |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 884 | |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 885 | done = 0; |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 886 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 887 | conn->xprt->rcv_buf(conn, check->bi, check->bi->size); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 888 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) { |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 889 | done = 1; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 890 | if ((conn->flags & CO_FL_ERROR) && !check->bi->i) { |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 891 | /* Report network errors only if we got no other data. Otherwise |
| 892 | * we'll let the upper layers decide whether the response is OK |
| 893 | * or not. It is very common that an RST sent by the server is |
| 894 | * reported as an error just after the last data chunk. |
| 895 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 896 | if (!(check->result & SRV_CHK_FAILED)) |
| 897 | set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL); |
Willy Tarreau | c1a0796 | 2010-03-16 20:55:43 +0100 | [diff] [blame] | 898 | goto out_wakeup; |
| 899 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 900 | } |
| 901 | |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 902 | /* Intermediate or complete response received. |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 903 | * Terminate string in check->bi->data buffer. |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 904 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 905 | if (check->bi->i < check->bi->size) |
| 906 | check->bi->data[check->bi->i] = '\0'; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 907 | else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 908 | check->bi->data[check->bi->i - 1] = '\0'; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 909 | done = 1; /* buffer full, don't wait for more data */ |
| 910 | } |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 911 | |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 912 | /* Run the checks... */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 913 | switch (check->type) { |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 914 | case PR_O2_HTTP_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 915 | if (!done && check->bi->i < strlen("HTTP/1.0 000\r")) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 916 | goto wait_more_data; |
| 917 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 918 | /* Check if the server speaks HTTP 1.X */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 919 | if ((check->bi->i < strlen("HTTP/1.0 000\r")) || |
| 920 | (memcmp(check->bi->data, "HTTP/1.", 7) != 0 || |
| 921 | (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) || |
| 922 | !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) || |
| 923 | !isdigit((unsigned char) *(check->bi->data + 11))) { |
| 924 | cut_crlf(check->bi->data); |
| 925 | set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 926 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 927 | goto out_wakeup; |
| 928 | } |
| 929 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 930 | check->code = str2uic(check->bi->data + 9); |
| 931 | desc = ltrim(check->bi->data + 12, ' '); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 932 | |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 933 | if ((s->proxy->options & PR_O_DISABLE404) && |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 934 | (s->state & SRV_RUNNING) && (check->code == 404)) { |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 935 | /* 404 may be accepted as "stopping" only if the server was up */ |
| 936 | cut_crlf(desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 937 | set_server_check_status(check, HCHK_STATUS_L7OKCD, desc); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 938 | } |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 939 | else if (s->proxy->options2 & PR_O2_EXP_TYPE) { |
| 940 | /* Run content verification check... We know we have at least 13 chars */ |
| 941 | if (!httpchk_expect(s, done)) |
| 942 | goto wait_more_data; |
| 943 | } |
| 944 | /* check the reply : HTTP/1.X 2xx and 3xx are OK */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 945 | else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') { |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 946 | cut_crlf(desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 947 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 948 | } |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 949 | else { |
| 950 | cut_crlf(desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 951 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 952 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 953 | break; |
| 954 | |
| 955 | case PR_O2_SSL3_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 956 | if (!done && check->bi->i < 5) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 957 | goto wait_more_data; |
| 958 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 959 | /* Check for SSLv3 alert or handshake */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 960 | if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16)) |
| 961 | set_server_check_status(check, HCHK_STATUS_L6OK, NULL); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 962 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 963 | set_server_check_status(check, HCHK_STATUS_L6RSP, NULL); |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 964 | break; |
| 965 | |
| 966 | case PR_O2_SMTP_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 967 | if (!done && check->bi->i < strlen("000\r")) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 968 | goto wait_more_data; |
| 969 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 970 | /* Check if the server speaks SMTP */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 971 | if ((check->bi->i < strlen("000\r")) || |
| 972 | (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') || |
| 973 | !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) || |
| 974 | !isdigit((unsigned char) *(check->bi->data + 2))) { |
| 975 | cut_crlf(check->bi->data); |
| 976 | set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 977 | |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 978 | goto out_wakeup; |
| 979 | } |
| 980 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 981 | check->code = str2uic(check->bi->data); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 982 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 983 | desc = ltrim(check->bi->data + 3, ' '); |
Krzysztof Piotr Oledzki | f7089f5 | 2009-10-10 21:06:49 +0200 | [diff] [blame] | 984 | cut_crlf(desc); |
| 985 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 986 | /* Check for SMTP code 2xx (should be 250) */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 987 | if (*check->bi->data == '2') |
| 988 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Krzysztof Piotr Oledzki | 213014e | 2009-09-27 15:50:02 +0200 | [diff] [blame] | 989 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 990 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 991 | break; |
| 992 | |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 993 | case PR_O2_LB_AGENT_CHK: { |
| 994 | short status = HCHK_STATUS_L7RSP; |
| 995 | const char *desc = "Unknown feedback string"; |
| 996 | const char *down_cmd = NULL; |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 997 | int disabled; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 998 | |
| 999 | if (!done) |
| 1000 | goto wait_more_data; |
| 1001 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1002 | cut_crlf(check->bi->data); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1003 | |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 1004 | /* |
| 1005 | * The agent may have been disabled after a check was |
| 1006 | * initialised. If so, ignore weight changes and drain |
| 1007 | * settings from the agent. Note that the setting is |
| 1008 | * always present in the state of the agent the server, |
| 1009 | * regardless of if the agent is being run as a primary or |
| 1010 | * secondary check. That is, regardless of if the check |
| 1011 | * parameter of this function is the agent or check field |
| 1012 | * of the server. |
| 1013 | */ |
| 1014 | disabled = check->server->agent.state & CHK_STATE_DISABLED; |
| 1015 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1016 | if (strchr(check->bi->data, '%')) { |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 1017 | if (disabled) |
| 1018 | break; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1019 | desc = server_parse_weight_change_request(s, check->bi->data); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1020 | if (!desc) { |
| 1021 | status = HCHK_STATUS_L7OKD; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1022 | desc = check->bi->data; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1023 | } |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1024 | } else if (!strcasecmp(check->bi->data, "drain")) { |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 1025 | if (disabled) |
| 1026 | break; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1027 | desc = server_parse_weight_change_request(s, "0%"); |
| 1028 | if (!desc) { |
| 1029 | desc = "drain"; |
| 1030 | status = HCHK_STATUS_L7OKD; |
| 1031 | } |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1032 | } else if (!strncasecmp(check->bi->data, "down", strlen("down"))) { |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1033 | down_cmd = "down"; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1034 | } else if (!strncasecmp(check->bi->data, "stopped", strlen("stopped"))) { |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1035 | down_cmd = "stopped"; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1036 | } else if (!strncasecmp(check->bi->data, "fail", strlen("fail"))) { |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1037 | down_cmd = "fail"; |
| 1038 | } |
| 1039 | |
| 1040 | if (down_cmd) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1041 | const char *end = check->bi->data + strlen(down_cmd); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1042 | /* |
| 1043 | * The command keyword must terminated the string or |
| 1044 | * be followed by a blank. |
| 1045 | */ |
Willy Tarreau | 8b4c376 | 2013-02-13 12:47:12 +0100 | [diff] [blame] | 1046 | if (end[0] == '\0' || end[0] == ' ' || end[0] == '\t') { |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1047 | status = HCHK_STATUS_L7STS; |
Simon Horman | 80fefae | 2013-11-25 10:46:34 +0900 | [diff] [blame] | 1048 | desc = check->bi->data; |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1052 | set_server_check_status(check, status, desc); |
Simon Horman | 8c3d0be | 2013-11-25 10:46:40 +0900 | [diff] [blame] | 1053 | set_server_drain_state(check->server); |
Simon Horman | a2b9dad | 2013-02-12 10:45:54 +0900 | [diff] [blame] | 1054 | break; |
| 1055 | } |
| 1056 | |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1057 | case PR_O2_PGSQL_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1058 | if (!done && check->bi->i < 9) |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1059 | goto wait_more_data; |
| 1060 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1061 | if (check->bi->data[0] == 'R') { |
| 1062 | set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok"); |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1063 | } |
| 1064 | else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1065 | if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0)) |
| 1066 | desc = &check->bi->data[6]; |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1067 | else |
| 1068 | desc = "PostgreSQL unknown error"; |
| 1069 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1070 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Rauf Kuliyev | 38b4156 | 2011-01-04 15:14:13 +0100 | [diff] [blame] | 1071 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1072 | break; |
| 1073 | |
| 1074 | case PR_O2_REDIS_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1075 | if (!done && check->bi->i < 7) |
Hervé COMMOWICK | ec032d6 | 2011-08-05 16:23:48 +0200 | [diff] [blame] | 1076 | goto wait_more_data; |
| 1077 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1078 | if (strcmp(check->bi->data, "+PONG\r\n") == 0) { |
| 1079 | set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok"); |
Hervé COMMOWICK | ec032d6 | 2011-08-05 16:23:48 +0200 | [diff] [blame] | 1080 | } |
| 1081 | else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1082 | set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data); |
Hervé COMMOWICK | ec032d6 | 2011-08-05 16:23:48 +0200 | [diff] [blame] | 1083 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1084 | break; |
| 1085 | |
| 1086 | case PR_O2_MYSQL_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1087 | if (!done && check->bi->i < 5) |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1088 | goto wait_more_data; |
| 1089 | |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1090 | if (s->proxy->check_len == 0) { // old mode |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1091 | if (*(check->bi->data + 4) != '\xff') { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1092 | /* We set the MySQL Version in description for information purpose |
| 1093 | * FIXME : it can be cool to use MySQL Version for other purpose, |
| 1094 | * like mark as down old MySQL server. |
| 1095 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1096 | if (check->bi->i > 51) { |
| 1097 | desc = ltrim(check->bi->data + 5, ' '); |
| 1098 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1099 | } |
| 1100 | else { |
| 1101 | if (!done) |
| 1102 | goto wait_more_data; |
| 1103 | /* it seems we have a OK packet but without a valid length, |
| 1104 | * it must be a protocol error |
| 1105 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1106 | set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1107 | } |
Hervé COMMOWICK | 698ae00 | 2010-01-12 09:25:13 +0100 | [diff] [blame] | 1108 | } |
| 1109 | else { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1110 | /* An error message is attached in the Error packet */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1111 | desc = ltrim(check->bi->data + 7, ' '); |
| 1112 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1113 | } |
| 1114 | } else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1115 | unsigned int first_packet_len = ((unsigned int) *check->bi->data) + |
| 1116 | (((unsigned int) *(check->bi->data + 1)) << 8) + |
| 1117 | (((unsigned int) *(check->bi->data + 2)) << 16); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1118 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1119 | if (check->bi->i == first_packet_len + 4) { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1120 | /* MySQL Error packet always begin with field_count = 0xff */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1121 | if (*(check->bi->data + 4) != '\xff') { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1122 | /* We have only one MySQL packet and it is a Handshake Initialization packet |
| 1123 | * but we need to have a second packet to know if it is alright |
| 1124 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1125 | if (!done && check->bi->i < first_packet_len + 5) |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1126 | goto wait_more_data; |
| 1127 | } |
| 1128 | else { |
| 1129 | /* We have only one packet and it is an Error packet, |
| 1130 | * an error message is attached, so we can display it |
| 1131 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1132 | desc = &check->bi->data[7]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1133 | //Warning("onlyoneERR: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1134 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1135 | } |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1136 | } else if (check->bi->i > first_packet_len + 4) { |
| 1137 | unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) + |
| 1138 | (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) + |
| 1139 | (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1140 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1141 | if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1142 | /* We have 2 packets and that's good */ |
| 1143 | /* Check if the second packet is a MySQL Error packet or not */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1144 | if (*(check->bi->data + first_packet_len + 8) != '\xff') { |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1145 | /* No error packet */ |
| 1146 | /* We set the MySQL Version in description for information purpose */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1147 | desc = &check->bi->data[5]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1148 | //Warning("2packetOK: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1149 | set_server_check_status(check, HCHK_STATUS_L7OKD, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1150 | } |
| 1151 | else { |
| 1152 | /* An error message is attached in the Error packet |
| 1153 | * so we can display it ! :) |
| 1154 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1155 | desc = &check->bi->data[first_packet_len+11]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1156 | //Warning("2packetERR: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1157 | set_server_check_status(check, HCHK_STATUS_L7STS, desc); |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | else { |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1162 | if (!done) |
| 1163 | goto wait_more_data; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1164 | /* 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] | 1165 | * it must be a protocol error |
| 1166 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1167 | desc = &check->bi->data[5]; |
Hervé COMMOWICK | 8776f1b | 2010-10-18 15:58:36 +0200 | [diff] [blame] | 1168 | //Warning("protoerr: %s\n", desc); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1169 | set_server_check_status(check, HCHK_STATUS_L7RSP, desc); |
Hervé COMMOWICK | 698ae00 | 2010-01-12 09:25:13 +0100 | [diff] [blame] | 1170 | } |
| 1171 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1172 | break; |
| 1173 | |
| 1174 | case PR_O2_LDAP_CHK: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1175 | if (!done && check->bi->i < 14) |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1176 | goto wait_more_data; |
| 1177 | |
| 1178 | /* Check if the server speaks LDAP (ASN.1/BER) |
| 1179 | * http://en.wikipedia.org/wiki/Basic_Encoding_Rules |
| 1180 | * http://tools.ietf.org/html/rfc4511 |
| 1181 | */ |
| 1182 | |
| 1183 | /* http://tools.ietf.org/html/rfc4511#section-4.1.1 |
| 1184 | * LDAPMessage: 0x30: SEQUENCE |
| 1185 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1186 | if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) { |
| 1187 | set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1188 | } |
| 1189 | else { |
| 1190 | /* size of LDAPMessage */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1191 | msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0; |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1192 | |
| 1193 | /* http://tools.ietf.org/html/rfc4511#section-4.2.2 |
| 1194 | * messageID: 0x02 0x01 0x01: INTEGER 1 |
| 1195 | * protocolOp: 0x61: bindResponse |
| 1196 | */ |
| 1197 | if ((msglen > 2) || |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1198 | (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) { |
| 1199 | set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1200 | |
| 1201 | goto out_wakeup; |
| 1202 | } |
| 1203 | |
| 1204 | /* size of bindResponse */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1205 | 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] | 1206 | |
| 1207 | /* http://tools.ietf.org/html/rfc4511#section-4.1.9 |
| 1208 | * ldapResult: 0x0a 0x01: ENUMERATION |
| 1209 | */ |
| 1210 | if ((msglen > 4) || |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1211 | (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) { |
| 1212 | set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1213 | |
| 1214 | goto out_wakeup; |
| 1215 | } |
| 1216 | |
| 1217 | /* http://tools.ietf.org/html/rfc4511#section-4.1.9 |
| 1218 | * resultCode |
| 1219 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1220 | check->code = *(check->bi->data + msglen + 9); |
| 1221 | if (check->code) { |
| 1222 | 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] | 1223 | } else { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1224 | set_server_check_status(check, HCHK_STATUS_L7OKD, "Success"); |
Gabor Lekeny | b4c81e4 | 2010-09-29 18:17:05 +0200 | [diff] [blame] | 1225 | } |
| 1226 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1227 | break; |
| 1228 | |
| 1229 | default: |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1230 | /* other checks are valid if the connection succeeded anyway */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1231 | set_server_check_status(check, HCHK_STATUS_L4OK, NULL); |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 1232 | break; |
| 1233 | } /* switch */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 1234 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 1235 | out_wakeup: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1236 | if (check->result & SRV_CHK_FAILED) |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1237 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1238 | |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 1239 | /* Reset the check buffer... */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1240 | *check->bi->data = '\0'; |
| 1241 | check->bi->i = 0; |
Nick Chalk | 57b1bf7 | 2010-03-16 15:50:46 +0000 | [diff] [blame] | 1242 | |
Willy Tarreau | fd29cc5 | 2012-11-23 09:18:20 +0100 | [diff] [blame] | 1243 | /* Close the connection... We absolutely want to perform a hard close |
| 1244 | * and reset the connection if some data are pending, otherwise we end |
| 1245 | * up with many TIME_WAITs and eat all the source port range quickly. |
| 1246 | * To avoid sending RSTs all the time, we first try to drain pending |
| 1247 | * data. |
| 1248 | */ |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1249 | if (conn->xprt && conn->xprt->shutw) |
| 1250 | conn->xprt->shutw(conn, 0); |
Willy Tarreau | 2b57cb8 | 2013-06-10 19:56:38 +0200 | [diff] [blame] | 1251 | |
| 1252 | if (conn->ctrl && !(conn->flags & CO_FL_SOCK_RD_SH)) { |
| 1253 | if (conn->flags & CO_FL_WAIT_RD || !conn->ctrl->drain || !conn->ctrl->drain(conn->t.sock.fd)) |
| 1254 | setsockopt(conn->t.sock.fd, SOL_SOCKET, SO_LINGER, |
| 1255 | (struct linger *) &nolinger, sizeof(struct linger)); |
Willy Tarreau | cfd97c6 | 2012-11-23 17:35:59 +0100 | [diff] [blame] | 1256 | } |
Willy Tarreau | a522f80 | 2012-11-23 08:56:35 +0100 | [diff] [blame] | 1257 | __conn_data_stop_both(conn); |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 1258 | task_wakeup(t, TASK_WOKEN_IO); |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 1259 | return; |
Willy Tarreau | 0393818 | 2010-03-17 21:52:07 +0100 | [diff] [blame] | 1260 | |
| 1261 | wait_more_data: |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1262 | __conn_data_poll_recv(conn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1263 | } |
| 1264 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1265 | /* |
| 1266 | * This function is used only for server health-checks. It handles connection |
| 1267 | * status updates including errors. If necessary, it wakes the check task up. |
| 1268 | * It always returns 0. |
| 1269 | */ |
| 1270 | static int wake_srv_chk(struct connection *conn) |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1271 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1272 | struct check *check = conn->owner; |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1273 | |
Willy Tarreau | 6c560da | 2012-11-24 11:14:45 +0100 | [diff] [blame] | 1274 | if (unlikely(conn->flags & CO_FL_ERROR)) { |
| 1275 | /* Note that we might as well have been woken up by a handshake handler */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1276 | if (check->result == SRV_CHK_UNKNOWN) |
| 1277 | check->result |= SRV_CHK_FAILED; |
Willy Tarreau | 6c560da | 2012-11-24 11:14:45 +0100 | [diff] [blame] | 1278 | __conn_data_stop_both(conn); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1279 | task_wakeup(check->task, TASK_WOKEN_IO); |
Willy Tarreau | 6c560da | 2012-11-24 11:14:45 +0100 | [diff] [blame] | 1280 | } |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1281 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1282 | if (check->result & (SRV_CHK_FAILED|SRV_CHK_PASSED)) |
Willy Tarreau | 2b199c9 | 2012-11-23 17:32:21 +0100 | [diff] [blame] | 1283 | conn_full_close(conn); |
Willy Tarreau | 3267d36 | 2012-08-17 23:53:56 +0200 | [diff] [blame] | 1284 | return 0; |
Willy Tarreau | 20bea42 | 2012-07-06 12:00:49 +0200 | [diff] [blame] | 1285 | } |
| 1286 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1287 | struct data_cb check_conn_cb = { |
| 1288 | .recv = event_srv_chk_r, |
| 1289 | .send = event_srv_chk_w, |
| 1290 | .wake = wake_srv_chk, |
| 1291 | }; |
| 1292 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1293 | /* |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1294 | * updates the server's weight during a warmup stage. Once the final weight is |
| 1295 | * reached, the task automatically stops. Note that any server status change |
| 1296 | * must have updated s->last_change accordingly. |
| 1297 | */ |
| 1298 | static struct task *server_warmup(struct task *t) |
| 1299 | { |
| 1300 | struct server *s = t->context; |
| 1301 | |
| 1302 | /* by default, plan on stopping the task */ |
| 1303 | t->expire = TICK_ETERNITY; |
| 1304 | if ((s->state & (SRV_RUNNING|SRV_WARMINGUP|SRV_MAINTAIN)) != (SRV_RUNNING|SRV_WARMINGUP)) |
| 1305 | return t; |
| 1306 | |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1307 | server_recalc_eweight(s); |
Willy Tarreau | 2e99390 | 2011-10-31 11:53:20 +0100 | [diff] [blame] | 1308 | |
| 1309 | /* probably that we can refill this server with a bit more connections */ |
| 1310 | check_for_pending(s); |
| 1311 | |
| 1312 | /* get back there in 1 second or 1/20th of the slowstart interval, |
| 1313 | * whichever is greater, resulting in small 5% steps. |
| 1314 | */ |
| 1315 | if (s->state & SRV_WARMINGUP) |
| 1316 | t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))); |
| 1317 | return t; |
| 1318 | } |
| 1319 | |
| 1320 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1321 | * manages a server health-check. Returns |
| 1322 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 1323 | */ |
Simon Horman | 63a4a82 | 2012-03-19 07:24:41 +0900 | [diff] [blame] | 1324 | static struct task *process_chk(struct task *t) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1325 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1326 | struct check *check = t->context; |
| 1327 | struct server *s = check->server; |
| 1328 | struct connection *conn = check->conn; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1329 | int rv; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1330 | int ret; |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1331 | int expired = tick_is_expired(t->expire, now_ms); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1332 | |
Simon Horman | cd5d7b6 | 2013-02-24 17:23:38 +0900 | [diff] [blame] | 1333 | if (!(check->state & CHK_STATE_RUNNING)) { |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1334 | /* no check currently running */ |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1335 | if (!expired) /* woke up too early */ |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1336 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1337 | |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 1338 | /* we don't send any health-checks when the proxy is |
| 1339 | * stopped, the server should not be checked or the check |
| 1340 | * is disabled. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1341 | */ |
Simon Horman | 671b6f0 | 2013-11-25 10:46:39 +0900 | [diff] [blame] | 1342 | if (!(s->state & SRV_CHECKED) || |
| 1343 | s->proxy->state == PR_STSTOPPED || |
| 1344 | (s->state & SRV_MAINTAIN) || |
| 1345 | (check->state & CHK_STATE_DISABLED)) |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1346 | goto reschedule; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1347 | |
| 1348 | /* we'll initiate a new check */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1349 | set_server_check_status(check, HCHK_STATUS_START, NULL); |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1350 | |
Simon Horman | cd5d7b6 | 2013-02-24 17:23:38 +0900 | [diff] [blame] | 1351 | check->state |= CHK_STATE_RUNNING; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1352 | check->bi->p = check->bi->data; |
| 1353 | check->bi->i = 0; |
| 1354 | check->bo->p = check->bo->data; |
| 1355 | check->bo->o = 0; |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1356 | |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 1357 | /* prepare the check buffer |
| 1358 | * This should not be used if check is the secondary agent check |
| 1359 | * of a server as s->proxy->check_req will relate to the |
| 1360 | * configuration of the primary check */ |
| 1361 | if (check->type && check != &s->agent) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1362 | bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len); |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1363 | |
| 1364 | /* we want to check if this host replies to HTTP or SSLv3 requests |
| 1365 | * so we'll send the request, and won't wake the checker up now. |
| 1366 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1367 | if ((check->type) == PR_O2_SSL3_CHK) { |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1368 | /* SSL requires that we put Unix time in the request */ |
| 1369 | int gmt_time = htonl(date.tv_sec); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1370 | memcpy(check->bo->data + 11, &gmt_time, 4); |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1371 | } |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1372 | else if ((check->type) == PR_O2_HTTP_CHK) { |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1373 | if (s->proxy->options2 & PR_O2_CHK_SNDST) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1374 | bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str)); |
| 1375 | bo_putstr(check->bo, "\r\n"); |
| 1376 | *check->bo->p = '\0'; /* to make gdb output easier to read */ |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | /* prepare a new connection */ |
Willy Tarreau | b63b596 | 2012-11-23 16:22:08 +0100 | [diff] [blame] | 1381 | conn->flags = CO_FL_NONE; |
Willy Tarreau | 14cba4b | 2012-11-30 17:33:05 +0100 | [diff] [blame] | 1382 | conn->err_code = CO_ER_NONE; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1383 | conn->target = &s->obj_type; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1384 | conn_prepare(conn, &check_conn_cb, s->check_common.proto, s->check_common.xprt, check); |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 1385 | |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 1386 | /* no client address */ |
| 1387 | clear_addr(&conn->addr.from); |
| 1388 | |
Simon Horman | 6618300 | 2013-02-23 10:16:43 +0900 | [diff] [blame] | 1389 | if (is_addr(&s->check_common.addr)) |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1390 | /* we'll connect to the check addr specified on the server */ |
Simon Horman | 6618300 | 2013-02-23 10:16:43 +0900 | [diff] [blame] | 1391 | conn->addr.to = s->check_common.addr; |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1392 | else |
| 1393 | /* we'll connect to the addr on the server */ |
| 1394 | conn->addr.to = s->addr; |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 1395 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1396 | set_host_port(&conn->addr.to, check->port); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 1397 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1398 | /* It can return one of : |
| 1399 | * - SN_ERR_NONE if everything's OK |
| 1400 | * - SN_ERR_SRVTO if there are no more servers |
| 1401 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 1402 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1403 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1404 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 1405 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
Willy Tarreau | 24db47e | 2012-11-23 14:16:39 +0100 | [diff] [blame] | 1406 | * Note that we try to prevent the network stack from sending the ACK during the |
Willy Tarreau | f0837b2 | 2012-11-24 10:24:27 +0100 | [diff] [blame] | 1407 | * connect() when a pure TCP check is used (without PROXY protocol). |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1408 | */ |
Willy Tarreau | 8f46cca | 2013-03-04 20:07:44 +0100 | [diff] [blame] | 1409 | ret = SN_ERR_INTERNAL; |
Simon Horman | 6618300 | 2013-02-23 10:16:43 +0900 | [diff] [blame] | 1410 | if (s->check_common.proto->connect) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1411 | ret = s->check_common.proto->connect(conn, check->type, |
| 1412 | s->check.send_proxy ? 1 : (check->type) ? 0 : 2); |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1413 | conn->flags |= CO_FL_WAKE_DATA; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1414 | if (check->send_proxy) |
Willy Tarreau | 6c16adc | 2012-10-05 00:04:16 +0200 | [diff] [blame] | 1415 | conn->flags |= CO_FL_LOCAL_SPROXY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1416 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1417 | switch (ret) { |
| 1418 | case SN_ERR_NONE: |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1419 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1420 | * to establish but only when timeout.check is set |
| 1421 | * as it may be to short for a full check otherwise |
| 1422 | */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1423 | t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter)); |
Krzysztof Piotr Oledzki | 0960541 | 2009-09-23 22:09:24 +0200 | [diff] [blame] | 1424 | |
Willy Tarreau | fb56aab | 2012-09-28 14:40:02 +0200 | [diff] [blame] | 1425 | if (s->proxy->timeout.check && s->proxy->timeout.connect) { |
| 1426 | int t_con = tick_add(now_ms, s->proxy->timeout.connect); |
| 1427 | t->expire = tick_first(t->expire, t_con); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1428 | } |
Willy Tarreau | db3b4a2 | 2012-11-23 16:32:33 +0100 | [diff] [blame] | 1429 | conn_data_poll_recv(conn); /* prepare for reading a possible reply */ |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1430 | goto reschedule; |
| 1431 | |
| 1432 | case SN_ERR_SRVTO: /* ETIMEDOUT */ |
| 1433 | case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1434 | set_server_check_status(check, HCHK_STATUS_L4CON, strerror(errno)); |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1435 | break; |
| 1436 | case SN_ERR_PRXCOND: |
| 1437 | case SN_ERR_RESOURCE: |
| 1438 | case SN_ERR_INTERNAL: |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1439 | set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL); |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1440 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1441 | } |
| 1442 | |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1443 | /* here, we have seen a synchronous error, no fd was allocated */ |
Willy Tarreau | 6b0a850 | 2012-11-23 08:51:32 +0100 | [diff] [blame] | 1444 | |
Simon Horman | cd5d7b6 | 2013-02-24 17:23:38 +0900 | [diff] [blame] | 1445 | check->state &= ~CHK_STATE_RUNNING; |
Simon Horman | d858306 | 2013-11-25 10:46:33 +0900 | [diff] [blame] | 1446 | check_failed(check); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1447 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1448 | /* we allow up to min(inter, timeout.connect) for a connection |
| 1449 | * to establish but only when timeout.check is set |
| 1450 | * as it may be to short for a full check otherwise |
| 1451 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1452 | while (tick_is_expired(t->expire, now_ms)) { |
| 1453 | int t_con; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1454 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1455 | t_con = tick_add(t->expire, s->proxy->timeout.connect); |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1456 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1457 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1458 | if (s->proxy->timeout.check) |
| 1459 | t->expire = tick_first(t->expire, t_con); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 1460 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1461 | } |
| 1462 | else { |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1463 | /* there was a test running. |
| 1464 | * First, let's check whether there was an uncaught error, |
| 1465 | * which can happen on connect timeout or error. |
| 1466 | */ |
Simon Horman | 28b5ffc | 2013-02-24 07:25:29 +0900 | [diff] [blame] | 1467 | if (s->check.result == SRV_CHK_UNKNOWN) { |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1468 | if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) { |
| 1469 | /* L4 not established (yet) */ |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1470 | if (conn->flags & CO_FL_ERROR) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1471 | set_server_check_status(check, HCHK_STATUS_L4CON, NULL); |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1472 | else if (expired) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1473 | set_server_check_status(check, HCHK_STATUS_L4TOUT, NULL); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1474 | } |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1475 | else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) { |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1476 | /* L6 not established (yet) */ |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1477 | if (conn->flags & CO_FL_ERROR) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1478 | set_server_check_status(check, HCHK_STATUS_L6RSP, NULL); |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1479 | else if (expired) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1480 | set_server_check_status(check, HCHK_STATUS_L6TOUT, NULL); |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1481 | } |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1482 | else if (!check->type) { |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1483 | /* good connection is enough for pure TCP check */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1484 | if (check->use_ssl) |
| 1485 | set_server_check_status(check, HCHK_STATUS_L6OK, NULL); |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1486 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1487 | set_server_check_status(check, HCHK_STATUS_L4OK, NULL); |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1488 | } |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1489 | else if (expired) { |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1490 | /* connection established but expired check */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1491 | if (check->type == PR_O2_SSL3_CHK) |
| 1492 | set_server_check_status(check, HCHK_STATUS_L6TOUT, NULL); |
Willy Tarreau | acbdc7a | 2012-11-23 14:02:10 +0100 | [diff] [blame] | 1493 | else /* HTTP, SMTP, ... */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1494 | set_server_check_status(check, HCHK_STATUS_L7TOUT, NULL); |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1495 | |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1496 | } |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1497 | else |
| 1498 | goto out_wait; /* timeout not reached, wait again */ |
Willy Tarreau | f150317 | 2012-09-28 19:39:36 +0200 | [diff] [blame] | 1499 | } |
| 1500 | |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1501 | /* check complete or aborted */ |
Willy Tarreau | 5ba04f6 | 2013-02-12 15:23:12 +0100 | [diff] [blame] | 1502 | if (conn->xprt) { |
| 1503 | /* The check was aborted and the connection was not yet closed. |
| 1504 | * This can happen upon timeout, or when an external event such |
| 1505 | * as a failed response coupled with "observe layer7" caused the |
| 1506 | * server state to be suddenly changed. |
| 1507 | */ |
| 1508 | if (conn->ctrl) |
| 1509 | setsockopt(conn->t.sock.fd, SOL_SOCKET, SO_LINGER, |
| 1510 | (struct linger *) &nolinger, sizeof(struct linger)); |
| 1511 | conn_full_close(conn); |
| 1512 | } |
| 1513 | |
Simon Horman | d858306 | 2013-11-25 10:46:33 +0900 | [diff] [blame] | 1514 | if (check->result & SRV_CHK_FAILED) /* a failure or timeout detected */ |
| 1515 | check_failed(check); |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1516 | else { /* check was OK */ |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 1517 | /* we may have to add/remove this server from the LB group */ |
| 1518 | if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1519 | if ((s->state & SRV_GOINGDOWN) && !(check->result & SRV_CHK_DISABLE)) |
| 1520 | set_server_enabled(check); |
| 1521 | else if (!(s->state & SRV_GOINGDOWN) && (check->result & SRV_CHK_DISABLE)) |
| 1522 | set_server_disabled(check); |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 1523 | } |
| 1524 | |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 1525 | if (check->health < check->rise + check->fall - 1) { |
Simon Horman | 125d099 | 2013-02-24 17:23:38 +0900 | [diff] [blame] | 1526 | check->health++; /* was bad, stays for a while */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1527 | set_server_up(check); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1528 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1529 | } |
Simon Horman | cd5d7b6 | 2013-02-24 17:23:38 +0900 | [diff] [blame] | 1530 | check->state &= ~CHK_STATE_RUNNING; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1531 | |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1532 | rv = 0; |
| 1533 | if (global.spread_checks > 0) { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1534 | rv = srv_getinter(check) * global.spread_checks / 100; |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1535 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1536 | } |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1537 | 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] | 1538 | } |
Willy Tarreau | 5a78f36 | 2012-11-23 12:47:05 +0100 | [diff] [blame] | 1539 | |
| 1540 | reschedule: |
| 1541 | while (tick_is_expired(t->expire, now_ms)) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1542 | t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter)); |
Willy Tarreau | 74fa7fb | 2012-11-23 14:43:49 +0100 | [diff] [blame] | 1543 | out_wait: |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1544 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1545 | } |
| 1546 | |
Simon Horman | 5c94242 | 2013-11-25 10:46:32 +0900 | [diff] [blame] | 1547 | static int start_check_task(struct check *check, int mininter, |
| 1548 | int nbcheck, int srvpos) |
| 1549 | { |
| 1550 | struct task *t; |
| 1551 | /* task for the check */ |
| 1552 | if ((t = task_new()) == NULL) { |
| 1553 | Alert("Starting [%s:%s] check: out of memory.\n", |
| 1554 | check->server->proxy->id, check->server->id); |
| 1555 | return 0; |
| 1556 | } |
| 1557 | |
| 1558 | check->task = t; |
| 1559 | t->process = process_chk; |
| 1560 | t->context = check; |
| 1561 | |
| 1562 | /* check this every ms */ |
| 1563 | t->expire = tick_add(now_ms, |
| 1564 | MS_TO_TICKS(((mininter && |
| 1565 | mininter >= srv_getinter(check)) ? |
| 1566 | mininter : srv_getinter(check)) * srvpos / nbcheck)); |
| 1567 | check->start = now; |
| 1568 | task_queue(t); |
| 1569 | |
| 1570 | return 1; |
| 1571 | } |
| 1572 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1573 | /* |
| 1574 | * Start health-check. |
| 1575 | * Returns 0 if OK, -1 if error, and prints the error in this case. |
| 1576 | */ |
| 1577 | int start_checks() { |
| 1578 | |
| 1579 | struct proxy *px; |
| 1580 | struct server *s; |
| 1581 | struct task *t; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1582 | int nbcheck=0, mininter=0, srvpos=0; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1583 | |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 1584 | /* 1- count the checkers to run simultaneously. |
| 1585 | * We also determine the minimum interval among all of those which |
| 1586 | * have an interval larger than SRV_CHK_INTER_THRES. This interval |
| 1587 | * will be used to spread their start-up date. Those which have |
Jamie Gloudon | 801a0a3 | 2012-08-25 00:18:33 -0400 | [diff] [blame] | 1588 | * a shorter interval will start independently and will not dictate |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 1589 | * too short an interval for all others. |
| 1590 | */ |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1591 | for (px = proxy; px; px = px->next) { |
| 1592 | for (s = px->srv; s; s = s->next) { |
Willy Tarreau | e7b7348 | 2013-11-21 11:50:50 +0100 | [diff] [blame] | 1593 | if (s->slowstart) { |
| 1594 | if ((t = task_new()) == NULL) { |
| 1595 | Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 1596 | return -1; |
| 1597 | } |
| 1598 | /* We need a warmup task that will be called when the server |
| 1599 | * state switches from down to up. |
| 1600 | */ |
| 1601 | s->warmup = t; |
| 1602 | t->process = server_warmup; |
| 1603 | t->context = s; |
| 1604 | t->expire = TICK_ETERNITY; |
| 1605 | } |
| 1606 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1607 | if (!(s->state & SRV_CHECKED)) |
| 1608 | continue; |
| 1609 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1610 | if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) && |
| 1611 | (!mininter || mininter > srv_getinter(&s->check))) |
| 1612 | mininter = srv_getinter(&s->check); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1613 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1614 | nbcheck++; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1615 | } |
| 1616 | } |
| 1617 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1618 | if (!nbcheck) |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1619 | return 0; |
| 1620 | |
| 1621 | srand((unsigned)time(NULL)); |
| 1622 | |
| 1623 | /* |
| 1624 | * 2- start them as far as possible from each others. For this, we will |
| 1625 | * start them after their interval set to the min interval divided by |
| 1626 | * the number of servers, weighted by the server's position in the list. |
| 1627 | */ |
| 1628 | for (px = proxy; px; px = px->next) { |
| 1629 | for (s = px->srv; s; s = s->next) { |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 1630 | /* A task for the main check */ |
| 1631 | if (s->state & SRV_CHECKED) { |
| 1632 | if (!start_check_task(&s->check, mininter, nbcheck, srvpos)) |
| 1633 | return -1; |
| 1634 | srvpos++; |
| 1635 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1636 | |
Simon Horman | d60d691 | 2013-11-25 10:46:36 +0900 | [diff] [blame] | 1637 | /* A task for a auxiliary agent check */ |
| 1638 | if (s->state & SRV_AGENT_CHECKED) { |
| 1639 | if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) { |
| 1640 | return -1; |
| 1641 | } |
| 1642 | srvpos++; |
| 1643 | } |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 1644 | } |
| 1645 | } |
| 1646 | return 0; |
| 1647 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1648 | |
| 1649 | /* |
Willy Tarreau | 5b3a202 | 2012-09-28 15:01:02 +0200 | [diff] [blame] | 1650 | * Perform content verification check on data in s->check.buffer buffer. |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1651 | * The buffer MUST be terminated by a null byte before calling this function. |
| 1652 | * Sets server status appropriately. The caller is responsible for ensuring |
| 1653 | * that the buffer contains at least 13 characters. If <done> is zero, we may |
| 1654 | * return 0 to indicate that data is required to decide of a match. |
| 1655 | */ |
| 1656 | static int httpchk_expect(struct server *s, int done) |
| 1657 | { |
| 1658 | static char status_msg[] = "HTTP status check returned code <000>"; |
| 1659 | char status_code[] = "000"; |
| 1660 | char *contentptr; |
| 1661 | int crlf; |
| 1662 | int ret; |
| 1663 | |
| 1664 | switch (s->proxy->options2 & PR_O2_EXP_TYPE) { |
| 1665 | case PR_O2_EXP_STS: |
| 1666 | case PR_O2_EXP_RSTS: |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1667 | memcpy(status_code, s->check.bi->data + 9, 3); |
| 1668 | 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] | 1669 | |
| 1670 | if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS) |
| 1671 | ret = strncmp(s->proxy->expect_str, status_code, 3) == 0; |
| 1672 | else |
| 1673 | ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0; |
| 1674 | |
| 1675 | /* we necessarily have the response, so there are no partial failures */ |
| 1676 | if (s->proxy->options2 & PR_O2_EXP_INV) |
| 1677 | ret = !ret; |
| 1678 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1679 | 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] | 1680 | break; |
| 1681 | |
| 1682 | case PR_O2_EXP_STR: |
| 1683 | case PR_O2_EXP_RSTR: |
| 1684 | /* very simple response parser: ignore CR and only count consecutive LFs, |
| 1685 | * stop with contentptr pointing to first char after the double CRLF or |
| 1686 | * to '\0' if crlf < 2. |
| 1687 | */ |
| 1688 | crlf = 0; |
Willy Tarreau | 1ae1b7b | 2012-09-28 15:28:30 +0200 | [diff] [blame] | 1689 | for (contentptr = s->check.bi->data; *contentptr; contentptr++) { |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1690 | if (crlf >= 2) |
| 1691 | break; |
| 1692 | if (*contentptr == '\r') |
| 1693 | continue; |
| 1694 | else if (*contentptr == '\n') |
| 1695 | crlf++; |
| 1696 | else |
| 1697 | crlf = 0; |
| 1698 | } |
| 1699 | |
| 1700 | /* Check that response contains a body... */ |
| 1701 | if (crlf < 2) { |
| 1702 | if (!done) |
| 1703 | return 0; |
| 1704 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1705 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1706 | "HTTP content check could not find a response body"); |
| 1707 | return 1; |
| 1708 | } |
| 1709 | |
| 1710 | /* Check that response body is not empty... */ |
| 1711 | if (*contentptr == '\0') { |
Willy Tarreau | a164fb5 | 2011-04-13 09:32:41 +0200 | [diff] [blame] | 1712 | if (!done) |
| 1713 | return 0; |
| 1714 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1715 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1716 | "HTTP content check found empty response body"); |
| 1717 | return 1; |
| 1718 | } |
| 1719 | |
| 1720 | /* Check the response content against the supplied string |
| 1721 | * or regex... */ |
| 1722 | if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR) |
| 1723 | ret = strstr(contentptr, s->proxy->expect_str) != NULL; |
| 1724 | else |
| 1725 | ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0; |
| 1726 | |
| 1727 | /* if we don't match, we may need to wait more */ |
| 1728 | if (!ret && !done) |
| 1729 | return 0; |
| 1730 | |
| 1731 | if (ret) { |
| 1732 | /* content matched */ |
| 1733 | if (s->proxy->options2 & PR_O2_EXP_INV) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1734 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1735 | "HTTP check matched unwanted content"); |
| 1736 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1737 | set_server_check_status(&s->check, HCHK_STATUS_L7OKD, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1738 | "HTTP content check matched"); |
| 1739 | } |
| 1740 | else { |
| 1741 | if (s->proxy->options2 & PR_O2_EXP_INV) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1742 | set_server_check_status(&s->check, HCHK_STATUS_L7OKD, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1743 | "HTTP check did not match unwanted content"); |
| 1744 | else |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 1745 | set_server_check_status(&s->check, HCHK_STATUS_L7RSP, |
Willy Tarreau | bd74154 | 2010-03-16 18:46:54 +0100 | [diff] [blame] | 1746 | "HTTP content check did not match"); |
| 1747 | } |
| 1748 | break; |
| 1749 | } |
| 1750 | return 1; |
| 1751 | } |
| 1752 | |
| 1753 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1754 | * Local variables: |
| 1755 | * c-indent-level: 8 |
| 1756 | * c-basic-offset: 8 |
| 1757 | * End: |
| 1758 | */ |