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