Corrected spinning loop potential in getrandombytes
[netsniff-ng-old.git] / ifpps.c
blob9e44c60c0def6e4a68f3f1dcd166d0c2e82cc104
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009 - 2013 Daniel Borkmann.
4 * Subject to the GPL, version 2.
6 * He had all the injured air of a liar suspected when for once he
7 * has told the truth, or part of it.
9 * -- The Lord of the Rings, On Gollum,
10 * Chapter 'The Black Gate is Closed'.
13 #include <stdio.h>
14 #include <string.h>
15 #include <curses.h>
16 #include <getopt.h>
17 #include <ctype.h>
18 #include <sys/socket.h>
19 #include <sys/fsuid.h>
20 #include <signal.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <time.h>
25 #include "die.h"
26 #include "xmalloc.h"
27 #include "xutils.h"
28 #include "xio.h"
29 #include "built_in.h"
31 struct wifi_stat {
32 uint32_t bitrate;
33 int16_t link_qual, link_qual_max;
34 int signal_level /*, noise_level*/;
37 struct ifstat {
38 long long unsigned int rx_bytes, rx_packets, rx_drops, rx_errors;
39 long long unsigned int rx_fifo, rx_frame, rx_multi;
40 long long unsigned int tx_bytes, tx_packets, tx_drops, tx_errors;
41 long long unsigned int tx_fifo, tx_colls, tx_carrier;
42 long long unsigned int irqs[MAX_CPUS], irqs_srx[MAX_CPUS], irqs_stx[MAX_CPUS];
43 int64_t cpu_user[MAX_CPUS], cpu_nice[MAX_CPUS], cpu_sys[MAX_CPUS];
44 int64_t cpu_idle[MAX_CPUS], cpu_iow[MAX_CPUS], mem_free, mem_total;
45 uint32_t irq_nr, procs_run, procs_iow, cswitch, forks;
46 struct wifi_stat wifi;
49 volatile sig_atomic_t sigint = 0;
51 static struct ifstat stats_old, stats_new, stats_delta;
53 static int stats_loop = 0;
55 static WINDOW *stats_screen = NULL;
57 static const char *short_options = "d:t:vhclp";
58 static const struct option long_options[] = {
59 {"dev", required_argument, NULL, 'd'},
60 {"interval", required_argument, NULL, 't'},
61 {"promisc", no_argument, NULL, 'p'},
62 {"csv", no_argument, NULL, 'c'},
63 {"loop", no_argument, NULL, 'l'},
64 {"version", no_argument, NULL, 'v'},
65 {"help", no_argument, NULL, 'h'},
66 {NULL, 0, NULL, 0}
69 static void signal_handler(int number)
71 switch (number) {
72 case SIGINT:
73 sigint = 1;
74 break;
75 case SIGHUP:
76 default:
77 break;
81 static inline char *snr_to_str(int level)
83 if (level > 40)
84 return "very good signal";
85 if (level > 25 && level <= 40)
86 return "good signal";
87 if (level > 15 && level <= 25)
88 return "poor signal";
89 if (level > 10 && level <= 15)
90 return "very poor signal";
91 if (level <= 10)
92 return "no signal";
94 return "unknown";
97 static inline int iswireless(const struct ifstat *stats)
99 return stats->wifi.bitrate > 0;
102 static void help(void)
104 printf("\nifpps %s, top-like kernel networking and system statistics\n",
105 VERSION_STRING);
106 puts("http://www.netsniff-ng.org\n\n"
107 "Usage: ifpps [options] || ifpps <netdev>\n"
108 "Options:\n"
109 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
110 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
111 " -p|--promisc Promiscuous mode\n"
112 " -c|--csv Output to terminal as Gnuplot-ready data\n"
113 " -l|--loop Continuous CSV output\n"
114 " -v|--version Print version\n"
115 " -h|--help Print this help\n\n"
116 "Examples:\n"
117 " ifpps eth0\n"
118 " ifpps -pd eth0\n"
119 " ifpps -lpcd wlan0 > plot.dat\n\n"
120 "Note:\n"
121 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
122 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
123 "Please report bugs to <bugs@netsniff-ng.org>\n"
124 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
125 "Swiss federal institute of technology (ETH Zurich)\n"
126 "License: GNU GPL version 2.0\n"
127 "This is free software: you are free to change and redistribute it.\n"
128 "There is NO WARRANTY, to the extent permitted by law.\n");
129 die();
132 static void version(void)
134 printf("\nifpps %s, top-like kernel networking and system statistics\n",
135 VERSION_STRING);
136 puts("http://www.netsniff-ng.org\n\n"
137 "Please report bugs to <bugs@netsniff-ng.org>\n"
138 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
139 "Swiss federal institute of technology (ETH Zurich)\n"
140 "License: GNU GPL version 2.0\n"
141 "This is free software: you are free to change and redistribute it.\n"
142 "There is NO WARRANTY, to the extent permitted by law.\n");
143 die();
146 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
148 int ret = -EINVAL;
149 char buff[256];
150 FILE *fp;
152 fp = fopen("/proc/net/dev", "r");
153 if (!fp)
154 panic("Cannot open /proc/net/dev!\n");
156 if (fgets(buff, sizeof(buff), fp)) { ; }
157 if (fgets(buff, sizeof(buff), fp)) { ; }
159 memset(buff, 0, sizeof(buff));
161 while (fgets(buff, sizeof(buff), fp) != NULL) {
162 buff[sizeof(buff) -1] = 0;
164 if (strstr(buff, ifname) == NULL)
165 continue;
167 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
168 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
169 &stats->rx_bytes, &stats->rx_packets,
170 &stats->rx_errors, &stats->rx_drops,
171 &stats->rx_fifo, &stats->rx_frame,
172 &stats->rx_multi, &stats->tx_bytes,
173 &stats->tx_packets, &stats->tx_errors,
174 &stats->tx_drops, &stats->tx_fifo,
175 &stats->tx_colls, &stats->tx_carrier) == 14) {
176 ret = 0;
177 break;
180 memset(buff, 0, sizeof(buff));
183 fclose(fp);
184 return ret;
187 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
189 int ret = -EINVAL, i, cpus, try = 0;
190 char *ptr, buff[256];
191 struct ethtool_drvinfo drvinf;
192 FILE *fp;
194 fp = fopen("/proc/interrupts", "r");
195 if (!fp)
196 panic("Cannot open /proc/interrupts!\n");
198 cpus = get_number_cpus();
199 bug_on(cpus > MAX_CPUS);
200 retry:
201 fseek(fp, 0, SEEK_SET);
202 memset(buff, 0, sizeof(buff));
204 while (fgets(buff, sizeof(buff), fp) != NULL) {
205 buff[sizeof(buff) - 1] = 0;
206 ptr = buff;
208 if (strstr(buff, ifname) == NULL)
209 continue;
211 stats->irq_nr = strtol(ptr, &ptr, 10);
212 bug_on(stats->irq_nr == 0);
214 if (ptr)
215 ptr++;
216 for (i = 0; i < cpus && ptr; ++i) {
217 stats->irqs[i] = strtol(ptr, &ptr, 10);
218 if (i == cpus - 1) {
219 ret = 0;
220 goto done;
224 memset(buff, 0, sizeof(buff));
227 if (ret == -EINVAL && try == 0) {
228 memset(&drvinf, 0, sizeof(drvinf));
229 if (ethtool_drvinf(ifname, &drvinf) < 0)
230 goto done;
232 ifname = drvinf.driver;
233 try++;
235 goto retry;
237 done:
238 fclose(fp);
239 return ret;
242 static int stats_proc_softirqs(struct ifstat *stats)
244 int i, cpus;
245 char *ptr, buff[256];
246 FILE *fp;
247 enum {
248 softirqs_net_rx,
249 softirqs_net_tx,
250 softirqs_net_none,
251 } net_type = softirqs_net_none;
253 fp = fopen("/proc/softirqs", "r");
254 if (!fp)
255 panic("Cannot open /proc/softirqs!\n");
257 cpus = get_number_cpus();
258 bug_on(cpus > MAX_CPUS);
260 memset(buff, 0, sizeof(buff));
262 while (fgets(buff, sizeof(buff), fp) != NULL) {
263 buff[sizeof(buff) - 1] = 0;
265 if ((ptr = strstr(buff, "NET_TX:")))
266 net_type = softirqs_net_tx;
267 else if ((ptr = strstr(buff, "NET_RX:")))
268 net_type = softirqs_net_rx;
269 else
270 continue;
272 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
273 switch (net_type) {
274 case softirqs_net_tx:
275 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
276 break;
277 case softirqs_net_rx:
278 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
279 break;
280 default:
281 bug();
285 memset(buff, 0, sizeof(buff));
288 fclose(fp);
289 return 0;
292 static int stats_proc_memory(struct ifstat *stats)
294 char *ptr, buff[256];
295 FILE *fp;
297 fp = fopen("/proc/meminfo", "r");
298 if (!fp)
299 panic("Cannot open /proc/meminfo!\n");
301 memset(buff, 0, sizeof(buff));
303 while (fgets(buff, sizeof(buff), fp) != NULL) {
304 buff[sizeof(buff) - 1] = 0;
306 if ((ptr = strstr(buff, "MemTotal:"))) {
307 ptr += strlen("MemTotal:");
308 stats->mem_total = strtol(ptr, &ptr, 10);
309 } else if ((ptr = strstr(buff, "MemFree:"))) {
310 ptr += strlen("MemFree:");
311 stats->mem_free = strtol(ptr, &ptr, 10);
314 memset(buff, 0, sizeof(buff));
317 fclose(fp);
318 return 0;
321 static int stats_proc_system(struct ifstat *stats)
323 int cpu, cpus;
324 char *ptr, buff[256];
325 FILE *fp;
327 fp = fopen("/proc/stat", "r");
328 if (!fp)
329 panic("Cannot open /proc/stat!\n");
331 cpus = get_number_cpus();
332 bug_on(cpus > MAX_CPUS);
334 memset(buff, 0, sizeof(buff));
336 while (fgets(buff, sizeof(buff), fp) != NULL) {
337 buff[sizeof(buff) - 1] = 0;
339 if ((ptr = strstr(buff, "cpu"))) {
340 ptr += strlen("cpu");
341 if (isblank(*ptr))
342 goto next;
344 cpu = strtol(ptr, &ptr, 10);
345 bug_on(cpu > cpus);
347 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
348 &stats->cpu_user[cpu],
349 &stats->cpu_nice[cpu],
350 &stats->cpu_sys[cpu],
351 &stats->cpu_idle[cpu],
352 &stats->cpu_iow[cpu]) != 5)
353 goto next;
354 } else if ((ptr = strstr(buff, "ctxt"))) {
355 ptr += strlen("ctxt");
356 stats->cswitch = strtoul(ptr, &ptr, 10);
357 } else if ((ptr = strstr(buff, "processes"))) {
358 ptr += strlen("processes");
359 stats->forks = strtoul(ptr, &ptr, 10);
360 } else if ((ptr = strstr(buff, "procs_running"))) {
361 ptr += strlen("procs_running");
362 stats->procs_run = strtoul(ptr, &ptr, 10);
363 } else if ((ptr = strstr(buff, "procs_blocked"))) {
364 ptr += strlen("procs_blocked");
365 stats->procs_iow = strtoul(ptr, &ptr, 10);
367 next:
368 memset(buff, 0, sizeof(buff));
371 fclose(fp);
372 return 0;
375 static int adjust_dbm_level(int in_dbm, int dbm_val)
377 if (!in_dbm)
378 return dbm_val;
380 return dbm_val - 0x100;
383 static int stats_wireless(const char *ifname, struct ifstat *stats)
385 int ret;
386 struct iw_statistics ws;
388 ret = wireless_sigqual(ifname, &ws);
389 if (ret != 0) {
390 stats->wifi.bitrate = 0;
391 return -EINVAL;
394 stats->wifi.bitrate = wireless_bitrate(ifname);
396 stats->wifi.signal_level =
397 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
399 stats->wifi.link_qual = ws.qual.qual;
400 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
402 return ret;
405 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
406 #define DIFF(member) do { \
407 if (sizeof(diff->member) != sizeof(new->member) || \
408 sizeof(diff->member) != sizeof(old->member)) \
409 bug(); \
410 bug_on((new->member - old->member) > (new->member)); \
411 DIFF1(member); \
412 } while (0)
414 static void stats_diff(struct ifstat *old, struct ifstat *new,
415 struct ifstat *diff)
417 int cpus, i;
419 DIFF(rx_bytes);
420 DIFF(rx_packets);
421 DIFF(rx_drops);
422 DIFF(rx_errors);
423 DIFF(rx_fifo);
424 DIFF(rx_frame);
425 DIFF(rx_multi);
427 DIFF(tx_bytes);
428 DIFF(tx_bytes);
429 DIFF(tx_packets);
430 DIFF(tx_drops);
431 DIFF(tx_errors);
432 DIFF(tx_fifo);
433 DIFF(tx_colls);
434 DIFF(tx_carrier);
436 DIFF1(procs_run);
437 DIFF1(procs_iow);
439 DIFF1(wifi.signal_level);
440 DIFF1(wifi.link_qual);
442 DIFF1(cswitch);
443 DIFF1(forks);
445 cpus = get_number_cpus();
446 bug_on(cpus > MAX_CPUS);
448 for (i = 0; i < cpus; ++i) {
449 DIFF(irqs[i]);
450 DIFF(irqs_srx[i]);
451 DIFF(irqs_stx[i]);
453 DIFF1(cpu_user[i]);
454 DIFF1(cpu_nice[i]);
455 DIFF1(cpu_sys[i]);
456 DIFF1(cpu_idle[i]);
457 DIFF1(cpu_iow[i]);
461 static void stats_fetch(const char *ifname, struct ifstat *stats)
463 if (stats_proc_net_dev(ifname, stats) < 0)
464 panic("Cannot fetch device stats!\n");
465 if (stats_proc_softirqs(stats) < 0)
466 panic("Cannot fetch software interrupts!\n");
467 if (stats_proc_memory(stats) < 0)
468 panic("Cannot fetch memory stats!\n");
469 if (stats_proc_system(stats) < 0)
470 panic("Cannot fetch system stats!\n");
472 stats_proc_interrupts((char *) ifname, stats);
474 stats_wireless(ifname, stats);
477 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
479 memset(&stats_old, 0, sizeof(stats_old));
480 memset(&stats_new, 0, sizeof(stats_new));
481 memset(&stats_delta, 0, sizeof(stats_delta));
483 stats_fetch(ifname, &stats_old);
484 usleep(ms_interval * 1000);
485 stats_fetch(ifname, &stats_new);
487 stats_diff(&stats_old, &stats_new, &stats_delta);
490 static void screen_init(WINDOW **screen)
492 (*screen) = initscr();
494 raw();
495 noecho();
496 cbreak();
497 nodelay((*screen), TRUE);
499 keypad(stdscr, TRUE);
501 refresh();
502 wrefresh((*screen));
505 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
506 uint64_t ms_interval)
508 size_t len = 0;
509 char buff[64];
510 struct ethtool_drvinfo drvinf;
511 u32 rate = device_bitrate(ifname);
512 int link = ethtool_link(ifname);
514 memset(&drvinf, 0, sizeof(drvinf));
515 ethtool_drvinf(ifname, &drvinf);
517 memset(buff, 0, sizeof(buff));
518 if (rate)
519 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
520 if (link >= 0)
521 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
522 link == 0 ? "no" : "yes");
524 mvwprintw(screen, (*voff)++, 2,
525 "Kernel net/sys statistics for %s (%s%s), t=%lums"
526 " ",
527 ifname, drvinf.driver, buff, ms_interval);
530 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
531 int *voff)
533 attron(A_REVERSE);
535 mvwprintw(screen, (*voff)++, 0,
536 " RX: %16.3llf MiB/t "
537 "%10llu pkts/t "
538 "%10llu drops/t "
539 "%10llu errors/t ",
540 ((long double) rel->rx_bytes) / (1LLU << 20),
541 rel->rx_packets, rel->rx_drops, rel->rx_errors);
543 mvwprintw(screen, (*voff)++, 0,
544 " TX: %16.3llf MiB/t "
545 "%10llu pkts/t "
546 "%10llu drops/t "
547 "%10llu errors/t ",
548 ((long double) rel->tx_bytes) / (1LLU << 20),
549 rel->tx_packets, rel->tx_drops, rel->tx_errors);
551 attroff(A_REVERSE);
554 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
555 int *voff)
557 mvwprintw(screen, (*voff)++, 2,
558 "RX: %16.3llf MiB "
559 "%10llu pkts "
560 "%10llu drops "
561 "%10llu errors",
562 ((long double) abs->rx_bytes) / (1LLU << 20),
563 abs->rx_packets, abs->rx_drops, abs->rx_errors);
565 mvwprintw(screen, (*voff)++, 2,
566 "TX: %16.3llf MiB "
567 "%10llu pkts "
568 "%10llu drops "
569 "%10llu errors",
570 ((long double) abs->tx_bytes) / (1LLU << 20),
571 abs->tx_packets, abs->tx_drops, abs->tx_errors);
574 static void screen_sys_mem(WINDOW *screen, const struct ifstat *rel,
575 const struct ifstat *abs, int *voff)
577 mvwprintw(screen, (*voff)++, 2,
578 "SYS: %14u cs/t "
579 "%10.1lf%% mem "
580 "%13u running "
581 "%10u iowait",
582 rel->cswitch,
583 (100.0 * (abs->mem_total - abs->mem_free)) / abs->mem_total,
584 abs->procs_run, abs->procs_iow);
587 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
588 int cpus, int *voff)
590 int i;
591 uint64_t all;
593 for (i = 0; i < cpus; ++i) {
594 all = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i] +
595 rel->cpu_idle[i] + rel->cpu_iow[i];
597 mvwprintw(screen, (*voff)++, 2,
598 "CPU%d: %13.1lf%% usr/t "
599 "%9.1lf%% sys/t "
600 "%10.1lf%% idl/t "
601 "%11.1lf%% iow/t ", i,
602 100.0 * (rel->cpu_user[i] + rel->cpu_nice[i]) / all,
603 100.0 * rel->cpu_sys[i] / all,
604 100.0 * rel->cpu_idle[i] / all,
605 100.0 * rel->cpu_iow[i] / all);
609 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
610 int cpus, int *voff)
612 int i;
614 for (i = 0; i < cpus; ++i) {
615 mvwprintw(screen, (*voff)++, 2,
616 "CPU%d: %14llu irqs/t "
617 "%15llu soirq RX/t "
618 "%15llu soirq TX/t ", i,
619 rel->irqs[i],
620 rel->irqs_srx[i],
621 rel->irqs_stx[i]);
625 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
626 int cpus, int *voff)
628 int i;
630 for (i = 0; i < cpus; ++i) {
631 mvwprintw(screen, (*voff)++, 2,
632 "CPU%d: %14llu irqs", i,
633 abs->irqs[i]);
637 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
638 const struct ifstat *abs, int *voff)
640 if (iswireless(abs)) {
641 mvwprintw(screen, (*voff)++, 2,
642 "LinkQual: %7d/%d (%d/t) ",
643 abs->wifi.link_qual,
644 abs->wifi.link_qual_max,
645 rel->wifi.link_qual);
647 mvwprintw(screen, (*voff)++, 2,
648 "Signal: %8d dBm (%d dBm/t) ",
649 abs->wifi.signal_level,
650 rel->wifi.signal_level);
654 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
655 const struct ifstat *abs, int *first, uint64_t ms_interval)
657 int cpus, voff = 1, cvoff = 2;
659 curs_set(0);
661 cpus = get_number_cpus();
662 bug_on(cpus > MAX_CPUS);
664 screen_header(screen, ifname, &voff, ms_interval);
666 voff++;
667 screen_net_dev_rel(screen, rel, &voff);
669 voff++;
670 screen_net_dev_abs(screen, abs, &voff);
672 voff++;
673 screen_sys_mem(screen, rel, abs, &voff);
675 voff++;
676 screen_percpu_states(screen, rel, cpus, &voff);
678 voff++;
679 screen_percpu_irqs_rel(screen, rel, cpus, &voff);
681 voff++;
682 screen_percpu_irqs_abs(screen, abs, cpus, &voff);
684 voff++;
685 screen_wireless(screen, rel, abs, &voff);
687 if (*first) {
688 mvwprintw(screen, cvoff, 2, "Collecting data ...");
689 *first = 0;
690 } else {
691 mvwprintw(screen, cvoff, 2, " ");
694 wrefresh(screen);
695 refresh();
698 static void screen_end(void)
700 endwin();
703 static int screen_main(const char *ifname, uint64_t ms_interval)
705 int first = 1, key;
707 screen_init(&stats_screen);
709 while (!sigint) {
710 key = getch();
711 if (key == 'q' || key == 0x1b || key == KEY_F(10))
712 break;
714 screen_update(stats_screen, ifname, &stats_delta, &stats_new,
715 &first, ms_interval);
717 stats_sample_generic(ifname, ms_interval);
720 screen_end();
722 return 0;
725 static void term_csv(const char *ifname, const struct ifstat *rel,
726 const struct ifstat *abs, uint64_t ms_interval)
728 int cpus, i;
730 printf("%ld ", time(0));
732 printf("%llu ", rel->rx_bytes);
733 printf("%llu ", rel->rx_packets);
734 printf("%llu ", rel->rx_drops);
735 printf("%llu ", rel->rx_errors);
737 printf("%llu ", abs->rx_bytes);
738 printf("%llu ", abs->rx_packets);
739 printf("%llu ", abs->rx_drops);
740 printf("%llu ", abs->rx_errors);
742 printf("%llu ", rel->tx_bytes);
743 printf("%llu ", rel->tx_packets);
744 printf("%llu ", rel->tx_drops);
745 printf("%llu ", rel->tx_errors);
747 printf("%llu ", abs->tx_bytes);
748 printf("%llu ", abs->tx_packets);
749 printf("%llu ", abs->tx_drops);
750 printf("%llu ", abs->tx_errors);
752 printf("%u ", rel->cswitch);
753 printf("%lu ", abs->mem_free);
754 printf("%lu ", abs->mem_total - abs->mem_free);
755 printf("%lu ", abs->mem_total);
756 printf("%u ", abs->procs_run);
757 printf("%u ", abs->procs_iow);
759 cpus = get_number_cpus();
760 bug_on(cpus > MAX_CPUS);
762 for (i = 0; i < cpus; ++i) {
763 printf("%lu ", rel->cpu_user[i]);
764 printf("%lu ", rel->cpu_nice[i]);
765 printf("%lu ", rel->cpu_sys[i]);
766 printf("%lu ", rel->cpu_idle[i]);
767 printf("%lu ", rel->cpu_iow[i]);
769 printf("%llu ", rel->irqs[i]);
770 printf("%llu ", abs->irqs[i]);
772 printf("%llu ", rel->irqs_srx[i]);
773 printf("%llu ", abs->irqs_srx[i]);
775 printf("%llu ", rel->irqs_stx[i]);
776 printf("%llu ", abs->irqs_stx[i]);
779 if (iswireless(abs)) {
780 printf("%u ", rel->wifi.link_qual);
781 printf("%u ", abs->wifi.link_qual);
782 printf("%u ", abs->wifi.link_qual_max);
784 printf("%d ", rel->wifi.signal_level);
785 printf("%d ", abs->wifi.signal_level);
788 puts("");
789 fflush(stdout);
792 static void term_csv_header(const char *ifname, const struct ifstat *abs,
793 uint64_t ms_interval)
795 int cpus, i, j = 1;
797 printf("# gnuplot dump (#col:description)\n");
798 printf("# networking interface: %s\n", ifname);
799 printf("# sampling interval (t): %lu ms\n", ms_interval);
800 printf("# %d:unixtime ", j++);
802 printf("%d:rx-bytes-per-t ", j++);
803 printf("%d:rx-pkts-per-t ", j++);
804 printf("%d:rx-drops-per-t ", j++);
805 printf("%d:rx-errors-per-t ", j++);
807 printf("%d:rx-bytes ", j++);
808 printf("%d:rx-pkts ", j++);
809 printf("%d:rx-drops ", j++);
810 printf("%d:rx-errors ", j++);
812 printf("%d:tx-bytes-per-t ", j++);
813 printf("%d:tx-pkts-per-t ", j++);
814 printf("%d:tx-drops-per-t ", j++);
815 printf("%d:tx-errors-per-t ", j++);
817 printf("%d:tx-bytes ", j++);
818 printf("%d:tx-pkts ", j++);
819 printf("%d:tx-drops ", j++);
820 printf("%d:tx-errors ", j++);
822 printf("%d:context-switches-per-t ", j++);
823 printf("%d:mem-free ", j++);
824 printf("%d:mem-used ", j++);
825 printf("%d:mem-total ", j++);
826 printf("%d:procs-in-run ", j++);
827 printf("%d:procs-in-iow ", j++);
829 cpus = get_number_cpus();
830 bug_on(cpus > MAX_CPUS);
832 for (i = 0, j = 22; i < cpus; ++i) {
833 printf("%d:cpu%i-usr-per-t ", j++, i);
834 printf("%d:cpu%i-nice-per-t ", j++, i);
835 printf("%d:cpu%i-sys-per-t ", j++, i);
836 printf("%d:cpu%i-idle-per-t ", j++, i);
837 printf("%d:cpu%i-iow-per-t ", j++, i);
839 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
840 printf("%d:cpu%i-net-irqs ", j++, i);
842 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
843 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
844 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
845 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
848 if (iswireless(abs)) {
849 printf("%d:wifi-link-qual-per-t ", j++);
850 printf("%d:wifi-link-qual ", j++);
851 printf("%d:wifi-link-qual-max ", j++);
853 printf("%d:wifi-signal-dbm-per-t ", j++);
854 printf("%d:wifi-signal-dbm ", j++);
857 puts("");
858 printf("# data:\n");
859 fflush(stdout);
862 static int term_main(const char *ifname, uint64_t ms_interval)
864 int first = 1;
866 do {
867 stats_sample_generic(ifname, ms_interval);
869 if (first) {
870 first = 0;
871 term_csv_header(ifname, &stats_new, ms_interval);
874 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
875 } while (stats_loop && !sigint);
877 return 0;
880 int main(int argc, char **argv)
882 short ifflags = 0;
883 int c, opt_index, ret, promisc = 0;
884 uint64_t interval = 1000;
885 char *ifname = NULL;
886 int (*func_main)(const char *ifname, uint64_t ms_interval) = screen_main;
888 setfsuid(getuid());
889 setfsgid(getgid());
891 while ((c = getopt_long(argc, argv, short_options, long_options,
892 &opt_index)) != EOF) {
893 switch (c) {
894 case 'h':
895 help();
896 break;
897 case 'v':
898 version();
899 break;
900 case 'd':
901 ifname = xstrndup(optarg, IFNAMSIZ);
902 break;
903 case 't':
904 interval = strtol(optarg, NULL, 10);
905 break;
906 case 'l':
907 stats_loop = 1;
908 break;
909 case 'p':
910 promisc = 1;
911 break;
912 case 'c':
913 func_main = term_main;
914 break;
915 case '?':
916 switch (optopt) {
917 case 'd':
918 case 't':
919 panic("Option -%c requires an argument!\n",
920 optopt);
921 default:
922 if (isprint(optopt))
923 printf("Unknown option character `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;