aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2009-05-30 12:57:16 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2009-05-30 12:57:16 +0300
commit2a6ef4f51500091e535b2b47d709eee5cd52468c (patch)
treee3e28622c41b8b477acf1f2be6db9955b7ab4c6b
parentfbc8bdac6efb0c63a73c5105bbf4ecb304187e44 (diff)
downloadtagr-2a6ef4f51500091e535b2b47d709eee5cd52468c.tar.gz
tagr-2a6ef4f51500091e535b2b47d709eee5cd52468c.tar.bz2
Adjust vertical axis as needed.HEADmaster
* src/graph.c (default_monitor): New global. (find_max): New function. (draw_graph): Adjust max rate if requested. * src/grid.c (ygrid_create): Use mon->ystep_absolute to determine the type of grid. * src/readconfig.c (cb_monitor): Handle `monitor default'. (monitor_kw): New statements: max-adjust and y-step-absolute. (tagr_kw): Remove rate-units kw. * src/tagr.h (struct monitor): New members: max_adjust and ystep_absolute. (default_monitor): New extern.
-rw-r--r--src/graph.c51
-rw-r--r--src/grid.c5
-rw-r--r--src/readconfig.c42
-rw-r--r--src/tagr.h20
4 files changed, 85 insertions, 33 deletions
diff --git a/src/graph.c b/src/graph.c
index 80b2a97..89f02a1 100644
--- a/src/graph.c
+++ b/src/graph.c
@@ -45,66 +45,106 @@ int color_in[3] = { 0,235,12 };
int color_out[3] = { 0,94,255 };
int color_grid[3] = { 0,0,0 };
int color_in_max[3] = { 0,166,33 };
int color_out_max[3] = { 255,0,255 };
int color_percent[3] = { 239,159,79 };
int graph_xsize = 460;
int graph_ysize = 100;
int graph_h_margin[2] = { 100, 14 };
int graph_v_margin[2] = { 14, 35 };
-/* FIXME: I18N?? */
-char *rate_unit = "Bytes per Second";
+struct monitor default_monitor = {
+ NULL,
+ NULL,
+ NULL,
+ "Bytes per Second", /* rate_unit, FIXME: I18N?? */
+ 0, /* max_rate */
+ 0, /* max_adjust */
+ 1.0, /* scale */
+ 0.1, /* ystep */
+ 0, /* ystep_absolute */
+ 0 /* swap */
+};
#define make_color_index(g, ar) \
gdImageColorAllocate (g, (ar)[0], (ar)[1], (ar)[2])
static void
draw_vtext (gdImagePtr graph, int color, const char *text)
{
gdImageStringUp (graph, gdFontSmall,
8,
graph_ysize + graph_v_margin[0] -
(graph_ysize - gdFontSmall->w * strlen (text))/2,
(unsigned char *) text, color);
}
+static double
+find_max (queue_t *dataq)
+{
+ int i, n = queue_count (dataq);
+ double mval = 0;
+ struct traffic_history *th;
+
+ for (i = 0; i < n; i++)
+ {
+ th = queue_get_ptr (dataq, i);
+ if (mval < th->inrate)
+ mval = th->inrate;
+ if (mval < th->outrate)
+ mval = th->outrate;
+ }
+ return mval;
+}
+
int
draw_graph (FILE *fp,
struct monitor *mon,
queue_t *dataq, const struct avg_acc *avg, time_t start,
int xstep, unsigned long xmax,
int growright,
struct grid_class *xgrid, struct grid_class *ygrid)
{
int x, y;
int i, n;
gdImagePtr graph, brush_out, brush_outm, brush_outp;
int i_background, i_light, i_dark;
int i_major, i_in, i_out, i_grid, i_inm, i_outm;
int i_outp, i_outpg;
double xscale, yscale;
int dotted_style[3];
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;
- double ymax = mon->max_rate;
+ double ymax;
+
+ if (mon->max_adjust)
+ {
+ double amax = find_max (dataq);
+ if (amax > mon->max_rate)
+ {
+ unsigned long n = (amax - mon->max_rate + mon->max_adjust - 1) /
+ mon->max_adjust;
+ mon->max_rate += n * mon->max_adjust;
+ }
+ }
+ ymax = mon->max_rate;
yscale = (double) graph_ysize / ymax;
xscale = (double) graph_xsize / xmax;
#define ytr(y) \
- (unsigned long) ((ymax >= (y) ? (ymax - (y)) : ymax) * \
+ (unsigned long) ((ymax >= (y) ? (ymax - (y)) : 0) * \
yscale + graph_h_margin[1])
#define xtr(x) \
(unsigned long) (growright ? \
((full_xsize - (x)*xscale)) : \
(graph_h_margin[0] + (x)*xscale))
graph = gdImageCreate (full_xsize, full_ysize);
brush_out = gdImageCreate (1, 2);
brush_outm = gdImageCreate (1, 2);
brush_outp = gdImageCreate (1, 2);
i_background = make_color_index (graph, color_background);
@@ -204,26 +244,25 @@ draw_graph (FILE *fp,
dotted_style[0] = i_grid;
dotted_style[1] = gdTransparent;
dotted_style[2] = gdTransparent;
gdImageSetStyle (graph, dotted_style, 3);
grid = grid_create (ygrid, dataq, 0, ymax, mon);
if (grid)
{
double d;
char *str = NULL;
- draw_vtext (graph, i_grid,
- mon->rate_unit ? mon->rate_unit : rate_unit);
+ draw_vtext (graph, i_grid, mon->rate_unit);
/* Draw vertical grid */
while ((d = grid_next (grid, &str, NULL)) < ymax)
{
int y = ytr (d);
/* Left tick */
gdImageLine (graph,
xtr (0) - 2, y,
xtr (0) + 1, y,
i_grid);
/* Right tick */
diff --git a/src/grid.c b/src/grid.c
index 08e1c08..4ecb576 100644
--- a/src/grid.c
+++ b/src/grid.c
@@ -86,25 +86,28 @@ char **number_suffix = short_suffix;
size_t number_suffix_count = sizeof (short_suffix) / sizeof (short_suffix[0]);
struct ygrid_data
{
double step;
};
static void *
ygrid_create (grid_t grid, void *cdata)
{
struct monitor *mon = cdata;
struct ygrid_data *gr = xmalloc (sizeof (*gr));
- gr->step = mon->ystep;
+ if (mon->ystep_absolute)
+ gr->step = mon->ystep;
+ else
+ gr->step = mon->ystep * mon->max_rate;
return gr;
}
static double
ygrid_next (grid_t grid, char **str, int *mark)
{
struct ygrid_data *gr = grid->data;
if (mark)
*mark = 0;
grid->cur += gr->step;
if (grid->cur < grid->vmax)
{
diff --git a/src/readconfig.c b/src/readconfig.c
index 1c22cb1..a60f19d 100644
--- a/src/readconfig.c
+++ b/src/readconfig.c
@@ -89,47 +89,53 @@ cb_monitor (enum grecs_callback_command cmd,
switch (cmd)
{
case grecs_callback_section_begin:
if (!value
|| value->type != GCONF_TYPE_STRING
|| value->v.string == NULL)
{
grecs_error (locus, 0, _("tag must be a string"));
return 1;
}
mon = xzalloc (sizeof (*mon));
+ *mon = default_monitor;
mon->id = strdup (value->v.string);
- mon->scale = 1.0;
- mon->ystep = 100.;
+ mon->name = NULL;
+ mon->dir = NULL;
*pdata = mon;
break;
case grecs_callback_section_end:
mon = *pdata;
- if (mon->max_rate == 0)
+ if (strcmp (mon->id, "default") == 0)
+ default_monitor = *mon;
+ else
{
- grecs_error (locus, 0, _("maximum speed not set"));
- free (mon);
- return 0;
+ if (mon->max_rate == 0 && mon->max_adjust == 0)
+ {
+ grecs_error (locus, 0, _("maximum speed not set"));
+ free (mon);
+ return 0;
+ }
+ 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 (&mon_stack, mon, sizeof *mon);
+ mon_count++;
}
- 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 (&mon_stack, mon, sizeof *mon);
- mon_count++;
free (mon);
*pdata = NULL;
break;
case grecs_callback_set_value:
grecs_error (locus, 0, _("invalid use of block statement"));
}
return 0;
}
static int
cb_double (enum grecs_callback_command cmd,
@@ -165,32 +171,36 @@ cb_double (enum grecs_callback_command cmd,
*dptr = d;
return 0;
}
static struct grecs_keyword monitor_kw[] = {
{ "host", NULL, N_("Host name or IP address"),
grecs_type_string, NULL, offsetof(struct monitor, name) },
{ "directory", N_("name"), N_("Subdirectory name"),
grecs_type_string, NULL, offsetof(struct monitor, dir) },
{ "max-speed", NULL, N_("Maximum speed"),
grecs_type_ulong, NULL, offsetof(struct monitor, max_rate) },
+ { "max-adjust", NULL, N_("Adjust maximum by this value when needed"),
+ grecs_type_ulong, NULL, offsetof(struct monitor, max_adjust) },
{ "rate-units", NULL, N_("Name of rate units"),
grecs_type_string, NULL, offsetof(struct monitor, rate_unit) },
{ "scale", N_("arg: double"), N_("Scaling factor"),
grecs_type_string, NULL, offsetof(struct monitor, scale),
cb_double },
{ "y-step", N_("arg: double"), N_("Step for Y axis"),
grecs_type_string, NULL, offsetof(struct monitor, ystep),
cb_double},
+ { "y-step-absolute", NULL, N_("y-step is an absolute value"),
+ grecs_type_bool, NULL, offsetof(struct monitor, ystep_absolute) },
{ "swap", NULL, N_("Swap in and out rates"),
grecs_type_bool, NULL, offsetof(struct monitor, swap) },
{ NULL }
};
static int
cb_number_suffixes (enum grecs_callback_command cmd,
grecs_locus_t *locus,
void *varptr,
grecs_value_t *value,
void *cb_data)
@@ -479,26 +489,24 @@ static struct grecs_keyword tagr_kw[] = {
{ "lock-count", NULL,
N_("Set maximum number of attempts to acquire the lock on a database"),
grecs_type_uint, &lock_retry_count_option },
{ "lock-timeout", N_("seconds"),
N_("Set the time span between the two locking attempts"),
grecs_type_uint, &lock_retry_timeout_option },
{ "idle-timeout", N_("seconds"),
N_("Idle timeout for stream connections"),
grecs_type_uint, &stream_idle_timeout },
- { "rate-units", NULL, N_("Name of rate units"),
- grecs_type_string, &rate_unit },
{ "number-suffixes", N_("suffixes"),
N_("Not implemented") /* FIXME */,
grecs_type_string, NULL, 0, cb_number_suffixes },
{ "transparent", NULL, N_("Transparent graphs"),
grecs_type_bool, &transparent_option },
{ "percent", NULL,
N_("Draw in/out percent graph (not implemented)"), /* FIXME */
grecs_type_bool, &percent_option },
{ "zero-unknown", NULL,
N_("Zero-out missing samples (not implemented)") /* FIXME */,
grecs_type_bool, &zero_unknown_option },
diff --git a/src/tagr.h b/src/tagr.h
index 5d7c3e0..6ed7ca2 100644
--- a/src/tagr.h
+++ b/src/tagr.h
@@ -34,58 +34,60 @@
#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
#define gettext(s) s
#define ngettext(s,p,c) ((c) == 1 ? (s) : (p))
struct monitor
{
- char *id;
- char *name;
- char *dir;
- char *rate_unit;
- unsigned long max_rate;
- double scale;
- double ystep;
- int swap;
+ char *id; /* Monitor ID */
+ char *name; /* Full name */
+ char *dir; /* Output directory */
+ char *rate_unit; /* Rate units */
+ unsigned long max_rate; /* Max. rate value */
+ unsigned long max_adjust; /* Adjustment for max. rate */
+ double scale; /* Y (rate) scale */
+ double ystep; /* Step of Y grid, in fractions of max_rate */
+ int ystep_absolute; /* When true, ystep is an absolute value */
+ int swap; /* Swap input and output rates */
};
extern int preprocess_only;
extern int log_to_stderr;
extern char *pidfile;
extern unsigned update_interval;
extern int single_process_option;
extern double cut_out_fraction;
extern unsigned lock_retry_count_option;
extern unsigned lock_retry_timeout_option;
extern unsigned stream_idle_timeout;
extern char *hostname;
extern char *user;
extern char *basedir;
extern char *configfile;
extern int foreground;
extern char *html_template;
extern char *html_input_file;
extern int html_input_line;
extern int verbose_level;
-extern char *rate_unit;
+extern struct monitor default_monitor;
extern int percent_option;
extern int transparent_option;
extern int zero_unknown_option;
extern int fill_incoming_option;
extern int color_background[3];
extern int color_light[3];
extern int color_dark[3];
extern int color_major[3];
extern int color_in[3];
extern int color_out[3];
extern int color_grid[3];
extern int color_in_max[3];

Return to:

Send suggestions and report system problems to the System administrator.