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