Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add basename support #155 #216

Merged
merged 2 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions doc/nethogs.8
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ nethogs \- Net top tool grouping bandwidth per process
.B nethogs
.RB [ "\-V" ]
.RB [ "\-h" ]
.RB [ "\-b" ]
.RB [ "\-x" ]
.RB [ "\-d seconds" ]
.RB [ "\-v mode" ]
.RB [ "\-c count" ]
Expand All @@ -19,6 +19,7 @@ nethogs \- Net top tool grouping bandwidth per process
.RB [ "\-l" ]
.RB [ "\-f filter" ]
.RB [ "\-C" ]
.RB [ "\-b" ]
.RB [ "\-g period" ]
.RI [device(s)]
.SH DESCRIPTION
Expand All @@ -32,7 +33,7 @@ prints version.
\fB-h\fP
prints available commands usage.
.TP
\fB-b\fP
\fB-x\fP
bughunt mode - implies tracemode.
.TP
\fB-d\fP
Expand Down Expand Up @@ -62,6 +63,9 @@ monitor all devices, even loopback/stopped ones.
\fB-C\fP
capture TCP and UDP.
.TP
\fB-b\fP
Display the program basename.
.TP
\fB-g\fP
garbage collection period in number of refresh. default is 50.
.TP
Expand All @@ -86,6 +90,9 @@ sort by RECEIVED traffic
l
display command line
.TP
b
display the program basename
.TP
m
switch between total (KB, B, MB) and throughput (KB/s, MB/s, GB/s) mode
.RE
Expand Down
14 changes: 14 additions & 0 deletions src/cui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <cstdlib>
#include <pwd.h>
#include <string>
#include <strings.h>
#include <sys/types.h>

#include "nethogs.h"
Expand All @@ -45,6 +46,7 @@ extern bool sortRecv;

extern int viewMode;
extern bool showcommandline;
extern bool showBasename;

extern unsigned refreshlimit;
extern unsigned refreshcount;
Expand All @@ -67,6 +69,8 @@ const char *COLUMN_FORMAT_RECEIVED = "%11.3f";
const char *const desc_view_mode[VIEWMODE_COUNT] = {
"KB/sec", "KB ", "B ", "MB ", "MB/sec", "GB/sec"};

constexpr char FILE_SEPARATOR = '/';

class Line {
public:
Line(const char *name, const char *cmdline, double n_recv_value,
Expand Down Expand Up @@ -152,6 +156,12 @@ static void mvaddstr_truncate_trailing(int row, int col, const char *str,
static void mvaddstr_truncate_cmdline(int row, int col, const char *progname,
const char *cmdline,
std::size_t max_len) {
if (showBasename) {
if (index(progname, FILE_SEPARATOR) != NULL) {
progname = rindex(progname, FILE_SEPARATOR) + 1;
}
}

std::size_t proglen = strlen(progname);
std::size_t max_cmdlen;

Expand Down Expand Up @@ -305,6 +315,10 @@ void ui_tick() {
/* switch mode: total vs kb/s */
viewMode = (viewMode + 1) % VIEWMODE_COUNT;
break;
case 'b':
/* show only the process basename */
showBasename = !showBasename;
break;
}
}

Expand Down
15 changes: 10 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ static void help(bool iserror) {

// output << "usage: nethogs [-V] [-b] [-d seconds] [-t] [-p] [-f (eth|ppp))]
// [device [device [device ...]]]\n";
output << "usage: nethogs [-V] [-h] [-b] [-d seconds] [-v mode] [-c count] "
"[-t] [-p] [-s] [-a] [-l] [-f filter] [-C]"
output << "usage: nethogs [-V] [-h] [-x] [-d seconds] [-v mode] [-c count] "
"[-t] [-p] [-s] [-a] [-l] [-f filter] [-C] [-b]"
"[device [device [device ...]]]\n";
output << " -V : prints version.\n";
output << " -h : prints this help.\n";
output << " -b : bughunt mode - implies tracemode.\n";
output << " -x : bughunt mode - implies tracemode.\n";
output << " -d : delay for update refresh rate in seconds. default "
"is 1.\n";
output << " -v : view mode (0 = KB/s, 1 = total KB, 2 = total B, 3 "
Expand All @@ -48,6 +48,7 @@ static void help(bool iserror) {
output << " -C : capture TCP and UDP.\n";
output << " -g : garbage collection period in number of refresh. "
"default is 50.\n";
output << " -b : Short program name. Displays only the program name.\n";
output << " -f : EXPERIMENTAL: specify string pcap filter (like "
"tcpdump)."
" This may be removed or changed in a future version.\n";
Expand All @@ -59,6 +60,7 @@ static void help(bool iserror) {
output << " s: sort by SENT traffic\n";
output << " r: sort by RECEIVED traffic\n";
output << " l: display command line\n";
output << " b: display the program basename instead of the fullpath\n";
output << " m: switch between total (KB, B, MB) and throughput (KB/s, MB/s, "
"GB/s) mode\n";
}
Expand Down Expand Up @@ -145,15 +147,15 @@ int main(int argc, char **argv) {
int garbage_collection_period = 50;

int opt;
while ((opt = getopt(argc, argv, "Vhbtpsd:v:c:laf:Cg:")) != -1) {
while ((opt = getopt(argc, argv, "Vhxtpsd:v:c:laf:Cbg:")) != -1) {
switch (opt) {
case 'V':
versiondisplay();
exit(0);
case 'h':
help(false);
exit(0);
case 'b':
case 'x':
bughuntmode = true;
tracemode = true;
break;
Expand Down Expand Up @@ -187,6 +189,9 @@ int main(int argc, char **argv) {
case 'C':
catchall = true;
break;
case 'b':
showBasename = true;
break;
case 'g':
garbage_collection_period = (time_t)atoi(optarg);
break;
Expand Down
1 change: 1 addition & 0 deletions src/nethogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ bool bughuntmode = false;
// sort on sent or received?
bool sortRecv = true;
bool showcommandline = false;
bool showBasename = false;
// viewMode: kb/s or total
int viewMode = VIEWMODE_KBPS;
const char version[] = " version " VERSION;
Expand Down