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 /* Create one DEF for each data source in the file. Called by
60 * "inst_get_default_defs" for each file. */
61 static graph_def_t *ident_get_default_defs (graph_config_t *cfg, /* {{{ */
62 graph_ident_t *ident, graph_def_t *def_head)
64 graph_def_t *defs = NULL;
71 if ((cfg == NULL) || (ident == NULL))
74 file = ident_to_file (ident);
77 fprintf (stderr, "ident_get_default_defs: ident_to_file failed\n");
81 status = ds_list_from_rrd_file (file, &dses_num, &dses);
88 for (i = 0; i < dses_num; i++)
92 def = def_search (def_head, ident, dses[i]);
96 def = def_create (cfg, ident, dses[i]);
103 def_append (defs, def);
112 } /* }}} int ident_get_default_defs */
114 /* Create one or more DEFs for each file in the graph instance. The number
115 * depends on the number of data sources in each of the files. Called from
116 * "inst_get_rrdargs" if no DEFs are available from the configuration.
118 static graph_def_t *inst_get_default_defs (graph_config_t *cfg, /* {{{ */
119 graph_instance_t *inst)
121 graph_def_t *defs = NULL;
124 if ((cfg == NULL) || (inst == NULL))
127 for (i = 0; i < inst->files_num; i++)
131 def = ident_get_default_defs (cfg, inst->files[i], defs);
138 def_append (defs, def);
142 } /* }}} graph_def_t *inst_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;
154 for (i = 0; i < inst->files_num; i++)
156 if (!def_matches (def, inst->files[i]))
159 def_get_rrdargs (def, inst->files[i], args);
163 } /* }}} int gl_instance_get_rrdargs_cb */
165 static const char *get_part_from_param (const char *prim_key, /* {{{ */
170 val = param (prim_key);
174 return (param (sec_key));
175 } /* }}} const char *get_part_from_param */
177 static graph_ident_t *inst_get_selector_from_params (void) /* {{{ */
179 const char *host = get_part_from_param ("inst_host", "host");
180 const char *plugin = get_part_from_param ("inst_plugin", "plugin");
181 const char *plugin_instance = get_part_from_param ("inst_plugin_instance",
183 const char *type = get_part_from_param ("inst_type", "type");
184 const char *type_instance = get_part_from_param ("inst_type_instance",
187 graph_ident_t *ident;
190 || (plugin == NULL) || (plugin_instance == NULL)
191 || (type == NULL) || (type_instance == NULL))
193 fprintf (stderr, "inst_get_selected: A parameter is NULL\n");
197 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
200 fprintf (stderr, "inst_get_selected: ident_create failed\n");
205 } /* }}} graph_ident_t *inst_get_selector_from_params */
210 graph_instance_t *inst_create (graph_config_t *cfg, /* {{{ */
211 const graph_ident_t *ident)
214 graph_ident_t *selector;
216 if ((cfg == NULL) || (ident == NULL))
219 i = malloc (sizeof (*i));
222 memset (i, 0, sizeof (*i));
224 selector = graph_get_selector (cfg);
225 if (selector == NULL)
227 fprintf (stderr, "inst_create: graph_get_selector failed\n");
232 i->select = ident_copy_with_selector (selector, ident,
233 IDENT_FLAG_REPLACE_ANY);
234 if (i->select == NULL)
236 fprintf (stderr, "inst_create: ident_copy_with_selector failed\n");
237 ident_destroy (selector);
242 ident_destroy (selector);
248 } /* }}} graph_instance_t *inst_create */
250 void inst_destroy (graph_instance_t *inst) /* {{{ */
257 ident_destroy (inst->select);
259 for (i = 0; i < inst->files_num; i++)
260 ident_destroy (inst->files[i]);
264 } /* }}} void inst_destroy */
266 int inst_add_file (graph_instance_t *inst, /* {{{ */
267 const graph_ident_t *file)
271 tmp = realloc (inst->files, sizeof (*inst->files) * (inst->files_num + 1));
276 inst->files[inst->files_num] = ident_clone (file);
277 if (inst->files[inst->files_num] == NULL)
283 } /* }}} int inst_add_file */
285 graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
287 graph_ident_t *ident;
288 graph_instance_t *inst;
291 cfg = gl_graph_get_selected ();
295 DEBUG ("inst_get_selected: cfg == NULL;\n");
299 ident = inst_get_selector_from_params ();
302 fprintf (stderr, "inst_get_selected: ident_create failed\n");
306 inst = graph_inst_find_exact (cfg, ident);
308 ident_destroy (ident);
310 } /* }}} graph_instance_t *inst_get_selected */
312 int inst_get_all_selected (graph_config_t *cfg, /* {{{ */
313 graph_inst_callback_t callback, void *user_data)
315 graph_ident_t *ident;
318 if ((cfg == NULL) || (callback == NULL))
321 ident = inst_get_selector_from_params ();
324 fprintf (stderr, "inst_get_all_selected: "
325 "inst_get_selector_from_params failed\n");
329 status = graph_inst_find_all_matching (cfg, ident, callback, user_data);
331 ident_destroy (ident);
333 } /* }}} int inst_get_all_selected */
335 int inst_get_rrdargs (graph_config_t *cfg, /* {{{ */
336 graph_instance_t *inst,
339 def_callback_data_t data = { inst, args };
343 if ((cfg == NULL) || (inst == NULL) || (args == NULL))
346 status = graph_get_rrdargs (cfg, inst, args);
350 defs = graph_get_defs (cfg);
353 defs = inst_get_default_defs (cfg, inst);
358 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
364 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
368 } /* }}} int inst_get_rrdargs */
370 graph_ident_t *inst_get_selector (graph_instance_t *inst) /* {{{ */
375 return (ident_clone (inst->select));
376 } /* }}} graph_ident_t *inst_get_selector */
378 int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
379 char *buffer, size_t buffer_size)
381 graph_ident_t *cfg_select;
383 if ((cfg == NULL) || (inst == NULL)
384 || (buffer == NULL) || (buffer_size < 1))
387 cfg_select = graph_get_selector (cfg);
388 if (cfg_select == NULL)
390 fprintf (stderr, "inst_get_params: graph_get_selector failed");
396 #define COPY_FIELD(field) do { \
397 const char *cfg_f = ident_get_##field (cfg_select); \
398 const char *inst_f = ident_get_##field (inst->select); \
399 if (strcmp (cfg_f, inst_f) == 0) \
401 strlcat (buffer, #field, buffer_size); \
402 strlcat (buffer, "=", buffer_size); \
403 strlcat (buffer, cfg_f, buffer_size); \
407 strlcat (buffer, "graph_", buffer_size); \
408 strlcat (buffer, #field, buffer_size); \
409 strlcat (buffer, "=", buffer_size); \
410 strlcat (buffer, cfg_f, buffer_size); \
411 strlcat (buffer, ";", buffer_size); \
412 strlcat (buffer, "inst_", buffer_size); \
413 strlcat (buffer, #field, buffer_size); \
414 strlcat (buffer, "=", buffer_size); \
415 strlcat (buffer, inst_f, buffer_size); \
420 strlcat (buffer, ";", buffer_size);
422 strlcat (buffer, ";", buffer_size);
423 COPY_FIELD(plugin_instance);
424 strlcat (buffer, ";", buffer_size);
426 strlcat (buffer, ";", buffer_size);
427 COPY_FIELD(type_instance);
431 ident_destroy (cfg_select);
434 } /* }}} int inst_get_params */
436 int inst_compare (const graph_instance_t *i0, /* {{{ */
437 const graph_instance_t *i1)
439 return (ident_compare (i0->select, i1->select));
440 } /* }}} int inst_compare */
442 int inst_compare_ident (graph_instance_t *inst, /* {{{ */
443 const graph_ident_t *ident)
445 if ((inst == NULL) || (ident == NULL))
448 return (ident_compare (inst->select, ident));
449 } /* }}} int inst_compare_ident */
451 _Bool inst_ident_matches (graph_instance_t *inst, /* {{{ */
452 const graph_ident_t *ident)
455 if ((inst == NULL) || (ident == NULL))
459 return (ident_matches (inst->select, ident));
460 } /* }}} _Bool inst_ident_matches */
462 _Bool inst_matches_ident (graph_instance_t *inst, /* {{{ */
463 const graph_ident_t *ident)
465 if ((inst == NULL) || (ident == NULL))
468 return (ident_matches (ident, inst->select));
469 } /* }}} _Bool inst_matches_ident */
471 _Bool inst_matches_string (graph_config_t *cfg, /* {{{ */
472 graph_instance_t *inst,
478 if ((cfg == NULL) || (inst == NULL) || (term == NULL))
481 status = inst_describe (cfg, inst, buffer, sizeof (buffer));
484 fprintf (stderr, "inst_matches_string: inst_describe failed\n");
491 if (strstr (buffer, term) == NULL)
495 } /* }}} _Bool inst_matches_string */
497 _Bool inst_matches_field (graph_instance_t *inst, /* {{{ */
498 graph_ident_field_t field, const char *field_value)
500 const char *selector_field;
503 if ((inst == NULL) || (field_value == NULL))
506 selector_field = ident_get_field (inst->select, field);
507 if (selector_field == NULL)
510 assert (!IS_ANY (selector_field));
511 if (!IS_ALL (selector_field))
513 if (strcasecmp (selector_field, field_value) == 0)
519 /* The selector field is an ALL selector
520 * => we need to check the files to see if the instance matches. */
521 for (i = 0; i < inst->files_num; i++)
523 selector_field = ident_get_field (inst->files[i], field);
524 if (selector_field == NULL)
527 assert (!IS_ANY (selector_field));
528 assert (!IS_ALL (selector_field));
530 if (strcasecmp (selector_field, field_value) == 0)
535 } /* }}} _Bool inst_matches_field */
537 int inst_describe (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
538 char *buffer, size_t buffer_size)
540 graph_ident_t *cfg_select;
542 if ((cfg == NULL) || (inst == NULL)
543 || (buffer == NULL) || (buffer_size < 2))
546 cfg_select = graph_get_selector (cfg);
547 if (cfg_select == NULL)
549 fprintf (stderr, "inst_describe: graph_get_selector failed\n");
555 #define CHECK_FIELD(field) do { \
556 if (IS_ANY (ident_get_##field (cfg_select))) \
558 if (buffer[0] != 0) \
559 strlcat (buffer, "/", buffer_size); \
560 strlcat (buffer, ident_get_##field (inst->select), buffer_size); \
565 CHECK_FIELD (plugin);
566 CHECK_FIELD (plugin_instance);
568 CHECK_FIELD (type_instance);
573 strlcat (buffer, "default", buffer_size);
575 ident_destroy (cfg_select);
578 } /* }}} int inst_describe */
580 time_t inst_get_mtime (graph_instance_t *inst) /* {{{ */
589 for (i = 0; i < inst->files_num; i++)
593 tmp = ident_get_mtime (inst->files[i]);
599 } /* }}} time_t inst_get_mtime */
601 /* vim: set sw=2 sts=2 et fdm=marker : */