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
29/07/04 (Fabian)
-tracemode (-t)
-------------------------------------------------
06/07/04 (Arnout)
-added support for monitoring multiple interfaces
at once

View File

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

View File

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

View File

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