]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
REORG: cli: move show stat resolvers to dns.c
authorWilliam Lallemand <wlallemand@haproxy.com>
Fri, 18 Nov 2016 23:58:54 +0000 (00:58 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 24 Nov 2016 15:59:27 +0000 (16:59 +0100)
Move dns CLI functions to dns.c and use the cli keyword API to register
actions on the CLI.

include/types/cli.h
src/cli.c
src/dns.c

index 8503e84c3834177c62deaddec9cbff4bc928425c..a7a85653f95c26b5a3e4dcedc105b03cef58513e 100644 (file)
@@ -118,7 +118,6 @@ enum {
        STAT_CLI_O_SET,      /* set entries in tables */
        STAT_CLI_O_STAT,     /* dump stats */
        STAT_CLI_O_POOLS,    /* dump memory pools */
-       STAT_CLI_O_RESOLVERS,/* dump a resolver's section nameservers counters */
        STAT_CLI_O_SERVERS_STATE, /* dump server state and changing information */
        STAT_CLI_O_BACKEND,  /* dump backend list */
        STAT_CLI_O_ENV,      /* dump environment */
index 39010510466722f2bfcc8ee492a930b326d49af6..c00fc865d2284def65f733025bab04a4ef45302f 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -142,7 +142,6 @@ static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct st
 static int stats_dump_sess_to_buffer(struct stream_interface *si);
 static int stats_dump_errors_to_buffer(struct stream_interface *si);
 static int stats_table_request(struct stream_interface *si, int show);
-static int stats_dump_resolvers_to_buffer(struct stream_interface *si);
 
 static int dump_servers_state(struct stream_interface *si, struct chunk *buf);
 
@@ -160,8 +159,6 @@ static const char stats_sock_usage_msg[] =
        "  show info      : report information about the running process\n"
        "  show pools     : report information about the memory pools usage\n"
        "  show stat      : report counters for each proxy and server\n"
-       "  show stat resolvers [id]: dumps counters from all resolvers section and\n"
-       "                            associated name servers\n"
        "  show errors    : report last request and response errors for each proxy\n"
        "  show sess [id] : report the list of current sessions or dump this session\n"
        "  show table [id]: report table usage stats or dump this table's contents\n"
@@ -1081,29 +1078,7 @@ static int stats_sock_parse_request(struct stream_interface *si, char *line)
                        }
                }
                else if (strcmp(args[1], "stat") == 0) {
-                       if (strcmp(args[2], "resolvers") == 0) {
-                               struct dns_resolvers *presolvers;
-
-                               if (*args[3]) {
-                                       appctx->ctx.resolvers.ptr = NULL;
-                                       list_for_each_entry(presolvers, &dns_resolvers, list) {
-                                               if (strcmp(presolvers->id, args[3]) == 0) {
-                                                       appctx->ctx.resolvers.ptr = presolvers;
-                                                       break;
-                                               }
-                                       }
-                                       if (appctx->ctx.resolvers.ptr == NULL) {
-                                               appctx->ctx.cli.msg = "Can't find that resolvers section\n";
-                                               appctx->st0 = STAT_CLI_PRINT;
-                                               return 1;
-                                       }
-                               }
-
-                               appctx->st2 = STAT_ST_INIT;
-                               appctx->st0 = STAT_CLI_O_RESOLVERS;
-                               return 1;
-                       }
-                       else if (*args[2] && *args[3] && *args[4]) {
+                       if (*args[2] && *args[3] && *args[4]) {
                                appctx->ctx.stats.flags |= STAT_BOUND;
                                appctx->ctx.stats.iid = atoi(args[2]);
                                appctx->ctx.stats.type = atoi(args[3]);
@@ -2070,10 +2045,6 @@ static void cli_io_handler(struct appctx *appctx)
                                if (stats_dump_stat_to_buffer(si, NULL))
                                        appctx->st0 = STAT_CLI_PROMPT;
                                break;
-                       case STAT_CLI_O_RESOLVERS:
-                               if (stats_dump_resolvers_to_buffer(si))
-                                       appctx->st0 = STAT_CLI_PROMPT;
-                               break;
                        case STAT_CLI_O_SESS:
                                if (stats_dump_sess_to_buffer(si))
                                        appctx->st0 = STAT_CLI_PROMPT;
@@ -3309,71 +3280,6 @@ static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len
        return ptr;
 }
 
-/* This function dumps counters from all resolvers section and associated name servers.
- * It returns 0 if the output buffer is full and it needs
- * to be called again, otherwise non-zero.
- */
-static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
-{
-       struct appctx *appctx = __objt_appctx(si->end);
-       struct dns_resolvers *presolvers;
-       struct dns_nameserver *pnameserver;
-
-       chunk_reset(&trash);
-
-       switch (appctx->st2) {
-       case STAT_ST_INIT:
-               appctx->st2 = STAT_ST_LIST; /* let's start producing data */
-               /* fall through */
-
-       case STAT_ST_LIST:
-               if (LIST_ISEMPTY(&dns_resolvers)) {
-                       chunk_appendf(&trash, "No resolvers found\n");
-               }
-               else {
-                       list_for_each_entry(presolvers, &dns_resolvers, list) {
-                               if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
-                                       continue;
-
-                               chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
-                               list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
-                                       chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
-                                       chunk_appendf(&trash, "  sent: %ld\n", pnameserver->counters.sent);
-                                       chunk_appendf(&trash, "  valid: %ld\n", pnameserver->counters.valid);
-                                       chunk_appendf(&trash, "  update: %ld\n", pnameserver->counters.update);
-                                       chunk_appendf(&trash, "  cname: %ld\n", pnameserver->counters.cname);
-                                       chunk_appendf(&trash, "  cname_error: %ld\n", pnameserver->counters.cname_error);
-                                       chunk_appendf(&trash, "  any_err: %ld\n", pnameserver->counters.any_err);
-                                       chunk_appendf(&trash, "  nx: %ld\n", pnameserver->counters.nx);
-                                       chunk_appendf(&trash, "  timeout: %ld\n", pnameserver->counters.timeout);
-                                       chunk_appendf(&trash, "  refused: %ld\n", pnameserver->counters.refused);
-                                       chunk_appendf(&trash, "  other: %ld\n", pnameserver->counters.other);
-                                       chunk_appendf(&trash, "  invalid: %ld\n", pnameserver->counters.invalid);
-                                       chunk_appendf(&trash, "  too_big: %ld\n", pnameserver->counters.too_big);
-                                       chunk_appendf(&trash, "  truncated: %ld\n", pnameserver->counters.truncated);
-                                       chunk_appendf(&trash, "  outdated: %ld\n", pnameserver->counters.outdated);
-                               }
-                       }
-               }
-
-               /* display response */
-               if (bi_putchk(si_ic(si), &trash) == -1) {
-                       /* let's try again later from this session. We add ourselves into
-                        * this session's users so that it can remove us upon termination.
-                        */
-                       si->flags |= SI_FL_WAIT_ROOM;
-                       return 0;
-               }
-
-               appctx->st2 = STAT_ST_FIN;
-               /* fall through */
-
-       default:
-               appctx->st2 = STAT_ST_FIN;
-               return 1;
-       }
-}
-
 /* This function dumps all captured errors onto the stream interface's
  * read buffer. It returns 0 if the output buffer is full and it needs
  * to be called again, otherwise non-zero.
index f21c50807d381ef81d7311f1ecc44a0d50fa7bb9..b391f4abadf1722cf14981af29612374192dfe7c 100644 (file)
--- a/src/dns.c
+++ b/src/dns.c
 #include <common/time.h>
 #include <common/ticks.h>
 
+#include <types/applet.h>
+#include <types/cli.h>
 #include <types/global.h>
 #include <types/dns.h>
 #include <types/proto_udp.h>
+#include <types/stats.h>
 
+#include <proto/channel.h>
+#include <proto/cli.h>
 #include <proto/checks.h>
 #include <proto/dns.h>
 #include <proto/fd.h>
@@ -33,6 +38,7 @@
 #include <proto/server.h>
 #include <proto/task.h>
 #include <proto/proto_udp.h>
+#include <proto/stream_interface.h>
 
 struct list dns_resolvers = LIST_HEAD_INIT(dns_resolvers);
 struct dns_resolution *resolution = NULL;
@@ -1261,3 +1267,108 @@ struct task *dns_process_resolve(struct task *t)
        dns_update_resolvers_timeout(resolvers);
        return t;
 }
+
+static int cli_parse_stat_resolvers(char **args, struct appctx *appctx, void *private)
+{
+       struct dns_resolvers *presolvers;
+
+       if (*args[3]) {
+               appctx->ctx.resolvers.ptr = NULL;
+               list_for_each_entry(presolvers, &dns_resolvers, list) {
+                       if (strcmp(presolvers->id, args[3]) == 0) {
+                               appctx->ctx.resolvers.ptr = presolvers;
+                               break;
+                       }
+               }
+               if (appctx->ctx.resolvers.ptr == NULL) {
+                       appctx->ctx.cli.msg = "Can't find that resolvers section\n";
+                       appctx->st0 = STAT_CLI_PRINT;
+                       return 1;
+               }
+       }
+
+       appctx->st2 = STAT_ST_INIT;
+       return 1;
+
+}
+
+/* This function dumps counters from all resolvers section and associated name servers.
+ * It returns 0 if the output buffer is full and it needs
+ * to be called again, otherwise non-zero.
+ */
+static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
+{
+       struct stream_interface *si = appctx->owner;
+       struct dns_resolvers *presolvers;
+       struct dns_nameserver *pnameserver;
+
+       chunk_reset(&trash);
+
+       switch (appctx->st2) {
+       case STAT_ST_INIT:
+               appctx->st2 = STAT_ST_LIST; /* let's start producing data */
+               /* fall through */
+
+       case STAT_ST_LIST:
+               if (LIST_ISEMPTY(&dns_resolvers)) {
+                       chunk_appendf(&trash, "No resolvers found\n");
+               }
+               else {
+                       list_for_each_entry(presolvers, &dns_resolvers, list) {
+                               if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
+                                       continue;
+
+                               chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
+                               list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
+                                       chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
+                                       chunk_appendf(&trash, "  sent: %ld\n", pnameserver->counters.sent);
+                                       chunk_appendf(&trash, "  valid: %ld\n", pnameserver->counters.valid);
+                                       chunk_appendf(&trash, "  update: %ld\n", pnameserver->counters.update);
+                                       chunk_appendf(&trash, "  cname: %ld\n", pnameserver->counters.cname);
+                                       chunk_appendf(&trash, "  cname_error: %ld\n", pnameserver->counters.cname_error);
+                                       chunk_appendf(&trash, "  any_err: %ld\n", pnameserver->counters.any_err);
+                                       chunk_appendf(&trash, "  nx: %ld\n", pnameserver->counters.nx);
+                                       chunk_appendf(&trash, "  timeout: %ld\n", pnameserver->counters.timeout);
+                                       chunk_appendf(&trash, "  refused: %ld\n", pnameserver->counters.refused);
+                                       chunk_appendf(&trash, "  other: %ld\n", pnameserver->counters.other);
+                                       chunk_appendf(&trash, "  invalid: %ld\n", pnameserver->counters.invalid);
+                                       chunk_appendf(&trash, "  too_big: %ld\n", pnameserver->counters.too_big);
+                                       chunk_appendf(&trash, "  truncated: %ld\n", pnameserver->counters.truncated);
+                                       chunk_appendf(&trash, "  outdated: %ld\n", pnameserver->counters.outdated);
+                               }
+                       }
+               }
+
+               /* display response */
+               if (bi_putchk(si_ic(si), &trash) == -1) {
+                       /* let's try again later from this session. We add ourselves into
+                        * this session's users so that it can remove us upon termination.
+                        */
+                       si->flags |= SI_FL_WAIT_ROOM;
+                       return 0;
+               }
+
+               appctx->st2 = STAT_ST_FIN;
+               /* fall through */
+
+       default:
+               appctx->st2 = STAT_ST_FIN;
+               return 1;
+       }
+}
+
+/* register cli keywords */
+static struct cli_kw_list cli_kws = {{ },{
+       { { "show", "stat", "resolvers", NULL }, "show stat resolvers [id]: dumps counters from all resolvers section and\n"
+                                                "                          associated name servers",
+                                                cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
+       {{},}
+}};
+
+
+__attribute__((constructor))
+static void __dns_init(void)
+{
+       cli_register_kw(&cli_kws);
+}
+