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 changed = true;
63 static const char *unit = "bytes";
64 static float scale = 1;
65
66 #ifndef timersub
67 #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)
68 #endif
69
70 static void update(int fd) {
71         sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC);
72         gettimeofday(&now, NULL);
73
74         timersub(&now, &prev, &diff);
75         prev = now;
76         float interval = diff.tv_sec + diff.tv_usec * 1e-6;
77
78         char line[4096];
79         char name[4096];
80         int code;
81         int req;
82         uint64_t in_packets;
83         uint64_t in_bytes;
84         uint64_t out_packets;
85         uint64_t out_bytes;
86
87         for(list_node_t *i = node_list.head; i; i = i->next) {
88                 nodestats_t *node = i->data;
89                 node->known = false;
90         }
91
92         while(recvline(fd, line, sizeof line)) {
93                 int n = sscanf(line, "%d %d %s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, name, &in_packets, &in_bytes, &out_packets, &out_bytes);
94
95                 if(n == 2)
96                         break;
97
98                 if(n != 7) {
99                         endwin();
100                         fprintf(stderr, "Error receiving traffic information\n");
101                         exit(1);
102                 }
103
104                 nodestats_t *found = NULL;
105
106                 for(list_node_t *i = node_list.head; i; i = i->next) {
107                         nodestats_t *node = i->data;
108                         int result = strcmp(name, node->name);
109                         if(result > 0) {
110                                 continue;
111                         } if(result == 0) {
112                                 found = node;
113                                 break;
114                         } else {
115                                 found = xmalloc_and_zero(sizeof *found);
116                                 found->name = xstrdup(name);
117                                 list_insert_before(&node_list, i, found);
118                                 changed = true;
119                                 break;
120                         }
121                 }
122
123                 if(!found) {
124                         found = xmalloc_and_zero(sizeof *found);
125                         found->name = xstrdup(name);
126                         list_insert_tail(&node_list, found);
127                         changed = true;
128                 }
129
130                 found->known = true;
131                 found->in_packets_rate = (in_packets - found->in_packets) / interval;
132                 found->in_bytes_rate = (in_bytes - found->in_bytes) / interval;
133                 found->out_packets_rate = (out_packets - found->out_packets) / interval;
134                 found->out_bytes_rate = (out_bytes - found->out_bytes) / interval;
135                 found->in_packets = in_packets;
136                 found->in_bytes = in_bytes;
137                 found->out_packets = out_packets;
138                 found->out_bytes = out_bytes;
139         }
140 }
141
142 static void redraw(void) {
143         erase();
144
145         mvprintw(0, 0, "Tinc %-16s  Nodes: %4d  Sort: %-10s  %s", netname ?: "", node_list.count, sortname[sortmode], cumulative ? "Cumulative" : "Current");
146         attrset(A_REVERSE);
147         mvprintw(2, 0, "Node                IN pkts   IN %s   OUT pkts  OUT %s", unit, unit);
148         chgat(-1, A_REVERSE, 0, NULL);
149
150         static nodestats_t **sorted = 0;
151         static int n = 0;
152         if(changed) {
153                 n = 0;
154                 sorted = xrealloc(sorted, node_list.count * sizeof *sorted);
155                 for(list_node_t *i = node_list.head; i; i = i->next)
156                         sorted[n++] = i->data;
157                 changed = false;
158         }
159
160         for(int i = 0; i < n; i++)
161                 sorted[i]->i = i;
162         
163         int cmpfloat(float a, float b) {
164                 if(a < b)
165                         return -1;
166                 else if(a > b)
167                         return 1;
168                 else
169                         return 0;
170         }
171
172         int cmpu64(uint64_t a, uint64_t b) {
173                 if(a < b)
174                         return -1;
175                 else if(a > b)
176                         return 1;
177                 else
178                         return 0;
179         }
180
181         int sortfunc(const void *a, const void *b) {
182                 const nodestats_t *na = *(const nodestats_t **)a;
183                 const nodestats_t *nb = *(const nodestats_t **)b;
184                 switch(sortmode) {
185                         case 1:
186                                 if(cumulative)
187                                         return -cmpu64(na->in_packets, nb->in_packets) ?: na->i - nb->i;
188                                 else
189                                         return -cmpfloat(na->in_packets_rate, nb->in_packets_rate) ?: na->i - nb->i;
190                         case 2:
191                                 if(cumulative)
192                                         return -cmpu64(na->in_bytes, nb->in_bytes) ?: na->i - nb->i;
193                                 else
194                                         return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate) ?: na->i - nb->i;
195                         case 3:
196                                 if(cumulative)
197                                         return -cmpu64(na->out_packets, nb->out_packets) ?: na->i - nb->i;
198                                 else
199                                         return -cmpfloat(na->out_packets_rate, nb->out_packets_rate) ?: na->i - nb->i;
200                         case 4:
201                                 if(cumulative)
202                                         return -cmpu64(na->out_bytes, nb->out_bytes) ?: na->i - nb->i;
203                                 else
204                                         return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate) ?: na->i - nb->i;
205                         case 5:
206                                 if(cumulative)
207                                         return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets) ?: na->i - nb->i;
208                                 else
209                                         return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate) ?: na->i - nb->i;
210                         case 6:
211                                 if(cumulative)
212                                         return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes) ?: na->i - nb->i;
213                                 else
214                                         return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate) ?: na->i - nb->i;
215                         default:
216                                 return strcmp(na->name, nb->name) ?: na->i - nb->i;
217                 }
218         }
219
220         qsort(sorted, n, sizeof *sorted, sortfunc);
221
222         for(int i = 0, row = 3; i < n; i++, row++) {
223                 nodestats_t *node = sorted[i];
224                 if(node->known)
225                         if(node->in_packets_rate || node->out_packets_rate)
226                                 attrset(A_BOLD);
227                         else
228                                 attrset(A_NORMAL);
229                 else
230                         attrset(A_DIM);
231
232                 if(cumulative)
233                         mvprintw(row, 0, "%-16s %10"PRIu64" %10.0f %10"PRIu64" %10.0f",
234                                         node->name, node->in_packets, node->in_bytes * scale, node->out_packets, node->out_bytes * scale);
235                 else
236                         mvprintw(row, 0, "%-16s %10.0f %10.0f %10.0f %10.0f",
237                                         node->name, node->in_packets_rate, node->in_bytes_rate * scale, node->out_packets_rate, node->out_bytes_rate * scale);
238         }
239
240         attrset(A_NORMAL);
241         move(1, 0);
242
243         refresh();
244 }
245
246 void top(int fd) {
247         initscr();
248         timeout(delay);
249         bool running = true;
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