diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2009-04-25 10:44:14 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2009-04-25 10:44:14 +0300 |
commit | d8c782807ffbd0e7e30b07f88dd91b6dc0988219 (patch) | |
tree | 64cdbde6fc4f6ae6f3c2c0564cd1fc57d5de9088 /src | |
parent | cedb36dcc43d5cb89a0b4af0e7eeeddb02401595 (diff) | |
download | tagr-d8c782807ffbd0e7e30b07f88dd91b6dc0988219.tar.gz tagr-d8c782807ffbd0e7e30b07f88dd91b6dc0988219.tar.bz2 |
Normalize terminology.
* etc/upgrade.awk: Translate "router" to "monitor".
Divide max rate by 8.
* src/graph.c (rate_unit): New global.
(draw_graph): Take monitor as an argument. Make rate
units configurable.
* src/readconfig.c (find_router, find_router_id): Rename
to find_monitor, find_monitor_id.
Rename "router" section to "monitor".
New statement "rate-units", allowed both at top level and
within a monitor.
* src/tagr.h (HostData): Rename to monitor. Add new field: rate_unit.
Rename `max' to `max_rate'.
Remove t, in, out.
(rate_unit): New declaration.
(find_router, find_router_id): Rename
to find_monitor, find_monitor_id.
(struct last_sample): Rename to struct traffic_sample.
* src/output.c, src/report.c, src/stat.c: Update
Diffstat (limited to 'src')
-rw-r--r-- | src/graph.c | 21 | ||||
-rw-r--r-- | src/output.c | 32 | ||||
-rw-r--r-- | src/readconfig.c | 82 | ||||
-rw-r--r-- | src/report.c | 49 | ||||
-rw-r--r-- | src/stat.c | 57 | ||||
-rw-r--r-- | src/tagr.h | 33 |
6 files changed, 138 insertions, 136 deletions
diff --git a/src/graph.c b/src/graph.c index 61befa5..0d9f9a7 100644 --- a/src/graph.c +++ b/src/graph.c @@ -54,6 +54,8 @@ int graph_ysize = 100; int graph_h_margin[2] = { 100, 14 }; int graph_v_margin[2] = { 14, 35 }; +char *rate_unit = "Bits per Second"; + static char *short_suffix[] = {"", "k", "M", "G", "T"}; char **number_suffix = short_suffix; size_t number_suffix_count = sizeof (short_suffix) / sizeof (short_suffix[0]); @@ -73,8 +75,9 @@ draw_vtext (gdImagePtr graph, int color, const char *text) int draw_graph (FILE *fp, + struct monitor *mon, queue_t *dataq, const struct avg_acc *avg, time_t now, - int xstep, unsigned long xmax, unsigned long ymax, + int xstep, unsigned long xmax, int growright, struct grid_class *xgrid, struct grid_class *ygrid) { @@ -86,12 +89,11 @@ draw_graph (FILE *fp, int i_outp, i_outpg; double xscale, yscale; int dotted_style[3]; - char *longup; - char *shortup; int full_xsize = graph_xsize + graph_h_margin[0] + graph_h_margin[1]; int full_ysize = graph_ysize + graph_v_margin[0] + graph_v_margin[1]; grid_t grid; - + unsigned long ymax = mon->max_rate; + yscale = (double) graph_ysize / ymax; xscale = (double) graph_xsize / xmax; @@ -176,21 +178,14 @@ draw_graph (FILE *fp, dotted_style[2] = gdTransparent; gdImageSetStyle (graph, dotted_style, 3); - /* draw the horizontal grid */ - /* FIXME: Both should be configurable and I18N */ - longup = "Bits per Second"; - shortup = "Bits/s"; - grid = grid_create (ygrid, dataq, 0, ymax, NULL); if (grid) { unsigned long i; char *str = NULL; - if (graph_ysize < gdFontSmall->w * 16) - draw_vtext (graph, i_grid, shortup); - else - draw_vtext (graph, i_grid, longup); + draw_vtext (graph, i_grid, + mon->rate_unit ? mon->rate_unit : rate_unit); /* Draw vertical grid */ while ((i = grid_next (grid, &str, NULL)) < ymax) diff --git a/src/output.c b/src/output.c index b201e0e..bb07eae 100644 --- a/src/output.c +++ b/src/output.c @@ -115,7 +115,7 @@ add_stats (pp_tab_t **ptab, queue_t *q, const char **symname) static int -do_update_output (SD *host, struct image_descr *dscr, +do_update_output (struct monitor *mon, struct image_descr *dscr, queue_t *queue, const struct avg_acc *avg, pp_tab_t **ptab, time_t timestamp, @@ -132,7 +132,7 @@ do_update_output (SD *host, struct image_descr *dscr, return 1; } - fname = mkfilename (dirname, host->name, dscr->suffix); + fname = mkfilename (dirname, mon->name, dscr->suffix); if (access (fname, R_OK) && needs_update) { FILE *fp; @@ -148,9 +148,9 @@ do_update_output (SD *host, struct image_descr *dscr, } else { - rc = draw_graph (fp, queue, avg, timestamp, dscr->step, + rc = draw_graph (fp, mon, queue, avg, timestamp, dscr->step, dscr->step*460 /* FIXME: must be queue->size * step */, - host->max/8, 0, dscr->xgrid, dscr->ygrid); + 0, dscr->xgrid, dscr->ygrid); fclose (fp); } } @@ -195,14 +195,14 @@ struct image_descr img_year = { }; int -update_output (SD *host, struct traffic_record *tr, time_t timestamp, - int force_update) +update_output (struct monitor *mon, struct traffic_record *tr, + time_t timestamp, int force_update) { - char *dirname = mkfilename (basedir, host->dir, NULL); + char *dirname = mkfilename (basedir, mon->dir, NULL); int rc; struct tm *tm = localtime (×tamp); pp_tab_t *tab = NULL; - char *tabfile = mkfilename (dirname, host->name, ".tab"); + char *tabfile = mkfilename (dirname, mon->name, ".tab"); char *htmlname; if (!force_update) @@ -211,24 +211,24 @@ update_output (SD *host, struct traffic_record *tr, time_t timestamp, force_update = 1; } - rc = do_update_output (host, &img_day, + rc = do_update_output (mon, &img_day, &tr->day_hist, NULL, &tab, timestamp, dirname, 1); - rc += do_update_output (host, &img_week, + rc += do_update_output (mon, &img_week, &tr->week_hist, &tr->week_avg, &tab, timestamp, dirname, force_update || (tm->tm_min >= 0 && tm->tm_min < 5) || (tm->tm_min >= 30 && tm->tm_min < 35)); - rc += do_update_output (host, &img_month, + rc += do_update_output (mon, &img_month, &tr->month_hist, &tr->month_avg, &tab, timestamp, dirname, force_update || (tm->tm_min >= 0 && tm->tm_min < 5 && (tm->tm_hour % 2) == 0)); - rc += do_update_output (host, &img_year, + rc += do_update_output (mon, &img_year, &tr->year_hist, &tr->year_avg, &tab, timestamp, dirname, @@ -240,14 +240,14 @@ update_output (SD *host, struct traffic_record *tr, time_t timestamp, { add_string_value (&tab, "PROGRAM", PACKAGE_NAME); add_string_value (&tab, "VERSION", PACKAGE_VERSION); - add_string_value (&tab, "ROUTERNAME", host->name); - add_string_value (&tab, "ROUTERIP", host->id); + add_string_value (&tab, "ROUTERNAME", mon->name); + add_string_value (&tab, "ROUTERIP", mon->id); add_string_value (&tab, "NOW", ctime (×tamp)); - add_numeric_value (&tab, "SPEED", host->max); + add_numeric_value (&tab, "SPEED", mon->max_rate); } - htmlname = mkfilename (dirname, host->name, ".html"); + htmlname = mkfilename (dirname, mon->name, ".html"); create_html (tab, html_template, htmlname); free (htmlname); diff --git a/src/readconfig.c b/src/readconfig.c index 4af1be3..2cb7652 100644 --- a/src/readconfig.c +++ b/src/readconfig.c @@ -29,45 +29,45 @@ #define ARG_UNUSED __attribute__ ((__unused__)) -static struct obstack sd_stack; -static SD *sd_base; -static size_t sd_count; +static struct obstack mon_stack; +static struct monitor *mon_base; +static size_t mon_count; static struct obstack pp_cmd_stack; static int pp_cmd_stack_init; -SD * -find_router (const char *name) +struct monitor * +find_monitor (const char *name) { - SD *sd; + struct monitor *mon; - for (sd = sd_base; sd < sd_base + sd_count; sd++) - if (strcmp (sd->name, name) == 0) - return sd; + for (mon = mon_base; mon < mon_base + mon_count; mon++) + if (strcmp (mon->name, name) == 0) + return mon; return NULL; } -SD * -find_router_id (const char *id) +struct monitor * +find_monitor_id (const char *id) { - SD *sd; + struct monitor *mon; - for (sd = sd_base; sd < sd_base + sd_count; sd++) - if (strcmp (sd->id, id) == 0) - return sd; + for (mon = mon_base; mon < mon_base + mon_count; mon++) + if (strcmp (mon->id, id) == 0) + return mon; return NULL; } static int -cb_router (enum grecs_callback_command cmd, +cb_monitor (enum grecs_callback_command cmd, grecs_locus_t *locus, void *varptr, grecs_value_t *value, void *cb_data) { int rc; - SD *sd; + struct monitor *mon; void **pdata = cb_data; switch (cmd) @@ -78,29 +78,29 @@ cb_router (enum grecs_callback_command cmd, grecs_error (locus, 0, _("tag must be a string")); return 0; } - sd = xzalloc (sizeof (*sd)); - sd->id = strdup (value->v.string); - *pdata = sd; + mon = xzalloc (sizeof (*mon)); + mon->id = strdup (value->v.string); + *pdata = mon; break; case grecs_callback_section_end: - sd = *pdata; - if (sd->max == 0) + mon = *pdata; + if (mon->max_rate == 0) { grecs_error (locus, 0, _("maximum speed not set")); - free (sd); + free (mon); return 0; } - if (!sd->name) - sd->name = strdup (sd->id); - if (!sd->dir) - sd->dir = strdup (sd->id); - if (sd_count == 0) - obstack_init (&sd_stack); + if (!mon->name) + mon->name = strdup (mon->id); + if (!mon->dir) + mon->dir = strdup (mon->id); + if (mon_count == 0) + obstack_init (&mon_stack); - obstack_grow (&sd_stack, sd, sizeof *sd); - sd_count++; - free (sd); + obstack_grow (&mon_stack, mon, sizeof *mon); + mon_count++; + free (mon); *pdata = NULL; break; @@ -111,13 +111,15 @@ cb_router (enum grecs_callback_command cmd, } -static struct grecs_keyword router_kw[] = { +static struct grecs_keyword monitor_kw[] = { { "host", NULL, N_("Host name or IP address"), - grecs_type_string, NULL, offsetof(struct HostData, name) }, + grecs_type_string, NULL, offsetof(struct monitor, name) }, { "directory", N_("name"), N_("Subdirectory name"), - grecs_type_string, NULL, offsetof(struct HostData, dir) }, + grecs_type_string, NULL, offsetof(struct monitor, dir) }, { "max-speed", NULL, N_("Maximum speed"), - grecs_type_size, NULL, offsetof(struct HostData, max) }, + grecs_type_size, NULL, offsetof(struct monitor, max_rate) }, + { "rate-units", NULL, N_("Name of rate units"), + grecs_type_string, NULL, offsetof(struct monitor, rate_unit) }, { NULL } }; @@ -234,10 +236,12 @@ static struct grecs_keyword tagr_kw[] = { { "listen", N_("socket"), N_("Listen on this address"), grecs_type_sockaddr, &listen_sockaddr, }, - { "router", N_("id: string"), N_("Configure a router"), + { "monitor", N_("id: string"), N_("Configure a monitor"), grecs_type_section, NULL, 0, - cb_router, NULL, router_kw }, + cb_monitor, NULL, monitor_kw }, + { "rate-units", NULL, N_("Name of rate units"), + grecs_type_string, &rate_unit }, { "number-suffixes", N_("suffixes"), NULL /* FIXME */, grecs_type_string, NULL, 0, cb_number_suffixes }, @@ -310,7 +314,7 @@ readconfig () exit (grecs_preproc_run (configfile, grecs_preprocessor) ? EX_CONFIG : 0); rc = grecs_parse (configfile); if (rc == 0) - sd_base = obstack_finish (&sd_stack); + mon_base = obstack_finish (&mon_stack); return rc; } diff --git a/src/report.c b/src/report.c index 6505820..a4edfcb 100644 --- a/src/report.c +++ b/src/report.c @@ -110,24 +110,24 @@ _read_db (datum key, struct traffic_record **tr) } void -read_db (SD *sd, struct traffic_record **tr) +read_db (struct monitor *mon, struct traffic_record **tr) { datum key; datum content; - key.dptr = sd->id; - key.dsize = strlen (sd->id); + key.dptr = mon->id; + key.dsize = strlen (mon->id); _read_db (key, tr); } void -write_db (SD *sd, struct traffic_record *tr) +write_db (struct monitor *mon, struct traffic_record *tr) { datum key; datum content; - key.dptr = sd->id; - key.dsize = strlen (sd->id); + key.dptr = mon->id; + key.dsize = strlen (mon->id); content.dsize = sizeof *tr; content.dptr = (char *) tr; @@ -135,7 +135,7 @@ write_db (SD *sd, struct traffic_record *tr) if (gdbm_store (dbf, key, content, GDBM_REPLACE)) { logmsg (L_ERR, "Failed to write data for %s: %s", - sd->id, gdbm_strerror (gdbm_errno)); + mon->id, gdbm_strerror (gdbm_errno)); } } @@ -194,7 +194,7 @@ list_db () { datum key; datum content; - SD sd; + struct monitor *mon; open_db (); key = gdbm_firstkey (dbf); @@ -214,18 +214,19 @@ list_db () void report (Stat *stat, time_t timestamp) { - SD *sd = find_router_id (stat->name); + struct monitor *mon = find_monitor_id (stat->name); - if (sd) + if (mon) { struct traffic_record *tr; - - sd->in = stat->in; - sd->out = stat->out; - sd->t = timestamp; - read_db (sd, &tr); - update_stats (sd, tr); - write_db (sd, tr); + struct traffic_sample s; + + s.in = stat->in; + s.out = stat->out; + s.time = timestamp; + read_db (mon, &tr); + update_stats (mon, &s, tr); + write_db (mon, tr); free (tr); } else @@ -233,11 +234,11 @@ report (Stat *stat, time_t timestamp) } int -update_router (datum key, time_t timestamp) +update_monitor (datum key, time_t timestamp) { char id[MAX_NAME_LENGTH+1]; struct traffic_record *tr = NULL; - SD *sd; + struct monitor *mon; if (key.dsize > MAX_NAME_LENGTH) { @@ -246,16 +247,16 @@ update_router (datum key, time_t timestamp) } memcpy (id, key.dptr, key.dsize); id[key.dsize] = 0; - sd = find_router_id (id); - if (!sd) + mon = find_monitor_id (id); + if (!mon) { - logmsg (L_ERR, _("%s: no such router"), id); + logmsg (L_ERR, _("%s: no such monitor"), id); return 1; } _read_db (key, &tr); if (tr) { - update_output (sd, tr, tr->last.time /*FIXME: must be now? */, 1); + update_output (mon, tr, tr->last.time /*FIXME: must be now? */, 1); free (tr); } @@ -274,7 +275,7 @@ rebuild () while (key.dptr) { datum nextkey = gdbm_nextkey ( dbf, key ); - update_router (key, now); + update_monitor (key, now); free (key.dptr); key = nextkey; } @@ -133,26 +133,27 @@ ovf_daily (struct traffic_history *th, struct traffic_record *tr, time_t now) } void -update_stats (SD *sd, struct traffic_record *tr) +update_stats (struct monitor *mon, struct traffic_sample *sample, + struct traffic_record *tr) { time_t interval; double inrate, outrate; struct traffic_history *lastp = queue_get_tail (&tr->day_hist); - interval = sd->t - tr->last.time; + interval = sample->time - tr->last.time; if (interval == 0) { logmsg (L_ERR, "Ignoring zero interval"); return; } - inrate = (double) sd->in / interval; - outrate = (double) sd->out / interval; + inrate = (double) sample->in / interval; + outrate = (double) sample->out / interval; if (lastp) { interpolate (&tr->day_hist, DAY_SAMPLE, - sd->t, + sample->time, tr->last.time, &tr->last_rates, interval, @@ -162,14 +163,14 @@ update_stats (SD *sd, struct traffic_record *tr) else { struct traffic_history th; - interval = sd->t - tr->last.time; + interval = sample->time - tr->last.time; th.inrate = inrate; th.outrate = outrate; queue_put (&tr->day_hist, &th); } - tr->last.time = sd->t; - tr->last.in = sd->in; - tr->last.out = sd->out; + tr->last.time = sample->time; + tr->last.in = sample->in; + tr->last.out = sample->out; tr->last_rates.inrate = inrate; tr->last_rates.outrate = outrate; } @@ -178,7 +179,7 @@ update_stats (SD *sd, struct traffic_record *tr) static void -compute_avg (struct avg_acc *avg, queue_t *q, struct last_sample *last) +compute_avg (struct avg_acc *avg, queue_t *q, struct traffic_sample *last) { int i, n = queue_count (q); avg->inrate = avg->outrate = 0; @@ -195,15 +196,13 @@ compute_avg (struct avg_acc *avg, queue_t *q, struct last_sample *last) avg->time = last->time; } -#define hist_rec last_sample - static void _convert (queue_t *q, ovf_t ovf, - struct traffic_record *tr, struct hist_rec *hist, size_t count, + struct traffic_record *tr, struct traffic_sample *hist, size_t count, time_t sample_interval) { size_t i; - struct hist_rec *hp; + struct traffic_sample *hp; tr->last.time = hist[count-1].time - sample_interval; for (hp = hist + count - 1; hp >= hist; hp--) @@ -250,7 +249,8 @@ _convert (queue_t *q, ovf_t ovf, } static void -convert_yearly (struct traffic_record *tr, struct hist_rec *hp, size_t count) +convert_yearly (struct traffic_record *tr, struct traffic_sample *hp, + size_t count) { verbose (2, "begin convert_yearly"); _convert (&tr->year_hist, NULL, tr, @@ -260,7 +260,8 @@ convert_yearly (struct traffic_record *tr, struct hist_rec *hp, size_t count) } static void -convert_monthly (struct traffic_record *tr, struct hist_rec *hp, size_t count) +convert_monthly (struct traffic_record *tr, struct traffic_sample *hp, + size_t count) { verbose (2, "begin convert_monthly"); if (count > MONTH_COUNT+1) @@ -272,7 +273,7 @@ convert_monthly (struct traffic_record *tr, struct hist_rec *hp, size_t count) } static void -convert_weekly (struct traffic_record *tr, struct hist_rec *hp, size_t count) +convert_weekly (struct traffic_record *tr, struct traffic_sample *hp, size_t count) { verbose (2, "begin convert_weekly"); if (count > WEEK_COUNT+1) @@ -284,7 +285,7 @@ convert_weekly (struct traffic_record *tr, struct hist_rec *hp, size_t count) } static void -convert_daily (struct traffic_record *tr, struct hist_rec *hp, size_t count) +convert_daily (struct traffic_record *tr, struct traffic_sample *hp, size_t count) { verbose (2, "begin convert_daily"); if (count > DAY_COUNT+1) @@ -294,8 +295,8 @@ convert_daily (struct traffic_record *tr, struct hist_rec *hp, size_t count) } static void -convert_stats (SD *sd, struct hist_rec *last, - struct hist_rec *hp, size_t count) +convert_stats (struct monitor *mon, struct traffic_sample *last, + struct traffic_sample *hp, size_t count) { struct traffic_record *trp, tr; @@ -304,10 +305,10 @@ convert_stats (SD *sd, struct hist_rec *last, convert_daily (&tr, hp, count); - read_db (sd, &trp); + read_db (mon, &trp); *trp = tr; trp->last = *last; - write_db (sd, trp); + write_db (mon, trp); free (trp); } @@ -317,7 +318,7 @@ import_log (const char *name) FILE *fp; int rc = 0; struct obstack stk; - struct hist_rec last, hist, *hp; + struct traffic_sample last, hist, *hp; unsigned long inmax, outmax; size_t count = 0; char *buf = NULL; @@ -387,7 +388,7 @@ import_log (const char *name) hp = obstack_finish (&stk); if (count) { - SD *sd; + struct monitor *mon; char *p = strrchr (name, '/'); char *base = xstrdup (p ? p + 1 : name); @@ -395,14 +396,14 @@ import_log (const char *name) if (p) *p = 0; - sd = find_router (base); - if (!sd) + mon = find_monitor (base); + if (!mon) { - logmsg (L_ERR, "cannot find router `%s'", base); + logmsg (L_ERR, "cannot find monitor `%s'", base); rc = 1; } else - convert_stats (sd, &last, hp, count); + convert_stats (mon, &last, hp, count); } obstack_free (&stk, NULL); @@ -30,16 +30,14 @@ #define _(s) s #define N_(s) s -typedef struct HostData +struct monitor { char *id; char *name; char *dir; - time_t t; - unsigned in; - unsigned out; - unsigned max; -} SD; + char *rate_unit; + unsigned max_rate; +}; extern struct grecs_sockaddr listen_sockaddr; extern int preprocess_only; @@ -57,6 +55,7 @@ extern char *html_template; extern char *html_input_file; extern int html_input_line; extern int verbose_level; +extern char *rate_unit; extern int percent_option; extern int transparent_option; @@ -80,8 +79,8 @@ void assign_string_n (char **pstr, const char *s, size_t length); int readconfig (void); void decode_buffer (); -SD *find_router (const char *name); -SD *find_router_id (const char *id); +struct monitor *find_monitor (const char *name); +struct monitor *find_monitor_id (const char *id); void begin_eval (); void end_eval (); @@ -160,7 +159,7 @@ struct stat_queue struct traffic_history *queue; }; -struct last_sample +struct traffic_sample { time_t time; unsigned long in, out; @@ -176,7 +175,7 @@ struct avg_acc struct traffic_record { - struct last_sample last; /* Lastly taken sample */ + struct traffic_sample last; /* Lastly taken sample */ struct traffic_history last_rates; /* Average accumulators */ struct avg_acc week_avg; @@ -194,9 +193,10 @@ struct traffic_record void open_db (); void close_db (); -void read_db (SD *sd, struct traffic_record **tr); -void write_db (SD *sd, struct traffic_record *tr); -void update_stats (SD *sd, struct traffic_record *tr); +void read_db (struct monitor *mon, struct traffic_record **tr); +void write_db (struct monitor *mon, struct traffic_record *tr); +void update_stats (struct monitor *mon, struct traffic_sample *sample, + struct traffic_record *tr); void rebuild (void); void import (const char *dir); @@ -212,8 +212,8 @@ char *mkfilename (char *dir, char *name, char *suffix); /* output.c */ -int update_output (SD *host, struct traffic_record *tr, time_t timestamp, - int force); +int update_output (struct monitor *mon, struct traffic_record *tr, + time_t timestamp, int force); /* graph.c */ @@ -238,7 +238,8 @@ extern struct grid_class grid_class_x_week; extern struct grid_class grid_class_x_month; int draw_graph (FILE *fp, + struct monitor *mon, queue_t *dataq, const struct avg_acc *avg, time_t now, - int xstep, unsigned long xmax, unsigned long ymax, + int xstep, unsigned long xmax, int growright, struct grid_class *xgrid, struct grid_class *ygrid); |