MEDIUM: server: implement agent check for dynamic servers

This commit is the counterpart for agent check of
"MEDIUM: server: implement check for dynamic servers".

The "agent-check" keyword is enabled for dynamic servers. The agent
check must manually be activated via "enable agent" CLI. This can
enable the dynamic server if the agent response is "ready" without an
explicit "enable server" CLI.
diff --git a/doc/management.txt b/doc/management.txt
index eaa6e31..dbc3229 100644
--- a/doc/management.txt
+++ b/doc/management.txt
@@ -1477,10 +1477,21 @@
 
   Use the "check" keyword to enable health-check support. Note that the
   health-check is disabled by default and must be enabled independently from
-  the server using the "enable health" command.
+  the server using the "enable health" command. For agent checks, use the
+  "agent-check" keyword and the "enable agent" command. Note that in this case
+  the server may be activated via the agent depending on the status reported,
+  without an explicit "enable server" command. This also means that extra care
+  is required when removing a dynamic server with agent check. The agent should
+  be first deactivated via "disable agent" to be able to put the server in the
+  required maintenance mode before removal.
 
   Here is the list of the currently supported keywords :
 
+  - agent-addr
+  - agent-check
+  - agent-inter
+  - agent-port
+  - agent-send
   - allow-0rtt
   - alpn
   - addr
diff --git a/src/check.c b/src/check.c
index 637b6c9..b49aca6 100644
--- a/src/check.c
+++ b/src/check.c
@@ -2382,7 +2382,7 @@
 static struct srv_kw_list srv_kws = { "CHK", { }, {
 	{ "addr",                srv_parse_addr,                1,  1,  1 }, /* IP address to send health to or to probe from agent-check */
 	{ "agent-addr",          srv_parse_agent_addr,          1,  1,  1 }, /* Enable an auxiliary agent check */
-	{ "agent-check",         srv_parse_agent_check,         0,  1,  0 }, /* Enable agent checks */
+	{ "agent-check",         srv_parse_agent_check,         0,  1,  1 }, /* Enable agent checks */
 	{ "agent-inter",         srv_parse_agent_inter,         1,  1,  1 }, /* Set the interval between two agent checks */
 	{ "agent-port",          srv_parse_agent_port,          1,  1,  1 }, /* Set the TCP port used for agent checks. */
 	{ "agent-send",          srv_parse_agent_send,          1,  1,  1 }, /* Set string to send to agent. */
diff --git a/src/server.c b/src/server.c
index 749d44e..b762ce9 100644
--- a/src/server.c
+++ b/src/server.c
@@ -4564,9 +4564,9 @@
 			goto out;
 	}
 
-	/* Init check if configured. The check is manually disabled because a
-	 * dynamic server is started in a disable state. It must be manually
-	 * activated via a "enable health" command.
+	/* Init check/agent if configured. The check is manually disabled
+	 * because a dynamic server is started in a disable state. It must be
+	 * manually activated via a "enable health/agent" command.
 	 */
 	if (srv->do_check) {
 		if (init_srv_check(srv))
@@ -4575,6 +4575,13 @@
 		srv->check.state &= ~CHK_ST_ENABLED;
 		srv_use_dynsrv(srv);
 	}
+	else if (srv->do_agent) {
+		if (init_srv_agent_check(srv))
+			goto out;
+
+		srv->agent.state &= ~CHK_ST_ENABLED;
+		srv_use_dynsrv(srv);
+	}
 
 	/* Attach the server to the end of the proxy linked list. Note that this
 	 * operation is not thread-safe so this is executed under thread
@@ -4636,6 +4643,10 @@
 		if (!start_check_task(&srv->check, 0, 1, 1))
 			ha_alert("System might be unstable, consider to execute a reload");
 	}
+	else if (srv->agent.state & CHK_ST_CONFIGURED) {
+		if (!start_check_task(&srv->agent, 0, 1, 1))
+			ha_alert("System might be unstable, consider to execute a reload");
+	}
 
 	ha_notice("New server registered.\n");
 	cli_msg(appctx, LOG_INFO, usermsgs_str());
@@ -4649,6 +4660,8 @@
 
 		if (srv->check.state & CHK_ST_CONFIGURED)
 			free_check(&srv->check);
+		else if (srv->agent.state & CHK_ST_CONFIGURED)
+			free_check(&srv->agent);
 
 		/* remove the server from the proxy linked list */
 		if (be->srv == srv) {
@@ -4756,6 +4769,8 @@
 	/* stop the check task if running */
 	if (srv->check.state & CHK_ST_CONFIGURED)
 		check_purge(&srv->check);
+	else if (srv->agent.state & CHK_ST_CONFIGURED)
+		check_purge(&srv->agent);
 
 	/* detach the server from the proxy linked list
 	 * The proxy servers list is currently not protected by a lock, so this