2 * collection4 - graph_instance.c
3 * Copyright (C) 2010 Florian octo Forster
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
21 * Florian octo Forster <ff at octo.it>
30 #include "graph_instance.h"
32 #include "graph_def.h"
33 #include "graph_ident.h"
34 #include "graph_list.h"
36 #include "utils_cgi.h"
39 #include <fcgi_stdio.h>
41 struct graph_instance_s /* {{{ */
43 graph_ident_t *select;
45 graph_ident_t **files;
47 }; /* }}} struct graph_instance_s */
49 struct def_callback_data_s
51 graph_instance_t *inst;
54 typedef struct def_callback_data_s def_callback_data_t;
59 struct ident_get_default_defs__data_s
64 typedef struct ident_get_default_defs__data_s ident_get_default_defs__data_t;
66 static int ident_get_default_defs__callback (__attribute__((unused))
68 const char *ds_name, void *user_data)
70 ident_get_default_defs__data_t *data = user_data;
73 tmp = realloc (data->dses, (data->dses_num + 1) * sizeof (data->dses));
78 data->dses[data->dses_num] = strdup (ds_name);
79 if (data->dses[data->dses_num] == NULL)
84 } /* }}} int ident_get_default_defs__callback */
86 /* Create one DEF for each data source in the file. Called by
87 * "inst_get_default_defs" for each file. */
88 static graph_def_t *ident_get_default_defs (graph_config_t *cfg, /* {{{ */
89 graph_ident_t *ident, graph_def_t *def_head)
91 graph_def_t *defs = NULL;
93 ident_get_default_defs__data_t ds_data;
100 if ((cfg == NULL) || (ident == NULL))
103 file = ident_to_file (ident);
106 fprintf (stderr, "ident_get_default_defs: ident_to_file failed\n");
110 status = data_provider_get_ident_ds_names (ident,
111 ident_get_default_defs__callback, &ds_data);
118 for (i = 0; i < ds_data.dses_num; i++)
122 def = def_search (def_head, ident, ds_data.dses[i]);
126 def = def_create (cfg, ident, ds_data.dses[i]);
133 def_append (defs, def);
135 free (ds_data.dses[i]);
142 } /* }}} int ident_get_default_defs */
144 /* Called with each DEF in turn. Calls "def_get_rrdargs" with every appropriate
145 * file / DEF pair. */
146 static int gl_instance_get_rrdargs_cb (graph_def_t *def, void *user_data) /* {{{ */
148 def_callback_data_t *data = user_data;
149 graph_instance_t *inst = data->inst;
150 rrd_args_t *args = data->args;
155 for (i = 0; i < inst->files_num; i++)
157 if (!def_matches (def, inst->files[i]))
160 status = def_get_rrdargs (def, inst->files[i], args);
163 fprintf (stderr, "gl_instance_get_rrdargs_cb: def_get_rrdargs failed with status %i\n",
170 } /* }}} int gl_instance_get_rrdargs_cb */
172 static const char *get_part_from_param (const char *prim_key, /* {{{ */
177 val = param (prim_key);
181 return (param (sec_key));
182 } /* }}} const char *get_part_from_param */
184 static graph_ident_t *inst_get_selector_from_params (void) /* {{{ */
186 const char *host = get_part_from_param ("inst_host", "host");
187 const char *plugin = get_part_from_param ("inst_plugin", "plugin");
188 const char *plugin_instance = get_part_from_param ("inst_plugin_instance",
190 const char *type = get_part_from_param ("inst_type", "type");
191 const char *type_instance = get_part_from_param ("inst_type_instance",
194 graph_ident_t *ident;
197 || (plugin == NULL) || (plugin_instance == NULL)
198 || (type == NULL) || (type_instance == NULL))
200 fprintf (stderr, "inst_get_selected: A parameter is NULL\n");
204 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
207 fprintf (stderr, "inst_get_selected: ident_create failed\n");
212 } /* }}} graph_ident_t *inst_get_selector_from_params */
217 graph_instance_t *inst_create (graph_config_t *cfg, /* {{{ */
218 const graph_ident_t *ident)
221 graph_ident_t *selector;
223 if ((cfg == NULL) || (ident == NULL))
226 i = malloc (sizeof (*i));
229 memset (i, 0, sizeof (*i));
231 selector = graph_get_selector (cfg);
232 if (selector == NULL)
234 fprintf (stderr, "inst_create: graph_get_selector failed\n");
239 i->select = ident_copy_with_selector (selector, ident,
240 IDENT_FLAG_REPLACE_ANY);
241 if (i->select == NULL)
243 fprintf (stderr, "inst_create: ident_copy_with_selector failed\n");
244 ident_destroy (selector);
249 ident_destroy (selector);
255 } /* }}} graph_instance_t *inst_create */
257 void inst_destroy (graph_instance_t *inst) /* {{{ */
264 ident_destroy (inst->select);
266 for (i = 0; i < inst->files_num; i++)
267 ident_destroy (inst->files[i]);
271 } /* }}} void inst_destroy */
273 int inst_add_file (graph_instance_t *inst, /* {{{ */
274 const graph_ident_t *file)
278 tmp = realloc (inst->files, sizeof (*inst->files) * (inst->files_num + 1));
283 inst->files[inst->files_num] = ident_clone (file);
284 if (inst->files[inst->files_num] == NULL)
290 } /* }}} int inst_add_file */
292 graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
294 graph_ident_t *ident;
295 graph_instance_t *inst;
298 cfg = gl_graph_get_selected ();
302 DEBUG ("inst_get_selected: cfg == NULL;\n");
306 ident = inst_get_selector_from_params ();
309 fprintf (stderr, "inst_get_selected: ident_create failed\n");
313 inst = graph_inst_find_exact (cfg, ident);
315 ident_destroy (ident);
317 } /* }}} graph_instance_t *inst_get_selected */
319 int inst_get_all_selected (graph_config_t *cfg, /* {{{ */
320 graph_inst_callback_t callback, void *user_data)
322 graph_ident_t *ident;
325 if ((cfg == NULL) || (callback == NULL))
328 ident = inst_get_selector_from_params ();
331 fprintf (stderr, "inst_get_all_selected: "
332 "inst_get_selector_from_params failed\n");
336 status = graph_inst_find_all_matching (cfg, ident, callback, user_data);
338 ident_destroy (ident);
340 } /* }}} int inst_get_all_selected */
342 int inst_get_rrdargs (graph_config_t *cfg, /* {{{ */
343 graph_instance_t *inst,
346 def_callback_data_t data = { inst, args };
350 if ((cfg == NULL) || (inst == NULL) || (args == NULL))
353 status = graph_get_rrdargs (cfg, inst, args);
357 defs = graph_get_defs (cfg);
360 defs = inst_get_default_defs (cfg, inst);
365 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
371 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
375 } /* }}} int inst_get_rrdargs */
377 /* Create one or more DEFs for each file in the graph instance. The number
378 * depends on the number of data sources in each of the files. Called from
379 * "inst_get_rrdargs" if no DEFs are available from the configuration.
381 graph_def_t *inst_get_default_defs (graph_config_t *cfg, /* {{{ */
382 graph_instance_t *inst)
384 graph_def_t *defs = NULL;
387 if ((cfg == NULL) || (inst == NULL))
390 for (i = 0; i < inst->files_num; i++)
394 def = ident_get_default_defs (cfg, inst->files[i], defs);
401 def_append (defs, def);
405 } /* }}} graph_def_t *inst_get_default_defs */
407 graph_ident_t *inst_get_selector (graph_instance_t *inst) /* {{{ */
412 return (ident_clone (inst->select));
413 } /* }}} graph_ident_t *inst_get_selector */
415 int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
416 char *buffer, size_t buffer_size)
418 graph_ident_t *cfg_select;
420 if ((cfg == NULL) || (inst == NULL)
421 || (buffer == NULL) || (buffer_size < 1))
424 cfg_select = graph_get_selector (cfg);
425 if (cfg_select == NULL)
427 fprintf (stderr, "inst_get_params: graph_get_selector failed");
433 #define COPY_ESCAPE(str) do { \
435 uri_escape_copy (tmp, (str), sizeof (tmp)); \
436 strlcat (buffer, tmp, buffer_size); \
439 #define COPY_FIELD(field) do { \
440 const char *cfg_f = ident_get_##field (cfg_select); \
441 const char *inst_f = ident_get_##field (inst->select); \
442 if (strcmp (cfg_f, inst_f) == 0) \
444 strlcat (buffer, #field, buffer_size); \
445 strlcat (buffer, "=", buffer_size); \
446 COPY_ESCAPE (cfg_f); \
450 strlcat (buffer, "graph_", buffer_size); \
451 strlcat (buffer, #field, buffer_size); \
452 strlcat (buffer, "=", buffer_size); \
453 COPY_ESCAPE (cfg_f); \
454 strlcat (buffer, ";", buffer_size); \
455 strlcat (buffer, "inst_", buffer_size); \
456 strlcat (buffer, #field, buffer_size); \
457 strlcat (buffer, "=", buffer_size); \
458 COPY_ESCAPE (inst_f); \
463 strlcat (buffer, ";", buffer_size);
465 strlcat (buffer, ";", buffer_size);
466 COPY_FIELD(plugin_instance);
467 strlcat (buffer, ";", buffer_size);
469 strlcat (buffer, ";", buffer_size);
470 COPY_FIELD(type_instance);
475 ident_destroy (cfg_select);
478 } /* }}} int inst_get_params */
480 int inst_compare (const graph_instance_t *i0, /* {{{ */
481 const graph_instance_t *i1)
483 return (ident_compare (i0->select, i1->select));
484 } /* }}} int inst_compare */
486 int inst_compare_ident (graph_instance_t *inst, /* {{{ */
487 const graph_ident_t *ident)
489 if ((inst == NULL) || (ident == NULL))
492 return (ident_compare (inst->select, ident));
493 } /* }}} int inst_compare_ident */
495 _Bool inst_ident_matches (graph_instance_t *inst, /* {{{ */
496 const graph_ident_t *ident)
499 if ((inst == NULL) || (ident == NULL))
503 return (ident_matches (inst->select, ident));
504 } /* }}} _Bool inst_ident_matches */
506 _Bool inst_matches_ident (graph_instance_t *inst, /* {{{ */
507 const graph_ident_t *ident)
509 if ((inst == NULL) || (ident == NULL))
512 return (ident_matches (ident, inst->select));
513 } /* }}} _Bool inst_matches_ident */
515 _Bool inst_matches_string (graph_config_t *cfg, /* {{{ */
516 graph_instance_t *inst,
522 if ((cfg == NULL) || (inst == NULL) || (term == NULL))
525 status = inst_describe (cfg, inst, buffer, sizeof (buffer));
528 fprintf (stderr, "inst_matches_string: inst_describe failed\n");
535 if (strstr (buffer, term) == NULL)
539 } /* }}} _Bool inst_matches_string */
541 _Bool inst_matches_field (graph_instance_t *inst, /* {{{ */
542 graph_ident_field_t field, const char *field_value)
544 const char *selector_field;
547 if ((inst == NULL) || (field_value == NULL))
550 selector_field = ident_get_field (inst->select, field);
551 if (selector_field == NULL)
554 assert (!IS_ANY (selector_field));
555 if (!IS_ALL (selector_field))
557 if (strcasecmp (selector_field, field_value) == 0)
563 /* The selector field is an ALL selector
564 * => we need to check the files to see if the instance matches. */
565 for (i = 0; i < inst->files_num; i++)
567 selector_field = ident_get_field (inst->files[i], field);
568 if (selector_field == NULL)
571 assert (!IS_ANY (selector_field));
572 assert (!IS_ALL (selector_field));
574 if (strcasecmp (selector_field, field_value) == 0)
579 } /* }}} _Bool inst_matches_field */
581 int inst_to_json (const graph_instance_t *inst, /* {{{ */
586 if ((inst == NULL) || (handler == NULL))
589 /* TODO: error handling */
590 yajl_gen_map_open (handler);
591 yajl_gen_string (handler,
592 (unsigned char *) "select",
593 (unsigned int) strlen ("select"));
594 ident_to_json (inst->select, handler);
595 yajl_gen_string (handler,
596 (unsigned char *) "files",
597 (unsigned int) strlen ("files"));
598 yajl_gen_array_open (handler);
599 for (i = 0; i < inst->files_num; i++)
600 ident_to_json (inst->files[i], handler);
601 yajl_gen_array_close (handler);
602 yajl_gen_map_close (handler);
605 } /* }}} int inst_to_json */
607 int inst_data_to_json (const graph_instance_t *inst, /* {{{ */
608 dp_time_t begin, dp_time_t end,
613 yajl_gen_array_open (handler);
614 for (i = 0; i < inst->files_num; i++)
615 ident_data_to_json (inst->files[i], begin, end, handler);
616 yajl_gen_array_close (handler);
619 } /* }}} int inst_data_to_json */
621 int inst_describe (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
622 char *buffer, size_t buffer_size)
624 graph_ident_t *cfg_select;
627 if ((cfg == NULL) || (inst == NULL)
628 || (buffer == NULL) || (buffer_size < 2))
631 cfg_select = graph_get_selector (cfg);
632 if (cfg_select == NULL)
634 fprintf (stderr, "inst_describe: graph_get_selector failed\n");
638 status = ident_describe (inst->select, cfg_select,
639 buffer, buffer_size);
641 ident_destroy (cfg_select);
644 } /* }}} int inst_describe */
646 time_t inst_get_mtime (graph_instance_t *inst) /* {{{ */
655 for (i = 0; i < inst->files_num; i++)
659 tmp = ident_get_mtime (inst->files[i]);
665 } /* }}} time_t inst_get_mtime */
667 /* vim: set sw=2 sts=2 et fdm=marker : */