man: netsniff-ng: add description paragraph and some options
[netsniff-ng.git] / ifpps.c
blobf52a10b9ad8258c18ea73a3d342a73ef3c04636b
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009 - 2013 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 */
7 #include <stdio.h>
8 #include <string.h>
9 #include <curses.h>
10 #include <getopt.h>
11 #include <ctype.h>
12 #include <sys/socket.h>
13 #include <sys/fsuid.h>
14 #include <signal.h>
15 #include <stdint.h>
16 #include <stdlib.h>
17 #include <time.h>
19 #include "die.h"
20 #include "xmalloc.h"
21 #include "xutils.h"
22 #include "xio.h"
23 #include "built_in.h"
25 struct wifi_stat {
26 uint32_t bitrate;
27 int16_t link_qual, link_qual_max;
28 int signal_level /*, noise_level*/;
31 struct ifstat {
32 long long unsigned int rx_bytes, rx_packets, rx_drops, rx_errors;
33 long long unsigned int rx_fifo, rx_frame, rx_multi;
34 long long unsigned int tx_bytes, tx_packets, tx_drops, tx_errors;
35 long long unsigned int tx_fifo, tx_colls, tx_carrier;
36 long long unsigned int irqs[MAX_CPUS], irqs_srx[MAX_CPUS], irqs_stx[MAX_CPUS];
37 int64_t cpu_user[MAX_CPUS], cpu_nice[MAX_CPUS], cpu_sys[MAX_CPUS];
38 int64_t cpu_idle[MAX_CPUS], cpu_iow[MAX_CPUS], mem_free, mem_total;
39 uint32_t irq_nr, procs_run, procs_iow, cswitch, forks;
40 struct wifi_stat wifi;
43 volatile sig_atomic_t sigint = 0;
45 static struct ifstat stats_old, stats_new, stats_delta;
47 static int stats_loop = 0;
49 static WINDOW *stats_screen = NULL;
51 static const char *short_options = "d:t:vhclp";
52 static const struct option long_options[] = {
53 {"dev", required_argument, NULL, 'd'},
54 {"interval", required_argument, NULL, 't'},
55 {"promisc", no_argument, NULL, 'p'},
56 {"csv", no_argument, NULL, 'c'},
57 {"loop", no_argument, NULL, 'l'},
58 {"version", no_argument, NULL, 'v'},
59 {"help", no_argument, NULL, 'h'},
60 {NULL, 0, NULL, 0}
63 static void signal_handler(int number)
65 switch (number) {
66 case SIGINT:
67 sigint = 1;
68 break;
69 case SIGHUP:
70 default:
71 break;
75 static inline char *snr_to_str(int level)
77 if (level > 40)
78 return "very good signal";
79 if (level > 25 && level <= 40)
80 return "good signal";
81 if (level > 15 && level <= 25)
82 return "poor signal";
83 if (level > 10 && level <= 15)
84 return "very poor signal";
85 if (level <= 10)
86 return "no signal";
88 return "unknown";
91 static inline int iswireless(const struct ifstat *stats)
93 return stats->wifi.bitrate > 0;
96 static void help(void)
98 printf("\nifpps %s, top-like kernel networking and system statistics\n",
99 VERSION_STRING);
100 puts("http://www.netsniff-ng.org\n\n"
101 "Usage: ifpps [options] || ifpps <netdev>\n"
102 "Options:\n"
103 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
104 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
105 " -p|--promisc Promiscuous mode\n"
106 " -c|--csv Output to terminal as Gnuplot-ready data\n"
107 " -l|--loop Continuous CSV output\n"
108 " -v|--version Print version\n"
109 " -h|--help Print this help\n\n"
110 "Examples:\n"
111 " ifpps eth0\n"
112 " ifpps -pd eth0\n"
113 " ifpps -lpcd wlan0 > plot.dat\n\n"
114 "Note:\n"
115 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
116 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
117 "Please report bugs to <bugs@netsniff-ng.org>\n"
118 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
119 "Swiss federal institute of technology (ETH Zurich)\n"
120 "License: GNU GPL version 2.0\n"
121 "This is free software: you are free to change and redistribute it.\n"
122 "There is NO WARRANTY, to the extent permitted by law.\n");
123 die();
126 static void version(void)
128 printf("\nifpps %s, top-like kernel networking and system statistics\n",
129 VERSION_STRING);
130 puts("http://www.netsniff-ng.org\n\n"
131 "Please report bugs to <bugs@netsniff-ng.org>\n"
132 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
133 "Swiss federal institute of technology (ETH Zurich)\n"
134 "License: GNU GPL version 2.0\n"
135 "This is free software: you are free to change and redistribute it.\n"
136 "There is NO WARRANTY, to the extent permitted by law.\n");
137 die();
140 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
142 int ret = -EINVAL;
143 char buff[256];
144 FILE *fp;
146 fp = fopen("/proc/net/dev", "r");
147 if (!fp)
148 panic("Cannot open /proc/net/dev!\n");
150 if (fgets(buff, sizeof(buff), fp)) { ; }
151 if (fgets(buff, sizeof(buff), fp)) { ; }
153 memset(buff, 0, sizeof(buff));
155 while (fgets(buff, sizeof(buff), fp) != NULL) {
156 buff[sizeof(buff) -1] = 0;
158 if (strstr(buff, ifname) == NULL)
159 continue;
161 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
162 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
163 &stats->rx_bytes, &stats->rx_packets,
164 &stats->rx_errors, &stats->rx_drops,
165 &stats->rx_fifo, &stats->rx_frame,
166 &stats->rx_multi, &stats->tx_bytes,
167 &stats->tx_packets, &stats->tx_errors,
168 &stats->tx_drops, &stats->tx_fifo,
169 &stats->tx_colls, &stats->tx_carrier) == 14) {
170 ret = 0;
171 break;
174 memset(buff, 0, sizeof(buff));
177 fclose(fp);
178 return ret;
181 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
183 int ret = -EINVAL, i, cpus, try = 0;
184 char *ptr, buff[256];
185 struct ethtool_drvinfo drvinf;
186 FILE *fp;
188 fp = fopen("/proc/interrupts", "r");
189 if (!fp)
190 panic("Cannot open /proc/interrupts!\n");
192 cpus = get_number_cpus();
193 bug_on(cpus > MAX_CPUS);
194 retry:
195 fseek(fp, 0, SEEK_SET);
196 memset(buff, 0, sizeof(buff));
198 while (fgets(buff, sizeof(buff), fp) != NULL) {
199 buff[sizeof(buff) - 1] = 0;
200 ptr = buff;
202 if (strstr(buff, ifname) == NULL)
203 continue;
205 stats->irq_nr = strtol(ptr, &ptr, 10);
206 bug_on(stats->irq_nr == 0);
208 if (ptr)
209 ptr++;
210 for (i = 0; i < cpus && ptr; ++i) {
211 stats->irqs[i] = strtol(ptr, &ptr, 10);
212 if (i == cpus - 1) {
213 ret = 0;
214 goto done;
218 memset(buff, 0, sizeof(buff));
221 if (ret == -EINVAL && try == 0) {
222 memset(&drvinf, 0, sizeof(drvinf));
223 if (ethtool_drvinf(ifname, &drvinf) < 0)
224 goto done;
226 ifname = drvinf.driver;
227 try++;
229 goto retry;
231 done:
232 fclose(fp);
233 return ret;
236 static int stats_proc_softirqs(struct ifstat *stats)
238 int i, cpus;
239 char *ptr, buff[256];
240 FILE *fp;
241 enum {
242 softirqs_net_rx,
243 softirqs_net_tx,
244 } net_type;
246 fp = fopen("/proc/softirqs", "r");
247 if (!fp)
248 panic("Cannot open /proc/softirqs!\n");
250 cpus = get_number_cpus();
251 bug_on(cpus > MAX_CPUS);
253 memset(buff, 0, sizeof(buff));
255 while (fgets(buff, sizeof(buff), fp) != NULL) {
256 buff[sizeof(buff) - 1] = 0;
258 if ((ptr = strstr(buff, "NET_TX:")))
259 net_type = softirqs_net_tx;
260 else if ((ptr = strstr(buff, "NET_RX:")))
261 net_type = softirqs_net_rx;
262 else
263 continue;
265 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
266 switch (net_type) {
267 case softirqs_net_tx:
268 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
269 break;
270 case softirqs_net_rx:
271 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
272 break;
276 memset(buff, 0, sizeof(buff));
279 fclose(fp);
280 return 0;
283 static int stats_proc_memory(struct ifstat *stats)
285 char *ptr, buff[256];
286 FILE *fp;
288 fp = fopen("/proc/meminfo", "r");
289 if (!fp)
290 panic("Cannot open /proc/meminfo!\n");
292 memset(buff, 0, sizeof(buff));
294 while (fgets(buff, sizeof(buff), fp) != NULL) {
295 buff[sizeof(buff) - 1] = 0;
297 if ((ptr = strstr(buff, "MemTotal:"))) {
298 ptr += strlen("MemTotal:");
299 stats->mem_total = strtol(ptr, &ptr, 10);
300 } else if ((ptr = strstr(buff, "MemFree:"))) {
301 ptr += strlen("MemFree:");
302 stats->mem_free = strtol(ptr, &ptr, 10);
305 memset(buff, 0, sizeof(buff));
308 fclose(fp);
309 return 0;
312 static int stats_proc_system(struct ifstat *stats)
314 int cpu, cpus;
315 char *ptr, buff[256];
316 FILE *fp;
318 fp = fopen("/proc/stat", "r");
319 if (!fp)
320 panic("Cannot open /proc/stat!\n");
322 cpus = get_number_cpus();
323 bug_on(cpus > MAX_CPUS);
325 memset(buff, 0, sizeof(buff));
327 while (fgets(buff, sizeof(buff), fp) != NULL) {
328 buff[sizeof(buff) - 1] = 0;
330 if ((ptr = strstr(buff, "cpu"))) {
331 ptr += strlen("cpu");
332 if (isblank(*ptr))
333 goto next;
335 cpu = strtol(ptr, &ptr, 10);
336 bug_on(cpu > cpus);
338 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
339 &stats->cpu_user[cpu],
340 &stats->cpu_nice[cpu],
341 &stats->cpu_sys[cpu],
342 &stats->cpu_idle[cpu],
343 &stats->cpu_iow[cpu]) != 5)
344 goto next;
345 } else if ((ptr = strstr(buff, "ctxt"))) {
346 ptr += strlen("ctxt");
347 stats->cswitch = strtoul(ptr, &ptr, 10);
348 } else if ((ptr = strstr(buff, "processes"))) {
349 ptr += strlen("processes");
350 stats->forks = strtoul(ptr, &ptr, 10);
351 } else if ((ptr = strstr(buff, "procs_running"))) {
352 ptr += strlen("procs_running");
353 stats->procs_run = strtoul(ptr, &ptr, 10);
354 } else if ((ptr = strstr(buff, "procs_blocked"))) {
355 ptr += strlen("procs_blocked");
356 stats->procs_iow = strtoul(ptr, &ptr, 10);
358 next:
359 memset(buff, 0, sizeof(buff));
362 fclose(fp);
363 return 0;
366 static int adjust_dbm_level(int in_dbm, int dbm_val)
368 if (!in_dbm)
369 return dbm_val;
371 return dbm_val - 0x100;
374 static int stats_wireless(const char *ifname, struct ifstat *stats)
376 int ret;
377 struct iw_statistics ws;
379 ret = wireless_sigqual(ifname, &ws);
380 if (ret != 0) {
381 stats->wifi.bitrate = 0;
382 return -EINVAL;
385 stats->wifi.bitrate = wireless_bitrate(ifname);
387 stats->wifi.signal_level =
388 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
390 stats->wifi.link_qual = ws.qual.qual;
391 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
393 return ret;
396 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
397 #define DIFF(member) do { \
398 if (sizeof(diff->member) != sizeof(new->member) || \
399 sizeof(diff->member) != sizeof(old->member)) \
400 bug(); \
401 bug_on((new->member - old->member) > (new->member)); \
402 DIFF1(member); \
403 } while (0)
405 static void stats_diff(struct ifstat *old, struct ifstat *new,
406 struct ifstat *diff)
408 int cpus, i;
410 DIFF(rx_bytes);
411 DIFF(rx_packets);
412 DIFF(rx_drops);
413 DIFF(rx_errors);
414 DIFF(rx_fifo);
415 DIFF(rx_frame);
416 DIFF(rx_multi);
418 DIFF(tx_bytes);
419 DIFF(tx_bytes);
420 DIFF(tx_packets);
421 DIFF(tx_drops);
422 DIFF(tx_errors);
423 DIFF(tx_fifo);
424 DIFF(tx_colls);
425 DIFF(tx_carrier);
427 DIFF1(procs_run);
428 DIFF1(procs_iow);
430 DIFF1(wifi.signal_level);
431 DIFF1(wifi.link_qual);
433 DIFF1(cswitch);
434 DIFF1(forks);
436 cpus = get_number_cpus();
437 bug_on(cpus > MAX_CPUS);
439 for (i = 0; i < cpus; ++i) {
440 DIFF(irqs[i]);
441 DIFF(irqs_srx[i]);
442 DIFF(irqs_stx[i]);
444 DIFF1(cpu_user[i]);
445 DIFF1(cpu_nice[i]);
446 DIFF1(cpu_sys[i]);
447 DIFF1(cpu_idle[i]);
448 DIFF1(cpu_iow[i]);
452 static void stats_fetch(const char *ifname, struct ifstat *stats)
454 if (stats_proc_net_dev(ifname, stats) < 0)
455 panic("Cannot fetch device stats!\n");
456 if (stats_proc_softirqs(stats) < 0)
457 panic("Cannot fetch software interrupts!\n");
458 if (stats_proc_memory(stats) < 0)
459 panic("Cannot fetch memory stats!\n");
460 if (stats_proc_system(stats) < 0)
461 panic("Cannot fetch system stats!\n");
463 stats_proc_interrupts((char *) ifname, stats);
465 stats_wireless(ifname, stats);
468 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
470 memset(&stats_old, 0, sizeof(stats_old));
471 memset(&stats_new, 0, sizeof(stats_new));
472 memset(&stats_delta, 0, sizeof(stats_delta));
474 stats_fetch(ifname, &stats_old);
475 usleep(ms_interval * 1000);
476 stats_fetch(ifname, &stats_new);
478 stats_diff(&stats_old, &stats_new, &stats_delta);
481 static void screen_init(WINDOW **screen)
483 (*screen) = initscr();
485 raw();
486 noecho();
487 cbreak();
488 nodelay((*screen), TRUE);
490 keypad(stdscr, TRUE);
492 refresh();
493 wrefresh((*screen));
496 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
497 uint64_t ms_interval)
499 size_t len = 0;
500 char buff[64];
501 struct ethtool_drvinfo drvinf;
502 u32 rate = device_bitrate(ifname);
503 int link = ethtool_link(ifname);
505 memset(&drvinf, 0, sizeof(drvinf));
506 ethtool_drvinf(ifname, &drvinf);
508 memset(buff, 0, sizeof(buff));
509 if (rate)
510 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
511 if (link >= 0)
512 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
513 link == 0 ? "no" : "yes");
515 mvwprintw(screen, (*voff)++, 2,
516 "Kernel net/sys statistics for %s (%s%s), t=%lums"
517 " ",
518 ifname, drvinf.driver, buff, ms_interval);
521 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
522 int *voff)
524 attron(A_REVERSE);
526 mvwprintw(screen, (*voff)++, 0,
527 " RX: %16.3llf MiB/t "
528 "%10llu pkts/t "
529 "%10llu drops/t "
530 "%10llu errors/t ",
531 ((long double) rel->rx_bytes) / (1LLU << 20),
532 rel->rx_packets, rel->rx_drops, rel->rx_errors);
534 mvwprintw(screen, (*voff)++, 0,
535 " TX: %16.3llf MiB/t "
536 "%10llu pkts/t "
537 "%10llu drops/t "
538 "%10llu errors/t ",
539 ((long double) rel->tx_bytes) / (1LLU << 20),
540 rel->tx_packets, rel->tx_drops, rel->tx_errors);
542 attroff(A_REVERSE);
545 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
546 int *voff)
548 mvwprintw(screen, (*voff)++, 2,
549 "RX: %16.3llf MiB "
550 "%10llu pkts "
551 "%10llu drops "
552 "%10llu errors",
553 ((long double) abs->rx_bytes) / (1LLU << 20),
554 abs->rx_packets, abs->rx_drops, abs->rx_errors);
556 mvwprintw(screen, (*voff)++, 2,
557 "TX: %16.3llf MiB "
558 "%10llu pkts "
559 "%10llu drops "
560 "%10llu errors",
561 ((long double) abs->tx_bytes) / (1LLU << 20),
562 abs->tx_packets, abs->tx_drops, abs->tx_errors);
565 static void screen_sys_mem(WINDOW *screen, const struct ifstat *rel,
566 const struct ifstat *abs, int *voff)
568 mvwprintw(screen, (*voff)++, 2,
569 "SYS: %14u cs/t "
570 "%10.1lf%% mem "
571 "%13u running "
572 "%10u iowait",
573 rel->cswitch,
574 (100.0 * (abs->mem_total - abs->mem_free)) / abs->mem_total,
575 abs->procs_run, abs->procs_iow);
578 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
579 int cpus, int *voff)
581 int i;
582 uint64_t all;
584 for (i = 0; i < cpus; ++i) {
585 all = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i] +
586 rel->cpu_idle[i] + rel->cpu_iow[i];
588 mvwprintw(screen, (*voff)++, 2,
589 "CPU%d: %13.1lf%% usr/t "
590 "%9.1lf%% sys/t "
591 "%10.1lf%% idl/t "
592 "%11.1lf%% iow/t ", i,
593 100.0 * (rel->cpu_user[i] + rel->cpu_nice[i]) / all,
594 100.0 * rel->cpu_sys[i] / all,
595 100.0 * rel->cpu_idle[i] / all,
596 100.0 * rel->cpu_iow[i] / all);
600 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
601 int cpus, int *voff)
603 int i;
605 for (i = 0; i < cpus; ++i) {
606 mvwprintw(screen, (*voff)++, 2,
607 "CPU%d: %14llu irqs/t "
608 "%15llu soirq RX/t "
609 "%15llu soirq TX/t ", i,
610 rel->irqs[i],
611 rel->irqs_srx[i],
612 rel->irqs_stx[i]);
616 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
617 int cpus, int *voff)
619 int i;
621 for (i = 0; i < cpus; ++i) {
622 mvwprintw(screen, (*voff)++, 2,
623 "CPU%d: %14llu irqs", i,
624 abs->irqs[i]);
628 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
629 const struct ifstat *abs, int *voff)
631 if (iswireless(abs)) {
632 mvwprintw(screen, (*voff)++, 2,
633 "LinkQual: %7d/%d (%d/t) ",
634 abs->wifi.link_qual,
635 abs->wifi.link_qual_max,
636 rel->wifi.link_qual);
638 mvwprintw(screen, (*voff)++, 2,
639 "Signal: %8d dBm (%d dBm/t) ",
640 abs->wifi.signal_level,
641 rel->wifi.signal_level);
645 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
646 const struct ifstat *abs, int *first, uint64_t ms_interval)
648 int cpus, voff = 1, cvoff = 2;
650 curs_set(0);
652 cpus = get_number_cpus();
653 bug_on(cpus > MAX_CPUS);
655 screen_header(screen, ifname, &voff, ms_interval);
657 voff++;
658 screen_net_dev_rel(screen, rel, &voff);
660 voff++;
661 screen_net_dev_abs(screen, abs, &voff);
663 voff++;
664 screen_sys_mem(screen, rel, abs, &voff);
666 voff++;
667 screen_percpu_states(screen, rel, cpus, &voff);
669 voff++;
670 screen_percpu_irqs_rel(screen, rel, cpus, &voff);
672 voff++;
673 screen_percpu_irqs_abs(screen, abs, cpus, &voff);
675 voff++;
676 screen_wireless(screen, rel, abs, &voff);
678 if (*first) {
679 mvwprintw(screen, cvoff, 2, "Collecting data ...");
680 *first = 0;
681 } else {
682 mvwprintw(screen, cvoff, 2, " ");
685 wrefresh(screen);
686 refresh();
689 static void screen_end(void)
691 endwin();
694 static int screen_main(const char *ifname, uint64_t ms_interval)
696 int first = 1, key;
698 screen_init(&stats_screen);
700 while (!sigint) {
701 key = getch();
702 if (key == 'q' || key == 0x1b || key == KEY_F(10))
703 break;
705 screen_update(stats_screen, ifname, &stats_delta, &stats_new,
706 &first, ms_interval);
708 stats_sample_generic(ifname, ms_interval);
711 screen_end();
713 return 0;
716 static void term_csv(const char *ifname, const struct ifstat *rel,
717 const struct ifstat *abs, uint64_t ms_interval)
719 int cpus, i;
721 printf("%ld ", time(0));
723 printf("%llu ", rel->rx_bytes);
724 printf("%llu ", rel->rx_packets);
725 printf("%llu ", rel->rx_drops);
726 printf("%llu ", rel->rx_errors);
728 printf("%llu ", abs->rx_bytes);
729 printf("%llu ", abs->rx_packets);
730 printf("%llu ", abs->rx_drops);
731 printf("%llu ", abs->rx_errors);
733 printf("%llu ", rel->tx_bytes);
734 printf("%llu ", rel->tx_packets);
735 printf("%llu ", rel->tx_drops);
736 printf("%llu ", rel->tx_errors);
738 printf("%llu ", abs->tx_bytes);
739 printf("%llu ", abs->tx_packets);
740 printf("%llu ", abs->tx_drops);
741 printf("%llu ", abs->tx_errors);
743 printf("%u ", rel->cswitch);
744 printf("%lu ", abs->mem_free);
745 printf("%lu ", abs->mem_total - abs->mem_free);
746 printf("%lu ", abs->mem_total);
747 printf("%u ", abs->procs_run);
748 printf("%u ", abs->procs_iow);
750 cpus = get_number_cpus();
751 bug_on(cpus > MAX_CPUS);
753 for (i = 0; i < cpus; ++i) {
754 printf("%lu ", rel->cpu_user[i]);
755 printf("%lu ", rel->cpu_nice[i]);
756 printf("%lu ", rel->cpu_sys[i]);
757 printf("%lu ", rel->cpu_idle[i]);
758 printf("%lu ", rel->cpu_iow[i]);
760 printf("%llu ", rel->irqs[i]);
761 printf("%llu ", abs->irqs[i]);
763 printf("%llu ", rel->irqs_srx[i]);
764 printf("%llu ", abs->irqs_srx[i]);
766 printf("%llu ", rel->irqs_stx[i]);
767 printf("%llu ", abs->irqs_stx[i]);
770 if (iswireless(abs)) {
771 printf("%u ", rel->wifi.link_qual);
772 printf("%u ", abs->wifi.link_qual);
773 printf("%u ", abs->wifi.link_qual_max);
775 printf("%d ", rel->wifi.signal_level);
776 printf("%d ", abs->wifi.signal_level);
779 puts("");
780 fflush(stdout);
783 static void term_csv_header(const char *ifname, const struct ifstat *abs,
784 uint64_t ms_interval)
786 int cpus, i, j = 1;
788 printf("# gnuplot dump (#col:description)\n");
789 printf("# networking interface: %s\n", ifname);
790 printf("# sampling interval (t): %lu ms\n", ms_interval);
791 printf("# %d:unixtime ", j++);
793 printf("%d:rx-bytes-per-t ", j++);
794 printf("%d:rx-pkts-per-t ", j++);
795 printf("%d:rx-drops-per-t ", j++);
796 printf("%d:rx-errors-per-t ", j++);
798 printf("%d:rx-bytes ", j++);
799 printf("%d:rx-pkts ", j++);
800 printf("%d:rx-drops ", j++);
801 printf("%d:rx-errors ", j++);
803 printf("%d:tx-bytes-per-t ", j++);
804 printf("%d:tx-pkts-per-t ", j++);
805 printf("%d:tx-drops-per-t ", j++);
806 printf("%d:tx-errors-per-t ", j++);
808 printf("%d:tx-bytes ", j++);
809 printf("%d:tx-pkts ", j++);
810 printf("%d:tx-drops ", j++);
811 printf("%d:tx-errors ", j++);
813 printf("%d:context-switches-per-t ", j++);
814 printf("%d:mem-free ", j++);
815 printf("%d:mem-used ", j++);
816 printf("%d:mem-total ", j++);
817 printf("%d:procs-in-run ", j++);
818 printf("%d:procs-in-iow ", j++);
820 cpus = get_number_cpus();
821 bug_on(cpus > MAX_CPUS);
823 for (i = 0, j = 22; i < cpus; ++i) {
824 printf("%d:cpu%i-usr-per-t ", j++, i);
825 printf("%d:cpu%i-nice-per-t ", j++, i);
826 printf("%d:cpu%i-sys-per-t ", j++, i);
827 printf("%d:cpu%i-idle-per-t ", j++, i);
828 printf("%d:cpu%i-iow-per-t ", j++, i);
830 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
831 printf("%d:cpu%i-net-irqs ", j++, i);
833 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
834 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
835 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
836 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
839 if (iswireless(abs)) {
840 printf("%d:wifi-link-qual-per-t ", j++);
841 printf("%d:wifi-link-qual ", j++);
842 printf("%d:wifi-link-qual-max ", j++);
844 printf("%d:wifi-signal-dbm-per-t ", j++);
845 printf("%d:wifi-signal-dbm ", j++);
848 puts("");
849 printf("# data:\n");
850 fflush(stdout);
853 static int term_main(const char *ifname, uint64_t ms_interval)
855 int first = 1;
857 do {
858 stats_sample_generic(ifname, ms_interval);
860 if (first) {
861 first = 0;
862 term_csv_header(ifname, &stats_new, ms_interval);
865 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
866 } while (stats_loop && !sigint);
868 return 0;
871 int main(int argc, char **argv)
873 short ifflags = 0;
874 int c, opt_index, ret, promisc = 0;
875 uint64_t interval = 1000;
876 char *ifname = NULL;
877 int (*func_main)(const char *ifname, uint64_t ms_interval) = screen_main;
879 setfsuid(getuid());
880 setfsgid(getgid());
882 while ((c = getopt_long(argc, argv, short_options, long_options,
883 &opt_index)) != EOF) {
884 switch (c) {
885 case 'h':
886 help();
887 break;
888 case 'v':
889 version();
890 break;
891 case 'd':
892 ifname = xstrndup(optarg, IFNAMSIZ);
893 break;
894 case 't':
895 interval = strtol(optarg, NULL, 10);
896 break;
897 case 'l':
898 stats_loop = 1;
899 break;
900 case 'p':
901 promisc = 1;
902 break;
903 case 'c':
904 func_main = term_main;
905 break;
906 case '?':
907 switch (optopt) {
908 case 'd':
909 case 't':
910 panic("Option -%c requires an argument!\n",
911 optopt);
912 default:
913 if (isprint(optopt))
914 printf("Unknown option character `0x%X\'!\n", optopt);
915 die();
917 default:
918 break;
922 if (argc == 1)
923 help();
925 if (argc == 2)
926 ifname = xstrndup(argv[1], IFNAMSIZ);
927 if (ifname == NULL)
928 panic("No networking device given!\n");
930 if (!strncmp("lo", ifname, IFNAMSIZ))
931 panic("lo is not supported!\n");
932 if (device_mtu(ifname) == 0)
933 panic("This is no networking device!\n");
935 register_signal(SIGINT, signal_handler);
936 register_signal(SIGHUP, signal_handler);
938 if (promisc)
939 ifflags = enter_promiscuous_mode(ifname);
940 ret = func_main(ifname, interval);
941 if (promisc)
942 leave_promiscuous_mode(ifname, ifflags);
944 xfree(ifname);
945 return ret;