]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
doveadm: Added initial implementation of "stats top" command.
authorTimo Sirainen <tss@iki.fi>
Fri, 26 Aug 2011 02:15:42 +0000 (05:15 +0300)
committerTimo Sirainen <tss@iki.fi>
Fri, 26 Aug 2011 02:15:42 +0000 (05:15 +0300)
src/doveadm/Makefile.am
src/doveadm/doveadm-stats.c [new file with mode: 0644]
src/doveadm/doveadm.c
src/doveadm/doveadm.h

index 8602a7e423ccd9f2f0b804bf96d30f53ddb6d9cf..7c7c5a3880b0ee429b4c625a52e39fd0e81a1056 100644 (file)
@@ -98,6 +98,7 @@ doveadm_SOURCES = \
        doveadm-proxy.c \
        doveadm-pw.c \
        doveadm-sis.c \
+       doveadm-stats.c \
        doveadm-who.c
 
 doveadm_server_SOURCES = \
diff --git a/src/doveadm/doveadm-stats.c b/src/doveadm/doveadm-stats.c
new file mode 100644 (file)
index 0000000..e5017ae
--- /dev/null
@@ -0,0 +1,95 @@
+/* Copyright (c) 2011 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "network.h"
+#include "istream.h"
+#include "str.h"
+#include "strescape.h"
+#include "doveadm.h"
+#include "doveadm-print.h"
+
+#include <unistd.h>
+
+static const char *const*
+read_next_line(struct istream *input)
+{
+       const char *line;
+       char **args;
+       unsigned int i;
+
+       line = i_stream_read_next_line(input);
+       if (line == NULL)
+               return NULL;
+
+       args = p_strsplit(pool_datastack_create(), line, "\t");
+       for (i = 0; args[i] != NULL; i++)
+               args[i] = str_tabunescape(args[i]);
+       return (void *)args;
+}
+
+static void stats_lookup(const char *path)
+{
+#define TOP_CMD "EXPORT\tsession\tconnected\n"
+       struct istream *input;
+       const char *const *args;
+       unsigned int i;
+       int fd;
+
+       fd = doveadm_connect(path);
+       net_set_nonblock(fd, FALSE);
+
+       input = i_stream_create_fd(fd, (size_t)-1, TRUE);
+       if (write(fd, TOP_CMD, strlen(TOP_CMD)) < 0)
+               i_fatal("write(%s) failed: %m", path);
+
+       /* read header */
+       args = read_next_line(input);
+       if (args == NULL)
+               i_fatal("read(%s) unexpectedly disconnected", path);
+       for (; *args != NULL; args++)
+               doveadm_print_header_simple(*args);
+
+       /* read lines */
+       do {
+               T_BEGIN {
+                       args = read_next_line(input);
+                       if (args[0] == NULL)
+                               args = NULL;
+                       if (args != NULL) {
+                               for (i = 0; args[i] != NULL; i++)
+                                       doveadm_print(args[i]);
+                       }
+               } T_END;
+       } while (args != NULL);
+       if (input->stream_errno != 0)
+               i_fatal("read(%s) failed: %m", path);
+       i_stream_destroy(&input);
+}
+
+static void cmd_stats_top(int argc, char *argv[])
+{
+       const char *path;
+       int c;
+
+       path = t_strconcat(doveadm_settings->base_dir, "/stats", NULL);
+
+       while ((c = getopt(argc, argv, "s:")) > 0) {
+               switch (c) {
+               case 's':
+                       path = optarg;
+                       break;
+               default:
+                       help(&doveadm_cmd_stats);
+               }
+       }
+       argv += optind - 1;
+       if (argv[1] != NULL)
+               help(&doveadm_cmd_stats);
+
+       doveadm_print_init(DOVEADM_PRINT_TYPE_TABLE);
+       stats_lookup(path);
+}
+
+struct doveadm_cmd doveadm_cmd_stats = {
+       cmd_stats_top, "stats top", "[-s <stats socket path>]"
+};
index 6aedced609a266c041c4e778c4fb2a5c1cea52fe..f0b55efdf5cb91ec7d882e439fc76710f3df4923 100644 (file)
@@ -259,7 +259,8 @@ static struct doveadm_cmd *doveadm_commands[] = {
        &doveadm_cmd_kick,
        &doveadm_cmd_mailbox_mutf7,
        &doveadm_cmd_sis_deduplicate,
-       &doveadm_cmd_sis_find
+       &doveadm_cmd_sis_find,
+       &doveadm_cmd_stats
 };
 
 int main(int argc, char *argv[])
index e8d0273eb093ae0d36f722f0e460a56058816190..25ebf88b369dd40c803081ae80813ab75e9cd7c7 100644 (file)
@@ -26,6 +26,7 @@ extern struct doveadm_cmd doveadm_cmd_kick;
 extern struct doveadm_cmd doveadm_cmd_mailbox_mutf7;
 extern struct doveadm_cmd doveadm_cmd_sis_deduplicate;
 extern struct doveadm_cmd doveadm_cmd_sis_find;
+extern struct doveadm_cmd doveadm_cmd_stats;
 
 void doveadm_register_cmd(const struct doveadm_cmd *cmd);