xio: add comment to function
[netsniff-ng.git] / ifpps.c
blobba6c9224f95444d14801aaa228da647127e2c115
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009 - 2013 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 <sys/fsuid.h>
29 #include <signal.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <time.h>
34 #include "die.h"
35 #include "xmalloc.h"
36 #include "xutils.h"
37 #include "xio.h"
38 #include "built_in.h"
40 struct wifi_stat {
41 uint32_t bitrate;
42 int16_t link_qual, link_qual_max;
43 int signal_level /*, noise_level*/;
46 struct ifstat {
47 long long unsigned int rx_bytes, rx_packets, rx_drops, rx_errors;
48 long long unsigned int rx_fifo, rx_frame, rx_multi;
49 long long unsigned int tx_bytes, tx_packets, tx_drops, tx_errors;
50 long long unsigned int tx_fifo, tx_colls, tx_carrier;
51 long long unsigned int irqs[MAX_CPUS], irqs_srx[MAX_CPUS], irqs_stx[MAX_CPUS];
52 int64_t cpu_user[MAX_CPUS], cpu_nice[MAX_CPUS], cpu_sys[MAX_CPUS];
53 int64_t cpu_idle[MAX_CPUS], cpu_iow[MAX_CPUS], mem_free, mem_total;
54 uint32_t irq_nr, procs_run, procs_iow, cswitch, forks;
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 Gnuplot-ready data\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 "Note:\n"
130 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
131 " Thus, in those situations, it's could be to use a -t of 10sec.\n\n"
132 "Please report bugs to <bugs@netsniff-ng.org>\n"
133 "Copyright (C) 2009-2013 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-2013 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 if (fgets(buff, sizeof(buff), fp)) { ; }
164 if (fgets(buff, sizeof(buff), fp)) { ; }
166 memset(buff, 0, sizeof(buff));
168 while (fgets(buff, sizeof(buff), fp) != NULL) {
169 buff[sizeof(buff) -1] = 0;
171 if (strstr(buff, ifname) == NULL)
172 continue;
174 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
175 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
176 &stats->rx_bytes, &stats->rx_packets,
177 &stats->rx_errors, &stats->rx_drops,
178 &stats->rx_fifo, &stats->rx_frame,
179 &stats->rx_multi, &stats->tx_bytes,
180 &stats->tx_packets, &stats->tx_errors,
181 &stats->tx_drops, &stats->tx_fifo,
182 &stats->tx_colls, &stats->tx_carrier) == 14) {
183 ret = 0;
184 break;
187 memset(buff, 0, sizeof(buff));
190 fclose(fp);
191 return ret;
194 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
196 int ret = -EINVAL, i, cpus, try = 0;
197 char *ptr, buff[256];
198 struct ethtool_drvinfo drvinf;
199 FILE *fp;
201 fp = fopen("/proc/interrupts", "r");
202 if (!fp)
203 panic("Cannot open /proc/interrupts!\n");
205 cpus = get_number_cpus();
206 bug_on(cpus > MAX_CPUS);
207 retry:
208 fseek(fp, 0, SEEK_SET);
209 memset(buff, 0, sizeof(buff));
211 while (fgets(buff, sizeof(buff), fp) != NULL) {
212 buff[sizeof(buff) - 1] = 0;
213 ptr = buff;
215 if (strstr(buff, ifname) == NULL)
216 continue;
218 stats->irq_nr = strtol(ptr, &ptr, 10);
219 bug_on(stats->irq_nr == 0);
221 if (ptr)
222 ptr++;
223 for (i = 0; i < cpus && ptr; ++i) {
224 stats->irqs[i] = strtol(ptr, &ptr, 10);
225 if (i == cpus - 1) {
226 ret = 0;
227 goto done;
231 memset(buff, 0, sizeof(buff));
234 if (ret == -EINVAL && try == 0) {
235 memset(&drvinf, 0, sizeof(drvinf));
236 if (ethtool_drvinf(ifname, &drvinf) < 0)
237 goto done;
239 ifname = drvinf.driver;
240 try++;
242 goto retry;
244 done:
245 fclose(fp);
246 return ret;
249 static int stats_proc_softirqs(struct ifstat *stats)
251 int i, cpus;
252 char *ptr, buff[256];
253 FILE *fp;
254 enum {
255 softirqs_net_rx,
256 softirqs_net_tx,
257 softirqs_net_none,
258 } net_type = softirqs_net_none;
260 fp = fopen("/proc/softirqs", "r");
261 if (!fp)
262 panic("Cannot open /proc/softirqs!\n");
264 cpus = get_number_cpus();
265 bug_on(cpus > MAX_CPUS);
267 memset(buff, 0, sizeof(buff));
269 while (fgets(buff, sizeof(buff), fp) != NULL) {
270 buff[sizeof(buff) - 1] = 0;
272 if ((ptr = strstr(buff, "NET_TX:")))
273 net_type = softirqs_net_tx;
274 else if ((ptr = strstr(buff, "NET_RX:")))
275 net_type = softirqs_net_rx;
276 else
277 continue;
279 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
280 switch (net_type) {
281 case softirqs_net_tx:
282 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
283 break;
284 case softirqs_net_rx:
285 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
286 break;
287 default:
288 bug();
292 memset(buff, 0, sizeof(buff));
295 fclose(fp);
296 return 0;
299 static int stats_proc_memory(struct ifstat *stats)
301 char *ptr, buff[256];
302 FILE *fp;
304 fp = fopen("/proc/meminfo", "r");
305 if (!fp)
306 panic("Cannot open /proc/meminfo!\n");
308 memset(buff, 0, sizeof(buff));
310 while (fgets(buff, sizeof(buff), fp) != NULL) {
311 buff[sizeof(buff) - 1] = 0;
313 if ((ptr = strstr(buff, "MemTotal:"))) {
314 ptr += strlen("MemTotal:");
315 stats->mem_total = strtol(ptr, &ptr, 10);
316 } else if ((ptr = strstr(buff, "MemFree:"))) {
317 ptr += strlen("MemFree:");
318 stats->mem_free = strtol(ptr, &ptr, 10);
321 memset(buff, 0, sizeof(buff));
324 fclose(fp);
325 return 0;
328 static int stats_proc_system(struct ifstat *stats)
330 int cpu, cpus;
331 char *ptr, buff[256];
332 FILE *fp;
334 fp = fopen("/proc/stat", "r");
335 if (!fp)
336 panic("Cannot open /proc/stat!\n");
338 cpus = get_number_cpus();
339 bug_on(cpus > MAX_CPUS);
341 memset(buff, 0, sizeof(buff));
343 while (fgets(buff, sizeof(buff), fp) != NULL) {
344 buff[sizeof(buff) - 1] = 0;
346 if ((ptr = strstr(buff, "cpu"))) {
347 ptr += strlen("cpu");
348 if (isblank(*ptr))
349 goto next;
351 cpu = strtol(ptr, &ptr, 10);
352 bug_on(cpu > cpus);
354 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
355 &stats->cpu_user[cpu],
356 &stats->cpu_nice[cpu],
357 &stats->cpu_sys[cpu],
358 &stats->cpu_idle[cpu],
359 &stats->cpu_iow[cpu]) != 5)
360 goto next;
361 } else if ((ptr = strstr(buff, "ctxt"))) {
362 ptr += strlen("ctxt");
363 stats->cswitch = strtoul(ptr, &ptr, 10);
364 } else if ((ptr = strstr(buff, "processes"))) {
365 ptr += strlen("processes");
366 stats->forks = strtoul(ptr, &ptr, 10);
367 } else if ((ptr = strstr(buff, "procs_running"))) {
368 ptr += strlen("procs_running");
369 stats->procs_run = strtoul(ptr, &ptr, 10);
370 } else if ((ptr = strstr(buff, "procs_blocked"))) {
371 ptr += strlen("procs_blocked");
372 stats->procs_iow = strtoul(ptr, &ptr, 10);
374 next:
375 memset(buff, 0, sizeof(buff));
378 fclose(fp);
379 return 0;
382 static int stats_wireless(const char *ifname, struct ifstat *stats)
384 int ret;
385 struct iw_statistics ws;
387 ret = wireless_sigqual(ifname, &ws);
388 if (ret != 0) {
389 stats->wifi.bitrate = 0;
390 return -EINVAL;
393 stats->wifi.bitrate = wireless_bitrate(ifname);
395 stats->wifi.signal_level =
396 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
398 stats->wifi.link_qual = ws.qual.qual;
399 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
401 return ret;
404 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
405 #define DIFF(member) do { \
406 if (sizeof(diff->member) != sizeof(new->member) || \
407 sizeof(diff->member) != sizeof(old->member)) \
408 bug(); \
409 bug_on((new->member - old->member) > (new->member)); \
410 DIFF1(member); \
411 } while (0)
413 static void stats_diff(struct ifstat *old, struct ifstat *new,
414 struct ifstat *diff)
416 int cpus, i;
418 DIFF(rx_bytes);
419 DIFF(rx_packets);
420 DIFF(rx_drops);
421 DIFF(rx_errors);
422 DIFF(rx_fifo);
423 DIFF(rx_frame);
424 DIFF(rx_multi);
426 DIFF(tx_bytes);
427 DIFF(tx_bytes);
428 DIFF(tx_packets);
429 DIFF(tx_drops);
430 DIFF(tx_errors);
431 DIFF(tx_fifo);
432 DIFF(tx_colls);
433 DIFF(tx_carrier);
435 DIFF1(procs_run);
436 DIFF1(procs_iow);
438 DIFF1(wifi.signal_level);
439 DIFF1(wifi.link_qual);
441 DIFF1(cswitch);
442 DIFF1(forks);
444 cpus = get_number_cpus();
445 bug_on(cpus > MAX_CPUS);
447 for (i = 0; i < cpus; ++i) {
448 DIFF(irqs[i]);
449 DIFF(irqs_srx[i]);
450 DIFF(irqs_stx[i]);
452 DIFF1(cpu_user[i]);
453 DIFF1(cpu_nice[i]);
454 DIFF1(cpu_sys[i]);
455 DIFF1(cpu_idle[i]);
456 DIFF1(cpu_iow[i]);
460 static void stats_fetch(const char *ifname, struct ifstat *stats)
462 if (stats_proc_net_dev(ifname, stats) < 0)
463 panic("Cannot fetch device stats!\n");
464 if (stats_proc_softirqs(stats) < 0)
465 panic("Cannot fetch software interrupts!\n");
466 if (stats_proc_memory(stats) < 0)
467 panic("Cannot fetch memory stats!\n");
468 if (stats_proc_system(stats) < 0)
469 panic("Cannot fetch system stats!\n");
471 stats_proc_interrupts((char *) ifname, stats);
473 stats_wireless(ifname, stats);
476 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
478 memset(&stats_old, 0, sizeof(stats_old));
479 memset(&stats_new, 0, sizeof(stats_new));
480 memset(&stats_delta, 0, sizeof(stats_delta));
482 stats_fetch(ifname, &stats_old);
483 usleep(ms_interval * 1000);
484 stats_fetch(ifname, &stats_new);
486 stats_diff(&stats_old, &stats_new, &stats_delta);
489 static void screen_init(WINDOW **screen)
491 (*screen) = initscr();
493 raw();
494 noecho();
495 cbreak();
496 nodelay((*screen), TRUE);
498 keypad(stdscr, TRUE);
500 refresh();
501 wrefresh((*screen));
504 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
505 uint64_t ms_interval)
507 size_t len = 0;
508 char buff[64];
509 struct ethtool_drvinfo drvinf;
510 u32 rate = device_bitrate(ifname);
511 int link = ethtool_link(ifname);
513 memset(&drvinf, 0, sizeof(drvinf));
514 ethtool_drvinf(ifname, &drvinf);
516 memset(buff, 0, sizeof(buff));
517 if (rate)
518 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
519 if (link >= 0)
520 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
521 link == 0 ? "no" : "yes");
523 mvwprintw(screen, (*voff)++, 2,
524 "Kernel net/sys statistics for %s (%s%s), t=%lums"
525 " ",
526 ifname, drvinf.driver, buff, ms_interval);
529 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
530 int *voff)
532 attron(A_REVERSE);
534 mvwprintw(screen, (*voff)++, 0,
535 " RX: %16.3llf MiB/t "
536 "%10llu pkts/t "
537 "%10llu drops/t "
538 "%10llu errors/t ",
539 ((long double) rel->rx_bytes) / (1LLU << 20),
540 rel->rx_packets, rel->rx_drops, rel->rx_errors);
542 mvwprintw(screen, (*voff)++, 0,
543 " TX: %16.3llf MiB/t "
544 "%10llu pkts/t "
545 "%10llu drops/t "
546 "%10llu errors/t ",
547 ((long double) rel->tx_bytes) / (1LLU << 20),
548 rel->tx_packets, rel->tx_drops, rel->tx_errors);
550 attroff(A_REVERSE);
553 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
554 int *voff)
556 mvwprintw(screen, (*voff)++, 2,
557 "RX: %16.3llf MiB "
558 "%10llu pkts "
559 "%10llu drops "
560 "%10llu errors",
561 ((long double) abs->rx_bytes) / (1LLU << 20),
562 abs->rx_packets, abs->rx_drops, abs->rx_errors);
564 mvwprintw(screen, (*voff)++, 2,
565 "TX: %16.3llf MiB "
566 "%10llu pkts "
567 "%10llu drops "
568 "%10llu errors",
569 ((long double) abs->tx_bytes) / (1LLU << 20),
570 abs->tx_packets, abs->tx_drops, abs->tx_errors);
573 static void screen_sys_mem(WINDOW *screen, const struct ifstat *rel,
574 const struct ifstat *abs, int *voff)
576 mvwprintw(screen, (*voff)++, 2,
577 "SYS: %14u cs/t "
578 "%10.1lf%% mem "
579 "%13u running "
580 "%10u iowait",
581 rel->cswitch,
582 (100.0 * (abs->mem_total - abs->mem_free)) / abs->mem_total,
583 abs->procs_run, abs->procs_iow);
586 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
587 int cpus, int *voff)
589 int i;
590 uint64_t all;
592 for (i = 0; i < cpus; ++i) {
593 all = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i] +
594 rel->cpu_idle[i] + rel->cpu_iow[i];
596 mvwprintw(screen, (*voff)++, 2,
597 "CPU%d: %13.1lf%% usr/t "
598 "%9.1lf%% sys/t "
599 "%10.1lf%% idl/t "
600 "%11.1lf%% iow/t ", i,
601 100.0 * (rel->cpu_user[i] + rel->cpu_nice[i]) / all,
602 100.0 * rel->cpu_sys[i] / all,
603 100.0 * rel->cpu_idle[i] / all,
604 100.0 * rel->cpu_iow[i] / all);
608 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
609 int cpus, int *voff)
611 int i;
613 for (i = 0; i < cpus; ++i) {
614 mvwprintw(screen, (*voff)++, 2,
615 "CPU%d: %14llu irqs/t "
616 "%15llu soirq RX/t "
617 "%15llu soirq TX/t ", i,
618 rel->irqs[i],
619 rel->irqs_srx[i],
620 rel->irqs_stx[i]);
624 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
625 int cpus, int *voff)
627 int i;
629 for (i = 0; i < cpus; ++i) {
630 mvwprintw(screen, (*voff)++, 2,
631 "CPU%d: %14llu irqs", i,
632 abs->irqs[i]);
636 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
637 const struct ifstat *abs, int *voff)
639 if (iswireless(abs)) {
640 mvwprintw(screen, (*voff)++, 2,
641 "LinkQual: %7d/%d (%d/t) ",
642 abs->wifi.link_qual,
643 abs->wifi.link_qual_max,
644 rel->wifi.link_qual);
646 mvwprintw(screen, (*voff)++, 2,
647 "Signal: %8d dBm (%d dBm/t) ",
648 abs->wifi.signal_level,
649 rel->wifi.signal_level);
653 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
654 const struct ifstat *abs, int *first, uint64_t ms_interval)
656 int cpus, voff = 1, cvoff = 2;
658 curs_set(0);
660 cpus = get_number_cpus();
661 bug_on(cpus > MAX_CPUS);
663 screen_header(screen, ifname, &voff, ms_interval);
665 voff++;
666 screen_net_dev_rel(screen, rel, &voff);
668 voff++;
669 screen_net_dev_abs(screen, abs, &voff);
671 voff++;
672 screen_sys_mem(screen, rel, abs, &voff);
674 voff++;
675 screen_percpu_states(screen, rel, cpus, &voff);
677 voff++;
678 screen_percpu_irqs_rel(screen, rel, cpus, &voff);
680 voff++;
681 screen_percpu_irqs_abs(screen, abs, cpus, &voff);
683 voff++;
684 screen_wireless(screen, rel, abs, &voff);
686 if (*first) {
687 mvwprintw(screen, cvoff, 2, "Collecting data ...");
688 *first = 0;
689 } else {
690 mvwprintw(screen, cvoff, 2, " ");
693 wrefresh(screen);
694 refresh();
697 static void screen_end(void)
699 endwin();
702 static int screen_main(const char *ifname, uint64_t ms_interval)
704 int first = 1, key;
706 screen_init(&stats_screen);
708 while (!sigint) {
709 key = getch();
710 if (key == 'q' || key == 0x1b || key == KEY_F(10))
711 break;
713 screen_update(stats_screen, ifname, &stats_delta, &stats_new,
714 &first, ms_interval);
716 stats_sample_generic(ifname, ms_interval);
719 screen_end();
721 return 0;
724 static void term_csv(const char *ifname, const struct ifstat *rel,
725 const struct ifstat *abs, uint64_t ms_interval)
727 int cpus, i;
729 printf("%ld ", time(0));
731 printf("%llu ", rel->rx_bytes);
732 printf("%llu ", rel->rx_packets);
733 printf("%llu ", rel->rx_drops);
734 printf("%llu ", rel->rx_errors);
736 printf("%llu ", abs->rx_bytes);
737 printf("%llu ", abs->rx_packets);
738 printf("%llu ", abs->rx_drops);
739 printf("%llu ", abs->rx_errors);
741 printf("%llu ", rel->tx_bytes);
742 printf("%llu ", rel->tx_packets);
743 printf("%llu ", rel->tx_drops);
744 printf("%llu ", rel->tx_errors);
746 printf("%llu ", abs->tx_bytes);
747 printf("%llu ", abs->tx_packets);
748 printf("%llu ", abs->tx_drops);
749 printf("%llu ", abs->tx_errors);
751 printf("%u ", rel->cswitch);
752 printf("%lu ", abs->mem_free);
753 printf("%lu ", abs->mem_total - abs->mem_free);
754 printf("%lu ", abs->mem_total);
755 printf("%u ", abs->procs_run);
756 printf("%u ", abs->procs_iow);
758 cpus = get_number_cpus();
759 bug_on(cpus > MAX_CPUS);
761 for (i = 0; i < cpus; ++i) {
762 printf("%lu ", rel->cpu_user[i]);
763 printf("%lu ", rel->cpu_nice[i]);
764 printf("%lu ", rel->cpu_sys[i]);
765 printf("%lu ", rel->cpu_idle[i]);
766 printf("%lu ", rel->cpu_iow[i]);
768 printf("%llu ", rel->irqs[i]);
769 printf("%llu ", abs->irqs[i]);
771 printf("%llu ", rel->irqs_srx[i]);
772 printf("%llu ", abs->irqs_srx[i]);
774 printf("%llu ", rel->irqs_stx[i]);
775 printf("%llu ", abs->irqs_stx[i]);
778 if (iswireless(abs)) {
779 printf("%u ", rel->wifi.link_qual);
780 printf("%u ", abs->wifi.link_qual);
781 printf("%u ", abs->wifi.link_qual_max);
783 printf("%d ", rel->wifi.signal_level);
784 printf("%d ", abs->wifi.signal_level);
787 puts("");
788 fflush(stdout);
791 static void term_csv_header(const char *ifname, const struct ifstat *abs,
792 uint64_t ms_interval)
794 int cpus, i, j = 1;
796 printf("# gnuplot dump (#col:description)\n");
797 printf("# networking interface: %s\n", ifname);
798 printf("# sampling interval (t): %lu ms\n", ms_interval);
799 printf("# %d:unixtime ", j++);
801 printf("%d:rx-bytes-per-t ", j++);
802 printf("%d:rx-pkts-per-t ", j++);
803 printf("%d:rx-drops-per-t ", j++);
804 printf("%d:rx-errors-per-t ", j++);
806 printf("%d:rx-bytes ", j++);
807 printf("%d:rx-pkts ", j++);
808 printf("%d:rx-drops ", j++);
809 printf("%d:rx-errors ", j++);
811 printf("%d:tx-bytes-per-t ", j++);
812 printf("%d:tx-pkts-per-t ", j++);
813 printf("%d:tx-drops-per-t ", j++);
814 printf("%d:tx-errors-per-t ", j++);
816 printf("%d:tx-bytes ", j++);
817 printf("%d:tx-pkts ", j++);
818 printf("%d:tx-drops ", j++);
819 printf("%d:tx-errors ", j++);
821 printf("%d:context-switches-per-t ", j++);
822 printf("%d:mem-free ", j++);
823 printf("%d:mem-used ", j++);
824 printf("%d:mem-total ", j++);
825 printf("%d:procs-in-run ", j++);
826 printf("%d:procs-in-iow ", j++);
828 cpus = get_number_cpus();
829 bug_on(cpus > MAX_CPUS);
831 for (i = 0, j = 22; i < cpus; ++i) {
832 printf("%d:cpu%i-usr-per-t ", j++, i);
833 printf("%d:cpu%i-nice-per-t ", j++, i);
834 printf("%d:cpu%i-sys-per-t ", j++, i);
835 printf("%d:cpu%i-idle-per-t ", j++, i);
836 printf("%d:cpu%i-iow-per-t ", j++, i);
838 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
839 printf("%d:cpu%i-net-irqs ", j++, i);
841 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
842 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
843 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
844 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
847 if (iswireless(abs)) {
848 printf("%d:wifi-link-qual-per-t ", j++);
849 printf("%d:wifi-link-qual ", j++);
850 printf("%d:wifi-link-qual-max ", j++);
852 printf("%d:wifi-signal-dbm-per-t ", j++);
853 printf("%d:wifi-signal-dbm ", j++);
856 puts("");
857 printf("# data:\n");
858 fflush(stdout);
861 static int term_main(const char *ifname, uint64_t ms_interval)
863 int first = 1;
865 do {
866 stats_sample_generic(ifname, ms_interval);
868 if (first) {
869 first = 0;
870 term_csv_header(ifname, &stats_new, ms_interval);
873 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
874 } while (stats_loop && !sigint);
876 return 0;
879 int main(int argc, char **argv)
881 short ifflags = 0;
882 int c, opt_index, ret, promisc = 0;
883 uint64_t interval = 1000;
884 char *ifname = NULL;
885 int (*func_main)(const char *ifname, uint64_t ms_interval) = screen_main;
887 setfsuid(getuid());
888 setfsgid(getgid());
890 while ((c = getopt_long(argc, argv, short_options, long_options,
891 &opt_index)) != EOF) {
892 switch (c) {
893 case 'h':
894 help();
895 break;
896 case 'v':
897 version();
898 break;
899 case 'd':
900 ifname = xstrndup(optarg, IFNAMSIZ);
901 break;
902 case 't':
903 interval = strtol(optarg, NULL, 10);
904 break;
905 case 'l':
906 stats_loop = 1;
907 break;
908 case 'p':
909 promisc = 1;
910 break;
911 case 'c':
912 func_main = term_main;
913 break;
914 case '?':
915 switch (optopt) {
916 case 'd':
917 case 't':
918 panic("Option -%c requires an argument!\n",
919 optopt);
920 default:
921 if (isprint(optopt))
922 whine("Unknown option character "
923 "`0x%X\'!\n", optopt);
924 die();
926 default:
927 break;
931 if (argc == 1)
932 help();
934 if (argc == 2)
935 ifname = xstrndup(argv[1], IFNAMSIZ);
936 if (ifname == NULL)
937 panic("No networking device given!\n");
939 if (!strncmp("lo", ifname, IFNAMSIZ))
940 panic("lo is not supported!\n");
941 if (device_mtu(ifname) == 0)
942 panic("This is no networking device!\n");
944 register_signal(SIGINT, signal_handler);
945 register_signal(SIGHUP, signal_handler);
947 if (promisc)
948 ifflags = enter_promiscuous_mode(ifname);
949 ret = func_main(ifname, interval);
950 if (promisc)
951 leave_promiscuous_mode(ifname, ifflags);
953 xfree(ifname);
954 return ret;