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 */
75 graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
78 graph_ident_t *selector;
81 if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
84 selector = gl_graph_get_selector (cfg);
88 ret = malloc (sizeof (*ret));
91 ident_destroy (selector);
94 memset (ret, 0, sizeof (*ret));
97 ret->ds_name = strdup (ds_name);
98 if (ret->ds_name == NULL)
100 ident_destroy (selector);
105 ret->color = get_random_color ();
108 ret->select = ident_copy_with_selector (selector, ident,
109 IDENT_FLAG_REPLACE_ALL);
110 if (ret->select == NULL)
112 ident_destroy (selector);
118 ident_destroy (selector);
120 } /* }}} graph_def_t *def_create */
122 void def_destroy (graph_def_t *def) /* {{{ */
131 ident_destroy (def->select);
138 } /* }}} void def_destroy */
140 int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
142 graph_ident_t *ident;
143 char *ds_name = NULL;
145 uint32_t color = 0x01000000;
149 ident = gl_graph_get_selector (cfg);
153 for (i = 0; i < ci->children_num; i++)
155 oconfig_item_t *child;
157 #define HANDLE_FIELD(name,field) \
158 else if (strcasecmp (name, child->key) == 0) \
159 def_config_##field (child, ident)
161 child = ci->children + i;
162 if (strcasecmp ("DSName", child->key) == 0)
163 graph_config_get_string (child, &ds_name);
164 else if (strcasecmp ("Legend", child->key) == 0)
165 graph_config_get_string (child, &legend);
166 else if (strcasecmp ("Color", child->key) == 0)
167 def_config_color (child, &color);
169 HANDLE_FIELD ("Host", host);
170 HANDLE_FIELD ("Plugin", plugin);
171 HANDLE_FIELD ("PluginInstance", plugin_instance);
172 HANDLE_FIELD ("Type", type);
173 HANDLE_FIELD ("TypeInstance", type_instance);
178 def = def_create (cfg, ident, ds_name);
181 fprintf (stderr, "def_config: def_create failed (ds_name = %s)\n",
182 (ds_name != NULL) ? ds_name : "(null)");
183 ident_destroy (ident);
187 def->legend = legend;
188 if (color < 0x01000000)
191 ident_destroy (ident);
194 return (gl_graph_add_def (cfg, def));
195 } /* }}} int def_config */
197 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
201 if ((head == NULL) || (def == NULL))
205 while (ptr->next != NULL)
211 } /* }}} int def_append */
213 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
218 if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
221 for (ptr = head; ptr != NULL; ptr = ptr->next)
223 if (!ident_matches (ptr->select, ident))
226 if (strcmp (ptr->ds_name, ds_name) == 0)
231 } /* }}} graph_def_t *def_search */
233 _Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
235 return (ident_matches (def->select, ident));
236 } /* }}} _Bool def_matches */
238 int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
243 if ((def == NULL) || (callback == NULL))
246 for (ptr = def; ptr != NULL; ptr = ptr->next)
250 status = (*callback) (ptr, user_data);
256 } /* }}} int def_foreach */
258 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
264 if ((def == NULL) || (ident == NULL) || (args == NULL))
267 file = ident_to_file (ident);
270 DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
274 DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
276 index = array_argc (args);
279 array_append_format (args, "DEF:def_%04i_min=%s:%s:MIN",
280 index, file, def->ds_name);
281 array_append_format (args, "DEF:def_%04i_avg=%s:%s:AVERAGE",
282 index, file, def->ds_name);
283 array_append_format (args, "DEF:def_%04i_max=%s:%s:MAX",
284 index, file, def->ds_name);
286 array_append_format (args, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
288 array_append_format (args, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
290 array_append_format (args, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
292 array_append_format (args, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
296 array_append_format (args, "LINE1:def_%04i_avg#%06"PRIx32":%s",
298 (def->legend != NULL) ? def->legend : def->ds_name);
299 array_append_format (args, "GPRINT:vdef_%04i_min:%%lg min,", index);
300 array_append_format (args, "GPRINT:vdef_%04i_avg:%%lg avg,", index);
301 array_append_format (args, "GPRINT:vdef_%04i_max:%%lg max,", index);
302 array_append_format (args, "GPRINT:vdef_%04i_lst:%%lg last\\l", index);
307 } /* }}} int def_get_rrdargs */
309 /* vim: set sw=2 sts=2 et fdm=marker : */