blob: 56f353767d97898e0f0490d0c700c720ccc4bb4c [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Health-checks functions.
3 *
Willy Tarreau26c25062009-03-08 09:38:41 +01004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
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 Tarreaub8816082008-01-18 12:18:15 +010014#include <assert.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020015#include <ctype.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <errno.h>
17#include <fcntl.h>
18#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020019#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020021#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#include <unistd.h>
23#include <sys/socket.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040024#include <sys/types.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020025#include <netinet/in.h>
Willy Tarreau1274bc42009-07-15 07:16:31 +020026#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027#include <arpa/inet.h>
28
Willy Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
32#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020033#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
36#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020039#include <proto/checks.h>
Simon Hormana2b9dad2013-02-12 10:45:54 +090040#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041#include <proto/fd.h>
42#include <proto/log.h>
43#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020044#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010045#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010046#include <proto/proto_tcp.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010047#include <proto/proxy.h>
Willy Tarreaufb56aab2012-09-28 14:40:02 +020048#include <proto/raw_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/server.h>
Simon Hormane0d1bfb2011-06-21 14:34:58 +090050#include <proto/session.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010051#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/task.h>
53
Willy Tarreaubd741542010-03-16 18:46:54 +010054static int httpchk_expect(struct server *s, int done);
55
Simon Horman63a4a822012-03-19 07:24:41 +090056static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020057 [HCHK_STATUS_UNKNOWN] = { SRV_CHK_UNKNOWN, "UNK", "Unknown" },
58 [HCHK_STATUS_INI] = { SRV_CHK_UNKNOWN, "INI", "Initializing" },
59 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020060
Willy Tarreaud3aac702012-11-23 11:32:12 +010061 [HCHK_STATUS_HANA] = { SRV_CHK_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010062
Willy Tarreaud3aac702012-11-23 11:32:12 +010063 [HCHK_STATUS_SOCKERR] = { SRV_CHK_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020064
Willy Tarreaud3aac702012-11-23 11:32:12 +010065 [HCHK_STATUS_L4OK] = { SRV_CHK_PASSED, "L4OK", "Layer4 check passed" },
66 [HCHK_STATUS_L4TOUT] = { SRV_CHK_FAILED, "L4TOUT", "Layer4 timeout" },
67 [HCHK_STATUS_L4CON] = { SRV_CHK_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020068
Willy Tarreaud3aac702012-11-23 11:32:12 +010069 [HCHK_STATUS_L6OK] = { SRV_CHK_PASSED, "L6OK", "Layer6 check passed" },
70 [HCHK_STATUS_L6TOUT] = { SRV_CHK_FAILED, "L6TOUT", "Layer6 timeout" },
71 [HCHK_STATUS_L6RSP] = { SRV_CHK_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020072
Willy Tarreaud3aac702012-11-23 11:32:12 +010073 [HCHK_STATUS_L7TOUT] = { SRV_CHK_FAILED, "L7TOUT", "Layer7 timeout" },
74 [HCHK_STATUS_L7RSP] = { SRV_CHK_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020075
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020076 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020077
Willy Tarreaud3aac702012-11-23 11:32:12 +010078 [HCHK_STATUS_L7OKD] = { SRV_CHK_PASSED, "L7OK", "Layer7 check passed" },
79 [HCHK_STATUS_L7OKCD] = { SRV_CHK_PASSED | SRV_CHK_DISABLE, "L7OKC", "Layer7 check conditionally passed" },
80 [HCHK_STATUS_L7STS] = { SRV_CHK_FAILED, "L7STS", "Layer7 wrong status" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020081};
82
Simon Horman63a4a822012-03-19 07:24:41 +090083static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010084 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
85
86 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
87 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
88
89 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
90 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
91 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
92 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
93
94 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
95 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
96 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
97};
98
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020099/*
100 * Convert check_status code to description
101 */
102const char *get_check_status_description(short check_status) {
103
104 const char *desc;
105
106 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200107 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200108 else
109 desc = NULL;
110
111 if (desc && *desc)
112 return desc;
113 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200114 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200115}
116
117/*
118 * Convert check_status code to short info
119 */
120const char *get_check_status_info(short check_status) {
121
122 const char *info;
123
124 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200125 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200126 else
127 info = NULL;
128
129 if (info && *info)
130 return info;
131 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200132 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200133}
134
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100135const char *get_analyze_status(short analyze_status) {
136
137 const char *desc;
138
139 if (analyze_status < HANA_STATUS_SIZE)
140 desc = analyze_statuses[analyze_status].desc;
141 else
142 desc = NULL;
143
144 if (desc && *desc)
145 return desc;
146 else
147 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
148}
149
Simon Horman4a741432013-02-23 15:35:38 +0900150static void server_status_printf(struct chunk *msg, struct server *s, struct check *check, int xferred) {
Willy Tarreau44267702011-10-28 15:35:33 +0200151 if (s->track)
Willy Tarreau77804732012-10-29 16:14:26 +0100152 chunk_appendf(msg, " via %s/%s",
Willy Tarreau44267702011-10-28 15:35:33 +0200153 s->track->proxy->id, s->track->id);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200154
Simon Horman4a741432013-02-23 15:35:38 +0900155 if (check) {
156 chunk_appendf(msg, ", reason: %s", get_check_status_description(check->status));
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200157
Simon Horman4a741432013-02-23 15:35:38 +0900158 if (check->status >= HCHK_STATUS_L57DATA)
159 chunk_appendf(msg, ", code: %d", check->code);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200160
Simon Horman4a741432013-02-23 15:35:38 +0900161 if (*check->desc) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200162 struct chunk src;
163
Willy Tarreau77804732012-10-29 16:14:26 +0100164 chunk_appendf(msg, ", info: \"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200165
Simon Horman4a741432013-02-23 15:35:38 +0900166 chunk_initlen(&src, check->desc, 0, strlen(check->desc));
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200167 chunk_asciiencode(msg, &src, '"');
168
Willy Tarreau77804732012-10-29 16:14:26 +0100169 chunk_appendf(msg, "\"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200170 }
171
Simon Horman4a741432013-02-23 15:35:38 +0900172 if (check->duration >= 0)
173 chunk_appendf(msg, ", check duration: %ldms", check->duration);
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200174 }
175
Krzysztof Piotr Oledzki3bb05712010-09-27 13:10:50 +0200176 if (xferred >= 0) {
Krzysztof Piotr Oledzkib16a6072010-01-10 21:12:58 +0100177 if (!(s->state & SRV_RUNNING))
Willy Tarreau77804732012-10-29 16:14:26 +0100178 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100179 " %d sessions active, %d requeued, %d remaining in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200180 s->proxy->srv_act, s->proxy->srv_bck,
181 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
182 s->cur_sess, xferred, s->nbpend);
183 else
Willy Tarreau77804732012-10-29 16:14:26 +0100184 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
Krzysztof Piotr Oledzki9f2b9d52010-01-11 13:16:27 +0100185 " %d sessions requeued, %d total in queue",
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200186 s->proxy->srv_act, s->proxy->srv_bck,
187 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
188 xferred, s->nbpend);
189 }
190}
191
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200192/*
Simon Horman4a741432013-02-23 15:35:38 +0900193 * Set check->status, update check->duration and fill check->result with
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200194 * an adequate SRV_CHK_* value.
195 *
196 * Show information in logs about failed health check if server is UP
197 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200198 */
Simon Horman4a741432013-02-23 15:35:38 +0900199static void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100200{
Simon Horman4a741432013-02-23 15:35:38 +0900201 struct server *s = check->server;
202
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200203 if (status == HCHK_STATUS_START) {
Simon Horman4a741432013-02-23 15:35:38 +0900204 check->result = SRV_CHK_UNKNOWN; /* no result yet */
205 check->desc[0] = '\0';
206 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200207 return;
208 }
209
Simon Horman4a741432013-02-23 15:35:38 +0900210 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200211 return;
212
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200213 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900214 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
215 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200216 } else
Simon Horman4a741432013-02-23 15:35:38 +0900217 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200218
Simon Horman4a741432013-02-23 15:35:38 +0900219 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200220 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900221 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200222
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100223 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900224 check->duration = -1;
225 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200226 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900227 check->duration = tv_ms_elapsed(&check->start, &now);
228 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200229 }
230
Simon Horman2f1f9552013-11-25 10:46:37 +0900231 /* Failure to connect to the agent as a secondary check should not
232 * cause the server to be marked down. So only log status changes
233 * for HCHK_STATUS_* statuses */
234 if (check == &s->agent && check->status < HCHK_STATUS_L7TOUT)
235 return;
236
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200237 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
Simon Horman125d0992013-02-24 17:23:38 +0900238 (((check->health != 0) && (check->result & SRV_CHK_FAILED)) ||
Simon Horman8c3d0be2013-11-25 10:46:40 +0900239 (((check->health != check->rise + check->fall - 1) ||
240 (!s->uweight && !(s->state & SRV_DRAIN)) ||
241 (s->uweight && (s->state & SRV_DRAIN))) &&
242 (check->result & SRV_CHK_PASSED)) ||
Simon Horman4a741432013-02-23 15:35:38 +0900243 ((s->state & SRV_GOINGDOWN) && !(check->result & SRV_CHK_DISABLE)) ||
244 (!(s->state & SRV_GOINGDOWN) && (check->result & SRV_CHK_DISABLE)))) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200245
246 int health, rise, fall, state;
247
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100248 chunk_reset(&trash);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200249
250 /* FIXME begin: calculate local version of the health/rise/fall/state */
Simon Horman125d0992013-02-24 17:23:38 +0900251 health = check->health;
Simon Horman58c32972013-11-25 10:46:38 +0900252 rise = check->rise;
253 fall = check->fall;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200254 state = s->state;
255
Simon Horman4a741432013-02-23 15:35:38 +0900256 if (check->result & SRV_CHK_FAILED) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200257 if (health > rise) {
258 health--; /* still good */
259 } else {
260 if (health == rise)
261 state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
262
263 health = 0;
264 }
265 }
266
Simon Horman4a741432013-02-23 15:35:38 +0900267 if (check->result & SRV_CHK_PASSED) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200268 if (health < rise + fall - 1) {
269 health++; /* was bad, stays for a while */
270
271 if (health == rise)
272 state |= SRV_RUNNING;
273
274 if (health >= rise)
275 health = rise + fall - 1; /* OK now */
276 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100277
278 /* clear consecutive_errors if observing is enabled */
279 if (s->onerror)
280 s->consecutive_errors = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200281 }
282 /* FIXME end: calculate local version of the health/rise/fall/state */
283
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100284 chunk_appendf(&trash,
285 "Health check for %sserver %s/%s %s%s",
286 s->state & SRV_BACKUP ? "backup " : "",
287 s->proxy->id, s->id,
Simon Horman4a741432013-02-23 15:35:38 +0900288 (check->result & SRV_CHK_DISABLE)?"conditionally ":"",
289 (check->result & SRV_CHK_PASSED)?"succeeded":"failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200290
Simon Horman4a741432013-02-23 15:35:38 +0900291 server_status_printf(&trash, s, check, -1);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200292
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100293 chunk_appendf(&trash, ", status: %d/%d %s",
294 (state & SRV_RUNNING) ? (health - rise + 1) : (health),
295 (state & SRV_RUNNING) ? (fall) : (rise),
Simon Horman8c3d0be2013-11-25 10:46:40 +0900296 (state & SRV_RUNNING)?(s->eweight?"UP":"DRAIN"):"DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200297
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100298 Warning("%s.\n", trash.str);
299 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200300 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200301}
302
Willy Tarreau48494c02007-11-30 10:41:39 +0100303/* sends a log message when a backend goes down, and also sets last
304 * change date.
305 */
306static void set_backend_down(struct proxy *be)
307{
308 be->last_change = now.tv_sec;
309 be->down_trans++;
310
311 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
312 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
313}
314
315/* Redistribute pending connections when a server goes down. The number of
316 * connections redistributed is returned.
317 */
318static int redistribute_pending(struct server *s)
319{
320 struct pendconn *pc, *pc_bck, *pc_end;
321 int xferred = 0;
322
323 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
324 struct session *sess = pc->sess;
Willy Tarreau4de91492010-01-22 19:10:05 +0100325 if ((sess->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP &&
326 !(sess->flags & SN_FORCE_PRST)) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100327 /* The REDISP option was specified. We will ignore
328 * cookie and force to balance or use the dispatcher.
329 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100330
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100331 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +0100332 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100333
Willy Tarreau48494c02007-11-30 10:41:39 +0100334 pendconn_free(pc);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200335 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100336 xferred++;
337 }
338 }
339 return xferred;
340}
341
342/* Check for pending connections at the backend, and assign some of them to
343 * the server coming up. The server's weight is checked before being assigned
344 * connections it may not be able to handle. The total number of transferred
345 * connections is returned.
346 */
347static int check_for_pending(struct server *s)
348{
349 int xferred;
350
351 if (!s->eweight)
352 return 0;
353
354 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
355 struct session *sess;
356 struct pendconn *p;
357
358 p = pendconn_from_px(s->proxy);
359 if (!p)
360 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100361 p->sess->target = &s->obj_type;
Willy Tarreau48494c02007-11-30 10:41:39 +0100362 sess = p->sess;
363 pendconn_free(p);
Willy Tarreaufdccded2008-08-29 18:19:04 +0200364 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreau48494c02007-11-30 10:41:39 +0100365 }
366 return xferred;
367}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700369/* Shutdown all connections of a server. The caller must pass a termination
370 * code in <why>, which must be one of SN_ERR_* indicating the reason for the
371 * shutdown.
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900372 */
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700373static void shutdown_sessions(struct server *srv, int why)
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900374{
375 struct session *session, *session_bck;
376
Willy Tarreaua2a64e92011-09-07 23:01:56 +0200377 list_for_each_entry_safe(session, session_bck, &srv->actconns, by_srv)
378 if (session->srv_conn == srv)
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700379 session_shutdown(session, why);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900380}
381
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700382/* Shutdown all connections of all backup servers of a proxy. The caller must
383 * pass a termination code in <why>, which must be one of SN_ERR_* indicating
384 * the reason for the shutdown.
385 */
386static void shutdown_backup_sessions(struct proxy *px, int why)
387{
388 struct server *srv;
389
390 for (srv = px->srv; srv != NULL; srv = srv->next)
391 if (srv->state & SRV_BACKUP)
392 shutdown_sessions(srv, why);
393}
394
Willy Tarreaubaaee002006-06-26 02:48:02 +0200395/* Sets server <s> down, notifies by all available means, recounts the
396 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200397 * possible to other servers. It automatically recomputes the number of
398 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200399 */
Simon Horman4a741432013-02-23 15:35:38 +0900400void set_server_down(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200401{
Simon Horman4a741432013-02-23 15:35:38 +0900402 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100403 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200404 int xferred;
405
Cyril Bontécd19e512010-01-31 22:34:03 +0100406 if (s->state & SRV_MAINTAIN) {
Simon Horman58c32972013-11-25 10:46:38 +0900407 check->health = check->rise;
Cyril Bontécd19e512010-01-31 22:34:03 +0100408 }
409
Simon Horman58c32972013-11-25 10:46:38 +0900410 if ((s->state & SRV_RUNNING && check->health == check->rise) || s->track) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100411 int srv_was_paused = s->state & SRV_GOINGDOWN;
Willy Tarreaud64d2252010-10-17 17:16:42 +0200412 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200413
414 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100415 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreau9580d162012-05-19 19:07:40 +0200416 if (s->proxy->lbprm.set_server_status_down)
417 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200418
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900419 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700420 shutdown_sessions(s, SN_ERR_DOWN);
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900421
Willy Tarreaubaaee002006-06-26 02:48:02 +0200422 /* we might have sessions queued on this server and waiting for
423 * a connection. Those which are redispatchable will be queued
424 * to another server or to the proxy itself.
425 */
Willy Tarreau48494c02007-11-30 10:41:39 +0100426 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100427
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100428 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100429
Cyril Bontécd19e512010-01-31 22:34:03 +0100430 if (s->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100431 chunk_appendf(&trash,
432 "%sServer %s/%s is DOWN for maintenance", s->state & SRV_BACKUP ? "Backup " : "",
433 s->proxy->id, s->id);
Cyril Bontécd19e512010-01-31 22:34:03 +0100434 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100435 chunk_appendf(&trash,
436 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
437 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100438
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100439 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900440 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : 0),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100441 xferred);
Cyril Bontécd19e512010-01-31 22:34:03 +0100442 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100443 Warning("%s.\n", trash.str);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200444
Willy Tarreau48494c02007-11-30 10:41:39 +0100445 /* we don't send an alert if the server was previously paused */
446 if (srv_was_paused)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100447 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreau48494c02007-11-30 10:41:39 +0100448 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100449 send_log(s->proxy, LOG_ALERT, "%s.\n", trash.str);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200450
Willy Tarreaud64d2252010-10-17 17:16:42 +0200451 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
Willy Tarreau48494c02007-11-30 10:41:39 +0100452 set_backend_down(s->proxy);
453
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200454 s->counters.down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100455
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100456 if (s->state & SRV_CHECKED)
Willy Tarreaubc16cd82013-11-27 16:52:23 +0100457 for (srv = s->tracknext; srv; srv = srv->tracknext)
458 if (!(srv->state & SRV_MAINTAIN))
Cyril Bontécd19e512010-01-31 22:34:03 +0100459 /* Only notify tracking servers that are not already in maintenance. */
Willy Tarreaubc16cd82013-11-27 16:52:23 +0100460 set_server_down(&srv->check);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200461 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100462
Simon Horman125d0992013-02-24 17:23:38 +0900463 check->health = 0; /* failure */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464}
465
Simon Horman4a741432013-02-23 15:35:38 +0900466void set_server_up(struct check *check) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100467
Simon Horman4a741432013-02-23 15:35:38 +0900468 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100469 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100470 int xferred;
Willy Tarreau45446782012-03-09 17:16:09 +0100471 unsigned int old_state = s->state;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100472
Cyril Bontécd19e512010-01-31 22:34:03 +0100473 if (s->state & SRV_MAINTAIN) {
Simon Horman58c32972013-11-25 10:46:38 +0900474 check->health = check->rise;
Cyril Bontécd19e512010-01-31 22:34:03 +0100475 }
476
Simon Horman58c32972013-11-25 10:46:38 +0900477 if ((s->check.health >= s->check.rise && s->agent.health >= s->agent.rise &&
478 check->health == check->rise) || s->track) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100479 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
480 if (s->proxy->last_change < now.tv_sec) // ignore negative times
481 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
482 s->proxy->last_change = now.tv_sec;
483 }
484
485 if (s->last_change < now.tv_sec) // ignore negative times
486 s->down_time += now.tv_sec - s->last_change;
487
488 s->last_change = now.tv_sec;
489 s->state |= SRV_RUNNING;
Willy Tarreau45446782012-03-09 17:16:09 +0100490 s->state &= ~SRV_MAINTAIN;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100491
492 if (s->slowstart > 0) {
493 s->state |= SRV_WARMINGUP;
Willy Tarreau2e993902011-10-31 11:53:20 +0100494 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100495 }
Willy Tarreau004e0452013-11-21 11:22:01 +0100496
497 server_recalc_eweight(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100498
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700499 /* 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 Oledzkic8b16fc2008-02-18 01:26:35 +0100508 /* check if we can handle some connections queued at the proxy. We
509 * will take as many as we can handle.
510 */
511 xferred = check_for_pending(s);
512
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100513 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100514
Willy Tarreau45446782012-03-09 17:16:09 +0100515 if (old_state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100516 chunk_appendf(&trash,
517 "%sServer %s/%s is UP (leaving maintenance)", s->state & SRV_BACKUP ? "Backup " : "",
518 s->proxy->id, s->id);
Cyril Bontécd19e512010-01-31 22:34:03 +0100519 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100520 chunk_appendf(&trash,
521 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
522 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100523
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100524 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900525 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100526 xferred);
Cyril Bontécd19e512010-01-31 22:34:03 +0100527 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100528
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100529 Warning("%s.\n", trash.str);
530 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100531
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100532 if (s->state & SRV_CHECKED)
Willy Tarreaubc16cd82013-11-27 16:52:23 +0100533 for (srv = s->tracknext; srv; srv = srv->tracknext)
534 if (!(srv->state & SRV_MAINTAIN))
Cyril Bontécd19e512010-01-31 22:34:03 +0100535 /* Only notify tracking servers if they're not in maintenance. */
Willy Tarreaubc16cd82013-11-27 16:52:23 +0100536 set_server_up(&srv->check);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100537 }
538
Simon Horman58c32972013-11-25 10:46:38 +0900539 if (check->health >= check->rise)
540 check->health = check->rise + check->fall - 1; /* OK now */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100541
542}
543
Simon Horman4a741432013-02-23 15:35:38 +0900544static void set_server_disabled(struct check *check) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100545
Simon Horman4a741432013-02-23 15:35:38 +0900546 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100547 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100548 int xferred;
549
550 s->state |= SRV_GOINGDOWN;
Willy Tarreau9580d162012-05-19 19:07:40 +0200551 if (s->proxy->lbprm.set_server_status_down)
552 s->proxy->lbprm.set_server_status_down(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100553
554 /* we might have sessions queued on this server and waiting for
555 * a connection. Those which are redispatchable will be queued
556 * to another server or to the proxy itself.
557 */
558 xferred = redistribute_pending(s);
559
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100560 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100561
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100562 chunk_appendf(&trash,
563 "Load-balancing on %sServer %s/%s is disabled",
564 s->state & SRV_BACKUP ? "Backup " : "",
565 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100566
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100567 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900568 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100569 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100570
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100571 Warning("%s.\n", trash.str);
572 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100573
574 if (!s->proxy->srv_bck && !s->proxy->srv_act)
575 set_backend_down(s->proxy);
576
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100577 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100578 for(srv = s->tracknext; srv; srv = srv->tracknext)
Willy Tarreau81cf08c2013-11-28 11:27:16 +0100579 set_server_disabled(&srv->check);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100580}
581
Simon Horman4a741432013-02-23 15:35:38 +0900582static void set_server_enabled(struct check *check) {
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100583
Simon Horman4a741432013-02-23 15:35:38 +0900584 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100585 struct server *srv;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100586 int xferred;
587
588 s->state &= ~SRV_GOINGDOWN;
Willy Tarreau9580d162012-05-19 19:07:40 +0200589 if (s->proxy->lbprm.set_server_status_up)
590 s->proxy->lbprm.set_server_status_up(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100591
592 /* check if we can handle some connections queued at the proxy. We
593 * will take as many as we can handle.
594 */
595 xferred = check_for_pending(s);
596
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100597 chunk_reset(&trash);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100598
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100599 chunk_appendf(&trash,
600 "Load-balancing on %sServer %s/%s is enabled again",
601 s->state & SRV_BACKUP ? "Backup " : "",
602 s->proxy->id, s->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100603
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100604 server_status_printf(&trash, s,
Simon Horman4a741432013-02-23 15:35:38 +0900605 ((!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check : NULL),
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100606 xferred);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100607
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100608 Warning("%s.\n", trash.str);
609 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100610
Krzysztof Piotr Oledzkif39c71c2009-01-30 00:52:49 +0100611 if (s->state & SRV_CHECKED)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100612 for(srv = s->tracknext; srv; srv = srv->tracknext)
Willy Tarreau81cf08c2013-11-28 11:27:16 +0100613 set_server_enabled(&srv->check);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100614}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200615
Simon Hormand8583062013-11-25 10:46:33 +0900616static void check_failed(struct check *check)
617{
618 struct server *s = check->server;
619
Simon Horman2f1f9552013-11-25 10:46:37 +0900620 /* The agent secondary check should only cause a server to be marked
621 * as down if check->status is HCHK_STATUS_L7STS, which indicates
622 * that the agent returned "fail", "stopped" or "down".
623 * The implication here is that failure to connect to the agent
624 * as a secondary check should not cause the server to be marked
625 * down. */
626 if (check == &s->agent && check->status != HCHK_STATUS_L7STS)
627 return;
628
Simon Horman58c32972013-11-25 10:46:38 +0900629 if (check->health > check->rise) {
Simon Hormand8583062013-11-25 10:46:33 +0900630 check->health--; /* still good */
631 s->counters.failed_checks++;
632 }
633 else
634 set_server_down(check);
635}
636
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100637void health_adjust(struct server *s, short status)
638{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100639 int failed;
640 int expire;
641
642 /* return now if observing nor health check is not enabled */
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200643 if (!s->observe || !s->check.task)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100644 return;
645
646 if (s->observe >= HANA_OBS_SIZE)
647 return;
648
Willy Tarreaubb956662013-01-24 00:37:39 +0100649 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100650 return;
651
652 switch (analyze_statuses[status].lr[s->observe - 1]) {
653 case 1:
654 failed = 1;
655 break;
656
657 case 2:
658 failed = 0;
659 break;
660
661 default:
662 return;
663 }
664
665 if (!failed) {
666 /* good: clear consecutive_errors */
667 s->consecutive_errors = 0;
668 return;
669 }
670
671 s->consecutive_errors++;
672
673 if (s->consecutive_errors < s->consecutive_errors_limit)
674 return;
675
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100676 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
677 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100678
679 switch (s->onerror) {
680 case HANA_ONERR_FASTINTER:
681 /* force fastinter - nothing to do here as all modes force it */
682 break;
683
684 case HANA_ONERR_SUDDTH:
685 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900686 if (s->check.health > s->check.rise)
687 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100688
689 /* no break - fall through */
690
691 case HANA_ONERR_FAILCHK:
692 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900693 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Simon Hormand8583062013-11-25 10:46:33 +0900694 check_failed(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100695
696 break;
697
698 case HANA_ONERR_MARKDWN:
699 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900700 s->check.health = s->check.rise;
Simon Horman4a741432013-02-23 15:35:38 +0900701 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
702 set_server_down(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100703
704 break;
705
706 default:
707 /* write a warning? */
708 break;
709 }
710
711 s->consecutive_errors = 0;
712 s->counters.failed_hana++;
713
Simon Horman66183002013-02-23 10:16:43 +0900714 if (s->check.fastinter) {
715 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300716 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200717 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300718 /* requeue check task with new expire */
719 task_queue(s->check.task);
720 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100721 }
722}
723
Willy Tarreauef781042010-01-27 11:53:01 +0100724static int httpchk_build_status_header(struct server *s, char *buffer)
725{
726 int sv_state;
727 int ratio;
728 int hlen = 0;
729 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
730 "UP %d/%d", "UP",
731 "NOLB %d/%d", "NOLB",
732 "no check" };
733
734 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
735 hlen += 24;
736
737 if (!(s->state & SRV_CHECKED))
738 sv_state = 6; /* should obviously never happen */
739 else if (s->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +0900740 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100741 sv_state = 3; /* UP */
742 else
743 sv_state = 2; /* going down */
744
745 if (s->state & SRV_GOINGDOWN)
746 sv_state += 2;
747 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900748 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100749 sv_state = 1; /* going up */
750 else
751 sv_state = 0; /* DOWN */
752 }
753
754 hlen += sprintf(buffer + hlen,
755 srv_hlt_st[sv_state],
Simon Horman58c32972013-11-25 10:46:38 +0900756 (s->state & SRV_RUNNING) ? (s->check.health - s->check.rise + 1) : (s->check.health),
757 (s->state & SRV_RUNNING) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100758
759 hlen += sprintf(buffer + hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
760 s->proxy->id, s->id,
761 global.node,
762 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
763 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
764 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
765 s->nbpend);
766
767 if ((s->state & SRV_WARMINGUP) &&
768 now.tv_sec < s->last_change + s->slowstart &&
769 now.tv_sec >= s->last_change) {
770 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
771 hlen += sprintf(buffer + hlen, "; throttle=%d%%", ratio);
772 }
773
774 buffer[hlen++] = '\r';
775 buffer[hlen++] = '\n';
776
777 return hlen;
778}
779
Willy Tarreau20a18342013-12-05 00:31:46 +0100780/* Check the connection. If an error has already been reported or the socket is
781 * closed, keep errno intact as it is supposed to contain the valid error code.
782 * If no error is reported, check the socket's error queue using getsockopt().
783 * Warning, this must be done only once when returning from poll, and never
784 * after an I/O error was attempted, otherwise the error queue might contain
785 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
786 * socket. Returns non-zero if an error was reported, zero if everything is
787 * clean (including a properly closed socket).
788 */
789static int retrieve_errno_from_socket(struct connection *conn)
790{
791 int skerr;
792 socklen_t lskerr = sizeof(skerr);
793
794 if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl))
795 return 1;
796
797 if (!conn->ctrl)
798 return 0;
799
800 if (getsockopt(conn->t.sock.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
801 errno = skerr;
802
803 if (errno == EAGAIN)
804 errno = 0;
805
806 if (!errno) {
807 /* we could not retrieve an error, that does not mean there is
808 * none. Just don't change anything and only report the prior
809 * error if any.
810 */
811 if (conn->flags & CO_FL_ERROR)
812 return 1;
813 else
814 return 0;
815 }
816
817 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
818 return 1;
819}
820
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100821/* Try to collect as much information as possible on the connection status,
822 * and adjust the server status accordingly. It may make use of <errno_bck>
823 * if non-null when the caller is absolutely certain of its validity (eg:
824 * checked just after a syscall). If the caller doesn't have a valid errno,
825 * it can pass zero, and retrieve_errno_from_socket() will be called to try
826 * to extract errno from the socket. If no error is reported, it will consider
827 * the <expired> flag. This is intended to be used when a connection error was
828 * reported in conn->flags or when a timeout was reported in <expired>. The
829 * function takes care of not updating a server status which was already set.
830 * All situations where at least one of <expired> or CO_FL_ERROR are set
831 * produce a status.
832 */
833static void chk_report_conn_err(struct connection *conn, int errno_bck, int expired)
834{
835 struct check *check = conn->owner;
836 const char *err_msg;
837
838 if (check->result != SRV_CHK_UNKNOWN)
839 return;
840
841 errno = errno_bck;
842 if (!errno || errno == EAGAIN)
843 retrieve_errno_from_socket(conn);
844
845 if (!(conn->flags & CO_FL_ERROR) && !expired)
846 return;
847
848 /* we'll try to build a meaningful error message depending on the
849 * context of the error possibly present in conn->err_code, and the
850 * socket error possibly collected above. This is useful to know the
851 * exact step of the L6 layer (eg: SSL handshake).
852 */
853 if (conn->err_code) {
854 if (errno && errno != EAGAIN)
855 chunk_printf(&trash, "%s (%s)", conn_err_code_str(conn), strerror(errno));
856 else
857 chunk_printf(&trash, "%s", conn_err_code_str(conn));
858 err_msg = trash.str;
859 }
860 else {
861 if (errno && errno != EAGAIN) {
862 chunk_printf(&trash, "%s", strerror(errno));
863 err_msg = trash.str;
864 }
865 else {
866 err_msg = NULL;
867 }
868 }
869
870 if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
871 /* L4 not established (yet) */
872 if (conn->flags & CO_FL_ERROR)
873 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
874 else if (expired)
875 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
876 }
877 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
878 /* L6 not established (yet) */
879 if (conn->flags & CO_FL_ERROR)
880 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
881 else if (expired)
882 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
883 }
884 else if (conn->flags & CO_FL_ERROR) {
885 /* I/O error after connection was established and before we could diagnose */
886 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
887 }
888 else if (expired) {
889 /* connection established but expired check */
890 if (check->type == PR_O2_SSL3_CHK)
891 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
892 else /* HTTP, SMTP, ... */
893 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
894 }
895
896 return;
897}
898
Willy Tarreaubaaee002006-06-26 02:48:02 +0200899/*
900 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200901 * the connection acknowledgement. If the proxy requires L7 health-checks,
902 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900903 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200904 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200905static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200906{
Simon Horman4a741432013-02-23 15:35:38 +0900907 struct check *check = conn->owner;
908 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900909 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200910
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100911 if (unlikely(check->result & SRV_CHK_FAILED))
912 goto out_wakeup;
913
914 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_WR))
915 return;
916
Willy Tarreau20a18342013-12-05 00:31:46 +0100917 if (retrieve_errno_from_socket(conn)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100918 chk_report_conn_err(conn, errno, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100919 __conn_data_stop_both(conn);
920 goto out_wakeup;
921 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100922
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100923 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100924 /* if the output is closed, we can't do anything */
925 conn->flags |= CO_FL_ERROR;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100926 chk_report_conn_err(conn, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100927 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200928 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200929
Willy Tarreau06559ac2013-12-05 01:53:08 +0100930 /* here, we know that the connection is established. That's enough for
931 * a pure TCP check.
932 */
933 if (!check->type)
934 goto out_wakeup;
935
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100936 if (check->bo->o) {
937 conn->xprt->snd_buf(conn, check->bo, MSG_DONTWAIT | MSG_NOSIGNAL);
938 if (conn->flags & CO_FL_ERROR) {
939 chk_report_conn_err(conn, errno, 0);
940 __conn_data_stop_both(conn);
941 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200942 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100943 if (check->bo->o)
944 return;
945 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200946
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100947 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
948 if (s->proxy->timeout.check) {
949 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
950 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200951 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100952 goto out_nowake;
953
Willy Tarreau83749182007-04-15 20:56:27 +0200954 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200955 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200956 out_nowake:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200957 __conn_data_stop_send(conn); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200958}
959
Willy Tarreaubaaee002006-06-26 02:48:02 +0200960/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200961 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200962 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900963 * set_server_check_status() to update check->status, check->duration
964 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200965
966 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
967 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
968 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
969 * response to an SSL HELLO (the principle is that this is enough to
970 * distinguish between an SSL server and a pure TCP relay). All other cases will
971 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
972 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200973 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200974static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200975{
Simon Horman4a741432013-02-23 15:35:38 +0900976 struct check *check = conn->owner;
977 struct server *s = check->server;
978 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200979 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100980 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200981 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200982
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100983 if (unlikely(check->result & SRV_CHK_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +0200984 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200985
Willy Tarreau5a78f362012-11-23 12:47:05 +0100986 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_RD))
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200987 return;
988
Willy Tarreau83749182007-04-15 20:56:27 +0200989 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
990 * but the connection was closed on the remote end. Fortunately, recv still
991 * works correctly and we don't need to do the getsockopt() on linux.
992 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000993
994 /* Set buffer to point to the end of the data already read, and check
995 * that there is free space remaining. If the buffer is full, proceed
996 * with running the checks without attempting another socket read.
997 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000998
Willy Tarreau03938182010-03-17 21:52:07 +0100999 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001000
Simon Horman4a741432013-02-23 15:35:38 +09001001 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreauf1503172012-09-28 19:39:36 +02001002 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +01001003 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +09001004 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +02001005 /* Report network errors only if we got no other data. Otherwise
1006 * we'll let the upper layers decide whether the response is OK
1007 * or not. It is very common that an RST sent by the server is
1008 * reported as an error just after the last data chunk.
1009 */
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001010 chk_report_conn_err(conn, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +01001011 goto out_wakeup;
1012 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001013 }
1014
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001015
Willy Tarreau03938182010-03-17 21:52:07 +01001016 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +09001017 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +01001018 */
Simon Horman4a741432013-02-23 15:35:38 +09001019 if (check->bi->i < check->bi->size)
1020 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +01001021 else {
Simon Horman4a741432013-02-23 15:35:38 +09001022 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +01001023 done = 1; /* buffer full, don't wait for more data */
1024 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001025
Nick Chalk57b1bf72010-03-16 15:50:46 +00001026 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +09001027 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +02001028 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001029 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +01001030 goto wait_more_data;
1031
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001032 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +09001033 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
1034 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
1035 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
1036 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
1037 !isdigit((unsigned char) *(check->bi->data + 11))) {
1038 cut_crlf(check->bi->data);
1039 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001040
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001041 goto out_wakeup;
1042 }
1043
Simon Horman4a741432013-02-23 15:35:38 +09001044 check->code = str2uic(check->bi->data + 9);
1045 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +00001046
Willy Tarreaubd741542010-03-16 18:46:54 +01001047 if ((s->proxy->options & PR_O_DISABLE404) &&
Simon Horman4a741432013-02-23 15:35:38 +09001048 (s->state & SRV_RUNNING) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +00001049 /* 404 may be accepted as "stopping" only if the server was up */
1050 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +09001051 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +00001052 }
Willy Tarreaubd741542010-03-16 18:46:54 +01001053 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
1054 /* Run content verification check... We know we have at least 13 chars */
1055 if (!httpchk_expect(s, done))
1056 goto wait_more_data;
1057 }
1058 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +09001059 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +01001060 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +09001061 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +01001062 }
Nick Chalk57b1bf72010-03-16 15:50:46 +00001063 else {
1064 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +09001065 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +00001066 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001067 break;
1068
1069 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001070 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001071 goto wait_more_data;
1072
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001073 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +09001074 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
1075 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001076 else
Simon Horman4a741432013-02-23 15:35:38 +09001077 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001078 break;
1079
1080 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001081 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +01001082 goto wait_more_data;
1083
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001084 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +09001085 if ((check->bi->i < strlen("000\r")) ||
1086 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
1087 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
1088 !isdigit((unsigned char) *(check->bi->data + 2))) {
1089 cut_crlf(check->bi->data);
1090 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001091
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001092 goto out_wakeup;
1093 }
1094
Simon Horman4a741432013-02-23 15:35:38 +09001095 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001096
Simon Horman4a741432013-02-23 15:35:38 +09001097 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001098 cut_crlf(desc);
1099
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001100 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +09001101 if (*check->bi->data == '2')
1102 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +02001103 else
Simon Horman4a741432013-02-23 15:35:38 +09001104 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +02001105 break;
1106
Simon Hormana2b9dad2013-02-12 10:45:54 +09001107 case PR_O2_LB_AGENT_CHK: {
1108 short status = HCHK_STATUS_L7RSP;
1109 const char *desc = "Unknown feedback string";
1110 const char *down_cmd = NULL;
Simon Horman671b6f02013-11-25 10:46:39 +09001111 int disabled;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001112
1113 if (!done)
1114 goto wait_more_data;
1115
Simon Horman4a741432013-02-23 15:35:38 +09001116 cut_crlf(check->bi->data);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001117
Simon Horman671b6f02013-11-25 10:46:39 +09001118 /*
1119 * The agent may have been disabled after a check was
1120 * initialised. If so, ignore weight changes and drain
1121 * settings from the agent. Note that the setting is
1122 * always present in the state of the agent the server,
1123 * regardless of if the agent is being run as a primary or
1124 * secondary check. That is, regardless of if the check
1125 * parameter of this function is the agent or check field
1126 * of the server.
1127 */
1128 disabled = check->server->agent.state & CHK_STATE_DISABLED;
1129
Simon Horman4a741432013-02-23 15:35:38 +09001130 if (strchr(check->bi->data, '%')) {
Simon Horman671b6f02013-11-25 10:46:39 +09001131 if (disabled)
1132 break;
Simon Horman4a741432013-02-23 15:35:38 +09001133 desc = server_parse_weight_change_request(s, check->bi->data);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001134 if (!desc) {
1135 status = HCHK_STATUS_L7OKD;
Simon Horman4a741432013-02-23 15:35:38 +09001136 desc = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001137 }
Simon Horman4a741432013-02-23 15:35:38 +09001138 } else if (!strcasecmp(check->bi->data, "drain")) {
Simon Horman671b6f02013-11-25 10:46:39 +09001139 if (disabled)
1140 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001141 desc = server_parse_weight_change_request(s, "0%");
1142 if (!desc) {
1143 desc = "drain";
1144 status = HCHK_STATUS_L7OKD;
1145 }
Simon Horman4a741432013-02-23 15:35:38 +09001146 } else if (!strncasecmp(check->bi->data, "down", strlen("down"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001147 down_cmd = "down";
Simon Horman4a741432013-02-23 15:35:38 +09001148 } else if (!strncasecmp(check->bi->data, "stopped", strlen("stopped"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001149 down_cmd = "stopped";
Simon Horman4a741432013-02-23 15:35:38 +09001150 } else if (!strncasecmp(check->bi->data, "fail", strlen("fail"))) {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001151 down_cmd = "fail";
1152 }
1153
1154 if (down_cmd) {
Simon Horman4a741432013-02-23 15:35:38 +09001155 const char *end = check->bi->data + strlen(down_cmd);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001156 /*
1157 * The command keyword must terminated the string or
1158 * be followed by a blank.
1159 */
Willy Tarreau8b4c3762013-02-13 12:47:12 +01001160 if (end[0] == '\0' || end[0] == ' ' || end[0] == '\t') {
Simon Hormana2b9dad2013-02-12 10:45:54 +09001161 status = HCHK_STATUS_L7STS;
Simon Horman80fefae2013-11-25 10:46:34 +09001162 desc = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001163 }
1164 }
1165
Simon Horman4a741432013-02-23 15:35:38 +09001166 set_server_check_status(check, status, desc);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001167 set_server_drain_state(check->server);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001168 break;
1169 }
1170
Willy Tarreau1620ec32011-08-06 17:05:02 +02001171 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001172 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001173 goto wait_more_data;
1174
Simon Horman4a741432013-02-23 15:35:38 +09001175 if (check->bi->data[0] == 'R') {
1176 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001177 }
1178 else {
Simon Horman4a741432013-02-23 15:35:38 +09001179 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1180 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001181 else
1182 desc = "PostgreSQL unknown error";
1183
Simon Horman4a741432013-02-23 15:35:38 +09001184 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001185 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001186 break;
1187
1188 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001189 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001190 goto wait_more_data;
1191
Simon Horman4a741432013-02-23 15:35:38 +09001192 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1193 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001194 }
1195 else {
Simon Horman4a741432013-02-23 15:35:38 +09001196 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001197 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001198 break;
1199
1200 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001201 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001202 goto wait_more_data;
1203
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001204 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001205 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001206 /* We set the MySQL Version in description for information purpose
1207 * FIXME : it can be cool to use MySQL Version for other purpose,
1208 * like mark as down old MySQL server.
1209 */
Simon Horman4a741432013-02-23 15:35:38 +09001210 if (check->bi->i > 51) {
1211 desc = ltrim(check->bi->data + 5, ' ');
1212 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001213 }
1214 else {
1215 if (!done)
1216 goto wait_more_data;
1217 /* it seems we have a OK packet but without a valid length,
1218 * it must be a protocol error
1219 */
Simon Horman4a741432013-02-23 15:35:38 +09001220 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001221 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001222 }
1223 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001224 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001225 desc = ltrim(check->bi->data + 7, ' ');
1226 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001227 }
1228 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001229 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1230 (((unsigned int) *(check->bi->data + 1)) << 8) +
1231 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001232
Simon Horman4a741432013-02-23 15:35:38 +09001233 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001234 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001235 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001236 /* We have only one MySQL packet and it is a Handshake Initialization packet
1237 * but we need to have a second packet to know if it is alright
1238 */
Simon Horman4a741432013-02-23 15:35:38 +09001239 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001240 goto wait_more_data;
1241 }
1242 else {
1243 /* We have only one packet and it is an Error packet,
1244 * an error message is attached, so we can display it
1245 */
Simon Horman4a741432013-02-23 15:35:38 +09001246 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001247 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001248 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001249 }
Simon Horman4a741432013-02-23 15:35:38 +09001250 } else if (check->bi->i > first_packet_len + 4) {
1251 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1252 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1253 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001254
Simon Horman4a741432013-02-23 15:35:38 +09001255 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001256 /* We have 2 packets and that's good */
1257 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001258 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001259 /* No error packet */
1260 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001261 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001262 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001263 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001264 }
1265 else {
1266 /* An error message is attached in the Error packet
1267 * so we can display it ! :)
1268 */
Simon Horman4a741432013-02-23 15:35:38 +09001269 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001270 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001271 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001272 }
1273 }
1274 }
1275 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001276 if (!done)
1277 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001278 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001279 * it must be a protocol error
1280 */
Simon Horman4a741432013-02-23 15:35:38 +09001281 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001282 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001283 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001284 }
1285 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001286 break;
1287
1288 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001289 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001290 goto wait_more_data;
1291
1292 /* Check if the server speaks LDAP (ASN.1/BER)
1293 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1294 * http://tools.ietf.org/html/rfc4511
1295 */
1296
1297 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1298 * LDAPMessage: 0x30: SEQUENCE
1299 */
Simon Horman4a741432013-02-23 15:35:38 +09001300 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1301 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001302 }
1303 else {
1304 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001305 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001306
1307 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1308 * messageID: 0x02 0x01 0x01: INTEGER 1
1309 * protocolOp: 0x61: bindResponse
1310 */
1311 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001312 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1313 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001314
1315 goto out_wakeup;
1316 }
1317
1318 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001319 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001320
1321 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1322 * ldapResult: 0x0a 0x01: ENUMERATION
1323 */
1324 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001325 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1326 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001327
1328 goto out_wakeup;
1329 }
1330
1331 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1332 * resultCode
1333 */
Simon Horman4a741432013-02-23 15:35:38 +09001334 check->code = *(check->bi->data + msglen + 9);
1335 if (check->code) {
1336 set_server_check_status(check, HCHK_STATUS_L7STS, "See RFC: http://tools.ietf.org/html/rfc4511#section-4.1.9");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001337 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001338 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001339 }
1340 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001341 break;
1342
1343 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001344 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001345 break;
1346 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001347
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001348 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001349 /* collect possible new errors */
1350 if (conn->flags & CO_FL_ERROR)
1351 chk_report_conn_err(conn, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001352
Nick Chalk57b1bf72010-03-16 15:50:46 +00001353 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001354 *check->bi->data = '\0';
1355 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001356
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001357 /* Close the connection... We absolutely want to perform a hard close
1358 * and reset the connection if some data are pending, otherwise we end
1359 * up with many TIME_WAITs and eat all the source port range quickly.
1360 * To avoid sending RSTs all the time, we first try to drain pending
1361 * data.
1362 */
Willy Tarreauf1503172012-09-28 19:39:36 +02001363 if (conn->xprt && conn->xprt->shutw)
1364 conn->xprt->shutw(conn, 0);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001365
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001366 /* OK, let's not stay here forever */
1367 if (check->result & SRV_CHK_FAILED)
1368 conn->flags |= CO_FL_ERROR;
1369
Willy Tarreaua522f802012-11-23 08:56:35 +01001370 __conn_data_stop_both(conn);
Willy Tarreaufdccded2008-08-29 18:19:04 +02001371 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001372 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001373
1374 wait_more_data:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001375 __conn_data_poll_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001376}
1377
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001378/*
1379 * This function is used only for server health-checks. It handles connection
1380 * status updates including errors. If necessary, it wakes the check task up.
1381 * It always returns 0.
1382 */
1383static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001384{
Simon Horman4a741432013-02-23 15:35:38 +09001385 struct check *check = conn->owner;
Willy Tarreau20bea422012-07-06 12:00:49 +02001386
Willy Tarreau6c560da2012-11-24 11:14:45 +01001387 if (unlikely(conn->flags & CO_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001388 /* We may get error reports bypassing the I/O handlers, typically
1389 * the case when sending a pure TCP check which fails, then the I/O
1390 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001391 * main processing task so let's simply wake it up. If we get here,
1392 * we expect errno to still be valid.
1393 */
1394 chk_report_conn_err(conn, errno, 0);
1395
Willy Tarreau2d351b62013-12-05 02:36:25 +01001396 __conn_data_stop_both(conn);
1397 task_wakeup(check->task, TASK_WOKEN_IO);
1398 }
1399
1400 if (check->result & (SRV_CHK_FAILED|SRV_CHK_PASSED)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001401 /* We're here because nobody wants to handle the error, so we
1402 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001403 */
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001404 if (conn->ctrl && !(conn->flags & CO_FL_SOCK_RD_SH)) {
Willy Tarreau2d351b62013-12-05 02:36:25 +01001405 if (conn->flags & CO_FL_WAIT_RD || !conn->ctrl->drain || !conn->ctrl->drain(conn->t.sock.fd))
1406 setsockopt(conn->t.sock.fd, SOL_SOCKET, SO_LINGER,
1407 (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001408 }
Willy Tarreau2b199c92012-11-23 17:32:21 +01001409 conn_full_close(conn);
Willy Tarreau2d351b62013-12-05 02:36:25 +01001410 }
Willy Tarreau3267d362012-08-17 23:53:56 +02001411 return 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001412}
1413
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001414struct data_cb check_conn_cb = {
1415 .recv = event_srv_chk_r,
1416 .send = event_srv_chk_w,
1417 .wake = wake_srv_chk,
1418};
1419
Willy Tarreaubaaee002006-06-26 02:48:02 +02001420/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001421 * updates the server's weight during a warmup stage. Once the final weight is
1422 * reached, the task automatically stops. Note that any server status change
1423 * must have updated s->last_change accordingly.
1424 */
1425static struct task *server_warmup(struct task *t)
1426{
1427 struct server *s = t->context;
1428
1429 /* by default, plan on stopping the task */
1430 t->expire = TICK_ETERNITY;
1431 if ((s->state & (SRV_RUNNING|SRV_WARMINGUP|SRV_MAINTAIN)) != (SRV_RUNNING|SRV_WARMINGUP))
1432 return t;
1433
Willy Tarreau004e0452013-11-21 11:22:01 +01001434 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001435
1436 /* probably that we can refill this server with a bit more connections */
1437 check_for_pending(s);
1438
1439 /* get back there in 1 second or 1/20th of the slowstart interval,
1440 * whichever is greater, resulting in small 5% steps.
1441 */
1442 if (s->state & SRV_WARMINGUP)
1443 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1444 return t;
1445}
1446
1447/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001448 * manages a server health-check. Returns
1449 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1450 */
Simon Horman63a4a822012-03-19 07:24:41 +09001451static struct task *process_chk(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001452{
Simon Horman4a741432013-02-23 15:35:38 +09001453 struct check *check = t->context;
1454 struct server *s = check->server;
1455 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001456 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001457 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001458 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001459
Simon Hormancd5d7b62013-02-24 17:23:38 +09001460 if (!(check->state & CHK_STATE_RUNNING)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01001461 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001462 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01001463 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001464
Simon Horman671b6f02013-11-25 10:46:39 +09001465 /* we don't send any health-checks when the proxy is
1466 * stopped, the server should not be checked or the check
1467 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001468 */
Simon Horman671b6f02013-11-25 10:46:39 +09001469 if (!(s->state & SRV_CHECKED) ||
1470 s->proxy->state == PR_STSTOPPED ||
1471 (s->state & SRV_MAINTAIN) ||
1472 (check->state & CHK_STATE_DISABLED))
Willy Tarreau5a78f362012-11-23 12:47:05 +01001473 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001474
1475 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09001476 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001477
Simon Hormancd5d7b62013-02-24 17:23:38 +09001478 check->state |= CHK_STATE_RUNNING;
Simon Horman4a741432013-02-23 15:35:38 +09001479 check->bi->p = check->bi->data;
1480 check->bi->i = 0;
1481 check->bo->p = check->bo->data;
1482 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001483
Simon Hormand60d6912013-11-25 10:46:36 +09001484 /* prepare the check buffer
1485 * This should not be used if check is the secondary agent check
1486 * of a server as s->proxy->check_req will relate to the
1487 * configuration of the primary check */
1488 if (check->type && check != &s->agent) {
Simon Horman4a741432013-02-23 15:35:38 +09001489 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001490
1491 /* we want to check if this host replies to HTTP or SSLv3 requests
1492 * so we'll send the request, and won't wake the checker up now.
1493 */
Simon Horman4a741432013-02-23 15:35:38 +09001494 if ((check->type) == PR_O2_SSL3_CHK) {
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001495 /* SSL requires that we put Unix time in the request */
1496 int gmt_time = htonl(date.tv_sec);
Simon Horman4a741432013-02-23 15:35:38 +09001497 memcpy(check->bo->data + 11, &gmt_time, 4);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001498 }
Simon Horman4a741432013-02-23 15:35:38 +09001499 else if ((check->type) == PR_O2_HTTP_CHK) {
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001500 if (s->proxy->options2 & PR_O2_CHK_SNDST)
Simon Horman4a741432013-02-23 15:35:38 +09001501 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str));
1502 bo_putstr(check->bo, "\r\n");
1503 *check->bo->p = '\0'; /* to make gdb output easier to read */
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001504 }
1505 }
1506
1507 /* prepare a new connection */
Willy Tarreaub63b5962012-11-23 16:22:08 +01001508 conn->flags = CO_FL_NONE;
Willy Tarreau14cba4b2012-11-30 17:33:05 +01001509 conn->err_code = CO_ER_NONE;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001510 conn->target = &s->obj_type;
Simon Horman4a741432013-02-23 15:35:38 +09001511 conn_prepare(conn, &check_conn_cb, s->check_common.proto, s->check_common.xprt, check);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001512
Willy Tarreau5f2877a2012-10-26 19:57:58 +02001513 /* no client address */
1514 clear_addr(&conn->addr.from);
1515
Simon Horman66183002013-02-23 10:16:43 +09001516 if (is_addr(&s->check_common.addr))
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001517 /* we'll connect to the check addr specified on the server */
Simon Horman66183002013-02-23 10:16:43 +09001518 conn->addr.to = s->check_common.addr;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001519 else
1520 /* we'll connect to the addr on the server */
1521 conn->addr.to = s->addr;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001522
Simon Horman4a741432013-02-23 15:35:38 +09001523 set_host_port(&conn->addr.to, check->port);
Willy Tarreaue8c66af2008-01-13 18:40:14 +01001524
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001525 /* It can return one of :
1526 * - SN_ERR_NONE if everything's OK
1527 * - SN_ERR_SRVTO if there are no more servers
1528 * - SN_ERR_SRVCL if the connection was refused by the server
1529 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1530 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1531 * - SN_ERR_INTERNAL for any other purely internal errors
1532 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau24db47e2012-11-23 14:16:39 +01001533 * Note that we try to prevent the network stack from sending the ACK during the
Willy Tarreauf0837b22012-11-24 10:24:27 +01001534 * connect() when a pure TCP check is used (without PROXY protocol).
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001535 */
Willy Tarreau8f46cca2013-03-04 20:07:44 +01001536 ret = SN_ERR_INTERNAL;
Simon Horman66183002013-02-23 10:16:43 +09001537 if (s->check_common.proto->connect)
Simon Horman4a741432013-02-23 15:35:38 +09001538 ret = s->check_common.proto->connect(conn, check->type,
1539 s->check.send_proxy ? 1 : (check->type) ? 0 : 2);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001540 conn->flags |= CO_FL_WAKE_DATA;
Simon Horman4a741432013-02-23 15:35:38 +09001541 if (check->send_proxy)
Willy Tarreau6c16adc2012-10-05 00:04:16 +02001542 conn->flags |= CO_FL_LOCAL_SPROXY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001543
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001544 switch (ret) {
1545 case SN_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001546 /* we allow up to min(inter, timeout.connect) for a connection
1547 * to establish but only when timeout.check is set
1548 * as it may be to short for a full check otherwise
1549 */
Simon Horman4a741432013-02-23 15:35:38 +09001550 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001551
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001552 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1553 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1554 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001555 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01001556
1557 if (check->type)
1558 conn_data_want_recv(conn); /* prepare for reading a possible reply */
1559
Willy Tarreau5a78f362012-11-23 12:47:05 +01001560 goto reschedule;
1561
1562 case SN_ERR_SRVTO: /* ETIMEDOUT */
1563 case SN_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Simon Horman4a741432013-02-23 15:35:38 +09001564 set_server_check_status(check, HCHK_STATUS_L4CON, strerror(errno));
Willy Tarreau5a78f362012-11-23 12:47:05 +01001565 break;
1566 case SN_ERR_PRXCOND:
1567 case SN_ERR_RESOURCE:
1568 case SN_ERR_INTERNAL:
Simon Horman4a741432013-02-23 15:35:38 +09001569 set_server_check_status(check, HCHK_STATUS_SOCKERR, NULL);
Willy Tarreau5a78f362012-11-23 12:47:05 +01001570 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001571 }
1572
Willy Tarreau5a78f362012-11-23 12:47:05 +01001573 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01001574
Simon Hormancd5d7b62013-02-24 17:23:38 +09001575 check->state &= ~CHK_STATE_RUNNING;
Simon Hormand8583062013-11-25 10:46:33 +09001576 check_failed(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001577
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001578 /* we allow up to min(inter, timeout.connect) for a connection
1579 * to establish but only when timeout.check is set
1580 * as it may be to short for a full check otherwise
1581 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001582 while (tick_is_expired(t->expire, now_ms)) {
1583 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001584
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001585 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09001586 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001587
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001588 if (s->proxy->timeout.check)
1589 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01001590 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001591 }
1592 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02001593 /* there was a test running.
1594 * First, let's check whether there was an uncaught error,
1595 * which can happen on connect timeout or error.
1596 */
Simon Horman28b5ffc2013-02-24 07:25:29 +09001597 if (s->check.result == SRV_CHK_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001598 /* good connection is enough for pure TCP check */
1599 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09001600 if (check->use_ssl)
1601 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02001602 else
Simon Horman4a741432013-02-23 15:35:38 +09001603 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01001604 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001605 else if ((conn->flags & CO_FL_ERROR) || expired) {
1606 chk_report_conn_err(conn, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02001607 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001608 else
1609 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02001610 }
1611
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001612 /* check complete or aborted */
Willy Tarreau5ba04f62013-02-12 15:23:12 +01001613 if (conn->xprt) {
1614 /* The check was aborted and the connection was not yet closed.
1615 * This can happen upon timeout, or when an external event such
1616 * as a failed response coupled with "observe layer7" caused the
1617 * server state to be suddenly changed.
1618 */
Willy Tarreau2d351b62013-12-05 02:36:25 +01001619 if (conn->ctrl && !(conn->flags & CO_FL_SOCK_RD_SH)) {
1620 if (conn->flags & CO_FL_WAIT_RD || !conn->ctrl->drain || !conn->ctrl->drain(conn->t.sock.fd))
1621 setsockopt(conn->t.sock.fd, SOL_SOCKET, SO_LINGER,
1622 (struct linger *) &nolinger, sizeof(struct linger));
1623 }
Willy Tarreau5ba04f62013-02-12 15:23:12 +01001624 conn_full_close(conn);
1625 }
1626
Simon Hormand8583062013-11-25 10:46:33 +09001627 if (check->result & SRV_CHK_FAILED) /* a failure or timeout detected */
1628 check_failed(check);
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001629 else { /* check was OK */
Willy Tarreau48494c02007-11-30 10:41:39 +01001630 /* we may have to add/remove this server from the LB group */
1631 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
Simon Horman4a741432013-02-23 15:35:38 +09001632 if ((s->state & SRV_GOINGDOWN) && !(check->result & SRV_CHK_DISABLE))
1633 set_server_enabled(check);
1634 else if (!(s->state & SRV_GOINGDOWN) && (check->result & SRV_CHK_DISABLE))
1635 set_server_disabled(check);
Willy Tarreau48494c02007-11-30 10:41:39 +01001636 }
1637
Simon Horman58c32972013-11-25 10:46:38 +09001638 if (check->health < check->rise + check->fall - 1) {
Simon Horman125d0992013-02-24 17:23:38 +09001639 check->health++; /* was bad, stays for a while */
Simon Horman4a741432013-02-23 15:35:38 +09001640 set_server_up(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001641 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001642 }
Simon Hormancd5d7b62013-02-24 17:23:38 +09001643 check->state &= ~CHK_STATE_RUNNING;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001644
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001645 rv = 0;
1646 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09001647 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001648 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001649 }
Simon Horman4a741432013-02-23 15:35:38 +09001650 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02001651 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01001652
1653 reschedule:
1654 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09001655 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01001656 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01001657 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001658}
1659
Simon Horman5c942422013-11-25 10:46:32 +09001660static int start_check_task(struct check *check, int mininter,
1661 int nbcheck, int srvpos)
1662{
1663 struct task *t;
1664 /* task for the check */
1665 if ((t = task_new()) == NULL) {
1666 Alert("Starting [%s:%s] check: out of memory.\n",
1667 check->server->proxy->id, check->server->id);
1668 return 0;
1669 }
1670
1671 check->task = t;
1672 t->process = process_chk;
1673 t->context = check;
1674
1675 /* check this every ms */
1676 t->expire = tick_add(now_ms,
1677 MS_TO_TICKS(((mininter &&
1678 mininter >= srv_getinter(check)) ?
1679 mininter : srv_getinter(check)) * srvpos / nbcheck));
1680 check->start = now;
1681 task_queue(t);
1682
1683 return 1;
1684}
1685
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001686/*
1687 * Start health-check.
1688 * Returns 0 if OK, -1 if error, and prints the error in this case.
1689 */
1690int start_checks() {
1691
1692 struct proxy *px;
1693 struct server *s;
1694 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09001695 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001696
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001697 /* 1- count the checkers to run simultaneously.
1698 * We also determine the minimum interval among all of those which
1699 * have an interval larger than SRV_CHK_INTER_THRES. This interval
1700 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04001701 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02001702 * too short an interval for all others.
1703 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001704 for (px = proxy; px; px = px->next) {
1705 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01001706 if (s->slowstart) {
1707 if ((t = task_new()) == NULL) {
1708 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
1709 return -1;
1710 }
1711 /* We need a warmup task that will be called when the server
1712 * state switches from down to up.
1713 */
1714 s->warmup = t;
1715 t->process = server_warmup;
1716 t->context = s;
1717 t->expire = TICK_ETERNITY;
1718 }
1719
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001720 if (!(s->state & SRV_CHECKED))
1721 continue;
1722
Simon Horman4a741432013-02-23 15:35:38 +09001723 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
1724 (!mininter || mininter > srv_getinter(&s->check)))
1725 mininter = srv_getinter(&s->check);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001726
Simon Horman4a741432013-02-23 15:35:38 +09001727 nbcheck++;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001728 }
1729 }
1730
Simon Horman4a741432013-02-23 15:35:38 +09001731 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001732 return 0;
1733
1734 srand((unsigned)time(NULL));
1735
1736 /*
1737 * 2- start them as far as possible from each others. For this, we will
1738 * start them after their interval set to the min interval divided by
1739 * the number of servers, weighted by the server's position in the list.
1740 */
1741 for (px = proxy; px; px = px->next) {
1742 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09001743 /* A task for the main check */
1744 if (s->state & SRV_CHECKED) {
1745 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
1746 return -1;
1747 srvpos++;
1748 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001749
Simon Hormand60d6912013-11-25 10:46:36 +09001750 /* A task for a auxiliary agent check */
1751 if (s->state & SRV_AGENT_CHECKED) {
1752 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
1753 return -1;
1754 }
1755 srvpos++;
1756 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02001757 }
1758 }
1759 return 0;
1760}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001761
1762/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02001763 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01001764 * The buffer MUST be terminated by a null byte before calling this function.
1765 * Sets server status appropriately. The caller is responsible for ensuring
1766 * that the buffer contains at least 13 characters. If <done> is zero, we may
1767 * return 0 to indicate that data is required to decide of a match.
1768 */
1769static int httpchk_expect(struct server *s, int done)
1770{
1771 static char status_msg[] = "HTTP status check returned code <000>";
1772 char status_code[] = "000";
1773 char *contentptr;
1774 int crlf;
1775 int ret;
1776
1777 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
1778 case PR_O2_EXP_STS:
1779 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001780 memcpy(status_code, s->check.bi->data + 9, 3);
1781 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01001782
1783 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
1784 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
1785 else
1786 ret = regexec(s->proxy->expect_regex, status_code, MAX_MATCH, pmatch, 0) == 0;
1787
1788 /* we necessarily have the response, so there are no partial failures */
1789 if (s->proxy->options2 & PR_O2_EXP_INV)
1790 ret = !ret;
1791
Simon Horman4a741432013-02-23 15:35:38 +09001792 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01001793 break;
1794
1795 case PR_O2_EXP_STR:
1796 case PR_O2_EXP_RSTR:
1797 /* very simple response parser: ignore CR and only count consecutive LFs,
1798 * stop with contentptr pointing to first char after the double CRLF or
1799 * to '\0' if crlf < 2.
1800 */
1801 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02001802 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01001803 if (crlf >= 2)
1804 break;
1805 if (*contentptr == '\r')
1806 continue;
1807 else if (*contentptr == '\n')
1808 crlf++;
1809 else
1810 crlf = 0;
1811 }
1812
1813 /* Check that response contains a body... */
1814 if (crlf < 2) {
1815 if (!done)
1816 return 0;
1817
Simon Horman4a741432013-02-23 15:35:38 +09001818 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001819 "HTTP content check could not find a response body");
1820 return 1;
1821 }
1822
1823 /* Check that response body is not empty... */
1824 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02001825 if (!done)
1826 return 0;
1827
Simon Horman4a741432013-02-23 15:35:38 +09001828 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001829 "HTTP content check found empty response body");
1830 return 1;
1831 }
1832
1833 /* Check the response content against the supplied string
1834 * or regex... */
1835 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
1836 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
1837 else
1838 ret = regexec(s->proxy->expect_regex, contentptr, MAX_MATCH, pmatch, 0) == 0;
1839
1840 /* if we don't match, we may need to wait more */
1841 if (!ret && !done)
1842 return 0;
1843
1844 if (ret) {
1845 /* content matched */
1846 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09001847 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001848 "HTTP check matched unwanted content");
1849 else
Simon Horman4a741432013-02-23 15:35:38 +09001850 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01001851 "HTTP content check matched");
1852 }
1853 else {
1854 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09001855 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01001856 "HTTP check did not match unwanted content");
1857 else
Simon Horman4a741432013-02-23 15:35:38 +09001858 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01001859 "HTTP content check did not match");
1860 }
1861 break;
1862 }
1863 return 1;
1864}
1865
1866/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001867 * Local variables:
1868 * c-indent-level: 8
1869 * c-basic-offset: 8
1870 * End:
1871 */