The first two patches are fixes, the third one is cleanup and the last one is enhancenment. The last one also cleans up the mess with set_barptr().
Please, apply.
Vitezslav Samel (4): serv.c: updateportent(): count packets even if dport is the same as sport serv.c: writeutslog(): fix the printed rate in non-KBITS mode serv.c: use struct proto_counter to count packets and bytes serv.c: use struct rate to compute traffic flow rate
src/serv.c | 275 ++++++++++++++++++++++++------------------------------------ 1 files changed, 111 insertions(+), 164 deletions(-)
We must count packets even if destination port is the same as source port otherwise we won't have traffic (eg. UDP/123 <--> UDP/123) counters accurate.
Signed-off-by: Vitezslav Samel vitezslav@samel.cz --- src/serv.c | 8 +++----- 1 files changed, 3 insertions(+), 5 deletions(-)
diff --git a/src/serv.c b/src/serv.c index 6777460..7d1470a 100644 --- a/src/serv.c +++ b/src/serv.c @@ -395,11 +395,9 @@ static void updateportent(struct portlist *list, unsigned int protocol, if (dport_listent == NULL) return;
- if (dport_listent != sport_listent) { - dport_listent->count++; - dport_listent->bcount += br; - dport_listent->spans.spanbr += br; - } + dport_listent->count++; + dport_listent->bcount += br; + dport_listent->spans.spanbr += br;
dport_listent->ibcount += br; dport_listent->spans.spanbr_in += br;
inrate should be calculated from ptmp->ibcount and totalrate should be calculated from ptmp->bcount.
Signed-off-by: Vitezslav Samel vitezslav@samel.cz --- src/serv.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/serv.c b/src/serv.c index 7d1470a..502c944 100644 --- a/src/serv.c +++ b/src/serv.c @@ -119,13 +119,13 @@ static void writeutslog(struct portlistent *list, unsigned long nsecs, (float) (now - ptmp->proto_starttime); } else { inrate = - (float) (ptmp->obcount / 1024) / + (float) (ptmp->ibcount / 1024) / (float) (now - ptmp->proto_starttime); outrate = (float) (ptmp->obcount / 1024) / (float) (now - ptmp->proto_starttime); totalrate = - (float) (ptmp->obcount / 1024) / + (float) (ptmp->bcount / 1024) / (float) (now - ptmp->proto_starttime); } }
Signed-off-by: Vitezslav Samel vitezslav@samel.cz --- src/serv.c | 125 ++++++++++++++++++++++++++--------------------------------- 1 files changed, 55 insertions(+), 70 deletions(-)
diff --git a/src/serv.c b/src/serv.c index 502c944..2b2133c 100644 --- a/src/serv.c +++ b/src/serv.c @@ -33,6 +33,7 @@ serv.c - TCP/UDP port statistics module #include "logvars.h" #include "error.h" #include "bar.h" +#include "counters.h"
#define SCROLLUP 0 #define SCROLLDOWN 1 @@ -51,15 +52,10 @@ struct portlistent { unsigned int protocol; char servname[11]; unsigned int idx; - unsigned long long count; - unsigned long long bcount; - unsigned long long icount; - unsigned long long ibcount; - unsigned long long ocount; - unsigned long long obcount; + struct proto_counter serv_count; + struct proto_counter span; time_t starttime; time_t proto_starttime; - struct serv_spans spans; struct portlistent *prev_entry; struct portlistent *next_entry; }; @@ -109,23 +105,23 @@ static void writeutslog(struct portlistent *list, unsigned long nsecs, else { if (units == KBITS) { inrate = - (float) (ptmp->ibcount * 8 / 1000) / + (float) (ptmp->serv_count.proto_in.pc_bytes * 8 / 1000) / (float) (now - ptmp->proto_starttime); outrate = - (float) (ptmp->obcount * 8 / 1000) / + (float) (ptmp->serv_count.proto_out.pc_bytes * 8 / 1000) / (float) (now - ptmp->proto_starttime); totalrate = - (float) (ptmp->bcount * 8 / 1000) / + (float) (ptmp->serv_count.proto_total.pc_bytes * 8 / 1000) / (float) (now - ptmp->proto_starttime); } else { inrate = - (float) (ptmp->ibcount / 1024) / + (float) (ptmp->serv_count.proto_in.pc_bytes / 1024) / (float) (now - ptmp->proto_starttime); outrate = - (float) (ptmp->obcount / 1024) / + (float) (ptmp->serv_count.proto_out.pc_bytes / 1024) / (float) (now - ptmp->proto_starttime); totalrate = - (float) (ptmp->bcount / 1024) / + (float) (ptmp->serv_count.proto_total.pc_bytes / 1024) / (float) (now - ptmp->proto_starttime); } } @@ -135,20 +131,23 @@ static void writeutslog(struct portlistent *list, unsigned long nsecs, else fprintf(fd, "UDP/%s: ", ptmp->servname);
- fprintf(fd, "%llu packets, %llu bytes total", ptmp->count, - ptmp->bcount); + fprintf(fd, "%llu packets, %llu bytes total", + ptmp->serv_count.proto_total.pc_packets, + ptmp->serv_count.proto_total.pc_bytes);
if (totalrate >= 0.0) fprintf(fd, ", %.2f %s", totalrate, dispmode(units));
- fprintf(fd, "; %llu packets, %llu bytes incoming", ptmp->icount, - ptmp->ibcount); + fprintf(fd, "; %llu packets, %llu bytes incoming", + ptmp->serv_count.proto_in.pc_packets, + ptmp->serv_count.proto_in.pc_bytes);
if (inrate >= 0.0) fprintf(fd, ", %.2f %s", inrate, dispmode(units));
- fprintf(fd, "; %llu packets, %llu bytes outgoing", ptmp->ocount, - ptmp->obcount); + fprintf(fd, "; %llu packets, %llu bytes outgoing", + ptmp->serv_count.proto_out.pc_packets, + ptmp->serv_count.proto_out.pc_bytes);
if (outrate >= 0.0) fprintf(fd, ", %.2f %s", outrate, dispmode(units)); @@ -232,9 +231,7 @@ static struct portlistent *addtoportlist(struct portlist *list,
servlook(servnames, htons(port), protocol, ptemp->servname, 10);
- ptemp->count = ptemp->bcount = 0; - ptemp->icount = ptemp->ibcount = 0; - ptemp->ocount = ptemp->obcount = 0; + memset(&ptemp->serv_count, 0, sizeof(ptemp->serv_count));
list->count++; ptemp->idx = list->count; @@ -328,17 +325,17 @@ static void printportent(struct portlist *list, struct portlistent *entry, wprintw(list->win, "/%s ", entry->servname); wattrset(list->win, highattr); wmove(list->win, target_row, 17 * screen_scale); - printlargenum(entry->count, list->win); + printlargenum(entry->serv_count.proto_total.pc_packets, list->win); wmove(list->win, target_row, 27 * screen_scale); - printlargenum(entry->bcount, list->win); + printlargenum(entry->serv_count.proto_total.pc_bytes, list->win); wmove(list->win, target_row, 37 * screen_scale); - printlargenum(entry->icount, list->win); + printlargenum(entry->serv_count.proto_in.pc_packets, list->win); wmove(list->win, target_row, 47 * screen_scale); - printlargenum(entry->ibcount, list->win); + printlargenum(entry->serv_count.proto_in.pc_bytes, list->win); wmove(list->win, target_row, 57 * screen_scale); - printlargenum(entry->ocount, list->win); + printlargenum(entry->serv_count.proto_out.pc_packets, list->win); wmove(list->win, target_row, 67 * screen_scale); - printlargenum(entry->obcount, list->win); + printlargenum(entry->serv_count.proto_out.pc_bytes, list->win); }
static void destroyportlist(struct portlist *list) @@ -376,13 +373,8 @@ static void updateportent(struct portlist *list, unsigned int protocol, if (sport_listent == NULL) return;
- sport_listent->count++; - sport_listent->bcount += br; - sport_listent->spans.spanbr += br; - - sport_listent->obcount += br; - sport_listent->spans.spanbr_out += br; - sport_listent->ocount++; + update_proto_counter(&sport_listent->serv_count, 1, br); + update_proto_counter(&sport_listent->span, 1, br); }
if (goodport(dport, ports)) { @@ -395,13 +387,8 @@ static void updateportent(struct portlist *list, unsigned int protocol, if (dport_listent == NULL) return;
- dport_listent->count++; - dport_listent->bcount += br; - dport_listent->spans.spanbr += br; - - dport_listent->ibcount += br; - dport_listent->spans.spanbr_in += br; - dport_listent->icount++; + update_proto_counter(&dport_listent->serv_count, 0, br); + update_proto_counter(&dport_listent->span, 0, br); } if (sport_listent != NULL || dport_listent != NULL) { if (sport_listent != NULL) @@ -468,22 +455,22 @@ static unsigned long long qp_getkey(struct portlistent *entry, int ch) result = entry->port; break; case 'B': - result = entry->bcount; + result = entry->serv_count.proto_total.pc_bytes; break; case 'O': - result = entry->ibcount; + result = entry->serv_count.proto_in.pc_bytes; break; case 'M': - result = entry->obcount; + result = entry->serv_count.proto_out.pc_bytes; break; case 'P': - result = entry->count; + result = entry->serv_count.proto_total.pc_packets; break; case 'T': - result = entry->icount; + result = entry->serv_count.proto_in.pc_packets; break; case 'F': - result = entry->ocount; + result = entry->serv_count.proto_out.pc_packets; break; }
@@ -746,26 +733,24 @@ static void update_serv_rates(struct portlist *list, WINDOW * win, int actmode,
if (actmode == KBITS) { inrate = - (float) (list->barptr->spans.spanbr_in * 8 / 1000) / + (float) (list->barptr->span.proto_in.pc_bytes * 8 / 1000) / (float) (now - list->barptr->starttime); outrate = - (float) (list->barptr->spans.spanbr_out * 8 / 1000) / + (float) (list->barptr->span.proto_out.pc_bytes * 8 / 1000) / (float) (now - list->barptr->starttime); totalrate = - (float) (list->barptr->spans.spanbr * 8 / 1000) / + (float) (list->barptr->span.proto_total.pc_bytes * 8 / 1000) / (float) (now - list->barptr->starttime); } else { inrate = - (float) (list->barptr->spans.spanbr_in / 1024) / + (float) (list->barptr->span.proto_in.pc_bytes / 1024) / (float) (now - list->barptr->starttime); outrate = - (float) (list->barptr->spans.spanbr_out / 1024) / + (float) (list->barptr->span.proto_out.pc_bytes / 1024) / (float) (now - list->barptr->starttime); totalrate = - (float) (list->barptr->spans.spanbr / 1024) / (float) (now - - list-> - barptr-> - starttime); + (float) (list->barptr->span.proto_total.pc_bytes / 1024) / + (float) (now - list->barptr->starttime); }
wattrset(win, IPSTATLABELATTR); @@ -777,7 +762,7 @@ static void update_serv_rates(struct portlist *list, WINDOW * win, int actmode, mvwprintw(win, 0, 46, "%10.2f", outrate); mvwprintw(win, 0, 61, "%10.2f", totalrate);
- memset(&(list->barptr->spans), 0, sizeof(struct serv_spans)); + memset(&list->barptr->span, 0, sizeof(list->barptr->span)); list->barptr->starttime = time(NULL); *cleared = 0; } @@ -982,8 +967,8 @@ void servmon(char *ifname, const struct OPTIONS *options, set_barptr((void *) &(list.barptr), list.barptr->prev_entry, &(list.barptr->prev_entry->starttime), - &(list.barptr->prev_entry->spans), - sizeof(struct serv_spans), + &list.barptr->prev_entry->span, + sizeof(list.barptr->prev_entry->span), statwin, &statcleared, statx); printportent(&list, serv_tmp, idx);
@@ -1003,8 +988,8 @@ void servmon(char *ifname, const struct OPTIONS *options, set_barptr((void *) &(list.barptr), list.barptr->next_entry, &(list.barptr->next_entry->starttime), - &(list.barptr->next_entry->spans), - sizeof(struct serv_spans), + &list.barptr->next_entry->span, + sizeof(list.barptr->next_entry->span), statwin, &statcleared, statx); printportent(&list,serv_tmp, idx);
@@ -1025,8 +1010,8 @@ void servmon(char *ifname, const struct OPTIONS *options, set_barptr((void *) &(list.barptr), list.lastvisible, &(list.lastvisible->starttime), - &(list.lastvisible->spans), - sizeof(struct serv_spans), + &list.lastvisible->span, + sizeof(list.lastvisible->span), statwin, &statcleared, statx); list.baridx = list.lastvisible->idx - idx + 1;
@@ -1042,8 +1027,8 @@ void servmon(char *ifname, const struct OPTIONS *options, set_barptr((void *) &(list.barptr), list.firstvisible, &(list.firstvisible->starttime), - &(list.firstvisible->spans), - sizeof(struct serv_spans), + &list.firstvisible->span, + sizeof(list.firstvisible->span), statwin, &statcleared, statx); list.baridx = 1;
@@ -1079,8 +1064,8 @@ void servmon(char *ifname, const struct OPTIONS *options, list.firstvisible, &(list.firstvisible-> starttime), - &(list.firstvisible->spans), - sizeof(struct serv_spans), + &list.firstvisible->span, + sizeof(list.firstvisible->span), statwin, &statcleared, statx); list.baridx = 1; @@ -1133,8 +1118,8 @@ void servmon(char *ifname, const struct OPTIONS *options, if ((list.barptr == NULL) && (list.head != NULL)) { set_barptr((void *) &(list.barptr), list.head, &(list.head->starttime), - &(list.head->spans), - sizeof(struct serv_spans), statwin, + &list.head->span, + sizeof(list.head->span), statwin, &statcleared, statx); list.baridx = 1; }
Vitezslav Samel vitezslav@samel.cz writes:
Signed-off-by: Vitezslav Samel vitezslav@samel.cz
src/serv.c | 125 ++++++++++++++++++++++++++--------------------------------- 1 files changed, 55 insertions(+), 70 deletions(-)
sport_listent->ocount++;
update_proto_counter(&sport_listent->serv_count, 1, br);
update_proto_counter(&sport_listent->span, 1, br);
... ... ...
update_proto_counter(&dport_listent->serv_count, 0, br);
update_proto_counter(&dport_listent->span, 0, br);
using 1 and 0 for incoming and outgoing pkts are not good, because I have to dig what that means. Could you please define or enum PKT_IN and PKT_OUT and use them or even better you can use pkt_pkttype and values PACKET_OUTGOING and PACKET_HOST.
On Mon, Jun 04, 2012 at 04:04:14PM +0200, Nikola Pajkovsky wrote:
Vitezslav Samel vitezslav@samel.cz writes:
Signed-off-by: Vitezslav Samel vitezslav@samel.cz
src/serv.c | 125 ++++++++++++++++++++++++++--------------------------------- 1 files changed, 55 insertions(+), 70 deletions(-)
sport_listent->ocount++;
update_proto_counter(&sport_listent->serv_count, 1, br);
update_proto_counter(&sport_listent->span, 1, br);
... ... ...
update_proto_counter(&dport_listent->serv_count, 0, br);
update_proto_counter(&dport_listent->span, 0, br);
using 1 and 0 for incoming and outgoing pkts are not good, because I have to dig what that means. Could you please define or enum PKT_IN and PKT_OUT and use them or even better you can use pkt_pkttype and values PACKET_OUTGOING and PACKET_HOST.
In this case the outgoing param of update_proto_counter() is overloaded: here it means for source port it is outgoing direction (from the port), for destination port it is incoming direction (to the port); so we can't use pkt_pkttype for this (think of iptraf-ng on router, where the packets are forwarded from LAN interface to the ISP interface).
Vita
Vitezslav Samel vitezslav@samel.cz writes:
On Mon, Jun 04, 2012 at 04:04:14PM +0200, Nikola Pajkovsky wrote:
Vitezslav Samel vitezslav@samel.cz writes:
Signed-off-by: Vitezslav Samel vitezslav@samel.cz
src/serv.c | 125 ++++++++++++++++++++++++++--------------------------------- 1 files changed, 55 insertions(+), 70 deletions(-)
sport_listent->ocount++;
update_proto_counter(&sport_listent->serv_count, 1, br);
update_proto_counter(&sport_listent->span, 1, br);
... ... ...
update_proto_counter(&dport_listent->serv_count, 0, br);
update_proto_counter(&dport_listent->span, 0, br);
using 1 and 0 for incoming and outgoing pkts are not good, because I have to dig what that means. Could you please define or enum PKT_IN and PKT_OUT and use them or even better you can use pkt_pkttype and values PACKET_OUTGOING and PACKET_HOST.
In this case the outgoing param of update_proto_counter() is overloaded: here it means for source port it is outgoing direction (from the port), for destination port it is incoming direction (to the port); so we can't use pkt_pkttype for this (think of iptraf-ng on router, where the packets are forwarded from LAN interface to the ISP interface).
we don't understand each other :) What I meant is, that we can reuse values PACKET_OUTGOING and PACKET_HOST from pkt_pkttype to name the direction of packet in counters.
update_proto_counter(&dport_listent->serv_count, PACKET_HOST, br); update_proto_counter(&sport_listent->serv_count, PACKET_OUTGOING, br);
void update_proto_counter(struct proto_counter *proto_counter, int direction, int bytes) { if (!proto_counter) return;
update_pkt_counter(&proto_counter->proto_total, bytes); if (direction == PACKET_OUTGOING) update_pkt_counter(&proto_counter->proto_out, bytes); else update_pkt_counter(&proto_counter->proto_in, bytes); }
to bring more readability in code, or if you don't what to reuse PACKET_* defines, define a new one.
hmm?
Signed-off-by: Vitezslav Samel vitezslav@samel.cz --- src/serv.c | 200 +++++++++++++++++++++++++----------------------------------- 1 files changed, 82 insertions(+), 118 deletions(-)
diff --git a/src/serv.c b/src/serv.c index 2b2133c..8540276 100644 --- a/src/serv.c +++ b/src/serv.c @@ -32,8 +32,8 @@ serv.c - TCP/UDP port statistics module #include "packet.h" #include "logvars.h" #include "error.h" -#include "bar.h" #include "counters.h" +#include "rate.h"
#define SCROLLUP 0 #define SCROLLDOWN 1 @@ -54,8 +54,14 @@ struct portlistent { unsigned int idx; struct proto_counter serv_count; struct proto_counter span; - time_t starttime; - time_t proto_starttime; + + struct timeval starttime; + struct timeval proto_starttime; + + struct rate rate; + struct rate rate_in; + struct rate rate_out; + struct portlistent *prev_entry; struct portlistent *next_entry; }; @@ -92,39 +98,17 @@ static void writeutslog(struct portlistent *list, unsigned long nsecs, { char atime[TIME_TARGET_MAX]; struct portlistent *ptmp = list; - float inrate, outrate, totalrate; - time_t now = time(NULL); + struct timeval now; + + gettimeofday(&now, NULL);
genatime(time(NULL), atime);
fprintf(fd, "\n*** TCP/UDP traffic log, generated %s\n\n", atime);
while (ptmp != NULL) { - if (now - ptmp->proto_starttime < 5) - inrate = outrate = totalrate = -1.0; - else { - if (units == KBITS) { - inrate = - (float) (ptmp->serv_count.proto_in.pc_bytes * 8 / 1000) / - (float) (now - ptmp->proto_starttime); - outrate = - (float) (ptmp->serv_count.proto_out.pc_bytes * 8 / 1000) / - (float) (now - ptmp->proto_starttime); - totalrate = - (float) (ptmp->serv_count.proto_total.pc_bytes * 8 / 1000) / - (float) (now - ptmp->proto_starttime); - } else { - inrate = - (float) (ptmp->serv_count.proto_in.pc_bytes / 1024) / - (float) (now - ptmp->proto_starttime); - outrate = - (float) (ptmp->serv_count.proto_out.pc_bytes / 1024) / - (float) (now - ptmp->proto_starttime); - totalrate = - (float) (ptmp->serv_count.proto_total.pc_bytes / 1024) / - (float) (now - ptmp->proto_starttime); - } - } + unsigned long secs = timeval_diff_msec(&now, &ptmp->proto_starttime) / 1000UL; + char bps_string[64];
if (ptmp->protocol == IPPROTO_TCP) fprintf(fd, "TCP/%s: ", ptmp->servname); @@ -135,22 +119,25 @@ static void writeutslog(struct portlistent *list, unsigned long nsecs, ptmp->serv_count.proto_total.pc_packets, ptmp->serv_count.proto_total.pc_bytes);
- if (totalrate >= 0.0) - fprintf(fd, ", %.2f %s", totalrate, dispmode(units)); + rate_print(ptmp->serv_count.proto_total.pc_bytes / secs, units, + bps_string, sizeof(bps_string)); + fprintf(fd, ", %s", bps_string);
fprintf(fd, "; %llu packets, %llu bytes incoming", ptmp->serv_count.proto_in.pc_packets, ptmp->serv_count.proto_in.pc_bytes);
- if (inrate >= 0.0) - fprintf(fd, ", %.2f %s", inrate, dispmode(units)); + rate_print(ptmp->serv_count.proto_in.pc_bytes / secs, units, + bps_string, sizeof(bps_string)); + fprintf(fd, ", %s", bps_string);
fprintf(fd, "; %llu packets, %llu bytes outgoing", ptmp->serv_count.proto_out.pc_packets, ptmp->serv_count.proto_out.pc_bytes);
- if (outrate >= 0.0) - fprintf(fd, ", %.2f %s", outrate, dispmode(units)); + rate_print(ptmp->serv_count.proto_out.pc_bytes / secs, units, + bps_string, sizeof(bps_string)); + fprintf(fd, ", %s", bps_string);
fprintf(fd, "\n\n"); ptmp = ptmp->next_entry; @@ -224,6 +211,9 @@ static struct portlistent *addtoportlist(struct portlist *list,
ptemp->protocol = protocol; ptemp->port = port; /* This is used in checks later. */ + rate_init(&ptemp->rate, 5); + rate_init(&ptemp->rate_in, 5); + rate_init(&ptemp->rate_out, 5);
/* * Obtain appropriate service name @@ -236,7 +226,7 @@ static struct portlistent *addtoportlist(struct portlist *list, list->count++; ptemp->idx = list->count;
- ptemp->proto_starttime = time(NULL); + gettimeofday(&ptemp->proto_starttime, NULL);
if (list->count <= LINES - 5) list->lastvisible = ptemp; @@ -347,6 +337,9 @@ static void destroyportlist(struct portlist *list) ctmp = list->head->next_entry;
while (ptmp != NULL) { + rate_destroy(&ptmp->rate_out); + rate_destroy(&ptmp->rate_in); + rate_destroy(&ptmp->rate); free(ptmp); ptmp = ctmp;
@@ -725,46 +718,35 @@ static void show_portsort_keywin(WINDOW ** win, PANEL ** panel) doupdate(); }
-static void update_serv_rates(struct portlist *list, WINDOW * win, int actmode, - int *cleared) +static void print_serv_rates(struct portlistent *ple, WINDOW *win, int actmode) { - float inrate, outrate, totalrate; - time_t now = time(NULL); - - if (actmode == KBITS) { - inrate = - (float) (list->barptr->span.proto_in.pc_bytes * 8 / 1000) / - (float) (now - list->barptr->starttime); - outrate = - (float) (list->barptr->span.proto_out.pc_bytes * 8 / 1000) / - (float) (now - list->barptr->starttime); - totalrate = - (float) (list->barptr->span.proto_total.pc_bytes * 8 / 1000) / - (float) (now - list->barptr->starttime); - } else { - inrate = - (float) (list->barptr->span.proto_in.pc_bytes / 1024) / - (float) (now - list->barptr->starttime); - outrate = - (float) (list->barptr->span.proto_out.pc_bytes / 1024) / - (float) (now - list->barptr->starttime); - totalrate = - (float) (list->barptr->span.proto_total.pc_bytes / 1024) / - (float) (now - list->barptr->starttime); - } + char buf[64];
wattrset(win, IPSTATLABELATTR); - mvwprintw(win, 0, 1, - "Protocol data rates (%s): in out total", - dispmode(actmode)); + mvwprintw(win, 0, 1, "Protocol data rates:"); + mvwprintw(win, 0, 36, "total"); + mvwprintw(win, 0, 57, "in"); + mvwprintw(win, 0, 76, "out"); + wattrset(win, IPSTATATTR); - mvwprintw(win, 0, 31, "%10.2f", inrate); - mvwprintw(win, 0, 46, "%10.2f", outrate); - mvwprintw(win, 0, 61, "%10.2f", totalrate); + rate_print(rate_get_average(&ple->rate), actmode, buf, sizeof(buf)); + mvwprintw(win, 0, 21, "%s", buf); + rate_print(rate_get_average(&ple->rate_in), actmode, buf, sizeof(buf)); + mvwprintw(win, 0, 42, "%s", buf); + rate_print(rate_get_average(&ple->rate_out), actmode, buf, sizeof(buf)); + mvwprintw(win, 0, 61, "%s", buf); +}
- memset(&list->barptr->span, 0, sizeof(list->barptr->span)); - list->barptr->starttime = time(NULL); - *cleared = 0; +static void update_serv_rates(struct portlist *list, unsigned long msecs) +{ + /* update rates of all portlistents */ + for (struct portlistent *ple = list->head; ple != NULL; ple = ple->next_entry) { + rate_add_rate(&ple->rate, ple->span.proto_total.pc_bytes, msecs); + rate_add_rate(&ple->rate_in, ple->span.proto_in.pc_bytes, msecs); + rate_add_rate(&ple->rate_out, ple->span.proto_out.pc_bytes, msecs); + + memset(&ple->span, 0, sizeof(ple->span)); + } }
/* @@ -785,6 +767,7 @@ void servmon(char *ifname, const struct OPTIONS *options, unsigned int dport = 0;
struct timeval tv; + struct timeval tv_rate; time_t starttime, startlog, timeint; time_t now; unsigned long long unow; @@ -797,7 +780,6 @@ void servmon(char *ifname, const struct OPTIONS *options,
struct portlist list; struct portlistent *serv_tmp; - int statcleared = 0;
FILE *logfile = NULL;
@@ -811,8 +793,6 @@ void servmon(char *ifname, const struct OPTIONS *options,
char sp_buf[10];
- const int statx = 1; - int fd;
struct porttab *ports; @@ -887,6 +867,7 @@ void servmon(char *ifname, const struct OPTIONS *options,
exitloop = 0; gettimeofday(&tv, NULL); + tv_rate = tv; starttime = startlog = timeint = tv.tv_sec;
wattrset(statwin, IPSTATATTR); @@ -925,12 +906,17 @@ void servmon(char *ifname, const struct OPTIONS *options, } }
- if (list.barptr != NULL) { - if ((now - list.barptr->starttime) >= 5) { - update_serv_rates(&list, statwin, - options->actmode, - &statcleared); - } + unsigned long rate_msecs = timeval_diff_msec(&tv, &tv_rate); + if (rate_msecs >= 1000) { + /* update all portlistent rates ... */ + update_serv_rates(&list, rate_msecs); + + /* ... and print the current one */ + if (list.barptr != NULL) + print_serv_rates(list.barptr, statwin, + options->actmode); + + tv_rate = tv; }
if (((options->updrate != 0) @@ -964,12 +950,7 @@ void servmon(char *ifname, const struct OPTIONS *options, break;
serv_tmp = list.barptr; - set_barptr((void *) &(list.barptr), - list.barptr->prev_entry, - &(list.barptr->prev_entry->starttime), - &list.barptr->prev_entry->span, - sizeof(list.barptr->prev_entry->span), - statwin, &statcleared, statx); + list.barptr = list.barptr->prev_entry; printportent(&list, serv_tmp, idx);
if (list.baridx == 1) @@ -978,6 +959,8 @@ void servmon(char *ifname, const struct OPTIONS *options, list.baridx--;
printportent(&list, list.barptr, idx); + + print_serv_rates(list.barptr, statwin, options->actmode); break; case KEY_DOWN: if (!list.barptr @@ -985,12 +968,7 @@ void servmon(char *ifname, const struct OPTIONS *options, break;
serv_tmp = list.barptr; - set_barptr((void *) &(list.barptr), - list.barptr->next_entry, - &(list.barptr->next_entry->starttime), - &list.barptr->next_entry->span, - sizeof(list.barptr->next_entry->span), - statwin, &statcleared, statx); + list.barptr = list.barptr->next_entry; printportent(&list,serv_tmp, idx);
if (list.baridx == list.imaxy) @@ -999,6 +977,8 @@ void servmon(char *ifname, const struct OPTIONS *options, list.baridx++;
printportent(&list, list.barptr, idx); + + print_serv_rates(list.barptr, statwin, options->actmode); break; case KEY_PPAGE: case '-': @@ -1007,15 +987,12 @@ void servmon(char *ifname, const struct OPTIONS *options,
pageservwin(&list, SCROLLDOWN, &idx);
- set_barptr((void *) &(list.barptr), - list.lastvisible, - &(list.lastvisible->starttime), - &list.lastvisible->span, - sizeof(list.lastvisible->span), - statwin, &statcleared, statx); + list.barptr = list.lastvisible; list.baridx = list.lastvisible->idx - idx + 1;
refresh_serv_screen(&list, idx); + + print_serv_rates(list.barptr, statwin, options->actmode); break; case KEY_NPAGE: case ' ': @@ -1024,16 +1001,12 @@ void servmon(char *ifname, const struct OPTIONS *options,
pageservwin(&list, SCROLLUP, &idx);
- set_barptr((void *) &(list.barptr), - list.firstvisible, - &(list.firstvisible->starttime), - &list.firstvisible->span, - sizeof(list.firstvisible->span), - statwin, &statcleared, statx); + list.barptr = list.firstvisible; list.baridx = 1;
refresh_serv_screen(&list, idx);
+ print_serv_rates(list.barptr, statwin, options->actmode); break; case 12: case 'l': @@ -1060,15 +1033,9 @@ void servmon(char *ifname, const struct OPTIONS *options, sortportents(&list, &idx, ch); keymode = 0; if (list.barptr != NULL) { - set_barptr((void *) &(list.barptr), - list.firstvisible, - &(list.firstvisible-> - starttime), - &list.firstvisible->span, - sizeof(list.firstvisible->span), - statwin, &statcleared, - statx); + list.barptr = list.firstvisible; list.baridx = 1; + print_serv_rates(list.barptr, statwin, options->actmode); } refresh_serv_screen(&list, idx); update_panels(); @@ -1116,12 +1083,9 @@ void servmon(char *ifname, const struct OPTIONS *options, continue; } if ((list.barptr == NULL) && (list.head != NULL)) { - set_barptr((void *) &(list.barptr), list.head, - &(list.head->starttime), - &list.head->span, - sizeof(list.head->span), statwin, - &statcleared, statx); + list.barptr = list.head; list.baridx = 1; + print_serv_rates(list.barptr, statwin, options->actmode); } }
iptraf-ng@lists.fedorahosted.org