]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: backend: Add sample fetches to get the server's weight
authorChristopher Faulet <cfaulet@haproxy.com>
Fri, 10 Jul 2020 14:03:45 +0000 (16:03 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 15 Jul 2020 12:08:14 +0000 (14:08 +0200)
The following sample fetches have been added :

 * srv_iweight : returns the initial server's weight
 * srv_uweight : returns the user-visible server's weight
 * srv_weight  : returns the current (or effetctive) server's weight

The requested server must be passed as argument, evnetually preceded by the
backend name. For instance :

  srv_weight(back-http/www1)

doc/configuration.txt
src/backend.c

index d040af6ba744084ee6a3327a7a1b04b498e673b3..b7c3b7e8d98e8b340bb4fdebd7dfbe600189583f 100644 (file)
@@ -16160,6 +16160,21 @@ srv_sess_rate([<backend>/]<server>) : integer
         acl srv2_full srv_sess_rate(be1/srv2) gt 50
         use_backend be2 if srv1_full or srv2_full
 
+srv_iweight([<backend>/]<server>): integer
+  Returns an integer corresponding to the server's initial weight. If <backend>
+  is omitted, then the server is looked up in the current backend. See also
+  "srv_weight" and "srv_uweight".
+
+srv_uweight([<backend>/]<server>): integer
+  Returns an integer corresponding to the user visible server's weight. If
+  <backend> is omitted, then the server is looked up in the current
+  backend. See also "srv_weight" and "srv_iweight".
+
+srv_weight([<backend>/]<server>): integer
+  Returns an integer corresponding to the current (or effective) server's
+  weight. If <backend> is omitted, then the server is looked up in the current
+  backend. See also "srv_iweight" and "srv_uweight".
+
 stopping : boolean
   Returns TRUE if the process calling the function is currently stopping. This
   can be useful for logging, or for relaxing certain checks or helping close
index 372389aae2a572d9846b24880a70d50ef2798b76..6ec45d265f7a045e8ab2a8abb099cd0fd817b630 100644 (file)
@@ -2810,6 +2810,48 @@ smp_fetch_srv_sess_rate(const struct arg *args, struct sample *smp, const char *
        return 1;
 }
 
+/* set temp integer to the server weight.
+ * Accepts exactly 1 argument. Argument is a server, other types will lead to
+ * undefined behaviour.
+ */
+static int
+smp_fetch_srv_weight(const struct arg *args, struct sample *smp, const char *kw, void *private)
+{
+       struct server *srv = args->data.srv;
+       struct proxy *px = srv->proxy;
+
+       smp->flags = SMP_F_VOL_TEST;
+       smp->data.type = SMP_T_SINT;
+       smp->data.u.sint = (srv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv;
+       return 1;
+}
+
+/* set temp integer to the server initial weight.
+ * Accepts exactly 1 argument. Argument is a server, other types will lead to
+ * undefined behaviour.
+ */
+static int
+smp_fetch_srv_iweight(const struct arg *args, struct sample *smp, const char *kw, void *private)
+{
+       smp->flags = SMP_F_VOL_TEST;
+       smp->data.type = SMP_T_SINT;
+       smp->data.u.sint = args->data.srv->iweight;
+       return 1;
+}
+
+/* set temp integer to the server user-specified weight.
+ * Accepts exactly 1 argument. Argument is a server, other types will lead to
+ * undefined behaviour.
+ */
+static int
+smp_fetch_srv_uweight(const struct arg *args, struct sample *smp, const char *kw, void *private)
+{
+       smp->flags = SMP_F_VOL_TEST;
+       smp->data.type = SMP_T_SINT;
+       smp->data.u.sint = args->data.srv->uweight;
+       return 1;
+}
+
 static int sample_conv_nbsrv(const struct arg *args, struct sample *smp, void *private)
 {
 
@@ -2881,6 +2923,9 @@ static struct sample_fetch_kw_list smp_kws = {ILH, {
        { "srv_name",      smp_fetch_srv_name,       0,           NULL, SMP_T_STR,  SMP_USE_SERVR, },
        { "srv_queue",     smp_fetch_srv_queue,      ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
        { "srv_sess_rate", smp_fetch_srv_sess_rate,  ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
+       { "srv_weight",    smp_fetch_srv_weight,     ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
+       { "srv_iweight",   smp_fetch_srv_iweight,    ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
+       { "srv_uweight",   smp_fetch_srv_uweight,    ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
        { /* END */ },
 }};