7 #include "graph_config.h"
8 #include "graph_ident.h"
13 #include <fcgi_stdio.h>
20 graph_ident_t *select;
35 #define DEF_CONFIG_FIELD(field) \
36 static int def_config_##field (const oconfig_item_t *ci, graph_ident_t *ident) \
39 int status = graph_config_get_string (ci, &tmp); \
42 ident_set_##field (ident, tmp); \
45 } /* }}} int def_config_field */
47 DEF_CONFIG_FIELD (host);
48 DEF_CONFIG_FIELD (plugin);
49 DEF_CONFIG_FIELD (plugin_instance);
50 DEF_CONFIG_FIELD (type);
51 DEF_CONFIG_FIELD (type_instance);
53 #undef DEF_CONFIG_FIELD
55 static int def_config_color (const oconfig_item_t *ci, uint32_t *ret_color) /* {{{ */
61 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
64 tmp = ci->values[0].value.string;
68 color = (uint32_t) strtoul (tmp, &endptr, /* base = */ 16);
69 if ((errno != 0) || (endptr == tmp) || (color > 0x00ffffff))
75 } /* }}} int def_config_color */
77 static graph_def_t *def_config_get_obj (graph_config_t *cfg, /* {{{ */
78 const oconfig_item_t *ci)
85 ident = graph_get_selector (cfg);
88 fprintf (stderr, "def_config_get_obj: graph_get_selector failed");
92 for (i = 0; i < ci->children_num; i++)
94 oconfig_item_t *child;
96 #define HANDLE_FIELD(name,field) \
97 else if (strcasecmp (name, child->key) == 0) \
98 def_config_##field (child, ident)
100 child = ci->children + i;
101 if (strcasecmp ("DSName", child->key) == 0)
102 graph_config_get_string (child, &ds_name);
104 HANDLE_FIELD ("Host", host);
105 HANDLE_FIELD ("Plugin", plugin);
106 HANDLE_FIELD ("PluginInstance", plugin_instance);
107 HANDLE_FIELD ("Type", type);
108 HANDLE_FIELD ("TypeInstance", type_instance);
113 def = def_create (cfg, ident, ds_name);
116 fprintf (stderr, "def_config_get_obj: def_create failed\n");
117 ident_destroy (ident);
122 ident_destroy (ident);
126 } /* }}} graph_def_t *def_config_get_obj */
131 graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
134 graph_ident_t *selector;
137 if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
140 selector = graph_get_selector (cfg);
141 if (selector == NULL)
144 ret = malloc (sizeof (*ret));
147 ident_destroy (selector);
150 memset (ret, 0, sizeof (*ret));
154 ret->ds_name = strdup (ds_name);
155 if (ret->ds_name == NULL)
157 ident_destroy (selector);
162 ret->color = get_random_color ();
165 ret->select = ident_copy_with_selector (selector, ident,
166 IDENT_FLAG_REPLACE_ALL);
167 if (ret->select == NULL)
169 ident_destroy (selector);
175 ident_destroy (selector);
177 } /* }}} graph_def_t *def_create */
179 void def_destroy (graph_def_t *def) /* {{{ */
188 ident_destroy (def->select);
197 } /* }}} void def_destroy */
199 int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
204 def = def_config_get_obj (cfg, ci);
208 for (i = 0; i < ci->children_num; i++)
210 oconfig_item_t *child;
212 child = ci->children + i;
213 if (strcasecmp ("Legend", child->key) == 0)
214 graph_config_get_string (child, &def->legend);
215 else if (strcasecmp ("Color", child->key) == 0)
216 def_config_color (child, &def->color);
217 else if (strcasecmp ("Stack", child->key) == 0)
218 graph_config_get_bool (child, &def->stack);
219 else if (strcasecmp ("Area", child->key) == 0)
220 graph_config_get_bool (child, &def->area);
221 else if (strcasecmp ("Format", child->key) == 0)
222 graph_config_get_string (child, &def->format);
225 return (graph_add_def (cfg, def));
226 } /* }}} int def_config */
228 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
232 if ((head == NULL) || (def == NULL))
236 while (ptr->next != NULL)
242 } /* }}} int def_append */
244 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
249 if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
252 for (ptr = head; ptr != NULL; ptr = ptr->next)
254 if (!ident_matches (ptr->select, ident))
257 if (strcmp (ptr->ds_name, ds_name) == 0)
262 } /* }}} graph_def_t *def_search */
264 _Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
266 return (ident_matches (def->select, ident));
267 } /* }}} _Bool def_matches */
269 int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
274 if ((def == NULL) || (callback == NULL))
277 for (ptr = def; ptr != NULL; ptr = ptr->next)
281 status = (*callback) (ptr, user_data);
287 } /* }}} int def_foreach */
289 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
295 if ((def == NULL) || (ident == NULL) || (args == NULL))
298 file = ident_to_file (ident);
301 DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
305 DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
307 index = array_argc (args);
310 array_append_format (args, "DEF:def_%04i_min=%s:%s:MIN",
311 index, file, def->ds_name);
312 array_append_format (args, "DEF:def_%04i_avg=%s:%s:AVERAGE",
313 index, file, def->ds_name);
314 array_append_format (args, "DEF:def_%04i_max=%s:%s:MAX",
315 index, file, def->ds_name);
317 array_append_format (args, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
319 array_append_format (args, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
321 array_append_format (args, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
323 array_append_format (args, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
327 array_append_format (args, "%s:def_%04i_avg#%06"PRIx32":%s%s",
328 def->area ? "AREA" : "LINE1",
330 (def->legend != NULL) ? def->legend : def->ds_name,
331 def->stack ? ":STACK" : "");
332 array_append_format (args, "GPRINT:vdef_%04i_min:%s min,",
333 index, (def->format != NULL) ? def->format : "%6.2lf");
334 array_append_format (args, "GPRINT:vdef_%04i_avg:%s avg,",
335 index, (def->format != NULL) ? def->format : "%6.2lf");
336 array_append_format (args, "GPRINT:vdef_%04i_max:%s max,",
337 index, (def->format != NULL) ? def->format : "%6.2lf");
338 array_append_format (args, "GPRINT:vdef_%04i_lst:%s last\\l",
339 index, (def->format != NULL) ? def->format : "%6.2lf");
344 } /* }}} int def_get_rrdargs */
346 /* vim: set sw=2 sts=2 et fdm=marker : */