8d9ffb09fe9da5edaa24bca1024f2b3143522483
[tinc] / src / top.c
1 /*
2     top.c -- Show real-time statistics from a running tincd
3     Copyright (C) 2011-2013 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 #ifdef HAVE_CURSES
23
24 #undef KEY_EVENT  /* There are conflicting declarations for KEY_EVENT in Windows wincon.h and curses.h. */
25 #include <curses.h>
26
27 #include "control_common.h"
28 #include "list.h"
29 #include "names.h"
30 #include "tincctl.h"
31 #include "top.h"
32 #include "xalloc.h"
33
34 typedef struct nodestats_t {
35         char *name;
36         int i;
37         uint64_t in_packets;
38         uint64_t in_bytes;
39         uint64_t out_packets;
40         uint64_t out_bytes;
41         float in_packets_rate;
42         float in_bytes_rate;
43         float out_packets_rate;
44         float out_bytes_rate;
45         bool known;
46 } nodestats_t;
47
48 static const char *const sortname[] = {
49         "name",
50         "in pkts",
51         "in bytes",
52         "out pkts",
53         "out bytes",
54         "tot pkts",
55         "tot bytes",
56 };
57
58 static int sortmode = 0;
59 static bool cumulative = false;
60
61 static list_t node_list;
62 static struct timeval cur, prev, diff;
63 static int delay = 1000;
64 static bool changed = true;
65 static const char *bunit = "bytes";
66 static float bscale = 1;
67 static const char *punit = "pkts";
68 static float pscale = 1;
69
70 static bool update(int fd) {
71         if(!sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC))
72                 return false;
73
74         gettimeofday(&cur, NULL);
75
76         timersub(&cur, &prev, &diff);
77         prev = cur;
78         float interval = diff.tv_sec + diff.tv_usec * 1e-6;
79
80         char line[4096];
81         char name[4096];
82         int code;
83         int req;
84         uint64_t in_packets;
85         uint64_t in_bytes;
86         uint64_t out_packets;
87         uint64_t out_bytes;
88
89         for list_each(nodestats_t, ns, &node_list)
90                 ns->known = false;
91
92         while(recvline(fd, line, sizeof(line))) {
93                 int n = sscanf(line, "%d %d %4095s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, name, &in_packets, &in_bytes, &out_packets, &out_bytes);
94
95                 if(n == 2)
96                         return true;
97
98                 if(n != 7)
99                         return false;
100
101                 nodestats_t *found = NULL;
102
103                 for list_each(nodestats_t, ns, &node_list) {
104                         int result = strcmp(name, ns->name);
105                         if(result > 0) {
106                                 continue;
107                         } if(result == 0) {
108                                 found = ns;
109                                 break;
110                         } else {
111                                 found = xzalloc(sizeof(*found));
112                                 found->name = xstrdup(name);
113                                 list_insert_before(&node_list, node, found);
114                                 changed = true;
115                                 break;
116                         }
117                 }
118
119                 if(!found) {
120                         found = xzalloc(sizeof(*found));
121                         found->name = xstrdup(name);
122                         list_insert_tail(&node_list, found);
123                         changed = true;
124                 }
125
126                 found->known = true;
127                 found->in_packets_rate = (in_packets - found->in_packets) / interval;
128                 found->in_bytes_rate = (in_bytes - found->in_bytes) / interval;
129                 found->out_packets_rate = (out_packets - found->out_packets) / interval;
130                 found->out_bytes_rate = (out_bytes - found->out_bytes) / interval;
131                 found->in_packets = in_packets;
132                 found->in_bytes = in_bytes;
133                 found->out_packets = out_packets;
134                 found->out_bytes = out_bytes;
135         }
136
137         return false;
138 }
139
140 static int cmpfloat(float a, float b) {
141         if(a < b)
142                 return -1;
143         else if(a > b)
144                 return 1;
145         else
146                 return 0;
147 }
148
149 static int cmpu64(uint64_t a, uint64_t b) {
150         if(a < b)
151                 return -1;
152         else if(a > b)
153                 return 1;
154         else
155                 return 0;
156 }
157
158 static int sortfunc(const void *a, const void *b) {
159         const nodestats_t *na = *(const nodestats_t **)a;
160         const nodestats_t *nb = *(const nodestats_t **)b;
161         int result;
162
163         switch(sortmode) {
164                 case 1:
165                         if(cumulative)
166                                 result = -cmpu64(na->in_packets, nb->in_packets);
167                         else
168                                 result = -cmpfloat(na->in_packets_rate, nb->in_packets_rate);
169                         break;
170                 case 2:
171                         if(cumulative)
172                                 result = -cmpu64(na->in_bytes, nb->in_bytes);
173                         else
174                                 result = -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate);
175                         break;
176                 case 3:
177                         if(cumulative)
178                                 result = -cmpu64(na->out_packets, nb->out_packets);
179                         else
180                                 result = -cmpfloat(na->out_packets_rate, nb->out_packets_rate);
181                         break;
182                 case 4:
183                         if(cumulative)
184                                 result = -cmpu64(na->out_bytes, nb->out_bytes);
185                         else
186                                 result = -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate);
187                         break;
188                 case 5:
189                         if(cumulative)
190                                 result = -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets);
191                         else
192                                 result = -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate);
193                         break;
194                 case 6:
195                         if(cumulative)
196                                 result = -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes);
197                         else
198                                 result = -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate);
199                         break;
200                 default:
201                         result = strcmp(na->name, nb->name);
202                         break;
203         }
204
205         if(result)
206                 return result;
207         else
208                 return na->i - nb->i;
209 }
210
211 static void redraw(void) {
212         erase();
213
214         mvprintw(0, 0, "Tinc %-16s  Nodes: %4d  Sort: %-10s  %s", netname ?: "", node_list.count, sortname[sortmode], cumulative ? "Cumulative" : "Current");
215         attrset(A_REVERSE);
216         mvprintw(2, 0, "Node                IN %s   IN %s   OUT %s  OUT %s", punit, bunit, punit, bunit);
217         chgat(-1, A_REVERSE, 0, NULL);
218
219         static nodestats_t **sorted = 0;
220         static int n = 0;
221         if(changed) {
222                 n = 0;
223                 sorted = xrealloc(sorted, node_list.count * sizeof(*sorted));
224                 for list_each(nodestats_t, ns, &node_list)
225                         sorted[n++] = ns;
226                 changed = false;
227         }
228
229         for(int i = 0; i < n; i++)
230                 sorted[i]->i = i;
231
232         if(sorted)
233                 qsort(sorted, n, sizeof(*sorted), sortfunc);
234
235         for(int i = 0, row = 3; i < n; i++, row++) {
236                 nodestats_t *node = sorted[i];
237                 if(node->known)
238                         if(node->in_packets_rate || node->out_packets_rate)
239                                 attrset(A_BOLD);
240                         else
241                                 attrset(A_NORMAL);
242                 else
243                         attrset(A_DIM);
244
245                 if(cumulative)
246                         mvprintw(row, 0, "%-16s %10.0f %10.0f %10.0f %10.0f",
247                                         node->name, node->in_packets * pscale, node->in_bytes * bscale, node->out_packets * pscale, node->out_bytes * bscale);
248                 else
249                         mvprintw(row, 0, "%-16s %10.0f %10.0f %10.0f %10.0f",
250                                         node->name, node->in_packets_rate * pscale, node->in_bytes_rate * bscale, node->out_packets_rate * pscale, node->out_bytes_rate * bscale);
251         }
252
253         attrset(A_NORMAL);
254         move(1, 0);
255
256         refresh();
257 }
258
259 void top(int fd) {
260         initscr();
261         timeout(delay);
262         bool running = true;
263
264         while(running) {
265                 if(!update(fd))
266                         break;
267
268                 redraw();
269
270                 switch(getch()) {
271                         case 's': {
272                                 timeout(-1);
273                                 float input = delay * 1e-3;
274                                 mvprintw(1, 0, "Change delay from %.1fs to: ", input);
275                                 scanw("%f", &input);
276                                 if(input < 0.1)
277                                         input = 0.1;
278                                 delay = input * 1e3;
279                                 timeout(delay);
280                                 break;
281                         }
282                         case 'c':
283                                 cumulative = !cumulative;
284                                 break;
285                         case 'n':
286                                 sortmode = 0;
287                                 break;
288                         case 'i':
289                                 sortmode = 2;
290                                 break;
291                         case 'I':
292                                 sortmode = 1;
293                                 break;
294                         case 'o':
295                                 sortmode = 4;
296                                 break;
297                         case 'O':
298                                 sortmode = 3;
299                                 break;
300                         case 't':
301                                 sortmode = 6;
302                                 break;
303                         case 'T':
304                                 sortmode = 5;
305                                 break;
306                         case 'b':
307                                 bunit = "bytes";
308                                 bscale = 1;
309                                 punit = "pkts";
310                                 pscale = 1;
311                                 break;
312                         case 'k':
313                                 bunit = "kbyte";
314                                 bscale = 1e-3;
315                                 punit = "pkts";
316                                 pscale = 1;
317                                 break;
318                         case 'M':
319                                 bunit = "Mbyte";
320                                 bscale = 1e-6;
321                                 punit = "kpkt";
322                                 pscale = 1e-3;
323                                 break;
324                         case 'G':
325                                 bunit = "Gbyte";
326                                 bscale = 1e-9;
327                                 punit = "Mpkt";
328                                 pscale = 1e-6;
329                                 break;
330                         case 'q':
331                         case KEY_BREAK:
332                                 running = false;
333                                 break;
334                         default:
335                                 break;
336                 }
337         }
338
339         endwin();
340 }
341
342 #endif