added 'tracemode' - thanks to Fabian

This commit is contained in:
Arnout Engelen
2004-07-30 19:00:20 +00:00
parent 200939f3fc
commit 164f3dc331
4 changed files with 58 additions and 45 deletions

View File

@@ -1,5 +1,10 @@
Changelog Changelog
29/07/04 (Fabian)
-tracemode (-t)
-------------------------------------------------
06/07/04 (Arnout) 06/07/04 (Arnout)
-added support for monitoring multiple interfaces -added support for monitoring multiple interfaces
at once at once

View File

@@ -22,8 +22,9 @@ extern "C" {
#include "process.h" #include "process.h"
#include "refresh.h" #include "refresh.h"
bool needrefresh = true;
unsigned refreshdelay = 1; unsigned refreshdelay = 1;
bool tracemode = false;
bool needrefresh = true;
char * currentdevice = NULL; char * currentdevice = NULL;
@@ -56,7 +57,6 @@ void process (u_char * args, const struct pcap_pkthdr * header, const u_char * m
} }
connection = new Connection (packet); connection = new Connection (packet);
Process * process = getProcess(connection, currentdevice); Process * process = getProcess(connection, currentdevice);
if (needrefresh) if (needrefresh)
{ {
do_refresh(); do_refresh();
@@ -76,9 +76,11 @@ void quit_cb (int i)
void forceExit(const char *msg) void forceExit(const char *msg)
{ {
clear(); if ((!tracemode)&&(!DEBUG)){
endwin(); clear();
std::cerr << msg << std::endl; endwin();
}
std::cerr << msg << std::endl;
exit(0); exit(0);
} }
@@ -93,6 +95,7 @@ static void help(void)
std::cerr << "usage: nethogs [-V] [-d] [device]\n"; std::cerr << "usage: nethogs [-V] [-d] [device]\n";
std::cerr << " -V : prints version.\n"; std::cerr << " -V : prints version.\n";
std::cerr << " -d : delay for update refresh rate in seconds. default is 1.\n"; std::cerr << " -d : delay for update refresh rate in seconds. default is 1.\n";
std::cerr << " -t : tracemode.\n";
std::cerr << " device : device to monitor. default is eth0\n"; std::cerr << " device : device to monitor. default is eth0\n";
} }
@@ -131,6 +134,8 @@ int main (int argc, char** argv)
exit(0); exit(0);
case 'h': help(); case 'h': help();
exit(0); exit(0);
case 't': tracemode = true;
break;
case 'd': if (argv[1]) case 'd': if (argv[1])
{ {
argv++; argv++;
@@ -149,17 +154,17 @@ int main (int argc, char** argv)
if (devices == NULL) if (devices == NULL)
devices = new device (strdup("eth0")); devices = new device (strdup("eth0"));
#if DEBUG
#else if ((!tracemode) && (!DEBUG)){
WINDOW * screen = initscr(); WINDOW * screen = initscr();
raw(); raw();
noecho(); noecho();
cbreak(); cbreak();
nodelay(screen, TRUE); nodelay(screen, TRUE);
#endif caption = new std::string ("NetHogs");
caption = new std::string ("NetHogs"); caption->append(version);
caption->append(version); caption->append(", running at ");
caption->append(", running at "); }
if (NEEDROOT && (getuid() != 0)) if (NEEDROOT && (getuid() != 0))
forceExit("You need to be root to run NetHogs !"); forceExit("You need to be root to run NetHogs !");
@@ -170,8 +175,10 @@ int main (int argc, char** argv)
device * current_dev = devices; device * current_dev = devices;
while (current_dev != NULL) { while (current_dev != NULL) {
getLocal(current_dev->name); getLocal(current_dev->name);
caption->append(current_dev->name); if ((!tracemode) && (!DEBUG)){
caption->append(" "); caption->append(current_dev->name);
caption->append(" ");
}
pcap_t * newhandle = pcap_open_live(current_dev->name, BUFSIZ, 0, 100, errbuf); pcap_t * newhandle = pcap_open_live(current_dev->name, BUFSIZ, 0, 100, errbuf);
if (newhandle != NULL) if (newhandle != NULL)
@@ -185,7 +192,6 @@ int main (int argc, char** argv)
signal (SIGALRM, &alarm_cb); signal (SIGALRM, &alarm_cb);
signal (SIGINT, &quit_cb); signal (SIGINT, &quit_cb);
alarm (refreshdelay); alarm (refreshdelay);
while (1) while (1)
{ {
handle * current_handle = handles; handle * current_handle = handles;
@@ -195,19 +201,20 @@ int main (int argc, char** argv)
pcap_dispatch (current_handle->content, -1, process, NULL); pcap_dispatch (current_handle->content, -1, process, NULL);
current_handle = current_handle->next; current_handle = current_handle->next;
} }
if (!DEBUG) {
switch (getch()) { if ((!DEBUG)&&(!tracemode)) {
case 'q': switch (getch()) {
/* quit */ case 'q':
quit_cb(0); /* quit */
break; quit_cb(0);
case 's': break;
/* sort on 'sent' */ case 's':
break; /* sort on 'sent' */
case 'r': break;
/* sort on 'received' */ case 'r':
break; /* sort on 'received' */
} break;
}
} }
if (needrefresh) if (needrefresh)
{ {

View File

@@ -262,9 +262,12 @@ public:
void show (int row) void show (int row)
{ {
#if DEBUG if (DEBUG || tracemode)
std::cout << m_name << "\t" << sent_kbps << "\t" << recv_kbps << std::endl; {
#else std::cout << m_name << "\t" << sent_kbps << "\t" << recv_kbps << std::endl;
return;
}
mvprintw (3+row, 0, "%d", m_pid); mvprintw (3+row, 0, "%d", m_pid);
char * username = uid2username(m_uid); char * username = uid2username(m_uid);
mvprintw (3+row, 6, "%s", username); mvprintw (3+row, 6, "%s", username);
@@ -274,12 +277,6 @@ public:
mvprintw (3+row, 6 + 9 + PROGNAME_WIDTH + 2 + 6, "%10.3f", sent_kbps); mvprintw (3+row, 6 + 9 + PROGNAME_WIDTH + 2 + 6, "%10.3f", sent_kbps);
mvprintw (3+row, 6 + 9 + PROGNAME_WIDTH + 2 + 6 + 9 + 3, "%10.3f", recv_kbps); mvprintw (3+row, 6 + 9 + PROGNAME_WIDTH + 2 + 6 + 9 + 3, "%10.3f", recv_kbps);
mvprintw (3+row, 6 + 9 + PROGNAME_WIDTH + 2 + 6 + 9 + 3 + 11, "KB/sec", recv_kbps); mvprintw (3+row, 6 + 9 + PROGNAME_WIDTH + 2 + 6 + 9 + 3 + 11, "KB/sec", recv_kbps);
// TODO fix
//if(m_kbps-upload_kbps>upload_kbps)
// mvprintw (3+row, 6 + 20 + PROGNAME_WIDTH + 2, "<<<<");
// else mvprintw (3+row, 6 + 20 + PROGNAME_WIDTH + 2, ">>>>");
#endif
} }
double sent_kbps; double sent_kbps;
@@ -320,7 +317,11 @@ int count_processes()
void do_refresh() void do_refresh()
{ {
if (!DEBUG) if (DEBUG || tracemode)
{
std::cout << "Refreshing:\n";
}
else
{ {
clear(); clear();
mvprintw (0, 0, "%s", caption->c_str()); mvprintw (0, 0, "%s", caption->c_str());
@@ -328,8 +329,6 @@ void do_refresh()
mvprintw (2, 0, " PID USER PROGRAM DEV SENT RECEIVED "); mvprintw (2, 0, " PID USER PROGRAM DEV SENT RECEIVED ");
attroff(A_REVERSE); attroff(A_REVERSE);
} }
else
std::cout << "Refreshing:\n";
ProcList * curproc = processes; ProcList * curproc = processes;
ProcList * lastproc = NULL; ProcList * lastproc = NULL;
int nproc = count_processes(); int nproc = count_processes();
@@ -387,7 +386,7 @@ void do_refresh()
lines[i]->show(i); lines[i]->show(i);
delete lines[i]; delete lines[i];
} }
if (!DEBUG) if ((!tracemode) && (!DEBUG))
refresh(); refresh();
} }

View File

@@ -5,6 +5,8 @@
#include "nethogs.h" #include "nethogs.h"
#include "connection.h" #include "connection.h"
extern bool tracemode;
class ConnList class ConnList
{ {
public: public: