6 #include "graph_config.h"
11 #include <fcgi_stdio.h>
18 graph_ident_t *select;
30 #define DEF_CONFIG_FIELD(field) \
31 static int def_config_##field (const oconfig_item_t *ci, graph_ident_t *ident) \
34 int status = graph_config_get_string (ci, &tmp); \
37 ident_set_##field (ident, tmp); \
40 } /* }}} int def_config_field */
42 DEF_CONFIG_FIELD (host);
43 DEF_CONFIG_FIELD (plugin);
44 DEF_CONFIG_FIELD (plugin_instance);
45 DEF_CONFIG_FIELD (type);
46 DEF_CONFIG_FIELD (type_instance);
48 #undef DEF_CONFIG_FIELD
50 static int def_config_color (const oconfig_item_t *ci, uint32_t *ret_color) /* {{{ */
56 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
59 tmp = ci->values[0].value.string;
63 color = (uint32_t) strtoul (tmp, &endptr, /* base = */ 16);
64 if ((errno != 0) || (endptr == tmp) || (color > 0x00ffffff))
70 } /* }}} int def_config_color */
72 static graph_def_t *def_config_get_obj (graph_config_t *cfg, /* {{{ */
73 const oconfig_item_t *ci)
80 ident = gl_graph_get_selector (cfg);
83 fprintf (stderr, "def_config_get_obj: gl_graph_get_selector failed");
87 for (i = 0; i < ci->children_num; i++)
89 oconfig_item_t *child;
91 #define HANDLE_FIELD(name,field) \
92 else if (strcasecmp (name, child->key) == 0) \
93 def_config_##field (child, ident)
95 child = ci->children + i;
96 if (strcasecmp ("DSName", child->key) == 0)
97 graph_config_get_string (child, &ds_name);
99 HANDLE_FIELD ("Host", host);
100 HANDLE_FIELD ("Plugin", plugin);
101 HANDLE_FIELD ("PluginInstance", plugin_instance);
102 HANDLE_FIELD ("Type", type);
103 HANDLE_FIELD ("TypeInstance", type_instance);
108 def = def_create (cfg, ident, ds_name);
111 fprintf (stderr, "def_config_get_obj: def_create failed\n");
112 ident_destroy (ident);
117 ident_destroy (ident);
121 } /* }}} graph_def_t *def_config_get_obj */
126 graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
129 graph_ident_t *selector;
132 if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
135 selector = gl_graph_get_selector (cfg);
136 if (selector == NULL)
139 ret = malloc (sizeof (*ret));
142 ident_destroy (selector);
145 memset (ret, 0, sizeof (*ret));
148 ret->ds_name = strdup (ds_name);
149 if (ret->ds_name == NULL)
151 ident_destroy (selector);
156 ret->color = get_random_color ();
159 ret->select = ident_copy_with_selector (selector, ident,
160 IDENT_FLAG_REPLACE_ALL);
161 if (ret->select == NULL)
163 ident_destroy (selector);
169 ident_destroy (selector);
171 } /* }}} graph_def_t *def_create */
173 void def_destroy (graph_def_t *def) /* {{{ */
182 ident_destroy (def->select);
189 } /* }}} void def_destroy */
191 int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
196 def = def_config_get_obj (cfg, ci);
200 for (i = 0; i < ci->children_num; i++)
202 oconfig_item_t *child;
204 child = ci->children + i;
205 if (strcasecmp ("Legend", child->key) == 0)
206 graph_config_get_string (child, &def->legend);
207 else if (strcasecmp ("Color", child->key) == 0)
208 def_config_color (child, &def->color);
211 return (gl_graph_add_def (cfg, def));
212 } /* }}} int def_config */
214 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
218 if ((head == NULL) || (def == NULL))
222 while (ptr->next != NULL)
228 } /* }}} int def_append */
230 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
235 if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
238 for (ptr = head; ptr != NULL; ptr = ptr->next)
240 if (!ident_matches (ptr->select, ident))
243 if (strcmp (ptr->ds_name, ds_name) == 0)
248 } /* }}} graph_def_t *def_search */
250 _Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
252 return (ident_matches (def->select, ident));
253 } /* }}} _Bool def_matches */
255 int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
260 if ((def == NULL) || (callback == NULL))
263 for (ptr = def; ptr != NULL; ptr = ptr->next)
267 status = (*callback) (ptr, user_data);
273 } /* }}} int def_foreach */
275 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
281 if ((def == NULL) || (ident == NULL) || (args == NULL))
284 file = ident_to_file (ident);
287 DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
291 DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
293 index = array_argc (args);
296 array_append_format (args, "DEF:def_%04i_min=%s:%s:MIN",
297 index, file, def->ds_name);
298 array_append_format (args, "DEF:def_%04i_avg=%s:%s:AVERAGE",
299 index, file, def->ds_name);
300 array_append_format (args, "DEF:def_%04i_max=%s:%s:MAX",
301 index, file, def->ds_name);
303 array_append_format (args, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
305 array_append_format (args, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
307 array_append_format (args, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
309 array_append_format (args, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
313 array_append_format (args, "LINE1:def_%04i_avg#%06"PRIx32":%s",
315 (def->legend != NULL) ? def->legend : def->ds_name);
316 array_append_format (args, "GPRINT:vdef_%04i_min:%%lg min,", index);
317 array_append_format (args, "GPRINT:vdef_%04i_avg:%%lg avg,", index);
318 array_append_format (args, "GPRINT:vdef_%04i_max:%%lg max,", index);
319 array_append_format (args, "GPRINT:vdef_%04i_lst:%%lg last\\l", index);
324 } /* }}} int def_get_rrdargs */
326 /* vim: set sw=2 sts=2 et fdm=marker : */