7 #include <dirent.h> /* for PATH_MAX */
14 #include "action_graph.h"
15 #include "graph_list.h"
16 #include "utils_params.h"
17 #include "utils_array.h"
20 #include <fcgi_stdio.h>
32 typedef struct data_source_s data_source_t;
36 data_source_t *data_sources;
37 size_t data_sources_num;
43 typedef struct graph_def_s graph_def_t;
45 static void graph_def_free (graph_def_t *gd) /* {{{ */
52 for (i = 0; i < gd->data_sources_num; i++)
54 free (gd->data_sources[i].file);
55 free (gd->data_sources[i].name);
56 free (gd->data_sources[i].legend);
58 free (gd->data_sources);
60 } /* }}} void graph_def_free */
62 static int hsv_to_rgb (double *hsv, double *rgb) /* {{{ */
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;
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; }
85 } /* }}} int hsv_to_rgb */
87 static uint32_t rgb_to_uint32 (double *rgb) /* {{{ */
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)
100 } /* }}} uint32_t rgb_to_uint32 */
102 static uint32_t get_random_color (void) /* {{{ */
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)
119 ds = realloc (gd->data_sources, sizeof (*ds) * (gd->data_sources_num + 1));
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)
131 ds->name = strdup (ds_name);
132 if (ds->name == NULL)
139 ds->color = get_random_color ();
141 gd->data_sources_num++;
144 } /* }}} int graph_def_add_ds */
146 static graph_def_t *graph_def_from_rrd_file (char *file) /* {{{ */
154 gd = malloc (sizeof (*gd));
157 memset (gd, 0, sizeof (*gd));
158 gd->data_sources = NULL;
160 status = ds_list_from_rrd_file (file, &dses_num, &dses);
167 for (i = 0; i < dses_num; i++)
169 graph_def_add_ds (gd, file, dses[i]);
176 } /* }}} graph_def_t *graph_def_from_rrd_file */
178 static graph_def_t *graph_def_from_gl (const graph_list_t *gl) /* {{{ */
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);
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)
205 assert (index < gd->data_sources_num);
207 ds = gd->data_sources + index;
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);
217 array_append_format (args, "VDEF:vdef_%04zu_min=def_%04zu_min,MINIMUM",
219 array_append_format (args, "VDEF:vdef_%04zu_avg=def_%04zu_avg,AVERAGE",
221 array_append_format (args, "VDEF:vdef_%04zu_max=def_%04zu_max,MAXIMUM",
223 array_append_format (args, "VDEF:vdef_%04zu_lst=def_%04zu_avg,LAST",
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);
235 } /* }}} int draw_graph_ds */
237 static void emulate_graph (int argc, char **argv) /* {{{ */
241 printf ("rrdtool \\\n");
242 for (i = 0; i < argc; i++)
245 printf (" \"%s\" \\\n", argv[i]);
247 printf (" \"%s\"\n", argv[i]);
249 } /* }}} void emulate_graph */
251 static int ag_info_print (rrd_info_t *info) /* {{{ */
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);
264 printf ("[info] %s = [unknown type %#x];\n", info->key, info->type);
267 } /* }}} int ag_info_print */
269 static int output_graph (rrd_info_t *info) /* {{{ */
273 for (img = info; img != NULL; img = img->next)
274 if ((strcmp ("image", img->key) == 0)
275 && (img->type == RD_I_BLO))
281 printf ("Content-Type: image/png\n"
282 "Content-Length: %lu\n"
284 img->value.u_blo.size);
285 fwrite (img->value.u_blo.ptr, img->value.u_blo.size,
286 /* nmemb = */ 1, stdout);
289 } /* }}} int output_graph */
291 #define OUTPUT_ERROR(...) do { \
292 printf ("Content-Type: text/plain\n\n"); \
293 printf (__VA_ARGS__); \
297 static int init_gl (graph_list_t *gl) /* {{{ */
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))
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] == '.'))
315 if ((strchr (gl->plugin, '-') != NULL)
316 || (strchr (gl->type, '-') != NULL))
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;
328 } /* }}} int init_gl */
330 int action_graph (void) /* {{{ */
334 graph_instance_t *inst;
338 cfg = graph_get_selected ();
340 OUTPUT_ERROR ("graph_get_selected () failed.\n");
342 inst = inst_get_selected (cfg);
344 OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
346 args = array_create ();
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);
358 array_destroy (args);
359 OUTPUT_ERROR ("gl_instance_get_rrdargs failed with status %i.\n", status);
363 info = rrd_graph_v (array_argc (args), array_argv (args));
364 if ((info == NULL) || rrd_test_error ())
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));
374 status = output_graph (info);
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)
390 rrd_info_free (info);
392 array_destroy (args);
396 } /* }}} int action_graph */
398 /* vim: set sw=2 sts=2 et fdm=marker : */