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>
35 #include <yajl/yajl_parse.h>
37 #include "graph_list.h"
39 #include "filesystem.h"
41 #include "graph_config.h"
42 #include "graph_def.h"
43 #include "graph_ident.h"
44 #include "graph_instance.h"
45 #include "utils_cgi.h"
46 #include "utils_search.h"
49 #include <fcgi_stdio.h>
54 #define UPDATE_INTERVAL 900
55 #define CACHE_FILE "/tmp/collection4.json"
60 static graph_config_t **gl_active = NULL;
61 static size_t gl_active_num = 0;
63 static graph_config_t **gl_staging = NULL;
64 static size_t gl_staging_num = 0;
66 /* Graphs created on-the-fly for files which don't match any existing graph
68 static graph_config_t **gl_dynamic = NULL;
69 static size_t gl_dynamic_num = 0;
71 static char **host_list = NULL;
72 static size_t host_list_len = 0;
74 static time_t gl_last_update = 0;
79 static int gl_add_graph_internal (graph_config_t *cfg, /* {{{ */
80 graph_config_t ***gl_array, size_t *gl_array_num)
84 #define ARRAY_PTR (*gl_array)
85 #define ARRAY_SIZE (*gl_array_num)
90 tmp = realloc (ARRAY_PTR, sizeof (*ARRAY_PTR) * (ARRAY_SIZE + 1));
95 ARRAY_PTR[ARRAY_SIZE] = cfg;
102 } /* }}} int gl_add_graph_internal */
104 static void gl_destroy (graph_config_t ***gl_array, /* {{{ */
105 size_t *gl_array_num)
109 if ((gl_array == NULL) || (gl_array_num == NULL))
112 #define ARRAY_PTR (*gl_array)
113 #define ARRAY_SIZE (*gl_array_num)
115 for (i = 0; i < ARRAY_SIZE; i++)
117 graph_destroy (ARRAY_PTR[i]);
126 } /* }}} void gl_destroy */
128 static int gl_register_host (const char *host) /* {{{ */
136 for (i = 0; i < host_list_len; i++)
137 if (strcmp (host_list[i], host) == 0)
140 tmp = realloc (host_list, sizeof (*host_list) * (host_list_len + 1));
145 host_list[host_list_len] = strdup (host);
146 if (host_list[host_list_len] == NULL)
151 } /* }}} int gl_register_host */
153 static int gl_clear_hosts (void) /* {{{ */
157 for (i = 0; i < host_list_len; i++)
165 } /* }}} int gl_clear_hosts */
167 static int gl_compare_hosts (const void *v0, const void *v1) /* {{{ */
169 return (strcmp (*(char * const *) v0, *(char * const *) v1));
170 } /* }}} int gl_compare_hosts */
172 static int gl_register_file (const graph_ident_t *file, /* {{{ */
173 __attribute__((unused)) void *user_data)
179 for (i = 0; i < gl_active_num; i++)
181 graph_config_t *cfg = gl_active[i];
184 if (!graph_ident_matches (cfg, file))
187 status = graph_add_file (cfg, file);
200 cfg = graph_create (file);
201 gl_add_graph_internal (cfg, &gl_dynamic, &gl_dynamic_num);
202 graph_add_file (cfg, file);
205 gl_register_host (ident_get_host (file));
208 } /* }}} int gl_register_file */
210 static const char *get_part_from_param (const char *prim_key, /* {{{ */
215 val = param (prim_key);
219 return (param (sec_key));
220 } /* }}} const char *get_part_from_param */
222 static int gl_clear_instances (void) /* {{{ */
226 for (i = 0; i < gl_active_num; i++)
227 graph_clear_instances (gl_active[i]);
230 } /* }}} int gl_clear_instances */
232 static void gl_dump_cb (void *ctx, /* {{{ */
233 const char *str, unsigned int len)
237 /* FIXME: Has everything been written? */
238 fwrite ((void *) str, /* size = */ 1, /* nmemb = */ len, fh);
239 } /* }}} void gl_dump_cb */
241 static int gl_dump (void) /* {{{ */
245 yajl_gen_config handler_config = { /* pretty = */ 1, /* indent = */ " " };
248 /* FIXME: Lock the file */
249 fh = fopen (CACHE_FILE, "w");
253 handler = yajl_gen_alloc2 (gl_dump_cb, &handler_config,
254 /* alloc funcs = */ NULL, /* ctx = */ fh);
261 yajl_gen_array_open (handler);
263 for (i = 0; i < gl_active_num; i++)
264 graph_to_json (gl_active[i], handler);
266 for (i = 0; i < gl_dynamic_num; i++)
267 graph_to_json (gl_dynamic[i], handler);
269 yajl_gen_array_close (handler);
271 yajl_gen_free (handler);
275 } /* }}} int gl_dump */
278 * JSON parsing functions
280 #define CTX_MASK 0xff000000
281 #define CTX_GRAPH 0x01000000
282 #define CTX_GRAPH_SELECT 0x02000000
283 #define CTX_INST 0x03000000
284 #define CTX_INST_SELECT 0x04000000
285 #define CTX_INST_FILE 0x05000000
287 #define CTX_IDENT_MASK 0x00ff0000
288 #define CTX_IDENT_HOST 0x00010000
289 #define CTX_IDENT_PLUGIN 0x00020000
290 #define CTX_IDENT_PLUGIN_INSTANCE 0x00030000
291 #define CTX_IDENT_TYPE 0x00040000
292 #define CTX_IDENT_TYPE_INSTANCE 0x00050000
294 struct gl_json_context_s
299 graph_instance_t *inst;
300 graph_ident_t *ident;
304 typedef struct gl_json_context_s gl_json_context_t;
306 static void set_state (gl_json_context_t *ctx, /* {{{ */
307 uint32_t new_state, uint32_t mask)
309 uint32_t old_state = ctx->state;
310 ctx->state = (old_state & ~mask) | (new_state & mask);
311 } /* }}} void set_state */
313 static int gl_json_string (void *user_data, /* {{{ */
314 const unsigned char *str,
315 unsigned int str_length)
317 gl_json_context_t *ctx = user_data;
318 char buffer[str_length + 1];
320 memcpy (buffer, str, str_length);
321 buffer[str_length] = 0;
323 if (((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
324 || ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
325 || ((ctx->state & CTX_MASK) == CTX_INST_FILE))
327 switch (ctx->state & CTX_IDENT_MASK)
330 ident_set_host (ctx->ident, buffer);
332 case CTX_IDENT_PLUGIN:
333 ident_set_plugin (ctx->ident, buffer);
335 case CTX_IDENT_PLUGIN_INSTANCE:
336 ident_set_plugin_instance (ctx->ident, buffer);
339 ident_set_type (ctx->ident, buffer);
341 case CTX_IDENT_TYPE_INSTANCE:
342 ident_set_type_instance (ctx->ident, buffer);
348 } /* }}} int gl_json_string */
350 static int gl_json_end_map (void *user_data) /* {{{ */
352 gl_json_context_t *ctx = user_data;
354 if ((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
358 /* ctx->ident should now hold the valid selector */
359 assert (ctx->cfg == NULL);
360 assert (ctx->inst == NULL);
361 assert (ctx->ident != NULL);
363 for (i = 0; i < gl_active_num; i++)
365 if (graph_compare (gl_active[i], ctx->ident) != 0)
368 ctx->cfg = gl_active[i];
369 ctx->dynamic_graph = 0;
373 if (ctx->cfg == NULL)
375 ctx->cfg = graph_create (ctx->ident);
376 ctx->dynamic_graph = 1;
379 ident_destroy (ctx->ident);
382 set_state (ctx, CTX_GRAPH, CTX_MASK);
384 else if ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
386 /* ctx->ident should now hold the valid selector */
387 assert (ctx->cfg != NULL);
388 assert (ctx->inst == NULL);
389 assert (ctx->ident != NULL);
391 ctx->inst = inst_create (ctx->cfg, ctx->ident);
392 ident_destroy (ctx->ident);
395 set_state (ctx, CTX_INST, CTX_MASK);
397 else if ((ctx->state & CTX_MASK) == CTX_INST_FILE)
399 /* ctx->ident should now hold the valid file */
400 assert (ctx->cfg != NULL);
401 assert (ctx->inst != NULL);
402 assert (ctx->ident != NULL);
404 inst_add_file (ctx->inst, ctx->ident);
405 ident_destroy (ctx->ident);
408 /* Don't reset the state here, files are in an array. */
410 else if ((ctx->state & CTX_MASK) == CTX_INST)
412 /* ctx->inst should now hold a complete instance */
413 assert (ctx->cfg != NULL);
414 assert (ctx->inst != NULL);
415 assert (ctx->ident == NULL);
417 graph_add_inst (ctx->cfg, ctx->inst);
418 /* don't destroy / free ctx->inst */
421 /* Don't reset the state here, instances are in an array. */
423 else if ((ctx->state & CTX_MASK) == CTX_GRAPH)
425 /* ctx->cfg should now hold a complete graph */
426 assert (ctx->cfg != NULL);
427 assert (ctx->inst == NULL);
428 assert (ctx->ident == NULL);
430 if (ctx->dynamic_graph)
431 gl_add_graph_internal (ctx->cfg, &gl_dynamic, &gl_dynamic_num);
432 /* else: already contained in gl_active */
435 /* Don't reset the state here, graphs are in an array. */
439 } /* }}} int gl_json_end_map */
441 static int gl_json_end_array (void *user_data) /* {{{ */
443 gl_json_context_t *ctx = user_data;
445 if ((ctx->state & CTX_MASK) == CTX_INST_FILE)
446 set_state (ctx, CTX_INST, CTX_MASK);
447 else if ((ctx->state & CTX_MASK) == CTX_INST)
448 set_state (ctx, CTX_GRAPH, CTX_MASK);
449 else if ((ctx->state & CTX_MASK) == CTX_GRAPH)
455 } /* }}} int gl_json_end_array */
457 static int gl_json_key (void *user_data, /* {{{ */
458 const unsigned char *str,
459 unsigned int str_length)
461 gl_json_context_t *ctx = user_data;
462 char buffer[str_length + 1];
464 memcpy (buffer, str, str_length);
465 buffer[str_length] = 0;
467 if ((ctx->state & CTX_MASK) == CTX_GRAPH)
469 if (strcasecmp ("select", buffer) == 0)
470 set_state (ctx, CTX_GRAPH_SELECT, CTX_MASK);
471 else if (strcasecmp ("instances", buffer) == 0)
472 set_state (ctx, CTX_INST, CTX_MASK);
474 else if ((ctx->state & CTX_MASK) == CTX_INST)
476 if (strcasecmp ("select", buffer) == 0)
477 set_state (ctx, CTX_INST_SELECT, CTX_MASK);
478 else if (strcasecmp ("files", buffer) == 0)
479 set_state (ctx, CTX_INST_FILE, CTX_MASK);
481 else if (((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
482 || ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
483 || ((ctx->state & CTX_MASK) == CTX_INST_FILE))
485 if (strcasecmp ("host", buffer) == 0)
486 set_state (ctx, CTX_IDENT_HOST, CTX_IDENT_MASK);
487 else if (strcasecmp ("plugin", buffer) == 0)
488 set_state (ctx, CTX_IDENT_PLUGIN, CTX_IDENT_MASK);
489 else if (strcasecmp ("plugin_instance", buffer) == 0)
490 set_state (ctx, CTX_IDENT_PLUGIN_INSTANCE, CTX_IDENT_MASK);
491 else if (strcasecmp ("type", buffer) == 0)
492 set_state (ctx, CTX_IDENT_TYPE, CTX_IDENT_MASK);
493 else if (strcasecmp ("type_instance", buffer) == 0)
494 set_state (ctx, CTX_IDENT_TYPE_INSTANCE, CTX_IDENT_MASK);
498 } /* }}} int gl_json_key */
500 yajl_callbacks gl_json_callbacks =
503 /* boolean = */ NULL,
504 /* integer = */ NULL,
507 /* string = */ gl_json_string,
508 /* start_map = */ NULL,
509 /* map_key = */ gl_json_key,
510 /* end_map = */ gl_json_end_map,
511 /* start_array = */ NULL,
512 /* end_array = */ gl_json_end_array
515 static int gl_read_cache (_Bool block) /* {{{ */
518 gl_json_context_t context;
519 yajl_parser_config handle_config = { /* comments = */ 0, /* check UTF-8 */ 0 };
526 fd = open (CACHE_FILE, O_RDONLY);
529 fprintf (stderr, "gl_read_cache: open(2) failed with status %i\n", errno);
538 memset (&lock, 0, sizeof (lock));
539 lock.l_type = F_RDLCK;
540 lock.l_whence = SEEK_SET;
542 lock.l_len = 0; /* lock everything */
546 status = fcntl (fd, cmd, &lock);
550 if (!block && ((errno == EACCES) || (errno == EAGAIN)))
559 fprintf (stderr, "gl_read_cache: fcntl(2) failed with status %i\n",
564 memset (&context, 0, sizeof (context));
567 context.ident = NULL;
569 handle = yajl_alloc (&gl_json_callbacks,
571 /* alloc funcs = */ NULL,
576 } /* }}} int gl_read_cache */
581 int gl_add_graph (graph_config_t *cfg) /* {{{ */
583 return (gl_add_graph_internal (cfg, &gl_staging, &gl_staging_num));
584 } /* }}} int gl_add_graph */
586 int gl_config_submit (void) /* {{{ */
588 graph_config_t **old;
592 old_num = gl_active_num;
594 gl_active = gl_staging;
595 gl_active_num = gl_staging_num;
600 gl_destroy (&old, &old_num);
603 } /* }}} int graph_config_submit */
605 int gl_graph_get_all (graph_callback_t callback, /* {{{ */
610 if (callback == NULL)
615 for (i = 0; i < gl_active_num; i++)
619 status = (*callback) (gl_active[i], user_data);
624 for (i = 0; i < gl_dynamic_num; i++)
628 status = (*callback) (gl_dynamic[i], user_data);
634 } /* }}} int gl_graph_get_all */
636 graph_config_t *gl_graph_get_selected (void) /* {{{ */
638 const char *host = get_part_from_param ("graph_host", "host");
639 const char *plugin = get_part_from_param ("graph_plugin", "plugin");
640 const char *plugin_instance = get_part_from_param ("graph_plugin_instance", "plugin_instance");
641 const char *type = get_part_from_param ("graph_type", "type");
642 const char *type_instance = get_part_from_param ("graph_type_instance", "type_instance");
643 graph_ident_t *ident;
647 || (plugin == NULL) || (plugin_instance == NULL)
648 || (type == NULL) || (type_instance == NULL))
651 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
655 for (i = 0; i < gl_active_num; i++)
657 if (graph_compare (gl_active[i], ident) != 0)
660 ident_destroy (ident);
661 return (gl_active[i]);
664 for (i = 0; i < gl_dynamic_num; i++)
666 if (graph_compare (gl_dynamic[i], ident) != 0)
669 ident_destroy (ident);
670 return (gl_dynamic[i]);
673 ident_destroy (ident);
675 } /* }}} graph_config_t *gl_graph_get_selected */
677 /* gl_instance_get_all, gl_graph_instance_get_all {{{ */
678 struct gl_inst_callback_data /* {{{ */
681 graph_inst_callback_t callback;
683 }; /* }}} struct gl_inst_callback_data */
685 static int gl_inst_callback_handler (graph_instance_t *inst, /* {{{ */
688 struct gl_inst_callback_data *data = user_data;
690 return ((*data->callback) (data->cfg, inst, data->user_data));
691 } /* }}} int gl_inst_callback_handler */
693 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
694 graph_inst_callback_t callback, void *user_data)
696 struct gl_inst_callback_data data =
703 if ((cfg == NULL) || (callback == NULL))
706 return (graph_inst_foreach (cfg, gl_inst_callback_handler, &data));
707 } /* }}} int gl_graph_instance_get_all */
709 int gl_instance_get_all (graph_inst_callback_t callback, /* {{{ */
716 for (i = 0; i < gl_active_num; i++)
720 status = gl_graph_instance_get_all (gl_active[i], callback, user_data);
725 for (i = 0; i < gl_dynamic_num; i++)
729 status = gl_graph_instance_get_all (gl_dynamic[i], callback, user_data);
735 } /* }}} int gl_instance_get_all */
736 /* }}} gl_instance_get_all, gl_graph_instance_get_all */
738 int gl_search (search_info_t *si, /* {{{ */
739 graph_inst_callback_t callback, void *user_data)
742 graph_ident_t *ident;
744 if ((si == NULL) || (callback == NULL))
747 if (search_has_selector (si))
749 ident = search_to_ident (si);
752 fprintf (stderr, "gl_search: search_to_ident failed\n");
761 for (i = 0; i < gl_active_num; i++)
765 if ((ident != NULL) && !graph_ident_intersect (gl_active[i], ident))
768 status = graph_search_inst (gl_active[i], si,
769 /* callback = */ callback,
770 /* user data = */ user_data);
775 for (i = 0; i < gl_dynamic_num; i++)
779 if ((ident != NULL) && !graph_ident_intersect (gl_dynamic[i], ident))
782 status = graph_search_inst (gl_dynamic[i], si,
783 /* callback = */ callback,
784 /* user data = */ user_data);
790 } /* }}} int gl_search */
792 int gl_search_string (const char *term, graph_inst_callback_t callback, /* {{{ */
797 for (i = 0; i < gl_active_num; i++)
801 status = graph_search_inst_string (gl_active[i], term,
802 /* callback = */ callback,
803 /* user data = */ user_data);
808 for (i = 0; i < gl_dynamic_num; i++)
812 status = graph_search_inst_string (gl_dynamic[i], term,
813 /* callback = */ callback,
814 /* user data = */ user_data);
820 } /* }}} int gl_search_string */
822 int gl_search_field (graph_ident_field_t field, /* {{{ */
823 const char *field_value,
824 graph_inst_callback_t callback, void *user_data)
828 if ((field_value == NULL) || (callback == NULL))
831 for (i = 0; i < gl_active_num; i++)
835 status = graph_inst_search_field (gl_active[i],
837 /* callback = */ callback,
838 /* user data = */ user_data);
843 for (i = 0; i < gl_dynamic_num; i++)
847 status = graph_inst_search_field (gl_dynamic[i],
849 /* callback = */ callback,
850 /* user data = */ user_data);
856 } /* }}} int gl_search_field */
858 int gl_foreach_host (int (*callback) (const char *host, void *user_data), /* {{{ */
864 for (i = 0; i < host_list_len; i++)
866 status = (*callback) (host_list[i], user_data);
872 } /* }}} int gl_foreach_host */
874 int gl_update (void) /* {{{ */
881 printf ("Content-Type: text/plain\n\n");
886 if ((gl_last_update + UPDATE_INTERVAL) >= now)
890 gl_clear_instances ();
892 gl_destroy (&gl_dynamic, &gl_dynamic_num);
894 graph_read_config ();
896 status = fs_scan (/* callback = */ gl_register_file, /* user data = */ NULL);
898 if (host_list_len > 0)
899 qsort (host_list, host_list_len, sizeof (*host_list),
902 gl_last_update = now;
904 for (i = 0; i < gl_active_num; i++)
905 graph_sort_instances (gl_active[i]);
910 } /* }}} int gl_update */
912 /* vim: set sw=2 sts=2 et fdm=marker : */