ifpps: major refactoring / code cleanup
[netsniff.git] / src / ifpps.c
blob6c479da050148dee116b65e432548c287c850023
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_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
510 const struct ifstat *abs, int *first, uint64_t ms_interval)
512 int cpus, i, j = 0;
513 uint64_t all;
515 curs_set(0);
517 cpus = get_number_cpus();
518 bug_on(cpus > MAX_CPUS);
520 mvwprintw(screen, 1, 2, "Kernel net/sys statistics for %s, t=%lums", ifname, ms_interval);
522 attron(A_REVERSE);
523 mvwprintw(screen, 3, 0, " RX: %16.3lf MiB/t %10lu pkts/t %10lu drops/t %10lu errors/t ",
524 1.0 * rel->rx_bytes / (1 << 20), rel->rx_packets, rel->rx_drops, rel->rx_errors);
525 mvwprintw(screen, 4, 0, " TX: %16.3lf MiB/t %10lu pkts/t %10lu drops/t %10lu errors/t ",
526 1.0 * rel->tx_bytes / (1 << 20), rel->tx_packets, rel->tx_drops, rel->tx_errors);
527 attroff(A_REVERSE);
529 mvwprintw(screen, 6, 2, "RX: %16.3lf MiB %10lu pkts %10lu drops %10lu errors",
530 1.0 * abs->rx_bytes / (1 << 20), abs->rx_packets, abs->rx_drops, abs->rx_errors);
531 mvwprintw(screen, 7, 2, "TX: %16.3lf MiB %10lu pkts %10lu drops %10lu errors",
532 1.0 * abs->tx_bytes / (1 << 20), abs->tx_packets, abs->tx_drops, abs->tx_errors);
534 mvwprintw(screen, 9, 2, "SYS: %14ld cs/t %10.1lf%% mem %13ld running %10ld iowait",
535 rel->cswitch, 100.0 * (abs->mem_total - abs->mem_free) / abs->mem_total,
536 abs->procs_run, abs->procs_iow);
538 for(i = 0, j = 11; i < cpus; ++i) {
539 all = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i] + rel->cpu_idle[i] + rel->cpu_iow[i];
541 mvwprintw(screen, j++, 2, "CPU%d: %13.1f%% usr/t %9.1f%% sys/t %10.1f%% idl/t %11.1f%% iow/t ",
542 i, 100.f * (rel->cpu_user[i] + rel->cpu_nice[i]) / all, 100.f * rel->cpu_sys[i] / all,
543 100.f * rel->cpu_idle[i] / all, 100.f * rel->cpu_iow[i] / all);
546 for(i = 0, j++; i < cpus; ++i) {
547 mvwprintw(screen, j++, 2, "CPU%d: %14ld irqs/t %15ld soirq RX/t %15ld soirq TX/t ",
548 i, rel->irqs[i], rel->irqs_srx[i], rel->irqs_stx[i]);
551 for(i = 0, j++; i < cpus; ++i) {
552 mvwprintw(screen, j++, 2, "CPU%d: %14ld irqs", i, abs->irqs[i]);
555 j++;
556 if (iswireless(abs)) {
557 mvwprintw(screen, j++, 2, "LinkQual: %7d/%d (%d/t) ",
558 abs->wifi.link_qual, abs->wifi.link_qual_max, rel->wifi.link_qual);
560 mvwprintw(screen, j++, 2, "Signal: %8d dBm (%d dBm/t) ",
561 abs->wifi.signal_level, rel->wifi.signal_level);
563 mvwprintw(screen, j++, 2, "Noise: %8d dBm (%d dBm/t) ",
564 abs->wifi.noise_level, rel->wifi.noise_level);
565 mvwprintw(screen, j++, 2, "SNR: %8d dBm (%s) ",
566 abs->wifi.signal_level - abs->wifi.noise_level,
567 snr_to_str(abs->wifi.signal_level - abs->wifi.noise_level));
568 j++;
571 if (*first) {
572 mvwprintw(screen, 2, 2, "Collecting data ...");
573 *first = 0;
574 } else {
575 mvwprintw(screen, 2, 2, " ");
578 wrefresh(screen);
579 refresh();
582 static void screen_end(void)
584 endwin();
587 static int screen_main(const char *ifname, uint64_t ms_interval)
589 int first = 1;
591 screen_init(&stats_screen);
593 while (!sigint) {
594 if (getch() == 'q')
595 break;
597 screen_update(stats_screen, ifname, &stats_delta, &stats_new,
598 &first, ms_interval);
600 stats_sample_generic(ifname, ms_interval);
603 screen_end();
605 return 0;
608 static void term_csv(const char *ifname, const struct ifstat *rel,
609 const struct ifstat *abs, uint64_t ms_interval)
611 int cpus, i;
613 printf("%ld ", time(0));
615 printf("%lu ", rel->rx_bytes);
616 printf("%lu ", rel->rx_packets);
617 printf("%lu ", rel->rx_drops);
618 printf("%lu ", rel->rx_errors);
620 printf("%lu ", abs->rx_bytes);
621 printf("%lu ", abs->rx_packets);
622 printf("%lu ", abs->rx_drops);
623 printf("%lu ", abs->rx_errors);
625 printf("%lu ", rel->tx_bytes);
626 printf("%lu ", rel->tx_packets);
627 printf("%lu ", rel->tx_drops);
628 printf("%lu ", rel->tx_errors);
630 printf("%lu ", abs->tx_bytes);
631 printf("%lu ", abs->tx_packets);
632 printf("%lu ", abs->tx_drops);
633 printf("%lu ", abs->tx_errors);
635 printf("%u ", rel->cswitch);
636 printf("%lu ", abs->mem_free);
637 printf("%lu ", abs->mem_total - abs->mem_free);
638 printf("%lu ", abs->mem_total);
639 printf("%u ", abs->procs_run);
640 printf("%u ", abs->procs_iow);
642 cpus = get_number_cpus();
643 bug_on(cpus > MAX_CPUS);
645 for (i = 0; i < cpus; ++i) {
646 printf("%lu ", rel->cpu_user[i]);
647 printf("%lu ", rel->cpu_nice[i]);
648 printf("%lu ", rel->cpu_sys[i]);
649 printf("%lu ", rel->cpu_idle[i]);
650 printf("%lu ", rel->cpu_iow[i]);
652 printf("%lu ", rel->irqs[i]);
653 printf("%lu ", abs->irqs[i]);
655 printf("%lu ", rel->irqs_srx[i]);
656 printf("%lu ", abs->irqs_srx[i]);
658 printf("%lu ", rel->irqs_stx[i]);
659 printf("%lu ", abs->irqs_stx[i]);
662 if (iswireless(abs)) {
663 printf("%u ", rel->wifi.link_qual);
664 printf("%u ", abs->wifi.link_qual);
665 printf("%u ", abs->wifi.link_qual_max);
667 printf("%d ", rel->wifi.signal_level);
668 printf("%d ", abs->wifi.signal_level);
670 printf("%d ", rel->wifi.noise_level);
671 printf("%d ", abs->wifi.noise_level);
674 puts("");
675 fflush(stdout);
678 static void term_csv_header(const char *ifname, const struct ifstat *stats,
679 const struct ifstat *curr, uint64_t ms_interval)
681 int cpus, i, j = 1;
683 printf("# gnuplot dump (#col:description)\n");
684 printf("# sampling interval (t): %lu ms\n", ms_interval);
685 printf("# %d:unixtime ", j++);
687 printf("%d:rx-bytes-per-t ", j++);
688 printf("%d:rx-pkts-per-t ", j++);
689 printf("%d:rx-drops-per-t ", j++);
690 printf("%d:rx-errors-per-t ", j++);
692 printf("%d:rx-bytes ", j++);
693 printf("%d:rx-pkts ", j++);
694 printf("%d:rx-drops ", j++);
695 printf("%d:rx-errors ", j++);
697 printf("%d:tx-bytes-per-t ", j++);
698 printf("%d:tx-pkts-per-t ", j++);
699 printf("%d:tx-drops-per-t ", j++);
700 printf("%d:tx-errors-per-t ", j++);
702 printf("%d:tx-bytes ", j++);
703 printf("%d:tx-pkts ", j++);
704 printf("%d:tx-drops ", j++);
705 printf("%d:tx-errors ", j++);
707 printf("%d:context-switches-per-t ", j++);
708 printf("%d:mem-free ", j++);
709 printf("%d:mem-used ", j++);
710 printf("%d:mem-total ", j++);
711 printf("%d:procs-in-run ", j++);
712 printf("%d:procs-in-iow ", j++);
714 cpus = get_number_cpus();
715 bug_on(cpus > MAX_CPUS);
717 for (i = 0, j = 22; i < cpus; ++i) {
718 printf("%d:cpu%i-usr-per-t ", j++, i);
719 printf("%d:cpu%i-nice-per-t ", j++, i);
720 printf("%d:cpu%i-sys-per-t ", j++, i);
721 printf("%d:cpu%i-idle-per-t ", j++, i);
722 printf("%d:cpu%i-iow-per-t ", j++, i);
724 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
725 printf("%d:cpu%i-net-irqs ", j++, i);
727 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
728 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
729 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
730 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
733 if (iswireless(curr)) {
734 printf("%d:wifi-link-qual-per-t ", j++);
735 printf("%d:wifi-link-qual ", j++);
736 printf("%d:wifi-link-qual-max ", j++);
738 printf("%d:wifi-signal-dbm-per-t ", j++);
739 printf("%d:wifi-signal-dbm ", j++);
741 printf("%d:wifi-noise-dbm-per-t ", j++);
742 printf("%d:wifi-noise-dbm ", j++);
745 puts("");
746 printf("# data:\n");
747 fflush(stdout);
750 static int term_main(const char *ifname, uint64_t ms_interval)
752 int first = 1;
754 do {
755 stats_sample_generic(ifname, ms_interval);
757 if (first) {
758 first = 0;
759 term_csv_header(ifname, &stats_delta, &stats_new,
760 ms_interval);
763 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
764 } while (stats_loop && !sigint);
766 return 0;
769 int main(int argc, char **argv)
771 short ifflags = 0;
772 int c, opt_index, ret, promisc = 0;
773 uint64_t interval = 1000;
774 char *ifname = NULL;
775 int (*func_main)(const char *ifname, uint64_t ms_interval) = screen_main;
777 while ((c = getopt_long(argc, argv, short_options, long_options,
778 &opt_index)) != EOF) {
779 switch (c) {
780 case 'h':
781 help();
782 break;
783 case 'v':
784 version();
785 break;
786 case 'd':
787 ifname = xstrndup(optarg, IFNAMSIZ);
788 break;
789 case 't':
790 interval = strtol(optarg, NULL, 10);
791 break;
792 case 'l':
793 stats_loop = 1;
794 break;
795 case 'p':
796 promisc = 1;
797 break;
798 case 'c':
799 func_main = term_main;
800 break;
801 case '?':
802 switch (optopt) {
803 case 'd':
804 case 't':
805 panic("Option -%c requires an argument!\n",
806 optopt);
807 default:
808 if (isprint(optopt))
809 whine("Unknown option character "
810 "`0x%X\'!\n", optopt);
811 die();
813 default:
814 break;
818 if (argc == 1)
819 help();
821 if (argc == 2)
822 ifname = xstrndup(argv[1], IFNAMSIZ);
823 if (ifname == NULL)
824 panic("No networking device given!\n");
826 if (!strncmp("lo", ifname, IFNAMSIZ))
827 panic("lo is not supported!\n");
828 if (device_mtu(ifname) == 0)
829 panic("This is no networking device!\n");
831 register_signal(SIGINT, signal_handler);
832 register_signal(SIGHUP, signal_handler);
834 if (promisc)
835 ifflags = enter_promiscuous_mode(ifname);
836 ret = func_main(ifname, interval);
837 if (promisc)
838 leave_promiscuous_mode(ifname, ifflags);
840 xfree(ifname);
841 return ret;