Beispiel #1
0
bool getdata(struct iface *ifa, int cols) {
	static unsigned long long rx, tx;

	if (rx && tx) {
		if (!getcounters(ifa->ifname, &ifa->rx, &ifa->tx))
			return false;

		memmove(ifa->rxs, ifa->rxs+1, sizeof(ifa->rxs) * (cols - 1));
		memmove(ifa->txs, ifa->txs+1, sizeof(ifa->txs) * (cols - 1));

		ifa->rxs[cols - 1] = (ifa->rx - rx) / delay;
		ifa->txs[cols - 1] = (ifa->tx - tx) / delay;

		if (globalmax) {
			ifa->rxmax = arraymax(ifa->rxs, cols, ifa->rxmax);
			ifa->txmax = arraymax(ifa->txs, cols, ifa->txmax);
		} else {
			ifa->rxmax = arraymax(ifa->rxs, cols, 0);
			ifa->txmax = arraymax(ifa->txs, cols, 0);
		}

		ifa->rxavg = arrayavg(ifa->rxs, cols);
		ifa->txavg = arrayavg(ifa->txs, cols);

		ifa->rxmin = arraymin(ifa->rxs, cols);
		ifa->txmin = arraymin(ifa->txs, cols);
	}

	if (!getcounters(ifa->ifname, &rx, &tx))
		return false;
	return true;
}
Beispiel #2
0
void getdata(struct iface *ifa, double delay, int cols) {
	static long long rx, tx;

	if (rx && tx && !resize) {
		getcounters(ifa->ifname, &ifa->rx, &ifa->tx);

		memmove(ifa->rxs, ifa->rxs+1, sizeof(long) * (cols-1));
		memmove(ifa->txs, ifa->txs+1, sizeof(long) * (cols-1));

		ifa->rxs[cols-1] = (ifa->rx - rx) / delay;
		ifa->txs[cols-1] = (ifa->tx - tx) / delay;

		ifa->rxavg = arrayavg(ifa->rxs, cols);
		ifa->txavg = arrayavg(ifa->txs, cols);

		ifa->rxmax = arraymax(ifa->rxs, cols);
		ifa->txmax = arraymax(ifa->txs, cols);
	}

	getcounters(ifa->ifname, &rx, &tx);
}
Beispiel #3
0
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;
}