2 * collection4 - graph.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>
34 #include "graph_ident.h"
35 #include "graph_instance.h"
36 #include "graph_list.h"
37 #include "graph_def.h"
38 #include "graph_config.h"
40 #include "filesystem.h"
41 #include "utils_cgi.h"
44 #include <fcgi_stdio.h>
49 struct graph_config_s /* {{{ */
51 graph_ident_t *select;
59 graph_instance_t **instances;
61 }; /* }}} struct graph_config_s */
70 static graph_ident_t *graph_config_get_selector (const oconfig_item_t *ci) /* {{{ */
74 char *plugin_instance = NULL;
76 char *type_instance = NULL;
80 for (i = 0; i < ci->children_num; i++)
82 oconfig_item_t *child;
84 child = ci->children + i;
86 if (strcasecmp ("Host", child->key) == 0)
87 graph_config_get_string (child, &host);
88 else if (strcasecmp ("Plugin", child->key) == 0)
89 graph_config_get_string (child, &plugin);
90 else if (strcasecmp ("PluginInstance", child->key) == 0)
91 graph_config_get_string (child, &plugin_instance);
92 else if (strcasecmp ("Type", child->key) == 0)
93 graph_config_get_string (child, &type);
94 else if (strcasecmp ("TypeInstance", child->key) == 0)
95 graph_config_get_string (child, &type_instance);
96 /* else: ignore all other directives here. */
99 ret = ident_create (host, plugin, plugin_instance, type, type_instance);
103 free (plugin_instance);
105 free (type_instance);
108 } /* }}} int graph_config_get_selector */
113 graph_config_t *graph_create (const graph_ident_t *selector) /* {{{ */
117 cfg = malloc (sizeof (*cfg));
120 memset (cfg, 0, sizeof (*cfg));
122 if (selector != NULL)
123 cfg->select = ident_clone (selector);
128 cfg->vertical_label = NULL;
130 cfg->instances = NULL;
133 } /* }}} int graph_create */
135 void graph_destroy (graph_config_t *cfg) /* {{{ */
142 ident_destroy (cfg->select);
145 free (cfg->vertical_label);
147 def_destroy (cfg->defs);
149 for (i = 0; i < cfg->instances_num; i++)
150 inst_destroy (cfg->instances[i]);
151 free (cfg->instances);
152 } /* }}} void graph_destroy */
154 int graph_config_add (const oconfig_item_t *ci) /* {{{ */
156 graph_ident_t *select;
157 graph_config_t *cfg = NULL;
160 select = graph_config_get_selector (ci);
164 cfg = graph_create (/* selector = */ NULL);
168 cfg->select = select;
170 for (i = 0; i < ci->children_num; i++)
172 oconfig_item_t *child;
174 child = ci->children + i;
176 if (strcasecmp ("Title", child->key) == 0)
177 graph_config_get_string (child, &cfg->title);
178 else if (strcasecmp ("VerticalLabel", child->key) == 0)
179 graph_config_get_string (child, &cfg->vertical_label);
180 else if (strcasecmp ("ShowZero", child->key) == 0)
181 graph_config_get_bool (child, &cfg->show_zero);
182 else if (strcasecmp ("DEF", child->key) == 0)
183 def_config (cfg, child);
189 } /* }}} graph_config_add */
191 int graph_add_file (graph_config_t *cfg, const graph_ident_t *file) /* {{{ */
193 graph_instance_t *inst;
195 inst = graph_inst_find_matching (cfg, file);
198 graph_instance_t **tmp;
200 tmp = realloc (cfg->instances,
201 sizeof (*cfg->instances) * (cfg->instances_num + 1));
204 cfg->instances = tmp;
206 inst = inst_create (cfg, file);
210 cfg->instances[cfg->instances_num] = inst;
211 cfg->instances_num++;
214 return (inst_add_file (inst, file));
215 } /* }}} int graph_add_file */
217 int graph_get_title (graph_config_t *cfg, /* {{{ */
218 char *buffer, size_t buffer_size)
220 if ((cfg == NULL) || (buffer == NULL) || (buffer_size < 1))
223 if (cfg->title == NULL)
224 cfg->title = ident_to_string (cfg->select);
226 if (cfg->title == NULL)
229 strncpy (buffer, cfg->title, buffer_size);
230 buffer[buffer_size - 1] = 0;
233 } /* }}} int graph_get_title */
235 int graph_get_params (graph_config_t *cfg, /* {{{ */
236 char *buffer, size_t buffer_size)
240 #define COPY_FIELD(field) do { \
241 const char *str = ident_get_##field (cfg->select); \
242 char uri_str[1024]; \
243 uri_escape_copy (uri_str, str, sizeof (uri_str)); \
244 strlcat (buffer, #field, buffer_size); \
245 strlcat (buffer, "=", buffer_size); \
246 strlcat (buffer, uri_str, buffer_size); \
250 strlcat (buffer, ";", buffer_size);
252 strlcat (buffer, ";", buffer_size);
253 COPY_FIELD(plugin_instance);
254 strlcat (buffer, ";", buffer_size);
256 strlcat (buffer, ";", buffer_size);
257 COPY_FIELD(type_instance);
262 } /* }}} int graph_get_params */
264 graph_ident_t *graph_get_selector (graph_config_t *cfg) /* {{{ */
269 return (ident_clone (cfg->select));
270 } /* }}} graph_ident_t *graph_get_selector */
272 graph_def_t *graph_get_defs (graph_config_t *cfg) /* {{{ */
278 } /* }}} graph_def_t *graph_get_defs */
280 int graph_add_def (graph_config_t *cfg, graph_def_t *def) /* {{{ */
284 if ((cfg == NULL) || (def == NULL))
287 if (cfg->defs == NULL)
293 /* Insert in reverse order. This makes the order in the config file and the
294 * order of the DEFs in the graph more natural. Really. */
297 return (def_append (cfg->defs, tmp));
298 } /* }}} int graph_add_def */
300 _Bool graph_ident_matches (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
303 if ((cfg == NULL) || (ident == NULL))
307 return (ident_matches (cfg->select, ident));
308 } /* }}} _Bool graph_ident_matches */
310 _Bool graph_matches_ident (graph_config_t *cfg, /* {{{ */
311 const graph_ident_t *selector)
314 if ((cfg == NULL) || (selector == NULL))
318 return (ident_matches (selector, cfg->select));
319 } /* }}} _Bool graph_matches_ident */
321 _Bool graph_ident_intersect (graph_config_t *cfg, /* {{{ */
322 const graph_ident_t *selector)
325 if ((cfg == NULL) || (selector == NULL))
329 return (ident_intersect (cfg->select, selector));
330 } /* }}} _Bool graph_ident_intersect */
332 _Bool graph_matches_field (graph_config_t *cfg, /* {{{ */
333 graph_ident_field_t field, const char *field_value)
335 const char *selector_value;
337 if ((cfg == NULL) || (field_value == NULL))
340 selector_value = ident_get_field (cfg->select, field);
341 if (selector_value == NULL)
344 if (IS_ALL (selector_value) || IS_ANY (selector_value))
346 else if (strcasecmp (selector_value, field_value) == 0)
350 } /* }}} _Bool graph_matches_field */
352 int graph_inst_foreach (graph_config_t *cfg, /* {{{ */
353 inst_callback_t cb, void *user_data)
358 for (i = 0; i < cfg->instances_num; i++)
360 status = (*cb) (cfg->instances[i], user_data);
366 } /* }}} int graph_inst_foreach */
368 graph_instance_t *graph_inst_find_exact (graph_config_t *cfg, /* {{{ */
369 graph_ident_t *ident)
373 if ((cfg == NULL) || (ident == NULL))
376 for (i = 0; i < cfg->instances_num; i++)
377 if (inst_compare_ident (cfg->instances[i], ident) == 0)
378 return (cfg->instances[i]);
381 } /* }}} graph_instance_t *graph_inst_find_exact */
383 graph_instance_t *graph_inst_find_matching (graph_config_t *cfg, /* {{{ */
384 const graph_ident_t *ident)
388 if ((cfg == NULL) || (ident == NULL))
391 for (i = 0; i < cfg->instances_num; i++)
392 if (inst_ident_matches (cfg->instances[i], ident))
393 return (cfg->instances[i]);
396 } /* }}} graph_instance_t *graph_inst_find_matching */
398 int graph_inst_find_all_matching (graph_config_t *cfg, /* {{{ */
399 const graph_ident_t *ident,
400 graph_inst_callback_t callback, void *user_data)
404 if ((cfg == NULL) || (ident == NULL) || (callback == NULL))
407 for (i = 0; i < cfg->instances_num; i++)
411 if (!inst_matches_ident (cfg->instances[i], ident))
414 status = (*callback) (cfg, cfg->instances[i], user_data);
420 } /* }}} int graph_inst_find_all_matching */
422 /* When this function is called from graph_list, it will already have checked
423 * that the selector of the graph matches the field selections contained in
424 * the search_info_t. So if the graphs title matches, this means that the
425 * field selections and the search term(s) apply to the graph in general; thus
426 * we return all instances. Otherwise, use the somewhat expensive
427 * "search_graph_inst_matches" function to look for matching instances. */
428 int graph_search_inst (graph_config_t *cfg, search_info_t *si, /* {{{ */
429 graph_inst_callback_t cb,
436 if ((cfg == NULL) || (si == NULL) || (cb == NULL))
439 status = graph_get_title (cfg, title, sizeof (title));
442 fprintf (stderr, "graph_search_inst: graph_get_title failed\n");
447 if (search_graph_title_matches (si, title))
449 /* The title of the graph matches, so return all instances. */
450 for (i = 0; i < cfg->instances_num; i++)
452 status = (*cb) (cfg, cfg->instances[i], user_data);
459 /* The title doesn't match, so use the more expensive
460 * "search_graph_inst_matches" to look for matching instances. Since part
461 * of the terms may match the title and other terms may match the
462 * instance, the title must be passed along to that function again. */
463 for (i = 0; i < cfg->instances_num; i++)
465 if (search_graph_inst_matches (si, cfg, cfg->instances[i], title))
467 status = (*cb) (cfg, cfg->instances[i], user_data);
475 } /* }}} int graph_search_inst */
477 int graph_search_inst_string (graph_config_t *cfg, const char *term, /* {{{ */
478 graph_inst_callback_t cb,
485 status = graph_get_title (cfg, buffer, sizeof (buffer));
488 fprintf (stderr, "graph_search_inst_string: graph_get_title failed\n");
494 if (strstr (buffer, term) != NULL)
496 for (i = 0; i < cfg->instances_num; i++)
498 status = (*cb) (cfg, cfg->instances[i], user_data);
505 for (i = 0; i < cfg->instances_num; i++)
507 if (inst_matches_string (cfg, cfg->instances[i], term))
509 status = (*cb) (cfg, cfg->instances[i], user_data);
517 } /* }}} int graph_search_inst_string */
519 int graph_inst_search_field (graph_config_t *cfg, /* {{{ */
520 graph_ident_field_t field, const char *field_value,
521 graph_inst_callback_t callback, void *user_data)
524 const char *selector_field;
525 _Bool need_check_instances = 0;
527 if ((cfg == NULL) || (field_value == NULL) || (callback == NULL))
530 if (!graph_matches_field (cfg, field, field_value))
533 selector_field = ident_get_field (cfg->select, field);
534 if (selector_field == NULL)
537 if (IS_ALL (selector_field) || IS_ANY (selector_field))
538 need_check_instances = 1;
540 for (i = 0; i < cfg->instances_num; i++)
544 if (need_check_instances
545 && !inst_matches_field (cfg->instances[i], field, field_value))
548 status = (*callback) (cfg, cfg->instances[i], user_data);
554 } /* }}} int graph_inst_search_field */
556 int graph_compare (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
558 if ((cfg == NULL) || (ident == NULL))
561 return (ident_compare (cfg->select, ident));
562 } /* }}} int graph_compare */
564 static int graph_sort_instances_cb (const void *v0, const void *v1) /* {{{ */
566 return (inst_compare (*(graph_instance_t * const *) v0,
567 *(graph_instance_t * const *) v1));
568 } /* }}} int graph_sort_instances_cb */
570 size_t graph_num_instances (graph_config_t *cfg) /* {{{ */
573 return ((size_t) -1);
575 return (cfg->instances_num);
576 } /* }}} size_t graph_num_instances */
578 int graph_sort_instances (graph_config_t *cfg) /* {{{ */
583 if (cfg->instances_num < 2)
586 qsort (cfg->instances, cfg->instances_num, sizeof (*cfg->instances),
587 graph_sort_instances_cb);
590 } /* }}} int graph_sort_instances */
592 int graph_clear_instances (graph_config_t *cfg) /* {{{ */
599 for (i = 0; i < cfg->instances_num; i++)
600 inst_destroy (cfg->instances[i]);
601 free (cfg->instances);
602 cfg->instances = NULL;
603 cfg->instances_num = 0;
606 } /* }}} int graph_clear_instances */
608 int graph_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
611 if ((cfg == NULL) || (inst == NULL) || (args == NULL))
614 if (cfg->title != NULL)
616 array_append (args->options, "-t");
617 array_append (args->options, cfg->title);
620 if (cfg->vertical_label != NULL)
622 array_append (args->options, "-v");
623 array_append (args->options, cfg->vertical_label);
628 array_append (args->options, "-l");
629 array_append (args->options, "0");
633 } /* }}} int graph_get_rrdargs */
635 /* vim: set sw=2 sts=2 et fdm=marker : */