diff --git a/Changelog b/Changelog index 58fd899..5d2b05a 100644 --- a/Changelog +++ b/Changelog @@ -1,5 +1,15 @@ Changelog +12/05/13 (muzso) +- added new command line switches: + -s Sorts output by the sent column. + -c Limits the number of updates (useful for tracemode and scripting the + output). + -v Sets view mode (0 = KB/s, 1 = total KB, 2 = total B, 3 = total MB) +- changed needrefresh default value from true to false + (upon startup there's no useful info on network usage, so displaying + any results has no use for the user) + 31/08/10 (Arnout) - support for screens wider than 80 characters, thanks to Shock at https://bugs.launchpad.net/ubuntu/+source/nethogs/+bug/627626 diff --git a/cui.cpp b/cui.cpp index 500829b..1e2bd14 100644 --- a/cui.cpp +++ b/cui.cpp @@ -43,15 +43,12 @@ extern Process * unknowntcp; extern Process * unknownudp; extern Process * unknownip; -// sort on sent or received? -bool sortRecv = true; -// viewMode: kb/s or total -int VIEWMODE_KBPS = 0; -int VIEWMODE_TOTAL_KB = 1; -int VIEWMODE_TOTAL_B = 2; -int VIEWMODE_TOTAL_MB = 3; -int viewMode = VIEWMODE_KBPS; -int nViewModes = 4; +extern bool sortRecv; + +extern int viewMode; + +extern unsigned refreshlimit; +extern unsigned refreshcount; class Line { @@ -233,7 +230,7 @@ void ui_tick () break; case 'm': /* switch mode: total vs kb/s */ - viewMode = (viewMode + 1) % nViewModes; + viewMode = (viewMode + 1) % VIEWMODE_COUNT; break; } } @@ -357,6 +354,7 @@ void do_refresh() proglen = cols - 53; refreshconninode(); + refreshcount++; if (DEBUG || tracemode) { std::cout << "\nRefreshing:\n"; @@ -513,6 +511,7 @@ void do_refresh() mvprintw (totalrow+1, 0, ""); refresh(); } + + if (refreshlimit != 0 && refreshcount >= refreshlimit) + quit_cb(0); } - - diff --git a/main.cpp b/main.cpp index 139dd98..86e6078 100644 --- a/main.cpp +++ b/main.cpp @@ -8,13 +8,16 @@ static void versiondisplay(void) static void help(void) { //std::cerr << "usage: nethogs [-V] [-b] [-d seconds] [-t] [-p] [-f (eth|ppp))] [device [device [device ...]]]\n"; - std::cerr << "usage: nethogs [-V] [-b] [-d seconds] [-t] [-p] [device [device [device ...]]]\n"; + std::cerr << "usage: nethogs [-V] [-b] [-d seconds] [-v mode] [-c count] [-t] [-p] [-s] [device [device [device ...]]]\n"; std::cerr << " -V : prints version.\n"; + std::cerr << " -b : bughunt mode - implies tracemode.\n"; std::cerr << " -d : delay for update refresh rate in seconds. default is 1.\n"; + std::cerr << " -v : view mode (0 = KB/s, 1 = total KB, 2 = total B, 3 = total MB). default is 0.\n"; + std::cerr << " -c : number of updates. default is 0 (unlimited).\n"; std::cerr << " -t : tracemode.\n"; //std::cerr << " -f : format of packets on interface, default is eth.\n"; - std::cerr << " -b : bughunt mode - implies tracemode.\n"; std::cerr << " -p : sniff in promiscious mode (not recommended).\n"; + std::cerr << " -s : sort output by sent column.\n"; std::cerr << " device : device(s) to monitor. default is eth0\n"; std::cerr << std::endl; std::cerr << "When nethogs is running, press:\n"; @@ -31,7 +34,7 @@ int main (int argc, char** argv) int promisc = 0; int opt; - while ((opt = getopt(argc, argv, "Vhbtpd:")) != -1) { + while ((opt = getopt(argc, argv, "Vhbtpd:v:c:s")) != -1) { switch(opt) { case 'V': versiondisplay(); @@ -49,8 +52,17 @@ int main (int argc, char** argv) case 'p': promisc = 1; break; + case 's': + sortRecv = false; + break; case 'd': - refreshdelay=atoi(optarg); + refreshdelay = atoi(optarg); + break; + case 'v': + viewMode = atoi(optarg) % VIEWMODE_COUNT; + break; + case 'c': + refreshlimit = atoi(optarg); break; /* case 'f': @@ -81,7 +93,7 @@ int main (int argc, char** argv) init_ui(); } - if (NEEDROOT && (getuid() != 0)) + if (NEEDROOT && (geteuid() != 0)) forceExit(false, "You need to be root to run NetHogs!"); char errbuf[PCAP_ERRBUF_SIZE]; diff --git a/nethogs.8 b/nethogs.8 index f0b64d3..489b303 100644 --- a/nethogs.8 +++ b/nethogs.8 @@ -6,22 +6,31 @@ nethogs \- Net top tool grouping bandwidth per process .SH SYNOPSIS .ft B .B nethogs -.RB [ "\-d" ] .RB [ "\-h" ] -.RB [ "\-p" ] -.RB [ "\-t" ] .RB [ "\-V" ] +.RB [ "\-d" ] +.RB [ "\-v" ] +.RB [ "\-t" ] +.RB [ "\-c" ] +.RB [ "\-p" ] +.RB [ "\-s" ] .RI [device(s)] .SH DESCRIPTION NetHogs is a small 'net top' tool. Instead of breaking the traffic down per protocol or per subnet, like most such tools do, it groups bandwidth by process - and does not rely on a special kernel module to be loaded. So if there's suddenly a lot of network traffic, you can fire up NetHogs and immediately see which PID is causing this, and if it's some kind of spinning process, kill it. .SS Options .TP +\fB-h\fP +display available commands usage. +.TP +\fB-V\fP +prints Version info. +.TP \fB-d\fP delay for refresh rate. .TP -\fB-h\fP -display available commands usage. +\fB-v\fP +select view mode .TP \fB-p\fP sniff in promiscious mode (not recommended). @@ -29,8 +38,11 @@ sniff in promiscious mode (not recommended). \fB-t\fP tracemode. .TP -\fB-V\fP -prints Version info. +\fB-c\fP +limit number of refreshes +.TP +\fB-s\fP +sort by traffic sent .PP .I device(s) to monitor. By default eth0 is being used. diff --git a/nethogs.cpp b/nethogs.cpp index a1920fb..1d4558b 100644 --- a/nethogs.cpp +++ b/nethogs.cpp @@ -52,9 +52,16 @@ extern "C" { extern Process * unknownudp; unsigned refreshdelay = 1; +unsigned refreshlimit = 0; +unsigned refreshcount = 0; +unsigned processlimit = 0; bool tracemode = false; bool bughuntmode = false; -bool needrefresh = true; +bool needrefresh = false; +// sort on sent or received? +bool sortRecv = true; +// viewMode: kb/s or total +int viewMode = VIEWMODE_KBPS; //packet_type packettype = packet_ethernet; //dp_link_type linktype = dp_link_ethernet; const char version[] = " version " VERSION "." SUBVERSION "." MINORVERSION; diff --git a/nethogs.h b/nethogs.h index cbca93b..5b33cbf 100644 --- a/nethogs.h +++ b/nethogs.h @@ -63,6 +63,13 @@ #define PROGNAME_WIDTH 512 +// viewMode: how to represent numbers +#define VIEWMODE_KBPS 0 +#define VIEWMODE_TOTAL_KB 1 +#define VIEWMODE_TOTAL_B 2 +#define VIEWMODE_TOTAL_MB 3 +#define VIEWMODE_COUNT 4 + #define NORETURN __attribute__ ((__noreturn__)) void forceExit(bool success, const char *msg, ...) NORETURN;