diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2009-04-25 01:55:11 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2009-04-25 01:55:11 +0300 |
commit | afb4d23167b4d2092f7fb84f98d7de4f007a83a3 (patch) | |
tree | 362fccbdb91c56fc15f8525af1e1e357bba87223 /src | |
parent | b8f6eec86f23d3fadd876654bcc145ef892fd1ea (diff) | |
download | tagr-afb4d23167b4d2092f7fb84f98d7de4f007a83a3.tar.gz tagr-afb4d23167b4d2092f7fb84f98d7de4f007a83a3.tar.bz2 |
Build html pages
* src/html.gram.y (read_symtab, write_symtab): Re-add.
* src/output.c: Update images only when needed or requested.
Build html page and symbol table.
* src/report.c (update_router): Update_output takes four args.
* src/tagr.h: Likewise.
Diffstat (limited to 'src')
-rw-r--r-- | src/html.gram.y | 109 | ||||
-rw-r--r-- | src/output.c | 206 | ||||
-rw-r--r-- | src/report.c | 2 | ||||
-rw-r--r-- | src/tagr.h | 3 |
4 files changed, 281 insertions, 39 deletions
diff --git a/src/html.gram.y b/src/html.gram.y index 830791f..f65c4ff 100644 --- a/src/html.gram.y +++ b/src/html.gram.y @@ -394,7 +394,114 @@ free_tab (pp_tab_t **ptab) } *ptab = NULL; } - + +int +read_symtab (pp_tab_t **tab, const char *name) +{ + FILE *fp; + char *buf = NULL; + size_t size = 0; + unsigned line = 0; + int status = 0; + + fp = fopen (name, "r"); + if (!fp) + return -1; + + while (getline (&buf, &size, fp) > 0) + { + char *p, *var, *value; + int cmd; + size_t len = strlen (buf); + + line++; + if (buf[len-1] == '\n') + buf[len-1] = 0; + + if (buf[0] == '#' || buf[0] == 0) + continue; + + p = buf; + cmd = *p++; + if (*p != ' ' || !(isascii (*++p) && isalpha (*p))) + { + logmsg (L_ERR, "%s:%u: invalid input: %s", + name, line, buf); + status = 1; + break; + } + var = p; + p = strchr (var, ' '); + if (!p) + { + logmsg (L_ERR, "%s:%u: invalid input: %s", + name, line, buf); + status = 1; + break; + } + *p++ = 0; + value = p; + + switch (cmd) + { + case 's': + add_string_value (tab, var, value); + break; + + case 'n': + { + char *p; + double num = strtod (value, &p); + if (*p) + { + logmsg (L_ERR, "%s:%u: invalid numeric value: %s", + name, line, value); + status = 1; + } + else + add_numeric_value (tab, var, num); + } + break; + + default: + logmsg (L_NOTICE, "%s:%u: ignoring unknown command %#03o", + name, line, cmd); + } + } + free (buf); + fclose (fp); + + return status; +} + +int +write_symtab (pp_tab_t *tab, const char *name) +{ + FILE *fp; + + fp = fopen (name, "w"); + if (!fp) + return -1; + for (; tab; tab = tab->next) + { + switch (tab->value.type) + { + case numeric_value: + fprintf (fp, "n %s %g\n", tab->name, tab->value.v.number); + break; + + case string_value: + fprintf (fp, "n %s %s\n", tab->name, tab->value.v.string); + break; + + case unspecified_value: + break; + } + } + fclose (fp); + return 0; +} + int check_template () { diff --git a/src/output.c b/src/output.c index ae2cc60..e7f6d75 100644 --- a/src/output.c +++ b/src/output.c @@ -65,16 +65,67 @@ create_hierarchy (char *dir, int perm) return rc; } +struct image_descr +{ + const char *suffix; + size_t step; + struct grid_class *xgrid; + struct grid_class *ygrid; + + const char *symname[6]; +}; + +enum symname_index + { + maxin_name, + avgin_name, + curin_name, + maxout_name, + avgout_name, + curout_name + }; + +static void +add_stats (pp_tab_t **ptab, queue_t *q, const char **symname) +{ + double val[6] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; + int i, n = queue_count (q); + struct traffic_history *th; + + for (i = 0; i < n; i++) + { + th = queue_get_ptr (q, i); + + val[avgin_name] += th->inrate; + if (th->inrate > val[maxin_name]) + val[maxin_name] = th->inrate; + + val[avgout_name] += th->outrate; + if (th->outrate > val[maxout_name]) + val[maxout_name] = th->outrate; + } + val[avgin_name] /= n; + val[avgout_name] /= n; + + th = queue_get_tail (q); + val[curin_name] = th->inrate; + val[curout_name] = th->outrate; + + for (i = 0; i < sizeof (val) / sizeof (val[0]); i++) + add_numeric_value (ptab, symname[i], val[i]); +} + + static int -do_update_output (SD *host, +do_update_output (SD *host, struct image_descr *dscr, queue_t *queue, const struct avg_acc *avg, - size_t step, time_t timestamp, - struct grid_class *xgrid, struct grid_class *ygrid, - char *dirname, char *img_suffix) + pp_tab_t **ptab, + time_t timestamp, + char *dirname, + int needs_update) { char *fname; - FILE *fp; - int rc; + int rc = 0; if (create_hierarchy (dirname, 0755)) { @@ -83,46 +134,129 @@ do_update_output (SD *host, return 1; } - fname = mkfilename (dirname, host->name, img_suffix); - fp = fopen (fname, "w"); - if (!fp) - { - logmsg (L_ERR, _("cannot create file %s: %s"), fname, strerror (errno)); - rc = 1; - } - else + fname = mkfilename (dirname, host->name, dscr->suffix); + if (access (fname, R_OK) && needs_update) { - rc = draw_graph (fp, queue, avg, timestamp, step, - step*460 /* FIXME: must be queue->size * step */, - host->max/8, 0, xgrid, ygrid); - fclose (fp); + FILE *fp; + + add_stats (ptab, queue, dscr->symname); + + fp = fopen (fname, "w"); + if (!fp) + { + logmsg (L_ERR, _("cannot create file %s: %s"), + fname, strerror (errno)); + rc = 1; + } + else + { + rc = draw_graph (fp, queue, avg, timestamp, dscr->step, + dscr->step*460 /* FIXME: must be queue->size * step */, + host->max/8, 0, dscr->xgrid, dscr->ygrid); + fclose (fp); + } } free (fname); return rc; } +struct image_descr img_day = { + "-day.png", + DAY_SAMPLE, + &grid_class_x_2h, &grid_class_y, + + { "MAXIN", "AVGIN", "CURIN", + "MAXOUT", "AVGOUT", "CUROUT" } +}; + +struct image_descr img_week = { + "-week.png", + WEEK_SAMPLE, + &grid_class_x_wday, &grid_class_y, + + { "HMAXIN", "HAVGIN", "HCURIN", + "HMAXOUT", "HAVGOUT", "HCUROUT" } +}; + +struct image_descr img_month = { + "-month.png", + MONTH_SAMPLE, + &grid_class_x_week, &grid_class_y, + + { "TMAXIN", "TAVGIN", "TCURIN", + "TMAXOUT", "TAVGOUT", "TCUROUT" } +}; + +struct image_descr img_year = { + "-year.png", + YEAR_SAMPLE, + &grid_class_x_month, &grid_class_y, + + { "DMAXIN", "DAVGIN", "DCURIN", + "DMAXOUT", "DAVGOUT", "DCUROUT" } +}; + int -update_output (SD *host, struct traffic_record *tr, time_t timestamp) +update_output (SD *host, struct traffic_record *tr, time_t timestamp, + int force_update) { char *dirname = mkfilename (basedir, host->dir, NULL); int rc; - - rc = do_update_output (host, &tr->day_hist, NULL, - DAY_SAMPLE, timestamp, - &grid_class_x_2h, &grid_class_y, - dirname, "-day.png"); - rc += do_update_output (host, &tr->week_hist, &tr->week_avg, - WEEK_SAMPLE, timestamp, - &grid_class_x_wday, &grid_class_y, - dirname, "-week.png"); - rc += do_update_output (host, &tr->month_hist, &tr->month_avg, - MONTH_SAMPLE, timestamp, - &grid_class_x_week, &grid_class_y, - dirname, "-month.png"); - rc += do_update_output (host, &tr->year_hist, &tr->year_avg, - YEAR_SAMPLE, timestamp, - &grid_class_x_month, &grid_class_y, - dirname, "-year.png"); + struct tm *tm = localtime (×tamp); + pp_tab_t *tab = NULL; + char *tabfile = mkfilename (dirname, host->name, ".tab"); + char *htmlname; + + if (!force_update) + { + if (read_symtab (&tab, tabfile)) + force_update = 1; + } + + rc = do_update_output (host, &img_day, + &tr->day_hist, NULL, &tab, + timestamp, dirname, 1); + rc += do_update_output (host, &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, + &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, + &tr->year_hist, &tr->year_avg, &tab, + timestamp, + dirname, + force_update + || (tm->tm_min >= 0 && tm->tm_min < 5 + && tm->tm_hour == 0)); + + if (force_update) + { + 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, "NOW", ctime (×tamp)); + + add_numeric_value (&tab, "SPEED", host->max); + } + + htmlname = mkfilename (dirname, host->name, ".html"); + create_html (tab, html_template, htmlname); + free (htmlname); + + write_symtab (tab, tabfile); + free (tabfile); + free_tab (&tab); + free (dirname); return rc; } diff --git a/src/report.c b/src/report.c index ea67ab8..d835079 100644 --- a/src/report.c +++ b/src/report.c @@ -257,7 +257,7 @@ update_router (datum key, time_t timestamp) _read_db (key, &tr); if (tr) { - update_output (sd, tr, tr->last.time);/*FIXME: must be now*/ + update_output (sd, tr, tr->last.time /*FIXME: must be now? */, 1); free (tr); } @@ -214,7 +214,8 @@ char *mkfilename (char *dir, char *name, char *suffix); /* output.c */ -int update_output (SD *host, struct traffic_record *tr, time_t timestamp); +int update_output (SD *host, struct traffic_record *tr, time_t timestamp, + int force); /* graph.c */ |