From 68192b2cdff4b294cbbe5bcf8ee4b7f60e22d569 Mon Sep 17 00:00:00 2001 From: William Lallemand Date: Sat, 24 Sep 2022 15:44:42 +0200 Subject: [PATCH] MINOR: mworker: store and shows loading status The environment variable HAPROXY_LOAD_SUCCESS stores "1" if it successfully load the configuration and started, "0" otherwise. The "_loadstatus" master CLI command displays either "Loading failure!\n" or "Loading success.\n" --- src/haproxy.c | 2 ++ src/mworker.c | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/haproxy.c b/src/haproxy.c index a837f48b9..9b3b37f7b 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -866,6 +866,7 @@ void reexec_on_failure() sock_drop_unused_old_sockets(); usermsgs_clr(NULL); + setenv("HAPROXY_LOAD_SUCCESS", "0", 1); ha_warning("Loading failure!\n"); #if defined(USE_SYSTEMD) /* the sd_notify API is not able to send a reload failure signal. So @@ -3507,6 +3508,7 @@ int main(int argc, char **argv) sd_notifyf(0, "READY=1\nMAINPID=%lu\nSTATUS=Ready.\n", (unsigned long)getpid()); #endif /* if not in wait mode, reload in wait mode to free the memory */ + setenv("HAPROXY_LOAD_SUCCESS", "1", 1); ha_notice("Loading success.\n"); proc_self->failedreloads = 0; /* reset the number of failure */ mworker_reexec_waitmode(); diff --git a/src/mworker.c b/src/mworker.c index 49113df62..e613a4e16 100644 --- a/src/mworker.c +++ b/src/mworker.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -656,6 +657,28 @@ static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, v return 1; } +/* Displays if the current reload failed or succeed */ +static int cli_parse_status(char **args, char *payload, struct appctx *appctx, void *private) +{ + char *env; + + if (!cli_has_level(appctx, ACCESS_LVL_OPER)) + return 1; + + env = getenv("HAPROXY_LOAD_SUCCESS"); + if (!env) + return 1; + + if (strcmp(env, "0") == 0) { + return cli_msg(appctx, LOG_INFO, "Loading failure!\n"); + } else if (strcmp(env, "1") == 0) { + return cli_msg(appctx, LOG_INFO, "Loading success.\n"); + } + + return 1; +} + + static int mworker_parse_global_max_reloads(char **args, int section_type, struct proxy *curpx, const struct proxy *defpx, const char *file, int linenum, char **err) @@ -714,6 +737,7 @@ static struct cli_kw_list cli_kws = {{ },{ { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY}, { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY}, { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY}, + { { "_loadstatus", NULL }, NULL, cli_parse_status, NULL, NULL, NULL, ACCESS_MASTER_ONLY}, {{},} }};