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, /* {{{ */
116 const char *in_ds_name, size_t ds_name_len)
118 char ds_name[ds_name_len + 1];
121 strncpy (ds_name, in_ds_name, sizeof (ds_name));
122 ds_name[sizeof (ds_name) - 1] = 0;
124 ds = realloc (gd->data_sources, sizeof (*ds) * (gd->data_sources_num + 1));
127 gd->data_sources = ds;
129 ds = gd->data_sources + gd->data_sources_num;
130 memset (ds, 0, sizeof (*ds));
132 ds->file = strdup (file);
133 if (ds->file == NULL)
136 ds->name = strdup (ds_name);
137 if (ds->name == NULL)
144 ds->color = get_random_color ();
146 gd->data_sources_num++;
149 } /* }}} int graph_def_add_ds */
151 static graph_def_t *graph_def_from_rrd_file (char *file) /* {{{ */
153 char *rrd_argv[] = { "info", file, NULL };
154 int rrd_argc = (sizeof (rrd_argv) / sizeof (rrd_argv[0])) - 1;
159 gd = malloc (sizeof (*gd));
162 memset (gd, 0, sizeof (*gd));
164 gd->data_sources = NULL;
166 info = rrd_info (rrd_argc, rrd_argv);
169 printf ("%s: rrd_info (%s) failed.\n", __func__, file);
174 for (ptr = info; ptr != NULL; ptr = ptr->next)
179 if (strncmp ("ds[", ptr->key, strlen ("ds[")) != 0)
182 keylen = strlen (ptr->key);
183 if (keylen < strlen ("ds[?].index"))
186 dslen = keylen - strlen ("ds[].index");
189 if (strcmp ("].index", ptr->key + (strlen ("ds[") + dslen)) != 0)
192 graph_def_add_ds (gd, file, ptr->key + strlen ("ds["), dslen);
195 rrd_info_free (info);
198 } /* }}} graph_def_t *graph_def_from_rrd_file */
200 static graph_def_t *graph_def_from_gl (const graph_list_t *gl) /* {{{ */
202 char rrd_file[PATH_MAX];
204 if ((gl->plugin_instance == NULL) && (gl->type_instance == NULL))
205 snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s/%s.rrd",
206 DATA_DIR, gl->host, gl->plugin, gl->type);
207 else if (gl->type_instance == NULL)
208 snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s-%s/%s.rrd",
209 DATA_DIR, gl->host, gl->plugin, gl->plugin_instance, gl->type);
210 else if (gl->plugin_instance == NULL)
211 snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s/%s-%s.rrd",
212 DATA_DIR, gl->host, gl->plugin, gl->type, gl->type_instance);
214 snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s-%s/%s-%s.rrd",
215 DATA_DIR, gl->host, gl->plugin, gl->plugin_instance,
216 gl->type, gl->type_instance);
217 rrd_file[sizeof (rrd_file) - 1] = 0;
219 return (graph_def_from_rrd_file (rrd_file));
220 } /* }}} graph_def_t *graph_def_from_gl */
222 static int draw_graph_ds (graph_def_t *gd, /* {{{ */
223 size_t index, str_array_t *args)
227 assert (index < gd->data_sources_num);
229 ds = gd->data_sources + index;
232 array_append_format (args, "DEF:def_%04zu_min=%s:%s:MIN",
233 index, ds->file, ds->name);
234 array_append_format (args, "DEF:def_%04zu_avg=%s:%s:AVERAGE",
235 index, ds->file, ds->name);
236 array_append_format (args, "DEF:def_%04zu_max=%s:%s:MAX",
237 index, ds->file, ds->name);
239 array_append_format (args, "VDEF:vdef_%04zu_min=def_%04zu_min,MINIMUM",
241 array_append_format (args, "VDEF:vdef_%04zu_avg=def_%04zu_avg,AVERAGE",
243 array_append_format (args, "VDEF:vdef_%04zu_max=def_%04zu_max,MAXIMUM",
245 array_append_format (args, "VDEF:vdef_%04zu_lst=def_%04zu_avg,LAST",
249 array_append_format (args, "LINE1:def_%04zu_avg#%06x:%s", index, ds->color,
250 (ds->legend != NULL) ? ds->legend : ds->name);
251 array_append_format (args, "GPRINT:vdef_%04zu_min:%%lg min,", index);
252 array_append_format (args, "GPRINT:vdef_%04zu_avg:%%lg avg,", index);
253 array_append_format (args, "GPRINT:vdef_%04zu_max:%%lg max,", index);
254 array_append_format (args, "GPRINT:vdef_%04zu_lst:%%lg last\\l", index);
257 } /* }}} int draw_graph_ds */
259 static void emulate_graph (int argc, char **argv) /* {{{ */
263 printf ("rrdtool \\\n");
264 for (i = 0; i < argc; i++)
267 printf (" \"%s\" \\\n", argv[i]);
269 printf (" \"%s\"\n", argv[i]);
271 } /* }}} void emulate_graph */
273 static int ag_info_print (rrd_info_t *info)
275 if (info->type == RD_I_VAL)
276 printf ("[info] %s = %g;\n", info->key, info->value.u_val);
277 else if (info->type == RD_I_CNT)
278 printf ("[info] %s = %lu;\n", info->key, info->value.u_cnt);
279 else if (info->type == RD_I_STR)
280 printf ("[info] %s = %s;\n", info->key, info->value.u_str);
281 else if (info->type == RD_I_INT)
282 printf ("[info] %s = %i;\n", info->key, info->value.u_int);
283 else if (info->type == RD_I_BLO)
284 printf ("[info] %s = [blob, %lu bytes];\n", info->key, info->value.u_blo.size);
286 printf ("[info] %s = [unknown type %#x];\n", info->key, info->type);
289 } /* }}} int ag_info_print */
291 static int output_graph (rrd_info_t *info)
295 for (img = info; img != NULL; img = img->next)
296 if ((strcmp ("image", img->key) == 0)
297 && (img->type == RD_I_BLO))
303 printf ("Content-Type: image/png\n"
304 "Content-Length: %lu\n"
306 img->value.u_blo.size);
307 fwrite (img->value.u_blo.ptr, img->value.u_blo.size,
308 /* nmemb = */ 1, stdout);
311 } /* }}} int output_graph */
313 static int draw_graph (graph_def_t *gd) /* {{{ */
319 args = array_create ();
323 array_append (args, "graph");
324 array_append (args, "-");
325 array_append (args, "--imgformat");
326 array_append (args, "PNG");
328 for (i = 0; i < gd->data_sources_num; i++)
329 draw_graph_ds (gd, i, args);
332 info = rrd_graph_v (array_argc (args), array_argv (args));
333 if ((info == NULL) || rrd_test_error ())
335 printf ("Content-Type: text/plain\n\n");
336 printf ("rrd_graph_v failed: %s\n", rrd_get_error ());
337 emulate_graph (array_argc (args), array_argv (args));
343 status = output_graph (info);
348 printf ("Content-Type: text/plain\n\n");
349 printf ("output_graph failed. Maybe the \"image\" info was not found?\n\n");
351 for (ptr = info; ptr != NULL; ptr = ptr->next)
359 rrd_info_free (info);
361 array_destroy (args);
364 } /* }}} int draw_graph */
366 static int init_gl (graph_list_t *gl) /* {{{ */
368 gl->host = param ("host");
369 gl->plugin = param ("plugin");
370 gl->plugin_instance = param ("plugin_instance");
371 gl->type = param ("type");
372 gl->type_instance = param ("type_instance");
374 if ((gl->host == NULL)
375 || (gl->plugin == NULL)
376 || (gl->type == NULL))
379 if ((gl->host[0] == 0) || (gl->host[0] == '.')
380 || (gl->plugin[0] == 0) || (gl->plugin[0] == '.')
381 || (gl->type[0] == 0) || (gl->type[0] == '.'))
384 if ((strchr (gl->plugin, '-') != NULL)
385 || (strchr (gl->type, '-') != NULL))
388 if ((gl->plugin_instance != NULL)
389 && (gl->plugin_instance[0] == 0))
390 gl->plugin_instance = NULL;
392 if ((gl->type_instance != NULL)
393 && (gl->type_instance[0] == 0))
394 gl->type_instance = NULL;
397 } /* }}} int init_gl */
399 int action_graph (void) /* {{{ */
405 memset (&gl, 0, sizeof (gl));
406 status = init_gl (&gl);
409 printf ("Content-Type: text/plain\n\n"
410 "init_gl failed with status %i.\n", status);
414 gd = graph_def_from_gl (&gl);
417 printf ("graph_def_from_gl failed.\n");
425 } /* }}} int action_graph */
427 /* vim: set sw=2 sts=2 et fdm=marker : */