5 #include "graph_instance.h"
6 #include "graph_ident.h"
8 #include "utils_params.h"
11 #include <fcgi_stdio.h>
13 struct graph_instance_s /* {{{ */
15 graph_ident_t *select;
17 graph_ident_t **files;
20 graph_instance_t *next;
21 }; /* }}} struct graph_instance_s */
23 struct def_callback_data_s
25 graph_instance_t *inst;
28 typedef struct def_callback_data_s def_callback_data_t;
33 /* Create one DEF for each data source in the file. Called by
34 * "inst_get_default_defs" for each file. */
35 static graph_def_t *ident_get_default_defs (graph_config_t *cfg, /* {{{ */
36 graph_ident_t *ident, graph_def_t *def_head)
38 graph_def_t *defs = NULL;
45 if ((cfg == NULL) || (ident == NULL))
48 file = ident_to_file (ident);
51 fprintf (stderr, "ident_get_default_defs: ident_to_file failed\n");
55 status = ds_list_from_rrd_file (file, &dses_num, &dses);
62 for (i = 0; i < dses_num; i++)
66 def = def_search (def_head, ident, dses[i]);
70 def = def_create (cfg, ident, dses[i]);
77 def_append (defs, def);
86 } /* }}} int ident_get_default_defs */
88 /* Create one or more DEFs for each file in the graph instance. The number
89 * depends on the number of data sources in each of the files. Called from
90 * "inst_get_rrdargs" if no DEFs are available from the configuration.
92 static graph_def_t *inst_get_default_defs (graph_config_t *cfg, /* {{{ */
93 graph_instance_t *inst)
95 graph_def_t *defs = NULL;
98 if ((cfg == NULL) || (inst == NULL))
101 for (i = 0; i < inst->files_num; i++)
105 def = ident_get_default_defs (cfg, inst->files[i], defs);
112 def_append (defs, def);
116 } /* }}} graph_def_t *inst_get_default_defs */
118 /* Called with each DEF in turn. Calls "def_get_rrdargs" with every appropriate
119 * file / DEF pair. */
120 static int gl_instance_get_rrdargs_cb (graph_def_t *def, void *user_data) /* {{{ */
122 def_callback_data_t *data = user_data;
123 graph_instance_t *inst = data->inst;
124 str_array_t *args = data->args;
128 for (i = 0; i < inst->files_num; i++)
130 if (!def_matches (def, inst->files[i]))
133 def_get_rrdargs (def, inst->files[i], args);
137 } /* }}} int gl_instance_get_rrdargs_cb */
139 static const char *get_part_from_param (const char *prim_key, /* {{{ */
144 val = param (prim_key);
148 return (param (sec_key));
149 } /* }}} const char *get_part_from_param */
154 graph_instance_t *inst_create (graph_config_t *cfg, /* {{{ */
155 const graph_ident_t *ident)
158 graph_ident_t *selector;
160 if ((cfg == NULL) || (ident == NULL))
163 i = malloc (sizeof (*i));
166 memset (i, 0, sizeof (*i));
168 selector = gl_graph_get_selector (cfg);
169 if (selector == NULL)
171 fprintf (stderr, "inst_create: gl_graph_get_selector failed\n");
176 i->select = ident_copy_with_selector (selector, ident,
177 IDENT_FLAG_REPLACE_ANY);
178 if (i->select == NULL)
180 fprintf (stderr, "inst_create: ident_copy_with_selector failed\n");
181 ident_destroy (selector);
186 ident_destroy (selector);
194 } /* }}} graph_instance_t *inst_create */
196 void inst_destroy (graph_instance_t *inst) /* {{{ */
198 graph_instance_t *next;
206 ident_destroy (inst->select);
208 for (i = 0; i < inst->files_num; i++)
209 ident_destroy (inst->files[i]);
215 } /* }}} void inst_destroy */
217 int inst_add_file (graph_instance_t *inst, /* {{{ */
218 const graph_ident_t *file)
222 tmp = realloc (inst->files, sizeof (*inst->files) * (inst->files_num + 1));
227 inst->files[inst->files_num] = ident_clone (file);
228 if (inst->files[inst->files_num] == NULL)
234 } /* }}} int inst_add_file */
236 graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
238 const char *host = get_part_from_param ("inst_host", "host");
239 const char *plugin = get_part_from_param ("inst_plugin", "plugin");
240 const char *plugin_instance = get_part_from_param ("inst_plugin_instance", "plugin_instance");
241 const char *type = get_part_from_param ("inst_type", "type");
242 const char *type_instance = get_part_from_param ("inst_type_instance", "type_instance");
243 graph_ident_t *ident;
244 graph_instance_t *inst;
247 cfg = graph_get_selected ();
251 DEBUG ("inst_get_selected: cfg == NULL;\n");
256 || (plugin == NULL) || (plugin_instance == NULL)
257 || (type == NULL) || (type_instance == NULL))
259 DEBUG ("inst_get_selected: A parameter is NULL.\n");
263 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
265 for (inst = gl_graph_get_instances (cfg); inst != NULL; inst = inst->next)
267 if (ident_compare (ident, inst->select) != 0)
270 ident_destroy (ident);
274 DEBUG ("inst_get_selected: No match found.\n");
275 ident_destroy (ident);
277 } /* }}} graph_instance_t *inst_get_selected */
279 int inst_get_rrdargs (graph_config_t *cfg, /* {{{ */
280 graph_instance_t *inst,
283 def_callback_data_t data = { inst, args };
287 if ((cfg == NULL) || (inst == NULL) || (args == NULL))
290 /* FIXME: Re-enable title and vertical label stuff. */
292 if (cfg->title != NULL)
294 array_append (args, "-t");
295 array_append (args, cfg->title);
298 if (cfg->vertical_label != NULL)
300 array_append (args, "-v");
301 array_append (args, cfg->vertical_label);
305 defs = gl_graph_get_defs (cfg);
308 defs = inst_get_default_defs (cfg, inst);
313 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
319 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
323 } /* }}} int inst_get_rrdargs */
325 graph_ident_t *inst_get_selector (graph_instance_t *inst) /* {{{ */
330 return (ident_clone (inst->select));
331 } /* }}} graph_ident_t *inst_get_selector */
333 int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
334 char *buffer, size_t buffer_size)
336 graph_ident_t *cfg_select;
338 if ((cfg == NULL) || (inst == NULL)
339 || (buffer == NULL) || (buffer_size < 1))
342 cfg_select = gl_graph_get_selector (cfg);
343 if (cfg_select == NULL)
345 fprintf (stderr, "inst_get_params: gl_graph_get_selector failed");
351 #define COPY_FIELD(field) do { \
352 const char *cfg_f = ident_get_##field (cfg_select); \
353 const char *inst_f = ident_get_##field (inst->select); \
354 if (strcmp (cfg_f, inst_f) == 0) \
356 strlcat (buffer, #field, buffer_size); \
357 strlcat (buffer, "=", buffer_size); \
358 strlcat (buffer, cfg_f, buffer_size); \
362 strlcat (buffer, "graph_", buffer_size); \
363 strlcat (buffer, #field, buffer_size); \
364 strlcat (buffer, "=", buffer_size); \
365 strlcat (buffer, cfg_f, buffer_size); \
366 strlcat (buffer, ";", buffer_size); \
367 strlcat (buffer, "inst_", buffer_size); \
368 strlcat (buffer, #field, buffer_size); \
369 strlcat (buffer, "=", buffer_size); \
370 strlcat (buffer, inst_f, buffer_size); \
375 strlcat (buffer, ";", buffer_size);
377 strlcat (buffer, ";", buffer_size);
378 COPY_FIELD(plugin_instance);
379 strlcat (buffer, ";", buffer_size);
381 strlcat (buffer, ";", buffer_size);
382 COPY_FIELD(type_instance);
386 ident_destroy (cfg_select);
389 } /* }}} int inst_get_params */
391 int inst_append (graph_instance_t *head, graph_instance_t *inst) /* {{{ */
393 graph_instance_t *ptr;
395 if ((head == NULL) || (inst == NULL))
399 while (ptr->next != NULL)
405 } /* }}} int inst_append */
407 int inst_foreach (graph_instance_t *inst, /* {{{ */
408 inst_callback_t cb, void *user_data)
410 graph_instance_t *ptr;
412 if ((inst == NULL) || (cb == NULL))
415 for (ptr = inst; ptr != NULL; ptr = ptr->next)
419 status = (*cb) (ptr, user_data);
425 } /* }}} int inst_foreach */
427 graph_instance_t *inst_find_matching (graph_instance_t *inst, /* {{{ */
428 const graph_ident_t *ident)
430 graph_instance_t *ptr;
432 if ((inst == NULL) || (ident == NULL))
435 for (ptr = inst; ptr != NULL; ptr = ptr->next)
436 if (ident_matches (ptr->select, ident))
440 } /* }}} graph_instance_t *inst_find_matching */
442 /* vim: set sw=2 sts=2 et fdm=marker : */