Don't use AM_CONDITIONAL for CURSES.
[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         uint64_t in_packets;
35         uint64_t in_bytes;
36         uint64_t out_packets;
37         uint64_t out_bytes;
38         float in_packets_rate;
39         float in_bytes_rate;
40         float out_packets_rate;
41         float out_bytes_rate;
42         bool known;
43 } nodestats_t;
44
45 static const char *const sortname[] = {
46         "name",
47         "in pkts",
48         "in bytes",
49         "out pkts",
50         "out bytes",
51         "tot pkts",
52         "tot bytes",
53 };
54
55 static int sortmode = 0;
56 static bool cumulative = false;
57
58 static list_t node_list;
59 static struct timeval now, prev, diff;
60 static int delay = 1000;
61 static bool running = true;
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: %-8s  %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         int cmpfloat(float a, float b) {
161                 if(a < b)
162                         return -1;
163                 else if(a > b)
164                         return 1;
165                 else
166                         return 0;
167         }
168
169         int cmpu64(uint64_t a, uint64_t b) {
170                 if(a < b)
171                         return -1;
172                 else if(a > b)
173                         return 1;
174                 else
175                         return 0;
176         }
177
178         int sortfunc(const void *a, const void *b) {
179                 const nodestats_t *na = *(const nodestats_t **)a;
180                 const nodestats_t *nb = *(const nodestats_t **)b;
181                 switch(sortmode) {
182                         case 1:
183                                 if(cumulative)
184                                         return -cmpu64(na->in_packets, nb->in_packets);
185                                 else
186                                         return -cmpfloat(na->in_packets_rate, nb->in_packets_rate);
187                         case 2:
188                                 if(cumulative)
189                                         return -cmpu64(na->in_bytes, nb->in_bytes);
190                                 else
191                                         return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate);
192                         case 3:
193                                 if(cumulative)
194                                         return -cmpu64(na->out_packets, nb->out_packets);
195                                 else
196                                         return -cmpfloat(na->out_packets_rate, nb->out_packets_rate);
197                         case 4:
198                                 if(cumulative)
199                                         return -cmpu64(na->out_bytes, nb->out_bytes);
200                                 else
201                                         return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate);
202                         case 5:
203                                 if(cumulative)
204                                         return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets);
205                                 else
206                                         return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate);
207                         case 6:
208                                 if(cumulative)
209                                         return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes);
210                                 else
211                                         return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate);
212                         default:
213                                 return strcmp(na->name, nb->name);
214                 }
215         }
216
217         qsort(sorted, n, sizeof *sorted, sortfunc);
218
219         for(int i = 0, row = 3; i < n; i++, row++) {
220                 nodestats_t *node = sorted[i];
221                 if(node->known)
222                         if(node->in_packets_rate || node->out_packets_rate)
223                                 attrset(A_BOLD);
224                         else
225                                 attrset(A_NORMAL);
226                 else
227                         attrset(A_DIM);
228
229                 if(cumulative)
230                         mvprintw(row, 0, "%-16s %10"PRIu64" %10.0f %10"PRIu64" %10.0f",
231                                         node->name, node->in_packets, node->in_bytes * scale, node->out_packets, node->out_bytes * scale);
232                 else
233                         mvprintw(row, 0, "%-16s %10.0f %10.0f %10.0f %10.0f",
234                                         node->name, node->in_packets_rate, node->in_bytes_rate * scale, node->out_packets_rate, node->out_bytes_rate * scale);
235         }
236
237         attrset(A_NORMAL);
238         move(1, 0);
239
240         refresh();
241 }
242
243 void top(int fd) {
244         initscr();
245         timeout(delay);
246
247         while(running) {
248                 update(fd);
249                 redraw();
250
251                 switch(getch()) {
252                         case 's': {
253                                 timeout(-1);
254                                 float input = delay * 1e-3;
255                                 mvprintw(1, 0, "Change delay from %.1fs to: ", input);
256                                 scanw("%f", &input);
257                                 if(input < 0.1)
258                                         input = 0.1;
259                                 delay = input * 1e3;
260                                 timeout(delay);
261                                 break;
262                         }
263                         case 'c':
264                                   cumulative = !cumulative;
265                                   break;
266                         case 'n':
267                                   sortmode = 0;
268                                   break;
269                         case 'i':
270                                   sortmode = 2;
271                                   break;
272                         case 'I':
273                                   sortmode = 1;
274                                   break;
275                         case 'o':
276                                   sortmode = 4;
277                                   break;
278                         case 'O':
279                                   sortmode = 3;
280                                   break;
281                         case 't':
282                                   sortmode = 6;
283                                   break;
284                         case 'T':
285                                   sortmode = 5;
286                                   break;
287                         case 'b':
288                                   unit = "bytes";
289                                   scale = 1;
290                                   break;
291                         case 'k':
292                                   unit = "kbyte";
293                                   scale = 1e-3;
294                                   break;
295                         case 'M':
296                                   unit = "Mbyte";
297                                   scale = 1e-6;
298                                   break;
299                         case 'G':
300                                   unit = "Gbyte";
301                                   scale = 1e-9;
302                                   break;
303                         case 'q':
304                         case 27:
305                         case KEY_BREAK:
306                                 running = false;
307                                 break;
308                         default:
309                                 break;
310                 }
311         }
312
313         endwin();
314 }
315
316 #endif