int main(int argc, char **argv) { int i; int linesold, colsold; int graphlines = 0; double delay = 0.5; char key; struct iface ifa; WINDOW *title, *rxgraph, *txgraph, *stats; bool colors = true; bool siunits = false; bool hidescale = false; bool syncgraphmax = false; bool fixedlines = false; memset(&ifa, 0, sizeof ifa); for (i = 1; i < argc; i++) { if (!strcmp("-v", argv[i])) eprintf("%s-%s\n", argv[0], VERSION); else if (!strcmp("-C", argv[i])) colors = false; else if (!strcmp("-s", argv[i])) siunits = true; else if (!strcmp("-S", argv[i])) hidescale = true; else if (!strcmp("-m", argv[i])) syncgraphmax = true; else if (argv[i+1] == NULL || argv[i+1][0] == '-') usage(argv); else if (!strcmp("-d", argv[i])) delay = estrtod(argv[++i]); else if (!strcmp("-i", argv[i])) strlcpy(ifa.ifname, argv[++i], IFNAMSIZ); else if (!strcmp("-l", argv[i])) { graphlines = estrtol(argv[++i]); fixedlines = true; } } if (ifa.ifname[0] == '\0') detectiface(ifa.ifname); initscr(); curs_set(0); noecho(); keypad(stdscr, TRUE); timeout(delay * 1000); if (colors && has_colors()) { start_color(); use_default_colors(); init_pair(1, COLOR_GREEN, -1); init_pair(2, COLOR_RED, -1); } signal(SIGWINCH, sighandler); ifa.rxs = ecalloc(COLS, sizeof(long)); ifa.txs = ecalloc(COLS, sizeof(long)); mvprintw(0, 0, "collecting data from %s for %.2f seconds\n", ifa.ifname, delay); if (!fixedlines) graphlines = (LINES-5)/2; title = newwin(1, COLS, 0, 0); rxgraph = newwin(graphlines, COLS, 1, 0); txgraph = newwin(graphlines, COLS, graphlines+1, 0); stats = newwin(LINES-(graphlines*2+1), COLS, graphlines*2+1, 0); getdata(&ifa, delay, COLS); while ((key = getch()) != 'q') { if (key != ERR) resize = 1; getdata(&ifa, delay, COLS); if (syncgraphmax) ifa.rxmax = ifa.txmax = MAX(ifa.rxmax, ifa.txmax); if (resize) { linesold = LINES; colsold = COLS; endwin(); refresh(); if (COLS != colsold) { arrayresize(&ifa.rxs, COLS, colsold); arrayresize(&ifa.txs, COLS, colsold); } if (LINES != linesold && !fixedlines) graphlines = (LINES-5)/2; wresize(title, 1, COLS); wresize(rxgraph, graphlines, COLS); wresize(txgraph, graphlines, COLS); wresize(stats, LINES-(graphlines*2+1), COLS); mvwin(txgraph, graphlines+1, 0); mvwin(stats, graphlines*2+1, 0); resize = 0; } werase(title); mvwprintw(title, 0, COLS/2-7, "interface: %s\n", ifa.ifname); wnoutrefresh(title); printgraphw(rxgraph, ifa.rxs, ifa.rxmax, siunits, graphlines, COLS, hidescale, COLOR_PAIR(1)); printgraphw(txgraph, ifa.txs, ifa.txmax, siunits, graphlines, COLS, hidescale, COLOR_PAIR(2)); printstatsw(stats, ifa, siunits, COLS); doupdate(); } delwin(title); delwin(rxgraph); delwin(txgraph); delwin(stats); endwin(); return EXIT_SUCCESS; }
int main(int argc, char **argv) { int key; int colsold; int graphlines; struct iface ifa; WINDOW *title, *rxgraph, *txgraph, *rxstats, *txstats; bool colors = true; bool siunits = false; double delay = 0.5; memset(&ifa, 0, sizeof(ifa)); ARGBEGIN { case 'v': eprintf("%s-%s\n", argv0, VERSION); case 'C': colors = false; break; case 's': siunits = true; break; case 'd': delay = estrtod(EARGF(usage())); break; case 'i': strlcpy(ifa.ifname, EARGF(usage()), IFNAMSIZ); break; default: usage(); } ARGEND; if (!detectiface(ifa.ifname)) eprintf("can't find network interface\n"); initscr(); curs_set(0); noecho(); keypad(stdscr, TRUE); timeout(delay * 1000); if (colors && has_colors()) { start_color(); use_default_colors(); init_pair(1, COLOR_GREEN, -1); init_pair(2, COLOR_RED, -1); } signal(SIGWINCH, sighandler); mvprintw(0, 0, "collecting data from %s for %.2f seconds\n", ifa.ifname, delay); ifa.rxs = ecalloc(COLS - 3, sizeof(*ifa.rxs)); ifa.txs = ecalloc(COLS - 3, sizeof(*ifa.txs)); graphlines = (LINES - 7) / 2; title = newwin(1, COLS, 0, 0); rxgraph = newwin(graphlines, COLS, 1, 0); txgraph = newwin(graphlines, COLS, graphlines + 1, 0); rxstats = newwin(LINES - (graphlines * 2 + 1), COLS / 2, graphlines * 2 + 1, 0); txstats = newwin(LINES - (graphlines * 2 + 1), COLS - COLS / 2, graphlines * 2 + 1, COLS / 2); if (!getdata(&ifa, delay, COLS - 3)) eprintf("can't read rx and tx bytes for %s\n", ifa.ifname); while ((key = getch()) != 'q') { if (key != ERR) resize = 1; if (!getdata(&ifa, delay, COLS - 3)) eprintf("can't read rx and tx bytes for %s\n", ifa.ifname); if (resize) { colsold = COLS; endwin(); refresh(); arrayresize(&ifa.rxs, COLS - 3, colsold - 3); arrayresize(&ifa.txs, COLS - 3, colsold - 3); graphlines = (LINES - 7) / 2; wresize(title, 1, COLS); wresize(rxgraph, graphlines, COLS); wresize(txgraph, graphlines, COLS); wresize(rxstats, LINES - (graphlines * 2 + 1), COLS / 2); wresize(txstats, LINES - (graphlines * 2 + 1), COLS - COLS / 2); mvwin(txgraph, graphlines + 1, 0); mvwin(rxstats, graphlines * 2 + 1, 0); mvwin(txstats, graphlines * 2 + 1, COLS / 2); resize = 0; } werase(title); mvwprintw(title, 0, COLS / 2 - 8, "[ interface: %s ]", ifa.ifname); wnoutrefresh(title); printgraphw(rxgraph, "Received", ifa.rxs, ifa.rxmax, siunits, graphlines, COLS, COLOR_PAIR(1)); printgraphw(txgraph, "Transmitted", ifa.txs, ifa.txmax, siunits, graphlines, COLS, COLOR_PAIR(2)); printstatsw(rxstats, "Received", ifa.rxs[COLS - 4], ifa.rxavg, ifa.rxmax, ifa.rx, siunits, COLS / 2); printstatsw(txstats, "Transmitted", ifa.txs[COLS - 4], ifa.txavg, ifa.txmax, ifa.tx, siunits, COLS - COLS / 2); doupdate(); } delwin(title); delwin(rxgraph); delwin(txgraph); delwin(rxstats); delwin(txstats); endwin(); free(ifa.rxs); free(ifa.txs); return EXIT_SUCCESS; }
int main(int argc, char **argv) { int y, x; int oldy, oldx; int graphy; int xhalf, graphyx2; int key; bool redraw = true; bool erase = true; bool changedelay = false; long timer = 0; struct timeval tv; struct iface ifa; WINDOW *rxgraph, *txgraph, *rxstats, *txstats; memset(&ifa, 0, sizeof(ifa)); ARGBEGIN { case 'v': eprintf("nbwmon-%s\n", VERSION); case 'C': colors = false; break; case 's': siunits = true; break; case 'm': minimum = true; break; case 'g': globalmax = true; break; case 'd': delay = estrtod(EARGF(usage())); break; case 'i': strlcpy(ifa.ifname, EARGF(usage()), IFNAMSIZ); break; default: usage(); } ARGEND; if (!detectiface(ifa.ifname)) eprintf("Can't find network interface\n"); if (!getcounters(ifa.ifname, &ifa.rx, &ifa.tx)) eprintf("Can't read rx and tx bytes for %s\n", ifa.ifname); initscr(); curs_set(FALSE); noecho(); timeout(10); if (colors && has_colors()) { start_color(); use_default_colors(); init_pair(1, COLOR_GREEN, -1); init_pair(2, COLOR_RED, -1); } getmaxyx(stdscr, y, x); getmaxyx(stdscr, oldy, oldx); ifa.rxs = ecalloc(x - 3, sizeof(*ifa.rxs)); ifa.txs = ecalloc(x - 3, sizeof(*ifa.txs)); graphy = (y - 7) / 2; rxgraph = newwin(graphy, x, 0, 0); txgraph = newwin(graphy, x, graphy, 0); rxstats = newwin(y - graphy * 2, x / 2, graphy * 2, 0); txstats = newwin(y - graphy * 2, x - x / 2, graphy * 2, x / 2); for (key = ERR; key != 'q'; key = getch()) { if (key != ERR) redraw = true; switch (key) { case 's': siunits = !siunits; break; case 'm': minimum = !minimum; break; case 'g': globalmax = !globalmax; break; case '+': if (delay < 8) { delay *= 2; changedelay = true; } break; case '-': if (delay > 0.25) { delay /= 2; changedelay = true; } break; } if (y < 11 || x < 44) { werase(stdscr); addstr("terminal too small"); wrefresh(stdscr); if (key == KEY_RESIZE) getmaxyx(stdscr, y, x); erase = true; continue; } if (erase) { werase(stdscr); wnoutrefresh(stdscr); erase = false; } if (oldy != y || oldx != x) { graphy = (y - 7) / 2; graphyx2 = graphy * 2; xhalf = x / 2; wresize(rxgraph, graphy, x); wresize(txgraph, graphy, x); wresize(rxstats, y - graphyx2, xhalf); wresize(txstats, y - graphyx2, x - xhalf); mvwin(rxgraph, 0, 0); mvwin(txgraph, graphy, 0); mvwin(rxstats, graphyx2, 0); mvwin(txstats, graphyx2, xhalf); if (oldx != x) { arrayresize(&ifa.rxs, x - 3, oldx - 3); arrayresize(&ifa.txs, x - 3, oldx - 3); } redraw = true; } gettimeofday(&tv, NULL); tv.tv_usec = (tv.tv_sec * 1000 + tv.tv_usec / 1000) / (delay * 1000.0); if (changedelay) { timer = tv.tv_usec; changedelay = false; } if (timer != tv.tv_usec) { timer = tv.tv_usec; if (!getdata(&ifa, x - 3)) eprintf("Can't read rx and tx bytes for %s\n", ifa.ifname); redraw = true; } if (redraw) { printgraphw(rxgraph, "Received", ifa.ifname, COLOR_PAIR(1), ifa.rxs, ifa.rxmin, ifa.rxmax); printgraphw(txgraph, "Transmitted", NULL, COLOR_PAIR(2), ifa.txs, ifa.txmin, ifa.txmax); printstatsw(rxstats, "Received", ifa.rxs[x - 4], ifa.rxmin, ifa.rxavg, ifa.rxmax, ifa.rx); printstatsw(txstats, "Transmitted", ifa.txs[x - 4], ifa.txmin, ifa.txavg, ifa.txmax, ifa.tx); doupdate(); redraw = false; } oldy = y; oldx = x; if (key == KEY_RESIZE) getmaxyx(stdscr, y, x); } delwin(rxgraph); delwin(txgraph); delwin(rxstats); delwin(txstats); endwin(); free(ifa.rxs); free(ifa.txs); return EXIT_SUCCESS; }