diff --git a/python/bindings.cpp b/python/bindings.cpp index 788d79f..058aaf5 100644 --- a/python/bindings.cpp +++ b/python/bindings.cpp @@ -76,6 +76,8 @@ PYBIND11_MODULE(nethogs, m) { .def_readwrite("device_name", &NethogsMonitorRecord::device_name) .def_readwrite("sent_bytes", &NethogsMonitorRecord::sent_bytes) .def_readwrite("recv_bytes", &NethogsMonitorRecord::recv_bytes) + .def_readwrite("sent_bytes_last", &NethogsMonitorRecord::sent_bytes_last) + .def_readwrite("recv_bytes_last", &NethogsMonitorRecord::sent_bytes_last) .def_readwrite("sent_kbs", &NethogsMonitorRecord::sent_kbs) .def_readwrite("recv_kbs", &NethogsMonitorRecord::recv_kbs); diff --git a/src/libnethogs.cpp b/src/libnethogs.cpp index 2d02d05..1aad548 100644 --- a/src/libnethogs.cpp +++ b/src/libnethogs.cpp @@ -207,10 +207,14 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb) { const u_int32_t uid = curproc->getVal()->getUid(); u_int64_t sent_bytes; u_int64_t recv_bytes; + u_int64_t sent_bytes_last; + u_int64_t recv_bytes_last; + float sent_kbs; float recv_kbs; curproc->getVal()->getkbps(&recv_kbs, &sent_kbs); curproc->getVal()->gettotal(&recv_bytes, &sent_bytes); + curproc->getVal()->getlast(&recv_bytes_last, &sent_bytes_last); // notify update bool const new_data = @@ -239,9 +243,12 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb) { NHM_UPDATE_ONE_FIELD(data.uid, uid) NHM_UPDATE_ONE_FIELD(data.sent_bytes, sent_bytes) NHM_UPDATE_ONE_FIELD(data.recv_bytes, recv_bytes) + NHM_UPDATE_ONE_FIELD(data.sent_bytes_last, sent_bytes_last) + NHM_UPDATE_ONE_FIELD(data.recv_bytes_last, recv_bytes_last) NHM_UPDATE_ONE_FIELD(data.sent_kbs, sent_kbs) NHM_UPDATE_ONE_FIELD(data.recv_kbs, recv_kbs) + #undef NHM_UPDATE_ONE_FIELD if (data_change) { diff --git a/src/libnethogs.h b/src/libnethogs.h index 2ea0167..2974662 100644 --- a/src/libnethogs.h +++ b/src/libnethogs.h @@ -26,6 +26,8 @@ typedef struct NethogsMonitorRecord { const char *device_name; uint64_t sent_bytes; uint64_t recv_bytes; + uint64_t sent_bytes_last; + uint64_t recv_bytes_last; float sent_kbs; float recv_kbs; } NethogsMonitorRecord; diff --git a/src/process.cpp b/src/process.cpp index 724aec9..bf61efd 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -192,6 +192,20 @@ void Process::gettotalb(float *recvd, float *sent) { *recvd = sum_recv; } +/** get only bytes since last request */ +void Process::getlast(u_int64_t *recvd, u_int64_t *sent) { + u_int64_t sum_sent = 0, sum_recv = 0; + gettotal(&sum_recv, &sum_sent); + + *sent = sum_sent - this->sent_last_reported; + *recvd = sum_recv - this->rcvd_last_reported; + + this->sent_last_reported = sum_sent; + this->rcvd_last_reported = sum_recv; +} + + + Process *findProcess(struct prg_node *node) { ProcList *current = processes; while (current != NULL) { diff --git a/src/process.h b/src/process.h index 5396b41..f20638a 100644 --- a/src/process.h +++ b/src/process.h @@ -77,6 +77,8 @@ public: uid = 0; sent_by_closed_bytes = 0; rcvd_by_closed_bytes = 0; + sent_last_reported = 0; + rcvd_last_reported = 0; } void check() { assert(pid >= 0); } @@ -95,6 +97,7 @@ public: void gettotalmb(float *recvd, float *sent); void gettotalkb(float *recvd, float *sent); void gettotalb(float *recvd, float *sent); + void getlast(u_int64_t *recvd, u_int64_t *sent); char *name; char *cmdline; @@ -103,6 +106,9 @@ public: u_int64_t sent_by_closed_bytes; u_int64_t rcvd_by_closed_bytes; + u_int64_t sent_last_reported; + u_int64_t rcvd_last_reported; + ConnList connections; uid_t getUid() { return uid; }