info_elements: add some more PCAPs, 802.11 bugfixing
[netsniff-ng.git] / src / ifpps.c
blob9870d03f5168c18cb15f270357e89cf9022bc67e
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 long long unsigned int rx_bytes, rx_packets, rx_drops, rx_errors;
47 long long unsigned int rx_fifo, rx_frame, rx_multi;
48 long long unsigned int tx_bytes, tx_packets, tx_drops, tx_errors;
49 long long unsigned int tx_fifo, tx_colls, tx_carrier;
50 long long unsigned int 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 uint32_t irq_nr, procs_run, procs_iow, cswitch, forks;
54 struct wifi_stat wifi;
57 volatile sig_atomic_t sigint = 0;
59 static struct ifstat stats_old, stats_new, stats_delta;
61 static int stats_loop = 0;
63 static WINDOW *stats_screen = NULL;
65 static const char *short_options = "d:t:vhclp";
66 static const struct option long_options[] = {
67 {"dev", required_argument, NULL, 'd'},
68 {"interval", required_argument, NULL, 't'},
69 {"promisc", no_argument, NULL, 'p'},
70 {"csv", no_argument, NULL, 'c'},
71 {"loop", no_argument, NULL, 'l'},
72 {"version", no_argument, NULL, 'v'},
73 {"help", no_argument, NULL, 'h'},
74 {NULL, 0, NULL, 0}
77 static void signal_handler(int number)
79 switch (number) {
80 case SIGINT:
81 sigint = 1;
82 break;
83 case SIGHUP:
84 default:
85 break;
89 static inline char *snr_to_str(int level)
91 if (level > 40)
92 return "very good signal";
93 if (level > 25 && level <= 40)
94 return "good signal";
95 if (level > 15 && level <= 25)
96 return "poor signal";
97 if (level > 10 && level <= 15)
98 return "very poor signal";
99 if (level <= 10)
100 return "no signal";
102 return "unknown";
105 static inline int iswireless(const struct ifstat *stats)
107 return stats->wifi.bitrate > 0;
110 static void help(void)
112 printf("\nifpps %s, top-like kernel networking and system statistics\n",
113 VERSION_STRING);
114 puts("http://www.netsniff-ng.org\n\n"
115 "Usage: ifpps [options] || ifpps <netdev>\n"
116 "Options:\n"
117 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
118 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
119 " -p|--promisc Promiscuous mode\n"
120 " -c|--csv Output to terminal as CSV\n"
121 " E.g. post-processing with Gnuplot et al.\n"
122 " -l|--loop Continuous CSV output\n"
123 " -v|--version Print version\n"
124 " -h|--help Print this help\n\n"
125 "Examples:\n"
126 " ifpps eth0\n"
127 " ifpps -pd eth0\n"
128 " ifpps -lpcd wlan0 > plot.dat\n\n"
129 "Please report bugs to <bugs@netsniff-ng.org>\n"
130 "Copyright (C) 2009-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n"
131 "License: GNU GPL version 2.0\n"
132 "This is free software: you are free to change and redistribute it.\n"
133 "There is NO WARRANTY, to the extent permitted by law.\n");
134 die();
137 static void version(void)
139 printf("\nifpps %s, top-like kernel networking and system statistics\n",
140 VERSION_STRING);
141 puts("http://www.netsniff-ng.org\n\n"
142 "Please report bugs to <bugs@netsniff-ng.org>\n"
143 "Copyright (C) 2009-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n"
144 "License: GNU GPL version 2.0\n"
145 "This is free software: you are free to change and redistribute it.\n"
146 "There is NO WARRANTY, to the extent permitted by law.\n");
147 die();
150 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
152 int ret = -EINVAL;
153 char buff[256];
154 FILE *fp;
156 fp = fopen("/proc/net/dev", "r");
157 if (!fp)
158 panic("Cannot open /proc/net/dev!\n");
160 /* Omit table header from procfs file */
161 if (fgets(buff, sizeof(buff), fp));
162 if (fgets(buff, sizeof(buff), fp));
164 memset(buff, 0, sizeof(buff));
166 while (fgets(buff, sizeof(buff), fp) != NULL) {
167 buff[sizeof(buff) -1] = 0;
169 if (strstr(buff, ifname) == NULL)
170 continue;
172 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
173 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
174 &stats->rx_bytes, &stats->rx_packets,
175 &stats->rx_errors, &stats->rx_drops,
176 &stats->rx_fifo, &stats->rx_frame,
177 &stats->rx_multi, &stats->tx_bytes,
178 &stats->tx_packets, &stats->tx_errors,
179 &stats->tx_drops, &stats->tx_fifo,
180 &stats->tx_colls, &stats->tx_carrier) == 14) {
181 ret = 0;
182 break;
185 memset(buff, 0, sizeof(buff));
188 fclose(fp);
189 return ret;
192 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
194 int ret = -EINVAL, i, cpus, try = 0;
195 char *ptr, buff[256];
196 struct ethtool_drvinfo drvinf;
197 FILE *fp;
199 fp = fopen("/proc/interrupts", "r");
200 if (!fp)
201 panic("Cannot open /proc/interrupts!\n");
203 cpus = get_number_cpus();
204 bug_on(cpus > MAX_CPUS);
205 retry:
206 fseek(fp, 0, SEEK_SET);
207 memset(buff, 0, sizeof(buff));
209 while (fgets(buff, sizeof(buff), fp) != NULL) {
210 buff[sizeof(buff) - 1] = 0;
211 ptr = buff;
213 if (strstr(buff, ifname) == NULL)
214 continue;
216 stats->irq_nr = strtol(ptr, &ptr, 10);
217 bug_on(stats->irq_nr == 0);
219 if (ptr)
220 ptr++; /* Skip ':' char */
221 for (i = 0; i < cpus && ptr; ++i) {
222 stats->irqs[i] = strtol(ptr, &ptr, 10);
223 if (i == cpus - 1) {
224 ret = 0;
225 goto done;
229 memset(buff, 0, sizeof(buff));
232 /* We could get caught here in case of wireless devices which
233 * are not necessarily listed under 'wlan0' et al. in
234 * proc/interrupts. Therefore, we try once again with the
235 * ethtool driver name.
237 if (ret == -EINVAL && try == 0) {
238 memset(&drvinf, 0, sizeof(drvinf));
239 if (ethtool_drvinf(ifname, &drvinf) < 0)
240 goto done;
242 ifname = drvinf.driver;
243 try++;
245 goto retry;
247 done:
248 fclose(fp);
249 return ret;
252 static int stats_proc_softirqs(struct ifstat *stats)
254 int i, cpus;
255 char *ptr, buff[256];
256 FILE *fp;
257 enum {
258 softirqs_net_rx,
259 softirqs_net_tx,
260 softirqs_net_none,
261 } net_type = softirqs_net_none;
263 fp = fopen("/proc/softirqs", "r");
264 if (!fp)
265 panic("Cannot open /proc/softirqs!\n");
267 cpus = get_number_cpus();
268 bug_on(cpus > MAX_CPUS);
270 memset(buff, 0, sizeof(buff));
272 while (fgets(buff, sizeof(buff), fp) != NULL) {
273 buff[sizeof(buff) - 1] = 0;
275 if ((ptr = strstr(buff, "NET_TX:")))
276 net_type = softirqs_net_tx;
277 else if ((ptr = strstr(buff, "NET_RX:")))
278 net_type = softirqs_net_rx;
279 else
280 continue;
282 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
283 switch (net_type) {
284 case softirqs_net_tx:
285 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
286 break;
287 case softirqs_net_rx:
288 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
289 break;
290 default:
291 bug();
295 memset(buff, 0, sizeof(buff));
298 fclose(fp);
299 return 0;
302 static int stats_proc_memory(struct ifstat *stats)
304 char *ptr, buff[256];
305 FILE *fp;
307 fp = fopen("/proc/meminfo", "r");
308 if (!fp)
309 panic("Cannot open /proc/meminfo!\n");
311 memset(buff, 0, sizeof(buff));
313 while (fgets(buff, sizeof(buff), fp) != NULL) {
314 buff[sizeof(buff) - 1] = 0;
316 if ((ptr = strstr(buff, "MemTotal:"))) {
317 ptr += strlen("MemTotal:");
318 stats->mem_total = strtol(ptr, &ptr, 10);
319 } else if ((ptr = strstr(buff, "MemFree:"))) {
320 ptr += strlen("MemFree:");
321 stats->mem_free = strtol(ptr, &ptr, 10);
324 memset(buff, 0, sizeof(buff));
327 fclose(fp);
328 return 0;
331 static int stats_proc_system(struct ifstat *stats)
333 int cpu, cpus;
334 char *ptr, buff[256];
335 FILE *fp;
337 fp = fopen("/proc/stat", "r");
338 if (!fp)
339 panic("Cannot open /proc/stat!\n");
341 cpus = get_number_cpus();
342 bug_on(cpus > MAX_CPUS);
344 memset(buff, 0, sizeof(buff));
346 while (fgets(buff, sizeof(buff), fp) != NULL) {
347 buff[sizeof(buff) - 1] = 0;
349 if ((ptr = strstr(buff, "cpu"))) {
350 ptr += strlen("cpu");
351 if (isblank(*ptr))
352 goto next;
354 cpu = strtol(ptr, &ptr, 10);
355 bug_on(cpu > cpus);
357 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
358 &stats->cpu_user[cpu],
359 &stats->cpu_nice[cpu],
360 &stats->cpu_sys[cpu],
361 &stats->cpu_idle[cpu],
362 &stats->cpu_iow[cpu]) != 5)
363 goto next;
364 } else if ((ptr = strstr(buff, "ctxt"))) {
365 ptr += strlen("ctxt");
366 stats->cswitch = strtoul(ptr, &ptr, 10);
367 } else if ((ptr = strstr(buff, "processes"))) {
368 ptr += strlen("processes");
369 stats->forks = strtoul(ptr, &ptr, 10);
370 } else if ((ptr = strstr(buff, "procs_running"))) {
371 ptr += strlen("procs_running");
372 stats->procs_run = strtoul(ptr, &ptr, 10);
373 } else if ((ptr = strstr(buff, "procs_blocked"))) {
374 ptr += strlen("procs_blocked");
375 stats->procs_iow = strtoul(ptr, &ptr, 10);
377 next:
378 memset(buff, 0, sizeof(buff));
381 fclose(fp);
382 return 0;
385 static int stats_wireless(const char *ifname, struct ifstat *stats)
387 int ret;
388 struct iw_statistics ws;
390 ret = wireless_sigqual(ifname, &ws);
391 if (ret != 0) {
392 stats->wifi.bitrate = 0;
393 return -EINVAL;
396 stats->wifi.bitrate = wireless_bitrate(ifname);
398 stats->wifi.signal_level =
399 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
401 stats->wifi.link_qual = ws.qual.qual;
402 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
404 return ret;
407 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
408 #define DIFF(member) do { \
409 if (sizeof(diff->member) != sizeof(new->member) || \
410 sizeof(diff->member) != sizeof(old->member)) \
411 bug(); \
412 bug_on((new->member - old->member) > (new->member)); \
413 DIFF1(member); \
414 } while (0)
416 static void stats_diff(struct ifstat *old, struct ifstat *new,
417 struct ifstat *diff)
419 int cpus, i;
421 DIFF(rx_bytes);
422 DIFF(rx_packets);
423 DIFF(rx_drops);
424 DIFF(rx_errors);
425 DIFF(rx_fifo);
426 DIFF(rx_frame);
427 DIFF(rx_multi);
429 DIFF(tx_bytes);
430 DIFF(tx_bytes);
431 DIFF(tx_packets);
432 DIFF(tx_drops);
433 DIFF(tx_errors);
434 DIFF(tx_fifo);
435 DIFF(tx_colls);
436 DIFF(tx_carrier);
438 DIFF1(procs_run);
439 DIFF1(procs_iow);
441 DIFF1(wifi.signal_level);
442 DIFF1(wifi.link_qual);
444 DIFF1(cswitch);
445 DIFF1(forks);
447 cpus = get_number_cpus();
448 bug_on(cpus > MAX_CPUS);
450 for (i = 0; i < cpus; ++i) {
451 DIFF(irqs[i]);
452 DIFF(irqs_srx[i]);
453 DIFF(irqs_stx[i]);
455 DIFF1(cpu_user[i]);
456 DIFF1(cpu_nice[i]);
457 DIFF1(cpu_sys[i]);
458 DIFF1(cpu_idle[i]);
459 DIFF1(cpu_iow[i]);
463 static void stats_fetch(const char *ifname, struct ifstat *stats)
465 if (stats_proc_net_dev(ifname, stats) < 0)
466 panic("Cannot fetch device stats!\n");
467 if (stats_proc_softirqs(stats) < 0)
468 panic("Cannot fetch software interrupts!\n");
469 if (stats_proc_memory(stats) < 0)
470 panic("Cannot fetch memory stats!\n");
471 if (stats_proc_system(stats) < 0)
472 panic("Cannot fetch system stats!\n");
474 stats_proc_interrupts((char *) ifname, stats);
476 stats_wireless(ifname, stats);
479 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
481 memset(&stats_old, 0, sizeof(stats_old));
482 memset(&stats_new, 0, sizeof(stats_new));
483 memset(&stats_delta, 0, sizeof(stats_delta));
485 stats_fetch(ifname, &stats_old);
486 usleep(ms_interval * 1000);
487 stats_fetch(ifname, &stats_new);
489 stats_diff(&stats_old, &stats_new, &stats_delta);
492 static void screen_init(WINDOW **screen)
494 (*screen) = initscr();
496 raw();
497 noecho();
498 cbreak();
499 nodelay((*screen), TRUE);
501 keypad(stdscr, TRUE);
503 refresh();
504 wrefresh((*screen));
507 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
508 uint64_t ms_interval)
510 size_t len = 0;
511 char buff[64];
512 struct ethtool_drvinfo drvinf;
513 u32 rate = device_bitrate(ifname);
514 int link = ethtool_link(ifname);
516 memset(&drvinf, 0, sizeof(drvinf));
517 ethtool_drvinf(ifname, &drvinf);
519 memset(buff, 0, sizeof(buff));
520 if (rate)
521 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
522 if (link >= 0)
523 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
524 link == 0 ? "no" : "yes");
526 mvwprintw(screen, (*voff)++, 2,
527 "Kernel net/sys statistics for %s (%s%s), t=%lums"
528 " ",
529 ifname, drvinf.driver, buff, ms_interval);
532 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
533 int *voff)
535 attron(A_REVERSE);
537 mvwprintw(screen, (*voff)++, 0,
538 " RX: %16.3llf MiB/t "
539 "%10llu pkts/t "
540 "%10llu drops/t "
541 "%10llu errors/t ",
542 ((long double) rel->rx_bytes) / (1LLU << 20),
543 rel->rx_packets, rel->rx_drops, rel->rx_errors);
545 mvwprintw(screen, (*voff)++, 0,
546 " TX: %16.3llf MiB/t "
547 "%10llu pkts/t "
548 "%10llu drops/t "
549 "%10llu errors/t ",
550 ((long double) rel->tx_bytes) / (1LLU << 20),
551 rel->tx_packets, rel->tx_drops, rel->tx_errors);
553 attroff(A_REVERSE);
556 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
557 int *voff)
559 mvwprintw(screen, (*voff)++, 2,
560 "RX: %16.3llf MiB "
561 "%10llu pkts "
562 "%10llu drops "
563 "%10llu errors",
564 ((long double) abs->rx_bytes) / (1LLU << 20),
565 abs->rx_packets, abs->rx_drops, abs->rx_errors);
567 mvwprintw(screen, (*voff)++, 2,
568 "TX: %16.3llf MiB "
569 "%10llu pkts "
570 "%10llu drops "
571 "%10llu errors",
572 ((long double) abs->tx_bytes) / (1LLU << 20),
573 abs->tx_packets, abs->tx_drops, abs->tx_errors);
576 static void screen_sys_mem(WINDOW *screen, const struct ifstat *rel,
577 const struct ifstat *abs, int *voff)
579 mvwprintw(screen, (*voff)++, 2,
580 "SYS: %14u cs/t "
581 "%10.1lf%% mem "
582 "%13u running "
583 "%10u iowait",
584 rel->cswitch,
585 (100.0 * (abs->mem_total - abs->mem_free)) / abs->mem_total,
586 abs->procs_run, abs->procs_iow);
589 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
590 int cpus, int *voff)
592 int i;
593 uint64_t all;
595 for (i = 0; i < cpus; ++i) {
596 all = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i] +
597 rel->cpu_idle[i] + rel->cpu_iow[i];
599 mvwprintw(screen, (*voff)++, 2,
600 "CPU%d: %13.1lf%% usr/t "
601 "%9.1lf%% sys/t "
602 "%10.1lf%% idl/t "
603 "%11.1lf%% iow/t ", i,
604 100.0 * (rel->cpu_user[i] + rel->cpu_nice[i]) / all,
605 100.0 * rel->cpu_sys[i] / all,
606 100.0 * rel->cpu_idle[i] / all,
607 100.0 * rel->cpu_iow[i] / all);
611 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
612 int cpus, int *voff)
614 int i;
616 for (i = 0; i < cpus; ++i) {
617 mvwprintw(screen, (*voff)++, 2,
618 "CPU%d: %14llu irqs/t "
619 "%15llu soirq RX/t "
620 "%15llu soirq TX/t ", i,
621 rel->irqs[i],
622 rel->irqs_srx[i],
623 rel->irqs_stx[i]);
627 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
628 int cpus, int *voff)
630 int i;
632 for (i = 0; i < cpus; ++i) {
633 mvwprintw(screen, (*voff)++, 2,
634 "CPU%d: %14llu irqs", i,
635 abs->irqs[i]);
639 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
640 const struct ifstat *abs, int *voff)
642 if (iswireless(abs)) {
643 mvwprintw(screen, (*voff)++, 2,
644 "LinkQual: %7d/%d (%d/t) ",
645 abs->wifi.link_qual,
646 abs->wifi.link_qual_max,
647 rel->wifi.link_qual);
649 mvwprintw(screen, (*voff)++, 2,
650 "Signal: %8d dBm (%d dBm/t) ",
651 abs->wifi.signal_level,
652 rel->wifi.signal_level);
653 #if 0
654 mvwprintw(screen, (*voff)++, 2,
655 "Noise: %8d dBm (%d dBm/t) ",
656 abs->wifi.noise_level,
657 rel->wifi.noise_level);
658 mvwprintw(screen, (*voff)++, 2,
659 "SNR: %8d dBm (%s) ",
660 abs->wifi.signal_level - abs->wifi.noise_level,
661 snr_to_str(abs->wifi.signal_level - abs->wifi.noise_level));
662 #endif
666 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
667 const struct ifstat *abs, int *first, uint64_t ms_interval)
669 int cpus, voff = 1, cvoff = 2;
671 curs_set(0);
673 cpus = get_number_cpus();
674 bug_on(cpus > MAX_CPUS);
676 screen_header(screen, ifname, &voff, ms_interval);
678 voff++;
679 screen_net_dev_rel(screen, rel, &voff);
681 voff++;
682 screen_net_dev_abs(screen, abs, &voff);
684 voff++;
685 screen_sys_mem(screen, rel, abs, &voff);
687 voff++;
688 screen_percpu_states(screen, rel, cpus, &voff);
690 voff++;
691 screen_percpu_irqs_rel(screen, rel, cpus, &voff);
693 voff++;
694 screen_percpu_irqs_abs(screen, abs, cpus, &voff);
696 voff++;
697 screen_wireless(screen, rel, abs, &voff);
699 if (*first) {
700 mvwprintw(screen, cvoff, 2, "Collecting data ...");
701 *first = 0;
702 } else {
703 mvwprintw(screen, cvoff, 2, " ");
706 wrefresh(screen);
707 refresh();
710 static void screen_end(void)
712 endwin();
715 static int screen_main(const char *ifname, uint64_t ms_interval)
717 int first = 1, key;
719 screen_init(&stats_screen);
721 while (!sigint) {
722 key = getch();
723 if (key == 'q' || key == 0x1b /* esq */ || key == KEY_F(10))
724 break;
726 screen_update(stats_screen, ifname, &stats_delta, &stats_new,
727 &first, ms_interval);
729 stats_sample_generic(ifname, ms_interval);
732 screen_end();
734 return 0;
737 static void term_csv(const char *ifname, const struct ifstat *rel,
738 const struct ifstat *abs, uint64_t ms_interval)
740 int cpus, i;
742 printf("%ld ", time(0));
744 printf("%llu ", rel->rx_bytes);
745 printf("%llu ", rel->rx_packets);
746 printf("%llu ", rel->rx_drops);
747 printf("%llu ", rel->rx_errors);
749 printf("%llu ", abs->rx_bytes);
750 printf("%llu ", abs->rx_packets);
751 printf("%llu ", abs->rx_drops);
752 printf("%llu ", abs->rx_errors);
754 printf("%llu ", rel->tx_bytes);
755 printf("%llu ", rel->tx_packets);
756 printf("%llu ", rel->tx_drops);
757 printf("%llu ", rel->tx_errors);
759 printf("%llu ", abs->tx_bytes);
760 printf("%llu ", abs->tx_packets);
761 printf("%llu ", abs->tx_drops);
762 printf("%llu ", abs->tx_errors);
764 printf("%u ", rel->cswitch);
765 printf("%lu ", abs->mem_free);
766 printf("%lu ", abs->mem_total - abs->mem_free);
767 printf("%lu ", abs->mem_total);
768 printf("%u ", abs->procs_run);
769 printf("%u ", abs->procs_iow);
771 cpus = get_number_cpus();
772 bug_on(cpus > MAX_CPUS);
774 for (i = 0; i < cpus; ++i) {
775 printf("%lu ", rel->cpu_user[i]);
776 printf("%lu ", rel->cpu_nice[i]);
777 printf("%lu ", rel->cpu_sys[i]);
778 printf("%lu ", rel->cpu_idle[i]);
779 printf("%lu ", rel->cpu_iow[i]);
781 printf("%llu ", rel->irqs[i]);
782 printf("%llu ", abs->irqs[i]);
784 printf("%llu ", rel->irqs_srx[i]);
785 printf("%llu ", abs->irqs_srx[i]);
787 printf("%llu ", rel->irqs_stx[i]);
788 printf("%llu ", abs->irqs_stx[i]);
791 if (iswireless(abs)) {
792 printf("%u ", rel->wifi.link_qual);
793 printf("%u ", abs->wifi.link_qual);
794 printf("%u ", abs->wifi.link_qual_max);
796 printf("%d ", rel->wifi.signal_level);
797 printf("%d ", abs->wifi.signal_level);
798 #if 0
799 printf("%d ", rel->wifi.noise_level);
800 printf("%d ", abs->wifi.noise_level);
801 #endif
804 puts("");
805 fflush(stdout);
808 static void term_csv_header(const char *ifname, const struct ifstat *abs,
809 uint64_t ms_interval)
811 int cpus, i, j = 1;
813 printf("# gnuplot dump (#col:description)\n");
814 printf("# networking interface: %s\n", ifname);
815 printf("# sampling interval (t): %lu ms\n", ms_interval);
816 printf("# %d:unixtime ", j++);
818 printf("%d:rx-bytes-per-t ", j++);
819 printf("%d:rx-pkts-per-t ", j++);
820 printf("%d:rx-drops-per-t ", j++);
821 printf("%d:rx-errors-per-t ", j++);
823 printf("%d:rx-bytes ", j++);
824 printf("%d:rx-pkts ", j++);
825 printf("%d:rx-drops ", j++);
826 printf("%d:rx-errors ", j++);
828 printf("%d:tx-bytes-per-t ", j++);
829 printf("%d:tx-pkts-per-t ", j++);
830 printf("%d:tx-drops-per-t ", j++);
831 printf("%d:tx-errors-per-t ", j++);
833 printf("%d:tx-bytes ", j++);
834 printf("%d:tx-pkts ", j++);
835 printf("%d:tx-drops ", j++);
836 printf("%d:tx-errors ", j++);
838 printf("%d:context-switches-per-t ", j++);
839 printf("%d:mem-free ", j++);
840 printf("%d:mem-used ", j++);
841 printf("%d:mem-total ", j++);
842 printf("%d:procs-in-run ", j++);
843 printf("%d:procs-in-iow ", j++);
845 cpus = get_number_cpus();
846 bug_on(cpus > MAX_CPUS);
848 for (i = 0, j = 22; i < cpus; ++i) {
849 printf("%d:cpu%i-usr-per-t ", j++, i);
850 printf("%d:cpu%i-nice-per-t ", j++, i);
851 printf("%d:cpu%i-sys-per-t ", j++, i);
852 printf("%d:cpu%i-idle-per-t ", j++, i);
853 printf("%d:cpu%i-iow-per-t ", j++, i);
855 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
856 printf("%d:cpu%i-net-irqs ", j++, i);
858 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
859 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
860 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
861 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
864 if (iswireless(abs)) {
865 printf("%d:wifi-link-qual-per-t ", j++);
866 printf("%d:wifi-link-qual ", j++);
867 printf("%d:wifi-link-qual-max ", j++);
869 printf("%d:wifi-signal-dbm-per-t ", j++);
870 printf("%d:wifi-signal-dbm ", j++);
871 #if 0
872 printf("%d:wifi-noise-dbm-per-t ", j++);
873 printf("%d:wifi-noise-dbm ", j++);
874 #endif
877 puts("");
878 printf("# data:\n");
879 fflush(stdout);
882 static int term_main(const char *ifname, uint64_t ms_interval)
884 int first = 1;
886 do {
887 stats_sample_generic(ifname, ms_interval);
889 if (first) {
890 first = 0;
891 term_csv_header(ifname, &stats_new, ms_interval);
894 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
895 } while (stats_loop && !sigint);
897 return 0;
900 int main(int argc, char **argv)
902 short ifflags = 0;
903 int c, opt_index, ret, promisc = 0;
904 uint64_t interval = 1000;
905 char *ifname = NULL;
906 int (*func_main)(const char *ifname, uint64_t ms_interval) = screen_main;
908 while ((c = getopt_long(argc, argv, short_options, long_options,
909 &opt_index)) != EOF) {
910 switch (c) {
911 case 'h':
912 help();
913 break;
914 case 'v':
915 version();
916 break;
917 case 'd':
918 ifname = xstrndup(optarg, IFNAMSIZ);
919 break;
920 case 't':
921 interval = strtol(optarg, NULL, 10);
922 break;
923 case 'l':
924 stats_loop = 1;
925 break;
926 case 'p':
927 promisc = 1;
928 break;
929 case 'c':
930 func_main = term_main;
931 break;
932 case '?':
933 switch (optopt) {
934 case 'd':
935 case 't':
936 panic("Option -%c requires an argument!\n",
937 optopt);
938 default:
939 if (isprint(optopt))
940 whine("Unknown option character "
941 "`0x%X\'!\n", optopt);
942 die();
944 default:
945 break;
949 if (argc == 1)
950 help();
952 if (argc == 2)
953 ifname = xstrndup(argv[1], IFNAMSIZ);
954 if (ifname == NULL)
955 panic("No networking device given!\n");
957 if (!strncmp("lo", ifname, IFNAMSIZ))
958 panic("lo is not supported!\n");
959 if (device_mtu(ifname) == 0)
960 panic("This is no networking device!\n");
962 register_signal(SIGINT, signal_handler);
963 register_signal(SIGHUP, signal_handler);
965 if (promisc)
966 ifflags = enter_promiscuous_mode(ifname);
967 ret = func_main(ifname, interval);
968 if (promisc)
969 leave_promiscuous_mode(ifname, ifflags);
971 xfree(ifname);
972 return ret;