2 * collection4 - graph_list.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>
32 #include "graph_list.h"
34 #include "filesystem.h"
36 #include "graph_config.h"
37 #include "graph_def.h"
38 #include "graph_ident.h"
39 #include "utils_cgi.h"
42 #include <fcgi_stdio.h>
47 #define UPDATE_INTERVAL 900
52 static graph_config_t **gl_active = NULL;
53 static size_t gl_active_num = 0;
55 static graph_config_t **gl_staging = NULL;
56 static size_t gl_staging_num = 0;
58 /* Graphs created on-the-fly for files which don't match any existing graph
60 static graph_config_t **gl_dynamic = NULL;
61 static size_t gl_dynamic_num = 0;
63 static char **host_list = NULL;
64 static size_t host_list_len = 0;
66 static time_t gl_last_update = 0;
71 static int gl_add_graph_internal (graph_config_t *cfg, /* {{{ */
72 graph_config_t ***gl_array, size_t *gl_array_num)
76 #define ARRAY_PTR (*gl_array)
77 #define ARRAY_SIZE (*gl_array_num)
82 tmp = realloc (ARRAY_PTR, sizeof (*ARRAY_PTR) * (ARRAY_SIZE + 1));
87 ARRAY_PTR[ARRAY_SIZE] = cfg;
94 } /* }}} int gl_add_graph_internal */
96 static void gl_destroy (graph_config_t ***gl_array, /* {{{ */
101 if ((gl_array == NULL) || (gl_array_num == NULL))
104 #define ARRAY_PTR (*gl_array)
105 #define ARRAY_SIZE (*gl_array_num)
107 for (i = 0; i < ARRAY_SIZE; i++)
109 graph_destroy (ARRAY_PTR[i]);
118 } /* }}} void gl_destroy */
120 static int gl_register_host (const char *host) /* {{{ */
128 for (i = 0; i < host_list_len; i++)
129 if (strcmp (host_list[i], host) == 0)
132 tmp = realloc (host_list, sizeof (*host_list) * (host_list_len + 1));
137 host_list[host_list_len] = strdup (host);
138 if (host_list[host_list_len] == NULL)
143 } /* }}} int gl_register_host */
145 static int gl_clear_hosts (void) /* {{{ */
149 for (i = 0; i < host_list_len; i++)
157 } /* }}} int gl_clear_hosts */
159 static int gl_compare_hosts (const void *v0, const void *v1) /* {{{ */
161 return (strcmp (*(char * const *) v0, *(char * const *) v1));
162 } /* }}} int gl_compare_hosts */
164 static int gl_register_file (const graph_ident_t *file, /* {{{ */
165 __attribute__((unused)) void *user_data)
171 for (i = 0; i < gl_active_num; i++)
173 graph_config_t *cfg = gl_active[i];
176 if (!graph_matches_ident (cfg, file))
179 status = graph_add_file (cfg, file);
192 cfg = graph_create (file);
193 gl_add_graph_internal (cfg, &gl_dynamic, &gl_dynamic_num);
194 graph_add_file (cfg, file);
197 gl_register_host (ident_get_host (file));
200 } /* }}} int gl_register_file */
202 static const char *get_part_from_param (const char *prim_key, /* {{{ */
207 val = param (prim_key);
211 return (param (sec_key));
212 } /* }}} const char *get_part_from_param */
214 static int gl_clear_instances (void) /* {{{ */
218 for (i = 0; i < gl_active_num; i++)
219 graph_clear_instances (gl_active[i]);
222 } /* }}} int gl_clear_instances */
227 int gl_add_graph (graph_config_t *cfg) /* {{{ */
229 return (gl_add_graph_internal (cfg, &gl_staging, &gl_staging_num));
230 } /* }}} int gl_add_graph */
232 int gl_config_submit (void) /* {{{ */
234 graph_config_t **old;
238 old_num = gl_active_num;
240 gl_active = gl_staging;
241 gl_active_num = gl_staging_num;
246 gl_destroy (&old, &old_num);
249 } /* }}} int graph_config_submit */
251 int gl_graph_get_all (graph_callback_t callback, /* {{{ */
256 if (callback == NULL)
261 for (i = 0; i < gl_active_num; i++)
265 status = (*callback) (gl_active[i], user_data);
270 for (i = 0; i < gl_dynamic_num; i++)
274 status = (*callback) (gl_dynamic[i], user_data);
280 } /* }}} int gl_graph_get_all */
282 graph_config_t *gl_graph_get_selected (void) /* {{{ */
284 const char *host = get_part_from_param ("graph_host", "host");
285 const char *plugin = get_part_from_param ("graph_plugin", "plugin");
286 const char *plugin_instance = get_part_from_param ("graph_plugin_instance", "plugin_instance");
287 const char *type = get_part_from_param ("graph_type", "type");
288 const char *type_instance = get_part_from_param ("graph_type_instance", "type_instance");
289 graph_ident_t *ident;
293 || (plugin == NULL) || (plugin_instance == NULL)
294 || (type == NULL) || (type_instance == NULL))
297 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
301 for (i = 0; i < gl_active_num; i++)
303 if (graph_compare (gl_active[i], ident) != 0)
306 ident_destroy (ident);
307 return (gl_active[i]);
310 for (i = 0; i < gl_dynamic_num; i++)
312 if (graph_compare (gl_dynamic[i], ident) != 0)
315 ident_destroy (ident);
316 return (gl_dynamic[i]);
319 ident_destroy (ident);
321 } /* }}} graph_config_t *gl_graph_get_selected */
323 /* gl_instance_get_all, gl_graph_instance_get_all {{{ */
324 struct gl_inst_callback_data /* {{{ */
327 graph_inst_callback_t callback;
329 }; /* }}} struct gl_inst_callback_data */
331 static int gl_inst_callback_handler (graph_instance_t *inst, /* {{{ */
334 struct gl_inst_callback_data *data = user_data;
336 return ((*data->callback) (data->cfg, inst, data->user_data));
337 } /* }}} int gl_inst_callback_handler */
339 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
340 graph_inst_callback_t callback, void *user_data)
342 struct gl_inst_callback_data data =
349 if ((cfg == NULL) || (callback == NULL))
352 return (graph_inst_foreach (cfg, gl_inst_callback_handler, &data));
353 } /* }}} int gl_graph_instance_get_all */
355 int gl_instance_get_all (graph_inst_callback_t callback, /* {{{ */
362 for (i = 0; i < gl_active_num; i++)
366 status = gl_graph_instance_get_all (gl_active[i], callback, user_data);
371 for (i = 0; i < gl_dynamic_num; i++)
375 status = gl_graph_instance_get_all (gl_dynamic[i], callback, user_data);
381 } /* }}} int gl_instance_get_all */
382 /* }}} gl_instance_get_all, gl_graph_instance_get_all */
384 int gl_search (const char *term, graph_inst_callback_t callback, /* {{{ */
389 for (i = 0; i < gl_active_num; i++)
393 status = graph_inst_search (gl_active[i], term,
394 /* callback = */ callback,
395 /* user data = */ user_data);
400 for (i = 0; i < gl_dynamic_num; i++)
404 status = graph_inst_search (gl_dynamic[i], term,
405 /* callback = */ callback,
406 /* user data = */ user_data);
412 } /* }}} int gl_search */
414 int gl_search_field (graph_ident_field_t field, /* {{{ */
415 const char *field_value,
416 graph_inst_callback_t callback, void *user_data)
420 if ((field_value == NULL) || (callback == NULL))
423 for (i = 0; i < gl_active_num; i++)
427 status = graph_inst_search_field (gl_active[i],
429 /* callback = */ callback,
430 /* user data = */ user_data);
435 for (i = 0; i < gl_dynamic_num; i++)
439 status = graph_inst_search_field (gl_dynamic[i],
441 /* callback = */ callback,
442 /* user data = */ user_data);
448 } /* }}} int gl_search_field */
450 int gl_foreach_host (int (*callback) (const char *host, void *user_data), /* {{{ */
456 for (i = 0; i < host_list_len; i++)
458 status = (*callback) (host_list[i], user_data);
464 } /* }}} int gl_foreach_host */
466 int gl_update (void) /* {{{ */
472 printf ("Content-Type: text/plain\n\n");
477 if ((gl_last_update + UPDATE_INTERVAL) >= now)
481 gl_clear_instances ();
483 gl_destroy (&gl_dynamic, &gl_dynamic_num);
485 graph_read_config ();
487 status = fs_scan (/* callback = */ gl_register_file, /* user data = */ NULL);
489 if (host_list_len > 0)
490 qsort (host_list, host_list_len, sizeof (*host_list),
493 gl_last_update = now;
496 } /* }}} int gl_update */
498 /* vim: set sw=2 sts=2 et fdm=marker : */