7 #include "graph_instance.h"
10 #include "graph_ident.h"
11 #include "graph_list.h"
13 #include "utils_cgi.h"
16 #include <fcgi_stdio.h>
18 struct graph_instance_s /* {{{ */
20 graph_ident_t *select;
22 graph_ident_t **files;
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 */
154 static graph_ident_t *inst_get_selector_from_params (void) /* {{{ */
156 const char *host = get_part_from_param ("inst_host", "host");
157 const char *plugin = get_part_from_param ("inst_plugin", "plugin");
158 const char *plugin_instance = get_part_from_param ("inst_plugin_instance",
160 const char *type = get_part_from_param ("inst_type", "type");
161 const char *type_instance = get_part_from_param ("inst_type_instance",
164 graph_ident_t *ident;
167 || (plugin == NULL) || (plugin_instance == NULL)
168 || (type == NULL) || (type_instance == NULL))
170 fprintf (stderr, "inst_get_selected: A parameter is NULL\n");
174 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
177 fprintf (stderr, "inst_get_selected: ident_create failed\n");
182 } /* }}} graph_ident_t *inst_get_selector_from_params */
187 graph_instance_t *inst_create (graph_config_t *cfg, /* {{{ */
188 const graph_ident_t *ident)
191 graph_ident_t *selector;
193 if ((cfg == NULL) || (ident == NULL))
196 i = malloc (sizeof (*i));
199 memset (i, 0, sizeof (*i));
201 selector = graph_get_selector (cfg);
202 if (selector == NULL)
204 fprintf (stderr, "inst_create: graph_get_selector failed\n");
209 i->select = ident_copy_with_selector (selector, ident,
210 IDENT_FLAG_REPLACE_ANY);
211 if (i->select == NULL)
213 fprintf (stderr, "inst_create: ident_copy_with_selector failed\n");
214 ident_destroy (selector);
219 ident_destroy (selector);
225 } /* }}} graph_instance_t *inst_create */
227 void inst_destroy (graph_instance_t *inst) /* {{{ */
234 ident_destroy (inst->select);
236 for (i = 0; i < inst->files_num; i++)
237 ident_destroy (inst->files[i]);
241 } /* }}} void inst_destroy */
243 int inst_add_file (graph_instance_t *inst, /* {{{ */
244 const graph_ident_t *file)
248 tmp = realloc (inst->files, sizeof (*inst->files) * (inst->files_num + 1));
253 inst->files[inst->files_num] = ident_clone (file);
254 if (inst->files[inst->files_num] == NULL)
260 } /* }}} int inst_add_file */
262 graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
264 graph_ident_t *ident;
265 graph_instance_t *inst;
268 cfg = gl_graph_get_selected ();
272 DEBUG ("inst_get_selected: cfg == NULL;\n");
276 ident = inst_get_selector_from_params ();
279 fprintf (stderr, "inst_get_selected: ident_create failed\n");
283 inst = graph_inst_find_exact (cfg, ident);
285 ident_destroy (ident);
287 } /* }}} graph_instance_t *inst_get_selected */
289 int inst_get_all_selected (graph_config_t *cfg, /* {{{ */
290 graph_inst_callback_t callback, void *user_data)
292 graph_ident_t *ident;
295 if ((cfg == NULL) || (callback == NULL))
298 ident = inst_get_selector_from_params ();
301 fprintf (stderr, "inst_get_all_selected: "
302 "inst_get_selector_from_params failed\n");
306 status = graph_inst_find_all_matching (cfg, ident, callback, user_data);
308 ident_destroy (ident);
310 } /* }}} int inst_get_all_selected */
312 int inst_get_rrdargs (graph_config_t *cfg, /* {{{ */
313 graph_instance_t *inst,
316 def_callback_data_t data = { inst, args };
320 if ((cfg == NULL) || (inst == NULL) || (args == NULL))
323 status = graph_get_rrdargs (cfg, inst, args);
327 defs = graph_get_defs (cfg);
330 defs = inst_get_default_defs (cfg, inst);
335 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
341 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
345 } /* }}} int inst_get_rrdargs */
347 graph_ident_t *inst_get_selector (graph_instance_t *inst) /* {{{ */
352 return (ident_clone (inst->select));
353 } /* }}} graph_ident_t *inst_get_selector */
355 int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
356 char *buffer, size_t buffer_size)
358 graph_ident_t *cfg_select;
360 if ((cfg == NULL) || (inst == NULL)
361 || (buffer == NULL) || (buffer_size < 1))
364 cfg_select = graph_get_selector (cfg);
365 if (cfg_select == NULL)
367 fprintf (stderr, "inst_get_params: graph_get_selector failed");
373 #define COPY_FIELD(field) do { \
374 const char *cfg_f = ident_get_##field (cfg_select); \
375 const char *inst_f = ident_get_##field (inst->select); \
376 if (strcmp (cfg_f, inst_f) == 0) \
378 strlcat (buffer, #field, buffer_size); \
379 strlcat (buffer, "=", buffer_size); \
380 strlcat (buffer, cfg_f, buffer_size); \
384 strlcat (buffer, "graph_", buffer_size); \
385 strlcat (buffer, #field, buffer_size); \
386 strlcat (buffer, "=", buffer_size); \
387 strlcat (buffer, cfg_f, buffer_size); \
388 strlcat (buffer, ";", buffer_size); \
389 strlcat (buffer, "inst_", buffer_size); \
390 strlcat (buffer, #field, buffer_size); \
391 strlcat (buffer, "=", buffer_size); \
392 strlcat (buffer, inst_f, buffer_size); \
397 strlcat (buffer, ";", buffer_size);
399 strlcat (buffer, ";", buffer_size);
400 COPY_FIELD(plugin_instance);
401 strlcat (buffer, ";", buffer_size);
403 strlcat (buffer, ";", buffer_size);
404 COPY_FIELD(type_instance);
408 ident_destroy (cfg_select);
411 } /* }}} int inst_get_params */
413 int inst_compare_ident (graph_instance_t *inst, /* {{{ */
414 const graph_ident_t *ident)
416 if ((inst == NULL) || (ident == NULL))
419 return (ident_compare (inst->select, ident));
420 } /* }}} int inst_compare_ident */
422 _Bool inst_ident_matches (graph_instance_t *inst, /* {{{ */
423 const graph_ident_t *ident)
425 if ((inst == NULL) || (ident == NULL))
428 return (ident_matches (inst->select, ident));
429 } /* }}} _Bool inst_ident_matches */
431 _Bool inst_matches_ident (graph_instance_t *inst, /* {{{ */
432 const graph_ident_t *ident)
434 if ((inst == NULL) || (ident == NULL))
437 return (ident_matches (ident, inst->select));
438 } /* }}} _Bool inst_matches_ident */
440 _Bool inst_matches_string (graph_config_t *cfg, /* {{{ */
441 graph_instance_t *inst,
447 if ((cfg == NULL) || (inst == NULL) || (term == NULL))
450 status = inst_describe (cfg, inst, buffer, sizeof (buffer));
453 fprintf (stderr, "inst_matches_string: inst_describe failed\n");
460 if (strstr (buffer, term) == NULL)
464 } /* }}} _Bool inst_matches_string */
466 _Bool inst_matches_field (graph_instance_t *inst, /* {{{ */
467 graph_ident_field_t field, const char *field_value)
469 const char *selector_field;
472 if ((inst == NULL) || (field_value == NULL))
475 selector_field = ident_get_field (inst->select, field);
476 if (selector_field == NULL)
479 assert (!IS_ANY (selector_field));
480 if (!IS_ALL (selector_field))
482 if (strcasecmp (selector_field, field_value) == 0)
488 /* The selector field is an ALL selector
489 * => we need to check the files to see if the instance matches. */
490 for (i = 0; i < inst->files_num; i++)
492 selector_field = ident_get_field (inst->files[i], field);
493 if (selector_field == NULL)
496 assert (!IS_ANY (selector_field));
497 assert (!IS_ALL (selector_field));
499 if (strcasecmp (selector_field, field_value) == 0)
504 } /* }}} _Bool inst_matches_field */
506 int inst_describe (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
507 char *buffer, size_t buffer_size)
509 graph_ident_t *cfg_select;
511 if ((cfg == NULL) || (inst == NULL)
512 || (buffer == NULL) || (buffer_size < 2))
515 cfg_select = graph_get_selector (cfg);
516 if (cfg_select == NULL)
518 fprintf (stderr, "inst_describe: graph_get_selector failed\n");
524 #define CHECK_FIELD(field) do { \
525 if (IS_ANY (ident_get_##field (cfg_select))) \
527 if (buffer[0] != 0) \
528 strlcat (buffer, "/", buffer_size); \
529 strlcat (buffer, ident_get_##field (inst->select), buffer_size); \
534 CHECK_FIELD (plugin);
535 CHECK_FIELD (plugin_instance);
537 CHECK_FIELD (type_instance);
542 strlcat (buffer, "default", buffer_size);
544 ident_destroy (cfg_select);
547 } /* }}} int inst_describe */
549 time_t inst_get_mtime (graph_instance_t *inst) /* {{{ */
558 for (i = 0; i < inst->files_num; i++)
562 tmp = ident_get_mtime (inst->files[i]);
568 } /* }}} time_t inst_get_mtime */
570 /* vim: set sw=2 sts=2 et fdm=marker : */