blob: 35fd020e3e2c55fe7e3b61114ed7cf4915861a9c [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>
Willy Tarreau9b39dc52014-07-08 00:54:10 +020018#include <signal.h>
Simon Horman0ba0e4a2015-01-30 11:23:00 +090019#include <stdarg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020021#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020023#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024#include <unistd.h>
25#include <sys/socket.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040026#include <sys/types.h>
Simon Horman98637e52014-06-20 12:30:16 +090027#include <sys/wait.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028#include <netinet/in.h>
Willy Tarreau1274bc42009-07-15 07:16:31 +020029#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <arpa/inet.h>
31
Willy Tarreauc7e42382012-08-24 19:22:53 +020032#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/compat.h>
34#include <common/config.h>
35#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020036#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
39#include <types/global.h>
Simon Horman0ba0e4a2015-01-30 11:23:00 +090040#include <types/mailers.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020041#include <types/dns.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042
Baptiste Assmann69e273f2013-12-11 00:52:19 +010043#ifdef USE_OPENSSL
44#include <types/ssl_sock.h>
45#include <proto/ssl_sock.h>
46#endif /* USE_OPENSSL */
47
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020049#include <proto/checks.h>
Simon Hormana2b9dad2013-02-12 10:45:54 +090050#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/fd.h>
52#include <proto/log.h>
53#include <proto/queue.h>
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020054#include <proto/port_range.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010055#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010056#include <proto/proto_tcp.h>
Baptiste Assmann69e273f2013-12-11 00:52:19 +010057#include <proto/protocol.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010058#include <proto/proxy.h>
Willy Tarreaufb56aab2012-09-28 14:40:02 +020059#include <proto/raw_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/server.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010061#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020062#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020063#include <proto/log.h>
64#include <proto/dns.h>
65#include <proto/proto_udp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066
Willy Tarreaubd741542010-03-16 18:46:54 +010067static int httpchk_expect(struct server *s, int done);
Simon Hormane16c1b32015-01-30 11:22:57 +090068static int tcpcheck_get_step_id(struct check *);
Baptiste Assmann22b09d22015-05-01 08:03:04 +020069static char * tcpcheck_get_step_comment(struct check *, int);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +020070static void tcpcheck_main(struct connection *);
Willy Tarreaubd741542010-03-16 18:46:54 +010071
Simon Horman63a4a822012-03-19 07:24:41 +090072static const struct check_status check_statuses[HCHK_STATUS_SIZE] = {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010073 [HCHK_STATUS_UNKNOWN] = { CHK_RES_UNKNOWN, "UNK", "Unknown" },
74 [HCHK_STATUS_INI] = { CHK_RES_UNKNOWN, "INI", "Initializing" },
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020075 [HCHK_STATUS_START] = { /* SPECIAL STATUS*/ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020076
Willy Tarreau23964182014-05-20 20:56:30 +020077 /* Below we have finished checks */
78 [HCHK_STATUS_CHECKED] = { CHK_RES_NEUTRAL, "CHECKED", "No status change" },
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010079 [HCHK_STATUS_HANA] = { CHK_RES_FAILED, "HANA", "Health analyze" },
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010080
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010081 [HCHK_STATUS_SOCKERR] = { CHK_RES_FAILED, "SOCKERR", "Socket error" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020082
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010083 [HCHK_STATUS_L4OK] = { CHK_RES_PASSED, "L4OK", "Layer4 check passed" },
84 [HCHK_STATUS_L4TOUT] = { CHK_RES_FAILED, "L4TOUT", "Layer4 timeout" },
85 [HCHK_STATUS_L4CON] = { CHK_RES_FAILED, "L4CON", "Layer4 connection problem" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020086
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010087 [HCHK_STATUS_L6OK] = { CHK_RES_PASSED, "L6OK", "Layer6 check passed" },
88 [HCHK_STATUS_L6TOUT] = { CHK_RES_FAILED, "L6TOUT", "Layer6 timeout" },
89 [HCHK_STATUS_L6RSP] = { CHK_RES_FAILED, "L6RSP", "Layer6 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020090
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010091 [HCHK_STATUS_L7TOUT] = { CHK_RES_FAILED, "L7TOUT", "Layer7 timeout" },
92 [HCHK_STATUS_L7RSP] = { CHK_RES_FAILED, "L7RSP", "Layer7 invalid response" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020093
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020094 [HCHK_STATUS_L57DATA] = { /* DUMMY STATUS */ },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020095
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010096 [HCHK_STATUS_L7OKD] = { CHK_RES_PASSED, "L7OK", "Layer7 check passed" },
97 [HCHK_STATUS_L7OKCD] = { CHK_RES_CONDPASS, "L7OKC", "Layer7 check conditionally passed" },
98 [HCHK_STATUS_L7STS] = { CHK_RES_FAILED, "L7STS", "Layer7 wrong status" },
Simon Horman98637e52014-06-20 12:30:16 +090099
100 [HCHK_STATUS_PROCERR] = { CHK_RES_FAILED, "PROCERR", "External check error" },
101 [HCHK_STATUS_PROCTOUT] = { CHK_RES_FAILED, "PROCTOUT", "External check timeout" },
Cyril Bonté77010d82014-08-07 01:55:37 +0200102 [HCHK_STATUS_PROCOK] = { CHK_RES_PASSED, "PROCOK", "External check passed" },
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200103};
104
Cyril Bontéac92a062014-12-27 22:28:38 +0100105const struct extcheck_env extcheck_envs[EXTCHK_SIZE] = {
106 [EXTCHK_PATH] = { "PATH", EXTCHK_SIZE_EVAL_INIT },
107 [EXTCHK_HAPROXY_PROXY_NAME] = { "HAPROXY_PROXY_NAME", EXTCHK_SIZE_EVAL_INIT },
108 [EXTCHK_HAPROXY_PROXY_ID] = { "HAPROXY_PROXY_ID", EXTCHK_SIZE_EVAL_INIT },
109 [EXTCHK_HAPROXY_PROXY_ADDR] = { "HAPROXY_PROXY_ADDR", EXTCHK_SIZE_EVAL_INIT },
110 [EXTCHK_HAPROXY_PROXY_PORT] = { "HAPROXY_PROXY_PORT", EXTCHK_SIZE_EVAL_INIT },
111 [EXTCHK_HAPROXY_SERVER_NAME] = { "HAPROXY_SERVER_NAME", EXTCHK_SIZE_EVAL_INIT },
112 [EXTCHK_HAPROXY_SERVER_ID] = { "HAPROXY_SERVER_ID", EXTCHK_SIZE_EVAL_INIT },
113 [EXTCHK_HAPROXY_SERVER_ADDR] = { "HAPROXY_SERVER_ADDR", EXTCHK_SIZE_EVAL_INIT },
114 [EXTCHK_HAPROXY_SERVER_PORT] = { "HAPROXY_SERVER_PORT", EXTCHK_SIZE_EVAL_INIT },
115 [EXTCHK_HAPROXY_SERVER_MAXCONN] = { "HAPROXY_SERVER_MAXCONN", EXTCHK_SIZE_EVAL_INIT },
116 [EXTCHK_HAPROXY_SERVER_CURCONN] = { "HAPROXY_SERVER_CURCONN", EXTCHK_SIZE_ULONG },
117};
118
Simon Horman63a4a822012-03-19 07:24:41 +0900119static const struct analyze_status analyze_statuses[HANA_STATUS_SIZE] = { /* 0: ignore, 1: error, 2: OK */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100120 [HANA_STATUS_UNKNOWN] = { "Unknown", { 0, 0 }},
121
122 [HANA_STATUS_L4_OK] = { "L4 successful connection", { 2, 0 }},
123 [HANA_STATUS_L4_ERR] = { "L4 unsuccessful connection", { 1, 1 }},
124
125 [HANA_STATUS_HTTP_OK] = { "Correct http response", { 0, 2 }},
126 [HANA_STATUS_HTTP_STS] = { "Wrong http response", { 0, 1 }},
127 [HANA_STATUS_HTTP_HDRRSP] = { "Invalid http response (headers)", { 0, 1 }},
128 [HANA_STATUS_HTTP_RSP] = { "Invalid http response", { 0, 1 }},
129
130 [HANA_STATUS_HTTP_READ_ERROR] = { "Read error (http)", { 0, 1 }},
131 [HANA_STATUS_HTTP_READ_TIMEOUT] = { "Read timeout (http)", { 0, 1 }},
132 [HANA_STATUS_HTTP_BROKEN_PIPE] = { "Close from server (http)", { 0, 1 }},
133};
134
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200135/*
136 * Convert check_status code to description
137 */
138const char *get_check_status_description(short check_status) {
139
140 const char *desc;
141
142 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200143 desc = check_statuses[check_status].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200144 else
145 desc = NULL;
146
147 if (desc && *desc)
148 return desc;
149 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200150 return check_statuses[HCHK_STATUS_UNKNOWN].desc;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200151}
152
153/*
154 * Convert check_status code to short info
155 */
156const char *get_check_status_info(short check_status) {
157
158 const char *info;
159
160 if (check_status < HCHK_STATUS_SIZE)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200161 info = check_statuses[check_status].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200162 else
163 info = NULL;
164
165 if (info && *info)
166 return info;
167 else
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200168 return check_statuses[HCHK_STATUS_UNKNOWN].info;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200169}
170
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100171const char *get_analyze_status(short analyze_status) {
172
173 const char *desc;
174
175 if (analyze_status < HANA_STATUS_SIZE)
176 desc = analyze_statuses[analyze_status].desc;
177 else
178 desc = NULL;
179
180 if (desc && *desc)
181 return desc;
182 else
183 return analyze_statuses[HANA_STATUS_UNKNOWN].desc;
184}
185
Willy Tarreaua150cf12014-05-20 21:57:23 +0200186/* Builds a string containing some information about the health check's result.
187 * The output string is allocated from the trash chunks. If the check is NULL,
188 * NULL is returned. This is designed to be used when emitting logs about health
189 * checks.
Willy Tarreauddd329c2014-05-16 16:46:12 +0200190 */
Willy Tarreaua150cf12014-05-20 21:57:23 +0200191static const char *check_reason_string(struct check *check)
Willy Tarreauddd329c2014-05-16 16:46:12 +0200192{
Willy Tarreaua150cf12014-05-20 21:57:23 +0200193 struct chunk *msg;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200194
Willy Tarreaua150cf12014-05-20 21:57:23 +0200195 if (!check)
196 return NULL;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200197
Willy Tarreaua150cf12014-05-20 21:57:23 +0200198 msg = get_trash_chunk();
199 chunk_printf(msg, "reason: %s", get_check_status_description(check->status));
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200200
Willy Tarreaua150cf12014-05-20 21:57:23 +0200201 if (check->status >= HCHK_STATUS_L57DATA)
202 chunk_appendf(msg, ", code: %d", check->code);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200203
Willy Tarreaua150cf12014-05-20 21:57:23 +0200204 if (*check->desc) {
205 struct chunk src;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200206
Willy Tarreaua150cf12014-05-20 21:57:23 +0200207 chunk_appendf(msg, ", info: \"");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200208
Willy Tarreaua150cf12014-05-20 21:57:23 +0200209 chunk_initlen(&src, check->desc, 0, strlen(check->desc));
210 chunk_asciiencode(msg, &src, '"');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200211
Willy Tarreaua150cf12014-05-20 21:57:23 +0200212 chunk_appendf(msg, "\"");
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200213 }
214
Willy Tarreaua150cf12014-05-20 21:57:23 +0200215 if (check->duration >= 0)
216 chunk_appendf(msg, ", check duration: %ldms", check->duration);
217
218 return msg->str;
Krzysztof Piotr Oledzki99ab5f82009-09-27 17:28:21 +0200219}
220
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200221/*
Simon Horman4a741432013-02-23 15:35:38 +0900222 * Set check->status, update check->duration and fill check->result with
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200223 * an adequate CHK_RES_* value. The new check->health is computed based
224 * on the result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200225 *
226 * Show information in logs about failed health check if server is UP
227 * or succeeded health checks if server is DOWN.
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200228 */
Simon Horman4a741432013-02-23 15:35:38 +0900229static void set_server_check_status(struct check *check, short status, const char *desc)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100230{
Simon Horman4a741432013-02-23 15:35:38 +0900231 struct server *s = check->server;
Willy Tarreaubef1b322014-05-13 21:01:39 +0200232 short prev_status = check->status;
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200233 int report = 0;
Simon Horman4a741432013-02-23 15:35:38 +0900234
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200235 if (status == HCHK_STATUS_START) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100236 check->result = CHK_RES_UNKNOWN; /* no result yet */
Simon Horman4a741432013-02-23 15:35:38 +0900237 check->desc[0] = '\0';
238 check->start = now;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200239 return;
240 }
241
Simon Horman4a741432013-02-23 15:35:38 +0900242 if (!check->status)
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200243 return;
244
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200245 if (desc && *desc) {
Simon Horman4a741432013-02-23 15:35:38 +0900246 strncpy(check->desc, desc, HCHK_DESC_LEN-1);
247 check->desc[HCHK_DESC_LEN-1] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200248 } else
Simon Horman4a741432013-02-23 15:35:38 +0900249 check->desc[0] = '\0';
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200250
Simon Horman4a741432013-02-23 15:35:38 +0900251 check->status = status;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200252 if (check_statuses[status].result)
Simon Horman4a741432013-02-23 15:35:38 +0900253 check->result = check_statuses[status].result;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200254
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100255 if (status == HCHK_STATUS_HANA)
Simon Horman4a741432013-02-23 15:35:38 +0900256 check->duration = -1;
257 else if (!tv_iszero(&check->start)) {
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200258 /* set_server_check_status() may be called more than once */
Simon Horman4a741432013-02-23 15:35:38 +0900259 check->duration = tv_ms_elapsed(&check->start, &now);
260 tv_zero(&check->start);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200261 }
262
Willy Tarreau23964182014-05-20 20:56:30 +0200263 /* no change is expected if no state change occurred */
264 if (check->result == CHK_RES_NEUTRAL)
265 return;
266
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200267 report = 0;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200268
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200269 switch (check->result) {
270 case CHK_RES_FAILED:
Willy Tarreau12634e12014-05-23 11:32:36 +0200271 /* Failure to connect to the agent as a secondary check should not
272 * cause the server to be marked down.
273 */
274 if ((!(check->state & CHK_ST_AGENT) ||
Simon Hormaneaabd522015-02-26 11:26:17 +0900275 (check->status >= HCHK_STATUS_L57DATA)) &&
Willy Tarreau12634e12014-05-23 11:32:36 +0200276 (check->health >= check->rise)) {
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200277 s->counters.failed_checks++;
278 report = 1;
279 check->health--;
280 if (check->health < check->rise)
281 check->health = 0;
282 }
283 break;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200284
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200285 case CHK_RES_PASSED:
286 case CHK_RES_CONDPASS: /* "condpass" cannot make the first step but it OK after a "passed" */
287 if ((check->health < check->rise + check->fall - 1) &&
288 (check->result == CHK_RES_PASSED || check->health > 0)) {
289 report = 1;
290 check->health++;
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200291
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200292 if (check->health >= check->rise)
293 check->health = check->rise + check->fall - 1; /* OK now */
294 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200295
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200296 /* clear consecutive_errors if observing is enabled */
297 if (s->onerror)
298 s->consecutive_errors = 0;
299 break;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100300
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200301 default:
302 break;
303 }
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200304
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200305 if (s->proxy->options2 & PR_O2_LOGHCHKS &&
306 (status != prev_status || report)) {
307 chunk_printf(&trash,
Willy Tarreau12634e12014-05-23 11:32:36 +0200308 "%s check for %sserver %s/%s %s%s",
309 (check->state & CHK_ST_AGENT) ? "Agent" : "Health",
Willy Tarreauc93cd162014-05-13 15:54:22 +0200310 s->flags & SRV_F_BACKUP ? "backup " : "",
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100311 s->proxy->id, s->id,
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100312 (check->result == CHK_RES_CONDPASS) ? "conditionally ":"",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200313 (check->result >= CHK_RES_PASSED) ? "succeeded" : "failed");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200314
Willy Tarreaua150cf12014-05-20 21:57:23 +0200315 srv_append_status(&trash, s, check_reason_string(check), -1, 0);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200316
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100317 chunk_appendf(&trash, ", status: %d/%d %s",
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200318 (check->health >= check->rise) ? check->health - check->rise + 1 : check->health,
319 (check->health >= check->rise) ? check->fall : check->rise,
320 (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200321
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100322 Warning("%s.\n", trash.str);
323 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman7ea9be02015-04-30 13:10:33 +0900324 send_email_alert(s, LOG_INFO, "%s", trash.str);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200325 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200326}
327
Willy Tarreau4eec5472014-05-20 22:32:27 +0200328/* Marks the check <check>'s server down if the current check is already failed
329 * and the server is not down yet nor in maintenance.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200330 */
Willy Tarreau4eec5472014-05-20 22:32:27 +0200331static void check_notify_failure(struct check *check)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332{
Simon Horman4a741432013-02-23 15:35:38 +0900333 struct server *s = check->server;
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900334
Willy Tarreau7b1d47c2014-05-20 14:55:13 +0200335 /* The agent secondary check should only cause a server to be marked
336 * as down if check->status is HCHK_STATUS_L7STS, which indicates
337 * that the agent returned "fail", "stopped" or "down".
338 * The implication here is that failure to connect to the agent
339 * as a secondary check should not cause the server to be marked
340 * down. */
341 if ((check->state & CHK_ST_AGENT) && check->status != HCHK_STATUS_L7STS)
342 return;
343
Willy Tarreau4eec5472014-05-20 22:32:27 +0200344 if (check->health > 0)
345 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100346
Willy Tarreau4eec5472014-05-20 22:32:27 +0200347 /* We only report a reason for the check if we did not do so previously */
348 srv_set_stopped(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349}
350
Willy Tarreauaf549582014-05-16 17:37:50 +0200351/* Marks the check <check> as valid and tries to set its server up, provided
Willy Tarreau3e048382014-05-21 10:30:54 +0200352 * it isn't in maintenance, it is not tracking a down server and other checks
353 * comply. The rule is simple : by default, a server is up, unless any of the
354 * following conditions is true :
355 * - health check failed (check->health < rise)
356 * - agent check failed (agent->health < rise)
357 * - the server tracks a down server (track && track->state == STOPPED)
358 * Note that if the server has a slowstart, it will switch to STARTING instead
359 * of RUNNING. Also, only the health checks support the nolb mode, so the
360 * agent's success may not take the server out of this mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200361 */
Willy Tarreau3e048382014-05-21 10:30:54 +0200362static void check_notify_success(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200363{
Simon Horman4a741432013-02-23 15:35:38 +0900364 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100365
Willy Tarreauaf549582014-05-16 17:37:50 +0200366 if (s->admin & SRV_ADMF_MAINT)
367 return;
Cyril Bontécd19e512010-01-31 22:34:03 +0100368
Willy Tarreau3e048382014-05-21 10:30:54 +0200369 if (s->track && s->track->state == SRV_ST_STOPPED)
Willy Tarreauaf549582014-05-16 17:37:50 +0200370 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100371
Willy Tarreau3e048382014-05-21 10:30:54 +0200372 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
373 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100374
Willy Tarreau3e048382014-05-21 10:30:54 +0200375 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
376 return;
Willy Tarreauaf549582014-05-16 17:37:50 +0200377
Willy Tarreau3e048382014-05-21 10:30:54 +0200378 if ((check->state & CHK_ST_AGENT) && s->state == SRV_ST_STOPPING)
379 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100380
Willy Tarreau3e048382014-05-21 10:30:54 +0200381 srv_set_running(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100382}
383
Willy Tarreaudb58b792014-05-21 13:57:23 +0200384/* Marks the check <check> as valid and tries to set its server into stopping mode
385 * if it was running or starting, and provided it isn't in maintenance and other
386 * checks comply. The conditions for the server to be marked in stopping mode are
387 * the same as for it to be turned up. Also, only the health checks support the
388 * nolb mode.
Willy Tarreauaf549582014-05-16 17:37:50 +0200389 */
Willy Tarreaudb58b792014-05-21 13:57:23 +0200390static void check_notify_stopping(struct check *check)
Willy Tarreauaf549582014-05-16 17:37:50 +0200391{
Simon Horman4a741432013-02-23 15:35:38 +0900392 struct server *s = check->server;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100393
Willy Tarreauaf549582014-05-16 17:37:50 +0200394 if (s->admin & SRV_ADMF_MAINT)
395 return;
396
Willy Tarreaudb58b792014-05-21 13:57:23 +0200397 if (check->state & CHK_ST_AGENT)
398 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100399
Willy Tarreaudb58b792014-05-21 13:57:23 +0200400 if (s->track && s->track->state == SRV_ST_STOPPED)
401 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100402
Willy Tarreaudb58b792014-05-21 13:57:23 +0200403 if ((s->check.state & CHK_ST_ENABLED) && (s->check.health < s->check.rise))
404 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100405
Willy Tarreaudb58b792014-05-21 13:57:23 +0200406 if ((s->agent.state & CHK_ST_ENABLED) && (s->agent.health < s->agent.rise))
407 return;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100408
Willy Tarreaudb58b792014-05-21 13:57:23 +0200409 srv_set_stopping(s, (!s->track && !(s->proxy->options2 & PR_O2_LOGHCHKS)) ? check_reason_string(check) : NULL);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100410}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411
Willy Tarreau9fe7aae2013-12-31 23:47:37 +0100412/* note: use health_adjust() only, which first checks that the observe mode is
413 * enabled.
414 */
415void __health_adjust(struct server *s, short status)
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100416{
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100417 int failed;
418 int expire;
419
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100420 if (s->observe >= HANA_OBS_SIZE)
421 return;
422
Willy Tarreaubb956662013-01-24 00:37:39 +0100423 if (status >= HANA_STATUS_SIZE || !analyze_statuses[status].desc)
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100424 return;
425
426 switch (analyze_statuses[status].lr[s->observe - 1]) {
427 case 1:
428 failed = 1;
429 break;
430
431 case 2:
432 failed = 0;
433 break;
434
435 default:
436 return;
437 }
438
439 if (!failed) {
440 /* good: clear consecutive_errors */
441 s->consecutive_errors = 0;
442 return;
443 }
444
445 s->consecutive_errors++;
446
447 if (s->consecutive_errors < s->consecutive_errors_limit)
448 return;
449
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100450 chunk_printf(&trash, "Detected %d consecutive errors, last one was: %s",
451 s->consecutive_errors, get_analyze_status(status));
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100452
453 switch (s->onerror) {
454 case HANA_ONERR_FASTINTER:
455 /* force fastinter - nothing to do here as all modes force it */
456 break;
457
458 case HANA_ONERR_SUDDTH:
459 /* simulate a pre-fatal failed health check */
Simon Horman58c32972013-11-25 10:46:38 +0900460 if (s->check.health > s->check.rise)
461 s->check.health = s->check.rise + 1;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100462
463 /* no break - fall through */
464
465 case HANA_ONERR_FAILCHK:
466 /* simulate a failed health check */
Simon Horman4a741432013-02-23 15:35:38 +0900467 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200468 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100469 break;
470
471 case HANA_ONERR_MARKDWN:
472 /* mark server down */
Simon Horman58c32972013-11-25 10:46:38 +0900473 s->check.health = s->check.rise;
Simon Horman4a741432013-02-23 15:35:38 +0900474 set_server_check_status(&s->check, HCHK_STATUS_HANA, trash.str);
Willy Tarreau4eec5472014-05-20 22:32:27 +0200475 check_notify_failure(&s->check);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100476 break;
477
478 default:
479 /* write a warning? */
480 break;
481 }
482
483 s->consecutive_errors = 0;
484 s->counters.failed_hana++;
485
Simon Horman66183002013-02-23 10:16:43 +0900486 if (s->check.fastinter) {
487 expire = tick_add(now_ms, MS_TO_TICKS(s->check.fastinter));
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300488 if (s->check.task->expire > expire) {
Willy Tarreau5b3a2022012-09-28 15:01:02 +0200489 s->check.task->expire = expire;
Sergiy Prykhodko1d57e502013-09-21 12:05:00 +0300490 /* requeue check task with new expire */
491 task_queue(s->check.task);
492 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100493 }
494}
495
Willy Tarreaua1dab552014-04-14 15:04:54 +0200496static int httpchk_build_status_header(struct server *s, char *buffer, int size)
Willy Tarreauef781042010-01-27 11:53:01 +0100497{
498 int sv_state;
499 int ratio;
500 int hlen = 0;
Joseph Lynch514061c2015-01-15 17:52:59 -0800501 char addr[46];
502 char port[6];
Willy Tarreauef781042010-01-27 11:53:01 +0100503 const char *srv_hlt_st[7] = { "DOWN", "DOWN %d/%d",
504 "UP %d/%d", "UP",
505 "NOLB %d/%d", "NOLB",
506 "no check" };
507
508 memcpy(buffer + hlen, "X-Haproxy-Server-State: ", 24);
509 hlen += 24;
510
Willy Tarreauff5ae352013-12-11 20:36:34 +0100511 if (!(s->check.state & CHK_ST_ENABLED))
512 sv_state = 6;
Willy Tarreau892337c2014-05-13 23:41:20 +0200513 else if (s->state != SRV_ST_STOPPED) {
Simon Horman58c32972013-11-25 10:46:38 +0900514 if (s->check.health == s->check.rise + s->check.fall - 1)
Willy Tarreauef781042010-01-27 11:53:01 +0100515 sv_state = 3; /* UP */
516 else
517 sv_state = 2; /* going down */
518
Willy Tarreau892337c2014-05-13 23:41:20 +0200519 if (s->state == SRV_ST_STOPPING)
Willy Tarreauef781042010-01-27 11:53:01 +0100520 sv_state += 2;
521 } else {
Simon Horman125d0992013-02-24 17:23:38 +0900522 if (s->check.health)
Willy Tarreauef781042010-01-27 11:53:01 +0100523 sv_state = 1; /* going up */
524 else
525 sv_state = 0; /* DOWN */
526 }
527
Willy Tarreaua1dab552014-04-14 15:04:54 +0200528 hlen += snprintf(buffer + hlen, size - hlen,
Willy Tarreauef781042010-01-27 11:53:01 +0100529 srv_hlt_st[sv_state],
Willy Tarreau892337c2014-05-13 23:41:20 +0200530 (s->state != SRV_ST_STOPPED) ? (s->check.health - s->check.rise + 1) : (s->check.health),
531 (s->state != SRV_ST_STOPPED) ? (s->check.fall) : (s->check.rise));
Willy Tarreauef781042010-01-27 11:53:01 +0100532
Joseph Lynch514061c2015-01-15 17:52:59 -0800533 addr_to_str(&s->addr, addr, sizeof(addr));
534 port_to_str(&s->addr, port, sizeof(port));
535
536 hlen += snprintf(buffer + hlen, size - hlen, "; address=%s; port=%s; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d",
537 addr, port, s->proxy->id, s->id,
Willy Tarreauef781042010-01-27 11:53:01 +0100538 global.node,
539 (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
540 (s->proxy->lbprm.tot_weight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv,
541 s->cur_sess, s->proxy->beconn - s->proxy->nbpend,
542 s->nbpend);
543
Willy Tarreau892337c2014-05-13 23:41:20 +0200544 if ((s->state == SRV_ST_STARTING) &&
Willy Tarreauef781042010-01-27 11:53:01 +0100545 now.tv_sec < s->last_change + s->slowstart &&
546 now.tv_sec >= s->last_change) {
547 ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart);
Willy Tarreaua1dab552014-04-14 15:04:54 +0200548 hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio);
Willy Tarreauef781042010-01-27 11:53:01 +0100549 }
550
551 buffer[hlen++] = '\r';
552 buffer[hlen++] = '\n';
553
554 return hlen;
555}
556
Willy Tarreau20a18342013-12-05 00:31:46 +0100557/* Check the connection. If an error has already been reported or the socket is
558 * closed, keep errno intact as it is supposed to contain the valid error code.
559 * If no error is reported, check the socket's error queue using getsockopt().
560 * Warning, this must be done only once when returning from poll, and never
561 * after an I/O error was attempted, otherwise the error queue might contain
562 * inconsistent errors. If an error is detected, the CO_FL_ERROR is set on the
563 * socket. Returns non-zero if an error was reported, zero if everything is
564 * clean (including a properly closed socket).
565 */
566static int retrieve_errno_from_socket(struct connection *conn)
567{
568 int skerr;
569 socklen_t lskerr = sizeof(skerr);
570
571 if (conn->flags & CO_FL_ERROR && ((errno && errno != EAGAIN) || !conn->ctrl))
572 return 1;
573
Willy Tarreau3c728722014-01-23 13:50:42 +0100574 if (!conn_ctrl_ready(conn))
Willy Tarreau20a18342013-12-05 00:31:46 +0100575 return 0;
576
577 if (getsockopt(conn->t.sock.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == 0)
578 errno = skerr;
579
580 if (errno == EAGAIN)
581 errno = 0;
582
583 if (!errno) {
584 /* we could not retrieve an error, that does not mean there is
585 * none. Just don't change anything and only report the prior
586 * error if any.
587 */
588 if (conn->flags & CO_FL_ERROR)
589 return 1;
590 else
591 return 0;
592 }
593
594 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_WR_SH | CO_FL_SOCK_RD_SH;
595 return 1;
596}
597
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100598/* Try to collect as much information as possible on the connection status,
599 * and adjust the server status accordingly. It may make use of <errno_bck>
600 * if non-null when the caller is absolutely certain of its validity (eg:
601 * checked just after a syscall). If the caller doesn't have a valid errno,
602 * it can pass zero, and retrieve_errno_from_socket() will be called to try
603 * to extract errno from the socket. If no error is reported, it will consider
604 * the <expired> flag. This is intended to be used when a connection error was
605 * reported in conn->flags or when a timeout was reported in <expired>. The
606 * function takes care of not updating a server status which was already set.
607 * All situations where at least one of <expired> or CO_FL_ERROR are set
608 * produce a status.
609 */
610static void chk_report_conn_err(struct connection *conn, int errno_bck, int expired)
611{
612 struct check *check = conn->owner;
613 const char *err_msg;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200614 struct chunk *chk;
Willy Tarreau213c6782014-10-02 14:51:02 +0200615 int step;
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200616 char *comment;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100617
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100618 if (check->result != CHK_RES_UNKNOWN)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100619 return;
620
621 errno = errno_bck;
622 if (!errno || errno == EAGAIN)
623 retrieve_errno_from_socket(conn);
624
625 if (!(conn->flags & CO_FL_ERROR) && !expired)
626 return;
627
628 /* we'll try to build a meaningful error message depending on the
629 * context of the error possibly present in conn->err_code, and the
630 * socket error possibly collected above. This is useful to know the
631 * exact step of the L6 layer (eg: SSL handshake).
632 */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200633 chk = get_trash_chunk();
634
635 if (check->type == PR_O2_TCPCHK_CHK) {
Simon Hormane16c1b32015-01-30 11:22:57 +0900636 step = tcpcheck_get_step_id(check);
Willy Tarreau213c6782014-10-02 14:51:02 +0200637 if (!step)
638 chunk_printf(chk, " at initial connection step of tcp-check");
639 else {
640 chunk_printf(chk, " at step %d of tcp-check", step);
641 /* we were looking for a string */
642 if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_CONNECT) {
643 if (check->last_started_step->port)
644 chunk_appendf(chk, " (connect port %d)" ,check->last_started_step->port);
645 else
646 chunk_appendf(chk, " (connect)");
647 }
648 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_EXPECT) {
649 if (check->last_started_step->string)
Baptiste Assmann96a5c9b2015-05-01 08:09:29 +0200650 chunk_appendf(chk, " (expect string '%s')", check->last_started_step->string);
Willy Tarreau213c6782014-10-02 14:51:02 +0200651 else if (check->last_started_step->expect_regex)
652 chunk_appendf(chk, " (expect regex)");
653 }
654 else if (check->last_started_step && check->last_started_step->action == TCPCHK_ACT_SEND) {
655 chunk_appendf(chk, " (send)");
656 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +0200657
658 comment = tcpcheck_get_step_comment(check, step);
659 if (comment)
660 chunk_appendf(chk, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200661 }
662 }
663
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100664 if (conn->err_code) {
665 if (errno && errno != EAGAIN)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200666 chunk_printf(&trash, "%s (%s)%s", conn_err_code_str(conn), strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100667 else
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200668 chunk_printf(&trash, "%s%s", conn_err_code_str(conn), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100669 err_msg = trash.str;
670 }
671 else {
672 if (errno && errno != EAGAIN) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200673 chunk_printf(&trash, "%s%s", strerror(errno), chk->str);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100674 err_msg = trash.str;
675 }
676 else {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200677 err_msg = chk->str;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100678 }
679 }
680
681 if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L4_CONN)) == CO_FL_WAIT_L4_CONN) {
682 /* L4 not established (yet) */
683 if (conn->flags & CO_FL_ERROR)
684 set_server_check_status(check, HCHK_STATUS_L4CON, err_msg);
685 else if (expired)
686 set_server_check_status(check, HCHK_STATUS_L4TOUT, err_msg);
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200687
688 /*
689 * might be due to a server IP change.
690 * Let's trigger a DNS resolution if none are currently running.
691 */
692 if ((check->server->resolution) && (check->server->resolution->step == RSLV_STEP_NONE))
693 trigger_resolution(check->server);
694
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100695 }
696 else if ((conn->flags & (CO_FL_CONNECTED|CO_FL_WAIT_L6_CONN)) == CO_FL_WAIT_L6_CONN) {
697 /* L6 not established (yet) */
698 if (conn->flags & CO_FL_ERROR)
699 set_server_check_status(check, HCHK_STATUS_L6RSP, err_msg);
700 else if (expired)
701 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
702 }
703 else if (conn->flags & CO_FL_ERROR) {
704 /* I/O error after connection was established and before we could diagnose */
705 set_server_check_status(check, HCHK_STATUS_SOCKERR, err_msg);
706 }
707 else if (expired) {
708 /* connection established but expired check */
709 if (check->type == PR_O2_SSL3_CHK)
710 set_server_check_status(check, HCHK_STATUS_L6TOUT, err_msg);
711 else /* HTTP, SMTP, ... */
712 set_server_check_status(check, HCHK_STATUS_L7TOUT, err_msg);
713 }
714
715 return;
716}
717
Willy Tarreaubaaee002006-06-26 02:48:02 +0200718/*
719 * This function is used only for server health-checks. It handles
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200720 * the connection acknowledgement. If the proxy requires L7 health-checks,
721 * it sends the request. In other cases, it calls set_server_check_status()
Simon Horman4a741432013-02-23 15:35:38 +0900722 * to set check->status, check->duration and check->result.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200723 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200724static void event_srv_chk_w(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200725{
Simon Horman4a741432013-02-23 15:35:38 +0900726 struct check *check = conn->owner;
727 struct server *s = check->server;
Simon Horman4a741432013-02-23 15:35:38 +0900728 struct task *t = check->task;
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200729
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100730 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100731 goto out_wakeup;
732
Willy Tarreau310987a2014-01-22 19:46:33 +0100733 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100734 return;
735
Willy Tarreau20a18342013-12-05 00:31:46 +0100736 if (retrieve_errno_from_socket(conn)) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100737 chk_report_conn_err(conn, errno, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100738 __conn_data_stop_both(conn);
739 goto out_wakeup;
740 }
Krzysztof Piotr Oledzki6492db52010-01-02 22:03:01 +0100741
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100742 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
Willy Tarreau20a18342013-12-05 00:31:46 +0100743 /* if the output is closed, we can't do anything */
744 conn->flags |= CO_FL_ERROR;
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100745 chk_report_conn_err(conn, 0, 0);
Willy Tarreau20a18342013-12-05 00:31:46 +0100746 goto out_wakeup;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200747 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200748
Willy Tarreau06559ac2013-12-05 01:53:08 +0100749 /* here, we know that the connection is established. That's enough for
750 * a pure TCP check.
751 */
752 if (!check->type)
753 goto out_wakeup;
754
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200755 if (check->type == PR_O2_TCPCHK_CHK) {
756 tcpcheck_main(conn);
757 return;
758 }
759
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100760 if (check->bo->o) {
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100761 conn->xprt->snd_buf(conn, check->bo, 0);
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100762 if (conn->flags & CO_FL_ERROR) {
763 chk_report_conn_err(conn, errno, 0);
764 __conn_data_stop_both(conn);
765 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200766 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100767 if (check->bo->o)
768 return;
769 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200770
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100771 /* full request sent, we allow up to <timeout.check> if nonzero for a response */
772 if (s->proxy->timeout.check) {
773 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
774 task_queue(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200775 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100776 goto out_nowake;
777
Willy Tarreau83749182007-04-15 20:56:27 +0200778 out_wakeup:
Willy Tarreaufdccded2008-08-29 18:19:04 +0200779 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau83749182007-04-15 20:56:27 +0200780 out_nowake:
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200781 __conn_data_stop_send(conn); /* nothing more to write */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200782}
783
Willy Tarreaubaaee002006-06-26 02:48:02 +0200784/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200785 * This function is used only for server health-checks. It handles the server's
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +0200786 * reply to an HTTP request, SSL HELLO or MySQL client Auth. It calls
Simon Horman4a741432013-02-23 15:35:38 +0900787 * set_server_check_status() to update check->status, check->duration
788 * and check->result.
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200789
790 * The set_server_check_status function is called with HCHK_STATUS_L7OKD if
791 * an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP server
792 * returns 2xx, HCHK_STATUS_L6OK if an SSL server returns at least 5 bytes in
793 * response to an SSL HELLO (the principle is that this is enough to
794 * distinguish between an SSL server and a pure TCP relay). All other cases will
795 * call it with a proper error status like HCHK_STATUS_L7STS, HCHK_STATUS_L6RSP,
796 * etc.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200797 */
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200798static void event_srv_chk_r(struct connection *conn)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200799{
Simon Horman4a741432013-02-23 15:35:38 +0900800 struct check *check = conn->owner;
801 struct server *s = check->server;
802 struct task *t = check->task;
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200803 char *desc;
Willy Tarreau03938182010-03-17 21:52:07 +0100804 int done;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +0200805 unsigned short msglen;
Willy Tarreau83749182007-04-15 20:56:27 +0200806
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100807 if (unlikely(check->result == CHK_RES_FAILED))
Willy Tarreau83749182007-04-15 20:56:27 +0200808 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200809
Willy Tarreau310987a2014-01-22 19:46:33 +0100810 if (conn->flags & CO_FL_HANDSHAKE)
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200811 return;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200812
813 if (check->type == PR_O2_TCPCHK_CHK) {
814 tcpcheck_main(conn);
815 return;
816 }
Willy Tarreaufb56aab2012-09-28 14:40:02 +0200817
Willy Tarreau83749182007-04-15 20:56:27 +0200818 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
819 * but the connection was closed on the remote end. Fortunately, recv still
820 * works correctly and we don't need to do the getsockopt() on linux.
821 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000822
823 /* Set buffer to point to the end of the data already read, and check
824 * that there is free space remaining. If the buffer is full, proceed
825 * with running the checks without attempting another socket read.
826 */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000827
Willy Tarreau03938182010-03-17 21:52:07 +0100828 done = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +0000829
Simon Horman4a741432013-02-23 15:35:38 +0900830 conn->xprt->rcv_buf(conn, check->bi, check->bi->size);
Willy Tarreauf1503172012-09-28 19:39:36 +0200831 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
Willy Tarreau03938182010-03-17 21:52:07 +0100832 done = 1;
Simon Horman4a741432013-02-23 15:35:38 +0900833 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
Willy Tarreauf1503172012-09-28 19:39:36 +0200834 /* Report network errors only if we got no other data. Otherwise
835 * we'll let the upper layers decide whether the response is OK
836 * or not. It is very common that an RST sent by the server is
837 * reported as an error just after the last data chunk.
838 */
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100839 chk_report_conn_err(conn, errno, 0);
Willy Tarreauc1a07962010-03-16 20:55:43 +0100840 goto out_wakeup;
841 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200842 }
843
Willy Tarreau25e2ab52013-12-04 11:17:05 +0100844
Willy Tarreau03938182010-03-17 21:52:07 +0100845 /* Intermediate or complete response received.
Simon Horman4a741432013-02-23 15:35:38 +0900846 * Terminate string in check->bi->data buffer.
Willy Tarreau03938182010-03-17 21:52:07 +0100847 */
Simon Horman4a741432013-02-23 15:35:38 +0900848 if (check->bi->i < check->bi->size)
849 check->bi->data[check->bi->i] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100850 else {
Simon Horman4a741432013-02-23 15:35:38 +0900851 check->bi->data[check->bi->i - 1] = '\0';
Willy Tarreau03938182010-03-17 21:52:07 +0100852 done = 1; /* buffer full, don't wait for more data */
853 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200854
Nick Chalk57b1bf72010-03-16 15:50:46 +0000855 /* Run the checks... */
Simon Horman4a741432013-02-23 15:35:38 +0900856 switch (check->type) {
Willy Tarreau1620ec32011-08-06 17:05:02 +0200857 case PR_O2_HTTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900858 if (!done && check->bi->i < strlen("HTTP/1.0 000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100859 goto wait_more_data;
860
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100861 /* Check if the server speaks HTTP 1.X */
Simon Horman4a741432013-02-23 15:35:38 +0900862 if ((check->bi->i < strlen("HTTP/1.0 000\r")) ||
863 (memcmp(check->bi->data, "HTTP/1.", 7) != 0 ||
864 (*(check->bi->data + 12) != ' ' && *(check->bi->data + 12) != '\r')) ||
865 !isdigit((unsigned char) *(check->bi->data + 9)) || !isdigit((unsigned char) *(check->bi->data + 10)) ||
866 !isdigit((unsigned char) *(check->bi->data + 11))) {
867 cut_crlf(check->bi->data);
868 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200869
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100870 goto out_wakeup;
871 }
872
Simon Horman4a741432013-02-23 15:35:38 +0900873 check->code = str2uic(check->bi->data + 9);
874 desc = ltrim(check->bi->data + 12, ' ');
Nick Chalk57b1bf72010-03-16 15:50:46 +0000875
Willy Tarreaubd741542010-03-16 18:46:54 +0100876 if ((s->proxy->options & PR_O_DISABLE404) &&
Willy Tarreau892337c2014-05-13 23:41:20 +0200877 (s->state != SRV_ST_STOPPED) && (check->code == 404)) {
Nick Chalk57b1bf72010-03-16 15:50:46 +0000878 /* 404 may be accepted as "stopping" only if the server was up */
879 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900880 set_server_check_status(check, HCHK_STATUS_L7OKCD, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000881 }
Willy Tarreaubd741542010-03-16 18:46:54 +0100882 else if (s->proxy->options2 & PR_O2_EXP_TYPE) {
883 /* Run content verification check... We know we have at least 13 chars */
884 if (!httpchk_expect(s, done))
885 goto wait_more_data;
886 }
887 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
Simon Horman4a741432013-02-23 15:35:38 +0900888 else if (*(check->bi->data + 9) == '2' || *(check->bi->data + 9) == '3') {
Willy Tarreaubd741542010-03-16 18:46:54 +0100889 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900890 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Willy Tarreaubd741542010-03-16 18:46:54 +0100891 }
Nick Chalk57b1bf72010-03-16 15:50:46 +0000892 else {
893 cut_crlf(desc);
Simon Horman4a741432013-02-23 15:35:38 +0900894 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Nick Chalk57b1bf72010-03-16 15:50:46 +0000895 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200896 break;
897
898 case PR_O2_SSL3_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900899 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +0100900 goto wait_more_data;
901
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100902 /* Check for SSLv3 alert or handshake */
Simon Horman4a741432013-02-23 15:35:38 +0900903 if ((check->bi->i >= 5) && (*check->bi->data == 0x15 || *check->bi->data == 0x16))
904 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200905 else
Simon Horman4a741432013-02-23 15:35:38 +0900906 set_server_check_status(check, HCHK_STATUS_L6RSP, NULL);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200907 break;
908
909 case PR_O2_SMTP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +0900910 if (!done && check->bi->i < strlen("000\r"))
Willy Tarreau03938182010-03-17 21:52:07 +0100911 goto wait_more_data;
912
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200913 /* Check if the server speaks SMTP */
Simon Horman4a741432013-02-23 15:35:38 +0900914 if ((check->bi->i < strlen("000\r")) ||
915 (*(check->bi->data + 3) != ' ' && *(check->bi->data + 3) != '\r') ||
916 !isdigit((unsigned char) *check->bi->data) || !isdigit((unsigned char) *(check->bi->data + 1)) ||
917 !isdigit((unsigned char) *(check->bi->data + 2))) {
918 cut_crlf(check->bi->data);
919 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200920
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200921 goto out_wakeup;
922 }
923
Simon Horman4a741432013-02-23 15:35:38 +0900924 check->code = str2uic(check->bi->data);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200925
Simon Horman4a741432013-02-23 15:35:38 +0900926 desc = ltrim(check->bi->data + 3, ' ');
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +0200927 cut_crlf(desc);
928
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100929 /* Check for SMTP code 2xx (should be 250) */
Simon Horman4a741432013-02-23 15:35:38 +0900930 if (*check->bi->data == '2')
931 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200932 else
Simon Horman4a741432013-02-23 15:35:38 +0900933 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Willy Tarreau1620ec32011-08-06 17:05:02 +0200934 break;
935
Simon Hormana2b9dad2013-02-12 10:45:54 +0900936 case PR_O2_LB_AGENT_CHK: {
Willy Tarreau81f5d942013-12-09 20:51:51 +0100937 int status = HCHK_STATUS_CHECKED;
938 const char *hs = NULL; /* health status */
939 const char *as = NULL; /* admin status */
940 const char *ps = NULL; /* performance status */
941 const char *err = NULL; /* first error to report */
942 const char *wrn = NULL; /* first warning to report */
943 char *cmd, *p;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900944
Willy Tarreau81f5d942013-12-09 20:51:51 +0100945 /* We're getting an agent check response. The agent could
946 * have been disabled in the mean time with a long check
947 * still pending. It is important that we ignore the whole
948 * response.
949 */
950 if (!(check->server->agent.state & CHK_ST_ENABLED))
951 break;
952
953 /* The agent supports strings made of a single line ended by the
954 * first CR ('\r') or LF ('\n'). This line is composed of words
955 * delimited by spaces (' '), tabs ('\t'), or commas (','). The
956 * line may optionally contained a description of a state change
957 * after a sharp ('#'), which is only considered if a health state
958 * is announced.
959 *
960 * Words may be composed of :
961 * - a numeric weight suffixed by the percent character ('%').
962 * - a health status among "up", "down", "stopped", and "fail".
963 * - an admin status among "ready", "drain", "maint".
964 *
965 * These words may appear in any order. If multiple words of the
966 * same category appear, the last one wins.
967 */
968
Willy Tarreau9809b782013-12-11 21:40:11 +0100969 p = check->bi->data;
970 while (*p && *p != '\n' && *p != '\r')
971 p++;
972
973 if (!*p) {
974 if (!done)
975 goto wait_more_data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900976
Willy Tarreau9809b782013-12-11 21:40:11 +0100977 /* at least inform the admin that the agent is mis-behaving */
978 set_server_check_status(check, check->status, "Ignoring incomplete line from agent");
979 break;
980 }
Willy Tarreau81f5d942013-12-09 20:51:51 +0100981
Willy Tarreau9809b782013-12-11 21:40:11 +0100982 *p = 0;
Willy Tarreau81f5d942013-12-09 20:51:51 +0100983 cmd = check->bi->data;
Simon Hormana2b9dad2013-02-12 10:45:54 +0900984
Willy Tarreau81f5d942013-12-09 20:51:51 +0100985 while (*cmd) {
986 /* look for next word */
987 if (*cmd == ' ' || *cmd == '\t' || *cmd == ',') {
988 cmd++;
989 continue;
990 }
Simon Horman671b6f02013-11-25 10:46:39 +0900991
Willy Tarreau81f5d942013-12-09 20:51:51 +0100992 if (*cmd == '#') {
993 /* this is the beginning of a health status description,
994 * skip the sharp and blanks.
995 */
996 cmd++;
997 while (*cmd == '\t' || *cmd == ' ')
998 cmd++;
Simon Horman671b6f02013-11-25 10:46:39 +0900999 break;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001000 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001001
1002 /* find the end of the word so that we have a null-terminated
1003 * word between <cmd> and <p>.
1004 */
1005 p = cmd + 1;
1006 while (*p && *p != '\t' && *p != ' ' && *p != '\n' && *p != ',')
1007 p++;
1008 if (*p)
1009 *p++ = 0;
1010
1011 /* first, health statuses */
1012 if (strcasecmp(cmd, "up") == 0) {
1013 check->health = check->rise + check->fall - 1;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001014 status = HCHK_STATUS_L7OKD;
Willy Tarreau81f5d942013-12-09 20:51:51 +01001015 hs = cmd;
1016 }
1017 else if (strcasecmp(cmd, "down") == 0) {
1018 check->health = 0;
1019 status = HCHK_STATUS_L7STS;
1020 hs = cmd;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001021 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001022 else if (strcasecmp(cmd, "stopped") == 0) {
1023 check->health = 0;
1024 status = HCHK_STATUS_L7STS;
1025 hs = cmd;
1026 }
1027 else if (strcasecmp(cmd, "fail") == 0) {
1028 check->health = 0;
1029 status = HCHK_STATUS_L7STS;
1030 hs = cmd;
1031 }
1032 /* admin statuses */
1033 else if (strcasecmp(cmd, "ready") == 0) {
1034 as = cmd;
1035 }
1036 else if (strcasecmp(cmd, "drain") == 0) {
1037 as = cmd;
1038 }
1039 else if (strcasecmp(cmd, "maint") == 0) {
1040 as = cmd;
1041 }
1042 /* else try to parse a weight here and keep the last one */
1043 else if (isdigit((unsigned char)*cmd) && strchr(cmd, '%') != NULL) {
1044 ps = cmd;
1045 }
1046 else {
1047 /* keep a copy of the first error */
1048 if (!err)
1049 err = cmd;
1050 }
1051 /* skip to next word */
1052 cmd = p;
1053 }
1054 /* here, cmd points either to \0 or to the beginning of a
1055 * description. Skip possible leading spaces.
1056 */
1057 while (*cmd == ' ' || *cmd == '\n')
1058 cmd++;
1059
1060 /* First, update the admin status so that we avoid sending other
1061 * possibly useless warnings and can also update the health if
1062 * present after going back up.
1063 */
1064 if (as) {
1065 if (strcasecmp(as, "drain") == 0)
1066 srv_adm_set_drain(check->server);
1067 else if (strcasecmp(as, "maint") == 0)
1068 srv_adm_set_maint(check->server);
1069 else
1070 srv_adm_set_ready(check->server);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001071 }
1072
Willy Tarreau81f5d942013-12-09 20:51:51 +01001073 /* now change weights */
1074 if (ps) {
1075 const char *msg;
1076
1077 msg = server_parse_weight_change_request(s, ps);
1078 if (!wrn || !*wrn)
1079 wrn = msg;
1080 }
1081
1082 /* and finally health status */
1083 if (hs) {
1084 /* We'll report some of the warnings and errors we have
1085 * here. Down reports are critical, we leave them untouched.
1086 * Lack of report, or report of 'UP' leaves the room for
1087 * ERR first, then WARN.
Simon Hormana2b9dad2013-02-12 10:45:54 +09001088 */
Willy Tarreau81f5d942013-12-09 20:51:51 +01001089 const char *msg = cmd;
1090 struct chunk *t;
1091
1092 if (!*msg || status == HCHK_STATUS_L7OKD) {
1093 if (err && *err)
1094 msg = err;
1095 else if (wrn && *wrn)
1096 msg = wrn;
Simon Hormana2b9dad2013-02-12 10:45:54 +09001097 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001098
1099 t = get_trash_chunk();
1100 chunk_printf(t, "via agent : %s%s%s%s",
1101 hs, *msg ? " (" : "",
1102 msg, *msg ? ")" : "");
1103
1104 set_server_check_status(check, status, t->str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001105 }
Willy Tarreau81f5d942013-12-09 20:51:51 +01001106 else if (err && *err) {
1107 /* No status change but we'd like to report something odd.
1108 * Just report the current state and copy the message.
1109 */
1110 chunk_printf(&trash, "agent reports an error : %s", err);
1111 set_server_check_status(check, status/*check->status*/, trash.str);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001112
Willy Tarreau81f5d942013-12-09 20:51:51 +01001113 }
1114 else if (wrn && *wrn) {
1115 /* No status change but we'd like to report something odd.
1116 * Just report the current state and copy the message.
1117 */
1118 chunk_printf(&trash, "agent warns : %s", wrn);
1119 set_server_check_status(check, status/*check->status*/, trash.str);
1120 }
1121 else
1122 set_server_check_status(check, status, NULL);
Simon Hormana2b9dad2013-02-12 10:45:54 +09001123 break;
1124 }
1125
Willy Tarreau1620ec32011-08-06 17:05:02 +02001126 case PR_O2_PGSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001127 if (!done && check->bi->i < 9)
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001128 goto wait_more_data;
1129
Simon Horman4a741432013-02-23 15:35:38 +09001130 if (check->bi->data[0] == 'R') {
1131 set_server_check_status(check, HCHK_STATUS_L7OKD, "PostgreSQL server is ok");
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001132 }
1133 else {
Simon Horman4a741432013-02-23 15:35:38 +09001134 if ((check->bi->data[0] == 'E') && (check->bi->data[5]!=0) && (check->bi->data[6]!=0))
1135 desc = &check->bi->data[6];
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001136 else
1137 desc = "PostgreSQL unknown error";
1138
Simon Horman4a741432013-02-23 15:35:38 +09001139 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Rauf Kuliyev38b41562011-01-04 15:14:13 +01001140 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001141 break;
1142
1143 case PR_O2_REDIS_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001144 if (!done && check->bi->i < 7)
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001145 goto wait_more_data;
1146
Simon Horman4a741432013-02-23 15:35:38 +09001147 if (strcmp(check->bi->data, "+PONG\r\n") == 0) {
1148 set_server_check_status(check, HCHK_STATUS_L7OKD, "Redis server is ok");
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001149 }
1150 else {
Simon Horman4a741432013-02-23 15:35:38 +09001151 set_server_check_status(check, HCHK_STATUS_L7STS, check->bi->data);
Hervé COMMOWICKec032d62011-08-05 16:23:48 +02001152 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001153 break;
1154
1155 case PR_O2_MYSQL_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001156 if (!done && check->bi->i < 5)
Willy Tarreau03938182010-03-17 21:52:07 +01001157 goto wait_more_data;
1158
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001159 if (s->proxy->check_len == 0) { // old mode
Simon Horman4a741432013-02-23 15:35:38 +09001160 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001161 /* We set the MySQL Version in description for information purpose
1162 * FIXME : it can be cool to use MySQL Version for other purpose,
1163 * like mark as down old MySQL server.
1164 */
Simon Horman4a741432013-02-23 15:35:38 +09001165 if (check->bi->i > 51) {
1166 desc = ltrim(check->bi->data + 5, ' ');
1167 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001168 }
1169 else {
1170 if (!done)
1171 goto wait_more_data;
1172 /* it seems we have a OK packet but without a valid length,
1173 * it must be a protocol error
1174 */
Simon Horman4a741432013-02-23 15:35:38 +09001175 set_server_check_status(check, HCHK_STATUS_L7RSP, check->bi->data);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001176 }
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001177 }
1178 else {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001179 /* An error message is attached in the Error packet */
Simon Horman4a741432013-02-23 15:35:38 +09001180 desc = ltrim(check->bi->data + 7, ' ');
1181 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001182 }
1183 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001184 unsigned int first_packet_len = ((unsigned int) *check->bi->data) +
1185 (((unsigned int) *(check->bi->data + 1)) << 8) +
1186 (((unsigned int) *(check->bi->data + 2)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001187
Simon Horman4a741432013-02-23 15:35:38 +09001188 if (check->bi->i == first_packet_len + 4) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001189 /* MySQL Error packet always begin with field_count = 0xff */
Simon Horman4a741432013-02-23 15:35:38 +09001190 if (*(check->bi->data + 4) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001191 /* We have only one MySQL packet and it is a Handshake Initialization packet
1192 * but we need to have a second packet to know if it is alright
1193 */
Simon Horman4a741432013-02-23 15:35:38 +09001194 if (!done && check->bi->i < first_packet_len + 5)
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001195 goto wait_more_data;
1196 }
1197 else {
1198 /* We have only one packet and it is an Error packet,
1199 * an error message is attached, so we can display it
1200 */
Simon Horman4a741432013-02-23 15:35:38 +09001201 desc = &check->bi->data[7];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001202 //Warning("onlyoneERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001203 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001204 }
Simon Horman4a741432013-02-23 15:35:38 +09001205 } else if (check->bi->i > first_packet_len + 4) {
1206 unsigned int second_packet_len = ((unsigned int) *(check->bi->data + first_packet_len + 4)) +
1207 (((unsigned int) *(check->bi->data + first_packet_len + 5)) << 8) +
1208 (((unsigned int) *(check->bi->data + first_packet_len + 6)) << 16);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001209
Simon Horman4a741432013-02-23 15:35:38 +09001210 if (check->bi->i == first_packet_len + 4 + second_packet_len + 4 ) {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001211 /* We have 2 packets and that's good */
1212 /* Check if the second packet is a MySQL Error packet or not */
Simon Horman4a741432013-02-23 15:35:38 +09001213 if (*(check->bi->data + first_packet_len + 8) != '\xff') {
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001214 /* No error packet */
1215 /* We set the MySQL Version in description for information purpose */
Simon Horman4a741432013-02-23 15:35:38 +09001216 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001217 //Warning("2packetOK: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001218 set_server_check_status(check, HCHK_STATUS_L7OKD, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001219 }
1220 else {
1221 /* An error message is attached in the Error packet
1222 * so we can display it ! :)
1223 */
Simon Horman4a741432013-02-23 15:35:38 +09001224 desc = &check->bi->data[first_packet_len+11];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001225 //Warning("2packetERR: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001226 set_server_check_status(check, HCHK_STATUS_L7STS, desc);
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001227 }
1228 }
1229 }
1230 else {
Willy Tarreau03938182010-03-17 21:52:07 +01001231 if (!done)
1232 goto wait_more_data;
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001233 /* it seems we have a Handshake Initialization packet but without a valid length,
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001234 * it must be a protocol error
1235 */
Simon Horman4a741432013-02-23 15:35:38 +09001236 desc = &check->bi->data[5];
Hervé COMMOWICK8776f1b2010-10-18 15:58:36 +02001237 //Warning("protoerr: %s\n", desc);
Simon Horman4a741432013-02-23 15:35:38 +09001238 set_server_check_status(check, HCHK_STATUS_L7RSP, desc);
Hervé COMMOWICK698ae002010-01-12 09:25:13 +01001239 }
1240 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001241 break;
1242
1243 case PR_O2_LDAP_CHK:
Simon Horman4a741432013-02-23 15:35:38 +09001244 if (!done && check->bi->i < 14)
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001245 goto wait_more_data;
1246
1247 /* Check if the server speaks LDAP (ASN.1/BER)
1248 * http://en.wikipedia.org/wiki/Basic_Encoding_Rules
1249 * http://tools.ietf.org/html/rfc4511
1250 */
1251
1252 /* http://tools.ietf.org/html/rfc4511#section-4.1.1
1253 * LDAPMessage: 0x30: SEQUENCE
1254 */
Simon Horman4a741432013-02-23 15:35:38 +09001255 if ((check->bi->i < 14) || (*(check->bi->data) != '\x30')) {
1256 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001257 }
1258 else {
1259 /* size of LDAPMessage */
Simon Horman4a741432013-02-23 15:35:38 +09001260 msglen = (*(check->bi->data + 1) & 0x80) ? (*(check->bi->data + 1) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001261
1262 /* http://tools.ietf.org/html/rfc4511#section-4.2.2
1263 * messageID: 0x02 0x01 0x01: INTEGER 1
1264 * protocolOp: 0x61: bindResponse
1265 */
1266 if ((msglen > 2) ||
Simon Horman4a741432013-02-23 15:35:38 +09001267 (memcmp(check->bi->data + 2 + msglen, "\x02\x01\x01\x61", 4) != 0)) {
1268 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001269
1270 goto out_wakeup;
1271 }
1272
1273 /* size of bindResponse */
Simon Horman4a741432013-02-23 15:35:38 +09001274 msglen += (*(check->bi->data + msglen + 6) & 0x80) ? (*(check->bi->data + msglen + 6) & 0x7f) : 0;
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001275
1276 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1277 * ldapResult: 0x0a 0x01: ENUMERATION
1278 */
1279 if ((msglen > 4) ||
Simon Horman4a741432013-02-23 15:35:38 +09001280 (memcmp(check->bi->data + 7 + msglen, "\x0a\x01", 2) != 0)) {
1281 set_server_check_status(check, HCHK_STATUS_L7RSP, "Not LDAPv3 protocol");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001282
1283 goto out_wakeup;
1284 }
1285
1286 /* http://tools.ietf.org/html/rfc4511#section-4.1.9
1287 * resultCode
1288 */
Simon Horman4a741432013-02-23 15:35:38 +09001289 check->code = *(check->bi->data + msglen + 9);
1290 if (check->code) {
1291 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 +02001292 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001293 set_server_check_status(check, HCHK_STATUS_L7OKD, "Success");
Gabor Lekenyb4c81e42010-09-29 18:17:05 +02001294 }
1295 }
Willy Tarreau1620ec32011-08-06 17:05:02 +02001296 break;
1297
1298 default:
Willy Tarreau06559ac2013-12-05 01:53:08 +01001299 /* for other checks (eg: pure TCP), delegate to the main task */
Willy Tarreau1620ec32011-08-06 17:05:02 +02001300 break;
1301 } /* switch */
Willy Tarreau83749182007-04-15 20:56:27 +02001302
Willy Tarreauc7dd71a2007-11-30 08:33:21 +01001303 out_wakeup:
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001304 /* collect possible new errors */
1305 if (conn->flags & CO_FL_ERROR)
1306 chk_report_conn_err(conn, 0, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001307
Nick Chalk57b1bf72010-03-16 15:50:46 +00001308 /* Reset the check buffer... */
Simon Horman4a741432013-02-23 15:35:38 +09001309 *check->bi->data = '\0';
1310 check->bi->i = 0;
Nick Chalk57b1bf72010-03-16 15:50:46 +00001311
Willy Tarreaufd29cc52012-11-23 09:18:20 +01001312 /* Close the connection... We absolutely want to perform a hard close
1313 * and reset the connection if some data are pending, otherwise we end
1314 * up with many TIME_WAITs and eat all the source port range quickly.
1315 * To avoid sending RSTs all the time, we first try to drain pending
1316 * data.
1317 */
Willy Tarreaub4017d02015-03-12 23:11:26 +01001318 __conn_data_stop_both(conn);
1319 conn_data_shutw_hard(conn);
Willy Tarreau2b57cb82013-06-10 19:56:38 +02001320
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001321 /* OK, let's not stay here forever */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001322 if (check->result == CHK_RES_FAILED)
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001323 conn->flags |= CO_FL_ERROR;
1324
Willy Tarreaufdccded2008-08-29 18:19:04 +02001325 task_wakeup(t, TASK_WOKEN_IO);
Willy Tarreau3267d362012-08-17 23:53:56 +02001326 return;
Willy Tarreau03938182010-03-17 21:52:07 +01001327
1328 wait_more_data:
Willy Tarreauf817e9f2014-01-10 16:58:45 +01001329 __conn_data_want_recv(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001330}
1331
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001332/*
1333 * This function is used only for server health-checks. It handles connection
1334 * status updates including errors. If necessary, it wakes the check task up.
1335 * It always returns 0.
1336 */
1337static int wake_srv_chk(struct connection *conn)
Willy Tarreau20bea422012-07-06 12:00:49 +02001338{
Simon Horman4a741432013-02-23 15:35:38 +09001339 struct check *check = conn->owner;
Willy Tarreau20bea422012-07-06 12:00:49 +02001340
Willy Tarreau6c560da2012-11-24 11:14:45 +01001341 if (unlikely(conn->flags & CO_FL_ERROR)) {
Willy Tarreau02b0f582013-12-03 15:42:33 +01001342 /* We may get error reports bypassing the I/O handlers, typically
1343 * the case when sending a pure TCP check which fails, then the I/O
1344 * handlers above are not called. This is completely handled by the
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001345 * main processing task so let's simply wake it up. If we get here,
1346 * we expect errno to still be valid.
1347 */
1348 chk_report_conn_err(conn, errno, 0);
1349
Willy Tarreau2d351b62013-12-05 02:36:25 +01001350 __conn_data_stop_both(conn);
1351 task_wakeup(check->task, TASK_WOKEN_IO);
1352 }
Willy Tarreau3be293f2014-02-05 18:31:24 +01001353 else if (!(conn->flags & (CO_FL_DATA_RD_ENA|CO_FL_DATA_WR_ENA|CO_FL_HANDSHAKE))) {
1354 /* we may get here if only a connection probe was required : we
1355 * don't have any data to send nor anything expected in response,
1356 * so the completion of the connection establishment is enough.
1357 */
1358 task_wakeup(check->task, TASK_WOKEN_IO);
1359 }
Willy Tarreau2d351b62013-12-05 02:36:25 +01001360
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01001361 if (check->result != CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01001362 /* We're here because nobody wants to handle the error, so we
1363 * sure want to abort the hard way.
Willy Tarreau02b0f582013-12-03 15:42:33 +01001364 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01001365 conn_sock_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02001366 conn_force_close(conn);
Willy Tarreau2d351b62013-12-05 02:36:25 +01001367 }
Willy Tarreau3267d362012-08-17 23:53:56 +02001368 return 0;
Willy Tarreau20bea422012-07-06 12:00:49 +02001369}
1370
Willy Tarreaufb56aab2012-09-28 14:40:02 +02001371struct data_cb check_conn_cb = {
1372 .recv = event_srv_chk_r,
1373 .send = event_srv_chk_w,
1374 .wake = wake_srv_chk,
1375};
1376
Willy Tarreaubaaee002006-06-26 02:48:02 +02001377/*
Willy Tarreau2e993902011-10-31 11:53:20 +01001378 * updates the server's weight during a warmup stage. Once the final weight is
1379 * reached, the task automatically stops. Note that any server status change
1380 * must have updated s->last_change accordingly.
1381 */
1382static struct task *server_warmup(struct task *t)
1383{
1384 struct server *s = t->context;
1385
1386 /* by default, plan on stopping the task */
1387 t->expire = TICK_ETERNITY;
Willy Tarreau20125212014-05-13 19:44:56 +02001388 if ((s->admin & SRV_ADMF_MAINT) ||
Willy Tarreau892337c2014-05-13 23:41:20 +02001389 (s->state != SRV_ST_STARTING))
Willy Tarreau2e993902011-10-31 11:53:20 +01001390 return t;
1391
Willy Tarreau892337c2014-05-13 23:41:20 +02001392 /* recalculate the weights and update the state */
Willy Tarreau004e0452013-11-21 11:22:01 +01001393 server_recalc_eweight(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001394
1395 /* probably that we can refill this server with a bit more connections */
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001396 pendconn_grab_from_px(s);
Willy Tarreau2e993902011-10-31 11:53:20 +01001397
1398 /* get back there in 1 second or 1/20th of the slowstart interval,
1399 * whichever is greater, resulting in small 5% steps.
1400 */
Willy Tarreau892337c2014-05-13 23:41:20 +02001401 if (s->state == SRV_ST_STARTING)
Willy Tarreau2e993902011-10-31 11:53:20 +01001402 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
1403 return t;
1404}
1405
1406/*
Simon Horman98637e52014-06-20 12:30:16 +09001407 * establish a server health-check that makes use of a connection.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001408 *
1409 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001410 * - SF_ERR_NONE if everything's OK and tcpcheck_main() was not called
1411 * - SF_ERR_UP if if everything's OK and tcpcheck_main() was called
1412 * - SF_ERR_SRVTO if there are no more servers
1413 * - SF_ERR_SRVCL if the connection was refused by the server
1414 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1415 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1416 * - SF_ERR_INTERNAL for any other purely internal errors
1417 * Additionnally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Hormanb00d17a2014-06-13 16:18:16 +09001418 * Note that we try to prevent the network stack from sending the ACK during the
1419 * connect() when a pure TCP check is used (without PROXY protocol).
1420 */
Simon Horman98637e52014-06-20 12:30:16 +09001421static int connect_conn_chk(struct task *t)
Simon Hormanb00d17a2014-06-13 16:18:16 +09001422{
1423 struct check *check = t->context;
1424 struct server *s = check->server;
1425 struct connection *conn = check->conn;
1426 struct protocol *proto;
1427 int ret;
Willy Tarreauf3d34822014-12-08 12:11:28 +01001428 int quickack;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001429
1430 /* tcpcheck send/expect initialisation */
1431 if (check->type == PR_O2_TCPCHK_CHK)
1432 check->current_step = NULL;
1433
1434 /* prepare the check buffer.
1435 * This should not be used if check is the secondary agent check
1436 * of a server as s->proxy->check_req will relate to the
1437 * configuration of the primary check. Similarly, tcp-check uses
1438 * its own strings.
1439 */
1440 if (check->type && check->type != PR_O2_TCPCHK_CHK && !(check->state & CHK_ST_AGENT)) {
1441 bo_putblk(check->bo, s->proxy->check_req, s->proxy->check_len);
1442
1443 /* we want to check if this host replies to HTTP or SSLv3 requests
1444 * so we'll send the request, and won't wake the checker up now.
1445 */
1446 if ((check->type) == PR_O2_SSL3_CHK) {
1447 /* SSL requires that we put Unix time in the request */
1448 int gmt_time = htonl(date.tv_sec);
1449 memcpy(check->bo->data + 11, &gmt_time, 4);
1450 }
1451 else if ((check->type) == PR_O2_HTTP_CHK) {
1452 if (s->proxy->options2 & PR_O2_CHK_SNDST)
1453 bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size));
Cyril Bonté32602d22015-01-30 00:07:07 +01001454 /* prevent HTTP keep-alive when "http-check expect" is used */
1455 if (s->proxy->options2 & PR_O2_EXP_TYPE)
1456 bo_putstr(check->bo, "Connection: close\r\n");
Simon Hormanb00d17a2014-06-13 16:18:16 +09001457 bo_putstr(check->bo, "\r\n");
1458 *check->bo->p = '\0'; /* to make gdb output easier to read */
1459 }
1460 }
1461
James Brown55f9ff12015-10-21 18:19:05 -07001462 if ((check->type & PR_O2_LB_AGENT_CHK) && check->send_string_len) {
1463 bo_putblk(check->bo, check->send_string, check->send_string_len);
1464 }
1465
Simon Hormanb00d17a2014-06-13 16:18:16 +09001466 /* prepare a new connection */
1467 conn_init(conn);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001468
Simon Horman41f58762015-01-30 11:22:56 +09001469 if (is_addr(&check->addr)) {
Simon Hormanb00d17a2014-06-13 16:18:16 +09001470 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09001471 conn->addr.to = check->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001472 }
1473 else {
1474 /* we'll connect to the addr on the server */
1475 conn->addr.to = s->addr;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001476 }
1477
1478 if (check->port) {
1479 set_host_port(&conn->addr.to, check->port);
1480 }
1481
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01001482 proto = protocol_by_family(conn->addr.to.ss_family);
1483
1484 conn_prepare(conn, proto, check->xprt);
1485 conn_attach(conn, check, &check_conn_cb);
1486 conn->target = &s->obj_type;
1487
1488 /* no client address */
1489 clear_addr(&conn->addr.from);
1490
Willy Tarreauf3d34822014-12-08 12:11:28 +01001491 /* only plain tcp-check supports quick ACK */
1492 quickack = check->type == 0 || check->type == PR_O2_TCPCHK_CHK;
1493
Simon Hormane16c1b32015-01-30 11:22:57 +09001494 if (check->type == PR_O2_TCPCHK_CHK && !LIST_ISEMPTY(check->tcpcheck_rules)) {
Willy Tarreau5581c272015-05-13 12:24:53 +02001495 struct tcpcheck_rule *r;
1496
1497 r = LIST_NEXT(check->tcpcheck_rules, struct tcpcheck_rule *, list);
1498
Simon Hormanb00d17a2014-06-13 16:18:16 +09001499 /* if first step is a 'connect', then tcpcheck_main must run it */
1500 if (r->action == TCPCHK_ACT_CONNECT) {
1501 tcpcheck_main(conn);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001502 return SF_ERR_UP;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001503 }
Willy Tarreauf3d34822014-12-08 12:11:28 +01001504 if (r->action == TCPCHK_ACT_EXPECT)
1505 quickack = 0;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001506 }
1507
Willy Tarreaue7dff022015-04-03 01:14:29 +02001508 ret = SF_ERR_INTERNAL;
Simon Hormanb00d17a2014-06-13 16:18:16 +09001509 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01001510 ret = proto->connect(conn, check->type, quickack ? 2 : 0);
Simon Hormanb00d17a2014-06-13 16:18:16 +09001511 conn->flags |= CO_FL_WAKE_DATA;
1512 if (s->check.send_proxy) {
1513 conn->send_proxy_ofs = 1;
1514 conn->flags |= CO_FL_SEND_PROXY;
1515 }
1516
1517 return ret;
1518}
1519
Simon Horman98637e52014-06-20 12:30:16 +09001520static struct list pid_list = LIST_HEAD_INIT(pid_list);
1521static struct pool_head *pool2_pid_list;
1522
1523void block_sigchld(void)
1524{
1525 sigset_t set;
1526 sigemptyset(&set);
1527 sigaddset(&set, SIGCHLD);
1528 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
1529}
1530
1531void unblock_sigchld(void)
1532{
1533 sigset_t set;
1534 sigemptyset(&set);
1535 assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
1536}
1537
1538/* Call with SIGCHLD blocked */
1539static struct pid_list *pid_list_add(pid_t pid, struct task *t)
1540{
1541 struct pid_list *elem;
1542 struct check *check = t->context;
1543
1544 elem = pool_alloc2(pool2_pid_list);
1545 if (!elem)
1546 return NULL;
1547 elem->pid = pid;
1548 elem->t = t;
1549 elem->exited = 0;
1550 check->curpid = elem;
1551 LIST_INIT(&elem->list);
1552 LIST_ADD(&pid_list, &elem->list);
1553 return elem;
1554}
1555
1556/* Blocks blocks and then unblocks SIGCHLD */
1557static void pid_list_del(struct pid_list *elem)
1558{
1559 struct check *check;
1560
1561 if (!elem)
1562 return;
1563
1564 block_sigchld();
1565 LIST_DEL(&elem->list);
1566 unblock_sigchld();
1567 if (!elem->exited)
1568 kill(elem->pid, SIGTERM);
1569
1570 check = elem->t->context;
1571 check->curpid = NULL;
1572 pool_free2(pool2_pid_list, elem);
1573}
1574
1575/* Called from inside SIGCHLD handler, SIGCHLD is blocked */
1576static void pid_list_expire(pid_t pid, int status)
1577{
1578 struct pid_list *elem;
1579
1580 list_for_each_entry(elem, &pid_list, list) {
1581 if (elem->pid == pid) {
1582 elem->t->expire = now_ms;
1583 elem->status = status;
1584 elem->exited = 1;
Cyril Bonté9dbcfab2014-08-07 01:55:39 +02001585 task_wakeup(elem->t, TASK_WOKEN_IO);
Simon Horman98637e52014-06-20 12:30:16 +09001586 return;
1587 }
1588 }
1589}
1590
1591static void sigchld_handler(int signal)
1592{
1593 pid_t pid;
1594 int status;
1595 while ((pid = waitpid(0, &status, WNOHANG)) > 0)
1596 pid_list_expire(pid, status);
1597}
1598
1599static int init_pid_list(void) {
1600 struct sigaction action = {
1601 .sa_handler = sigchld_handler,
1602 .sa_flags = SA_NOCLDSTOP
1603 };
1604
1605 if (pool2_pid_list != NULL)
1606 /* Nothing to do */
1607 return 0;
1608
1609 if (sigaction(SIGCHLD, &action, NULL)) {
1610 Alert("Failed to set signal handler for external health checks: %s. Aborting.\n",
1611 strerror(errno));
1612 return 1;
1613 }
1614
1615 pool2_pid_list = create_pool("pid_list", sizeof(struct pid_list), MEM_F_SHARED);
1616 if (pool2_pid_list == NULL) {
1617 Alert("Failed to allocate memory pool for external health checks: %s. Aborting.\n",
1618 strerror(errno));
1619 return 1;
1620 }
1621
1622 return 0;
1623}
1624
Cyril Bontéac92a062014-12-27 22:28:38 +01001625/* helper macro to set an environment variable and jump to a specific label on failure. */
1626#define EXTCHK_SETENV(check, envidx, value, fail) { if (extchk_setenv(check, envidx, value)) goto fail; }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001627
1628/*
Cyril Bontéac92a062014-12-27 22:28:38 +01001629 * helper function to allocate enough memory to store an environment variable.
1630 * It will also check that the environment variable is updatable, and silently
1631 * fail if not.
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001632 */
Cyril Bontéac92a062014-12-27 22:28:38 +01001633static int extchk_setenv(struct check *check, int idx, const char *value)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001634{
1635 int len, ret;
Cyril Bontéac92a062014-12-27 22:28:38 +01001636 char *envname;
1637 int vmaxlen;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001638
Cyril Bontéac92a062014-12-27 22:28:38 +01001639 if (idx < 0 || idx >= EXTCHK_SIZE) {
1640 Alert("Illegal environment variable index %d. Aborting.\n", idx);
1641 return 1;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001642 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001643
1644 envname = extcheck_envs[idx].name;
1645 vmaxlen = extcheck_envs[idx].vmaxlen;
1646
1647 /* Check if the environment variable is already set, and silently reject
1648 * the update if this one is not updatable. */
1649 if ((vmaxlen == EXTCHK_SIZE_EVAL_INIT) && (check->envp[idx]))
1650 return 0;
1651
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001652 /* Instead of sending NOT_USED, sending an empty value is preferable */
1653 if (strcmp(value, "NOT_USED") == 0) {
1654 value = "";
1655 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001656
1657 len = strlen(envname) + 1;
1658 if (vmaxlen == EXTCHK_SIZE_EVAL_INIT)
1659 len += strlen(value);
1660 else
1661 len += vmaxlen;
1662
1663 if (!check->envp[idx])
1664 check->envp[idx] = malloc(len + 1);
1665
1666 if (!check->envp[idx]) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001667 Alert("Failed to allocate memory for the environment variable '%s'. Aborting.\n", envname);
1668 return 1;
1669 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001670 ret = snprintf(check->envp[idx], len + 1, "%s=%s", envname, value);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001671 if (ret < 0) {
1672 Alert("Failed to store the environment variable '%s'. Reason : %s. Aborting.\n", envname, strerror(errno));
1673 return 1;
1674 }
Cyril Bontéac92a062014-12-27 22:28:38 +01001675 else if (ret > len) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001676 Alert("Environment variable '%s' was truncated. Aborting.\n", envname);
1677 return 1;
1678 }
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001679 return 0;
1680}
Simon Horman98637e52014-06-20 12:30:16 +09001681
1682static int prepare_external_check(struct check *check)
1683{
1684 struct server *s = check->server;
1685 struct proxy *px = s->proxy;
1686 struct listener *listener = NULL, *l;
1687 int i;
Simon Horman98637e52014-06-20 12:30:16 +09001688 const char *path = px->check_path ? px->check_path : DEF_CHECK_PATH;
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001689 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001690
1691 list_for_each_entry(l, &px->conf.listeners, by_fe)
1692 /* Use the first INET, INET6 or UNIX listener */
1693 if (l->addr.ss_family == AF_INET ||
1694 l->addr.ss_family == AF_INET6 ||
1695 l->addr.ss_family == AF_UNIX) {
1696 listener = l;
1697 break;
1698 }
1699
Simon Horman98637e52014-06-20 12:30:16 +09001700 check->curpid = NULL;
Cyril Bontéac92a062014-12-27 22:28:38 +01001701 check->envp = calloc((EXTCHK_SIZE + 1), sizeof(char *));
1702 if (!check->envp) {
1703 Alert("Failed to allocate memory for environment variables. Aborting\n");
1704 goto err;
1705 }
Simon Horman98637e52014-06-20 12:30:16 +09001706
Cyril Bontéac92a062014-12-27 22:28:38 +01001707 check->argv = calloc(6, sizeof(char *));
1708 if (!check->argv) {
1709 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001710 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001711 }
Simon Horman98637e52014-06-20 12:30:16 +09001712
1713 check->argv[0] = px->check_command;
1714
Cyril Bonté777be862014-12-02 21:21:35 +01001715 if (!listener) {
1716 check->argv[1] = strdup("NOT_USED");
1717 check->argv[2] = strdup("NOT_USED");
1718 }
1719 else if (listener->addr.ss_family == AF_INET ||
Simon Horman98637e52014-06-20 12:30:16 +09001720 listener->addr.ss_family == AF_INET6) {
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001721 addr_to_str(&listener->addr, buf, sizeof(buf));
1722 check->argv[1] = strdup(buf);
1723 port_to_str(&listener->addr, buf, sizeof(buf));
1724 check->argv[2] = strdup(buf);
Cyril Bonté777be862014-12-02 21:21:35 +01001725 }
1726 else if (listener->addr.ss_family == AF_UNIX) {
Simon Horman98637e52014-06-20 12:30:16 +09001727 const struct sockaddr_un *un;
1728
1729 un = (struct sockaddr_un *)&listener->addr;
1730 check->argv[1] = strdup(un->sun_path);
1731 check->argv[2] = strdup("NOT_USED");
Cyril Bonté777be862014-12-02 21:21:35 +01001732 }
1733 else {
Cyril Bontéac92a062014-12-27 22:28:38 +01001734 Alert("Starting [%s:%s] check: unsupported address family.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001735 goto err;
1736 }
1737
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001738 addr_to_str(&s->addr, buf, sizeof(buf));
1739 check->argv[3] = strdup(buf);
1740 port_to_str(&s->addr, buf, sizeof(buf));
1741 check->argv[4] = strdup(buf);
Simon Horman98637e52014-06-20 12:30:16 +09001742
Cyril Bontéac92a062014-12-27 22:28:38 +01001743 for (i = 0; i < 5; i++) {
1744 if (!check->argv[i]) {
1745 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
Simon Horman98637e52014-06-20 12:30:16 +09001746 goto err;
Cyril Bontéac92a062014-12-27 22:28:38 +01001747 }
1748 }
Simon Horman98637e52014-06-20 12:30:16 +09001749
Cyril Bontéac92a062014-12-27 22:28:38 +01001750 EXTCHK_SETENV(check, EXTCHK_PATH, path, err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001751 /* Add proxy environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001752 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err);
1753 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err);
1754 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ADDR, check->argv[1], err);
1755 EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_PORT, check->argv[2], err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001756 /* Add server environment variables */
Cyril Bontéac92a062014-12-27 22:28:38 +01001757 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_NAME, s->id, err);
1758 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ID, ultoa_r(s->puid, buf, sizeof(buf)), err);
1759 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_ADDR, check->argv[3], err);
1760 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_PORT, check->argv[4], err);
1761 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_MAXCONN, ultoa_r(s->maxconn, buf, sizeof(buf)), err);
1762 EXTCHK_SETENV(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)), err);
1763
1764 /* Ensure that we don't leave any hole in check->envp */
1765 for (i = 0; i < EXTCHK_SIZE; i++)
1766 if (!check->envp[i])
1767 EXTCHK_SETENV(check, i, "", err);
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001768
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001769 return 1;
Simon Horman98637e52014-06-20 12:30:16 +09001770err:
1771 if (check->envp) {
Cyril Bontéac92a062014-12-27 22:28:38 +01001772 for (i = 0; i < EXTCHK_SIZE; i++)
Cyril Bonté9ede66b2014-12-02 21:21:36 +01001773 free(check->envp[i]);
Simon Horman98637e52014-06-20 12:30:16 +09001774 free(check->envp);
1775 check->envp = NULL;
1776 }
1777
1778 if (check->argv) {
1779 for (i = 1; i < 5; i++)
1780 free(check->argv[i]);
1781 free(check->argv);
1782 check->argv = NULL;
1783 }
Cyril Bonté99c5bf52014-08-07 01:55:38 +02001784 return 0;
Simon Horman98637e52014-06-20 12:30:16 +09001785}
1786
Simon Hormanb00d17a2014-06-13 16:18:16 +09001787/*
Simon Horman98637e52014-06-20 12:30:16 +09001788 * establish a server health-check that makes use of a process.
1789 *
1790 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001791 * - SF_ERR_NONE if everything's OK
1792 * - SF_ERR_SRVTO if there are no more servers
1793 * - SF_ERR_SRVCL if the connection was refused by the server
1794 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1795 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1796 * - SF_ERR_INTERNAL for any other purely internal errors
1797 * Additionnally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Simon Horman98637e52014-06-20 12:30:16 +09001798 *
1799 * Blocks and then unblocks SIGCHLD
1800 */
1801static int connect_proc_chk(struct task *t)
1802{
Cyril Bontéac92a062014-12-27 22:28:38 +01001803 char buf[256];
Simon Horman98637e52014-06-20 12:30:16 +09001804 struct check *check = t->context;
1805 struct server *s = check->server;
1806 struct proxy *px = s->proxy;
1807 int status;
1808 pid_t pid;
1809
Willy Tarreaue7dff022015-04-03 01:14:29 +02001810 status = SF_ERR_RESOURCE;
Simon Horman98637e52014-06-20 12:30:16 +09001811
1812 block_sigchld();
1813
1814 pid = fork();
1815 if (pid < 0) {
1816 Alert("Failed to fork process for external health check: %s. Aborting.\n",
1817 strerror(errno));
1818 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1819 goto out;
1820 }
1821 if (pid == 0) {
1822 /* Child */
1823 extern char **environ;
1824 environ = check->envp;
Cyril Bontéac92a062014-12-27 22:28:38 +01001825 extchk_setenv(check, EXTCHK_HAPROXY_SERVER_CURCONN, ultoa_r(s->cur_sess, buf, sizeof(buf)));
Simon Horman98637e52014-06-20 12:30:16 +09001826 execvp(px->check_command, check->argv);
1827 Alert("Failed to exec process for external health check: %s. Aborting.\n",
1828 strerror(errno));
1829 exit(-1);
1830 }
1831
1832 /* Parent */
1833 if (check->result == CHK_RES_UNKNOWN) {
1834 if (pid_list_add(pid, t) != NULL) {
1835 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1836
1837 if (px->timeout.check && px->timeout.connect) {
1838 int t_con = tick_add(now_ms, px->timeout.connect);
1839 t->expire = tick_first(t->expire, t_con);
1840 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02001841 status = SF_ERR_NONE;
Simon Horman98637e52014-06-20 12:30:16 +09001842 goto out;
1843 }
1844 else {
1845 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1846 }
1847 kill(pid, SIGTERM); /* process creation error */
1848 }
1849 else
1850 set_server_check_status(check, HCHK_STATUS_SOCKERR, strerror(errno));
1851
1852out:
1853 unblock_sigchld();
1854 return status;
1855}
1856
1857/*
Simon Horman98637e52014-06-20 12:30:16 +09001858 * manages a server health-check that uses a process. Returns
Willy Tarreaubaaee002006-06-26 02:48:02 +02001859 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1860 */
Simon Horman98637e52014-06-20 12:30:16 +09001861static struct task *process_chk_proc(struct task *t)
1862{
1863 struct check *check = t->context;
1864 struct server *s = check->server;
1865 struct connection *conn = check->conn;
1866 int rv;
1867 int ret;
1868 int expired = tick_is_expired(t->expire, now_ms);
1869
1870 if (!(check->state & CHK_ST_INPROGRESS)) {
1871 /* no check currently running */
1872 if (!expired) /* woke up too early */
1873 return t;
1874
1875 /* we don't send any health-checks when the proxy is
1876 * stopped, the server should not be checked or the check
1877 * is disabled.
1878 */
1879 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
1880 s->proxy->state == PR_STSTOPPED)
1881 goto reschedule;
1882
1883 /* we'll initiate a new check */
1884 set_server_check_status(check, HCHK_STATUS_START, NULL);
1885
1886 check->state |= CHK_ST_INPROGRESS;
1887
Simon Hormandbf70192015-01-30 11:22:53 +09001888 ret = connect_proc_chk(t);
Simon Horman98637e52014-06-20 12:30:16 +09001889 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001890 case SF_ERR_UP:
Simon Horman98637e52014-06-20 12:30:16 +09001891 return t;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001892 case SF_ERR_NONE:
Simon Horman98637e52014-06-20 12:30:16 +09001893 /* we allow up to min(inter, timeout.connect) for a connection
1894 * to establish but only when timeout.check is set
1895 * as it may be to short for a full check otherwise
1896 */
1897 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
1898
1899 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
1900 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
1901 t->expire = tick_first(t->expire, t_con);
1902 }
1903
1904 goto reschedule;
1905
Willy Tarreaue7dff022015-04-03 01:14:29 +02001906 case SF_ERR_SRVTO: /* ETIMEDOUT */
1907 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Simon Horman98637e52014-06-20 12:30:16 +09001908 conn->flags |= CO_FL_ERROR;
1909 chk_report_conn_err(conn, errno, 0);
1910 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001911 case SF_ERR_PRXCOND:
1912 case SF_ERR_RESOURCE:
1913 case SF_ERR_INTERNAL:
Simon Horman98637e52014-06-20 12:30:16 +09001914 conn->flags |= CO_FL_ERROR;
1915 chk_report_conn_err(conn, 0, 0);
1916 break;
1917 }
1918
1919 /* here, we have seen a synchronous error, no fd was allocated */
1920
1921 check->state &= ~CHK_ST_INPROGRESS;
1922 check_notify_failure(check);
1923
1924 /* we allow up to min(inter, timeout.connect) for a connection
1925 * to establish but only when timeout.check is set
1926 * as it may be to short for a full check otherwise
1927 */
1928 while (tick_is_expired(t->expire, now_ms)) {
1929 int t_con;
1930
1931 t_con = tick_add(t->expire, s->proxy->timeout.connect);
1932 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1933
1934 if (s->proxy->timeout.check)
1935 t->expire = tick_first(t->expire, t_con);
1936 }
1937 }
1938 else {
1939 /* there was a test running.
1940 * First, let's check whether there was an uncaught error,
1941 * which can happen on connect timeout or error.
1942 */
1943 if (check->result == CHK_RES_UNKNOWN) {
1944 /* good connection is enough for pure TCP check */
1945 struct pid_list *elem = check->curpid;
1946 int status = HCHK_STATUS_UNKNOWN;
1947
1948 if (elem->exited) {
1949 status = elem->status; /* Save in case the process exits between use below */
1950 if (!WIFEXITED(status))
1951 check->code = -1;
1952 else
1953 check->code = WEXITSTATUS(status);
1954 if (!WIFEXITED(status) || WEXITSTATUS(status))
1955 status = HCHK_STATUS_PROCERR;
1956 else
1957 status = HCHK_STATUS_PROCOK;
1958 } else if (expired) {
1959 status = HCHK_STATUS_PROCTOUT;
Willy Tarreaudc3d1902014-07-08 00:56:27 +02001960 Warning("kill %d\n", (int)elem->pid);
Simon Horman98637e52014-06-20 12:30:16 +09001961 kill(elem->pid, SIGTERM);
1962 }
1963 set_server_check_status(check, status, NULL);
1964 }
1965
1966 if (check->result == CHK_RES_FAILED) {
1967 /* a failure or timeout detected */
1968 check_notify_failure(check);
1969 }
1970 else if (check->result == CHK_RES_CONDPASS) {
1971 /* check is OK but asks for stopping mode */
1972 check_notify_stopping(check);
1973 }
1974 else if (check->result == CHK_RES_PASSED) {
1975 /* a success was detected */
1976 check_notify_success(check);
1977 }
1978 check->state &= ~CHK_ST_INPROGRESS;
1979
1980 pid_list_del(check->curpid);
1981
1982 rv = 0;
1983 if (global.spread_checks > 0) {
1984 rv = srv_getinter(check) * global.spread_checks / 100;
1985 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
1986 }
1987 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
1988 }
1989
1990 reschedule:
1991 while (tick_is_expired(t->expire, now_ms))
1992 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
1993 return t;
1994}
1995
1996/*
1997 * manages a server health-check that uses a connection. Returns
1998 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
1999 */
2000static struct task *process_chk_conn(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002001{
Simon Horman4a741432013-02-23 15:35:38 +09002002 struct check *check = t->context;
2003 struct server *s = check->server;
2004 struct connection *conn = check->conn;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002005 int rv;
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002006 int ret;
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002007 int expired = tick_is_expired(t->expire, now_ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002008
Willy Tarreau2c115e52013-12-11 19:41:16 +01002009 if (!(check->state & CHK_ST_INPROGRESS)) {
Willy Tarreau5a78f362012-11-23 12:47:05 +01002010 /* no check currently running */
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002011 if (!expired) /* woke up too early */
Willy Tarreau26c25062009-03-08 09:38:41 +01002012 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002013
Simon Horman671b6f02013-11-25 10:46:39 +09002014 /* we don't send any health-checks when the proxy is
2015 * stopped, the server should not be checked or the check
2016 * is disabled.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002017 */
Willy Tarreau0d924cc2013-12-11 21:26:24 +01002018 if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
Willy Tarreau33a08db2013-12-11 21:03:31 +01002019 s->proxy->state == PR_STSTOPPED)
Willy Tarreau5a78f362012-11-23 12:47:05 +01002020 goto reschedule;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002021
2022 /* we'll initiate a new check */
Simon Horman4a741432013-02-23 15:35:38 +09002023 set_server_check_status(check, HCHK_STATUS_START, NULL);
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002024
Willy Tarreau2c115e52013-12-11 19:41:16 +01002025 check->state |= CHK_ST_INPROGRESS;
Simon Horman4a741432013-02-23 15:35:38 +09002026 check->bi->p = check->bi->data;
2027 check->bi->i = 0;
2028 check->bo->p = check->bo->data;
2029 check->bo->o = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002030
Simon Hormandbf70192015-01-30 11:22:53 +09002031 ret = connect_conn_chk(t);
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002032 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002033 case SF_ERR_UP:
Simon Hormanb00d17a2014-06-13 16:18:16 +09002034 return t;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002035 case SF_ERR_NONE:
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002036 /* we allow up to min(inter, timeout.connect) for a connection
2037 * to establish but only when timeout.check is set
2038 * as it may be to short for a full check otherwise
2039 */
Simon Horman4a741432013-02-23 15:35:38 +09002040 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002041
Willy Tarreaufb56aab2012-09-28 14:40:02 +02002042 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2043 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2044 t->expire = tick_first(t->expire, t_con);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002045 }
Willy Tarreau06559ac2013-12-05 01:53:08 +01002046
2047 if (check->type)
2048 conn_data_want_recv(conn); /* prepare for reading a possible reply */
2049
Willy Tarreau5a78f362012-11-23 12:47:05 +01002050 goto reschedule;
2051
Willy Tarreaue7dff022015-04-03 01:14:29 +02002052 case SF_ERR_SRVTO: /* ETIMEDOUT */
2053 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Willy Tarreau4bd07de2014-01-24 16:10:57 +01002054 conn->flags |= CO_FL_ERROR;
2055 chk_report_conn_err(conn, errno, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002056 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002057 case SF_ERR_PRXCOND:
2058 case SF_ERR_RESOURCE:
2059 case SF_ERR_INTERNAL:
Willy Tarreau4bd07de2014-01-24 16:10:57 +01002060 conn->flags |= CO_FL_ERROR;
2061 chk_report_conn_err(conn, 0, 0);
Willy Tarreau5a78f362012-11-23 12:47:05 +01002062 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002063 }
2064
Willy Tarreau5a78f362012-11-23 12:47:05 +01002065 /* here, we have seen a synchronous error, no fd was allocated */
Willy Tarreau6b0a8502012-11-23 08:51:32 +01002066
Willy Tarreau2c115e52013-12-11 19:41:16 +01002067 check->state &= ~CHK_ST_INPROGRESS;
Willy Tarreau4eec5472014-05-20 22:32:27 +02002068 check_notify_failure(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002069
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002070 /* we allow up to min(inter, timeout.connect) for a connection
2071 * to establish but only when timeout.check is set
2072 * as it may be to short for a full check otherwise
2073 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002074 while (tick_is_expired(t->expire, now_ms)) {
2075 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002076
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002077 t_con = tick_add(t->expire, s->proxy->timeout.connect);
Simon Horman4a741432013-02-23 15:35:38 +09002078 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002079
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002080 if (s->proxy->timeout.check)
2081 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01002082 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002083 }
2084 else {
Willy Tarreauf1503172012-09-28 19:39:36 +02002085 /* there was a test running.
2086 * First, let's check whether there was an uncaught error,
2087 * which can happen on connect timeout or error.
2088 */
Simon Hormanccaabcd2014-06-20 12:29:47 +09002089 if (check->result == CHK_RES_UNKNOWN) {
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002090 /* good connection is enough for pure TCP check */
2091 if ((conn->flags & CO_FL_CONNECTED) && !check->type) {
Simon Horman4a741432013-02-23 15:35:38 +09002092 if (check->use_ssl)
2093 set_server_check_status(check, HCHK_STATUS_L6OK, NULL);
Willy Tarreauf1503172012-09-28 19:39:36 +02002094 else
Simon Horman4a741432013-02-23 15:35:38 +09002095 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Willy Tarreauacbdc7a2012-11-23 14:02:10 +01002096 }
Willy Tarreau25e2ab52013-12-04 11:17:05 +01002097 else if ((conn->flags & CO_FL_ERROR) || expired) {
2098 chk_report_conn_err(conn, 0, expired);
Willy Tarreauf1503172012-09-28 19:39:36 +02002099 }
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002100 else
2101 goto out_wait; /* timeout not reached, wait again */
Willy Tarreauf1503172012-09-28 19:39:36 +02002102 }
2103
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002104 /* check complete or aborted */
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002105 if (conn->xprt) {
2106 /* The check was aborted and the connection was not yet closed.
2107 * This can happen upon timeout, or when an external event such
2108 * as a failed response coupled with "observe layer7" caused the
2109 * server state to be suddenly changed.
2110 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01002111 conn_sock_drain(conn);
Willy Tarreauf79c8172013-10-21 16:30:56 +02002112 conn_force_close(conn);
Willy Tarreau5ba04f62013-02-12 15:23:12 +01002113 }
2114
Willy Tarreauaf549582014-05-16 17:37:50 +02002115 if (check->result == CHK_RES_FAILED) {
2116 /* a failure or timeout detected */
Willy Tarreau4eec5472014-05-20 22:32:27 +02002117 check_notify_failure(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002118 }
Willy Tarreaudb58b792014-05-21 13:57:23 +02002119 else if (check->result == CHK_RES_CONDPASS) {
2120 /* check is OK but asks for stopping mode */
2121 check_notify_stopping(check);
Willy Tarreauaf549582014-05-16 17:37:50 +02002122 }
Willy Tarreau3e048382014-05-21 10:30:54 +02002123 else if (check->result == CHK_RES_PASSED) {
2124 /* a success was detected */
2125 check_notify_success(check);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002126 }
Willy Tarreau2c115e52013-12-11 19:41:16 +01002127 check->state &= ~CHK_ST_INPROGRESS;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002128
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002129 rv = 0;
2130 if (global.spread_checks > 0) {
Simon Horman4a741432013-02-23 15:35:38 +09002131 rv = srv_getinter(check) * global.spread_checks / 100;
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002132 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002133 }
Simon Horman4a741432013-02-23 15:35:38 +09002134 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(check) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002135 }
Willy Tarreau5a78f362012-11-23 12:47:05 +01002136
2137 reschedule:
2138 while (tick_is_expired(t->expire, now_ms))
Simon Horman4a741432013-02-23 15:35:38 +09002139 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
Willy Tarreau74fa7fb2012-11-23 14:43:49 +01002140 out_wait:
Willy Tarreau26c25062009-03-08 09:38:41 +01002141 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002142}
2143
Simon Horman98637e52014-06-20 12:30:16 +09002144/*
2145 * manages a server health-check. Returns
2146 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
2147 */
2148static struct task *process_chk(struct task *t)
2149{
2150 struct check *check = t->context;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002151 struct server *s = check->server;
2152 struct dns_resolution *resolution = s->resolution;
2153
2154 /* trigger name resolution */
2155 if ((s->check.state & CHK_ST_ENABLED) && (resolution)) {
2156 /* check if a no resolution is running for this server */
2157 if (resolution->step == RSLV_STEP_NONE) {
2158 /*
2159 * if there has not been any name resolution for a longer period than
2160 * hold.valid, let's trigger a new one.
2161 */
Baptiste Assmannf0d93702015-09-03 10:59:39 +02002162 if (!resolution->last_resolution || tick_is_expired(tick_add(resolution->last_resolution, resolution->resolvers->hold.valid), now_ms)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002163 trigger_resolution(s);
2164 }
2165 }
2166 }
Simon Horman98637e52014-06-20 12:30:16 +09002167
2168 if (check->type == PR_O2_EXT_CHK)
2169 return process_chk_proc(t);
2170 return process_chk_conn(t);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002171
2172}
2173
2174/*
2175 * Initiates a new name resolution:
2176 * - generates a query id
2177 * - configure the resolution structure
2178 * - startup the resolvers task if required
2179 *
2180 * returns:
2181 * - 0 in case of error or if resolution already running
2182 * - 1 if everything started properly
2183 */
2184int trigger_resolution(struct server *s)
2185{
2186 struct dns_resolution *resolution;
2187 struct dns_resolvers *resolvers;
2188 int query_id;
2189 int i;
2190
2191 resolution = s->resolution;
2192 resolvers = resolution->resolvers;
2193
2194 /*
2195 * check if a resolution has already been started for this server
2196 * return directly to avoid resolution pill up
2197 */
2198 if (resolution->step != RSLV_STEP_NONE)
2199 return 0;
2200
2201 /* generates a query id */
2202 i = 0;
2203 do {
2204 query_id = dns_rnd16();
2205 /* we do try only 100 times to find a free query id */
2206 if (i++ > 100) {
2207 chunk_printf(&trash, "could not generate a query id for %s/%s, in resolvers %s",
2208 s->proxy->id, s->id, resolvers->id);
2209
2210 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
2211 return 0;
2212 }
2213 } while (eb32_lookup(&resolvers->query_ids, query_id));
2214
2215 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
2216
2217 /* now update resolution parameters */
2218 resolution->query_id = query_id;
2219 resolution->qid.key = query_id;
2220 resolution->step = RSLV_STEP_RUNNING;
Thierry Fournierada34842016-02-17 21:25:09 +01002221 resolution->opts = &s->dns_opts;
2222 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00002223 resolution->query_type = DNS_RTYPE_A;
2224 } else {
2225 resolution->query_type = DNS_RTYPE_AAAA;
2226 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002227 resolution->try = resolvers->resolve_retries;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002228 resolution->try_cname = 0;
2229 resolution->nb_responses = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002230 eb32_insert(&resolvers->query_ids, &resolution->qid);
2231
2232 dns_send_query(resolution);
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002233 resolution->try -= 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002234
2235 /* update wakeup date if this resolution is the only one in the FIFO list */
2236 if (dns_check_resolution_queue(resolvers) == 1) {
2237 /* update task timeout */
2238 dns_update_resolvers_timeout(resolvers);
2239 task_queue(resolvers->t);
2240 }
2241
2242 return 1;
Simon Horman98637e52014-06-20 12:30:16 +09002243}
2244
Simon Horman5c942422013-11-25 10:46:32 +09002245static int start_check_task(struct check *check, int mininter,
2246 int nbcheck, int srvpos)
2247{
2248 struct task *t;
2249 /* task for the check */
2250 if ((t = task_new()) == NULL) {
2251 Alert("Starting [%s:%s] check: out of memory.\n",
2252 check->server->proxy->id, check->server->id);
2253 return 0;
2254 }
2255
2256 check->task = t;
2257 t->process = process_chk;
2258 t->context = check;
2259
Willy Tarreau1746eec2014-04-25 10:46:47 +02002260 if (mininter < srv_getinter(check))
2261 mininter = srv_getinter(check);
2262
2263 if (global.max_spread_checks && mininter > global.max_spread_checks)
2264 mininter = global.max_spread_checks;
2265
Simon Horman5c942422013-11-25 10:46:32 +09002266 /* check this every ms */
Willy Tarreau1746eec2014-04-25 10:46:47 +02002267 t->expire = tick_add(now_ms, MS_TO_TICKS(mininter * srvpos / nbcheck));
Simon Horman5c942422013-11-25 10:46:32 +09002268 check->start = now;
2269 task_queue(t);
2270
2271 return 1;
2272}
2273
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002274/*
2275 * Start health-check.
2276 * Returns 0 if OK, -1 if error, and prints the error in this case.
2277 */
2278int start_checks() {
2279
2280 struct proxy *px;
2281 struct server *s;
2282 struct task *t;
Simon Horman4a741432013-02-23 15:35:38 +09002283 int nbcheck=0, mininter=0, srvpos=0;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002284
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002285 /* 1- count the checkers to run simultaneously.
2286 * We also determine the minimum interval among all of those which
2287 * have an interval larger than SRV_CHK_INTER_THRES. This interval
2288 * will be used to spread their start-up date. Those which have
Jamie Gloudon801a0a32012-08-25 00:18:33 -04002289 * a shorter interval will start independently and will not dictate
Willy Tarreau2c43a1e2007-10-14 23:05:39 +02002290 * too short an interval for all others.
2291 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002292 for (px = proxy; px; px = px->next) {
2293 for (s = px->srv; s; s = s->next) {
Willy Tarreaue7b73482013-11-21 11:50:50 +01002294 if (s->slowstart) {
2295 if ((t = task_new()) == NULL) {
2296 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
2297 return -1;
2298 }
2299 /* We need a warmup task that will be called when the server
2300 * state switches from down to up.
2301 */
2302 s->warmup = t;
2303 t->process = server_warmup;
2304 t->context = s;
2305 t->expire = TICK_ETERNITY;
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002306 /* server can be in this state only because of */
2307 if (s->state == SRV_ST_STARTING)
2308 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, (now.tv_sec - s->last_change)) / 20)));
Willy Tarreaue7b73482013-11-21 11:50:50 +01002309 }
2310
Willy Tarreaud8514a22013-12-11 21:10:14 +01002311 if (s->check.state & CHK_ST_CONFIGURED) {
2312 nbcheck++;
2313 if ((srv_getinter(&s->check) >= SRV_CHK_INTER_THRES) &&
2314 (!mininter || mininter > srv_getinter(&s->check)))
2315 mininter = srv_getinter(&s->check);
2316 }
Willy Tarreau15f39102013-12-11 20:41:18 +01002317
Willy Tarreaud8514a22013-12-11 21:10:14 +01002318 if (s->agent.state & CHK_ST_CONFIGURED) {
2319 nbcheck++;
2320 if ((srv_getinter(&s->agent) >= SRV_CHK_INTER_THRES) &&
2321 (!mininter || mininter > srv_getinter(&s->agent)))
2322 mininter = srv_getinter(&s->agent);
2323 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002324 }
2325 }
2326
Simon Horman4a741432013-02-23 15:35:38 +09002327 if (!nbcheck)
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002328 return 0;
2329
2330 srand((unsigned)time(NULL));
2331
2332 /*
2333 * 2- start them as far as possible from each others. For this, we will
2334 * start them after their interval set to the min interval divided by
2335 * the number of servers, weighted by the server's position in the list.
2336 */
2337 for (px = proxy; px; px = px->next) {
Simon Horman98637e52014-06-20 12:30:16 +09002338 if ((px->options2 & PR_O2_CHK_ANY) == PR_O2_EXT_CHK) {
2339 if (init_pid_list()) {
2340 Alert("Starting [%s] check: out of memory.\n", px->id);
2341 return -1;
2342 }
2343 }
2344
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002345 for (s = px->srv; s; s = s->next) {
Simon Hormand60d6912013-11-25 10:46:36 +09002346 /* A task for the main check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002347 if (s->check.state & CHK_ST_CONFIGURED) {
Cyril Bonté99c5bf52014-08-07 01:55:38 +02002348 if (s->check.type == PR_O2_EXT_CHK) {
2349 if (!prepare_external_check(&s->check))
2350 return -1;
2351 }
Simon Hormand60d6912013-11-25 10:46:36 +09002352 if (!start_check_task(&s->check, mininter, nbcheck, srvpos))
2353 return -1;
2354 srvpos++;
2355 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002356
Simon Hormand60d6912013-11-25 10:46:36 +09002357 /* A task for a auxiliary agent check */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002358 if (s->agent.state & CHK_ST_CONFIGURED) {
Simon Hormand60d6912013-11-25 10:46:36 +09002359 if (!start_check_task(&s->agent, mininter, nbcheck, srvpos)) {
2360 return -1;
2361 }
2362 srvpos++;
2363 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +02002364 }
2365 }
2366 return 0;
2367}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002368
2369/*
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002370 * Perform content verification check on data in s->check.buffer buffer.
Willy Tarreaubd741542010-03-16 18:46:54 +01002371 * The buffer MUST be terminated by a null byte before calling this function.
2372 * Sets server status appropriately. The caller is responsible for ensuring
2373 * that the buffer contains at least 13 characters. If <done> is zero, we may
2374 * return 0 to indicate that data is required to decide of a match.
2375 */
2376static int httpchk_expect(struct server *s, int done)
2377{
2378 static char status_msg[] = "HTTP status check returned code <000>";
2379 char status_code[] = "000";
2380 char *contentptr;
2381 int crlf;
2382 int ret;
2383
2384 switch (s->proxy->options2 & PR_O2_EXP_TYPE) {
2385 case PR_O2_EXP_STS:
2386 case PR_O2_EXP_RSTS:
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002387 memcpy(status_code, s->check.bi->data + 9, 3);
2388 memcpy(status_msg + strlen(status_msg) - 4, s->check.bi->data + 9, 3);
Willy Tarreaubd741542010-03-16 18:46:54 +01002389
2390 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STS)
2391 ret = strncmp(s->proxy->expect_str, status_code, 3) == 0;
2392 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002393 ret = regex_exec(s->proxy->expect_regex, status_code);
Willy Tarreaubd741542010-03-16 18:46:54 +01002394
2395 /* we necessarily have the response, so there are no partial failures */
2396 if (s->proxy->options2 & PR_O2_EXP_INV)
2397 ret = !ret;
2398
Simon Horman4a741432013-02-23 15:35:38 +09002399 set_server_check_status(&s->check, ret ? HCHK_STATUS_L7OKD : HCHK_STATUS_L7STS, status_msg);
Willy Tarreaubd741542010-03-16 18:46:54 +01002400 break;
2401
2402 case PR_O2_EXP_STR:
2403 case PR_O2_EXP_RSTR:
2404 /* very simple response parser: ignore CR and only count consecutive LFs,
2405 * stop with contentptr pointing to first char after the double CRLF or
2406 * to '\0' if crlf < 2.
2407 */
2408 crlf = 0;
Willy Tarreau1ae1b7b2012-09-28 15:28:30 +02002409 for (contentptr = s->check.bi->data; *contentptr; contentptr++) {
Willy Tarreaubd741542010-03-16 18:46:54 +01002410 if (crlf >= 2)
2411 break;
2412 if (*contentptr == '\r')
2413 continue;
2414 else if (*contentptr == '\n')
2415 crlf++;
2416 else
2417 crlf = 0;
2418 }
2419
2420 /* Check that response contains a body... */
2421 if (crlf < 2) {
2422 if (!done)
2423 return 0;
2424
Simon Horman4a741432013-02-23 15:35:38 +09002425 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002426 "HTTP content check could not find a response body");
2427 return 1;
2428 }
2429
2430 /* Check that response body is not empty... */
2431 if (*contentptr == '\0') {
Willy Tarreaua164fb52011-04-13 09:32:41 +02002432 if (!done)
2433 return 0;
2434
Simon Horman4a741432013-02-23 15:35:38 +09002435 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002436 "HTTP content check found empty response body");
2437 return 1;
2438 }
2439
2440 /* Check the response content against the supplied string
2441 * or regex... */
2442 if ((s->proxy->options2 & PR_O2_EXP_TYPE) == PR_O2_EXP_STR)
2443 ret = strstr(contentptr, s->proxy->expect_str) != NULL;
2444 else
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02002445 ret = regex_exec(s->proxy->expect_regex, contentptr);
Willy Tarreaubd741542010-03-16 18:46:54 +01002446
2447 /* if we don't match, we may need to wait more */
2448 if (!ret && !done)
2449 return 0;
2450
2451 if (ret) {
2452 /* content matched */
2453 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002454 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002455 "HTTP check matched unwanted content");
2456 else
Simon Horman4a741432013-02-23 15:35:38 +09002457 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002458 "HTTP content check matched");
2459 }
2460 else {
2461 if (s->proxy->options2 & PR_O2_EXP_INV)
Simon Horman4a741432013-02-23 15:35:38 +09002462 set_server_check_status(&s->check, HCHK_STATUS_L7OKD,
Willy Tarreaubd741542010-03-16 18:46:54 +01002463 "HTTP check did not match unwanted content");
2464 else
Simon Horman4a741432013-02-23 15:35:38 +09002465 set_server_check_status(&s->check, HCHK_STATUS_L7RSP,
Willy Tarreaubd741542010-03-16 18:46:54 +01002466 "HTTP content check did not match");
2467 }
2468 break;
2469 }
2470 return 1;
2471}
2472
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002473/*
2474 * return the id of a step in a send/expect session
2475 */
Simon Hormane16c1b32015-01-30 11:22:57 +09002476static int tcpcheck_get_step_id(struct check *check)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002477{
2478 struct tcpcheck_rule *cur = NULL, *next = NULL;
2479 int i = 0;
2480
Willy Tarreau213c6782014-10-02 14:51:02 +02002481 /* not even started anything yet => step 0 = initial connect */
Baptiste Assmannf95bc8e2015-04-25 16:03:06 +02002482 if (!check->current_step)
Willy Tarreau213c6782014-10-02 14:51:02 +02002483 return 0;
2484
Simon Hormane16c1b32015-01-30 11:22:57 +09002485 cur = check->last_started_step;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002486
2487 /* no step => first step */
2488 if (cur == NULL)
2489 return 1;
2490
2491 /* increment i until current step */
Simon Hormane16c1b32015-01-30 11:22:57 +09002492 list_for_each_entry(next, check->tcpcheck_rules, list) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002493 if (next->list.p == &cur->list)
2494 break;
2495 ++i;
2496 }
2497
2498 return i;
2499}
2500
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002501/*
2502 * return the latest known comment before (including) the given stepid
2503 * returns NULL if no comment found
2504 */
2505static char * tcpcheck_get_step_comment(struct check *check, int stepid)
2506{
2507 struct tcpcheck_rule *cur = NULL;
2508 char *ret = NULL;
2509 int i = 0;
2510
2511 /* not even started anything yet, return latest comment found before any action */
2512 if (!check->current_step) {
2513 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2514 if (cur->action == TCPCHK_ACT_COMMENT)
2515 ret = cur->comment;
2516 else
2517 goto return_comment;
2518 }
2519 }
2520
2521 i = 1;
2522 list_for_each_entry(cur, check->tcpcheck_rules, list) {
2523 if (cur->comment)
2524 ret = cur->comment;
2525
2526 if (i >= stepid)
2527 goto return_comment;
2528
2529 ++i;
2530 }
2531
2532 return_comment:
2533 return ret;
2534}
2535
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002536static void tcpcheck_main(struct connection *conn)
2537{
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002538 char *contentptr, *comment;
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002539 struct tcpcheck_rule *next;
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002540 int done = 0, ret = 0, step = 0;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002541 struct check *check = conn->owner;
2542 struct server *s = check->server;
2543 struct task *t = check->task;
Simon Hormane16c1b32015-01-30 11:22:57 +09002544 struct list *head = check->tcpcheck_rules;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002545
Willy Tarreauef953952014-10-02 14:30:14 +02002546 /* here, we know that the check is complete or that it failed */
2547 if (check->result != CHK_RES_UNKNOWN)
2548 goto out_end_tcpcheck;
2549
2550 /* We have 4 possibilities here :
2551 * 1. we've not yet attempted step 1, and step 1 is a connect, so no
2552 * connection attempt was made yet ;
2553 * 2. we've not yet attempted step 1, and step 1 is a not connect or
2554 * does not exist (no rule), so a connection attempt was made
2555 * before coming here.
2556 * 3. we're coming back after having started with step 1, so we may
2557 * be waiting for a connection attempt to complete.
2558 * 4. the connection + handshake are complete
2559 *
2560 * #2 and #3 are quite similar, we want both the connection and the
2561 * handshake to complete before going any further. Thus we must always
2562 * wait for a connection to complete unless we're before and existing
2563 * step 1.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002564 */
Willy Tarreau449f9522015-05-13 15:39:48 +02002565
2566 /* find first rule and skip comments */
2567 next = LIST_NEXT(head, struct tcpcheck_rule *, list);
2568 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
2569 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
2570
Willy Tarreauef953952014-10-02 14:30:14 +02002571 if ((!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE)) &&
Willy Tarreau449f9522015-05-13 15:39:48 +02002572 (check->current_step || &next->list == head)) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002573 /* we allow up to min(inter, timeout.connect) for a connection
2574 * to establish but only when timeout.check is set
2575 * as it may be to short for a full check otherwise
2576 */
2577 while (tick_is_expired(t->expire, now_ms)) {
2578 int t_con;
2579
2580 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2581 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2582
2583 if (s->proxy->timeout.check)
2584 t->expire = tick_first(t->expire, t_con);
2585 }
2586 return;
2587 }
2588
Willy Tarreauef953952014-10-02 14:30:14 +02002589 /* special case: option tcp-check with no rule, a connect is enough */
Willy Tarreau449f9522015-05-13 15:39:48 +02002590 if (&next->list == head) {
Willy Tarreauef953952014-10-02 14:30:14 +02002591 set_server_check_status(check, HCHK_STATUS_L4OK, NULL);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002592 goto out_end_tcpcheck;
Willy Tarreauef953952014-10-02 14:30:14 +02002593 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002594
Willy Tarreau213c6782014-10-02 14:51:02 +02002595 /* no step means first step initialisation */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002596 if (check->current_step == NULL) {
Willy Tarreau213c6782014-10-02 14:51:02 +02002597 check->last_started_step = NULL;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002598 check->bo->p = check->bo->data;
2599 check->bo->o = 0;
2600 check->bi->p = check->bi->data;
2601 check->bi->i = 0;
Willy Tarreau449f9522015-05-13 15:39:48 +02002602 check->current_step = next;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002603 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2604 if (s->proxy->timeout.check)
2605 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
2606 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002607
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002608 /* It's only the rules which will enable send/recv */
2609 __conn_data_stop_both(conn);
2610
Willy Tarreauabca5b62013-12-06 14:19:25 +01002611 while (1) {
Willy Tarreau263013d2015-05-13 11:59:14 +02002612 /* We have to try to flush the output buffer before reading, at
2613 * the end, or if we're about to send a string that does not fit
2614 * in the remaining space. That explains why we break out of the
2615 * loop after this control.
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002616 */
2617 if (check->bo->o &&
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002618 (&check->current_step->list == head ||
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002619 check->current_step->action != TCPCHK_ACT_SEND ||
2620 check->current_step->string_len >= buffer_total_space(check->bo))) {
2621
Willy Tarreau1049b1f2014-02-02 01:51:17 +01002622 if (conn->xprt->snd_buf(conn, check->bo, 0) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002623 if (conn->flags & CO_FL_ERROR) {
2624 chk_report_conn_err(conn, errno, 0);
2625 __conn_data_stop_both(conn);
2626 goto out_end_tcpcheck;
2627 }
Willy Tarreau263013d2015-05-13 11:59:14 +02002628 break;
Willy Tarreauabca5b62013-12-06 14:19:25 +01002629 }
Willy Tarreauabca5b62013-12-06 14:19:25 +01002630 }
2631
Willy Tarreau263013d2015-05-13 11:59:14 +02002632 if (&check->current_step->list == head)
Willy Tarreauabca5b62013-12-06 14:19:25 +01002633 break;
2634
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002635 /* have 'next' point to the next rule or NULL if we're on the
2636 * last one, connect() needs this.
2637 */
Willy Tarreau5581c272015-05-13 12:24:53 +02002638 next = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002639
2640 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002641 while (&next->list != head && next->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002642 next = LIST_NEXT(&next->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002643
2644 /* NULL if we're on the last rule */
Willy Tarreauf3d34822014-12-08 12:11:28 +01002645 if (&next->list == head)
2646 next = NULL;
2647
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002648 if (check->current_step->action == TCPCHK_ACT_CONNECT) {
2649 struct protocol *proto;
2650 struct xprt_ops *xprt;
2651
2652 /* mark the step as started */
2653 check->last_started_step = check->current_step;
2654 /* first, shut existing connection */
2655 conn_force_close(conn);
2656
2657 /* prepare new connection */
2658 /* initialization */
2659 conn_init(conn);
2660 conn_attach(conn, check, &check_conn_cb);
2661 conn->target = &s->obj_type;
2662
2663 /* no client address */
2664 clear_addr(&conn->addr.from);
2665
Simon Horman41f58762015-01-30 11:22:56 +09002666 if (is_addr(&check->addr)) {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002667 /* we'll connect to the check addr specified on the server */
Simon Horman41f58762015-01-30 11:22:56 +09002668 conn->addr.to = check->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002669 }
2670 else {
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002671 /* we'll connect to the addr on the server */
2672 conn->addr.to = s->addr;
Willy Tarreau640556c2014-05-09 23:38:15 +02002673 }
Thierry FOURNIERbb2ae642015-01-14 11:31:49 +01002674 proto = protocol_by_family(conn->addr.to.ss_family);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002675
2676 /* port */
2677 if (check->current_step->port)
2678 set_host_port(&conn->addr.to, check->current_step->port);
2679 else if (check->port)
2680 set_host_port(&conn->addr.to, check->port);
2681
2682#ifdef USE_OPENSSL
2683 if (check->current_step->conn_opts & TCPCHK_OPT_SSL) {
2684 xprt = &ssl_sock;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002685 }
2686 else {
2687 xprt = &raw_sock;
2688 }
2689#else /* USE_OPENSSL */
2690 xprt = &raw_sock;
2691#endif /* USE_OPENSSL */
2692 conn_prepare(conn, proto, xprt);
2693
Willy Tarreaue7dff022015-04-03 01:14:29 +02002694 ret = SF_ERR_INTERNAL;
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002695 if (proto->connect)
Willy Tarreauf3d34822014-12-08 12:11:28 +01002696 ret = proto->connect(conn,
2697 1 /* I/O polling is always needed */,
2698 (next && next->action == TCPCHK_ACT_EXPECT) ? 0 : 2);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002699 conn->flags |= CO_FL_WAKE_DATA;
2700 if (check->current_step->conn_opts & TCPCHK_OPT_SEND_PROXY) {
2701 conn->send_proxy_ofs = 1;
2702 conn->flags |= CO_FL_SEND_PROXY;
2703 }
2704
2705 /* It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02002706 * - SF_ERR_NONE if everything's OK
2707 * - SF_ERR_SRVTO if there are no more servers
2708 * - SF_ERR_SRVCL if the connection was refused by the server
2709 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
2710 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
2711 * - SF_ERR_INTERNAL for any other purely internal errors
2712 * Additionnally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002713 * Note that we try to prevent the network stack from sending the ACK during the
2714 * connect() when a pure TCP check is used (without PROXY protocol).
2715 */
2716 switch (ret) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002717 case SF_ERR_NONE:
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002718 /* we allow up to min(inter, timeout.connect) for a connection
2719 * to establish but only when timeout.check is set
2720 * as it may be to short for a full check otherwise
2721 */
2722 t->expire = tick_add(now_ms, MS_TO_TICKS(check->inter));
2723
2724 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
2725 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
2726 t->expire = tick_first(t->expire, t_con);
2727 }
2728 break;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002729 case SF_ERR_SRVTO: /* ETIMEDOUT */
2730 case SF_ERR_SRVCL: /* ECONNREFUSED, ENETUNREACH, ... */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002731 step = tcpcheck_get_step_id(check);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002732 chunk_printf(&trash, "TCPCHK error establishing connection at step %d: %s",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002733 step, strerror(errno));
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002734 comment = tcpcheck_get_step_comment(check, step);
2735 if (comment)
2736 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002737 set_server_check_status(check, HCHK_STATUS_L4CON, trash.str);
2738 goto out_end_tcpcheck;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002739 case SF_ERR_PRXCOND:
2740 case SF_ERR_RESOURCE:
2741 case SF_ERR_INTERNAL:
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002742 step = tcpcheck_get_step_id(check);
2743 chunk_printf(&trash, "TCPCHK error establishing connection at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002744 comment = tcpcheck_get_step_comment(check, step);
2745 if (comment)
2746 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002747 set_server_check_status(check, HCHK_STATUS_SOCKERR, trash.str);
2748 goto out_end_tcpcheck;
2749 }
2750
2751 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002752 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002753
2754 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002755 while (&check->current_step->list != head &&
2756 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002757 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002758
Willy Tarreauf2c87352015-05-13 12:08:21 +02002759 if (&check->current_step->list == head)
2760 break;
2761
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002762 /* don't do anything until the connection is established */
2763 if (!(conn->flags & CO_FL_CONNECTED)) {
2764 /* update expire time, should be done by process_chk */
2765 /* we allow up to min(inter, timeout.connect) for a connection
2766 * to establish but only when timeout.check is set
2767 * as it may be to short for a full check otherwise
2768 */
2769 while (tick_is_expired(t->expire, now_ms)) {
2770 int t_con;
2771
2772 t_con = tick_add(t->expire, s->proxy->timeout.connect);
2773 t->expire = tick_add(t->expire, MS_TO_TICKS(check->inter));
2774
2775 if (s->proxy->timeout.check)
2776 t->expire = tick_first(t->expire, t_con);
2777 }
2778 return;
2779 }
2780
2781 } /* end 'connect' */
2782 else if (check->current_step->action == TCPCHK_ACT_SEND) {
2783 /* mark the step as started */
2784 check->last_started_step = check->current_step;
2785
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002786 /* reset the read buffer */
2787 if (*check->bi->data != '\0') {
2788 *check->bi->data = '\0';
2789 check->bi->i = 0;
2790 }
2791
2792 if (conn->flags & (CO_FL_SOCK_WR_SH | CO_FL_DATA_WR_SH)) {
2793 conn->flags |= CO_FL_ERROR;
2794 chk_report_conn_err(conn, 0, 0);
2795 goto out_end_tcpcheck;
2796 }
2797
Willy Tarreauabca5b62013-12-06 14:19:25 +01002798 if (check->current_step->string_len >= check->bo->size) {
2799 chunk_printf(&trash, "tcp-check send : string too large (%d) for buffer size (%d) at step %d",
2800 check->current_step->string_len, check->bo->size,
Simon Hormane16c1b32015-01-30 11:22:57 +09002801 tcpcheck_get_step_id(check));
Willy Tarreauabca5b62013-12-06 14:19:25 +01002802 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2803 goto out_end_tcpcheck;
2804 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002805
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002806 /* do not try to send if there is no space */
2807 if (check->current_step->string_len >= buffer_total_space(check->bo))
2808 continue;
2809
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002810 bo_putblk(check->bo, check->current_step->string, check->current_step->string_len);
2811 *check->bo->p = '\0'; /* to make gdb output easier to read */
2812
Willy Tarreauabca5b62013-12-06 14:19:25 +01002813 /* go to next rule and try to send */
Willy Tarreau5581c272015-05-13 12:24:53 +02002814 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002815
2816 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002817 while (&check->current_step->list != head &&
2818 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002819 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Willy Tarreauf2c87352015-05-13 12:08:21 +02002820
2821 if (&check->current_step->list == head)
2822 break;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002823 } /* end 'send' */
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002824 else if (check->current_step->action == TCPCHK_ACT_EXPECT) {
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002825 if (unlikely(check->result == CHK_RES_FAILED))
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002826 goto out_end_tcpcheck;
2827
Willy Tarreau310987a2014-01-22 19:46:33 +01002828 if (conn->xprt->rcv_buf(conn, check->bi, check->bi->size) <= 0) {
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002829 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH)) {
2830 done = 1;
2831 if ((conn->flags & CO_FL_ERROR) && !check->bi->i) {
2832 /* Report network errors only if we got no other data. Otherwise
2833 * we'll let the upper layers decide whether the response is OK
2834 * or not. It is very common that an RST sent by the server is
2835 * reported as an error just after the last data chunk.
2836 */
2837 chk_report_conn_err(conn, errno, 0);
2838 goto out_end_tcpcheck;
2839 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002840 }
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002841 else
Willy Tarreau263013d2015-05-13 11:59:14 +02002842 break;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002843 }
2844
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002845 /* mark the step as started */
2846 check->last_started_step = check->current_step;
2847
2848
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002849 /* Intermediate or complete response received.
2850 * Terminate string in check->bi->data buffer.
2851 */
2852 if (check->bi->i < check->bi->size) {
2853 check->bi->data[check->bi->i] = '\0';
2854 }
2855 else {
2856 check->bi->data[check->bi->i - 1] = '\0';
2857 done = 1; /* buffer full, don't wait for more data */
2858 }
2859
2860 contentptr = check->bi->data;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002861
2862 /* Check that response body is not empty... */
Willy Tarreauec6b0122014-05-13 17:57:29 +02002863 if (!check->bi->i) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002864 if (!done)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002865 continue;
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002866
2867 /* empty response */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002868 step = tcpcheck_get_step_id(check);
2869 chunk_printf(&trash, "TCPCHK got an empty response at step %d", step);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002870 comment = tcpcheck_get_step_comment(check, step);
2871 if (comment)
2872 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002873 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2874
2875 goto out_end_tcpcheck;
2876 }
2877
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002878 if (!done && (check->current_step->string != NULL) && (check->bi->i < check->current_step->string_len) )
Willy Tarreaua970c282013-12-06 12:47:19 +01002879 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002880
Willy Tarreaua970c282013-12-06 12:47:19 +01002881 tcpcheck_expect:
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002882 if (check->current_step->string != NULL)
2883 ret = my_memmem(contentptr, check->bi->i, check->current_step->string, check->current_step->string_len) != NULL;
2884 else if (check->current_step->expect_regex != NULL)
2885 ret = regex_exec(check->current_step->expect_regex, contentptr);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002886
2887 if (!ret && !done)
Willy Tarreaua970c282013-12-06 12:47:19 +01002888 continue; /* try to read more */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002889
2890 /* matched */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002891 step = tcpcheck_get_step_id(check);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002892 if (ret) {
2893 /* matched but we did not want to => ERROR */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002894 if (check->current_step->inverse) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002895 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002896 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002897 chunk_printf(&trash, "TCPCHK matched unwanted content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002898 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002899 }
2900 else {
2901 /* we were looking for a regex */
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002902 chunk_printf(&trash, "TCPCHK matched unwanted content (regex) at step %d", step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002903 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002904 comment = tcpcheck_get_step_comment(check, step);
2905 if (comment)
2906 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002907 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2908 goto out_end_tcpcheck;
2909 }
2910 /* matched and was supposed to => OK, next step */
2911 else {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002912 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002913 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002914
2915 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002916 while (&check->current_step->list != head &&
2917 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002918 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002919
Willy Tarreauf2c87352015-05-13 12:08:21 +02002920 if (&check->current_step->list == head)
2921 break;
2922
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002923 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002924 goto tcpcheck_expect;
2925 __conn_data_stop_recv(conn);
2926 }
2927 }
2928 else {
2929 /* not matched */
2930 /* not matched and was not supposed to => OK, next step */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002931 if (check->current_step->inverse) {
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002932 /* allow next rule */
Willy Tarreau5581c272015-05-13 12:24:53 +02002933 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002934
2935 /* bypass all comment rules */
Willy Tarreauf2c87352015-05-13 12:08:21 +02002936 while (&check->current_step->list != head &&
2937 check->current_step->action == TCPCHK_ACT_COMMENT)
Willy Tarreau5581c272015-05-13 12:24:53 +02002938 check->current_step = LIST_NEXT(&check->current_step->list, struct tcpcheck_rule *, list);
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002939
Willy Tarreauf2c87352015-05-13 12:08:21 +02002940 if (&check->current_step->list == head)
2941 break;
2942
Willy Tarreau98aec9f2013-12-06 16:16:41 +01002943 if (check->current_step->action == TCPCHK_ACT_EXPECT)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002944 goto tcpcheck_expect;
2945 __conn_data_stop_recv(conn);
2946 }
2947 /* not matched but was supposed to => ERROR */
2948 else {
2949 /* we were looking for a string */
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002950 if (check->current_step->string != NULL) {
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002951 chunk_printf(&trash, "TCPCHK did not match content '%s' at step %d",
Willy Tarreauce8c42a2015-05-13 11:23:01 +02002952 check->current_step->string, step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002953 }
2954 else {
2955 /* we were looking for a regex */
2956 chunk_printf(&trash, "TCPCHK did not match content (regex) at step %d",
Baptiste Assmanncfbd1b82015-05-02 09:00:23 +02002957 step);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002958 }
Baptiste Assmann22b09d22015-05-01 08:03:04 +02002959 comment = tcpcheck_get_step_comment(check, step);
2960 if (comment)
2961 chunk_appendf(&trash, " comment: '%s'", comment);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002962 set_server_check_status(check, HCHK_STATUS_L7RSP, trash.str);
2963 goto out_end_tcpcheck;
2964 }
2965 }
2966 } /* end expect */
2967 } /* end loop over double chained step list */
2968
Willy Tarreau263013d2015-05-13 11:59:14 +02002969 /* We're waiting for some I/O to complete, we've reached the end of the
2970 * rules, or both. Do what we have to do, otherwise we're done.
2971 */
2972 if (&check->current_step->list == head && !check->bo->o) {
2973 set_server_check_status(check, HCHK_STATUS_L7OKD, "(tcp-check)");
2974 goto out_end_tcpcheck;
2975 }
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002976
Willy Tarreau53c5a042015-05-13 11:38:17 +02002977 /* warning, current_step may now point to the head */
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002978 if (check->bo->o)
2979 __conn_data_want_send(conn);
2980
Willy Tarreau53c5a042015-05-13 11:38:17 +02002981 if (&check->current_step->list != head &&
2982 check->current_step->action == TCPCHK_ACT_EXPECT)
Willy Tarreaufbe0edf2013-12-06 16:54:31 +01002983 __conn_data_want_recv(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002984 return;
2985
2986 out_end_tcpcheck:
2987 /* collect possible new errors */
2988 if (conn->flags & CO_FL_ERROR)
2989 chk_report_conn_err(conn, 0, 0);
2990
Baptiste Assmann69e273f2013-12-11 00:52:19 +01002991 /* cleanup before leaving */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002992 check->current_step = NULL;
2993
Willy Tarreau6aaa1b82013-12-11 17:09:34 +01002994 if (check->result == CHK_RES_FAILED)
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002995 conn->flags |= CO_FL_ERROR;
2996
2997 __conn_data_stop_both(conn);
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02002998 return;
2999}
3000
Simon Hormanb1900d52015-01-30 11:22:54 +09003001const char *init_check(struct check *check, int type)
3002{
3003 check->type = type;
3004
3005 /* Allocate buffer for requests... */
3006 if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3007 return "out of memory while allocating check buffer";
3008 }
3009 check->bi->size = global.tune.chksize;
3010
3011 /* Allocate buffer for responses... */
3012 if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
3013 return "out of memory while allocating check buffer";
3014 }
3015 check->bo->size = global.tune.chksize;
3016
3017 /* Allocate buffer for partial results... */
3018 if ((check->conn = calloc(1, sizeof(struct connection))) == NULL) {
3019 return "out of memory while allocating check connection";
3020 }
3021
3022 check->conn->t.sock.fd = -1; /* no agent in progress yet */
3023
3024 return NULL;
3025}
3026
Simon Hormanbfb5d332015-01-30 11:22:55 +09003027void free_check(struct check *check)
3028{
3029 free(check->bi);
3030 free(check->bo);
3031 free(check->conn);
3032}
3033
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003034void email_alert_free(struct email_alert *alert)
3035{
3036 struct tcpcheck_rule *rule, *back;
3037
3038 if (!alert)
3039 return;
3040
3041 list_for_each_entry_safe(rule, back, &alert->tcpcheck_rules, list)
3042 free(rule);
3043 free(alert);
3044}
3045
3046static struct task *process_email_alert(struct task *t)
3047{
3048 struct check *check = t->context;
3049 struct email_alertq *q;
3050
3051 q = container_of(check, typeof(*q), check);
3052
3053 if (!(check->state & CHK_ST_ENABLED)) {
3054 if (LIST_ISEMPTY(&q->email_alerts)) {
3055 /* All alerts processed, delete check */
3056 task_delete(t);
3057 task_free(t);
3058 check->task = NULL;
3059 return NULL;
3060 } else {
3061 struct email_alert *alert;
3062
3063 alert = LIST_NEXT(&q->email_alerts, typeof(alert), list);
3064 check->tcpcheck_rules = &alert->tcpcheck_rules;
3065 LIST_DEL(&alert->list);
3066
3067 check->state |= CHK_ST_ENABLED;
3068 }
3069
3070 }
3071
3072 process_chk(t);
3073
3074 if (!(check->state & CHK_ST_INPROGRESS) && check->tcpcheck_rules) {
3075 struct email_alert *alert;
3076
3077 alert = container_of(check->tcpcheck_rules, typeof(*alert), tcpcheck_rules);
3078 email_alert_free(alert);
3079
3080 check->tcpcheck_rules = NULL;
3081 check->state &= ~CHK_ST_ENABLED;
3082 }
3083 return t;
3084}
3085
3086static int init_email_alert_checks(struct server *s)
3087{
3088 int i;
3089 struct mailer *mailer;
3090 const char *err_str;
3091 struct proxy *p = s->proxy;
3092
3093 if (p->email_alert.queues)
3094 /* Already initialised, nothing to do */
3095 return 1;
3096
3097 p->email_alert.queues = calloc(p->email_alert.mailers.m->count, sizeof *p->email_alert.queues);
3098 if (!p->email_alert.queues) {
3099 err_str = "out of memory while allocating checks array";
3100 goto error_alert;
3101 }
3102
3103 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3104 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
3105 struct email_alertq *q = &p->email_alert.queues[i];
3106 struct check *check = &q->check;
3107
3108
3109 LIST_INIT(&q->email_alerts);
3110
Pieter Baauw235fcfc2016-02-13 15:33:40 +01003111 check->inter = p->email_alert.mailers.m->timeout.mail;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003112 check->rise = DEF_AGENT_RISETIME;
3113 check->fall = DEF_AGENT_FALLTIME;
3114 err_str = init_check(check, PR_O2_TCPCHK_CHK);
3115 if (err_str) {
3116 goto error_free;
3117 }
3118
3119 check->xprt = mailer->xprt;
3120 if (!get_host_port(&mailer->addr))
3121 /* Default to submission port */
3122 check->port = 587;
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003123 check->addr = mailer->addr;
3124 check->server = s;
3125 }
3126
3127 return 1;
3128
3129error_free:
3130 while (i-- > 1)
3131 task_free(p->email_alert.queues[i].check.task);
3132 free(p->email_alert.queues);
3133 p->email_alert.queues = NULL;
3134error_alert:
3135 Alert("Email alert [%s] could not be initialised: %s\n", p->id, err_str);
3136 return 0;
3137}
3138
3139
3140static int add_tcpcheck_expect_str(struct list *list, const char *str)
3141{
3142 struct tcpcheck_rule *tcpcheck;
3143
3144 tcpcheck = calloc(1, sizeof *tcpcheck);
3145 if (!tcpcheck)
3146 return 0;
3147
3148 tcpcheck->action = TCPCHK_ACT_EXPECT;
3149 tcpcheck->string = strdup(str);
3150 if (!tcpcheck->string) {
3151 free(tcpcheck);
3152 return 0;
3153 }
3154
3155 LIST_ADDQ(list, &tcpcheck->list);
3156 return 1;
3157}
3158
3159static int add_tcpcheck_send_strs(struct list *list, const char * const *strs)
3160{
3161 struct tcpcheck_rule *tcpcheck;
3162 int i;
3163
3164 tcpcheck = calloc(1, sizeof *tcpcheck);
3165 if (!tcpcheck)
3166 return 0;
3167
3168 tcpcheck->action = TCPCHK_ACT_SEND;
3169
3170 tcpcheck->string_len = 0;
3171 for (i = 0; strs[i]; i++)
3172 tcpcheck->string_len += strlen(strs[i]);
3173
3174 tcpcheck->string = malloc(tcpcheck->string_len + 1);
3175 if (!tcpcheck->string) {
3176 free(tcpcheck);
3177 return 0;
3178 }
3179 tcpcheck->string[0] = '\0';
3180
3181 for (i = 0; strs[i]; i++)
3182 strcat(tcpcheck->string, strs[i]);
3183
3184 LIST_ADDQ(list, &tcpcheck->list);
3185 return 1;
3186}
3187
3188static int enqueue_one_email_alert(struct email_alertq *q, const char *msg)
3189{
3190 struct email_alert *alert = NULL;
3191 struct tcpcheck_rule *tcpcheck;
3192 struct check *check = &q->check;
3193 struct proxy *p = check->server->proxy;
3194
3195 alert = calloc(1, sizeof *alert);
3196 if (!alert) {
3197 goto error;
3198 }
3199 LIST_INIT(&alert->tcpcheck_rules);
3200
3201 tcpcheck = calloc(1, sizeof *tcpcheck);
3202 if (!tcpcheck)
3203 goto error;
3204 tcpcheck->action = TCPCHK_ACT_CONNECT;
3205 LIST_ADDQ(&alert->tcpcheck_rules, &tcpcheck->list);
3206
3207 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "220 "))
3208 goto error;
3209
3210 {
3211 const char * const strs[4] = { "EHLO ", p->email_alert.myhostname, "\r\n" };
3212 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3213 goto error;
3214 }
3215
3216 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3217 goto error;
3218
3219 {
3220 const char * const strs[4] = { "MAIL FROM:<", p->email_alert.from, ">\r\n" };
3221 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3222 goto error;
3223 }
3224
3225 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3226 goto error;
3227
3228 {
3229 const char * const strs[4] = { "RCPT TO:<", p->email_alert.to, ">\r\n" };
3230 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3231 goto error;
3232 }
3233
3234 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3235 goto error;
3236
3237 {
3238 const char * const strs[2] = { "DATA\r\n" };
3239 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3240 goto error;
3241 }
3242
3243 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "354 "))
3244 goto error;
3245
3246 {
3247 struct tm tm;
3248 char datestr[48];
3249 const char * const strs[18] = {
Pieter Baauw5e0964e2016-02-13 16:27:35 +01003250 "From: ", p->email_alert.from, "\r\n",
3251 "To: ", p->email_alert.to, "\r\n",
3252 "Date: ", datestr, "\r\n",
3253 "Subject: [HAproxy Alert] ", msg, "\r\n",
3254 "\r\n",
3255 msg, "\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003256 "\r\n",
Pieter Baauwed35c372015-07-22 19:51:54 +02003257 ".\r\n",
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003258 NULL
3259 };
3260
3261 get_localtime(date.tv_sec, &tm);
3262
3263 if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %T %z (%Z)", &tm) == 0) {
3264 goto error;
3265 }
3266
3267 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3268 goto error;
3269 }
3270
3271 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "250 "))
3272 goto error;
3273
3274 {
3275 const char * const strs[2] = { "QUIT\r\n" };
3276 if (!add_tcpcheck_send_strs(&alert->tcpcheck_rules, strs))
3277 goto error;
3278 }
3279
3280 if (!add_tcpcheck_expect_str(&alert->tcpcheck_rules, "221 "))
3281 goto error;
3282
3283 if (!check->task) {
3284 struct task *t;
3285
3286 if ((t = task_new()) == NULL)
3287 goto error;
3288
3289 check->task = t;
3290 t->process = process_email_alert;
3291 t->context = check;
3292
3293 /* check this in one ms */
3294 t->expire = tick_add(now_ms, MS_TO_TICKS(1));
3295 check->start = now;
3296 task_queue(t);
3297 }
3298
3299 LIST_ADDQ(&q->email_alerts, &alert->list);
3300
3301 return 1;
3302
3303error:
3304 email_alert_free(alert);
3305 return 0;
3306}
3307
3308static void enqueue_email_alert(struct proxy *p, const char *msg)
3309{
3310 int i;
3311 struct mailer *mailer;
3312
3313 for (i = 0, mailer = p->email_alert.mailers.m->mailer_list;
3314 i < p->email_alert.mailers.m->count; i++, mailer = mailer->next) {
3315 if (!enqueue_one_email_alert(&p->email_alert.queues[i], msg)) {
3316 Alert("Email alert [%s] could not be enqueued: out of memory\n", p->id);
3317 return;
3318 }
3319 }
3320
3321 return;
3322}
3323
3324/*
3325 * Send email alert if configured.
3326 */
Simon Horman64e34162015-02-06 11:11:57 +09003327void send_email_alert(struct server *s, int level, const char *format, ...)
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003328{
3329 va_list argp;
3330 char buf[1024];
3331 int len;
3332 struct proxy *p = s->proxy;
3333
Simon Horman64e34162015-02-06 11:11:57 +09003334 if (!p->email_alert.mailers.m || level > p->email_alert.level ||
3335 format == NULL || !init_email_alert_checks(s))
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003336 return;
3337
3338 va_start(argp, format);
3339 len = vsnprintf(buf, sizeof(buf), format, argp);
3340 va_end(argp);
3341
3342 if (len < 0) {
Cyril Bontéb65e0332015-12-04 03:07:08 +01003343 Alert("Email alert [%s] could not format message\n", p->id);
Simon Horman0ba0e4a2015-01-30 11:23:00 +09003344 return;
3345 }
3346
3347 enqueue_email_alert(p, buf);
3348}
3349
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +02003350
Willy Tarreaubd741542010-03-16 18:46:54 +01003351/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02003352 * Local variables:
3353 * c-indent-level: 8
3354 * c-basic-offset: 8
3355 * End:
3356 */