]> git.kaiwu.me - haproxy.git/commitdiff
MINOR: extcheck: pass the master and worker IDs to external checks
authorWilly Tarreau <w@1wt.eu>
Tue, 15 Sep 2026 14:20:58 +0000 (16:20 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 16 Sep 2026 16:00:51 +0000 (18:00 +0200)
By default, i.e. unless "external-check preserve-env" is used, the
environment of an external check is not inherited but entirely rebuilt
from the curated list in extcheck_envs[], so the two new identifiers
would not be visible from a check script. Let's add them there, next to
PATH, as a new process-scoped group before the proxy and server ones.
It's also cleaner because we pass the real directives, not the env
variables which could have been modified.

doc/configuration.txt
src/extcheck.c

index 1f3a096d4b4970af94e9242350b1832e231dfe68..3b44d7592218e5cdaf09202d1df027e369eb68bd 100644 (file)
@@ -8123,6 +8123,13 @@ external-check command <command>
   Some values are also provided through environment variables.
 
   Environment variables :
+    HAPROXY_MASTER_ID       The unique identifier of the master process, or
+                            empty when not running in master-worker mode. See
+                            section 2.3.
+
+    HAPROXY_WORKER_ID       The unique identifier of the worker process. See
+                            section 2.3 and the "worker-id" global keyword.
+
     HAPROXY_PROXY_ADDR      The first bind address if available (or empty if not
                             applicable, for example in a "backend" section).
 
index 3d325eaf83857f589f9c01958707e30d0971acb6..e19c30afc4c39b3e1ed6ce746f021f61d9dd5391 100644 (file)
@@ -63,6 +63,10 @@ struct extcheck_env {
 enum {
        EXTCHK_PATH = 0,
 
+       /* Process specific environment variables */
+       EXTCHK_HAPROXY_MASTER_ID,       /* the master's unique ID (empty if no master) */
+       EXTCHK_HAPROXY_WORKER_ID,       /* the worker's unique ID */
+
        /* Proxy specific environment variables */
        EXTCHK_HAPROXY_PROXY_NAME,      /* the backend name */
        EXTCHK_HAPROXY_PROXY_ID,        /* the backend id */
@@ -84,6 +88,8 @@ enum {
 
 const struct extcheck_env extcheck_envs[EXTCHK_SIZE] = {
        [EXTCHK_PATH]                   = { "PATH",                   EXTCHK_SIZE_EVAL_INIT },
+       [EXTCHK_HAPROXY_MASTER_ID]      = { "HAPROXY_MASTER_ID",      EXTCHK_SIZE_EVAL_INIT },
+       [EXTCHK_HAPROXY_WORKER_ID]      = { "HAPROXY_WORKER_ID",      EXTCHK_SIZE_EVAL_INIT },
        [EXTCHK_HAPROXY_PROXY_NAME]     = { "HAPROXY_PROXY_NAME",     EXTCHK_SIZE_EVAL_INIT },
        [EXTCHK_HAPROXY_PROXY_ID]       = { "HAPROXY_PROXY_ID",       EXTCHK_SIZE_EVAL_INIT },
        [EXTCHK_HAPROXY_PROXY_ADDR]     = { "HAPROXY_PROXY_ADDR",     EXTCHK_SIZE_EVAL_INIT },
@@ -331,6 +337,11 @@ int prepare_external_check(struct check *check)
        }
 
        EXTCHK_SETENV(check, EXTCHK_PATH, path, err);
+       /* Add process environment variables. The master's ID is empty when
+        * there is no master, i.e. when not running in master-worker mode.
+        */
+       EXTCHK_SETENV(check, EXTCHK_HAPROXY_MASTER_ID, global.master_id ? global.master_id : "", err);
+       EXTCHK_SETENV(check, EXTCHK_HAPROXY_WORKER_ID, global.worker_id ? global.worker_id : "", err);
        /* Add proxy environment variables */
        EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_NAME, px->id, err);
        EXTCHK_SETENV(check, EXTCHK_HAPROXY_PROXY_ID, ultoa_r(px->uuid, buf, sizeof(buf)), err);