5 #include "graph_instance.h"
6 #include "graph_ident.h"
7 #include "graph_list.h"
9 #include "utils_params.h"
12 #include <fcgi_stdio.h>
14 struct graph_instance_s /* {{{ */
16 graph_ident_t *select;
18 graph_ident_t **files;
21 graph_instance_t *next;
22 }; /* }}} struct graph_instance_s */
24 struct def_callback_data_s
26 graph_instance_t *inst;
29 typedef struct def_callback_data_s def_callback_data_t;
34 /* Create one DEF for each data source in the file. Called by
35 * "inst_get_default_defs" for each file. */
36 static graph_def_t *ident_get_default_defs (graph_config_t *cfg, /* {{{ */
37 graph_ident_t *ident, graph_def_t *def_head)
39 graph_def_t *defs = NULL;
46 if ((cfg == NULL) || (ident == NULL))
49 file = ident_to_file (ident);
52 fprintf (stderr, "ident_get_default_defs: ident_to_file failed\n");
56 status = ds_list_from_rrd_file (file, &dses_num, &dses);
63 for (i = 0; i < dses_num; i++)
67 def = def_search (def_head, ident, dses[i]);
71 def = def_create (cfg, ident, dses[i]);
78 def_append (defs, def);
87 } /* }}} int ident_get_default_defs */
89 /* Create one or more DEFs for each file in the graph instance. The number
90 * depends on the number of data sources in each of the files. Called from
91 * "inst_get_rrdargs" if no DEFs are available from the configuration.
93 static graph_def_t *inst_get_default_defs (graph_config_t *cfg, /* {{{ */
94 graph_instance_t *inst)
96 graph_def_t *defs = NULL;
99 if ((cfg == NULL) || (inst == NULL))
102 for (i = 0; i < inst->files_num; i++)
106 def = ident_get_default_defs (cfg, inst->files[i], defs);
113 def_append (defs, def);
117 } /* }}} graph_def_t *inst_get_default_defs */
119 /* Called with each DEF in turn. Calls "def_get_rrdargs" with every appropriate
120 * file / DEF pair. */
121 static int gl_instance_get_rrdargs_cb (graph_def_t *def, void *user_data) /* {{{ */
123 def_callback_data_t *data = user_data;
124 graph_instance_t *inst = data->inst;
125 str_array_t *args = data->args;
129 for (i = 0; i < inst->files_num; i++)
131 if (!def_matches (def, inst->files[i]))
134 def_get_rrdargs (def, inst->files[i], args);
138 } /* }}} int gl_instance_get_rrdargs_cb */
140 static const char *get_part_from_param (const char *prim_key, /* {{{ */
145 val = param (prim_key);
149 return (param (sec_key));
150 } /* }}} const char *get_part_from_param */
155 graph_instance_t *inst_create (graph_config_t *cfg, /* {{{ */
156 const graph_ident_t *ident)
159 graph_ident_t *selector;
161 if ((cfg == NULL) || (ident == NULL))
164 i = malloc (sizeof (*i));
167 memset (i, 0, sizeof (*i));
169 selector = graph_get_selector (cfg);
170 if (selector == NULL)
172 fprintf (stderr, "inst_create: graph_get_selector failed\n");
177 i->select = ident_copy_with_selector (selector, ident,
178 IDENT_FLAG_REPLACE_ANY);
179 if (i->select == NULL)
181 fprintf (stderr, "inst_create: ident_copy_with_selector failed\n");
182 ident_destroy (selector);
187 ident_destroy (selector);
195 } /* }}} graph_instance_t *inst_create */
197 void inst_destroy (graph_instance_t *inst) /* {{{ */
199 graph_instance_t *next;
207 ident_destroy (inst->select);
209 for (i = 0; i < inst->files_num; i++)
210 ident_destroy (inst->files[i]);
216 } /* }}} void inst_destroy */
218 int inst_add_file (graph_instance_t *inst, /* {{{ */
219 const graph_ident_t *file)
223 tmp = realloc (inst->files, sizeof (*inst->files) * (inst->files_num + 1));
228 inst->files[inst->files_num] = ident_clone (file);
229 if (inst->files[inst->files_num] == NULL)
235 } /* }}} int inst_add_file */
237 graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
239 const char *host = get_part_from_param ("inst_host", "host");
240 const char *plugin = get_part_from_param ("inst_plugin", "plugin");
241 const char *plugin_instance = get_part_from_param ("inst_plugin_instance", "plugin_instance");
242 const char *type = get_part_from_param ("inst_type", "type");
243 const char *type_instance = get_part_from_param ("inst_type_instance", "type_instance");
244 graph_ident_t *ident;
245 graph_instance_t *inst;
248 cfg = gl_graph_get_selected ();
252 DEBUG ("inst_get_selected: cfg == NULL;\n");
257 || (plugin == NULL) || (plugin_instance == NULL)
258 || (type == NULL) || (type_instance == NULL))
260 DEBUG ("inst_get_selected: A parameter is NULL.\n");
264 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
266 for (inst = graph_get_instances (cfg); inst != NULL; inst = inst->next)
268 if (ident_compare (ident, inst->select) != 0)
271 ident_destroy (ident);
275 DEBUG ("inst_get_selected: No match found.\n");
276 ident_destroy (ident);
278 } /* }}} graph_instance_t *inst_get_selected */
280 int inst_get_rrdargs (graph_config_t *cfg, /* {{{ */
281 graph_instance_t *inst,
284 def_callback_data_t data = { inst, args };
288 if ((cfg == NULL) || (inst == NULL) || (args == NULL))
291 status = graph_get_rrdargs (cfg, inst, args);
295 defs = graph_get_defs (cfg);
298 defs = inst_get_default_defs (cfg, inst);
303 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
309 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
313 } /* }}} int inst_get_rrdargs */
315 graph_ident_t *inst_get_selector (graph_instance_t *inst) /* {{{ */
320 return (ident_clone (inst->select));
321 } /* }}} graph_ident_t *inst_get_selector */
323 int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
324 char *buffer, size_t buffer_size)
326 graph_ident_t *cfg_select;
328 if ((cfg == NULL) || (inst == NULL)
329 || (buffer == NULL) || (buffer_size < 1))
332 cfg_select = graph_get_selector (cfg);
333 if (cfg_select == NULL)
335 fprintf (stderr, "inst_get_params: graph_get_selector failed");
341 #define COPY_FIELD(field) do { \
342 const char *cfg_f = ident_get_##field (cfg_select); \
343 const char *inst_f = ident_get_##field (inst->select); \
344 if (strcmp (cfg_f, inst_f) == 0) \
346 strlcat (buffer, #field, buffer_size); \
347 strlcat (buffer, "=", buffer_size); \
348 strlcat (buffer, cfg_f, buffer_size); \
352 strlcat (buffer, "graph_", buffer_size); \
353 strlcat (buffer, #field, buffer_size); \
354 strlcat (buffer, "=", buffer_size); \
355 strlcat (buffer, cfg_f, buffer_size); \
356 strlcat (buffer, ";", buffer_size); \
357 strlcat (buffer, "inst_", buffer_size); \
358 strlcat (buffer, #field, buffer_size); \
359 strlcat (buffer, "=", buffer_size); \
360 strlcat (buffer, inst_f, buffer_size); \
365 strlcat (buffer, ";", buffer_size);
367 strlcat (buffer, ";", buffer_size);
368 COPY_FIELD(plugin_instance);
369 strlcat (buffer, ";", buffer_size);
371 strlcat (buffer, ";", buffer_size);
372 COPY_FIELD(type_instance);
376 ident_destroy (cfg_select);
379 } /* }}} int inst_get_params */
381 int inst_append (graph_instance_t *head, graph_instance_t *inst) /* {{{ */
383 graph_instance_t *ptr;
385 if ((head == NULL) || (inst == NULL))
389 while (ptr->next != NULL)
395 } /* }}} int inst_append */
397 int inst_foreach (graph_instance_t *inst, /* {{{ */
398 inst_callback_t cb, void *user_data)
400 graph_instance_t *ptr;
402 if ((inst == NULL) || (cb == NULL))
405 for (ptr = inst; ptr != NULL; ptr = ptr->next)
409 status = (*cb) (ptr, user_data);
415 } /* }}} int inst_foreach */
417 graph_instance_t *inst_find_matching (graph_instance_t *inst, /* {{{ */
418 const graph_ident_t *ident)
420 graph_instance_t *ptr;
422 if ((inst == NULL) || (ident == NULL))
425 for (ptr = inst; ptr != NULL; ptr = ptr->next)
426 if (ident_matches (ptr->select, ident))
430 } /* }}} graph_instance_t *inst_find_matching */
432 int inst_describe (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
433 char *buffer, size_t buffer_size)
435 graph_ident_t *cfg_select;
437 if ((cfg == NULL) || (inst == NULL)
438 || (buffer == NULL) || (buffer_size < 2))
441 cfg_select = graph_get_selector (cfg);
442 if (cfg_select == NULL)
444 fprintf (stderr, "inst_describe: graph_get_selector failed\n");
450 #define CHECK_FIELD(field) do { \
451 if (IS_ANY (ident_get_##field (cfg_select))) \
453 if (buffer[0] != 0) \
454 strlcat (buffer, "/", buffer_size); \
455 strlcat (buffer, ident_get_##field (inst->select), buffer_size); \
460 CHECK_FIELD (plugin);
461 CHECK_FIELD (plugin_instance);
463 CHECK_FIELD (type_instance);
468 strlcat (buffer, "default", buffer_size);
470 ident_destroy (cfg_select);
473 } /* }}} int inst_describe */
475 /* vim: set sw=2 sts=2 et fdm=marker : */