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 return (NULL);
163 selector = graph_get_selector (cfg);
164 if (selector == NULL)
165 return (NULL);
167 ret = malloc (sizeof (*ret));
168 if (ret == NULL)
169 {
170 ident_destroy (selector);
171 return (NULL);
172 }
173 memset (ret, 0, sizeof (*ret));
174 ret->legend = NULL;
175 ret->format = NULL;
177 ret->ds_name = strdup (ds_name);
178 if (ret->ds_name == NULL)
179 {
180 ident_destroy (selector);
181 free (ret);
182 return (NULL);
183 }
185 ret->color = get_random_color ();
186 ret->next = NULL;
188 ret->select = ident_copy_with_selector (selector, ident,
189 IDENT_FLAG_REPLACE_ALL);
190 if (ret->select == NULL)
191 {
192 ident_destroy (selector);
193 free (ret->ds_name);
194 free (ret);
195 return (NULL);
196 }
198 ident_destroy (selector);
199 return (ret);
200 } /* }}} graph_def_t *def_create */
202 void def_destroy (graph_def_t *def) /* {{{ */
203 {
204 graph_def_t *next;
206 if (def == NULL)
207 return;
209 next = def->next;
211 ident_destroy (def->select);
213 free (def->ds_name);
214 free (def->legend);
215 free (def->format);
217 free (def);
219 def_destroy (next);
220 } /* }}} void def_destroy */
222 int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
223 {
224 graph_def_t *def;
225 int i;
227 def = def_config_get_obj (cfg, ci);
228 if (def == NULL)
229 return (EINVAL);
231 for (i = 0; i < ci->children_num; i++)
232 {
233 oconfig_item_t *child;
235 child = ci->children + i;
236 if (strcasecmp ("Legend", child->key) == 0)
237 graph_config_get_string (child, &def->legend);
238 else if (strcasecmp ("Color", child->key) == 0)
239 def_config_color (child, &def->color);
240 else if (strcasecmp ("Stack", child->key) == 0)
241 graph_config_get_bool (child, &def->stack);
242 else if (strcasecmp ("Area", child->key) == 0)
243 graph_config_get_bool (child, &def->area);
244 else if (strcasecmp ("Format", child->key) == 0)
245 graph_config_get_string (child, &def->format);
246 }
248 return (graph_add_def (cfg, def));
249 } /* }}} int def_config */
251 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
252 {
253 graph_def_t *ptr;
255 if ((head == NULL) || (def == NULL))
256 return (EINVAL);
258 ptr = head;
259 while (ptr->next != NULL)
260 ptr = ptr->next;
262 ptr->next = def;
264 return (0);
265 } /* }}} int def_append */
267 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
268 const char *ds_name)
269 {
270 graph_def_t *ptr;
272 if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
273 return (NULL);
275 for (ptr = head; ptr != NULL; ptr = ptr->next)
276 {
277 if (!ident_matches (ptr->select, ident))
278 continue;
280 if (strcmp (ptr->ds_name, ds_name) == 0)
281 return (ptr);
282 }
284 return (NULL);
285 } /* }}} graph_def_t *def_search */
287 _Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
288 {
289 return (ident_matches (def->select, ident));
290 } /* }}} _Bool def_matches */
292 int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
293 void *user_data)
294 {
295 graph_def_t *ptr;
297 if ((def == NULL) || (callback == NULL))
298 return (EINVAL);
300 for (ptr = def; ptr != NULL; ptr = ptr->next)
301 {
302 int status;
304 status = (*callback) (ptr, user_data);
305 if (status != 0)
306 return (status);
307 }
309 return (0);
310 } /* }}} int def_foreach */
312 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
313 rrd_args_t *args)
314 {
315 char *file;
316 int index;
317 char draw_def[64];
319 if ((def == NULL) || (ident == NULL) || (args == NULL))
320 return (EINVAL);
322 file = ident_to_file (ident);
323 if (file == NULL)
324 {
325 DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
326 return (-1);
327 }
329 DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
331 index = args->index;
332 args->index++;
334 /* CDEFs */
335 array_append_format (args->data, "DEF:def_%04i_min=%s:%s:MIN",
336 index, file, def->ds_name);
337 array_append_format (args->data, "DEF:def_%04i_avg=%s:%s:AVERAGE",
338 index, file, def->ds_name);
339 array_append_format (args->data, "DEF:def_%04i_max=%s:%s:MAX",
340 index, file, def->ds_name);
341 /* VDEFs */
342 array_append_format (args->data, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
343 index, index);
344 array_append_format (args->data, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
345 index, index);
346 array_append_format (args->data, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
347 index, index);
348 array_append_format (args->data, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
349 index, index);
351 if (def->stack)
352 {
353 if (args->last_stack_cdef[0] != 0)
354 {
355 array_append_format (args->calc, "CDEF:cdef_%04i_stack=%s,def_%04i_avg,+",
356 index, args->last_stack_cdef, index);
357 snprintf (draw_def, sizeof (draw_def), "cdef_%04i_stack", index);
358 }
359 else
360 {
361 snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
362 }
363 }
364 else
365 {
366 snprintf (draw_def, sizeof (draw_def), "def_%04i_avg", index);
367 }
369 if (def->area)
370 array_prepend_format (args->areas, "AREA:%s#%06"PRIx32,
371 draw_def, fade_color (def->color));
373 /* Graph part */
374 array_prepend_format (args->lines, "GPRINT:vdef_%04i_lst:%s last\\l",
375 index, (def->format != NULL) ? def->format : "%6.2lf");
376 array_prepend_format (args->lines, "GPRINT:vdef_%04i_max:%s max,",
377 index, (def->format != NULL) ? def->format : "%6.2lf");
378 array_prepend_format (args->lines, "GPRINT:vdef_%04i_avg:%s avg,",
379 index, (def->format != NULL) ? def->format : "%6.2lf");
380 array_prepend_format (args->lines, "GPRINT:vdef_%04i_min:%s min,",
381 index, (def->format != NULL) ? def->format : "%6.2lf");
382 array_prepend_format (args->lines, "LINE1:%s#%06"PRIx32":%s",
383 draw_def, def->color,
384 (def->legend != NULL) ? def->legend : def->ds_name);
386 free (file);
388 memcpy (args->last_stack_cdef, draw_def, sizeof (args->last_stack_cdef));
390 return (0);
391 } /* }}} int def_get_rrdargs */
393 /* vim: set sw=2 sts=2 et fdm=marker : */