7 #include "graph_config.h"
12 #include <fcgi_stdio.h>
19 graph_ident_t *select;
34 #define DEF_CONFIG_FIELD(field) \
35 static int def_config_##field (const oconfig_item_t *ci, graph_ident_t *ident) \
38 int status = graph_config_get_string (ci, &tmp); \
41 ident_set_##field (ident, tmp); \
44 } /* }}} int def_config_field */
46 DEF_CONFIG_FIELD (host);
47 DEF_CONFIG_FIELD (plugin);
48 DEF_CONFIG_FIELD (plugin_instance);
49 DEF_CONFIG_FIELD (type);
50 DEF_CONFIG_FIELD (type_instance);
52 #undef DEF_CONFIG_FIELD
54 static int def_config_color (const oconfig_item_t *ci, uint32_t *ret_color) /* {{{ */
60 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
63 tmp = ci->values[0].value.string;
67 color = (uint32_t) strtoul (tmp, &endptr, /* base = */ 16);
68 if ((errno != 0) || (endptr == tmp) || (color > 0x00ffffff))
74 } /* }}} int def_config_color */
76 static graph_def_t *def_config_get_obj (graph_config_t *cfg, /* {{{ */
77 const oconfig_item_t *ci)
84 ident = graph_get_selector (cfg);
87 fprintf (stderr, "def_config_get_obj: graph_get_selector failed");
91 for (i = 0; i < ci->children_num; i++)
93 oconfig_item_t *child;
95 #define HANDLE_FIELD(name,field) \
96 else if (strcasecmp (name, child->key) == 0) \
97 def_config_##field (child, ident)
99 child = ci->children + i;
100 if (strcasecmp ("DSName", child->key) == 0)
101 graph_config_get_string (child, &ds_name);
103 HANDLE_FIELD ("Host", host);
104 HANDLE_FIELD ("Plugin", plugin);
105 HANDLE_FIELD ("PluginInstance", plugin_instance);
106 HANDLE_FIELD ("Type", type);
107 HANDLE_FIELD ("TypeInstance", type_instance);
112 def = def_create (cfg, ident, ds_name);
115 fprintf (stderr, "def_config_get_obj: def_create failed\n");
116 ident_destroy (ident);
121 ident_destroy (ident);
125 } /* }}} graph_def_t *def_config_get_obj */
130 graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
133 graph_ident_t *selector;
136 if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
139 selector = graph_get_selector (cfg);
140 if (selector == NULL)
143 ret = malloc (sizeof (*ret));
146 ident_destroy (selector);
149 memset (ret, 0, sizeof (*ret));
153 ret->ds_name = strdup (ds_name);
154 if (ret->ds_name == NULL)
156 ident_destroy (selector);
161 ret->color = get_random_color ();
164 ret->select = ident_copy_with_selector (selector, ident,
165 IDENT_FLAG_REPLACE_ALL);
166 if (ret->select == NULL)
168 ident_destroy (selector);
174 ident_destroy (selector);
176 } /* }}} graph_def_t *def_create */
178 void def_destroy (graph_def_t *def) /* {{{ */
187 ident_destroy (def->select);
196 } /* }}} void def_destroy */
198 int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
203 def = def_config_get_obj (cfg, ci);
207 for (i = 0; i < ci->children_num; i++)
209 oconfig_item_t *child;
211 child = ci->children + i;
212 if (strcasecmp ("Legend", child->key) == 0)
213 graph_config_get_string (child, &def->legend);
214 else if (strcasecmp ("Color", child->key) == 0)
215 def_config_color (child, &def->color);
216 else if (strcasecmp ("Stack", child->key) == 0)
217 graph_config_get_bool (child, &def->stack);
218 else if (strcasecmp ("Area", child->key) == 0)
219 graph_config_get_bool (child, &def->area);
220 else if (strcasecmp ("Format", child->key) == 0)
221 graph_config_get_string (child, &def->format);
224 return (graph_add_def (cfg, def));
225 } /* }}} int def_config */
227 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
231 if ((head == NULL) || (def == NULL))
235 while (ptr->next != NULL)
241 } /* }}} int def_append */
243 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
248 if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
251 for (ptr = head; ptr != NULL; ptr = ptr->next)
253 if (!ident_matches (ptr->select, ident))
256 if (strcmp (ptr->ds_name, ds_name) == 0)
261 } /* }}} graph_def_t *def_search */
263 _Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
265 return (ident_matches (def->select, ident));
266 } /* }}} _Bool def_matches */
268 int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
273 if ((def == NULL) || (callback == NULL))
276 for (ptr = def; ptr != NULL; ptr = ptr->next)
280 status = (*callback) (ptr, user_data);
286 } /* }}} int def_foreach */
288 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
294 if ((def == NULL) || (ident == NULL) || (args == NULL))
297 file = ident_to_file (ident);
300 DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
304 DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
306 index = array_argc (args);
309 array_append_format (args, "DEF:def_%04i_min=%s:%s:MIN",
310 index, file, def->ds_name);
311 array_append_format (args, "DEF:def_%04i_avg=%s:%s:AVERAGE",
312 index, file, def->ds_name);
313 array_append_format (args, "DEF:def_%04i_max=%s:%s:MAX",
314 index, file, def->ds_name);
316 array_append_format (args, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
318 array_append_format (args, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
320 array_append_format (args, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
322 array_append_format (args, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
326 array_append_format (args, "%s:def_%04i_avg#%06"PRIx32":%s%s",
327 def->area ? "AREA" : "LINE1",
329 (def->legend != NULL) ? def->legend : def->ds_name,
330 def->stack ? ":STACK" : "");
331 array_append_format (args, "GPRINT:vdef_%04i_min:%s min,",
332 index, (def->format != NULL) ? def->format : "%lg");
333 array_append_format (args, "GPRINT:vdef_%04i_avg:%s avg,",
334 index, (def->format != NULL) ? def->format : "%lg");
335 array_append_format (args, "GPRINT:vdef_%04i_max:%s max,",
336 index, (def->format != NULL) ? def->format : "%lg");
337 array_append_format (args, "GPRINT:vdef_%04i_lst:%s last\\l",
338 index, (def->format != NULL) ? def->format : "%lg");
343 } /* }}} int def_get_rrdargs */
345 /* vim: set sw=2 sts=2 et fdm=marker : */