6 #include "graph_config.h"
11 #include <fcgi_stdio.h>
18 graph_ident_t *select;
32 #define DEF_CONFIG_FIELD(field) \
33 static int def_config_##field (const oconfig_item_t *ci, graph_ident_t *ident) \
36 int status = graph_config_get_string (ci, &tmp); \
39 ident_set_##field (ident, tmp); \
42 } /* }}} int def_config_field */
44 DEF_CONFIG_FIELD (host);
45 DEF_CONFIG_FIELD (plugin);
46 DEF_CONFIG_FIELD (plugin_instance);
47 DEF_CONFIG_FIELD (type);
48 DEF_CONFIG_FIELD (type_instance);
50 #undef DEF_CONFIG_FIELD
52 static int def_config_color (const oconfig_item_t *ci, uint32_t *ret_color) /* {{{ */
58 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
61 tmp = ci->values[0].value.string;
65 color = (uint32_t) strtoul (tmp, &endptr, /* base = */ 16);
66 if ((errno != 0) || (endptr == tmp) || (color > 0x00ffffff))
72 } /* }}} int def_config_color */
74 static graph_def_t *def_config_get_obj (graph_config_t *cfg, /* {{{ */
75 const oconfig_item_t *ci)
82 ident = gl_graph_get_selector (cfg);
85 fprintf (stderr, "def_config_get_obj: gl_graph_get_selector failed");
89 for (i = 0; i < ci->children_num; i++)
91 oconfig_item_t *child;
93 #define HANDLE_FIELD(name,field) \
94 else if (strcasecmp (name, child->key) == 0) \
95 def_config_##field (child, ident)
97 child = ci->children + i;
98 if (strcasecmp ("DSName", child->key) == 0)
99 graph_config_get_string (child, &ds_name);
101 HANDLE_FIELD ("Host", host);
102 HANDLE_FIELD ("Plugin", plugin);
103 HANDLE_FIELD ("PluginInstance", plugin_instance);
104 HANDLE_FIELD ("Type", type);
105 HANDLE_FIELD ("TypeInstance", type_instance);
110 def = def_create (cfg, ident, ds_name);
113 fprintf (stderr, "def_config_get_obj: def_create failed\n");
114 ident_destroy (ident);
119 ident_destroy (ident);
123 } /* }}} graph_def_t *def_config_get_obj */
128 graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
131 graph_ident_t *selector;
134 if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
137 selector = gl_graph_get_selector (cfg);
138 if (selector == NULL)
141 ret = malloc (sizeof (*ret));
144 ident_destroy (selector);
147 memset (ret, 0, sizeof (*ret));
150 ret->ds_name = strdup (ds_name);
151 if (ret->ds_name == NULL)
153 ident_destroy (selector);
158 ret->color = get_random_color ();
161 ret->select = ident_copy_with_selector (selector, ident,
162 IDENT_FLAG_REPLACE_ALL);
163 if (ret->select == NULL)
165 ident_destroy (selector);
171 ident_destroy (selector);
173 } /* }}} graph_def_t *def_create */
175 void def_destroy (graph_def_t *def) /* {{{ */
184 ident_destroy (def->select);
191 } /* }}} void def_destroy */
193 int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
198 def = def_config_get_obj (cfg, ci);
202 for (i = 0; i < ci->children_num; i++)
204 oconfig_item_t *child;
206 child = ci->children + i;
207 if (strcasecmp ("Legend", child->key) == 0)
208 graph_config_get_string (child, &def->legend);
209 else if (strcasecmp ("Color", child->key) == 0)
210 def_config_color (child, &def->color);
211 else if (strcasecmp ("Stack", child->key) == 0)
212 graph_config_get_bool (child, &def->stack);
213 else if (strcasecmp ("Area", child->key) == 0)
214 graph_config_get_bool (child, &def->area);
216 fprintf (stderr, "def_config: Ignoring unknown config option \"%s\"",
220 return (gl_graph_add_def (cfg, def));
221 } /* }}} int def_config */
223 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
227 if ((head == NULL) || (def == NULL))
231 while (ptr->next != NULL)
237 } /* }}} int def_append */
239 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
244 if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
247 for (ptr = head; ptr != NULL; ptr = ptr->next)
249 if (!ident_matches (ptr->select, ident))
252 if (strcmp (ptr->ds_name, ds_name) == 0)
257 } /* }}} graph_def_t *def_search */
259 _Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
261 return (ident_matches (def->select, ident));
262 } /* }}} _Bool def_matches */
264 int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
269 if ((def == NULL) || (callback == NULL))
272 for (ptr = def; ptr != NULL; ptr = ptr->next)
276 status = (*callback) (ptr, user_data);
282 } /* }}} int def_foreach */
284 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
290 if ((def == NULL) || (ident == NULL) || (args == NULL))
293 file = ident_to_file (ident);
296 DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
300 DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
302 index = array_argc (args);
305 array_append_format (args, "DEF:def_%04i_min=%s:%s:MIN",
306 index, file, def->ds_name);
307 array_append_format (args, "DEF:def_%04i_avg=%s:%s:AVERAGE",
308 index, file, def->ds_name);
309 array_append_format (args, "DEF:def_%04i_max=%s:%s:MAX",
310 index, file, def->ds_name);
312 array_append_format (args, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
314 array_append_format (args, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
316 array_append_format (args, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
318 array_append_format (args, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
322 array_append_format (args, "%s:def_%04i_avg#%06"PRIx32":%s%s",
323 def->area ? "AREA" : "LINE1",
325 (def->legend != NULL) ? def->legend : def->ds_name,
326 def->stack ? ":STACK" : "");
327 array_append_format (args, "GPRINT:vdef_%04i_min:%%lg min,", index);
328 array_append_format (args, "GPRINT:vdef_%04i_avg:%%lg avg,", index);
329 array_append_format (args, "GPRINT:vdef_%04i_max:%%lg max,", index);
330 array_append_format (args, "GPRINT:vdef_%04i_lst:%%lg last\\l", index);
335 } /* }}} int def_get_rrdargs */
337 /* vim: set sw=2 sts=2 et fdm=marker : */