docs: update email address
[netsniff-ng.git] / src / ifpps.c
blobce41d0f9ea678908d44251c002b6615dd6c89a77
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;
43 char driver_name[IFNAMSIZ];
46 struct ifstat {
47 uint32_t irq_nr;
48 uint64_t rx_bytes, rx_packets, rx_drops, rx_errors;
49 uint64_t rx_fifo, rx_frame, rx_multi;
50 uint64_t tx_bytes, tx_packets, tx_drops, tx_errors;
51 uint64_t tx_fifo, tx_colls, tx_carrier;
52 uint64_t irqs[MAX_CPUS], irqs_srx[MAX_CPUS], irqs_stx[MAX_CPUS];
53 int64_t cpu_user[MAX_CPUS], cpu_nice[MAX_CPUS], cpu_sys[MAX_CPUS];
54 int64_t cpu_idle[MAX_CPUS], cpu_iow[MAX_CPUS];
55 int32_t cswitch, forks, procs_run, procs_iow;
56 int64_t mem_free, mem_total;
57 struct wifi_stat wifi;
60 volatile sig_atomic_t sigint = 0;
62 static struct ifstat stats_old, stats_new, stats_delta;
64 static int stats_loop = 0;
66 static WINDOW *stats_screen = NULL;
68 static const char *short_options = "d:t:vhclp";
69 static const struct option long_options[] = {
70 {"dev", required_argument, NULL, 'd'},
71 {"interval", required_argument, NULL, 't'},
72 {"promisc", no_argument, NULL, 'p'},
73 {"csv", no_argument, NULL, 'c'},
74 {"loop", no_argument, NULL, 'l'},
75 {"version", no_argument, NULL, 'v'},
76 {"help", no_argument, NULL, 'h'},
77 {NULL, 0, NULL, 0}
80 static void signal_handler(int number)
82 switch (number) {
83 case SIGINT:
84 sigint = 1;
85 break;
86 case SIGHUP:
87 default:
88 break;
92 static inline char *snr_to_str(int level)
94 if (level > 40)
95 return "very good signal";
96 if (level > 25 && level <= 40)
97 return "good signal";
98 if (level > 15 && level <= 25)
99 return "poor signal";
100 if (level > 10 && level <= 15)
101 return "very poor signal";
102 if (level <= 10)
103 return "no signal";
105 return "unknown";
108 static inline int iswireless(const struct ifstat *stats)
110 return stats->wifi.bitrate > 0;
113 static void help(void)
115 printf("\nifpps %s, top-like kernel networking and system statistics\n",
116 VERSION_STRING);
117 puts("http://www.netsniff-ng.org\n\n"
118 "Usage: ifpps [options] || ifpps <netdev>\n"
119 "Options:\n"
120 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
121 " -t|--interval <time> Refresh time in ms (default 500 ms)\n"
122 " -p|--promisc Promiscuous mode\n"
123 " -c|--csv Output to terminal as CSV\n"
124 " E.g. post-processing with Gnuplot et al.\n"
125 " -l|--loop Continuous CSV output\n"
126 " -v|--version Print version\n"
127 " -h|--help Print this help\n\n"
128 "Examples:\n"
129 " ifpps eth0\n"
130 " ifpps -pd eth0\n"
131 " ifpps -lpcd wlan0 > plot.dat\n\n"
132 "Please report bugs to <bugs@netsniff-ng.org>\n"
133 "Copyright (C) 2009-2012 Daniel Borkmann <daniel@netsniff-ng.org>\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 void version(void)
142 printf("\nifpps %s, top-like kernel networking and system statistics\n",
143 VERSION_STRING);
144 puts("http://www.netsniff-ng.org\n\n"
145 "Please report bugs to <bugs@netsniff-ng.org>\n"
146 "Copyright (C) 2009-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n"
147 "License: GNU GPL version 2.0\n"
148 "This is free software: you are free to change and redistribute it.\n"
149 "There is NO WARRANTY, to the extent permitted by law.\n");
150 die();
153 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
155 int ret = -EINVAL;
156 char buff[256];
157 FILE *fp;
159 fp = fopen("/proc/net/dev", "r");
160 if (!fp)
161 panic("Cannot open /proc/net/dev!\n");
163 /* Omit table header from procfs file */
164 if (fgets(buff, sizeof(buff), fp));
165 if (fgets(buff, sizeof(buff), fp));
167 memset(buff, 0, sizeof(buff));
169 while (fgets(buff, sizeof(buff), fp) != NULL) {
170 buff[sizeof(buff) -1] = 0;
172 if (strstr(buff, ifname) == NULL)
173 continue;
175 if (sscanf(buff, "%*[a-z0-9 .-]:%lu%lu%lu%lu%lu%lu"
176 "%lu%*u%lu%lu%lu%lu%lu%lu%lu",
177 &stats->rx_bytes, &stats->rx_packets,
178 &stats->rx_errors, &stats->rx_drops,
179 &stats->rx_fifo, &stats->rx_frame,
180 &stats->rx_multi, &stats->tx_bytes,
181 &stats->tx_packets, &stats->tx_errors,
182 &stats->tx_drops, &stats->tx_fifo,
183 &stats->tx_colls, &stats->tx_carrier) == 14) {
184 ret = 0;
185 break;
188 memset(buff, 0, sizeof(buff));
191 fclose(fp);
192 return ret;
195 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
197 int ret = -EINVAL, i, cpus, try = 0;
198 char *ptr, buff[256];
199 struct ethtool_drvinfo drvinf;
200 FILE *fp;
202 fp = fopen("/proc/interrupts", "r");
203 if (!fp)
204 panic("Cannot open /proc/interrupts!\n");
206 cpus = get_number_cpus();
207 bug_on(cpus > MAX_CPUS);
208 retry:
209 fseek(fp, 0, SEEK_SET);
210 memset(buff, 0, sizeof(buff));
212 while (fgets(buff, sizeof(buff), fp) != NULL) {
213 buff[sizeof(buff) - 1] = 0;
214 ptr = buff;
216 if (strstr(buff, ifname) == NULL)
217 continue;
219 stats->irq_nr = strtol(ptr, &ptr, 10);
220 bug_on(stats->irq_nr == 0);
222 if (ptr)
223 ptr++; /* Skip ':' char */
224 for (i = 0; i < cpus && ptr; ++i) {
225 stats->irqs[i] = strtol(ptr, &ptr, 10);
226 if (i == cpus - 1) {
227 ret = 0;
228 goto done;
232 memset(buff, 0, sizeof(buff));
235 /* We could get caught here in case of wireless devices which
236 * are not necessarily listed under 'wlan0' et al. in
237 * proc/interrupts. Therefore, we try once again with the
238 * ethtool driver name.
240 if (ret == -EINVAL && try == 0) {
241 memset(&drvinf, 0, sizeof(drvinf));
242 if (ethtool_drvinf(ifname, &drvinf) < 0)
243 goto done;
245 ifname = drvinf.driver;
246 try++;
248 goto retry;
250 done:
251 fclose(fp);
252 return ret;
255 static int stats_proc_softirqs(struct ifstat *stats)
257 int i, cpus;
258 char *ptr, buff[256];
259 FILE *fp;
260 enum {
261 softirqs_net_rx,
262 softirqs_net_tx,
263 softirqs_net_none,
264 } net_type = softirqs_net_none;
266 fp = fopen("/proc/softirqs", "r");
267 if (!fp)
268 panic("Cannot open /proc/softirqs!\n");
270 cpus = get_number_cpus();
271 bug_on(cpus > MAX_CPUS);
273 memset(buff, 0, sizeof(buff));
275 while (fgets(buff, sizeof(buff), fp) != NULL) {
276 buff[sizeof(buff) - 1] = 0;
278 if ((ptr = strstr(buff, "NET_TX:")))
279 net_type = softirqs_net_tx;
280 else if ((ptr = strstr(buff, "NET_RX:")))
281 net_type = softirqs_net_rx;
282 else
283 continue;
285 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
286 switch (net_type) {
287 case softirqs_net_tx:
288 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
289 break;
290 case softirqs_net_rx:
291 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
292 break;
293 default:
294 bug();
298 memset(buff, 0, sizeof(buff));
301 fclose(fp);
302 return 0;
305 static int stats_proc_memory(struct ifstat *stats)
307 char *ptr, buff[256];
308 FILE *fp;
310 fp = fopen("/proc/meminfo", "r");
311 if (!fp)
312 panic("Cannot open /proc/meminfo!\n");
314 memset(buff, 0, sizeof(buff));
316 while (fgets(buff, sizeof(buff), fp) != NULL) {
317 buff[sizeof(buff) - 1] = 0;
319 if ((ptr = strstr(buff, "MemTotal:"))) {
320 ptr += strlen("MemTotal:");
321 stats->mem_total = strtol(ptr, &ptr, 10);
322 } else if ((ptr = strstr(buff, "MemFree:"))) {
323 ptr += strlen("MemFree:");
324 stats->mem_free = strtol(ptr, &ptr, 10);
327 memset(buff, 0, sizeof(buff));
330 fclose(fp);
331 return 0;
334 static int stats_proc_system(struct ifstat *stats)
336 int cpu, cpus;
337 char *ptr, buff[256];
338 FILE *fp;
340 fp = fopen("/proc/stat", "r");
341 if (!fp)
342 panic("Cannot open /proc/stat!\n");
344 cpus = get_number_cpus();
345 bug_on(cpus > MAX_CPUS);
347 memset(buff, 0, sizeof(buff));
349 while (fgets(buff, sizeof(buff), fp) != NULL) {
350 buff[sizeof(buff) - 1] = 0;
352 if ((ptr = strstr(buff, "cpu"))) {
353 ptr += strlen("cpu");
354 if (isblank(*ptr))
355 goto next;
357 cpu = strtol(ptr, &ptr, 10);
358 bug_on(cpu > cpus);
360 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
361 &stats->cpu_user[cpu],
362 &stats->cpu_nice[cpu],
363 &stats->cpu_sys[cpu],
364 &stats->cpu_idle[cpu],
365 &stats->cpu_iow[cpu]) != 5)
366 goto next;
367 } else if ((ptr = strstr(buff, "ctxt"))) {
368 ptr += strlen("ctxt");
369 stats->cswitch = strtol(ptr, &ptr, 10);
370 } else if ((ptr = strstr(buff, "processes"))) {
371 ptr += strlen("processes");
372 stats->forks = strtol(ptr, &ptr, 10);
373 } else if ((ptr = strstr(buff, "procs_running"))) {
374 ptr += strlen("procs_running");
375 stats->procs_run = strtol(ptr, &ptr, 10);
376 } else if ((ptr = strstr(buff, "procs_blocked"))) {
377 ptr += strlen("procs_blocked");
378 stats->procs_iow = strtol(ptr, &ptr, 10);
380 next:
381 memset(buff, 0, sizeof(buff));
384 fclose(fp);
385 return 0;
388 static int stats_wireless(const char *ifname, struct ifstat *stats)
390 int ret;
391 struct iw_statistics ws;
393 ret = wireless_sigqual(ifname, &ws);
394 if (ret != 0) {
395 stats->wifi.bitrate = 0;
396 return -EINVAL;
399 stats->wifi.bitrate = wireless_bitrate(ifname);
401 stats->wifi.signal_level =
402 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
403 stats->wifi.noise_level =
404 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.noise);
406 stats->wifi.link_qual = ws.qual.qual;
407 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
409 return ret;
412 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
413 #define DIFF(member) do { \
414 if (sizeof(diff->member) != sizeof(new->member) || \
415 sizeof(diff->member) != sizeof(old->member)) \
416 bug(); \
417 bug_on((new->member - old->member) > (new->member)); \
418 DIFF1(member); \
419 } while (0)
421 static void stats_diff(struct ifstat *old, struct ifstat *new,
422 struct ifstat *diff)
424 int cpus, i;
426 DIFF(rx_bytes);
427 DIFF(rx_packets);
428 DIFF(rx_drops);
429 DIFF(rx_errors);
430 DIFF(rx_fifo);
431 DIFF(rx_frame);
432 DIFF(rx_multi);
434 DIFF(tx_bytes);
435 DIFF(tx_bytes);
436 DIFF(tx_packets);
437 DIFF(tx_drops);
438 DIFF(tx_errors);
439 DIFF(tx_fifo);
440 DIFF(tx_colls);
441 DIFF(tx_carrier);
443 DIFF1(procs_run);
444 DIFF1(procs_iow);
446 DIFF1(wifi.signal_level);
447 DIFF1(wifi.noise_level);
449 DIFF1(wifi.link_qual);
451 DIFF1(cswitch);
452 DIFF1(forks);
454 cpus = get_number_cpus();
455 bug_on(cpus > MAX_CPUS);
457 for (i = 0; i < cpus; ++i) {
458 DIFF(irqs[i]);
459 DIFF(irqs_srx[i]);
460 DIFF(irqs_stx[i]);
462 DIFF1(cpu_user[i]);
463 DIFF1(cpu_nice[i]);
464 DIFF1(cpu_sys[i]);
465 DIFF1(cpu_idle[i]);
466 DIFF1(cpu_iow[i]);
470 static void stats_fetch(const char *ifname, struct ifstat *stats)
472 if (stats_proc_net_dev(ifname, stats) < 0)
473 panic("Cannot fetch device stats!\n");
474 if (stats_proc_softirqs(stats) < 0)
475 panic("Cannot fetch software interrupts!\n");
476 if (stats_proc_memory(stats) < 0)
477 panic("Cannot fetch memory stats!\n");
478 if (stats_proc_system(stats) < 0)
479 panic("Cannot fetch system stats!\n");
481 stats_proc_interrupts((char *) ifname, stats);
483 stats_wireless(ifname, stats);
486 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
488 memset(&stats_old, 0, sizeof(stats_old));
489 memset(&stats_new, 0, sizeof(stats_new));
490 memset(&stats_delta, 0, sizeof(stats_delta));
492 stats_fetch(ifname, &stats_old);
493 usleep(ms_interval * 1000);
494 stats_fetch(ifname, &stats_new);
496 stats_diff(&stats_old, &stats_new, &stats_delta);
499 static void screen_init(WINDOW **screen)
501 (*screen) = initscr();
502 noecho();
503 cbreak();
504 nodelay((*screen), TRUE);
505 refresh();
506 wrefresh((*screen));
509 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
510 int *voff)
512 (*voff) += 2;
514 attron(A_REVERSE);
516 mvwprintw(screen, (*voff)++, 0,
517 " RX: %16.3lf MiB/t "
518 "%10lu pkts/t "
519 "%10lu drops/t "
520 "%10lu errors/t ",
521 1.0 * rel->rx_bytes / (1 << 20),
522 rel->rx_packets, rel->rx_drops, rel->rx_errors);
524 mvwprintw(screen, (*voff)++, 0,
525 " TX: %16.3lf MiB/t "
526 "%10lu pkts/t "
527 "%10lu drops/t "
528 "%10lu errors/t ",
529 1.0 * rel->tx_bytes / (1 << 20),
530 rel->tx_packets, rel->tx_drops, rel->tx_errors);
532 attroff(A_REVERSE);
535 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
536 int *voff)
538 (*voff)++;
540 mvwprintw(screen, (*voff)++, 2,
541 "RX: %16.3lf MiB "
542 "%10lu pkts "
543 "%10lu drops "
544 "%10lu errors",
545 1.0 * abs->rx_bytes / (1 << 20),
546 abs->rx_packets, abs->rx_drops, abs->rx_errors);
548 mvwprintw(screen, (*voff)++, 2,
549 "TX: %16.3lf MiB "
550 "%10lu pkts "
551 "%10lu drops "
552 "%10lu errors",
553 1.0 * abs->tx_bytes / (1 << 20),
554 abs->tx_packets, abs->tx_drops, abs->tx_errors);
557 static void screen_sys_mem(WINDOW *screen, const struct ifstat *rel,
558 const struct ifstat *abs, int *voff)
560 (*voff)++;
562 mvwprintw(screen, (*voff)++, 2,
563 "SYS: %14ld cs/t "
564 "%10.1lf%% mem "
565 "%13ld running "
566 "%10ld iowait",
567 rel->cswitch,
568 100.0 * (abs->mem_total - abs->mem_free) / abs->mem_total,
569 abs->procs_run, abs->procs_iow);
572 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
573 int cpus, int *voff)
575 int i;
576 uint64_t all;
578 (*voff)++;
580 for (i = 0; i < cpus; ++i) {
581 all = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i] +
582 rel->cpu_idle[i] + rel->cpu_iow[i];
584 mvwprintw(screen, (*voff)++, 2,
585 "CPU%d: %13.1lf%% usr/t "
586 "%9.1lf%% sys/t "
587 "%10.1lf%% idl/t "
588 "%11.1lf%% iow/t ", i,
589 100.0 * (rel->cpu_user[i] + rel->cpu_nice[i]) / all,
590 100.0 * rel->cpu_sys[i] / all,
591 100.0 * rel->cpu_idle[i] / all,
592 100.0 * rel->cpu_iow[i] / all);
596 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
597 int cpus, int *voff)
599 int i;
601 for (i = 0, (*voff)++; i < cpus; ++i) {
602 mvwprintw(screen, (*voff)++, 2,
603 "CPU%d: %14ld irqs/t "
604 "%15ld soirq RX/t "
605 "%15ld soirq TX/t ", i,
606 rel->irqs[i],
607 rel->irqs_srx[i],
608 rel->irqs_stx[i]);
612 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
613 int cpus, int *voff)
615 int i;
617 for (i = 0, (*voff)++; i < cpus; ++i) {
618 mvwprintw(screen, (*voff)++, 2,
619 "CPU%d: %14ld irqs", i,
620 abs->irqs[i]);
624 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
625 const struct ifstat *abs, int *voff)
627 (*voff)++;
629 if (iswireless(abs)) {
630 mvwprintw(screen, (*voff)++, 2,
631 "LinkQual: %7d/%d (%d/t) ",
632 abs->wifi.link_qual,
633 abs->wifi.link_qual_max,
634 rel->wifi.link_qual);
636 mvwprintw(screen, (*voff)++, 2,
637 "Signal: %8d dBm (%d dBm/t) ",
638 abs->wifi.signal_level,
639 rel->wifi.signal_level);
641 mvwprintw(screen, (*voff)++, 2,
642 "Noise: %8d dBm (%d dBm/t) ",
643 abs->wifi.noise_level,
644 rel->wifi.noise_level);
646 mvwprintw(screen, (*voff)++, 2,
647 "SNR: %8d dBm (%s) ",
648 abs->wifi.signal_level - abs->wifi.noise_level,
649 snr_to_str(abs->wifi.signal_level - abs->wifi.noise_level));
651 (*voff)++;
655 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
656 const struct ifstat *abs, int *first, uint64_t ms_interval)
658 int cpus, voff = 1;
660 curs_set(0);
662 cpus = get_number_cpus();
663 bug_on(cpus > MAX_CPUS);
665 mvwprintw(screen, voff, 2, "Kernel net/sys statistics for %s, t=%lums",
666 ifname, ms_interval);
668 screen_net_dev_rel(screen, rel, &voff);
669 screen_net_dev_abs(screen, abs, &voff);
670 screen_sys_mem(screen, rel, abs, &voff);
671 screen_percpu_states(screen, rel, cpus, &voff);
672 screen_percpu_irqs_rel(screen, rel, cpus, &voff);
673 screen_percpu_irqs_abs(screen, abs, cpus, &voff);
674 screen_wireless(screen, rel, abs, &voff);
676 if (*first) {
677 mvwprintw(screen, 2, 2, "Collecting data ...");
678 *first = 0;
679 } else {
680 mvwprintw(screen, 2, 2, " ");
683 wrefresh(screen);
684 refresh();
687 static void screen_end(void)
689 endwin();
692 static int screen_main(const char *ifname, uint64_t ms_interval)
694 int first = 1, key;
696 screen_init(&stats_screen);
698 while (!sigint) {
699 key = getch();
700 if (key == 'q' || key == 0x1b /* esq */)
701 break;
703 screen_update(stats_screen, ifname, &stats_delta, &stats_new,
704 &first, ms_interval);
706 stats_sample_generic(ifname, ms_interval);
709 screen_end();
711 return 0;
714 static void term_csv(const char *ifname, const struct ifstat *rel,
715 const struct ifstat *abs, uint64_t ms_interval)
717 int cpus, i;
719 printf("%ld ", time(0));
721 printf("%lu ", rel->rx_bytes);
722 printf("%lu ", rel->rx_packets);
723 printf("%lu ", rel->rx_drops);
724 printf("%lu ", rel->rx_errors);
726 printf("%lu ", abs->rx_bytes);
727 printf("%lu ", abs->rx_packets);
728 printf("%lu ", abs->rx_drops);
729 printf("%lu ", abs->rx_errors);
731 printf("%lu ", rel->tx_bytes);
732 printf("%lu ", rel->tx_packets);
733 printf("%lu ", rel->tx_drops);
734 printf("%lu ", rel->tx_errors);
736 printf("%lu ", abs->tx_bytes);
737 printf("%lu ", abs->tx_packets);
738 printf("%lu ", abs->tx_drops);
739 printf("%lu ", abs->tx_errors);
741 printf("%u ", rel->cswitch);
742 printf("%lu ", abs->mem_free);
743 printf("%lu ", abs->mem_total - abs->mem_free);
744 printf("%lu ", abs->mem_total);
745 printf("%u ", abs->procs_run);
746 printf("%u ", abs->procs_iow);
748 cpus = get_number_cpus();
749 bug_on(cpus > MAX_CPUS);
751 for (i = 0; i < cpus; ++i) {
752 printf("%lu ", rel->cpu_user[i]);
753 printf("%lu ", rel->cpu_nice[i]);
754 printf("%lu ", rel->cpu_sys[i]);
755 printf("%lu ", rel->cpu_idle[i]);
756 printf("%lu ", rel->cpu_iow[i]);
758 printf("%lu ", rel->irqs[i]);
759 printf("%lu ", abs->irqs[i]);
761 printf("%lu ", rel->irqs_srx[i]);
762 printf("%lu ", abs->irqs_srx[i]);
764 printf("%lu ", rel->irqs_stx[i]);
765 printf("%lu ", abs->irqs_stx[i]);
768 if (iswireless(abs)) {
769 printf("%u ", rel->wifi.link_qual);
770 printf("%u ", abs->wifi.link_qual);
771 printf("%u ", abs->wifi.link_qual_max);
773 printf("%d ", rel->wifi.signal_level);
774 printf("%d ", abs->wifi.signal_level);
776 printf("%d ", rel->wifi.noise_level);
777 printf("%d ", abs->wifi.noise_level);
780 puts("");
781 fflush(stdout);
784 static void term_csv_header(const char *ifname, const struct ifstat *stats,
785 const struct ifstat *curr, uint64_t ms_interval)
787 int cpus, i, j = 1;
789 printf("# gnuplot dump (#col:description)\n");
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(curr)) {
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++);
847 printf("%d:wifi-noise-dbm-per-t ", j++);
848 printf("%d:wifi-noise-dbm ", j++);
851 puts("");
852 printf("# data:\n");
853 fflush(stdout);
856 static int term_main(const char *ifname, uint64_t ms_interval)
858 int first = 1;
860 do {
861 stats_sample_generic(ifname, ms_interval);
863 if (first) {
864 first = 0;
865 term_csv_header(ifname, &stats_delta, &stats_new,
866 ms_interval);
869 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
870 } while (stats_loop && !sigint);
872 return 0;
875 int main(int argc, char **argv)
877 short ifflags = 0;
878 int c, opt_index, ret, promisc = 0;
879 uint64_t interval = 1000;
880 char *ifname = NULL;
881 int (*func_main)(const char *ifname, uint64_t ms_interval) = screen_main;
883 while ((c = getopt_long(argc, argv, short_options, long_options,
884 &opt_index)) != EOF) {
885 switch (c) {
886 case 'h':
887 help();
888 break;
889 case 'v':
890 version();
891 break;
892 case 'd':
893 ifname = xstrndup(optarg, IFNAMSIZ);
894 break;
895 case 't':
896 interval = strtol(optarg, NULL, 10);
897 break;
898 case 'l':
899 stats_loop = 1;
900 break;
901 case 'p':
902 promisc = 1;
903 break;
904 case 'c':
905 func_main = term_main;
906 break;
907 case '?':
908 switch (optopt) {
909 case 'd':
910 case 't':
911 panic("Option -%c requires an argument!\n",
912 optopt);
913 default:
914 if (isprint(optopt))
915 whine("Unknown option character "
916 "`0x%X\'!\n", optopt);
917 die();
919 default:
920 break;
924 if (argc == 1)
925 help();
927 if (argc == 2)
928 ifname = xstrndup(argv[1], IFNAMSIZ);
929 if (ifname == NULL)
930 panic("No networking device given!\n");
932 if (!strncmp("lo", ifname, IFNAMSIZ))
933 panic("lo is not supported!\n");
934 if (device_mtu(ifname) == 0)
935 panic("This is no networking device!\n");
937 register_signal(SIGINT, signal_handler);
938 register_signal(SIGHUP, signal_handler);
940 if (promisc)
941 ifflags = enter_promiscuous_mode(ifname);
942 ret = func_main(ifname, interval);
943 if (promisc)
944 leave_promiscuous_mode(ifname, ifflags);
946 xfree(ifname);
947 return ret;