diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2009-05-30 12:57:16 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2009-05-30 12:57:16 +0300 |
commit | 2a6ef4f51500091e535b2b47d709eee5cd52468c (patch) | |
tree | e3e28622c41b8b477acf1f2be6db9955b7ab4c6b | |
parent | fbc8bdac6efb0c63a73c5105bbf4ecb304187e44 (diff) | |
download | tagr-2a6ef4f51500091e535b2b47d709eee5cd52468c.tar.gz tagr-2a6ef4f51500091e535b2b47d709eee5cd52468c.tar.bz2 |
* 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.c | 51 | ||||
-rw-r--r-- | src/grid.c | 3 | ||||
-rw-r--r-- | src/readconfig.c | 18 | ||||
-rw-r--r-- | src/tagr.h | 20 |
4 files changed, 72 insertions, 20 deletions
diff --git a/src/graph.c b/src/graph.c index 80b2a97..89f02a1 100644 --- a/src/graph.c +++ b/src/graph.c | |||
@@ -51,14 +51,24 @@ int color_percent[3] = { 239,159,79 }; | |||
51 | int graph_xsize = 460; | 51 | int graph_xsize = 460; |
52 | int graph_ysize = 100; | 52 | int graph_ysize = 100; |
53 | 53 | ||
54 | int graph_h_margin[2] = { 100, 14 }; | 54 | int graph_h_margin[2] = { 100, 14 }; |
55 | int graph_v_margin[2] = { 14, 35 }; | 55 | int graph_v_margin[2] = { 14, 35 }; |
56 | 56 | ||
57 | /* FIXME: I18N?? */ | 57 | struct monitor default_monitor = { |
58 | char *rate_unit = "Bytes per Second"; | 58 | NULL, |
59 | NULL, | ||
60 | NULL, | ||
61 | "Bytes per Second", /* rate_unit, FIXME: I18N?? */ | ||
62 | 0, /* max_rate */ | ||
63 | 0, /* max_adjust */ | ||
64 | 1.0, /* scale */ | ||
65 | 0.1, /* ystep */ | ||
66 | 0, /* ystep_absolute */ | ||
67 | 0 /* swap */ | ||
68 | }; | ||
59 | 69 | ||
60 | #define make_color_index(g, ar) \ | 70 | #define make_color_index(g, ar) \ |
61 | gdImageColorAllocate (g, (ar)[0], (ar)[1], (ar)[2]) | 71 | gdImageColorAllocate (g, (ar)[0], (ar)[1], (ar)[2]) |
62 | 72 | ||
63 | static void | 73 | static void |
64 | draw_vtext (gdImagePtr graph, int color, const char *text) | 74 | draw_vtext (gdImagePtr graph, int color, const char *text) |
@@ -67,12 +77,30 @@ draw_vtext (gdImagePtr graph, int color, const char *text) | |||
67 | 8, | 77 | 8, |
68 | graph_ysize + graph_v_margin[0] - | 78 | graph_ysize + graph_v_margin[0] - |
69 | (graph_ysize - gdFontSmall->w * strlen (text))/2, | 79 | (graph_ysize - gdFontSmall->w * strlen (text))/2, |
70 | (unsigned char *) text, color); | 80 | (unsigned char *) text, color); |
71 | } | 81 | } |
72 | 82 | ||
83 | static double | ||
84 | find_max (queue_t *dataq) | ||
85 | { | ||
86 | int i, n = queue_count (dataq); | ||
87 | double mval = 0; | ||
88 | struct traffic_history *th; | ||
89 | |||
90 | for (i = 0; i < n; i++) | ||
91 | { | ||
92 | th = queue_get_ptr (dataq, i); | ||
93 | if (mval < th->inrate) | ||
94 | mval = th->inrate; | ||
95 | if (mval < th->outrate) | ||
96 | mval = th->outrate; | ||
97 | } | ||
98 | return mval; | ||
99 | } | ||
100 | |||
73 | int | 101 | int |
74 | draw_graph (FILE *fp, | 102 | draw_graph (FILE *fp, |
75 | struct monitor *mon, | 103 | struct monitor *mon, |
76 | queue_t *dataq, const struct avg_acc *avg, time_t start, | 104 | queue_t *dataq, const struct avg_acc *avg, time_t start, |
77 | int xstep, unsigned long xmax, | 105 | int xstep, unsigned long xmax, |
78 | int growright, | 106 | int growright, |
@@ -86,19 +114,31 @@ draw_graph (FILE *fp, | |||
86 | int i_outp, i_outpg; | 114 | int i_outp, i_outpg; |
87 | double xscale, yscale; | 115 | double xscale, yscale; |
88 | int dotted_style[3]; | 116 | int dotted_style[3]; |
89 | int full_xsize = graph_xsize + graph_h_margin[0] + graph_h_margin[1]; | 117 | int full_xsize = graph_xsize + graph_h_margin[0] + graph_h_margin[1]; |
90 | int full_ysize = graph_ysize + graph_v_margin[0] + graph_v_margin[1]; | 118 | int full_ysize = graph_ysize + graph_v_margin[0] + graph_v_margin[1]; |
91 | grid_t grid; | 119 | grid_t grid; |
92 | double ymax = mon->max_rate; | 120 | double ymax; |
121 | |||
122 | if (mon->max_adjust) | ||
123 | { | ||
124 | double amax = find_max (dataq); | ||
125 | if (amax > mon->max_rate) | ||
126 | { | ||
127 | unsigned long n = (amax - mon->max_rate + mon->max_adjust - 1) / | ||
128 | mon->max_adjust; | ||
129 | mon->max_rate += n * mon->max_adjust; | ||
130 | } | ||
131 | } | ||
132 | ymax = mon->max_rate; | ||
93 | 133 | ||
94 | yscale = (double) graph_ysize / ymax; | 134 | yscale = (double) graph_ysize / ymax; |
95 | xscale = (double) graph_xsize / xmax; | 135 | xscale = (double) graph_xsize / xmax; |
96 | 136 | ||
97 | #define ytr(y) \ | 137 | #define ytr(y) \ |
98 | (unsigned long) ((ymax >= (y) ? (ymax - (y)) : ymax) * \ | 138 | (unsigned long) ((ymax >= (y) ? (ymax - (y)) : 0) * \ |
99 | yscale + graph_h_margin[1]) | 139 | yscale + graph_h_margin[1]) |
100 | #define xtr(x) \ | 140 | #define xtr(x) \ |
101 | (unsigned long) (growright ? \ | 141 | (unsigned long) (growright ? \ |
102 | ((full_xsize - (x)*xscale)) : \ | 142 | ((full_xsize - (x)*xscale)) : \ |
103 | (graph_h_margin[0] + (x)*xscale)) | 143 | (graph_h_margin[0] + (x)*xscale)) |
104 | 144 | ||
@@ -210,14 +250,13 @@ draw_graph (FILE *fp, | |||
210 | grid = grid_create (ygrid, dataq, 0, ymax, mon); | 250 | grid = grid_create (ygrid, dataq, 0, ymax, mon); |
211 | if (grid) | 251 | if (grid) |
212 | { | 252 | { |
213 | double d; | 253 | double d; |
214 | char *str = NULL; | 254 | char *str = NULL; |
215 | 255 | ||
216 | draw_vtext (graph, i_grid, | 256 | draw_vtext (graph, i_grid, mon->rate_unit); |
217 | mon->rate_unit ? mon->rate_unit : rate_unit); | ||
218 | 257 | ||
219 | /* Draw vertical grid */ | 258 | /* Draw vertical grid */ |
220 | while ((d = grid_next (grid, &str, NULL)) < ymax) | 259 | while ((d = grid_next (grid, &str, NULL)) < ymax) |
221 | { | 260 | { |
222 | int y = ytr (d); | 261 | int y = ytr (d); |
223 | 262 | ||
@@ -92,13 +92,16 @@ struct ygrid_data | |||
92 | 92 | ||
93 | static void * | 93 | static void * |
94 | ygrid_create (grid_t grid, void *cdata) | 94 | ygrid_create (grid_t grid, void *cdata) |
95 | { | 95 | { |
96 | struct monitor *mon = cdata; | 96 | struct monitor *mon = cdata; |
97 | struct ygrid_data *gr = xmalloc (sizeof (*gr)); | 97 | struct ygrid_data *gr = xmalloc (sizeof (*gr)); |
98 | if (mon->ystep_absolute) | ||
98 | gr->step = mon->ystep; | 99 | gr->step = mon->ystep; |
100 | else | ||
101 | gr->step = mon->ystep * mon->max_rate; | ||
99 | return gr; | 102 | return gr; |
100 | } | 103 | } |
101 | 104 | ||
102 | static double | 105 | static double |
103 | ygrid_next (grid_t grid, char **str, int *mark) | 106 | ygrid_next (grid_t grid, char **str, int *mark) |
104 | { | 107 | { |
diff --git a/src/readconfig.c b/src/readconfig.c index 1c22cb1..a60f19d 100644 --- a/src/readconfig.c +++ b/src/readconfig.c | |||
@@ -95,21 +95,26 @@ cb_monitor (enum grecs_callback_command cmd, | |||
95 | || value->v.string == NULL) | 95 | || value->v.string == NULL) |
96 | { | 96 | { |
97 | grecs_error (locus, 0, _("tag must be a string")); | 97 | grecs_error (locus, 0, _("tag must be a string")); |
98 | return 1; | 98 | return 1; |
99 | } | 99 | } |
100 | mon = xzalloc (sizeof (*mon)); | 100 | mon = xzalloc (sizeof (*mon)); |
101 | *mon = default_monitor; | ||
101 | mon->id = strdup (value->v.string); | 102 | mon->id = strdup (value->v.string); |
102 | mon->scale = 1.0; | 103 | mon->name = NULL; |
103 | mon->ystep = 100.; | 104 | mon->dir = NULL; |
104 | *pdata = mon; | 105 | *pdata = mon; |
105 | break; | 106 | break; |
106 | 107 | ||
107 | case grecs_callback_section_end: | 108 | case grecs_callback_section_end: |
108 | mon = *pdata; | 109 | mon = *pdata; |
109 | if (mon->max_rate == 0) | 110 | if (strcmp (mon->id, "default") == 0) |
111 | default_monitor = *mon; | ||
112 | else | ||
113 | { | ||
114 | if (mon->max_rate == 0 && mon->max_adjust == 0) | ||
110 | { | 115 | { |
111 | grecs_error (locus, 0, _("maximum speed not set")); | 116 | grecs_error (locus, 0, _("maximum speed not set")); |
112 | free (mon); | 117 | free (mon); |
113 | return 0; | 118 | return 0; |
114 | } | 119 | } |
115 | if (!mon->name) | 120 | if (!mon->name) |
@@ -118,12 +123,13 @@ cb_monitor (enum grecs_callback_command cmd, | |||
118 | mon->dir = strdup (mon->id); | 123 | mon->dir = strdup (mon->id); |
119 | if (mon_count == 0) | 124 | if (mon_count == 0) |
120 | obstack_init (&mon_stack); | 125 | obstack_init (&mon_stack); |
121 | 126 | ||
122 | obstack_grow (&mon_stack, mon, sizeof *mon); | 127 | obstack_grow (&mon_stack, mon, sizeof *mon); |
123 | mon_count++; | 128 | mon_count++; |
129 | } | ||
124 | free (mon); | 130 | free (mon); |
125 | *pdata = NULL; | 131 | *pdata = NULL; |
126 | break; | 132 | break; |
127 | 133 | ||
128 | case grecs_callback_set_value: | 134 | case grecs_callback_set_value: |
129 | grecs_error (locus, 0, _("invalid use of block statement")); | 135 | grecs_error (locus, 0, _("invalid use of block statement")); |
@@ -171,20 +177,24 @@ static struct grecs_keyword monitor_kw[] = { | |||
171 | { "host", NULL, N_("Host name or IP address"), | 177 | { "host", NULL, N_("Host name or IP address"), |
172 | grecs_type_string, NULL, offsetof(struct monitor, name) }, | 178 | grecs_type_string, NULL, offsetof(struct monitor, name) }, |
173 | { "directory", N_("name"), N_("Subdirectory name"), | 179 | { "directory", N_("name"), N_("Subdirectory name"), |
174 | grecs_type_string, NULL, offsetof(struct monitor, dir) }, | 180 | grecs_type_string, NULL, offsetof(struct monitor, dir) }, |
175 | { "max-speed", NULL, N_("Maximum speed"), | 181 | { "max-speed", NULL, N_("Maximum speed"), |
176 | grecs_type_ulong, NULL, offsetof(struct monitor, max_rate) }, | 182 | grecs_type_ulong, NULL, offsetof(struct monitor, max_rate) }, |
183 | { "max-adjust", NULL, N_("Adjust maximum by this value when needed"), | ||
184 | grecs_type_ulong, NULL, offsetof(struct monitor, max_adjust) }, | ||
177 | { "rate-units", NULL, N_("Name of rate units"), | 185 | { "rate-units", NULL, N_("Name of rate units"), |
178 | grecs_type_string, NULL, offsetof(struct monitor, rate_unit) }, | 186 | grecs_type_string, NULL, offsetof(struct monitor, rate_unit) }, |
179 | { "scale", N_("arg: double"), N_("Scaling factor"), | 187 | { "scale", N_("arg: double"), N_("Scaling factor"), |
180 | grecs_type_string, NULL, offsetof(struct monitor, scale), | 188 | grecs_type_string, NULL, offsetof(struct monitor, scale), |
181 | cb_double }, | 189 | cb_double }, |
182 | { "y-step", N_("arg: double"), N_("Step for Y axis"), |