graph_list: Add basic configuration infrastructure.
[collection4.git] / action_graph.c
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>
10
11 #include <rrd.h>
12
13 #include "common.h"
14 #include "action_graph.h"
15 #include "graph_list.h"
16 #include "utils_params.h"
17 #include "utils_array.h"
18
19 #include <fcgiapp.h>
20 #include <fcgi_stdio.h>
21
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;
33
34 struct graph_def_s
35 {
36   data_source_t *data_sources;
37   size_t data_sources_num;
38
39   _Bool stack;
40
41   int def_num;
42 };
43 typedef struct graph_def_s graph_def_t;
44
45 static void graph_def_free (graph_def_t *gd) /* {{{ */
46 {
47   size_t i;
48
49   if (gd == NULL)
50     return;
51
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 */
61
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;
68
69   rgb[0] = 0.0;
70   rgb[1] = 0.0;
71   rgb[2] = 0.0;
72
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; }
79
80   rgb[0] += m;
81   rgb[1] += m;
82   rgb[2] += m;
83
84   return (0);
85 } /* }}} int hsv_to_rgb */
86
87 static uint32_t rgb_to_uint32 (double *rgb) /* {{{ */
88 {
89   uint8_t r;
90   uint8_t g;
91   uint8_t b;
92
93   r = (uint8_t) (255.0 * rgb[0]);
94   g = (uint8_t) (255.0 * rgb[1]);
95   b = (uint8_t) (255.0 * rgb[2]);
96
97   return ((((uint32_t) r) << 16)
98       | (((uint32_t) g) << 8)
99       | ((uint32_t) b));
100 } /* }}} uint32_t rgb_to_uint32 */
101
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 };
106
107   hsv[0] = 360.0 * ((double) rand ()) / (((double) RAND_MAX) + 1.0);
108
109   hsv_to_rgb (hsv, rgb);
110
111   return (rgb_to_uint32 (rgb));
112 } /* }}} uint32_t get_random_color */
113
114 static int graph_def_add_ds (graph_def_t *gd, /* {{{ */
115     const char *file,
116     const char *in_ds_name, size_t ds_name_len)
117 {
118   char ds_name[ds_name_len + 1];
119   data_source_t *ds;
120
121   strncpy (ds_name, in_ds_name, sizeof (ds_name));
122   ds_name[sizeof (ds_name) - 1] = 0;
123
124   ds = realloc (gd->data_sources, sizeof (*ds) * (gd->data_sources_num + 1));
125   if (ds == NULL)
126     return (ENOMEM);
127   gd->data_sources = ds;
128
129   ds = gd->data_sources + gd->data_sources_num;
130   memset (ds, 0, sizeof (*ds));
131
132   ds->file = strdup (file);
133   if (ds->file == NULL)
134     return (ENOMEM);
135
136   ds->name = strdup (ds_name);
137   if (ds->name == NULL)
138   {
139     free (ds->file);
140     return (ENOMEM);
141   }
142
143   ds->legend = NULL;
144   ds->color = get_random_color ();
145
146   gd->data_sources_num++;
147
148   return (0);
149 } /* }}} int graph_def_add_ds */
150
151 static graph_def_t *graph_def_from_rrd_file (char *file) /* {{{ */
152 {
153   char *rrd_argv[] = { "info", file, NULL };
154   int rrd_argc = (sizeof (rrd_argv) / sizeof (rrd_argv[0])) - 1;
155   rrd_info_t *info;
156   rrd_info_t *ptr;
157   graph_def_t *gd;
158
159   gd = malloc (sizeof (*gd));
160   if (gd == NULL)
161     return (NULL);
162   memset (gd, 0, sizeof (*gd));
163
164   gd->data_sources = NULL;
165
166   info = rrd_info (rrd_argc, rrd_argv);
167   if (info == NULL)
168   {
169     printf ("%s: rrd_info (%s) failed.\n", __func__, file);
170     free (gd);
171     return (NULL);
172   }
173
174   for (ptr = info; ptr != NULL; ptr = ptr->next)
175   {
176     size_t keylen;
177     size_t dslen;
178
179     if (strncmp ("ds[", ptr->key, strlen ("ds[")) != 0)
180       continue;
181
182     keylen = strlen (ptr->key);
183     if (keylen < strlen ("ds[?].index"))
184       continue;
185
186     dslen = keylen - strlen ("ds[].index");
187     assert (dslen >= 1);
188
189     if (strcmp ("].index", ptr->key + (strlen ("ds[") + dslen)) != 0)
190       continue;
191
192     graph_def_add_ds (gd, file, ptr->key + strlen ("ds["), dslen);
193   }
194
195   rrd_info_free (info);
196
197   return (gd);
198 } /* }}} graph_def_t *graph_def_from_rrd_file */
199
200 static graph_def_t *graph_def_from_gl (const graph_list_t *gl) /* {{{ */
201 {
202   char rrd_file[PATH_MAX];
203
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);
213   else
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;
218
219   return (graph_def_from_rrd_file (rrd_file));
220 } /* }}} graph_def_t *graph_def_from_gl */
221
222 static int draw_graph_ds (graph_def_t *gd, /* {{{ */
223     size_t index, str_array_t *args)
224 {
225   data_source_t *ds;
226
227   assert (index < gd->data_sources_num);
228
229   ds = gd->data_sources + index;
230
231   /* CDEFs */
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);
238   /* VDEFs */
239   array_append_format (args, "VDEF:vdef_%04zu_min=def_%04zu_min,MINIMUM",
240       index, index);
241   array_append_format (args, "VDEF:vdef_%04zu_avg=def_%04zu_avg,AVERAGE",
242       index, index);
243   array_append_format (args, "VDEF:vdef_%04zu_max=def_%04zu_max,MAXIMUM",
244       index, index);
245   array_append_format (args, "VDEF:vdef_%04zu_lst=def_%04zu_avg,LAST",
246       index, index);
247
248   /* Graph part */
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);
255
256   return (0);
257 } /* }}} int draw_graph_ds */
258
259 static void emulate_graph (int argc, char **argv) /* {{{ */
260 {
261   int i;
262
263   printf ("rrdtool \\\n");
264   for (i = 0; i < argc; i++)
265   {
266     if (i < (argc - 1))
267       printf ("  \"%s\" \\\n", argv[i]);
268     else
269       printf ("  \"%s\"\n", argv[i]);
270   }
271 } /* }}} void emulate_graph */
272
273 static int ag_info_print (rrd_info_t *info)
274 {
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);
285   else
286     printf ("[info] %s = [unknown type %#x];\n", info->key, info->type);
287
288   return (0);
289 } /* }}} int ag_info_print */
290
291 static int output_graph (rrd_info_t *info)
292 {
293   rrd_info_t *img;
294
295   for (img = info; img != NULL; img = img->next)
296     if ((strcmp ("image", img->key) == 0)
297         && (img->type == RD_I_BLO))
298       break;
299
300   if (img == NULL)
301     return (ENOENT);
302
303   printf ("Content-Type: image/png\n"
304       "Content-Length: %lu\n"
305       "\n",
306       img->value.u_blo.size);
307   fwrite (img->value.u_blo.ptr, img->value.u_blo.size,
308       /* nmemb = */ 1, stdout);
309
310   return (0);
311 } /* }}} int output_graph */
312
313 static int draw_graph (graph_def_t *gd) /* {{{ */
314 {
315   str_array_t *args;
316   rrd_info_t *info;
317   size_t i;
318
319   args = array_create ();
320   if (args == NULL)
321     return (ENOMEM);
322
323   array_append (args, "graph");
324   array_append (args, "-");
325   array_append (args, "--imgformat");
326   array_append (args, "PNG");
327
328   for (i = 0; i < gd->data_sources_num; i++)
329     draw_graph_ds (gd, i, args);
330
331   rrd_clear_error ();
332   info = rrd_graph_v (array_argc (args), array_argv (args));
333   if ((info == NULL) || rrd_test_error ())
334   {
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));
338   }
339   else
340   {
341     int status;
342
343     status = output_graph (info);
344     if (status != 0)
345     {
346       rrd_info_t *ptr;
347
348       printf ("Content-Type: text/plain\n\n");
349       printf ("output_graph failed. Maybe the \"image\" info was not found?\n\n");
350
351       for (ptr = info; ptr != NULL; ptr = ptr->next)
352       {
353         ag_info_print (ptr);
354       }
355     }
356   }
357
358   if (info != NULL)
359     rrd_info_free (info);
360
361   array_destroy (args);
362
363   return (0);
364 } /* }}} int draw_graph */
365
366 static int init_gl (graph_list_t *gl) /* {{{ */
367 {
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");
373
374   if ((gl->host == NULL)
375       || (gl->plugin == NULL)
376       || (gl->type == NULL))
377     return (EINVAL);
378
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] == '.'))
382     return (EINVAL);
383
384   if ((strchr (gl->plugin, '-') != NULL)
385       || (strchr (gl->type, '-') != NULL))
386     return (EINVAL);
387
388   if ((gl->plugin_instance != NULL)
389       && (gl->plugin_instance[0] == 0))
390     gl->plugin_instance = NULL;
391
392   if ((gl->type_instance != NULL)
393       && (gl->type_instance[0] == 0))
394     gl->type_instance = NULL;
395
396   return (0);
397 } /* }}} int init_gl */
398
399 int action_graph (void) /* {{{ */
400 {
401   graph_list_t gl;
402   graph_def_t *gd;
403   int status;
404
405   memset (&gl, 0, sizeof (gl));
406   status = init_gl (&gl);
407   if (status != 0)
408   {
409     printf ("Content-Type: text/plain\n\n"
410         "init_gl failed with status %i.\n", status);
411     return (status);
412   }
413
414   gd = graph_def_from_gl (&gl);
415   if (gd == NULL)
416   {
417     printf ("graph_def_from_gl failed.\n");
418     return (0);
419   }
420
421   draw_graph (gd);
422   graph_def_free (gd);
423
424   return (0);
425 } /* }}} int action_graph */
426
427 /* vim: set sw=2 sts=2 et fdm=marker : */