2 top.c -- Show real-time statistics from a running tincd
3 Copyright (C) 2011 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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.
24 #include "control_common.h"
30 typedef struct nodestats_t {
36 float in_packets_rate;
38 float out_packets_rate;
43 static const char *const sortname[] = {
53 static int sortmode = 0;
54 static bool cumulative = false;
56 static list_t node_list;
57 static struct timeval now, prev, diff;
58 static int delay = 1000;
59 static bool running = true;
60 static bool changed = true;
61 static const char *unit = "bytes";
62 static float scale = 1;
64 static void update(int fd) {
65 sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC);
66 gettimeofday(&now, NULL);
68 timersub(&now, &prev, &diff);
70 float interval = diff.tv_sec + diff.tv_usec * 1e-6;
81 for(list_node_t *i = node_list.head; i; i = i->next) {
82 nodestats_t *node = i->data;
86 while(recvline(fd, line, sizeof line)) {
87 int n = sscanf(line, "%d %d %s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, name, &in_packets, &in_bytes, &out_packets, &out_bytes);
94 fprintf(stderr, "Error receiving traffic information\n");
98 nodestats_t *found = NULL;
100 for(list_node_t *i = node_list.head; i; i = i->next) {
101 nodestats_t *node = i->data;
102 int result = strcmp(name, node->name);
109 found = xmalloc_and_zero(sizeof *found);
110 found->name = xstrdup(name);
111 fprintf(stderr, "Inserting %s before %s\n", found->name, node->name);
112 list_insert_before(&node_list, i, found);
119 found = xmalloc_and_zero(sizeof *found);
120 found->name = xstrdup(name);
121 list_insert_tail(&node_list, found);
126 found->in_packets_rate = (in_packets - found->in_packets) / interval;
127 found->in_bytes_rate = (in_bytes - found->in_bytes) / interval;
128 found->out_packets_rate = (out_packets - found->out_packets) / interval;
129 found->out_bytes_rate = (out_bytes - found->out_bytes) / interval;
130 found->in_packets = in_packets;
131 found->in_bytes = in_bytes;
132 found->out_packets = out_packets;
133 found->out_bytes = out_bytes;
137 static void redraw(void) {
140 mvprintw(0, 0, "Tinc %-16s Nodes: %4d Sort: %-8s %s", netname, node_list.count, sortname[sortmode], cumulative ? "Cumulative" : "Current");
142 mvprintw(2, 0, "Node IN pkts IN %s OUT pkts OUT %s", unit, unit);
143 chgat(-1, A_REVERSE, 0, NULL);
145 static nodestats_t **sorted = 0;
149 sorted = xrealloc(sorted, node_list.count * sizeof *sorted);
150 for(list_node_t *i = node_list.head; i; i = i->next)
151 sorted[n++] = i->data;
155 int cmpfloat(float a, float b) {
164 int cmpu64(uint64_t a, uint64_t b) {
173 int sortfunc(const void *a, const void *b) {
174 const nodestats_t *na = *(const nodestats_t **)a;
175 const nodestats_t *nb = *(const nodestats_t **)b;
179 return -cmpu64(na->in_packets, nb->in_packets);
181 return -cmpfloat(na->in_packets_rate, nb->in_packets_rate);
184 return -cmpu64(na->in_bytes, nb->in_bytes);
186 return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate);
189 return -cmpu64(na->out_packets, nb->out_packets);
191 return -cmpfloat(na->out_packets_rate, nb->out_packets_rate);
194 return -cmpu64(na->out_bytes, nb->out_bytes);
196 return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate);
199 return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets);
201 return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate);
204 return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes);
206 return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate);
208 return strcmp(na->name, nb->name);
212 qsort(sorted, n, sizeof *sorted, sortfunc);
214 for(int i = 0, row = 3; i < n; i++, row++) {
215 nodestats_t *node = sorted[i];
217 if(node->in_packets_rate || node->out_packets_rate)
225 mvprintw(row, 0, "%-16s %'10"PRIu64" %'10.0f %'10"PRIu64" %'10.0f",
226 node->name, node->in_packets, node->in_bytes * scale, node->out_packets, node->out_bytes * scale);
228 mvprintw(row, 0, "%-16s %'10.0f %'10.0f %'10.0f %'10.0f",
229 node->name, node->in_packets_rate, node->in_bytes_rate * scale, node->out_packets_rate, node->out_bytes_rate * scale);
249 float input = delay * 1e-3;
250 mvprintw(1, 0, "Change delay from %.1fs to: ", input);
259 cumulative = !cumulative;