1 /**
2 * collection4 - graph_def.c
3 * Copyright (C) 2010 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Florian octo Forster <ff at octo.it>
22 **/
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
28 #include "graph_def.h"
29 #include "graph.h"
30 #include "graph_config.h"
31 #include "graph_ident.h"
32 #include "common.h"
33 #include "oconfig.h"
35 #include <fcgiapp.h>
36 #include <fcgi_stdio.h>
38 /*
39 * Data structures
40 */
41 struct graph_def_s
42 {
43 graph_ident_t *select;
45 char *ds_name;
46 char *legend;
47 uint32_t color;
48 _Bool stack;
49 _Bool area;
50 char *format;
52 graph_def_t *next;
53 };
55 /*
56 * Private functions
57 */
58 #define DEF_CONFIG_FIELD(field) \
59 static int def_config_##field (const oconfig_item_t *ci, graph_ident_t *ident) \
60 { \
61 char *tmp = NULL; \
62 int status = graph_config_get_string (ci, &tmp); \
63 if (status != 0) \
64 return (status); \
65 ident_set_##field (ident, tmp); \
66 free (tmp); \
67 return (0); \
68 } /* }}} int def_config_field */
70 DEF_CONFIG_FIELD (host);
71 DEF_CONFIG_FIELD (plugin);
72 DEF_CONFIG_FIELD (plugin_instance);
73 DEF_CONFIG_FIELD (type);
74 DEF_CONFIG_FIELD (type_instance);
76 #undef DEF_CONFIG_FIELD
78 static int def_config_color (const oconfig_item_t *ci, uint32_t *ret_color) /* {{{ */
79 {
80 char *tmp;
81 char *endptr;
82 uint32_t color;
84 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
85 return (EINVAL);
87 tmp = ci->values[0].value.string;
89 endptr = NULL;
90 errno = 0;
91 color = (uint32_t) strtoul (tmp, &endptr, /* base = */ 16);
92 if ((errno != 0) || (endptr == tmp) || (color > 0x00ffffff))
93 return (EINVAL);
95 *ret_color = color;
97 return (0);
98 } /* }}} int def_config_color */
100 static graph_def_t *def_config_get_obj (graph_config_t *cfg, /* {{{ */
101 const oconfig_item_t *ci)
102 {
103 graph_ident_t *ident;
104 char *ds_name = NULL;
105 graph_def_t *def;
106 int i;
108 ident = graph_get_selector (cfg);
109 if (ident == NULL)
110 {
111 fprintf (stderr, "def_config_get_obj: graph_get_selector failed");
112 return (NULL);
113 }
115 for (i = 0; i < ci->children_num; i++)
116 {
117 oconfig_item_t *child;
119 #define HANDLE_FIELD(name,field) \
120 else if (strcasecmp (name, child->key) == 0) \
121 def_config_##field (child, ident)
123 child = ci->children + i;
124 if (strcasecmp ("DSName", child->key) == 0)
125 graph_config_get_string (child, &ds_name);
127 HANDLE_FIELD ("Host", host);
128 HANDLE_FIELD ("Plugin", plugin);
129 HANDLE_FIELD ("PluginInstance", plugin_instance);
130 HANDLE_FIELD ("Type", type);
131 HANDLE_FIELD ("TypeInstance", type_instance);
133 #undef HANDLE_FIELD
134 }
136 def = def_create (cfg, ident, ds_name);
137 if (def == NULL)
138 {
139 fprintf (stderr, "def_config_get_obj: def_create failed\n");
140 ident_destroy (ident);
141 free (ds_name);
142 return (NULL);
143 }
145 ident_destroy (ident);
146 free (ds_name);
148 return (def);
149 } /* }}} graph_def_t *def_config_get_obj */
151 /*
152 * Public functions
153 */
154 graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
155 const char *ds_name)
156 {
157 graph_ident_t *selector;
158 graph_def_t *ret;
160 if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
161 {
162 fprintf (stderr, "def_create: An argument is NULL\n");
163 return (NULL);
164 }
166 selector = graph_get_selector (cfg);
167 if (selector == NULL)
168 {
169 fprintf (stderr, "def_create: graph_get_selector failed\n");
170 return (NULL);
171 }
173 ret = malloc (sizeof (*ret));
174 if (ret == NULL)
175 {
176 fprintf (stderr, "def_create: malloc failed\n");
177 ident_destroy (selector);
178 return (NULL);
179 }
180 memset (ret, 0, sizeof (*ret));
181 ret->legend = NULL;
182 ret->format = NULL;
184 ret->ds_name = strdup (ds_name);
185 if (ret->ds_name == NULL)
186 {
187 fprintf (stderr, "def_create: Unable to copy DS name\n");
188 ident_destroy (selector);
189 free (ret);
190 return (NULL);
191 }
193 ret->color = UINT32_MAX;
194 ret->next = NULL;
196 ret->select = ident_copy_with_selector (selector, ident,
197 IDENT_FLAG_REPLACE_ANY);
198 if (ret->select == NULL)
199 {
200 fprintf (stderr, "def_create: ident_copy_with_selector failed\n");
201 ident_destroy (selector);
202 free (ret->ds_name);
203 free (ret);
204 return (NULL);
205 }
207 ident_destroy (selector);
208 return (ret);
209 } /* }}} graph_def_t *def_create */
211 void def_destroy (graph_def_t *def) /* {{{ */
212 {
213 graph_def_t *next;
215 if (def == NULL)
216 return;
218 next = def->next;
220 ident_destroy (def->select);
222 free (def->ds_name);
223 free (def->legend);
224 free (def->format);
226 free (def);
228 def_destroy (next);
229 } /* }}} void def_destroy */
231 int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
232 {
233 graph_def_t *def;
234 int i;
236 def = def_config_get_obj (cfg, ci);
237 if (def == NULL)
238 return (EINVAL);
240 for (i = 0; i < ci->children_num; i++)
241 {
242 oconfig_item_t *child;
244 child = ci->children + i;
245 if (strcasecmp ("Legend", child->key) == 0)
246 graph_config_get_string (child, &def->legend);
247 else if (strcasecmp ("Color", child->key) == 0)
248 def_config_color (child, &def->color);
249 else if (strcasecmp ("Stack", child->key) == 0)
250 graph_config_get_bool (child, &def->stack);
251 else if (strcasecmp ("Area", child->key) == 0)
252 graph_config_get_bool (child, &def->area);
253 else if (strcasecmp ("Format", child->key) == 0)
254 graph_config_get_string (child, &def->format);
255 }
257 return (graph_add_def (cfg, def));
258 } /* }}} int def_config */
260 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
261 {
262 graph_def_t *ptr;
264 if ((head == NULL) || (def == NULL))
265 return (EINVAL);
267 ptr = head;
268 while (ptr->next != NULL)
269 ptr = ptr->next;
271 ptr->next = def;
273 return (0);
274 } /* }}} int def_append */
276 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
277 const char *ds_name)
278 {
279 graph_def_t *ptr;
281 if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
282 return (NULL);
284 for (ptr = head; ptr != NULL; ptr = ptr->next)
285 {
286 if (!ident_matches (ptr->select, ident))
287 continue;
289 if (strcmp (ptr->ds_name, ds_name) == 0)
290 return (ptr);
291 }
293 return (NULL);
294 } /* }}} graph_def_t *def_search */
296 _Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
297 {
298 return (ident_matches (def->select, ident));
299 } /* }}} _Bool def_matches */
301 int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
302 void *user_data)
303 {
304 graph_def_t *ptr;
306 if ((def == NULL) || (callback == NULL))
307 return (EINVAL);
309 for (ptr = def; ptr != NULL; ptr = ptr->next)
310 {
311 int status;
313 status = (*callback) (ptr, user_data);
314 if (status != 0)
315 return (status);
316 }
318 return (0);
319 } /* }}} int def_foreach */
321 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
322 rrd_args_t *args)
323 {
324 char *file;
325 int index;
326 char draw_def[64];
327 char legend[256];
328 uint32_t color;
330 if ((def == NULL) || (ident == NULL) || (args == NULL))
331 return (EINVAL);
333 file = ident_to_file (ident);
334 if (file == NULL)
335 {
336 DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
337 return (-1);
338 }
340 DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
342 if (def->legend != NULL)
343 {
344 strncpy (legend, def->legend, sizeof (legend));
345 legend[sizeof (legend) - 1] = 0;
346 }
347 else
348 {
349 ident_describe (ident, def->select,
350 legend, sizeof (legend));
352 if ((legend[0] == 0) || (strcmp ("default", legend) == 0))
353 {
354 strncpy (legend, def->ds_name, sizeof (legend));
355 legend[sizeof (legend) - 1] = 0;
356 }
357 }
359 color = def->color;
360 if (color > 0x00ffffff)
361 color = get_random_color ();
363 index = args->index;
364 args->index++;
366 /* CDEFs */
367 array_append_format (args->data, "DEF:def_%04i_min=%s:%s:MIN",
368 index, file, def->ds_name);
369 array_append_format (args->data, "DEF:def_%04i_avg=%s:%s:AVERAGE",
370 index, file, def->ds_name);
371 array_append_format (args->data, "DEF:def_%04i_max=%s:%s:MAX",
372 index, file, def->ds_name);
373 /* VDEFs */
374 array_append_format (args->data, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
375 index, index);
376 array_append_format (args->data, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
377 index, index);
378 array_append_format (args->data, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
379 index, index);
380 array_append_format (args->data, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
381 index, index);
383 if (def->stack)
384 {
385 if (args->last_stack_cdef[0] != 0)
386 {
387 array_append_format (args->calc, "CDEF:cdef_%04i_stack=%s,def_%04i_avg,+",
388 index, args->last_stack_cdef, index);
389 snprintf (draw_def, sizeof (draw_def), "cdef_%04i_stack", index);
390 }
391 else
392 {
393 snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
394 }
395 }
396 else
397 {
398 snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
399 }
401 if (def->area)
402 array_prepend_format (args->areas, "AREA:%s#%06"PRIx32,
403 draw_def, fade_color (color));
405 /* Graph part */
406 array_prepend_format (args->lines, "GPRINT:vdef_%04i_lst:%s last\\l",
407 index, (def->format != NULL) ? def->format : "%6.2lf");
408 array_prepend_format (args->lines, "GPRINT:vdef_%04i_max:%s max,",
409 index, (def->format != NULL) ? def->format : "%6.2lf");
410 array_prepend_format (args->lines, "GPRINT:vdef_%04i_avg:%s avg,",
411 index, (def->format != NULL) ? def->format : "%6.2lf");
412 array_prepend_format (args->lines, "GPRINT:vdef_%04i_min:%s min,",
413 index, (def->format != NULL) ? def->format : "%6.2lf");
414 array_prepend_format (args->lines, "LINE1:%s#%06"PRIx32":%s",
415 draw_def, color, legend);
417 free (file);
419 memcpy (args->last_stack_cdef, draw_def, sizeof (args->last_stack_cdef));
421 return (0);
422 } /* }}} int def_get_rrdargs */
424 /* vim: set sw=2 sts=2 et fdm=marker : */