aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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 };
45int color_out[3] = { 0,94,255 }; 45int color_out[3] = { 0,94,255 };
46int color_grid[3] = { 0,0,0 }; 46int color_grid[3] = { 0,0,0 };
47int color_in_max[3] = { 0,166,33 }; 47int color_in_max[3] = { 0,166,33 };
48int color_out_max[3] = { 255,0,255 }; 48int color_out_max[3] = { 255,0,255 };
49int color_percent[3] = { 239,159,79 }; 49int color_percent[3] = { 239,159,79 };
50 50
51int graph_xsize = 460; 51int graph_xsize = 460;
52int graph_ysize = 100; 52int graph_ysize = 100;
53 53
54int graph_h_margin[2] = { 100, 14 }; 54int graph_h_margin[2] = { 100, 14 };
55int graph_v_margin[2] = { 14, 35 }; 55int graph_v_margin[2] = { 14, 35 };
56 56
57/* FIXME: I18N?? */ 57struct monitor default_monitor = {
58char *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
63static void 73static void
64draw_vtext (gdImagePtr graph, int color, const char *text) 74draw_vtext (gdImagePtr graph, int color, const char *text)
65{ 75{
66 gdImageStringUp (graph, gdFontSmall, 76 gdImageStringUp (graph, gdFontSmall,
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
83static double
84find_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
73int 101int
74draw_graph (FILE *fp, 102draw_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,
79 struct grid_class *xgrid, struct grid_class *ygrid) 107 struct grid_class *xgrid, struct grid_class *ygrid)
80{ 108{
81 int x, y; 109 int x, y;
82 int i, n; 110 int i, n;
83 gdImagePtr graph, brush_out, brush_outm, brush_outp; 111 gdImagePtr graph, brush_out, brush_outm, brush_outp;
84 int i_background, i_light, i_dark; 112 int i_background, i_light, i_dark;
85 int i_major, i_in, i_out, i_grid, i_inm, i_outm; 113 int i_major, i_in, i_out, i_grid, i_inm, i_outm;
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
105 graph = gdImageCreate (full_xsize, full_ysize); 145 graph = gdImageCreate (full_xsize, full_ysize);
106 brush_out = gdImageCreate (1, 2); 146 brush_out = gdImageCreate (1, 2);
107 brush_outm = gdImageCreate (1, 2); 147 brush_outm = gdImageCreate (1, 2);
108 brush_outp = gdImageCreate (1, 2); 148 brush_outp = gdImageCreate (1, 2);
109 149
110 i_background = make_color_index (graph, color_background); 150 i_background = make_color_index (graph, color_background);
@@ -204,26 +244,25 @@ draw_graph (FILE *fp,
204 244
205 dotted_style[0] = i_grid; 245 dotted_style[0] = i_grid;
206 dotted_style[1] = gdTransparent; 246 dotted_style[1] = gdTransparent;
207 dotted_style[2] = gdTransparent; 247 dotted_style[2] = gdTransparent;
208 gdImageSetStyle (graph, dotted_style, 3); 248 gdImageSetStyle (graph, dotted_style, 3);
209 249
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
224 /* Left tick */ 263 /* Left tick */
225 gdImageLine (graph, 264 gdImageLine (graph,
226 xtr (0) - 2, y, 265 xtr (0) - 2, y,
227 xtr (0) + 1, y, 266 xtr (0) + 1, y,
228 i_grid); 267 i_grid);
229 /* Right tick */ 268 /* 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;
86size_t number_suffix_count = sizeof (short_suffix) / sizeof (short_suffix[0]); 86size_t number_suffix_count = sizeof (short_suffix) / sizeof (short_suffix[0]);
87 87
88struct ygrid_data 88struct ygrid_data
89{ 89{
90 double step; 90 double step;
91}; 91};
92 92
93static void * 93static void *
94ygrid_create (grid_t grid, void *cdata) 94ygrid_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 gr->step = mon->ystep; 98 if (mon->ystep_absolute)
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
102static double 105static double
103ygrid_next (grid_t grid, char **str, int *mark) 106ygrid_next (grid_t grid, char **str, int *mark)
104{ 107{
105 struct ygrid_data *gr = grid->data; 108 struct ygrid_data *gr = grid->data;
106 if (mark) 109 if (mark)
107 *mark = 0; 110 *mark = 0;
108 grid->cur += gr->step; 111 grid->cur += gr->step;
109 if (grid->cur < grid->vmax) 112 if (grid->cur < grid->vmax)
110 { 113 {
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,
89 89
90 switch (cmd) 90 switch (cmd)
91 { 91 {
92 case grecs_callback_section_begin: 92 case grecs_callback_section_begin:
93 if (!value 93 if (!value
94 || value->type != GCONF_TYPE_STRING 94 || value->type != GCONF_TYPE_STRING
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;