diff options
author | Sergey Poznyakoff <gray@Pirx.gnu.org.ua> | 2009-04-28 16:27:03 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@Pirx.gnu.org.ua> | 2009-04-28 16:27:03 +0300 |
commit | 20d7f1d7e051c6b021a3f5d088985a74c3370b29 (patch) | |
tree | 5c5dc1c1b401e055096b8ec216a6bd97eb76a520 /src | |
parent | 29e4b3da0990aca9db232dc3ce0a57722ccf1cac (diff) | |
download | tagr-20d7f1d7e051c6b021a3f5d088985a74c3370b29.tar.gz tagr-20d7f1d7e051c6b021a3f5d088985a74c3370b29.tar.bz2 |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/graph.c | 46 | ||||
-rw-r--r-- | src/output.c | 3 | ||||
-rw-r--r-- | src/readconfig.c | 16 | ||||
-rw-r--r-- | src/report.c | 23 | ||||
-rw-r--r-- | src/stat.c | 64 | ||||
-rw-r--r-- | src/tagr.h | 8 |
6 files changed, 110 insertions, 50 deletions
diff --git a/src/graph.c b/src/graph.c index 8088773..1197a7d 100644 --- a/src/graph.c +++ b/src/graph.c @@ -90,6 +90,7 @@ draw_graph (FILE *fp, int full_ysize = graph_ysize + graph_v_margin[0] + graph_v_margin[1]; grid_t grid; unsigned long ymax = mon->max_rate; + time_t start; yscale = (double) graph_ysize / ymax; xscale = (double) graph_xsize / xmax; @@ -142,38 +143,65 @@ draw_graph (FILE *fp, n = queue_count (dataq); /* Incoming traffic */ - for (i = n - 1, x = 0; i > 0 && x < xmax; i--, x += xstep) + for (i = n - 1, start = 0; i > 0; i--) { struct traffic_history th, tnext; scale_sample (mon, queue_get_ptr (dataq, i), &th); + if (start == 0) + start = th.time; scale_sample (mon, queue_get_ptr (dataq, i - 1), &tnext); + if (start - tnext.time > xmax) + break; + if (zero_unknown_option + && th.time - tnext.time > xstep * cut_out_fraction) + { + gdImageLine (graph, xtr (start - tnext.time), ytr (0), + xtr (start - tnext.time), ytr (tnext.inrate), i_in); + th.inrate = tnext.inrate = 0; + th.outrate = tnext.outrate = 0; + } + if (fill_incoming_option) gdImageLine (graph, - xtr (x), ytr (0), - xtr (x), ytr (tnext.inrate), i_in); - gdImageLine (graph, xtr (x), ytr (th.inrate), - xtr (x + xstep), ytr (tnext.inrate), i_in); + xtr (start - th.time), ytr (0), + xtr (start - th.time), ytr (tnext.inrate), i_in); + gdImageLine (graph, xtr (start - th.time), ytr (th.inrate), + xtr (start - tnext.time), ytr (tnext.inrate), i_in); } /* Outgoing traffic */ gdImageSetBrush (graph, brush_out); - for (i = n - 1, x = 0; i > 0 && x < xmax; i--, x += xstep) + for (i = n - 1, start = 0; i > 0; i--) { struct traffic_history th, tnext; scale_sample (mon, queue_get_ptr (dataq, i), &th); + if (start == 0) + start = th.time; scale_sample (mon, queue_get_ptr (dataq, i - 1), &tnext); + if (start - tnext.time > xmax) + break; - gdImageLine (graph, xtr (x), ytr (th.outrate), - xtr (x + xstep), ytr (tnext.outrate), gdBrushed); + if (zero_unknown_option + && th.time - tnext.time > xstep * cut_out_fraction) + { + gdImageLine (graph, xtr (start - tnext.time), ytr (0), + xtr (start - tnext.time), ytr (tnext.inrate), + gdBrushed); + th.inrate = tnext.inrate = 0; + th.outrate = tnext.outrate = 0; + } + + gdImageLine (graph, xtr (start - th.time), ytr (th.outrate), + xtr (start - tnext.time), ytr (tnext.outrate), gdBrushed); } /* Border */ gdImageRectangle (graph, xtr (0), ytr (0), xtr (xmax), ytr (ymax), i_grid); - + dotted_style[0] = i_grid; dotted_style[1] = gdTransparent; diff --git a/src/output.c b/src/output.c index e376ad4..4b6e206 100644 --- a/src/output.c +++ b/src/output.c @@ -41,6 +41,7 @@ scale_sample (struct monitor *mon, out->inrate = in->inrate; out->outrate = in->outrate; } + out->time = in->time; out->inrate *= mon->scale; out->outrate *= mon->scale; } @@ -230,7 +231,7 @@ format_timestamp (FILE *fp, union value v, const char *fmt, int prec) if (!fmt) fmt = "%c"; - fprintftime(fp, fmt, tm, 0, 0); + fprintftime (fp, fmt, tm, 0, 0); } int diff --git a/src/readconfig.c b/src/readconfig.c index 9eff4c7..e75cae4 100644 --- a/src/readconfig.c +++ b/src/readconfig.c @@ -89,10 +89,12 @@ cb_monitor (enum grecs_callback_command cmd, switch (cmd) { case grecs_callback_section_begin: - if (!value || value->type != GCONF_TYPE_STRING) + if (!value + || value->type != GCONF_TYPE_STRING + || value->v.string == NULL) { grecs_error (locus, 0, _("tag must be a string")); - return 0; + return 1; } mon = xzalloc (sizeof (*mon)); mon->id = strdup (value->v.string); @@ -399,10 +401,11 @@ cb_server (enum grecs_callback_command cmd, switch (cmd) { case grecs_callback_section_begin: - if (!value || value->type != GCONF_TYPE_STRING) + if (!value || value->type != GCONF_TYPE_STRING + || !value->v.string) { grecs_error (locus, 0, _("tag must be a string")); - return 0; + return 1; } cfg = xzalloc (sizeof (*cfg)); cfg->id = value->v.string; @@ -482,6 +485,9 @@ static struct grecs_keyword tagr_kw[] = { { "zero-unknown", NULL, N_("Zero-out missing samples (not implemented)") /* FIXME */, grecs_type_bool, &zero_unknown_option }, + { "cut-out-fraction", N_("arg: double"), NULL, /* FIXME */ + grecs_type_string, &cut_out_fraction, 0, + cb_double }, { "fill-incoming", NULL, N_("Fill incoming graph"), grecs_type_bool, &fill_incoming_option }, @@ -547,6 +553,8 @@ readconfig () rc = grecs_parse (configfile); if (rc == 0) mon_base = obstack_finish (&mon_stack); + if (cut_out_fraction < 1.0) + cut_out_fraction = TAGR_CUT_OUT; return rc; } diff --git a/src/report.c b/src/report.c index 398881b..71a735f 100644 --- a/src/report.c +++ b/src/report.c @@ -30,6 +30,7 @@ #include <arpa/inet.h> #include <tagr.h> #include <report.h> +#include <fprintftime.h> #include <gdbm.h> static char *dbname; @@ -144,6 +145,14 @@ write_db (struct monitor *mon, struct traffic_record *tr) } static void +print_time (FILE *fp, time_t time) +{ + fprintf (fp, "%lu [", (unsigned long) time); + fprintftime (fp, "%Y-%m-%d %H:%M:%S %z", localtime (&time), 0, 0); + fprintf (fp, "]"); +} + +static void print_queue (const char *title, queue_t *q) { int i, count; @@ -155,7 +164,9 @@ print_queue (const char *title, queue_t *q) for (i = count - 1; i >= 0; i--) { struct traffic_history *th = queue_get_ptr (q, i); - printf("%d %g %g\n", count - i, th->inrate, th->outrate); + printf ("%d ", count - i); + print_time (stdout, th->time); + printf (" %g %g\n", th->inrate, th->outrate); } } @@ -172,15 +183,13 @@ print_avg (const char *title, struct avg_acc *avg) static void print_tr (datum key, struct traffic_record *tr) { - char buf[512]; - struct tm *tm; int i, count; - tm = localtime (&tr->last.time); printf ("ID: %*.*s\n", key.dsize, key.dsize, key.dptr); - strftime (buf, sizeof buf, "%c", tm); - printf (_("Last sample: %lu (%s) %lu %lu\n"), - tr->last.time, buf, tr->last.in, tr->last.out); + + printf (_("Last sample: ")); + print_time (stdout, tr->last.time); + printf (" %lu %lu\n", tr->last.in, tr->last.out); printf (_("Last rates: %g %g\n"), tr->last_rates.inrate, tr->last_rates.outrate); @@ -27,8 +27,9 @@ #include <obstack.h> #include <tagr.h> -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; } @@ -25,6 +25,7 @@ #define TAGR_PIDFILE "/var/run/tagr.pid" #define TAGR_DBNAME "tagr.db" #define TAGR_DBMODE 0600 +#define TAGR_CUT_OUT 1.5 #define _(s) gettext (s) #define N_(s) s @@ -48,6 +49,7 @@ extern int log_to_stderr; extern char *pidfile; extern unsigned update_interval; extern int single_process_option; +extern double cut_out_fraction; extern char *user; extern char *basedir; @@ -76,13 +78,10 @@ extern int color_percent[3]; extern char **number_suffix; extern size_t number_suffix_count; -void assign_string (char **pstr, const char *s); -void assign_string_n (char **pstr, const char *s, size_t length); - int readconfig (void); void config_help (void); void define_symbol (char *s); -void decode_buffer (); + struct monitor *find_monitor (const char *name); struct monitor *find_monitor_id (const char *id); @@ -166,6 +165,7 @@ void verbose (int level, const char *fmt, ...); struct traffic_history { + time_t time; double inrate; double outrate; }; |