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