1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <stdint.h>
6 #include <inttypes.h>
7 #include <dirent.h> /* for PATH_MAX */
8 #include <assert.h>
9 #include <math.h>
11 #include <rrd.h>
13 #include "common.h"
14 #include "action_graph.h"
15 #include "graph_list.h"
16 #include "utils_params.h"
17 #include "utils_array.h"
19 #include <fcgiapp.h>
20 #include <fcgi_stdio.h>
22 struct data_source_s
23 {
24 char *file;
25 char *name;
26 char *legend;
27 double scale;
28 _Bool nan_to_zero;
29 _Bool draw_area;
30 uint32_t color;
31 };
32 typedef struct data_source_s data_source_t;
34 struct graph_def_s
35 {
36 data_source_t *data_sources;
37 size_t data_sources_num;
39 _Bool stack;
41 int def_num;
42 };
43 typedef struct graph_def_s graph_def_t;
45 static void graph_def_free (graph_def_t *gd) /* {{{ */
46 {
47 size_t i;
49 if (gd == NULL)
50 return;
52 for (i = 0; i < gd->data_sources_num; i++)
53 {
54 free (gd->data_sources[i].file);
55 free (gd->data_sources[i].name);
56 free (gd->data_sources[i].legend);
57 }
58 free (gd->data_sources);
59 free (gd);
60 } /* }}} void graph_def_free */
62 static int hsv_to_rgb (double *hsv, double *rgb) /* {{{ */
63 {
64 double c = hsv[2] * hsv[1];
65 double h = hsv[0] / 60.0;
66 double x = c * (1.0 - fabs (fmod (h, 2.0) - 1));
67 double m = hsv[2] - c;
69 rgb[0] = 0.0;
70 rgb[1] = 0.0;
71 rgb[2] = 0.0;
73 if ((0.0 <= h) && (h < 1.0)) { rgb[0] = 1.0; rgb[1] = x; rgb[2] = 0.0; }
74 else if ((1.0 <= h) && (h < 2.0)) { rgb[0] = x; rgb[1] = 1.0; rgb[2] = 0.0; }
75 else if ((2.0 <= h) && (h < 3.0)) { rgb[0] = 0.0; rgb[1] = 1.0; rgb[2] = x; }
76 else if ((3.0 <= h) && (h < 4.0)) { rgb[0] = 0.0; rgb[1] = x; rgb[2] = 1.0; }
77 else if ((4.0 <= h) && (h < 5.0)) { rgb[0] = x; rgb[1] = 0.0; rgb[2] = 1.0; }
78 else if ((5.0 <= h) && (h < 6.0)) { rgb[0] = 1.0; rgb[1] = 0.0; rgb[2] = x; }
80 rgb[0] += m;
81 rgb[1] += m;
82 rgb[2] += m;
84 return (0);
85 } /* }}} int hsv_to_rgb */
87 static uint32_t rgb_to_uint32 (double *rgb) /* {{{ */
88 {
89 uint8_t r;
90 uint8_t g;
91 uint8_t b;
93 r = (uint8_t) (255.0 * rgb[0]);
94 g = (uint8_t) (255.0 * rgb[1]);
95 b = (uint8_t) (255.0 * rgb[2]);
97 return ((((uint32_t) r) << 16)
98 | (((uint32_t) g) << 8)
99 | ((uint32_t) b));
100 } /* }}} uint32_t rgb_to_uint32 */
102 static uint32_t get_random_color (void) /* {{{ */
103 {
104 double hsv[3] = { 0.0, 1.0, 1.0 };
105 double rgb[3] = { 0.0, 0.0, 0.0 };
107 hsv[0] = 360.0 * ((double) rand ()) / (((double) RAND_MAX) + 1.0);
109 hsv_to_rgb (hsv, rgb);
111 return (rgb_to_uint32 (rgb));
112 } /* }}} uint32_t get_random_color */
114 static int graph_def_add_ds (graph_def_t *gd, /* {{{ */
115 const char *file, const char *ds_name)
116 {
117 data_source_t *ds;
119 ds = realloc (gd->data_sources, sizeof (*ds) * (gd->data_sources_num + 1));
120 if (ds == NULL)
121 return (ENOMEM);
122 gd->data_sources = ds;
124 ds = gd->data_sources + gd->data_sources_num;
125 memset (ds, 0, sizeof (*ds));
127 ds->file = strdup (file);
128 if (ds->file == NULL)
129 return (ENOMEM);
131 ds->name = strdup (ds_name);
132 if (ds->name == NULL)
133 {
134 free (ds->file);
135 return (ENOMEM);
136 }
138 ds->legend = NULL;
139 ds->color = get_random_color ();
141 gd->data_sources_num++;
143 return (0);
144 } /* }}} int graph_def_add_ds */
146 static graph_def_t *graph_def_from_rrd_file (char *file) /* {{{ */
147 {
148 graph_def_t *gd;
149 char **dses = NULL;
150 size_t dses_num = 0;
151 int status;
152 size_t i;
154 gd = malloc (sizeof (*gd));
155 if (gd == NULL)
156 return (NULL);
157 memset (gd, 0, sizeof (*gd));
158 gd->data_sources = NULL;
160 status = ds_list_from_rrd_file (file, &dses_num, &dses);
161 if (status != 0)
162 {
163 free (gd);
164 return (NULL);
165 }
167 for (i = 0; i < dses_num; i++)
168 {
169 graph_def_add_ds (gd, file, dses[i]);
170 free (dses[i]);
171 }
173 free (dses);
175 return (gd);
176 } /* }}} graph_def_t *graph_def_from_rrd_file */
178 static graph_def_t *graph_def_from_gl (const graph_list_t *gl) /* {{{ */
179 {
180 char rrd_file[PATH_MAX];
182 if ((gl->plugin_instance == NULL) && (gl->type_instance == NULL))
183 snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s/%s.rrd",
184 DATA_DIR, gl->host, gl->plugin, gl->type);
185 else if (gl->type_instance == NULL)
186 snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s-%s/%s.rrd",
187 DATA_DIR, gl->host, gl->plugin, gl->plugin_instance, gl->type);
188 else if (gl->plugin_instance == NULL)
189 snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s/%s-%s.rrd",
190 DATA_DIR, gl->host, gl->plugin, gl->type, gl->type_instance);
191 else
192 snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s-%s/%s-%s.rrd",
193 DATA_DIR, gl->host, gl->plugin, gl->plugin_instance,
194 gl->type, gl->type_instance);
195 rrd_file[sizeof (rrd_file) - 1] = 0;
197 return (graph_def_from_rrd_file (rrd_file));
198 } /* }}} graph_def_t *graph_def_from_gl */
200 static int draw_graph_ds (graph_def_t *gd, /* {{{ */
201 size_t index, str_array_t *args)
202 {
203 data_source_t *ds;
205 assert (index < gd->data_sources_num);
207 ds = gd->data_sources + index;
209 /* CDEFs */
210 array_append_format (args, "DEF:def_%04zu_min=%s:%s:MIN",
211 index, ds->file, ds->name);
212 array_append_format (args, "DEF:def_%04zu_avg=%s:%s:AVERAGE",
213 index, ds->file, ds->name);
214 array_append_format (args, "DEF:def_%04zu_max=%s:%s:MAX",
215 index, ds->file, ds->name);
216 /* VDEFs */
217 array_append_format (args, "VDEF:vdef_%04zu_min=def_%04zu_min,MINIMUM",
218 index, index);
219 array_append_format (args, "VDEF:vdef_%04zu_avg=def_%04zu_avg,AVERAGE",
220 index, index);
221 array_append_format (args, "VDEF:vdef_%04zu_max=def_%04zu_max,MAXIMUM",
222 index, index);
223 array_append_format (args, "VDEF:vdef_%04zu_lst=def_%04zu_avg,LAST",
224 index, index);
226 /* Graph part */
227 array_append_format (args, "LINE1:def_%04zu_avg#%06x:%s", index, ds->color,
228 (ds->legend != NULL) ? ds->legend : ds->name);
229 array_append_format (args, "GPRINT:vdef_%04zu_min:%%lg min,", index);
230 array_append_format (args, "GPRINT:vdef_%04zu_avg:%%lg avg,", index);
231 array_append_format (args, "GPRINT:vdef_%04zu_max:%%lg max,", index);
232 array_append_format (args, "GPRINT:vdef_%04zu_lst:%%lg last\\l", index);
234 return (0);
235 } /* }}} int draw_graph_ds */
237 static void emulate_graph (int argc, char **argv) /* {{{ */
238 {
239 int i;
241 printf ("rrdtool \\\n");
242 for (i = 0; i < argc; i++)
243 {
244 if (i < (argc - 1))
245 printf (" \"%s\" \\\n", argv[i]);
246 else
247 printf (" \"%s\"\n", argv[i]);
248 }
249 } /* }}} void emulate_graph */
251 static int ag_info_print (rrd_info_t *info) /* {{{ */
252 {
253 if (info->type == RD_I_VAL)
254 printf ("[info] %s = %g;\n", info->key, info->value.u_val);
255 else if (info->type == RD_I_CNT)
256 printf ("[info] %s = %lu;\n", info->key, info->value.u_cnt);
257 else if (info->type == RD_I_STR)
258 printf ("[info] %s = %s;\n", info->key, info->value.u_str);
259 else if (info->type == RD_I_INT)
260 printf ("[info] %s = %i;\n", info->key, info->value.u_int);
261 else if (info->type == RD_I_BLO)
262 printf ("[info] %s = [blob, %lu bytes];\n", info->key, info->value.u_blo.size);
263 else
264 printf ("[info] %s = [unknown type %#x];\n", info->key, info->type);
266 return (0);
267 } /* }}} int ag_info_print */
269 static int output_graph (rrd_info_t *info) /* {{{ */
270 {
271 rrd_info_t *img;
273 for (img = info; img != NULL; img = img->next)
274 if ((strcmp ("image", img->key) == 0)
275 && (img->type == RD_I_BLO))
276 break;
278 if (img == NULL)
279 return (ENOENT);
281 printf ("Content-Type: image/png\n"
282 "Content-Length: %lu\n"
283 "\n",
284 img->value.u_blo.size);
285 fwrite (img->value.u_blo.ptr, img->value.u_blo.size,
286 /* nmemb = */ 1, stdout);
288 return (0);
289 } /* }}} int output_graph */
291 #define OUTPUT_ERROR(...) do { \
292 printf ("Content-Type: text/plain\n\n"); \
293 printf (__VA_ARGS__); \
294 return (0); \
295 } while (0)
297 static int init_gl (graph_list_t *gl) /* {{{ */
298 {
299 gl->host = param ("host");
300 gl->plugin = param ("plugin");
301 gl->plugin_instance = param ("plugin_instance");
302 gl->type = param ("type");
303 gl->type_instance = param ("type_instance");
305 if ((gl->host == NULL)
306 || (gl->plugin == NULL)
307 || (gl->type == NULL))
308 return (EINVAL);
310 if ((gl->host[0] == 0) || (gl->host[0] == '.')
311 || (gl->plugin[0] == 0) || (gl->plugin[0] == '.')
312 || (gl->type[0] == 0) || (gl->type[0] == '.'))
313 return (EINVAL);
315 if ((strchr (gl->plugin, '-') != NULL)
316 || (strchr (gl->type, '-') != NULL))
317 return (EINVAL);
319 if ((gl->plugin_instance != NULL)
320 && (gl->plugin_instance[0] == 0))
321 gl->plugin_instance = NULL;
323 if ((gl->type_instance != NULL)
324 && (gl->type_instance[0] == 0))
325 gl->type_instance = NULL;
327 return (0);
328 } /* }}} int init_gl */
330 int action_graph (void) /* {{{ */
331 {
332 str_array_t *args;
333 graph_config_t *cfg;
334 graph_instance_t *inst;
335 rrd_info_t *info;
336 int status;
338 cfg = graph_get_selected ();
339 if (cfg == NULL)
340 OUTPUT_ERROR ("graph_get_selected () failed.\n");
342 inst = inst_get_selected (cfg);
343 if (inst == NULL)
344 OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
346 args = array_create ();
347 if (args == NULL)
348 return (ENOMEM);
350 array_append (args, "graph");
351 array_append (args, "-");
352 array_append (args, "--imgformat");
353 array_append (args, "PNG");
355 status = gl_instance_get_rrdargs (cfg, inst, args);
356 if (status != 0)
357 {
358 array_destroy (args);
359 OUTPUT_ERROR ("gl_instance_get_rrdargs failed with status %i.\n", status);
360 }
362 rrd_clear_error ();
363 info = rrd_graph_v (array_argc (args), array_argv (args));
364 if ((info == NULL) || rrd_test_error ())
365 {
366 printf ("Content-Type: text/plain\n\n");
367 printf ("rrd_graph_v failed: %s\n", rrd_get_error ());
368 emulate_graph (array_argc (args), array_argv (args));
369 }
370 else
371 {
372 int status;
374 status = output_graph (info);
375 if (status != 0)
376 {
377 rrd_info_t *ptr;
379 printf ("Content-Type: text/plain\n\n");
380 printf ("output_graph failed. Maybe the \"image\" info was not found?\n\n");
382 for (ptr = info; ptr != NULL; ptr = ptr->next)
383 {
384 ag_info_print (ptr);
385 }
386 }
387 }
389 if (info != NULL)
390 rrd_info_free (info);
392 array_destroy (args);
393 args = NULL;
395 return (0);
396 } /* }}} int action_graph */
398 /* vim: set sw=2 sts=2 et fdm=marker : */