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