5 #include "graph_instance.h"
7 #include "graph_ident.h"
8 #include "graph_list.h"
10 #include "utils_params.h"
13 #include <fcgi_stdio.h>
15 struct graph_instance_s /* {{{ */
17 graph_ident_t *select;
19 graph_ident_t **files;
22 graph_instance_t *next;
23 }; /* }}} struct graph_instance_s */
25 struct def_callback_data_s
27 graph_instance_t *inst;
30 typedef struct def_callback_data_s def_callback_data_t;
35 /* Create one DEF for each data source in the file. Called by
36 * "inst_get_default_defs" for each file. */
37 static graph_def_t *ident_get_default_defs (graph_config_t *cfg, /* {{{ */
38 graph_ident_t *ident, graph_def_t *def_head)
40 graph_def_t *defs = NULL;
47 if ((cfg == NULL) || (ident == NULL))
50 file = ident_to_file (ident);
53 fprintf (stderr, "ident_get_default_defs: ident_to_file failed\n");
57 status = ds_list_from_rrd_file (file, &dses_num, &dses);
64 for (i = 0; i < dses_num; i++)
68 def = def_search (def_head, ident, dses[i]);
72 def = def_create (cfg, ident, dses[i]);
79 def_append (defs, def);
88 } /* }}} int ident_get_default_defs */
90 /* Create one or more DEFs for each file in the graph instance. The number
91 * depends on the number of data sources in each of the files. Called from
92 * "inst_get_rrdargs" if no DEFs are available from the configuration.
94 static graph_def_t *inst_get_default_defs (graph_config_t *cfg, /* {{{ */
95 graph_instance_t *inst)
97 graph_def_t *defs = NULL;
100 if ((cfg == NULL) || (inst == NULL))
103 for (i = 0; i < inst->files_num; i++)
107 def = ident_get_default_defs (cfg, inst->files[i], defs);
114 def_append (defs, def);
118 } /* }}} graph_def_t *inst_get_default_defs */
120 /* Called with each DEF in turn. Calls "def_get_rrdargs" with every appropriate
121 * file / DEF pair. */
122 static int gl_instance_get_rrdargs_cb (graph_def_t *def, void *user_data) /* {{{ */
124 def_callback_data_t *data = user_data;
125 graph_instance_t *inst = data->inst;
126 str_array_t *args = data->args;
130 for (i = 0; i < inst->files_num; i++)
132 if (!def_matches (def, inst->files[i]))
135 def_get_rrdargs (def, inst->files[i], args);
139 } /* }}} int gl_instance_get_rrdargs_cb */
141 static const char *get_part_from_param (const char *prim_key, /* {{{ */
146 val = param (prim_key);
150 return (param (sec_key));
151 } /* }}} const char *get_part_from_param */
156 graph_instance_t *inst_create (graph_config_t *cfg, /* {{{ */
157 const graph_ident_t *ident)
160 graph_ident_t *selector;
162 if ((cfg == NULL) || (ident == NULL))
165 i = malloc (sizeof (*i));
168 memset (i, 0, sizeof (*i));
170 selector = graph_get_selector (cfg);
171 if (selector == NULL)
173 fprintf (stderr, "inst_create: graph_get_selector failed\n");
178 i->select = ident_copy_with_selector (selector, ident,
179 IDENT_FLAG_REPLACE_ANY);
180 if (i->select == NULL)
182 fprintf (stderr, "inst_create: ident_copy_with_selector failed\n");
183 ident_destroy (selector);
188 ident_destroy (selector);
196 } /* }}} graph_instance_t *inst_create */
198 void inst_destroy (graph_instance_t *inst) /* {{{ */
200 graph_instance_t *next;
208 ident_destroy (inst->select);
210 for (i = 0; i < inst->files_num; i++)
211 ident_destroy (inst->files[i]);
217 } /* }}} void inst_destroy */
219 int inst_add_file (graph_instance_t *inst, /* {{{ */
220 const graph_ident_t *file)
224 tmp = realloc (inst->files, sizeof (*inst->files) * (inst->files_num + 1));
229 inst->files[inst->files_num] = ident_clone (file);
230 if (inst->files[inst->files_num] == NULL)
236 } /* }}} int inst_add_file */
238 graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
240 const char *host = get_part_from_param ("inst_host", "host");
241 const char *plugin = get_part_from_param ("inst_plugin", "plugin");
242 const char *plugin_instance = get_part_from_param ("inst_plugin_instance", "plugin_instance");
243 const char *type = get_part_from_param ("inst_type", "type");
244 const char *type_instance = get_part_from_param ("inst_type_instance", "type_instance");
245 graph_ident_t *ident;
246 graph_instance_t *inst;
249 cfg = gl_graph_get_selected ();
253 DEBUG ("inst_get_selected: cfg == NULL;\n");
258 || (plugin == NULL) || (plugin_instance == NULL)
259 || (type == NULL) || (type_instance == NULL))
261 DEBUG ("inst_get_selected: A parameter is NULL.\n");
265 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
267 for (inst = graph_get_instances (cfg); inst != NULL; inst = inst->next)
269 if (ident_compare (ident, inst->select) != 0)
272 ident_destroy (ident);
276 DEBUG ("inst_get_selected: No match found.\n");
277 ident_destroy (ident);
279 } /* }}} graph_instance_t *inst_get_selected */
281 int inst_get_rrdargs (graph_config_t *cfg, /* {{{ */
282 graph_instance_t *inst,
285 def_callback_data_t data = { inst, args };
289 if ((cfg == NULL) || (inst == NULL) || (args == NULL))
292 status = graph_get_rrdargs (cfg, inst, args);
296 defs = graph_get_defs (cfg);
299 defs = inst_get_default_defs (cfg, inst);
304 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
310 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
314 } /* }}} int inst_get_rrdargs */
316 graph_ident_t *inst_get_selector (graph_instance_t *inst) /* {{{ */
321 return (ident_clone (inst->select));
322 } /* }}} graph_ident_t *inst_get_selector */
324 int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
325 char *buffer, size_t buffer_size)
327 graph_ident_t *cfg_select;
329 if ((cfg == NULL) || (inst == NULL)
330 || (buffer == NULL) || (buffer_size < 1))
333 cfg_select = graph_get_selector (cfg);
334 if (cfg_select == NULL)
336 fprintf (stderr, "inst_get_params: graph_get_selector failed");
342 #define COPY_FIELD(field) do { \
343 const char *cfg_f = ident_get_##field (cfg_select); \
344 const char *inst_f = ident_get_##field (inst->select); \
345 if (strcmp (cfg_f, inst_f) == 0) \
347 strlcat (buffer, #field, buffer_size); \
348 strlcat (buffer, "=", buffer_size); \
349 strlcat (buffer, cfg_f, buffer_size); \
353 strlcat (buffer, "graph_", buffer_size); \
354 strlcat (buffer, #field, buffer_size); \
355 strlcat (buffer, "=", buffer_size); \
356 strlcat (buffer, cfg_f, buffer_size); \
357 strlcat (buffer, ";", buffer_size); \
358 strlcat (buffer, "inst_", buffer_size); \
359 strlcat (buffer, #field, buffer_size); \
360 strlcat (buffer, "=", buffer_size); \
361 strlcat (buffer, inst_f, buffer_size); \
366 strlcat (buffer, ";", buffer_size);
368 strlcat (buffer, ";", buffer_size);
369 COPY_FIELD(plugin_instance);
370 strlcat (buffer, ";", buffer_size);
372 strlcat (buffer, ";", buffer_size);
373 COPY_FIELD(type_instance);
377 ident_destroy (cfg_select);
380 } /* }}} int inst_get_params */
382 int inst_append (graph_instance_t *head, graph_instance_t *inst) /* {{{ */
384 graph_instance_t *ptr;
386 if ((head == NULL) || (inst == NULL))
390 while (ptr->next != NULL)
396 } /* }}} int inst_append */
398 int inst_foreach (graph_instance_t *inst, /* {{{ */
399 inst_callback_t cb, void *user_data)
401 graph_instance_t *ptr;
403 if ((inst == NULL) || (cb == NULL))
406 for (ptr = inst; ptr != NULL; ptr = ptr->next)
410 status = (*cb) (ptr, user_data);
416 } /* }}} int inst_foreach */
418 int inst_search (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
419 const char *term, inst_callback_t cb, void *user_data)
421 graph_instance_t *ptr;
425 if ((inst == NULL) || (cb == NULL))
428 for (ptr = inst; ptr != NULL; ptr = ptr->next)
430 status = inst_describe (cfg, ptr, buffer, sizeof (buffer));
433 fprintf (stderr, "inst_search: inst_describe failed\n");
438 if (strstr (buffer, term) == NULL)
442 status = (*cb) (ptr, user_data);
448 } /* }}} int inst_search */
450 graph_instance_t *inst_find_matching (graph_instance_t *inst, /* {{{ */
451 const graph_ident_t *ident)
453 graph_instance_t *ptr;
455 if ((inst == NULL) || (ident == NULL))
458 for (ptr = inst; ptr != NULL; ptr = ptr->next)
459 if (ident_matches (ptr->select, ident))
463 } /* }}} graph_instance_t *inst_find_matching */
465 int inst_describe (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
466 char *buffer, size_t buffer_size)
468 graph_ident_t *cfg_select;
470 if ((cfg == NULL) || (inst == NULL)
471 || (buffer == NULL) || (buffer_size < 2))
474 cfg_select = graph_get_selector (cfg);
475 if (cfg_select == NULL)
477 fprintf (stderr, "inst_describe: graph_get_selector failed\n");
483 #define CHECK_FIELD(field) do { \
484 if (IS_ANY (ident_get_##field (cfg_select))) \
486 if (buffer[0] != 0) \
487 strlcat (buffer, "/", buffer_size); \
488 strlcat (buffer, ident_get_##field (inst->select), buffer_size); \
493 CHECK_FIELD (plugin);
494 CHECK_FIELD (plugin_instance);
496 CHECK_FIELD (type_instance);
501 strlcat (buffer, "default", buffer_size);
503 ident_destroy (cfg_select);
506 } /* }}} int inst_describe */
508 /* vim: set sw=2 sts=2 et fdm=marker : */