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;
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);
193 } /* }}} graph_instance_t *inst_create */
195 void inst_destroy (graph_instance_t *inst) /* {{{ */
202 ident_destroy (inst->select);
204 for (i = 0; i < inst->files_num; i++)
205 ident_destroy (inst->files[i]);
209 } /* }}} void inst_destroy */
211 int inst_add_file (graph_instance_t *inst, /* {{{ */
212 const graph_ident_t *file)
216 tmp = realloc (inst->files, sizeof (*inst->files) * (inst->files_num + 1));
221 inst->files[inst->files_num] = ident_clone (file);
222 if (inst->files[inst->files_num] == NULL)
228 } /* }}} int inst_add_file */
230 graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
232 const char *host = get_part_from_param ("inst_host", "host");
233 const char *plugin = get_part_from_param ("inst_plugin", "plugin");
234 const char *plugin_instance = get_part_from_param ("inst_plugin_instance",
236 const char *type = get_part_from_param ("inst_type", "type");
237 const char *type_instance = get_part_from_param ("inst_type_instance",
240 graph_ident_t *ident;
241 graph_instance_t *inst;
244 cfg = gl_graph_get_selected ();
248 DEBUG ("inst_get_selected: cfg == NULL;\n");
253 || (plugin == NULL) || (plugin_instance == NULL)
254 || (type == NULL) || (type_instance == NULL))
256 DEBUG ("inst_get_selected: A parameter is NULL.\n");
260 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
263 fprintf (stderr, "inst_get_selected: ident_create failed\n");
267 inst = graph_inst_find_exact (cfg, ident);
268 ident_destroy (ident);
271 } /* }}} graph_instance_t *inst_get_selected */
273 int inst_get_rrdargs (graph_config_t *cfg, /* {{{ */
274 graph_instance_t *inst,
277 def_callback_data_t data = { inst, args };
281 if ((cfg == NULL) || (inst == NULL) || (args == NULL))
284 status = graph_get_rrdargs (cfg, inst, args);
288 defs = graph_get_defs (cfg);
291 defs = inst_get_default_defs (cfg, inst);
296 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
302 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
306 } /* }}} int inst_get_rrdargs */
308 graph_ident_t *inst_get_selector (graph_instance_t *inst) /* {{{ */
313 return (ident_clone (inst->select));
314 } /* }}} graph_ident_t *inst_get_selector */
316 int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
317 char *buffer, size_t buffer_size)
319 graph_ident_t *cfg_select;
321 if ((cfg == NULL) || (inst == NULL)
322 || (buffer == NULL) || (buffer_size < 1))
325 cfg_select = graph_get_selector (cfg);
326 if (cfg_select == NULL)
328 fprintf (stderr, "inst_get_params: graph_get_selector failed");
334 #define COPY_FIELD(field) do { \
335 const char *cfg_f = ident_get_##field (cfg_select); \
336 const char *inst_f = ident_get_##field (inst->select); \
337 if (strcmp (cfg_f, inst_f) == 0) \
339 strlcat (buffer, #field, buffer_size); \
340 strlcat (buffer, "=", buffer_size); \
341 strlcat (buffer, cfg_f, buffer_size); \
345 strlcat (buffer, "graph_", buffer_size); \
346 strlcat (buffer, #field, buffer_size); \
347 strlcat (buffer, "=", buffer_size); \
348 strlcat (buffer, cfg_f, buffer_size); \
349 strlcat (buffer, ";", buffer_size); \
350 strlcat (buffer, "inst_", buffer_size); \
351 strlcat (buffer, #field, buffer_size); \
352 strlcat (buffer, "=", buffer_size); \
353 strlcat (buffer, inst_f, buffer_size); \
358 strlcat (buffer, ";", buffer_size);
360 strlcat (buffer, ";", buffer_size);
361 COPY_FIELD(plugin_instance);
362 strlcat (buffer, ";", buffer_size);
364 strlcat (buffer, ";", buffer_size);
365 COPY_FIELD(type_instance);
369 ident_destroy (cfg_select);
372 } /* }}} int inst_get_params */
374 int inst_compare_ident (graph_instance_t *inst, /* {{{ */
375 const graph_ident_t *ident)
377 if ((inst == NULL) || (ident == NULL))
380 return (ident_compare (inst->select, ident));
381 } /* }}} int inst_compare_ident */
383 _Bool inst_matches_ident (graph_instance_t *inst, /* {{{ */
384 const graph_ident_t *ident)
386 if ((inst == NULL) || (ident == NULL))
389 return (ident_matches (inst->select, ident));
390 } /* }}} _Bool inst_matches_ident */
392 _Bool inst_matches_string (graph_config_t *cfg, /* {{{ */
393 graph_instance_t *inst,
399 if ((cfg == NULL) || (inst == NULL) || (term == NULL))
402 status = inst_describe (cfg, inst, buffer, sizeof (buffer));
405 fprintf (stderr, "inst_matches_string: inst_describe failed\n");
412 if (strstr (buffer, term) == NULL)
416 } /* }}} _Bool inst_matches_string */
418 _Bool inst_matches_field (graph_instance_t *inst, /* {{{ */
419 graph_ident_field_t field, const char *field_value)
421 const char *selector_field;
424 if ((inst == NULL) || (field_value == NULL))
427 selector_field = ident_get_field (inst->select, field);
428 if (selector_field == NULL)
431 assert (!IS_ANY (selector_field));
432 if (!IS_ALL (selector_field))
434 if (strcasecmp (selector_field, field_value) == 0)
440 /* The selector field is an ALL selector
441 * => we need to check the files to see if the instance matches. */
442 for (i = 0; i < inst->files_num; i++)
444 selector_field = ident_get_field (inst->files[i], field);
445 if (selector_field == NULL)
448 assert (!IS_ANY (selector_field));
449 assert (!IS_ALL (selector_field));
451 if (strcasecmp (selector_field, field_value) == 0)
456 } /* }}} _Bool inst_matches_field */
458 int inst_describe (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
459 char *buffer, size_t buffer_size)
461 graph_ident_t *cfg_select;
463 if ((cfg == NULL) || (inst == NULL)
464 || (buffer == NULL) || (buffer_size < 2))
467 cfg_select = graph_get_selector (cfg);
468 if (cfg_select == NULL)
470 fprintf (stderr, "inst_describe: graph_get_selector failed\n");
476 #define CHECK_FIELD(field) do { \
477 if (IS_ANY (ident_get_##field (cfg_select))) \
479 if (buffer[0] != 0) \
480 strlcat (buffer, "/", buffer_size); \
481 strlcat (buffer, ident_get_##field (inst->select), buffer_size); \
486 CHECK_FIELD (plugin);
487 CHECK_FIELD (plugin_instance);
489 CHECK_FIELD (type_instance);
494 strlcat (buffer, "default", buffer_size);
496 ident_destroy (cfg_select);
499 } /* }}} int inst_describe */
501 time_t inst_get_mtime (graph_instance_t *inst) /* {{{ */
510 for (i = 0; i < inst->files_num; i++)
514 tmp = ident_get_mtime (inst->files[i]);
520 } /* }}} time_t inst_get_mtime */
522 /* vim: set sw=2 sts=2 et fdm=marker : */