7 #include "graph_config.h"
12 #include <fcgi_stdio.h>
19 graph_ident_t *select;
33 #define DEF_CONFIG_FIELD(field) \
34 static int def_config_##field (const oconfig_item_t *ci, graph_ident_t *ident) \
37 int status = graph_config_get_string (ci, &tmp); \
40 ident_set_##field (ident, tmp); \
43 } /* }}} int def_config_field */
45 DEF_CONFIG_FIELD (host);
46 DEF_CONFIG_FIELD (plugin);
47 DEF_CONFIG_FIELD (plugin_instance);
48 DEF_CONFIG_FIELD (type);
49 DEF_CONFIG_FIELD (type_instance);
51 #undef DEF_CONFIG_FIELD
53 static int def_config_color (const oconfig_item_t *ci, uint32_t *ret_color) /* {{{ */
59 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
62 tmp = ci->values[0].value.string;
66 color = (uint32_t) strtoul (tmp, &endptr, /* base = */ 16);
67 if ((errno != 0) || (endptr == tmp) || (color > 0x00ffffff))
73 } /* }}} int def_config_color */
75 static graph_def_t *def_config_get_obj (graph_config_t *cfg, /* {{{ */
76 const oconfig_item_t *ci)
83 ident = graph_get_selector (cfg);
86 fprintf (stderr, "def_config_get_obj: graph_get_selector failed");
90 for (i = 0; i < ci->children_num; i++)
92 oconfig_item_t *child;
94 #define HANDLE_FIELD(name,field) \
95 else if (strcasecmp (name, child->key) == 0) \
96 def_config_##field (child, ident)
98 child = ci->children + i;
99 if (strcasecmp ("DSName", child->key) == 0)
100 graph_config_get_string (child, &ds_name);
102 HANDLE_FIELD ("Host", host);
103 HANDLE_FIELD ("Plugin", plugin);
104 HANDLE_FIELD ("PluginInstance", plugin_instance);
105 HANDLE_FIELD ("Type", type);
106 HANDLE_FIELD ("TypeInstance", type_instance);
111 def = def_create (cfg, ident, ds_name);
114 fprintf (stderr, "def_config_get_obj: def_create failed\n");
115 ident_destroy (ident);
120 ident_destroy (ident);
124 } /* }}} graph_def_t *def_config_get_obj */
129 graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
132 graph_ident_t *selector;
135 if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
138 selector = graph_get_selector (cfg);
139 if (selector == NULL)
142 ret = malloc (sizeof (*ret));
145 ident_destroy (selector);
148 memset (ret, 0, sizeof (*ret));
151 ret->ds_name = strdup (ds_name);
152 if (ret->ds_name == NULL)
154 ident_destroy (selector);
159 ret->color = get_random_color ();
162 ret->select = ident_copy_with_selector (selector, ident,
163 IDENT_FLAG_REPLACE_ALL);
164 if (ret->select == NULL)
166 ident_destroy (selector);
172 ident_destroy (selector);
174 } /* }}} graph_def_t *def_create */
176 void def_destroy (graph_def_t *def) /* {{{ */
185 ident_destroy (def->select);
192 } /* }}} void def_destroy */
194 int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
199 def = def_config_get_obj (cfg, ci);
203 for (i = 0; i < ci->children_num; i++)
205 oconfig_item_t *child;
207 child = ci->children + i;
208 if (strcasecmp ("Legend", child->key) == 0)
209 graph_config_get_string (child, &def->legend);
210 else if (strcasecmp ("Color", child->key) == 0)
211 def_config_color (child, &def->color);
212 else if (strcasecmp ("Stack", child->key) == 0)
213 graph_config_get_bool (child, &def->stack);
214 else if (strcasecmp ("Area", child->key) == 0)
215 graph_config_get_bool (child, &def->area);
217 fprintf (stderr, "def_config: Ignoring unknown config option \"%s\"",
221 return (graph_add_def (cfg, def));
222 } /* }}} int def_config */
224 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
228 if ((head == NULL) || (def == NULL))
232 while (ptr->next != NULL)
238 } /* }}} int def_append */
240 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
245 if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
248 for (ptr = head; ptr != NULL; ptr = ptr->next)
250 if (!ident_matches (ptr->select, ident))
253 if (strcmp (ptr->ds_name, ds_name) == 0)
258 } /* }}} graph_def_t *def_search */
260 _Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
262 return (ident_matches (def->select, ident));
263 } /* }}} _Bool def_matches */
265 int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
270 if ((def == NULL) || (callback == NULL))
273 for (ptr = def; ptr != NULL; ptr = ptr->next)
277 status = (*callback) (ptr, user_data);
283 } /* }}} int def_foreach */
285 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
291 if ((def == NULL) || (ident == NULL) || (args == NULL))
294 file = ident_to_file (ident);
297 DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
301 DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
303 index = array_argc (args);
306 array_append_format (args, "DEF:def_%04i_min=%s:%s:MIN",
307 index, file, def->ds_name);
308 array_append_format (args, "DEF:def_%04i_avg=%s:%s:AVERAGE",
309 index, file, def->ds_name);
310 array_append_format (args, "DEF:def_%04i_max=%s:%s:MAX",
311 index, file, def->ds_name);
313 array_append_format (args, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
315 array_append_format (args, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
317 array_append_format (args, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
319 array_append_format (args, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
323 array_append_format (args, "%s:def_%04i_avg#%06"PRIx32":%s%s",
324 def->area ? "AREA" : "LINE1",
326 (def->legend != NULL) ? def->legend : def->ds_name,
327 def->stack ? ":STACK" : "");
328 array_append_format (args, "GPRINT:vdef_%04i_min:%%lg min,", index);
329 array_append_format (args, "GPRINT:vdef_%04i_avg:%%lg avg,", index);
330 array_append_format (args, "GPRINT:vdef_%04i_max:%%lg max,", index);
331 array_append_format (args, "GPRINT:vdef_%04i_lst:%%lg last\\l", index);
336 } /* }}} int def_get_rrdargs */
338 /* vim: set sw=2 sts=2 et fdm=marker : */