]> git.meshlink.io Git - meshlink/commitdiff
Add a very primitive "top" command to tincctl.
authorGuus Sliepen <guus@tinc-vpn.org>
Sun, 15 May 2011 11:16:48 +0000 (13:16 +0200)
committerGuus Sliepen <guus@tinc-vpn.org>
Sun, 15 May 2011 11:16:48 +0000 (13:16 +0200)
m4/curses.m4
src/Makefile.am
src/tincctl.c
src/top.c [new file with mode: 0644]

index 1001f4970bb6542e52df92e62108811f6e9ce5e2..408ae282424f8e6d40261e084b83e883d9c5a7fb 100644 (file)
@@ -6,6 +6,7 @@ AC_DEFUN([tinc_CURSES],
     AS_HELP_STRING([--disable-curses], [disable curses support]))
   AS_IF([test "x$enable_curses" != "xno"], [
   AC_DEFINE(HAVE_CURSES, 1, [have curses support])
+    curses=true
     AC_ARG_WITH(curses,
       AS_HELP_STRING([--with-curses=DIR], [curses base directory, or:]),
       [curses="$withval"
@@ -31,8 +32,11 @@ AC_DEFUN([tinc_CURSES],
     )
 
     AC_CHECK_LIB(curses, initscr,
-      [LIBS="$LIBS -lcurses"],
+      [CURSES_LIBS="-lcurses"],
       [AC_MSG_ERROR("curses libraries not found.")]
     )
   ])
+
+  AC_SUBST(CURSES_LIBS)
+  AM_CONDITIONAL(CURSES, test "$curses" = true)
 ])
index 5c2ab85d2767a1811dd3c139f50ec82118244572..04ef0f9fe7afe8293e5a4fe68a9db9770cbf333e 100644 (file)
@@ -18,6 +18,11 @@ if TUNEMU
 tincd_SOURCES += bsd/tunemu.c
 endif
 
+if CURSES
+tincctl_SOURCES += top.c
+tincctl_LDADD = $(CURSES_LIBS)
+endif
+
 nodist_tincd_SOURCES = device.c
 
 DEFAULT_INCLUDES =
index 632e7ac6efa8720197e7623b69b429087c49f528..489d10cd0e32dab518dc4085ab19c0a97d983701 100644 (file)
@@ -1,6 +1,6 @@
 /*
     tincctl.c -- Controlling a running tincd
-    Copyright (C) 2007-2009 Guus Sliepen <guus@tinc-vpn.org>
+    Copyright (C) 2007-2011 Guus Sliepen <guus@tinc-vpn.org>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -26,6 +26,8 @@
 #include "control_common.h"
 #include "rsagen.h"
 #include "utils.h"
+#include "tincctl.h"
+#include "top.h"
 
 /* The name this program was run with. */
 char *program_name = NULL;
@@ -93,6 +95,9 @@ static void usage(bool status) {
                                "  retry                      Retry all outgoing connections\n"
                                "  reload                     Partial reload of configuration\n"
                                "  disconnect NODE            Close meta connection with NODE\n"
+#ifdef HAVE_CURSES
+                               "  top                        Show real-time statistics\n"
+#endif
                                "\n");
                printf("Report bugs to tinc@tinc-vpn.org.\n");
        }
@@ -296,7 +301,7 @@ static void make_names(void) {
        }
 }
 
-static bool recvline(int fd, char *line, size_t len) {
+bool recvline(int fd, char *line, size_t len) {
        static char buffer[4096];
        static size_t blen = 0;
        char *newline = NULL;
@@ -323,7 +328,7 @@ static bool recvline(int fd, char *line, size_t len) {
        return true;
 }
 
-static bool sendline(int fd, char *format, ...) {
+bool sendline(int fd, char *format, ...) {
        static char buffer[4096];
        char *p = buffer;
        size_t blen = 0;
@@ -626,6 +631,13 @@ int main(int argc, char *argv[], char *envp[]) {
                return 0;
        }
 
+#ifdef HAVE_CURSES
+       if(!strcasecmp(argv[optind], "top")) {
+               top(fd);
+               return 0;
+       }
+#endif
+
        fprintf(stderr, "Unknown command `%s'.\n", argv[optind]);
        usage(true);
        
diff --git a/src/top.c b/src/top.c
new file mode 100644 (file)
index 0000000..0a3bae9
--- /dev/null
+++ b/src/top.c
@@ -0,0 +1,66 @@
+/*
+    top.c -- Show real-time statistics from a running tincd
+    Copyright (C) 2011 Guus Sliepen <guus@tinc-vpn.org>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License along
+    with this program; if not, write to the Free Software Foundation, Inc.,
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "system.h"
+
+#include <curses.h>
+
+#include "control_common.h"
+#include "tincctl.h"
+#include "top.h"
+
+void top(int fd) {
+       initscr();
+
+       timeout(1000);
+
+       do {
+               sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC);
+
+               erase();
+
+               char line[4096];
+               while(recvline(fd, line, sizeof line)) {
+                       char node[4096];
+                       int code;
+                       int req;
+                       uint64_t in_packets;
+                       uint64_t in_bytes;
+                       uint64_t out_packets;
+                       uint64_t out_bytes;
+
+                       int n = sscanf(line, "%d %d %s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, node, &in_packets, &in_bytes, &out_packets, &out_bytes);
+
+                       if(n == 2)
+                               break;
+
+                       if(n != 7) {
+                               fprintf(stderr, "Error receiving traffic information\n");
+                               return;
+                       }
+
+                       printw("%16s %8"PRIu64" %8"PRIu64" %8"PRIu64" %8"PRIu64"\n", node, in_packets, in_bytes, out_packets, out_bytes);
+               }
+
+               refresh();
+
+       } while(getch() == ERR);
+
+       endwin();
+}