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