ifpps: also allow F10 to exit screen
[netsniff-ng.git] / src / ifpps.c
blob5e2b4d8c3061660432b62dc833a0cb20bc322ea4
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009 - 2012 Daniel Borkmann.
5 * Subject to the GPL, version 2.
7 * A tiny tool to provide top-like reliable networking statistics.
8 * Why? Well, some time ago I used iptraf to display network traffic
9 * statistics. During that time and probably also today, they are
10 * using libpcap to collect statistics. Well, bad idea since this
11 * will give you false statistics on high I/O load. Therefore, ifpps
12 * reads out the 'real' kernel statistics, so things your NIC sees
13 * and not some userland library.
15 * He had all the injured air of a liar suspected when for once he
16 * has told the truth, or part of it.
18 * -- The Lord of the Rings, On Gollum,
19 * Chapter 'The Black Gate is Closed'.
22 #include <stdio.h>
23 #include <string.h>
24 #include <curses.h>
25 #include <getopt.h>
26 #include <ctype.h>
27 #include <sys/socket.h>
28 #include <signal.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <time.h>
33 #include "die.h"
34 #include "xmalloc.h"
35 #include "xutils.h"
36 #include "xio.h"
37 #include "built_in.h"
39 struct wifi_stat {
40 uint32_t bitrate;
41 int16_t link_qual, link_qual_max;
42 int signal_level /*, noise_level*/;
45 struct ifstat {
46 uint64_t rx_bytes, rx_packets, rx_drops, rx_errors;
47 uint64_t rx_fifo, rx_frame, rx_multi;
48 uint64_t tx_bytes, tx_packets, tx_drops, tx_errors;
49 uint64_t tx_fifo, tx_colls, tx_carrier;
50 uint64_t irqs[MAX_CPUS], irqs_srx[MAX_CPUS], irqs_stx[MAX_CPUS];
51 int64_t cpu_user[MAX_CPUS], cpu_nice[MAX_CPUS], cpu_sys[MAX_CPUS];
52 int64_t cpu_idle[MAX_CPUS], cpu_iow[MAX_CPUS], mem_free, mem_total;
53 int32_t cswitch, forks, procs_run, procs_iow;
54 uint32_t irq_nr;
55 struct wifi_stat wifi;
58 volatile sig_atomic_t sigint = 0;
60 static struct ifstat stats_old, stats_new, stats_delta;
62 static int stats_loop = 0;
64 static WINDOW *stats_screen = NULL;
66 static const char *short_options = "d:t:vhclp";
67 static const struct option long_options[] = {
68 {"dev", required_argument, NULL, 'd'},
69 {"interval", required_argument, NULL, 't'},
70 {"promisc", no_argument, NULL, 'p'},
71 {"csv", no_argument, NULL, 'c'},
72 {"loop", no_argument, NULL, 'l'},
73 {"version", no_argument, NULL, 'v'},
74 {"help", no_argument, NULL, 'h'},
75 {NULL, 0, NULL, 0}
78 static void signal_handler(int number)
80 switch (number) {
81 case SIGINT:
82 sigint = 1;
83 break;
84 case SIGHUP:
85 default:
86 break;
90 static inline char *snr_to_str(int level)
92 if (level > 40)
93 return "very good signal";
94 if (level > 25 && level <= 40)
95 return "good signal";
96 if (level > 15 && level <= 25)
97 return "poor signal";
98 if (level > 10 && level <= 15)
99 return "very poor signal";
100 if (level <= 10)
101 return "no signal";
103 return "unknown";
106 static inline int iswireless(const struct ifstat *stats)
108 return stats->wifi.bitrate > 0;
111 static void help(void)
113 printf("\nifpps %s, top-like kernel networking and system statistics\n",
114 VERSION_STRING);
115 puts("http://www.netsniff-ng.org\n\n"
116 "Usage: ifpps [options] || ifpps <netdev>\n"
117 "Options:\n"
118 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
119 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
120 " -p|--promisc Promiscuous mode\n"
121 " -c|--csv Output to terminal as CSV\n"
122 " E.g. post-processing with Gnuplot et al.\n"
123 " -l|--loop Continuous CSV output\n"
124 " -v|--version Print version\n"
125 " -h|--help Print this help\n\n"
126 "Examples:\n"
127 " ifpps eth0\n"
128 " ifpps -pd eth0\n"
129 " ifpps -lpcd wlan0 > plot.dat\n\n"
130 "Please report bugs to <bugs@netsniff-ng.org>\n"
131 "Copyright (C) 2009-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n"
132 "License: GNU GPL version 2.0\n"
133 "This is free software: you are free to change and redistribute it.\n"
134 "There is NO WARRANTY, to the extent permitted by law.\n");
135 die();
138 static void version(void)
140 printf("\nifpps %s, top-like kernel networking and system statistics\n",
141 VERSION_STRING);
142 puts("http://www.netsniff-ng.org\n\n"
143 "Please report bugs to <bugs@netsniff-ng.org>\n"
144 "Copyright (C) 2009-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n"
145 "License: GNU GPL version 2.0\n"
146 "This is free software: you are free to change and redistribute it.\n"
147 "There is NO WARRANTY, to the extent permitted by law.\n");
148 die();
151 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
153 int ret = -EINVAL;
154 char buff[256];
155 FILE *fp;
157 fp = fopen("/proc/net/dev", "r");
158 if (!fp)
159 panic("Cannot open /proc/net/dev!\n");
161 /* Omit table header from procfs file */
162 if (fgets(buff, sizeof(buff), fp));
163 if (fgets(buff, sizeof(buff), fp));
165 memset(buff, 0, sizeof(buff));
167 while (fgets(buff, sizeof(buff), fp) != NULL) {
168 buff[sizeof(buff) -1] = 0;
170 if (strstr(buff, ifname) == NULL)
171 continue;
173 if (sscanf(buff, "%*[a-z0-9 .-]:%lu%lu%lu%lu%lu%lu"
174 "%lu%*u%lu%lu%lu%lu%lu%lu%lu",
175 &stats->rx_bytes, &stats->rx_packets,
176 &stats->rx_errors, &stats->rx_drops,
177 &stats->rx_fifo, &stats->rx_frame,
178 &stats->rx_multi, &stats->tx_bytes,
179 &stats->tx_packets, &stats->tx_errors,
180 &stats->tx_drops, &stats->tx_fifo,
181 &stats->tx_colls, &stats->tx_carrier) == 14) {
182 ret = 0;
183 break;
186 memset(buff, 0, sizeof(buff));
189 fclose(fp);
190 return ret;
193 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
195 int ret = -EINVAL, i, cpus, try = 0;
196 char *ptr, buff[256];
197 struct ethtool_drvinfo drvinf;
198 FILE *fp;
200 fp = fopen("/proc/interrupts", "r");
201 if (!fp)
202 panic("Cannot open /proc/interrupts!\n");
204 cpus = get_number_cpus();
205 bug_on(cpus > MAX_CPUS);
206 retry:
207 fseek(fp, 0, SEEK_SET);
208 memset(buff, 0, sizeof(buff));
210 while (fgets(buff, sizeof(buff), fp) != NULL) {
211 buff[sizeof(buff) - 1] = 0;
212 ptr = buff;
214 if (strstr(buff, ifname) == NULL)
215 continue;
217 stats->irq_nr = strtol(ptr, &ptr, 10);
218 bug_on(stats->irq_nr == 0);
220 if (ptr)
221 ptr++; /* Skip ':' char */
222 for (i = 0; i < cpus && ptr; ++i) {
223 stats->irqs[i] = strtol(ptr, &ptr, 10);
224 if (i == cpus - 1) {
225 ret = 0;
226 goto done;
230 memset(buff, 0, sizeof(buff));
233 /* We could get caught here in case of wireless devices which
234 * are not necessarily listed under 'wlan0' et al. in
235 * proc/interrupts. Therefore, we try once again with the
236 * ethtool driver name.
238 if (ret == -EINVAL && try == 0) {
239 memset(&drvinf, 0, sizeof(drvinf));
240 if (ethtool_drvinf(ifname, &drvinf) < 0)
241 goto done;
243 ifname = drvinf.driver;
244 try++;
246 goto retry;
248 done:
249 fclose(fp);
250 return ret;
253 static int stats_proc_softirqs(struct ifstat *stats)
255 int i, cpus;
256 char *ptr, buff[256];
257 FILE *fp;
258 enum {
259 softirqs_net_rx,
260 softirqs_net_tx,
261 softirqs_net_none,
262 } net_type = softirqs_net_none;
264 fp = fopen("/proc/softirqs", "r");
265 if (!fp)
266 panic("Cannot open /proc/softirqs!\n");
268 cpus = get_number_cpus();
269 bug_on(cpus > MAX_CPUS);
271 memset(buff, 0, sizeof(buff));
273 while (fgets(buff, sizeof(buff), fp) != NULL) {
274 buff[sizeof(buff) - 1] = 0;
276 if ((ptr = strstr(buff, "NET_TX:")))
277 net_type = softirqs_net_tx;
278 else if ((ptr = strstr(buff, "NET_RX:")))
279 net_type = softirqs_net_rx;
280 else
281 continue;
283 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
284 switch (net_type) {
285 case softirqs_net_tx:
286 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
287 break;
288 case softirqs_net_rx:
289 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
290 break;
291 default:
292 bug();
296 memset(buff, 0, sizeof(buff));
299 fclose(fp);
300 return 0;
303 static int stats_proc_memory(struct ifstat *stats)
305 char *ptr, buff[256];
306 FILE *fp;
308 fp = fopen("/proc/meminfo", "r");
309 if (!fp)
310 panic("Cannot open /proc/meminfo!\n");
312 memset(buff, 0, sizeof(buff));
314 while (fgets(buff, sizeof(buff), fp) != NULL) {
315 buff[sizeof(buff) - 1] = 0;
317 if ((ptr = strstr(buff, "MemTotal:"))) {
318 ptr += strlen("MemTotal:");
319 stats->mem_total = strtol(ptr, &ptr, 10);
320 } else if ((ptr = strstr(buff, "MemFree:"))) {
321 ptr += strlen("MemFree:");
322 stats->mem_free = strtol(ptr, &ptr, 10);
325 memset(buff, 0, sizeof(buff));
328 fclose(fp);
329 return 0;
332 static int stats_proc_system(struct ifstat *stats)
334 int cpu, cpus;
335 char *ptr, buff[256];
336 FILE *fp;
338 fp = fopen("/proc/stat", "r");
339 if (!fp)
340 panic("Cannot open /proc/stat!\n");
342 cpus = get_number_cpus();
343 bug_on(cpus > MAX_CPUS);
345 memset(buff, 0, sizeof(buff));
347 while (fgets(buff, sizeof(buff), fp) != NULL) {
348 buff[sizeof(buff) - 1] = 0;
350 if ((ptr = strstr(buff, "cpu"))) {
351 ptr += strlen("cpu");
352 if (isblank(*ptr))
353 goto next;
355 cpu = strtol(ptr, &ptr, 10);
356 bug_on(cpu > cpus);
358 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
359 &stats->cpu_user[cpu],
360 &stats->cpu_nice[cpu],
361 &stats->cpu_sys[cpu],
362 &stats->cpu_idle[cpu],
363 &stats->cpu_iow[cpu]) != 5)
364 goto next;
365 } else if ((ptr = strstr(buff, "ctxt"))) {
366 ptr += strlen("ctxt");
367 stats->cswitch = strtol(ptr, &ptr, 10);
368 } else if ((ptr = strstr(buff, "processes"))) {
369 ptr += strlen("processes");
370 stats->forks = strtol(ptr, &ptr, 10);
371 } else if ((ptr = strstr(buff, "procs_running"))) {
372 ptr += strlen("procs_running");
373 stats->procs_run = strtol(ptr, &ptr, 10);
374 } else if ((ptr = strstr(buff, "procs_blocked"))) {
375 ptr += strlen("procs_blocked");
376 stats->procs_iow = strtol(ptr, &ptr, 10);
378 next:
379 memset(buff, 0, sizeof(buff));
382 fclose(fp);
383 return 0;
386 static int stats_wireless(const char *ifname, struct ifstat *stats)
388 int ret;
389 struct iw_statistics ws;
391 ret = wireless_sigqual(ifname, &ws);
392 if (ret != 0) {
393 stats->wifi.bitrate = 0;
394 return -EINVAL;
397 stats->wifi.bitrate = wireless_bitrate(ifname);
399 stats->wifi.signal_level =
400 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
402 stats->wifi.link_qual = ws.qual.qual;
403 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
405 return ret;
408 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
409 #define DIFF(member) do { \
410 if (sizeof(diff->member) != sizeof(new->member) || \
411 sizeof(diff->member) != sizeof(old->member)) \
412 bug(); \
413 bug_on((new->member - old->member) > (new->member)); \
414 DIFF1(member); \
415 } while (0)
417 static void stats_diff(struct ifstat *old, struct ifstat *new,
418 struct ifstat *diff)
420 int cpus, i;
422 DIFF(rx_bytes);
423 DIFF(rx_packets);
424 DIFF(rx_drops);
425 DIFF(rx_errors);
426 DIFF(rx_fifo);
427 DIFF(rx_frame);
428 DIFF(rx_multi);
430 DIFF(tx_bytes);
431 DIFF(tx_bytes);
432 DIFF(tx_packets);
433 DIFF(tx_drops);
434 DIFF(tx_errors);
435 DIFF(tx_fifo);
436 DIFF(tx_colls);
437 DIFF(tx_carrier);
439 DIFF1(procs_run);
440 DIFF1(procs_iow);
442 DIFF1(wifi.signal_level);
443 DIFF1(wifi.link_qual);
445 DIFF1(cswitch);
446 DIFF1(forks);
448 cpus = get_number_cpus();
449 bug_on(cpus > MAX_CPUS);
451 for (i = 0; i < cpus; ++i) {
452 DIFF(irqs[i]);
453 DIFF(irqs_srx[i]);
454 DIFF(irqs_stx[i]);
456 DIFF1(cpu_user[i]);
457 DIFF1(cpu_nice[i]);
458 DIFF1(cpu_sys[i]);
459 DIFF1(cpu_idle[i]);
460 DIFF1(cpu_iow[i]);
464 static void stats_fetch(const char *ifname, struct ifstat *stats)
466 if (stats_proc_net_dev(ifname, stats) < 0)
467 panic("Cannot fetch device stats!\n");
468 if (stats_proc_softirqs(stats) < 0)
469 panic("Cannot fetch software interrupts!\n");
470 if (stats_proc_memory(stats) < 0)
471 panic("Cannot fetch memory stats!\n");
472 if (stats_proc_system(stats) < 0)
473 panic("Cannot fetch system stats!\n");
475 stats_proc_interrupts((char *) ifname, stats);
477 stats_wireless(ifname, stats);
480 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
482 memset(&stats_old, 0, sizeof(stats_old));
483 memset(&stats_new, 0, sizeof(stats_new));
484 memset(&stats_delta, 0, sizeof(stats_delta));
486 stats_fetch(ifname, &stats_old);
487 usleep(ms_interval * 1000);
488 stats_fetch(ifname, &stats_new);
490 stats_diff(&stats_old, &stats_new, &stats_delta);
493 static void screen_init(WINDOW **screen)
495 (*screen) = initscr();
497 raw();
498 noecho();
499 cbreak();
500 nodelay((*screen), TRUE);
502 keypad(stdscr, TRUE);
504 refresh();
505 wrefresh((*screen));
508 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
509 uint64_t ms_interval)
511 size_t len = 0;
512 char buff[64];
513 struct ethtool_drvinfo drvinf;
514 u32 rate = device_bitrate(ifname);
515 int link = ethtool_link(ifname);
517 memset(&drvinf, 0, sizeof(drvinf));
518 ethtool_drvinf(ifname, &drvinf);
520 memset(buff, 0, sizeof(buff));
521 if (rate)
522 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
523 if (link >= 0)
524 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
525 link == 0 ? "no" : "yes");
527 mvwprintw(screen, (*voff)++, 2,
528 "Kernel net/sys statistics for %s (%s%s), t=%lums"
529 " ",
530 ifname, drvinf.driver, buff, ms_interval);
533 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
534 int *voff)
536 attron(A_REVERSE);
538 mvwprintw(screen, (*voff)++, 0,
539 " RX: %16.3lf MiB/t "
540 "%10lu pkts/t "
541 "%10lu drops/t "
542 "%10lu errors/t ",
543 1.0 * rel->rx_bytes / (1 << 20),
544 rel->rx_packets, rel->rx_drops, rel->rx_errors);
546 mvwprintw(screen, (*voff)++, 0,
547 " TX: %16.3lf MiB/t "
548 "%10lu pkts/t "
549 "%10lu drops/t "
550 "%10lu errors/t ",
551 1.0 * rel->tx_bytes / (1 << 20),
552 rel->tx_packets, rel->tx_drops, rel->tx_errors);
554 attroff(A_REVERSE);
557 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
558 int *voff)
560 mvwprintw(screen, (*voff)++, 2,
561 "RX: %16.3lf MiB "
562 "%10lu pkts "
563 "%10lu drops "
564 "%10lu errors",
565 1.0 * abs->rx_bytes / (1 << 20),
566 abs->rx_packets, abs->rx_drops, abs->rx_errors);
568 mvwprintw(screen, (*voff)++, 2,
569 "TX: %16.3lf MiB "
570 "%10lu pkts "
571 "%10lu drops "
572 "%10lu errors",
573 1.0 * abs->tx_bytes / (1 << 20),
574 abs->tx_packets, abs->tx_drops, abs->tx_errors);
577 static void screen_sys_mem(WINDOW *screen, const struct ifstat *rel,
578 const struct ifstat *abs, int *voff)
580 mvwprintw(screen, (*voff)++, 2,
581 "SYS: %14ld cs/t "
582 "%10.1lf%% mem "
583 "%13ld running "
584 "%10ld iowait",
585 rel->cswitch,
586 100.0 * (abs->mem_total - abs->mem_free) / abs->mem_total,
587 abs->procs_run, abs->procs_iow);
590 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
591 int cpus, int *voff)
593 int i;
594 uint64_t all;
596 for (i = 0; i < cpus; ++i) {
597 all = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i] +
598 rel->cpu_idle[i] + rel->cpu_iow[i];
600 mvwprintw(screen, (*voff)++, 2,
601 "CPU%d: %13.1lf%% usr/t "
602 "%9.1lf%% sys/t "
603 "%10.1lf%% idl/t "
604 "%11.1lf%% iow/t ", i,
605 100.0 * (rel->cpu_user[i] + rel->cpu_nice[i]) / all,
606 100.0 * rel->cpu_sys[i] / all,
607 100.0 * rel->cpu_idle[i] / all,
608 100.0 * rel->cpu_iow[i] / all);
612 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
613 int cpus, int *voff)
615 int i;
617 for (i = 0; i < cpus; ++i) {
618 mvwprintw(screen, (*voff)++, 2,
619 "CPU%d: %14ld irqs/t "
620 "%15ld soirq RX/t "
621 "%15ld soirq TX/t ", i,
622 rel->irqs[i],
623 rel->irqs_srx[i],
624 rel->irqs_stx[i]);
628 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
629 int cpus, int *voff)
631 int i;
633 for (i = 0; i < cpus; ++i) {
634 mvwprintw(screen, (*voff)++, 2,
635 "CPU%d: %14ld irqs", i,
636 abs->irqs[i]);
640 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
641 const struct ifstat *abs, int *voff)
643 if (iswireless(abs)) {
644 mvwprintw(screen, (*voff)++, 2,
645 "LinkQual: %7d/%d (%d/t) ",
646 abs->wifi.link_qual,
647 abs->wifi.link_qual_max,
648 rel->wifi.link_qual);
650 mvwprintw(screen, (*voff)++, 2,
651 "Signal: %8d dBm (%d dBm/t) ",
652 abs->wifi.signal_level,
653 rel->wifi.signal_level);
654 #if 0
655 mvwprintw(screen, (*voff)++, 2,
656 "Noise: %8d dBm (%d dBm/t) ",
657 abs->wifi.noise_level,
658 rel->wifi.noise_level);
659 mvwprintw(screen, (*voff)++, 2,
660 "SNR: %8d dBm (%s) ",
661 abs->wifi.signal_level - abs->wifi.noise_level,
662 snr_to_str(abs->wifi.signal_level - abs->wifi.noise_level));
663 #endif
667 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
668 const struct ifstat *abs, int *first, uint64_t ms_interval)
670 int cpus, voff = 1, cvoff = 2;
672 curs_set(0);
674 cpus = get_number_cpus();
675 bug_on(cpus > MAX_CPUS);
677 screen_header(screen, ifname, &voff, ms_interval);
679 voff++;
680 screen_net_dev_rel(screen, rel, &voff);
682 voff++;
683 screen_net_dev_abs(screen, abs, &voff);
685 voff++;
686 screen_sys_mem(screen, rel, abs, &voff);
688 voff++;
689 screen_percpu_states(screen, rel, cpus, &voff);
691 voff++;
692 screen_percpu_irqs_rel(screen, rel, cpus, &voff);
694 voff++;
695 screen_percpu_irqs_abs(screen, abs, cpus, &voff);
697 voff++;
698 screen_wireless(screen, rel, abs, &voff);
700 if (*first) {
701 mvwprintw(screen, cvoff, 2, "Collecting data ...");
702 *first = 0;
703 } else {
704 mvwprintw(screen, cvoff, 2, " ");
707 wrefresh(screen);
708 refresh();
711 static void screen_end(void)
713 endwin();
716 static int screen_main(const char *ifname, uint64_t ms_interval)
718 int first = 1, key;
720 screen_init(&stats_screen);
722 while (!sigint) {
723 key = getch();
724 if (key == 'q' || key == 0x1b /* esq */ || key == KEY_F(10))
725 break;
727 screen_update(stats_screen, ifname, &stats_delta, &stats_new,
728 &first, ms_interval);
730 stats_sample_generic(ifname, ms_interval);
733 screen_end();
735 return 0;
738 static void term_csv(const char *ifname, const struct ifstat *rel,
739 const struct ifstat *abs, uint64_t ms_interval)
741 int cpus, i;
743 printf("%ld ", time(0));
745 printf("%lu ", rel->rx_bytes);
746 printf("%lu ", rel->rx_packets);
747 printf("%lu ", rel->rx_drops);
748 printf("%lu ", rel->rx_errors);
750 printf("%lu ", abs->rx_bytes);
751 printf("%lu ", abs->rx_packets);
752 printf("%lu ", abs->rx_drops);
753 printf("%lu ", abs->rx_errors);
755 printf("%lu ", rel->tx_bytes);
756 printf("%lu ", rel->tx_packets);
757 printf("%lu ", rel->tx_drops);
758 printf("%lu ", rel->tx_errors);
760 printf("%lu ", abs->tx_bytes);
761 printf("%lu ", abs->tx_packets);
762 printf("%lu ", abs->tx_drops);
763 printf("%lu ", abs->tx_errors);
765 printf("%u ", rel->cswitch);
766 printf("%lu ", abs->mem_free);
767 printf("%lu ", abs->mem_total - abs->mem_free);
768 printf("%lu ", abs->mem_total);
769 printf("%u ", abs->procs_run);
770 printf("%u ", abs->procs_iow);
772 cpus = get_number_cpus();
773 bug_on(cpus > MAX_CPUS);
775 for (i = 0; i < cpus; ++i) {
776 printf("%lu ", rel->cpu_user[i]);
777 printf("%lu ", rel->cpu_nice[i]);
778 printf("%lu ", rel->cpu_sys[i]);
779 printf("%lu ", rel->cpu_idle[i]);
780 printf("%lu ", rel->cpu_iow[i]);
782 printf("%lu ", rel->irqs[i]);
783 printf("%lu ", abs->irqs[i]);
785 printf("%lu ", rel->irqs_srx[i]);
786 printf("%lu ", abs->irqs_srx[i]);
788 printf("%lu ", rel->irqs_stx[i]);
789 printf("%lu ", abs->irqs_stx[i]);
792 if (iswireless(abs)) {
793 printf("%u ", rel->wifi.link_qual);
794 printf("%u ", abs->wifi.link_qual);
795 printf("%u ", abs->wifi.link_qual_max);
797 printf("%d ", rel->wifi.signal_level);
798 printf("%d ", abs->wifi.signal_level);
799 #if 0
800 printf("%d ", rel->wifi.noise_level);
801 printf("%d ", abs->wifi.noise_level);
802 #endif
805 puts("");
806 fflush(stdout);
809 static void term_csv_header(const char *ifname, const struct ifstat *abs,
810 uint64_t ms_interval)
812 int cpus, i, j = 1;
814 printf("# gnuplot dump (#col:description)\n");
815 printf("# networking interface: %s\n", ifname);
816 printf("# sampling interval (t): %lu ms\n", ms_interval);
817 printf("# %d:unixtime ", j++);
819 printf("%d:rx-bytes-per-t ", j++);
820 printf("%d:rx-pkts-per-t ", j++);
821 printf("%d:rx-drops-per-t ", j++);
822 printf("%d:rx-errors-per-t ", j++);
824 printf("%d:rx-bytes ", j++);
825 printf("%d:rx-pkts ", j++);
826 printf("%d:rx-drops ", j++);
827 printf("%d:rx-errors ", j++);
829 printf("%d:tx-bytes-per-t ", j++);
830 printf("%d:tx-pkts-per-t ", j++);
831 printf("%d:tx-drops-per-t ", j++);
832 printf("%d:tx-errors-per-t ", j++);
834 printf("%d:tx-bytes ", j++);
835 printf("%d:tx-pkts ", j++);
836 printf("%d:tx-drops ", j++);
837 printf("%d:tx-errors ", j++);
839 printf("%d:context-switches-per-t ", j++);
840 printf("%d:mem-free ", j++);
841 printf("%d:mem-used ", j++);
842 printf("%d:mem-total ", j++);
843 printf("%d:procs-in-run ", j++);
844 printf("%d:procs-in-iow ", j++);
846 cpus = get_number_cpus();
847 bug_on(cpus > MAX_CPUS);
849 for (i = 0, j = 22; i < cpus; ++i) {
850 printf("%d:cpu%i-usr-per-t ", j++, i);
851 printf("%d:cpu%i-nice-per-t ", j++, i);
852 printf("%d:cpu%i-sys-per-t ", j++, i);
853 printf("%d:cpu%i-idle-per-t ", j++, i);
854 printf("%d:cpu%i-iow-per-t ", j++, i);
856 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
857 printf("%d:cpu%i-net-irqs ", j++, i);
859 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
860 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
861 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
862 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
865 if (iswireless(abs)) {
866 printf("%d:wifi-link-qual-per-t ", j++);
867 printf("%d:wifi-link-qual ", j++);
868 printf("%d:wifi-link-qual-max ", j++);
870 printf("%d:wifi-signal-dbm-per-t ", j++);
871 printf("%d:wifi-signal-dbm ", j++);
872 #if 0
873 printf("%d:wifi-noise-dbm-per-t ", j++);
874 printf("%d:wifi-noise-dbm ", j++);
875 #endif
878 puts("");
879 printf("# data:\n");
880 fflush(stdout);
883 static int term_main(const char *ifname, uint64_t ms_interval)
885 int first = 1;
887 do {
888 stats_sample_generic(ifname, ms_interval);
890 if (first) {
891 first = 0;
892 term_csv_header(ifname, &stats_new, ms_interval);
895 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
896 } while (stats_loop && !sigint);
898 return 0;
901 int main(int argc, char **argv)
903 short ifflags = 0;
904 int c, opt_index, ret, promisc = 0;
905 uint64_t interval = 1000;
906 char *ifname = NULL;
907 int (*func_main)(const char *ifname, uint64_t ms_interval) = screen_main;
909 while ((c = getopt_long(argc, argv, short_options, long_options,
910 &opt_index)) != EOF) {
911 switch (c) {
912 case 'h':
913 help();
914 break;
915 case 'v':
916 version();
917 break;
918 case 'd':
919 ifname = xstrndup(optarg, IFNAMSIZ);
920 break;
921 case 't':
922 interval = strtol(optarg, NULL, 10);
923 break;
924 case 'l':
925 stats_loop = 1;
926 break;
927 case 'p':
928 promisc = 1;
929 break;
930 case 'c':
931 func_main = term_main;
932 break;
933 case '?':
934 switch (optopt) {
935 case 'd':
936 case 't':
937 panic("Option -%c requires an argument!\n",
938 optopt);
939 default:
940 if (isprint(optopt))
941 whine("Unknown option character "
942 "`0x%X\'!\n", optopt);
943 die();
945 default:
946 break;
950 if (argc == 1)
951 help();
953 if (argc == 2)
954 ifname = xstrndup(argv[1], IFNAMSIZ);
955 if (ifname == NULL)
956 panic("No networking device given!\n");
958 if (!strncmp("lo", ifname, IFNAMSIZ))
959 panic("lo is not supported!\n");
960 if (device_mtu(ifname) == 0)
961 panic("This is no networking device!\n");
963 register_signal(SIGINT, signal_handler);
964 register_signal(SIGHUP, signal_handler);
966 if (promisc)
967 ifflags = enter_promiscuous_mode(ifname);
968 ret = func_main(ifname, interval);
969 if (promisc)
970 leave_promiscuous_mode(ifname, ifflags);
972 xfree(ifname);
973 return ret;