]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: global: define diagnostic mode of execution
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Mon, 29 Mar 2021 08:29:07 +0000 (10:29 +0200)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Thu, 1 Apr 2021 16:03:37 +0000 (18:03 +0200)
Define MODE_DIAG which is used to run haproxy in diagnostic mode. This
mode is used to output extra warnings about possible configuration
blunder or sub-optimal usage. It can be activated with argument '-dD'.

A new output function ha_diag_warning is implemented reserved for
diagnostic output. It serves to standardize the format of diagnostic
messages.

A macro HA_DIAG_WARN_COND is also available to automatically check if
diagnostic mode is on before executing the diagnostic check.

doc/management.txt
include/haproxy/errors.h
include/haproxy/global-t.h
src/haproxy.c
src/log.c

index c7eb7fff4ecf1349f703e351450f3ac23529609e..44183282ac28ff7d67caec68b48a2ff13eb55ab7 100644 (file)
@@ -199,6 +199,10 @@ list of options is :
     in foreground and to show incoming and outgoing events. It must never be
     used in an init script.
 
+  -dD : enable diagnostic mode. This mode will output extra warnings about
+    suspicious configuration statements. This will never prevent startup even in
+    "zero-warning" mode nor change the exit status code.
+
   -dG : disable use of getaddrinfo() to resolve host names into addresses. It
     can be used when suspecting that getaddrinfo() doesn't work as expected.
     This option was made available because many bogus implementations of
index 521876f74ed1fb400c86c1153985debb40638e95..e0eff9c3cc56c60022188c6a283bf042c0ee3397 100644 (file)
@@ -74,6 +74,22 @@ void ha_alert(const char *fmt, ...)
 void ha_warning(const char *fmt, ...)
        __attribute__ ((format(printf, 1, 2)));
 
+/*
+ * These functions are reserved to output diagnostics on MODE_DIAG.
+ * Use the underscore variants only if MODE_DIAG has already been checked.
+ */
+void _ha_vdiag_warning(const char *fmt, va_list argp);
+void _ha_diag_warning(const char *fmt, ...);
+void ha_diag_warning(const char *fmt, ...)
+       __attribute__ ((format(printf, 1 ,2)));
+
+/* Check for both MODE_DIAG and <cond> before outputting a diagnostic warning */
+#define HA_DIAG_WARNING_COND(cond, fmt, ...)                  \
+       do {                                                  \
+               if ((global.mode & MODE_DIAG) && (cond))      \
+                       _ha_diag_warning((fmt), __VA_ARGS__); \
+       } while (0)
+
 /*
  * Displays the message on stderr with the date and pid.
  */
index 3bbdd72907dd580caab9014330bd9d02c98563be..c98a0fabaea1faee97d3b8c4047f3ebe60cc6e8e 100644 (file)
@@ -38,6 +38,7 @@
 #define        MODE_MWORKER    0x80    /* Master Worker */
 #define        MODE_MWORKER_WAIT       0x100    /* Master Worker wait mode */
 #define        MODE_ZERO_WARNING       0x200    /* warnings cause a failure */
+#define        MODE_DIAG       0x400   /* extra warnings */
 
 /* list of last checks to perform, depending on config options */
 #define LSTCHK_CAP_BIND        0x00000001      /* check that we can bind to any port */
index 50d91f770ac86501b6f0822f97ae9691809578bf..aeb92e6b0761790e52b471fbd16f9725d303ed4e 100644 (file)
@@ -403,6 +403,7 @@ static void usage(char *name)
                "        -dr ignores server address resolution failures\n"
                "        -dV disables SSL verify on servers side\n"
                "        -dW fails if any warning is emitted\n"
+               "        -dD diagnostic mode : warn about suspicious configuration statements\n"
                "        -sf/-st [pid ]* finishes/terminates old pids.\n"
                "        -x <unix_socket> get listening sockets from a unix socket\n"
                "        -S <bind>[,<bind options>...] new master CLI\n"
@@ -1401,6 +1402,8 @@ static void init(int argc, char **argv)
                                arg_mode |= MODE_VERBOSE;
                        else if (*flag == 'd' && flag[1] == 'b')
                                arg_mode |= MODE_FOREGROUND;
+                       else if (*flag == 'd' && flag[1] == 'D')
+                               arg_mode |= MODE_DIAG;
                        else if (*flag == 'd' && flag[1] == 'W')
                                arg_mode |= MODE_ZERO_WARNING;
                        else if (*flag == 'd' && flag[1] == 'M')
@@ -1541,7 +1544,8 @@ static void init(int argc, char **argv)
        }
 
        global.mode |= (arg_mode & (MODE_DAEMON | MODE_MWORKER | MODE_FOREGROUND | MODE_VERBOSE
-                                   | MODE_QUIET | MODE_CHECK | MODE_DEBUG | MODE_ZERO_WARNING));
+                                   | MODE_QUIET | MODE_CHECK | MODE_DEBUG | MODE_ZERO_WARNING
+                                   | MODE_DIAG));
 
        if (getenv("HAPROXY_MWORKER_WAIT_ONLY")) {
                unsetenv("HAPROXY_MWORKER_WAIT_ONLY");
index e002c48ac2e395a14b3a917401de4ce54d515bb8..292eb588a8d5b340d88b0aa3cd637d3b4a89880e 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -1133,6 +1133,42 @@ void ha_warning(const char *fmt, ...)
        }
 }
 
+/*
+ * Variant of _ha_diag_warning with va_list.
+ * Use it only if MODE_DIAG has been previously checked.
+ */
+void _ha_vdiag_warning(const char *fmt, va_list argp)
+{
+       print_message("DIAG/WARNING", fmt, argp);
+}
+
+/*
+ * Output a diagnostic warning.
+ * Use it only if MODE_DIAG has been previously checked.
+ */
+void _ha_diag_warning(const char *fmt, ...)
+{
+       va_list argp;
+
+       va_start(argp, fmt);
+       _ha_vdiag_warning(fmt, argp);
+       va_end(argp);
+}
+
+/*
+ * Output a diagnostic warning. Do nothing of MODE_DIAG is not on.
+ */
+void ha_diag_warning(const char *fmt, ...)
+{
+       va_list argp;
+
+       if (global.mode & MODE_DIAG) {
+               va_start(argp, fmt);
+               _ha_vdiag_warning(fmt, argp);
+               va_end(argp);
+       }
+}
+
 /*
  * Displays the message on stderr with the date and pid.
  */