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"
40 #include "utils_search.h"
43 #include <fcgi_stdio.h>
48 #define UPDATE_INTERVAL 900
53 static graph_config_t **gl_active = NULL;
54 static size_t gl_active_num = 0;
56 static graph_config_t **gl_staging = NULL;
57 static size_t gl_staging_num = 0;
59 /* Graphs created on-the-fly for files which don't match any existing graph
61 static graph_config_t **gl_dynamic = NULL;
62 static size_t gl_dynamic_num = 0;
64 static char **host_list = NULL;
65 static size_t host_list_len = 0;
67 static time_t gl_last_update = 0;
72 static int gl_add_graph_internal (graph_config_t *cfg, /* {{{ */
73 graph_config_t ***gl_array, size_t *gl_array_num)
77 #define ARRAY_PTR (*gl_array)
78 #define ARRAY_SIZE (*gl_array_num)
83 tmp = realloc (ARRAY_PTR, sizeof (*ARRAY_PTR) * (ARRAY_SIZE + 1));
88 ARRAY_PTR[ARRAY_SIZE] = cfg;
95 } /* }}} int gl_add_graph_internal */
97 static void gl_destroy (graph_config_t ***gl_array, /* {{{ */
102 if ((gl_array == NULL) || (gl_array_num == NULL))
105 #define ARRAY_PTR (*gl_array)
106 #define ARRAY_SIZE (*gl_array_num)
108 for (i = 0; i < ARRAY_SIZE; i++)
110 graph_destroy (ARRAY_PTR[i]);
119 } /* }}} void gl_destroy */
121 static int gl_register_host (const char *host) /* {{{ */
129 for (i = 0; i < host_list_len; i++)
130 if (strcmp (host_list[i], host) == 0)
133 tmp = realloc (host_list, sizeof (*host_list) * (host_list_len + 1));
138 host_list[host_list_len] = strdup (host);
139 if (host_list[host_list_len] == NULL)
144 } /* }}} int gl_register_host */
146 static int gl_clear_hosts (void) /* {{{ */
150 for (i = 0; i < host_list_len; i++)
158 } /* }}} int gl_clear_hosts */
160 static int gl_compare_hosts (const void *v0, const void *v1) /* {{{ */
162 return (strcmp (*(char * const *) v0, *(char * const *) v1));
163 } /* }}} int gl_compare_hosts */
165 static int gl_register_file (const graph_ident_t *file, /* {{{ */
166 __attribute__((unused)) void *user_data)
172 for (i = 0; i < gl_active_num; i++)
174 graph_config_t *cfg = gl_active[i];
177 if (!graph_ident_matches (cfg, file))
180 status = graph_add_file (cfg, file);
193 cfg = graph_create (file);
194 gl_add_graph_internal (cfg, &gl_dynamic, &gl_dynamic_num);
195 graph_add_file (cfg, file);
198 gl_register_host (ident_get_host (file));
201 } /* }}} int gl_register_file */
203 static const char *get_part_from_param (const char *prim_key, /* {{{ */
208 val = param (prim_key);
212 return (param (sec_key));
213 } /* }}} const char *get_part_from_param */
215 static int gl_clear_instances (void) /* {{{ */
219 for (i = 0; i < gl_active_num; i++)
220 graph_clear_instances (gl_active[i]);
223 } /* }}} int gl_clear_instances */
225 static void gl_dump_cb (void *ctx, /* {{{ */
226 const char *str, unsigned int len)
230 /* FIXME: Has everything been written? */
231 fwrite ((void *) str, /* size = */ 1, /* nmemb = */ len, fh);
232 } /* }}} void gl_dump_cb */
234 static int gl_dump (void) /* {{{ */
238 yajl_gen_config handler_config = { /* pretty = */ 1, /* indent = */ " " };
241 /* FIXME: Lock the file */
242 fh = fopen ("/tmp/collection4.json", "w");
246 handler = yajl_gen_alloc2 (gl_dump_cb, &handler_config,
247 /* alloc funcs = */ NULL, /* ctx = */ fh);
254 yajl_gen_array_open (handler);
256 for (i = 0; i < gl_active_num; i++)
257 graph_to_json (gl_active[i], handler);
259 for (i = 0; i < gl_dynamic_num; i++)
260 graph_to_json (gl_dynamic[i], handler);
262 yajl_gen_array_close (handler);
264 yajl_gen_free (handler);
268 } /* }}} int gl_dump */
273 int gl_add_graph (graph_config_t *cfg) /* {{{ */
275 return (gl_add_graph_internal (cfg, &gl_staging, &gl_staging_num));
276 } /* }}} int gl_add_graph */
278 int gl_config_submit (void) /* {{{ */
280 graph_config_t **old;
284 old_num = gl_active_num;
286 gl_active = gl_staging;
287 gl_active_num = gl_staging_num;
292 gl_destroy (&old, &old_num);
295 } /* }}} int graph_config_submit */
297 int gl_graph_get_all (graph_callback_t callback, /* {{{ */
302 if (callback == NULL)
307 for (i = 0; i < gl_active_num; i++)
311 status = (*callback) (gl_active[i], user_data);
316 for (i = 0; i < gl_dynamic_num; i++)
320 status = (*callback) (gl_dynamic[i], user_data);
326 } /* }}} int gl_graph_get_all */
328 graph_config_t *gl_graph_get_selected (void) /* {{{ */
330 const char *host = get_part_from_param ("graph_host", "host");
331 const char *plugin = get_part_from_param ("graph_plugin", "plugin");
332 const char *plugin_instance = get_part_from_param ("graph_plugin_instance", "plugin_instance");
333 const char *type = get_part_from_param ("graph_type", "type");
334 const char *type_instance = get_part_from_param ("graph_type_instance", "type_instance");
335 graph_ident_t *ident;
339 || (plugin == NULL) || (plugin_instance == NULL)
340 || (type == NULL) || (type_instance == NULL))
343 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
347 for (i = 0; i < gl_active_num; i++)
349 if (graph_compare (gl_active[i], ident) != 0)
352 ident_destroy (ident);
353 return (gl_active[i]);
356 for (i = 0; i < gl_dynamic_num; i++)
358 if (graph_compare (gl_dynamic[i], ident) != 0)
361 ident_destroy (ident);
362 return (gl_dynamic[i]);
365 ident_destroy (ident);
367 } /* }}} graph_config_t *gl_graph_get_selected */
369 /* gl_instance_get_all, gl_graph_instance_get_all {{{ */
370 struct gl_inst_callback_data /* {{{ */
373 graph_inst_callback_t callback;
375 }; /* }}} struct gl_inst_callback_data */
377 static int gl_inst_callback_handler (graph_instance_t *inst, /* {{{ */
380 struct gl_inst_callback_data *data = user_data;
382 return ((*data->callback) (data->cfg, inst, data->user_data));
383 } /* }}} int gl_inst_callback_handler */
385 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
386 graph_inst_callback_t callback, void *user_data)
388 struct gl_inst_callback_data data =
395 if ((cfg == NULL) || (callback == NULL))
398 return (graph_inst_foreach (cfg, gl_inst_callback_handler, &data));
399 } /* }}} int gl_graph_instance_get_all */
401 int gl_instance_get_all (graph_inst_callback_t callback, /* {{{ */
408 for (i = 0; i < gl_active_num; i++)
412 status = gl_graph_instance_get_all (gl_active[i], callback, user_data);
417 for (i = 0; i < gl_dynamic_num; i++)
421 status = gl_graph_instance_get_all (gl_dynamic[i], callback, user_data);
427 } /* }}} int gl_instance_get_all */
428 /* }}} gl_instance_get_all, gl_graph_instance_get_all */
430 int gl_search (search_info_t *si, /* {{{ */
431 graph_inst_callback_t callback, void *user_data)
434 graph_ident_t *ident;
436 if ((si == NULL) || (callback == NULL))
439 if (search_has_selector (si))
441 ident = search_to_ident (si);
444 fprintf (stderr, "gl_search: search_to_ident failed\n");
453 for (i = 0; i < gl_active_num; i++)
457 if ((ident != NULL) && !graph_ident_intersect (gl_active[i], ident))
460 status = graph_search_inst (gl_active[i], si,
461 /* callback = */ callback,
462 /* user data = */ user_data);
467 for (i = 0; i < gl_dynamic_num; i++)
471 if ((ident != NULL) && !graph_ident_intersect (gl_dynamic[i], ident))
474 status = graph_search_inst (gl_dynamic[i], si,
475 /* callback = */ callback,
476 /* user data = */ user_data);
482 } /* }}} int gl_search */
484 int gl_search_string (const char *term, graph_inst_callback_t callback, /* {{{ */
489 for (i = 0; i < gl_active_num; i++)
493 status = graph_search_inst_string (gl_active[i], term,
494 /* callback = */ callback,
495 /* user data = */ user_data);
500 for (i = 0; i < gl_dynamic_num; i++)
504 status = graph_search_inst_string (gl_dynamic[i], term,
505 /* callback = */ callback,
506 /* user data = */ user_data);
512 } /* }}} int gl_search_string */
514 int gl_search_field (graph_ident_field_t field, /* {{{ */
515 const char *field_value,
516 graph_inst_callback_t callback, void *user_data)
520 if ((field_value == NULL) || (callback == NULL))
523 for (i = 0; i < gl_active_num; i++)
527 status = graph_inst_search_field (gl_active[i],
529 /* callback = */ callback,
530 /* user data = */ user_data);
535 for (i = 0; i < gl_dynamic_num; i++)
539 status = graph_inst_search_field (gl_dynamic[i],
541 /* callback = */ callback,
542 /* user data = */ user_data);
548 } /* }}} int gl_search_field */
550 int gl_foreach_host (int (*callback) (const char *host, void *user_data), /* {{{ */
556 for (i = 0; i < host_list_len; i++)
558 status = (*callback) (host_list[i], user_data);
564 } /* }}} int gl_foreach_host */
566 int gl_update (void) /* {{{ */
573 printf ("Content-Type: text/plain\n\n");
578 if ((gl_last_update + UPDATE_INTERVAL) >= now)
582 gl_clear_instances ();
584 gl_destroy (&gl_dynamic, &gl_dynamic_num);
586 graph_read_config ();
588 status = fs_scan (/* callback = */ gl_register_file, /* user data = */ NULL);
590 if (host_list_len > 0)
591 qsort (host_list, host_list_len, sizeof (*host_list),
594 gl_last_update = now;
596 for (i = 0; i < gl_active_num; i++)
597 graph_sort_instances (gl_active[i]);
602 } /* }}} int gl_update */
604 /* vim: set sw=2 sts=2 et fdm=marker : */