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 /* FIXME: Re-enable title and vertical label stuff. */
293 if (cfg->title != NULL)
295 array_append (args, "-t");
296 array_append (args, cfg->title);
299 if (cfg->vertical_label != NULL)
301 array_append (args, "-v");
302 array_append (args, cfg->vertical_label);
306 defs = graph_get_defs (cfg);
309 defs = inst_get_default_defs (cfg, inst);
314 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
320 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
324 } /* }}} int inst_get_rrdargs */
326 graph_ident_t *inst_get_selector (graph_instance_t *inst) /* {{{ */
331 return (ident_clone (inst->select));
332 } /* }}} graph_ident_t *inst_get_selector */
334 int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
335 char *buffer, size_t buffer_size)
337 graph_ident_t *cfg_select;
339 if ((cfg == NULL) || (inst == NULL)
340 || (buffer == NULL) || (buffer_size < 1))
343 cfg_select = graph_get_selector (cfg);
344 if (cfg_select == NULL)
346 fprintf (stderr, "inst_get_params: graph_get_selector failed");
352 #define COPY_FIELD(field) do { \
353 const char *cfg_f = ident_get_##field (cfg_select); \
354 const char *inst_f = ident_get_##field (inst->select); \
355 if (strcmp (cfg_f, inst_f) == 0) \
357 strlcat (buffer, #field, buffer_size); \
358 strlcat (buffer, "=", buffer_size); \
359 strlcat (buffer, cfg_f, buffer_size); \
363 strlcat (buffer, "graph_", buffer_size); \
364 strlcat (buffer, #field, buffer_size); \
365 strlcat (buffer, "=", buffer_size); \
366 strlcat (buffer, cfg_f, buffer_size); \
367 strlcat (buffer, ";", buffer_size); \
368 strlcat (buffer, "inst_", buffer_size); \
369 strlcat (buffer, #field, buffer_size); \
370 strlcat (buffer, "=", buffer_size); \
371 strlcat (buffer, inst_f, buffer_size); \
376 strlcat (buffer, ";", buffer_size);
378 strlcat (buffer, ";", buffer_size);
379 COPY_FIELD(plugin_instance);
380 strlcat (buffer, ";", buffer_size);
382 strlcat (buffer, ";", buffer_size);
383 COPY_FIELD(type_instance);
387 ident_destroy (cfg_select);
390 } /* }}} int inst_get_params */
392 int inst_append (graph_instance_t *head, graph_instance_t *inst) /* {{{ */
394 graph_instance_t *ptr;
396 if ((head == NULL) || (inst == NULL))
400 while (ptr->next != NULL)
406 } /* }}} int inst_append */
408 int inst_foreach (graph_instance_t *inst, /* {{{ */
409 inst_callback_t cb, void *user_data)
411 graph_instance_t *ptr;
413 if ((inst == NULL) || (cb == NULL))
416 for (ptr = inst; ptr != NULL; ptr = ptr->next)
420 status = (*cb) (ptr, user_data);
426 } /* }}} int inst_foreach */
428 graph_instance_t *inst_find_matching (graph_instance_t *inst, /* {{{ */
429 const graph_ident_t *ident)
431 graph_instance_t *ptr;
433 if ((inst == NULL) || (ident == NULL))
436 for (ptr = inst; ptr != NULL; ptr = ptr->next)
437 if (ident_matches (ptr->select, ident))
441 } /* }}} graph_instance_t *inst_find_matching */
443 /* vim: set sw=2 sts=2 et fdm=marker : */