blob: c23a58ee03caac5687b0c05fd52a3210544eacdc [file] [log] [blame]
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001/*
2 * Health-checks.
3 *
4 * Copyright 2008-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Thierry FOURNIER7eeb4352013-06-14 15:28:25 +020013#ifndef _TYPES_CHECKS_H
14#define _TYPES_CHECKS_H
15
Willy Tarreau8e85ad52013-12-11 16:45:07 +010016#include <sys/time.h>
17
18#include <common/config.h>
19#include <common/mini-clist.h>
20#include <common/regex.h>
Willy Tarreaua8561db2020-03-06 18:40:31 +010021#include <common/buf.h>
Willy Tarreau8e85ad52013-12-11 16:45:07 +010022
23#include <types/connection.h>
24#include <types/obj_type.h>
25#include <types/task.h>
Willy Tarreau8e85ad52013-12-11 16:45:07 +010026
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010027/* enum used by check->result. Must remain in this order, as some code uses
28 * result >= CHK_RES_PASSED to declare success.
29 */
30enum chk_result {
31 CHK_RES_UNKNOWN = 0, /* initialized to this by default */
Willy Tarreau23964182014-05-20 20:56:30 +020032 CHK_RES_NEUTRAL, /* valid check but no status information */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +010033 CHK_RES_FAILED, /* check failed */
34 CHK_RES_PASSED, /* check succeeded and server is fully up again */
35 CHK_RES_CONDPASS, /* check reports the server doesn't want new sessions */
36};
Willy Tarreau8e85ad52013-12-11 16:45:07 +010037
Willy Tarreau2c115e52013-12-11 19:41:16 +010038/* flags used by check->state */
39#define CHK_ST_INPROGRESS 0x0001 /* a check is currently running */
Willy Tarreau2e10f5a2013-12-11 20:11:55 +010040#define CHK_ST_CONFIGURED 0x0002 /* this check is configured and may be enabled */
41#define CHK_ST_ENABLED 0x0004 /* this check is currently administratively enabled */
Willy Tarreau33a08db2013-12-11 21:03:31 +010042#define CHK_ST_PAUSED 0x0008 /* checks are paused because of maintenance (health only) */
Willy Tarreau33434322013-12-11 21:15:19 +010043#define CHK_ST_AGENT 0x0010 /* check is an agent check (otherwise it's a health check) */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +020044#define CHK_ST_PORT_MISS 0x0020 /* check can't be send because no port is configured to run it */
Willy Tarreau8e85ad52013-12-11 16:45:07 +010045
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020046/* check status */
47enum {
48 HCHK_STATUS_UNKNOWN = 0, /* Unknown */
49 HCHK_STATUS_INI, /* Initializing */
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020050 HCHK_STATUS_START, /* Check started - SPECIAL STATUS */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020051
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020052 /* Below we have finished checks */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020053 HCHK_STATUS_CHECKED, /* DUMMY STATUS */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010054
Simon Hormanb7cd8f92012-03-19 07:25:28 +090055 HCHK_STATUS_HANA, /* Health analyze detected enough consecutive errors */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010056
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020057 HCHK_STATUS_SOCKERR, /* Socket error */
58
59 HCHK_STATUS_L4OK, /* L4 check passed, for example tcp connect */
60 HCHK_STATUS_L4TOUT, /* L4 timeout */
61 HCHK_STATUS_L4CON, /* L4 connection problem, for example: */
62 /* "Connection refused" (tcp rst) or "No route to host" (icmp) */
63
64 HCHK_STATUS_L6OK, /* L6 check passed */
65 HCHK_STATUS_L6TOUT, /* L6 (SSL) timeout */
66 HCHK_STATUS_L6RSP, /* L6 invalid response - protocol error */
67
68 HCHK_STATUS_L7TOUT, /* L7 (HTTP/SMTP) timeout */
69 HCHK_STATUS_L7RSP, /* L7 invalid response - protocol error */
70
Simon Hormanb7cd8f92012-03-19 07:25:28 +090071 /* Below we have layer 5-7 data available */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020072 HCHK_STATUS_L57DATA, /* DUMMY STATUS */
73 HCHK_STATUS_L7OKD, /* L7 check passed */
74 HCHK_STATUS_L7OKCD, /* L7 check conditionally passed */
75 HCHK_STATUS_L7STS, /* L7 response error, for example HTTP 5xx */
76
Simon Horman98637e52014-06-20 12:30:16 +090077 HCHK_STATUS_PROCERR, /* External process check failure */
78 HCHK_STATUS_PROCTOUT, /* External process check timeout */
79 HCHK_STATUS_PROCOK, /* External process check passed */
80
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020081 HCHK_STATUS_SIZE
82};
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +020083
Cyril Bontéac92a062014-12-27 22:28:38 +010084/* environment variables memory requirement for different types of data */
Christopher Fauletaaae9a02020-04-26 09:50:31 +020085#define EXTCHK_SIZE_EVAL_INIT 0 /* size determined during the init phase,
86 * such environment variables are not updatable. */
87#define EXTCHK_SIZE_ULONG 20 /* max string length for an unsigned long value */
88#define EXTCHK_SIZE_UINT 11 /* max string length for an unsigned int value */
89#define EXTCHK_SIZE_ADDR INET6_ADDRSTRLEN+1 /* max string length for an address */
Cyril Bontéac92a062014-12-27 22:28:38 +010090
91/* external checks environment variables */
92enum {
93 EXTCHK_PATH = 0,
94
95 /* Proxy specific environment variables */
96 EXTCHK_HAPROXY_PROXY_NAME, /* the backend name */
97 EXTCHK_HAPROXY_PROXY_ID, /* the backend id */
98 EXTCHK_HAPROXY_PROXY_ADDR, /* the first bind address if available (or empty) */
99 EXTCHK_HAPROXY_PROXY_PORT, /* the first bind port if available (or empty) */
100
101 /* Server specific environment variables */
102 EXTCHK_HAPROXY_SERVER_NAME, /* the server name */
103 EXTCHK_HAPROXY_SERVER_ID, /* the server id */
104 EXTCHK_HAPROXY_SERVER_ADDR, /* the server address */
105 EXTCHK_HAPROXY_SERVER_PORT, /* the server port if available (or empty) */
106 EXTCHK_HAPROXY_SERVER_MAXCONN, /* the server max connections */
107 EXTCHK_HAPROXY_SERVER_CURCONN, /* the current number of connections on the server */
108
109 EXTCHK_SIZE
110};
111
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100112
113/* health status for response tracking */
114enum {
115 HANA_STATUS_UNKNOWN = 0,
116
117 HANA_STATUS_L4_OK, /* L4 successful connection */
118 HANA_STATUS_L4_ERR, /* L4 unsuccessful connection */
119
120 HANA_STATUS_HTTP_OK, /* Correct http response */
121 HANA_STATUS_HTTP_STS, /* Wrong http response, for example HTTP 5xx */
122 HANA_STATUS_HTTP_HDRRSP, /* Invalid http response (headers) */
123 HANA_STATUS_HTTP_RSP, /* Invalid http response */
124
125 HANA_STATUS_HTTP_READ_ERROR, /* Read error */
126 HANA_STATUS_HTTP_READ_TIMEOUT, /* Read timeout */
127 HANA_STATUS_HTTP_BROKEN_PIPE, /* Unexpected close from server */
128
129 HANA_STATUS_SIZE
130};
131
132enum {
133 HANA_ONERR_UNKNOWN = 0,
134
135 HANA_ONERR_FASTINTER, /* Force fastinter*/
136 HANA_ONERR_FAILCHK, /* Simulate a failed check */
137 HANA_ONERR_SUDDTH, /* Enters sudden death - one more failed check will mark this server down */
138 HANA_ONERR_MARKDWN, /* Mark this server down, now! */
139};
140
141enum {
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900142 HANA_ONMARKEDDOWN_NONE = 0,
Simon Hormane0d1bfb2011-06-21 14:34:58 +0900143 HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS, /* Shutdown peer sessions */
144};
145
146enum {
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700147 HANA_ONMARKEDUP_NONE = 0,
148 HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS, /* Shutdown peer sessions */
149};
150
151enum {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100152 HANA_OBS_NONE = 0,
153
154 HANA_OBS_LAYER4, /* Observe L4 - for example tcp */
155 HANA_OBS_LAYER7, /* Observe L7 - for example http */
156
157 HANA_OBS_SIZE
158};
159
Willy Tarreau8e85ad52013-12-11 16:45:07 +0100160struct check {
Gaetan Rivet05d692d2020-02-14 17:42:54 +0100161 struct session *sess; /* Health check session. */
Gaetan Rivet13a50432020-02-21 18:13:44 +0100162 struct vars vars; /* Health check dynamic variables. */
Cyril Bonté9ce13112014-11-15 22:41:27 +0100163 struct xprt_ops *xprt; /* transport layer operations for health checks */
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200164 struct conn_stream *cs; /* conn_stream state for health checks */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200165 struct buffer bi, bo; /* input and output buffers to send/recv check */
Willy Tarreau8e85ad52013-12-11 16:45:07 +0100166 struct task *task; /* the task associated to the health check processing, NULL if disabled */
167 struct timeval start; /* last health check start time */
168 long duration; /* time in ms took to finish last health check */
169 short status, code; /* check result, check code */
Willy Tarreau08eaa782017-11-26 08:44:34 +0100170 unsigned short port; /* the port to use for the health checks */
Cyril Bonté9ce13112014-11-15 22:41:27 +0100171 char desc[HCHK_DESC_LEN]; /* health check description */
Christopher Fauletf61f33a2020-03-27 18:55:49 +0100172 char use_ssl; /* use SSL for health checks (1: on, 0: server mode, -1: off) */
Willy Tarreau8e85ad52013-12-11 16:45:07 +0100173 int send_proxy; /* send a PROXY protocol header with checks */
Christopher Faulet5d503fc2020-03-30 20:34:34 +0200174 struct tcpcheck_rules *tcpcheck_rules; /* tcp-check send / expect rules */
Willy Tarreau8e85ad52013-12-11 16:45:07 +0100175 struct tcpcheck_rule *current_step; /* current step when using tcpcheck */
176 int inter, fastinter, downinter; /* checks: time in milliseconds */
Willy Tarreau6aaa1b82013-12-11 17:09:34 +0100177 enum chk_result result; /* health-check result : CHK_RES_* */
Willy Tarreau2c115e52013-12-11 19:41:16 +0100178 int state; /* state of the check : CHK_ST_* */
Willy Tarreau8e85ad52013-12-11 16:45:07 +0100179 int health; /* 0 to rise-1 = bad;
180 * rise to rise+fall-1 = good */
181 int rise, fall; /* time in iterations */
182 int type; /* Check type, one of PR_O2_*_CHK */
James Brown55f9ff12015-10-21 18:19:05 -0700183 int send_string_len; /* length of agent command string */
Willy Tarreau08eaa782017-11-26 08:44:34 +0100184 char *send_string; /* optionally send a string when connecting to the agent */
Willy Tarreau8e85ad52013-12-11 16:45:07 +0100185 struct server *server; /* back-pointer to server */
Olivier Houchardc98aa1f2019-01-11 18:17:17 +0100186 struct proxy *proxy; /* proxy to be used */
Simon Horman98637e52014-06-20 12:30:16 +0900187 char **argv; /* the arguments to use if running a process-based check */
188 char **envp; /* the environment to use if running a process-based check */
189 struct pid_list *curpid; /* entry in pid_list used for current process-based test, or -1 if not in test */
Simon Horman0ba0e4a2015-01-30 11:23:00 +0900190 struct sockaddr_storage addr; /* the address to check */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200191 struct wait_event wait_list; /* Waiting for I/O events */
Olivier Houchard9130a962017-10-17 17:33:43 +0200192 char *sni; /* Server name */
Olivier Houchard92150142018-12-21 19:47:01 +0100193 char *alpn_str; /* ALPN to use for checks */
194 int alpn_len; /* ALPN string length */
Alexander Liu2a54bb72019-05-22 19:44:48 +0800195
196 int via_socks4; /* check the connection via socks4 proxy */
Willy Tarreau8e85ad52013-12-11 16:45:07 +0100197};
198
Krzysztof Piotr Oledzki213014e2009-09-27 15:50:02 +0200199struct check_status {
200 short result; /* one of SRV_CHK_* */
201 char *info; /* human readable short info */
202 char *desc; /* long description */
203};
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100204
Cyril Bontéac92a062014-12-27 22:28:38 +0100205struct extcheck_env {
206 char *name; /* environment variable name */
207 int vmaxlen; /* value maximum length, used to determine the required memory allocation */
208};
209
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100210struct analyze_status {
211 char *desc; /* description */
212 unsigned char lr[HANA_OBS_SIZE]; /* result for l4/l7: 0 = ignore, 1 - error, 2 - OK */
213};
Thierry FOURNIER7eeb4352013-06-14 15:28:25 +0200214
Christopher Fauleta202d1d2020-03-26 17:38:49 +0100215#define TCPCHK_OPT_NONE 0x0000 /* no options specified, default */
216#define TCPCHK_OPT_SEND_PROXY 0x0001 /* send proxy-protocol string */
217#define TCPCHK_OPT_SSL 0x0002 /* SSL connection */
218#define TCPCHK_OPT_LINGER 0x0004 /* Do not RST connection, let it linger */
219#define TCPCHK_OPT_DEFAULT_CONNECT 0x0008 /* Do a connect using server params */
Christopher Faulet085426a2020-03-30 13:07:02 +0200220#define TCPCHK_OPT_SOCKS4 0x0010 /* check the connection via socks4 proxy */
Gaetan Rivet06d963a2020-02-21 18:49:05 +0100221
222struct tcpcheck_connect {
Christopher Faulet5c288742020-03-31 08:15:58 +0200223 char *sni; /* server name to use for SSL connections */
224 char *alpn; /* ALPN to use for the SSL connection */
225 int alpn_len; /* ALPN string length */
226 uint16_t options; /* options when setting up a new connection */
227 uint16_t port; /* port to connect to */
Christopher Fauletb7d30092020-03-30 15:19:03 +0200228 struct sample_expr *port_expr; /* sample expr to determine the port, may be NULL */
Christopher Faulet5c288742020-03-31 08:15:58 +0200229 struct sockaddr_storage addr; /* the address to the connect */
Gaetan Rivet06d963a2020-02-21 18:49:05 +0100230};
231
Gaetan Rivet48219dc2020-02-21 18:41:28 +0100232enum tcpcheck_send_type {
Christopher Fauletf50f4e92020-03-30 19:52:29 +0200233 TCPCHK_SEND_UNDEF = 0, /* Send is not parsed. */
234 TCPCHK_SEND_STRING, /* Send an ASCII string. */
235 TCPCHK_SEND_BINARY, /* Send a binary sequence. */
236 TCPCHK_SEND_STRING_LF, /* Send an ASCII log-format string. */
237 TCPCHK_SEND_BINARY_LF, /* Send a binary log-format sequence. */
Gaetan Rivet48219dc2020-02-21 18:41:28 +0100238};
239
240struct tcpcheck_send {
241 enum tcpcheck_send_type type;
Christopher Fauletf50f4e92020-03-30 19:52:29 +0200242 union {
243 struct ist data; /* an ASCII string or a binary sequence */
244 struct list fmt; /* an ASCII or hexa log-format string */
245 };
Gaetan Rivet48219dc2020-02-21 18:41:28 +0100246};
247
Gaetan Rivetb616add2020-02-07 15:37:17 +0100248enum tcpcheck_expect_type {
249 TCPCHK_EXPECT_UNDEF = 0, /* Match is not used. */
250 TCPCHK_EXPECT_STRING, /* Matches a string. */
251 TCPCHK_EXPECT_REGEX, /* Matches a regular pattern. */
Gaetan Rivetefab6c62020-02-07 15:37:17 +0100252 TCPCHK_EXPECT_REGEX_BINARY, /* Matches a regular pattern on a hex-encoded text. */
Gaetan Rivetb616add2020-02-07 15:37:17 +0100253 TCPCHK_EXPECT_BINARY, /* Matches a binary sequence. */
254};
255
256struct tcpcheck_expect {
257 enum tcpcheck_expect_type type; /* Type of pattern used for matching. */
258 union {
259 char *string; /* Matching a literal string / binary anywhere in the response. */
260 struct my_regex *regex; /* Matching a regex pattern. */
261 };
262 struct tcpcheck_rule *head; /* first expect of a chain. */
263 int length; /* Size in bytes of the pattern referenced by string / binary. */
264 int inverse; /* Match is inversed. */
Gaetan Rivet9dcb09f2020-02-07 15:37:17 +0100265 int with_capture; /* Match will store captured groups for back-reference in comment. */
Gaetan Rivetb616add2020-02-07 15:37:17 +0100266 int min_recv; /* Minimum amount of data before an expect can be applied. (default: -1, ignored) */
267};
268
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100269struct tcpcheck_action_kw {
270 struct act_rule *rule;
271};
272
Willy Tarreau98aec9f2013-12-06 16:16:41 +0100273/* possible actions for tcpcheck_rule->action */
Gaetan Rivetdd667322020-02-14 11:25:09 +0100274enum tcpcheck_rule_type {
275 TCPCHK_ACT_SEND = 0, /* send action, regular string format */
276 TCPCHK_ACT_EXPECT, /* expect action, either regular or binary string */
277 TCPCHK_ACT_CONNECT, /* connect action, to probe a new port */
278 TCPCHK_ACT_COMMENT, /* no action, simply a comment used for logs */
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100279 TCPCHK_ACT_ACTION_KW, /* custom registered action_kw rule. */
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200280};
281
282struct tcpcheck_rule {
283 struct list list; /* list linked to from the proxy */
Gaetan Rivetdd667322020-02-14 11:25:09 +0100284 enum tcpcheck_rule_type action; /* type of the rule. */
Gaetan Rivet5301b012020-02-25 17:19:17 +0100285 int index; /* Index within the list. Starts at 0. */
Baptiste Assmannaa12b472015-04-25 16:16:48 +0200286 char *comment; /* comment to be used in the logs and on the stats socket */
Gaetan Rivet48219dc2020-02-21 18:41:28 +0100287 union {
Gaetan Rivet06d963a2020-02-21 18:49:05 +0100288 struct tcpcheck_connect connect; /* Connect rule. */
Gaetan Rivet48219dc2020-02-21 18:41:28 +0100289 struct tcpcheck_send send; /* Send rule. */
290 struct tcpcheck_expect expect; /* Expected pattern. */
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100291 struct tcpcheck_action_kw action_kw; /* Custom action. */
Gaetan Rivet48219dc2020-02-21 18:41:28 +0100292 };
Baptiste Assmann5ecb77f2013-10-06 23:24:13 +0200293};
294
Christopher Faulet5d503fc2020-03-30 20:34:34 +0200295#define TCPCHK_RULES_NONE 0x00000000
296#define TCPCHK_RULES_SHARED 0x00000001 /* Set for shared list of tcp-check rules */
297#define TCPCHK_RULES_DEF 0x00000002 /* Ruleset inherited from the default section */
298
299/* a list of tcp-check rules */
300struct tcpcheck_rules {
301 unsigned int flags; /* flags applied to the rules */
302 struct list *list; /* the list of tcpcheck_rules */
303};
304
305/* A list of tcp-check rules with a name */
306struct tcpcheck_ruleset {
307 char *name; /* the ruleset name */
308 struct list rules; /* the list of tcpcheck_rule */
309 struct list list; /* used to chain rulesets */
310};
311
312
Thierry FOURNIER7eeb4352013-06-14 15:28:25 +0200313#endif /* _TYPES_CHECKS_H */