src: setfsuid / setfsgid
[netsniff-ng.git] / src / ifpps.c
blob35048037ca44d3760272c7a750acc1880cc93504
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 <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 CSV\n"
122 " E.g. post-processing with Gnuplot et al.\n"
123 " -l|--loop Continuous CSV output\n"
124 " -v|--version Print version\n"
125 " -h|--help Print this help\n\n"
126 "Examples:\n"
127 " ifpps eth0\n"
128 " ifpps -pd eth0\n"
129 " ifpps -lpcd wlan0 > plot.dat\n\n"
130 "Please report bugs to <bugs@netsniff-ng.org>\n"
131 "Copyright (C) 2009-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n"
132 "License: GNU GPL version 2.0\n"
133 "This is free software: you are free to change and redistribute it.\n"
134 "There is NO WARRANTY, to the extent permitted by law.\n");
135 die();
138 static void version(void)
140 printf("\nifpps %s, top-like kernel networking and system statistics\n",
141 VERSION_STRING);
142 puts("http://www.netsniff-ng.org\n\n"
143 "Please report bugs to <bugs@netsniff-ng.org>\n"
144 "Copyright (C) 2009-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n"
145 "License: GNU GPL version 2.0\n"
146 "This is free software: you are free to change and redistribute it.\n"
147 "There is NO WARRANTY, to the extent permitted by law.\n");
148 die();
151 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
153 int ret = -EINVAL;
154 char buff[256];
155 FILE *fp;
157 fp = fopen("/proc/net/dev", "r");
158 if (!fp)
159 panic("Cannot open /proc/net/dev!\n");
161 if (fgets(buff, sizeof(buff), fp)) { ; }
162 if (fgets(buff, sizeof(buff), fp)) { ; }
164 memset(buff, 0, sizeof(buff));
166 while (fgets(buff, sizeof(buff), fp) != NULL) {
167 buff[sizeof(buff) -1] = 0;
169 if (strstr(buff, ifname) == NULL)
170 continue;
172 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
173 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
174 &stats->rx_bytes, &stats->rx_packets,
175 &stats->rx_errors, &stats->rx_drops,
176 &stats->rx_fifo, &stats->rx_frame,
177 &stats->rx_multi, &stats->tx_bytes,
178 &stats->tx_packets, &stats->tx_errors,
179 &stats->tx_drops, &stats->tx_fifo,
180 &stats->tx_colls, &stats->tx_carrier) == 14) {
181 ret = 0;
182 break;
185 memset(buff, 0, sizeof(buff));
188 fclose(fp);
189 return ret;
192 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
194 int ret = -EINVAL, i, cpus, try = 0;
195 char *ptr, buff[256];
196 struct ethtool_drvinfo drvinf;
197 FILE *fp;
199 fp = fopen("/proc/interrupts", "r");
200 if (!fp)
201 panic("Cannot open /proc/interrupts!\n");
203 cpus = get_number_cpus();
204 bug_on(cpus > MAX_CPUS);
205 retry:
206 fseek(fp, 0, SEEK_SET);
207 memset(buff, 0, sizeof(buff));
209 while (fgets(buff, sizeof(buff), fp) != NULL) {
210 buff[sizeof(buff) - 1] = 0;
211 ptr = buff;
213 if (strstr(buff, ifname) == NULL)
214 continue;
216 stats->irq_nr = strtol(ptr, &ptr, 10);
217 bug_on(stats->irq_nr == 0);
219 if (ptr)
220 ptr++;
221 for (i = 0; i < cpus && ptr; ++i) {
222 stats->irqs[i] = strtol(ptr, &ptr, 10);
223 if (i == cpus - 1) {
224 ret = 0;
225 goto done;
229 memset(buff, 0, sizeof(buff));
232 if (ret == -EINVAL && try == 0) {
233 memset(&drvinf, 0, sizeof(drvinf));
234 if (ethtool_drvinf(ifname, &drvinf) < 0)
235 goto done;
237 ifname = drvinf.driver;
238 try++;
240 goto retry;
242 done:
243 fclose(fp);
244 return ret;
247 static int stats_proc_softirqs(struct ifstat *stats)
249 int i, cpus;
250 char *ptr, buff[256];
251 FILE *fp;
252 enum {
253 softirqs_net_rx,
254 softirqs_net_tx,
255 softirqs_net_none,
256 } net_type = softirqs_net_none;
258 fp = fopen("/proc/softirqs", "r");
259 if (!fp)
260 panic("Cannot open /proc/softirqs!\n");
262 cpus = get_number_cpus();
263 bug_on(cpus > MAX_CPUS);
265 memset(buff, 0, sizeof(buff));
267 while (fgets(buff, sizeof(buff), fp) != NULL) {
268 buff[sizeof(buff) - 1] = 0;
270 if ((ptr = strstr(buff, "NET_TX:")))
271 net_type = softirqs_net_tx;
272 else if ((ptr = strstr(buff, "NET_RX:")))
273 net_type = softirqs_net_rx;
274 else
275 continue;
277 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
278 switch (net_type) {
279 case softirqs_net_tx:
280 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
281 break;
282 case softirqs_net_rx:
283 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
284 break;
285 default:
286 bug();
290 memset(buff, 0, sizeof(buff));
293 fclose(fp);
294 return 0;
297 static int stats_proc_memory(struct ifstat *stats)
299 char *ptr, buff[256];
300 FILE *fp;
302 fp = fopen("/proc/meminfo", "r");
303 if (!fp)
304 panic("Cannot open /proc/meminfo!\n");
306 memset(buff, 0, sizeof(buff));
308 while (fgets(buff, sizeof(buff), fp) != NULL) {
309 buff[sizeof(buff) - 1] = 0;
311 if ((ptr = strstr(buff, "MemTotal:"))) {
312 ptr += strlen("MemTotal:");
313 stats->mem_total = strtol(ptr, &ptr, 10);
314 } else if ((ptr = strstr(buff, "MemFree:"))) {
315 ptr += strlen("MemFree:");
316 stats->mem_free = strtol(ptr, &ptr, 10);
319 memset(buff, 0, sizeof(buff));
322 fclose(fp);
323 return 0;
326 static int stats_proc_system(struct ifstat *stats)
328 int cpu, cpus;
329 char *ptr, buff[256];
330 FILE *fp;
332 fp = fopen("/proc/stat", "r");
333 if (!fp)
334 panic("Cannot open /proc/stat!\n");
336 cpus = get_number_cpus();
337 bug_on(cpus > MAX_CPUS);
339 memset(buff, 0, sizeof(buff));
341 while (fgets(buff, sizeof(buff), fp) != NULL) {
342 buff[sizeof(buff) - 1] = 0;
344 if ((ptr = strstr(buff, "cpu"))) {
345 ptr += strlen("cpu");
346 if (isblank(*ptr))
347 goto next;
349 cpu = strtol(ptr, &ptr, 10);
350 bug_on(cpu > cpus);
352 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
353 &stats->cpu_user[cpu],
354 &stats->cpu_nice[cpu],
355 &stats->cpu_sys[cpu],
356 &stats->cpu_idle[cpu],
357 &stats->cpu_iow[cpu]) != 5)
358 goto next;
359 } else if ((ptr = strstr(buff, "ctxt"))) {
360 ptr += strlen("ctxt");
361 stats->cswitch = strtoul(ptr, &ptr, 10);
362 } else if ((ptr = strstr(buff, "processes"))) {
363 ptr += strlen("processes");
364 stats->forks = strtoul(ptr, &ptr, 10);
365 } else if ((ptr = strstr(buff, "procs_running"))) {
366 ptr += strlen("procs_running");
367 stats->procs_run = strtoul(ptr, &ptr, 10);
368 } else if ((ptr = strstr(buff, "procs_blocked"))) {
369 ptr += strlen("procs_blocked");
370 stats->procs_iow = strtoul(ptr, &ptr, 10);
372 next:
373 memset(buff, 0, sizeof(buff));
376 fclose(fp);
377 return 0;
380 static int stats_wireless(const char *ifname, struct ifstat *stats)
382 int ret;
383 struct iw_statistics ws;
385 ret = wireless_sigqual(ifname, &ws);
386 if (ret != 0) {
387 stats->wifi.bitrate = 0;
388 return -EINVAL;
391 stats->wifi.bitrate = wireless_bitrate(ifname);
393 stats->wifi.signal_level =
394 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
396 stats->wifi.link_qual = ws.qual.qual;
397 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
399 return ret;
402 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
403 #define DIFF(member) do { \
404 if (sizeof(diff->member) != sizeof(new->member) || \
405 sizeof(diff->member) != sizeof(old->member)) \
406 bug(); \
407 bug_on((new->member - old->member) > (new->member)); \
408 DIFF1(member); \
409 } while (0)
411 static void stats_diff(struct ifstat *old, struct ifstat *new,
412 struct ifstat *diff)
414 int cpus, i;
416 DIFF(rx_bytes);
417 DIFF(rx_packets);
418 DIFF(rx_drops);
419 DIFF(rx_errors);
420 DIFF(rx_fifo);
421 DIFF(rx_frame);
422 DIFF(rx_multi);
424 DIFF(tx_bytes);
425 DIFF(tx_bytes);
426 DIFF(tx_packets);
427 DIFF(tx_drops);
428 DIFF(tx_errors);
429 DIFF(tx_fifo);
430 DIFF(tx_colls);
431 DIFF(tx_carrier);
433 DIFF1(procs_run);
434 DIFF1(procs_iow);
436 DIFF1(wifi.signal_level);
437 DIFF1(wifi.link_qual);
439 DIFF1(cswitch);
440 DIFF1(forks);
442 cpus = get_number_cpus();
443 bug_on(cpus > MAX_CPUS);
445 for (i = 0; i < cpus; ++i) {
446 DIFF(irqs[i]);
447 DIFF(irqs_srx[i]);
448 DIFF(irqs_stx[i]);
450 DIFF1(cpu_user[i]);
451 DIFF1(cpu_nice[i]);
452 DIFF1(cpu_sys[i]);
453 DIFF1(cpu_idle[i]);
454 DIFF1(cpu_iow[i]);
458 static void stats_fetch(const char *ifname, struct ifstat *stats)
460 if (stats_proc_net_dev(ifname, stats) < 0)
461 panic("Cannot fetch device stats!\n");
462 if (stats_proc_softirqs(stats) < 0)
463 panic("Cannot fetch software interrupts!\n");
464 if (stats_proc_memory(stats) < 0)
465 panic("Cannot fetch memory stats!\n");
466 if (stats_proc_system(stats) < 0)
467 panic("Cannot fetch system stats!\n");
469 stats_proc_interrupts((char *) ifname, stats);
471 stats_wireless(ifname, stats);
474 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
476 memset(&stats_old, 0, sizeof(stats_old));
477 memset(&stats_new, 0, sizeof(stats_new));
478 memset(&stats_delta, 0, sizeof(stats_delta));
480 stats_fetch(ifname, &stats_old);
481 usleep(ms_interval * 1000);
482 stats_fetch(ifname, &stats_new);
484 stats_diff(&stats_old, &stats_new, &stats_delta);
487 static void screen_init(WINDOW **screen)
489 (*screen) = initscr();
491 raw();
492 noecho();
493 cbreak();
494 nodelay((*screen), TRUE);
496 keypad(stdscr, TRUE);
498 refresh();
499 wrefresh((*screen));
502 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
503 uint64_t ms_interval)
505 size_t len = 0;
506 char buff[64];
507 struct ethtool_drvinfo drvinf;
508 u32 rate = device_bitrate(ifname);
509 int link = ethtool_link(ifname);
511 memset(&drvinf, 0, sizeof(drvinf));
512 ethtool_drvinf(ifname, &drvinf);
514 memset(buff, 0, sizeof(buff));
515 if (rate)
516 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
517 if (link >= 0)
518 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
519 link == 0 ? "no" : "yes");
521 mvwprintw(screen, (*voff)++, 2,
522 "Kernel net/sys statistics for %s (%s%s), t=%lums"
523 " ",
524 ifname, drvinf.driver, buff, ms_interval);
527 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
528 int *voff)
530 attron(A_REVERSE);
532 mvwprintw(screen, (*voff)++, 0,
533 " RX: %16.3llf MiB/t "
534 "%10llu pkts/t "
535 "%10llu drops/t "
536 "%10llu errors/t ",
537 ((long double) rel->rx_bytes) / (1LLU << 20),
538 rel->rx_packets, rel->rx_drops, rel->rx_errors);
540 mvwprintw(screen, (*voff)++, 0,
541 " TX: %16.3llf MiB/t "
542 "%10llu pkts/t "
543 "%10llu drops/t "
544 "%10llu errors/t ",
545 ((long double) rel->tx_bytes) / (1LLU << 20),
546 rel->tx_packets, rel->tx_drops, rel->tx_errors);
548 attroff(A_REVERSE);
551 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
552 int *voff)
554 mvwprintw(screen, (*voff)++, 2,
555 "RX: %16.3llf MiB "
556 "%10llu pkts "
557 "%10llu drops "
558 "%10llu errors",
559 ((long double) abs->rx_bytes) / (1LLU << 20),
560 abs->rx_packets, abs->rx_drops, abs->rx_errors);
562 mvwprintw(screen, (*voff)++, 2,
563 "TX: %16.3llf MiB "
564 "%10llu pkts "
565 "%10llu drops "
566 "%10llu errors",
567 ((long double) abs->tx_bytes) / (1LLU << 20),
568 abs->tx_packets, abs->tx_drops, abs->tx_errors);
571 static void screen_sys_mem(WINDOW *screen, const struct ifstat *rel,
572 const struct ifstat *abs, int *voff)
574 mvwprintw(screen, (*voff)++, 2,
575 "SYS: %14u cs/t "
576 "%10.1lf%% mem "
577 "%13u running "
578 "%10u iowait",
579 rel->cswitch,
580 (100.0 * (abs->mem_total - abs->mem_free)) / abs->mem_total,
581 abs->procs_run, abs->procs_iow);
584 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
585 int cpus, int *voff)
587 int i;
588 uint64_t all;
590 for (i = 0; i < cpus; ++i) {
591 all = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i] +
592 rel->cpu_idle[i] + rel->cpu_iow[i];
594 mvwprintw(screen, (*voff)++, 2,
595 "CPU%d: %13.1lf%% usr/t "
596 "%9.1lf%% sys/t "
597 "%10.1lf%% idl/t "
598 "%11.1lf%% iow/t ", i,
599 100.0 * (rel->cpu_user[i] + rel->cpu_nice[i]) / all,
600 100.0 * rel->cpu_sys[i] / all,
601 100.0 * rel->cpu_idle[i] / all,
602 100.0 * rel->cpu_iow[i] / all);
606 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
607 int cpus, int *voff)
609 int i;
611 for (i = 0; i < cpus; ++i) {
612 mvwprintw(screen, (*voff)++, 2,
613 "CPU%d: %14llu irqs/t "
614 "%15llu soirq RX/t "
615 "%15llu soirq TX/t ", i,
616 rel->irqs[i],
617 rel->irqs_srx[i],
618 rel->irqs_stx[i]);
622 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
623 int cpus, int *voff)
625 int i;
627 for (i = 0; i < cpus; ++i) {
628 mvwprintw(screen, (*voff)++, 2,
629 "CPU%d: %14llu irqs", i,
630 abs->irqs[i]);
634 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
635 const struct ifstat *abs, int *voff)
637 if (iswireless(abs)) {
638 mvwprintw(screen, (*voff)++, 2,
639 "LinkQual: %7d/%d (%d/t) ",
640 abs->wifi.link_qual,
641 abs->wifi.link_qual_max,
642 rel->wifi.link_qual);
644 mvwprintw(screen, (*voff)++, 2,
645 "Signal: %8d dBm (%d dBm/t) ",
646 abs->wifi.signal_level,
647 rel->wifi.signal_level);
651 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
652 const struct ifstat *abs, int *first, uint64_t ms_interval)
654 int cpus, voff = 1, cvoff = 2;
656 curs_set(0);
658 cpus = get_number_cpus();
659 bug_on(cpus > MAX_CPUS);
661 screen_header(screen, ifname, &voff, ms_interval);
663 voff++;
664 screen_net_dev_rel(screen, rel, &voff);
666 voff++;
667 screen_net_dev_abs(screen, abs, &voff);
669 voff++;
670 screen_sys_mem(screen, rel, abs, &voff);
672 voff++;
673 screen_percpu_states(screen, rel, cpus, &voff);
675 voff++;
676 screen_percpu_irqs_rel(screen, rel, cpus, &voff);
678 voff++;
679 screen_percpu_irqs_abs(screen, abs, cpus, &voff);
681 voff++;
682 screen_wireless(screen, rel, abs, &voff);
684 if (*first) {
685 mvwprintw(screen, cvoff, 2, "Collecting data ...");
686 *first = 0;
687 } else {
688 mvwprintw(screen, cvoff, 2, " ");
691 wrefresh(screen);
692 refresh();
695 static void screen_end(void)
697 endwin();
700 static int screen_main(const char *ifname, uint64_t ms_interval)
702 int first = 1, key;
704 screen_init(&stats_screen);
706 while (!sigint) {
707 key = getch();
708 if (key == 'q' || key == 0x1b || key == KEY_F(10))
709 break;
711 screen_update(stats_screen, ifname, &stats_delta, &stats_new,
712 &first, ms_interval);
714 stats_sample_generic(ifname, ms_interval);
717 screen_end();
719 return 0;
722 static void term_csv(const char *ifname, const struct ifstat *rel,
723 const struct ifstat *abs, uint64_t ms_interval)
725 int cpus, i;
727 printf("%ld ", time(0));
729 printf("%llu ", rel->rx_bytes);
730 printf("%llu ", rel->rx_packets);
731 printf("%llu ", rel->rx_drops);
732 printf("%llu ", rel->rx_errors);
734 printf("%llu ", abs->rx_bytes);
735 printf("%llu ", abs->rx_packets);
736 printf("%llu ", abs->rx_drops);
737 printf("%llu ", abs->rx_errors);
739 printf("%llu ", rel->tx_bytes);
740 printf("%llu ", rel->tx_packets);
741 printf("%llu ", rel->tx_drops);
742 printf("%llu ", rel->tx_errors);
744 printf("%llu ", abs->tx_bytes);
745 printf("%llu ", abs->tx_packets);
746 printf("%llu ", abs->tx_drops);
747 printf("%llu ", abs->tx_errors);
749 printf("%u ", rel->cswitch);
750 printf("%lu ", abs->mem_free);
751 printf("%lu ", abs->mem_total - abs->mem_free);
752 printf("%lu ", abs->mem_total);
753 printf("%u ", abs->procs_run);
754 printf("%u ", abs->procs_iow);
756 cpus = get_number_cpus();
757 bug_on(cpus > MAX_CPUS);
759 for (i = 0; i < cpus; ++i) {
760 printf("%lu ", rel->cpu_user[i]);
761 printf("%lu ", rel->cpu_nice[i]);
762 printf("%lu ", rel->cpu_sys[i]);
763 printf("%lu ", rel->cpu_idle[i]);
764 printf("%lu ", rel->cpu_iow[i]);
766 printf("%llu ", rel->irqs[i]);
767 printf("%llu ", abs->irqs[i]);
769 printf("%llu ", rel->irqs_srx[i]);
770 printf("%llu ", abs->irqs_srx[i]);
772 printf("%llu ", rel->irqs_stx[i]);
773 printf("%llu ", abs->irqs_stx[i]);
776 if (iswireless(abs)) {
777 printf("%u ", rel->wifi.link_qual);
778 printf("%u ", abs->wifi.link_qual);
779 printf("%u ", abs->wifi.link_qual_max);
781 printf("%d ", rel->wifi.signal_level);
782 printf("%d ", abs->wifi.signal_level);
785 puts("");
786 fflush(stdout);
789 static void term_csv_header(const char *ifname, const struct ifstat *abs,
790 uint64_t ms_interval)
792 int cpus, i, j = 1;
794 printf("# gnuplot dump (#col:description)\n");
795 printf("# networking interface: %s\n", ifname);
796 printf("# sampling interval (t): %lu ms\n", ms_interval);
797 printf("# %d:unixtime ", j++);
799 printf("%d:rx-bytes-per-t ", j++);
800 printf("%d:rx-pkts-per-t ", j++);
801 printf("%d:rx-drops-per-t ", j++);
802 printf("%d:rx-errors-per-t ", j++);
804 printf("%d:rx-bytes ", j++);
805 printf("%d:rx-pkts ", j++);
806 printf("%d:rx-drops ", j++);
807 printf("%d:rx-errors ", j++);
809 printf("%d:tx-bytes-per-t ", j++);
810 printf("%d:tx-pkts-per-t ", j++);
811 printf("%d:tx-drops-per-t ", j++);
812 printf("%d:tx-errors-per-t ", j++);
814 printf("%d:tx-bytes ", j++);
815 printf("%d:tx-pkts ", j++);
816 printf("%d:tx-drops ", j++);
817 printf("%d:tx-errors ", j++);
819 printf("%d:context-switches-per-t ", j++);
820 printf("%d:mem-free ", j++);
821 printf("%d:mem-used ", j++);
822 printf("%d:mem-total ", j++);
823 printf("%d:procs-in-run ", j++);
824 printf("%d:procs-in-iow ", j++);
826 cpus = get_number_cpus();
827 bug_on(cpus > MAX_CPUS);
829 for (i = 0, j = 22; i < cpus; ++i) {
830 printf("%d:cpu%i-usr-per-t ", j++, i);
831 printf("%d:cpu%i-nice-per-t ", j++, i);
832 printf("%d:cpu%i-sys-per-t ", j++, i);
833 printf("%d:cpu%i-idle-per-t ", j++, i);
834 printf("%d:cpu%i-iow-per-t ", j++, i);
836 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
837 printf("%d:cpu%i-net-irqs ", j++, i);
839 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
840 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
841 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
842 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
845 if (iswireless(abs)) {
846 printf("%d:wifi-link-qual-per-t ", j++);
847 printf("%d:wifi-link-qual ", j++);
848 printf("%d:wifi-link-qual-max ", j++);
850 printf("%d:wifi-signal-dbm-per-t ", j++);
851 printf("%d:wifi-signal-dbm ", j++);
854 puts("");
855 printf("# data:\n");
856 fflush(stdout);
859 static int term_main(const char *ifname, uint64_t ms_interval)
861 int first = 1;
863 do {
864 stats_sample_generic(ifname, ms_interval);
866 if (first) {
867 first = 0;
868 term_csv_header(ifname, &stats_new, ms_interval);
871 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
872 } while (stats_loop && !sigint);
874 return 0;
877 int main(int argc, char **argv)
879 short ifflags = 0;
880 int c, opt_index, ret, promisc = 0;
881 uint64_t interval = 1000;
882 char *ifname = NULL;
883 int (*func_main)(const char *ifname, uint64_t ms_interval) = screen_main;
885 setfsuid(getuid());
886 setfsgid(getgid());
888 while ((c = getopt_long(argc, argv, short_options, long_options,
889 &opt_index)) != EOF) {
890 switch (c) {
891 case 'h':
892 help();
893 break;
894 case 'v':
895 version();
896 break;
897 case 'd':
898 ifname = xstrndup(optarg, IFNAMSIZ);
899 break;
900 case 't':
901 interval = strtol(optarg, NULL, 10);
902 break;
903 case 'l':
904 stats_loop = 1;
905 break;
906 case 'p':
907 promisc = 1;
908 break;
909 case 'c':
910 func_main = term_main;
911 break;
912 case '?':
913 switch (optopt) {
914 case 'd':
915 case 't':
916 panic("Option -%c requires an argument!\n",
917 optopt);
918 default:
919 if (isprint(optopt))
920 whine("Unknown option character "
921 "`0x%X\'!\n", optopt);
922 die();
924 default:
925 break;
929 if (argc == 1)
930 help();
932 if (argc == 2)
933 ifname = xstrndup(argv[1], IFNAMSIZ);
934 if (ifname == NULL)
935 panic("No networking device given!\n");
937 if (!strncmp("lo", ifname, IFNAMSIZ))
938 panic("lo is not supported!\n");
939 if (device_mtu(ifname) == 0)
940 panic("This is no networking device!\n");
942 register_signal(SIGINT, signal_handler);
943 register_signal(SIGHUP, signal_handler);
945 if (promisc)
946 ifflags = enter_promiscuous_mode(ifname);
947 ret = func_main(ifname, interval);
948 if (promisc)
949 leave_promiscuous_mode(ifname, ifflags);
951 xfree(ifname);
952 return ret;