From 20d7f1d7e051c6b021a3f5d088985a74c3370b29 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Tue, 28 Apr 2009 16:27:03 +0300 Subject: Store timestamps. Implement zero-unknown option. * src/graph.c (draw_graph): Use timestamps from queue entries. Implement zero_unknown_option. * src/output.c: Store timestamps in traffic_history entries. * src/readconfig.c (cb_monitor, cb_server): Fix checks for empty ID. (tagr_kw): New keyword cut-out-fraction. (readconfig): Reset cut_out_fraction if it is < 1.0. * src/report.c: Print timestamps. * src/stat.c (cut_out_fraction): New variable. (ovf_t, overflow): Remove (spurious now) arguments. (interpolate, overflow, update_stats): Don't interpolate if time interval between this sample and the lastly taken one is greater than step * cut_out_fraction. * src/tagr.h (TAGR_CUT_OUT): New define. --- src/stat.c | 64 ++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 25 deletions(-) (limited to 'src/stat.c') diff --git a/src/stat.c b/src/stat.c index 813c5cb..434d19e 100644 --- a/src/stat.c +++ b/src/stat.c @@ -27,8 +27,9 @@ #include #include -typedef int (*ovf_t) (struct traffic_history *th, struct traffic_record *tr, - time_t now); +double cut_out_fraction = TAGR_CUT_OUT; + +typedef int (*ovf_t) (struct traffic_history *th, struct traffic_record *tr); void interpolate (queue_t *q, @@ -43,17 +44,19 @@ interpolate (queue_t *q, if (now - last_time <= step) { + th.time = now; th.inrate = inrate; th.outrate = outrate; verbose (3, _("insert %lu %g %g"), next, th.inrate, th.outrate); queue_put (q, &th); if (ovf) - ovf (&th, tr, now); + ovf (&th, tr); return; } for (next = last_time + step; next <= now; next += step) { + th.time = next; th.inrate = (inrate - last_rates->inrate) * (next - last_time) / interval + last_rates->inrate; th.outrate = (outrate - last_rates->outrate) * (next - last_time) @@ -61,44 +64,45 @@ interpolate (queue_t *q, verbose (3, _("insert %lu %g %g"), next, th.inrate, th.outrate); queue_put (q, &th); if (ovf) - ovf (&th, tr, now); + ovf (&th, tr); } } int overflow (struct traffic_history *th, struct traffic_record *tr, - time_t now, ovf_t ovf, struct avg_acc *avg, queue_t *q, - int maxcount, int step) { - if (now - avg->time >= step) + time_t interval = th->time - avg->time; + if (interval >= step) { struct traffic_history *lastp = queue_get_tail (q); - if (lastp) + if (lastp && interval < step * cut_out_fraction) interpolate (q, step, - now, + th->time, avg->time, lastp, - now - avg->time, + interval, avg->inrate, avg->outrate, ovf, tr); else { struct traffic_history tmp; + tmp.time = avg->time; tmp.inrate = avg->inrate; tmp.outrate = avg->outrate; - verbose (3, _("insert %lu %g %g"), now, tmp.inrate, tmp.outrate); + verbose (3, _("insert %lu %g %g"), + tmp.time, tmp.inrate, tmp.outrate); queue_put (q, &tmp); } avg->inrate = avg->outrate = 0; avg->count = 0; - avg->time = now; + avg->time = th->time; } avg->inrate = (avg->count * avg->inrate + th->inrate) / (avg->count + 1); avg->outrate = (avg->count * avg->outrate + th->outrate) / (avg->count + 1); @@ -106,32 +110,32 @@ overflow (struct traffic_history *th, } int -ovf_monthly (struct traffic_history *th, struct traffic_record *tr, time_t now) +ovf_monthly (struct traffic_history *th, struct traffic_record *tr) { verbose (2, _("begin overflow_monthly %lu %g %g"), - now, th->inrate, th->outrate); - overflow (th, tr, now, NULL, &tr->year_avg, &tr->year_hist, - YEAR_COUNT, YEAR_SAMPLE); + th->time, th->inrate, th->outrate); + overflow (th, tr, NULL, &tr->year_avg, &tr->year_hist, + YEAR_SAMPLE); verbose (2, _("end overflow_monthly")); } int -ovf_weekly (struct traffic_history *th, struct traffic_record *tr, time_t now) +ovf_weekly (struct traffic_history *th, struct traffic_record *tr) { verbose (2, _("begin overflow_weekly %lu %g %g"), - now, th->inrate, th->outrate); - overflow (th, tr, now, ovf_monthly, &tr->month_avg, &tr->month_hist, - MONTH_COUNT, MONTH_SAMPLE); + th->time, th->inrate, th->outrate); + overflow (th, tr, ovf_monthly, &tr->month_avg, &tr->month_hist, + MONTH_SAMPLE); verbose (2, _("end overflow_daily")); } int -ovf_daily (struct traffic_history *th, struct traffic_record *tr, time_t now) +ovf_daily (struct traffic_history *th, struct traffic_record *tr) { verbose (2, _("begin overflow_daily %lu %g %g"), - now, th->inrate, th->outrate); - overflow (th, tr, now, ovf_weekly, &tr->week_avg, &tr->week_hist, - WEEK_COUNT, WEEK_SAMPLE); + th->time, th->inrate, th->outrate); + overflow (th, tr, ovf_weekly, &tr->week_avg, &tr->week_hist, + WEEK_SAMPLE); verbose (2, _("end overflow_daily")); } @@ -150,9 +154,15 @@ update_stats (struct monitor *mon, struct traffic_sample *sample, logmsg (L_ERR, _("ignoring zero interval")); return; } + else if (interval < 0) + { + logmsg (L_ERR, _("ignoring negative interval")); + return; + } + inrate = (double) sample->in / interval; outrate = (double) sample->out / interval; - if (lastp) + if (lastp && interval < DAY_SAMPLE * cut_out_fraction) { interpolate (&tr->day_hist, DAY_SAMPLE, @@ -167,6 +177,7 @@ update_stats (struct monitor *mon, struct traffic_sample *sample, { struct traffic_history th; interval = sample->time - tr->last.time; + th.time = sample->time; th.inrate = inrate; th.outrate = outrate; queue_put (&tr->day_hist, &th); @@ -174,6 +185,7 @@ update_stats (struct monitor *mon, struct traffic_sample *sample, tr->last.time = sample->time; tr->last.in = sample->in; tr->last.out = sample->out; + tr->last_rates.time = sample->time; tr->last_rates.inrate = inrate; tr->last_rates.outrate = outrate; } @@ -238,6 +250,7 @@ _convert (queue_t *q, ovf_t ovf, { struct traffic_history th; interval = hp->time - tr->last.time; + th.time = hp->time; th.inrate = inrate; th.outrate = outrate; queue_put (q, &th); @@ -246,6 +259,7 @@ _convert (queue_t *q, ovf_t ovf, tr->last.time = hp->time; tr->last.in = hp->in; tr->last.out = hp->out; + tr->last_rates.time = hp->time; tr->last_rates.inrate = inrate; tr->last_rates.outrate = outrate; } -- cgit v1.2.1