]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
REORG: cli: move "clear counters" to stats.c
authorWilly Tarreau <w@1wt.eu>
Wed, 23 Nov 2016 10:02:40 +0000 (11:02 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 24 Nov 2016 15:59:28 +0000 (16:59 +0100)
This command is only used to clear stats. It now relies on cli_has_level()
to validate the permissions.

src/cli.c
src/stats.c

index 8c7414fb95937a33e4bd9846d6e191a86683c6d2..b0e0a6debc03d6272cb941e133a8c716f2da5876 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -70,7 +70,6 @@ static struct applet cli_applet;
 
 static const char stats_sock_usage_msg[] =
        "Unknown command. Please enter one of the following commands only :\n"
-       "  clear counters : clear max statistics counters (add 'all' for all counters)\n"
        "  help           : this message\n"
        "  prompt         : toggle interactive mode with prompt\n"
        "  quit           : disconnect\n"
@@ -542,70 +541,6 @@ static int stats_sock_parse_request(struct stream_interface *si, char *line)
                        }
                }
        }
-       else if (strcmp(args[0], "clear") == 0) {
-               if (strcmp(args[1], "counters") == 0) {
-                       struct proxy *px;
-                       struct server *sv;
-                       struct listener *li;
-                       int clrall = 0;
-
-                       if (strcmp(args[2], "all") == 0)
-                               clrall = 1;
-
-                       /* check permissions */
-                       if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
-                           (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
-                               appctx->ctx.cli.msg = stats_permission_denied_msg;
-                               appctx->st0 = STAT_CLI_PRINT;
-                               return 1;
-                       }
-
-                       for (px = proxy; px; px = px->next) {
-                               if (clrall) {
-                                       memset(&px->be_counters, 0, sizeof(px->be_counters));
-                                       memset(&px->fe_counters, 0, sizeof(px->fe_counters));
-                               }
-                               else {
-                                       px->be_counters.conn_max = 0;
-                                       px->be_counters.p.http.rps_max = 0;
-                                       px->be_counters.sps_max = 0;
-                                       px->be_counters.cps_max = 0;
-                                       px->be_counters.nbpend_max = 0;
-
-                                       px->fe_counters.conn_max = 0;
-                                       px->fe_counters.p.http.rps_max = 0;
-                                       px->fe_counters.sps_max = 0;
-                                       px->fe_counters.cps_max = 0;
-                                       px->fe_counters.nbpend_max = 0;
-                               }
-
-                               for (sv = px->srv; sv; sv = sv->next)
-                                       if (clrall)
-                                               memset(&sv->counters, 0, sizeof(sv->counters));
-                                       else {
-                                               sv->counters.cur_sess_max = 0;
-                                               sv->counters.nbpend_max = 0;
-                                               sv->counters.sps_max = 0;
-                                       }
-
-                               list_for_each_entry(li, &px->conf.listeners, by_fe)
-                                       if (li->counters) {
-                                               if (clrall)
-                                                       memset(li->counters, 0, sizeof(*li->counters));
-                                               else
-                                                       li->counters->conn_max = 0;
-                                       }
-                       }
-
-                       global.cps_max = 0;
-                       global.sps_max = 0;
-                       return 1;
-               }
-               else {
-                       /* unknown "clear" argument */
-                       return 0;
-               }
-       }
        else if (strcmp(args[0], "set") == 0) {
                if (strcmp(args[1], "maxconn") == 0) {
                        if (strcmp(args[2], "frontend") == 0) {
index 56266eba7964c649fdf26cbdd344edf3976bbb50..64be56c65a651e92978b84b7d67a32b2035e51d9 100644 (file)
@@ -3034,6 +3034,64 @@ static int stats_dump_info_to_buffer(struct stream_interface *si)
        return 1;
 }
 
+static int cli_parse_clear_counters(char **args, struct appctx *appctx, void *private)
+{
+       struct proxy *px;
+       struct server *sv;
+       struct listener *li;
+       int clrall = 0;
+
+       if (strcmp(args[2], "all") == 0)
+               clrall = 1;
+
+       /* check permissions */
+       if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
+           (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
+               return 1;
+
+       for (px = proxy; px; px = px->next) {
+               if (clrall) {
+                       memset(&px->be_counters, 0, sizeof(px->be_counters));
+                       memset(&px->fe_counters, 0, sizeof(px->fe_counters));
+               }
+               else {
+                       px->be_counters.conn_max = 0;
+                       px->be_counters.p.http.rps_max = 0;
+                       px->be_counters.sps_max = 0;
+                       px->be_counters.cps_max = 0;
+                       px->be_counters.nbpend_max = 0;
+
+                       px->fe_counters.conn_max = 0;
+                       px->fe_counters.p.http.rps_max = 0;
+                       px->fe_counters.sps_max = 0;
+                       px->fe_counters.cps_max = 0;
+                       px->fe_counters.nbpend_max = 0;
+               }
+
+               for (sv = px->srv; sv; sv = sv->next)
+                       if (clrall)
+                               memset(&sv->counters, 0, sizeof(sv->counters));
+                       else {
+                               sv->counters.cur_sess_max = 0;
+                               sv->counters.nbpend_max = 0;
+                               sv->counters.sps_max = 0;
+                       }
+
+               list_for_each_entry(li, &px->conf.listeners, by_fe)
+                       if (li->counters) {
+                               if (clrall)
+                                       memset(li->counters, 0, sizeof(*li->counters));
+                               else
+                                       li->counters->conn_max = 0;
+                       }
+       }
+
+       global.cps_max = 0;
+       global.sps_max = 0;
+       return 1;
+}
+
+
 static int cli_parse_show_info(char **args, struct appctx *appctx, void *private)
 {
        if (strcmp(args[2], "typed") == 0)
@@ -3075,6 +3133,7 @@ static int cli_io_handler_dump_stat(struct appctx *appctx)
 
 /* register cli keywords */
 static struct cli_kw_list cli_kws = {{ },{
+       { { "clear", "counters",  NULL }, "clear counters : clear max statistics counters (add 'all' for all counters)", cli_parse_clear_counters, NULL, NULL },
        { { "show", "info",  NULL }, "show info      : report information about the running process", cli_parse_show_info, cli_io_handler_dump_info, NULL },
        { { "show", "stat",  NULL }, "show stat      : report counters for each proxy and server", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
        {{},}