6 #include "graph_instance.h"
8 #include "graph_ident.h"
9 #include "graph_list.h"
11 #include "utils_cgi.h"
14 #include <fcgi_stdio.h>
16 struct graph_instance_s /* {{{ */
18 graph_ident_t *select;
20 graph_ident_t **files;
23 graph_instance_t *next;
24 }; /* }}} struct graph_instance_s */
26 struct def_callback_data_s
28 graph_instance_t *inst;
31 typedef struct def_callback_data_s def_callback_data_t;
36 /* Create one DEF for each data source in the file. Called by
37 * "inst_get_default_defs" for each file. */
38 static graph_def_t *ident_get_default_defs (graph_config_t *cfg, /* {{{ */
39 graph_ident_t *ident, graph_def_t *def_head)
41 graph_def_t *defs = NULL;
48 if ((cfg == NULL) || (ident == NULL))
51 file = ident_to_file (ident);
54 fprintf (stderr, "ident_get_default_defs: ident_to_file failed\n");
58 status = ds_list_from_rrd_file (file, &dses_num, &dses);
65 for (i = 0; i < dses_num; i++)
69 def = def_search (def_head, ident, dses[i]);
73 def = def_create (cfg, ident, dses[i]);
80 def_append (defs, def);
89 } /* }}} int ident_get_default_defs */
91 /* Create one or more DEFs for each file in the graph instance. The number
92 * depends on the number of data sources in each of the files. Called from
93 * "inst_get_rrdargs" if no DEFs are available from the configuration.
95 static graph_def_t *inst_get_default_defs (graph_config_t *cfg, /* {{{ */
96 graph_instance_t *inst)
98 graph_def_t *defs = NULL;
101 if ((cfg == NULL) || (inst == NULL))
104 for (i = 0; i < inst->files_num; i++)
108 def = ident_get_default_defs (cfg, inst->files[i], defs);
115 def_append (defs, def);
119 } /* }}} graph_def_t *inst_get_default_defs */
121 /* Called with each DEF in turn. Calls "def_get_rrdargs" with every appropriate
122 * file / DEF pair. */
123 static int gl_instance_get_rrdargs_cb (graph_def_t *def, void *user_data) /* {{{ */
125 def_callback_data_t *data = user_data;
126 graph_instance_t *inst = data->inst;
127 str_array_t *args = data->args;
131 for (i = 0; i < inst->files_num; i++)
133 if (!def_matches (def, inst->files[i]))
136 def_get_rrdargs (def, inst->files[i], args);
140 } /* }}} int gl_instance_get_rrdargs_cb */
142 static const char *get_part_from_param (const char *prim_key, /* {{{ */
147 val = param (prim_key);
151 return (param (sec_key));
152 } /* }}} const char *get_part_from_param */
157 graph_instance_t *inst_create (graph_config_t *cfg, /* {{{ */
158 const graph_ident_t *ident)
161 graph_ident_t *selector;
163 if ((cfg == NULL) || (ident == NULL))
166 i = malloc (sizeof (*i));
169 memset (i, 0, sizeof (*i));
171 selector = graph_get_selector (cfg);
172 if (selector == NULL)
174 fprintf (stderr, "inst_create: graph_get_selector failed\n");
179 i->select = ident_copy_with_selector (selector, ident,
180 IDENT_FLAG_REPLACE_ANY);
181 if (i->select == NULL)
183 fprintf (stderr, "inst_create: ident_copy_with_selector failed\n");
184 ident_destroy (selector);
189 ident_destroy (selector);
197 } /* }}} graph_instance_t *inst_create */
199 void inst_destroy (graph_instance_t *inst) /* {{{ */
201 graph_instance_t *next;
209 ident_destroy (inst->select);
211 for (i = 0; i < inst->files_num; i++)
212 ident_destroy (inst->files[i]);
218 } /* }}} void inst_destroy */
220 int inst_add_file (graph_instance_t *inst, /* {{{ */
221 const graph_ident_t *file)
225 tmp = realloc (inst->files, sizeof (*inst->files) * (inst->files_num + 1));
230 inst->files[inst->files_num] = ident_clone (file);
231 if (inst->files[inst->files_num] == NULL)
237 } /* }}} int inst_add_file */
239 graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
241 const char *host = get_part_from_param ("inst_host", "host");
242 const char *plugin = get_part_from_param ("inst_plugin", "plugin");
243 const char *plugin_instance = get_part_from_param ("inst_plugin_instance", "plugin_instance");
244 const char *type = get_part_from_param ("inst_type", "type");
245 const char *type_instance = get_part_from_param ("inst_type_instance", "type_instance");
246 graph_ident_t *ident;
247 graph_instance_t *inst;
250 cfg = gl_graph_get_selected ();
254 DEBUG ("inst_get_selected: cfg == NULL;\n");
259 || (plugin == NULL) || (plugin_instance == NULL)
260 || (type == NULL) || (type_instance == NULL))
262 DEBUG ("inst_get_selected: A parameter is NULL.\n");
266 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
268 for (inst = graph_get_instances (cfg); inst != NULL; inst = inst->next)
270 if (ident_compare (ident, inst->select) != 0)
273 ident_destroy (ident);
277 DEBUG ("inst_get_selected: No match found.\n");
278 ident_destroy (ident);
280 } /* }}} graph_instance_t *inst_get_selected */
282 int inst_get_rrdargs (graph_config_t *cfg, /* {{{ */
283 graph_instance_t *inst,
286 def_callback_data_t data = { inst, args };
290 if ((cfg == NULL) || (inst == NULL) || (args == NULL))
293 status = graph_get_rrdargs (cfg, inst, args);
297 defs = graph_get_defs (cfg);
300 defs = inst_get_default_defs (cfg, inst);
305 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
311 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
315 } /* }}} int inst_get_rrdargs */
317 graph_ident_t *inst_get_selector (graph_instance_t *inst) /* {{{ */
322 return (ident_clone (inst->select));
323 } /* }}} graph_ident_t *inst_get_selector */
325 int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
326 char *buffer, size_t buffer_size)
328 graph_ident_t *cfg_select;
330 if ((cfg == NULL) || (inst == NULL)
331 || (buffer == NULL) || (buffer_size < 1))
334 cfg_select = graph_get_selector (cfg);
335 if (cfg_select == NULL)
337 fprintf (stderr, "inst_get_params: graph_get_selector failed");
343 #define COPY_FIELD(field) do { \
344 const char *cfg_f = ident_get_##field (cfg_select); \
345 const char *inst_f = ident_get_##field (inst->select); \
346 if (strcmp (cfg_f, inst_f) == 0) \
348 strlcat (buffer, #field, buffer_size); \
349 strlcat (buffer, "=", buffer_size); \
350 strlcat (buffer, cfg_f, buffer_size); \
354 strlcat (buffer, "graph_", buffer_size); \
355 strlcat (buffer, #field, buffer_size); \
356 strlcat (buffer, "=", buffer_size); \
357 strlcat (buffer, cfg_f, buffer_size); \
358 strlcat (buffer, ";", buffer_size); \
359 strlcat (buffer, "inst_", buffer_size); \
360 strlcat (buffer, #field, buffer_size); \
361 strlcat (buffer, "=", buffer_size); \
362 strlcat (buffer, inst_f, buffer_size); \
367 strlcat (buffer, ";", buffer_size);
369 strlcat (buffer, ";", buffer_size);
370 COPY_FIELD(plugin_instance);
371 strlcat (buffer, ";", buffer_size);
373 strlcat (buffer, ";", buffer_size);
374 COPY_FIELD(type_instance);
378 ident_destroy (cfg_select);
381 } /* }}} int inst_get_params */
383 int inst_append (graph_instance_t *head, graph_instance_t *inst) /* {{{ */
385 graph_instance_t *ptr;
387 if ((head == NULL) || (inst == NULL))
391 while (ptr->next != NULL)
397 } /* }}} int inst_append */
399 int inst_foreach (graph_instance_t *inst, /* {{{ */
400 inst_callback_t cb, void *user_data)
402 graph_instance_t *ptr;
404 if ((inst == NULL) || (cb == NULL))
407 for (ptr = inst; ptr != NULL; ptr = ptr->next)
411 status = (*cb) (ptr, user_data);
417 } /* }}} int inst_foreach */
419 int inst_search (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
420 const char *term, inst_callback_t cb, void *user_data)
422 graph_instance_t *ptr;
426 if ((inst == NULL) || (cb == NULL))
429 for (ptr = inst; ptr != NULL; ptr = ptr->next)
431 status = inst_describe (cfg, ptr, buffer, sizeof (buffer));
434 fprintf (stderr, "inst_search: inst_describe failed\n");
439 if (strstr (buffer, term) == NULL)
443 status = (*cb) (ptr, user_data);
449 } /* }}} int inst_search */
451 graph_instance_t *inst_find_matching (graph_instance_t *inst, /* {{{ */
452 const graph_ident_t *ident)
454 graph_instance_t *ptr;
456 if ((inst == NULL) || (ident == NULL))
459 for (ptr = inst; ptr != NULL; ptr = ptr->next)
460 if (ident_matches (ptr->select, ident))
464 } /* }}} graph_instance_t *inst_find_matching */
466 int inst_describe (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
467 char *buffer, size_t buffer_size)
469 graph_ident_t *cfg_select;
471 if ((cfg == NULL) || (inst == NULL)
472 || (buffer == NULL) || (buffer_size < 2))
475 cfg_select = graph_get_selector (cfg);
476 if (cfg_select == NULL)
478 fprintf (stderr, "inst_describe: graph_get_selector failed\n");
484 #define CHECK_FIELD(field) do { \
485 if (IS_ANY (ident_get_##field (cfg_select))) \
487 if (buffer[0] != 0) \
488 strlcat (buffer, "/", buffer_size); \
489 strlcat (buffer, ident_get_##field (inst->select), buffer_size); \
494 CHECK_FIELD (plugin);
495 CHECK_FIELD (plugin_instance);
497 CHECK_FIELD (type_instance);
502 strlcat (buffer, "default", buffer_size);
504 ident_destroy (cfg_select);
507 } /* }}} int inst_describe */
509 time_t inst_get_mtime (graph_instance_t *inst) /* {{{ */
518 for (i = 0; i < inst->files_num; i++)
522 tmp = ident_get_mtime (inst->files[i]);
528 } /* }}} time_t inst_get_mtime */
530 /* vim: set sw=2 sts=2 et fdm=marker : */