Add a very primitive "top" command to tincctl.
[tinc] / src / top.c
1 /*
2     top.c -- Show real-time statistics from a running tincd
3     Copyright (C) 2011 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include <curses.h>
23
24 #include "control_common.h"
25 #include "tincctl.h"
26 #include "top.h"
27
28 void top(int fd) {
29         initscr();
30
31         timeout(1000);
32
33         do {
34                 sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC);
35
36                 erase();
37
38                 char line[4096];
39                 while(recvline(fd, line, sizeof line)) {
40                         char node[4096];
41                         int code;
42                         int req;
43                         uint64_t in_packets;
44                         uint64_t in_bytes;
45                         uint64_t out_packets;
46                         uint64_t out_bytes;
47
48                         int n = sscanf(line, "%d %d %s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, node, &in_packets, &in_bytes, &out_packets, &out_bytes);
49
50                         if(n == 2)
51                                 break;
52
53                         if(n != 7) {
54                                 fprintf(stderr, "Error receiving traffic information\n");
55                                 return;
56                         }
57
58                         printw("%16s %8"PRIu64" %8"PRIu64" %8"PRIu64" %8"PRIu64"\n", node, in_packets, in_bytes, out_packets, out_bytes);
59                 }
60
61                 refresh();
62
63         } while(getch() == ERR);
64
65         endwin();
66 }