1 /**
2 * collection4 - graph_list.c
3 * Copyright (C) 2010 Florian octo Forster
4 *
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.
9 *
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.
14 *
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
19 *
20 * Authors:
21 * Florian octo Forster <ff at octo.it>
22 **/
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <inttypes.h>
28 #include <string.h>
29 #include <time.h>
30 #include <errno.h>
31 #include <assert.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
37 #include <yajl/yajl_parse.h>
39 #include "graph_list.h"
40 #include "common.h"
41 #include "filesystem.h"
42 #include "graph.h"
43 #include "graph_config.h"
44 #include "graph_def.h"
45 #include "graph_ident.h"
46 #include "graph_instance.h"
47 #include "utils_cgi.h"
48 #include "utils_search.h"
50 #include <fcgiapp.h>
51 #include <fcgi_stdio.h>
53 /*
54 * Defines
55 */
56 #define UPDATE_INTERVAL 900
57 #define CACHE_FILE "/tmp/collection4.json"
59 /*
60 * Global variables
61 */
62 static graph_config_t **gl_active = NULL;
63 static size_t gl_active_num = 0;
65 static graph_config_t **gl_staging = NULL;
66 static size_t gl_staging_num = 0;
68 /* Graphs created on-the-fly for files which don't match any existing graph
69 * definition. */
70 static graph_config_t **gl_dynamic = NULL;
71 static size_t gl_dynamic_num = 0;
73 static char **host_list = NULL;
74 static size_t host_list_len = 0;
76 static time_t gl_last_update = 0;
78 /* TODO: Turn this into an array for multiple data providers. */
79 static data_provider_t *data_provider = NULL;
81 /*
82 * Private functions
83 */
84 static int gl_add_graph_internal (graph_config_t *cfg, /* {{{ */
85 graph_config_t ***gl_array, size_t *gl_array_num)
86 {
87 graph_config_t **tmp;
89 #define ARRAY_PTR (*gl_array)
90 #define ARRAY_SIZE (*gl_array_num)
92 if (cfg == NULL)
93 return (EINVAL);
95 tmp = realloc (ARRAY_PTR, sizeof (*ARRAY_PTR) * (ARRAY_SIZE + 1));
96 if (tmp == NULL)
97 return (ENOMEM);
98 ARRAY_PTR = tmp;
100 ARRAY_PTR[ARRAY_SIZE] = cfg;
101 ARRAY_SIZE++;
103 #undef ARRAY_SIZE
104 #undef ARRAY_PTR
106 return (0);
107 } /* }}} int gl_add_graph_internal */
109 static void gl_destroy (graph_config_t ***gl_array, /* {{{ */
110 size_t *gl_array_num)
111 {
112 size_t i;
114 if ((gl_array == NULL) || (gl_array_num == NULL))
115 return;
117 #define ARRAY_PTR (*gl_array)
118 #define ARRAY_SIZE (*gl_array_num)
120 for (i = 0; i < ARRAY_SIZE; i++)
121 {
122 graph_destroy (ARRAY_PTR[i]);
123 ARRAY_PTR[i] = NULL;
124 }
125 free (ARRAY_PTR);
126 ARRAY_PTR = NULL;
127 ARRAY_SIZE = 0;
129 #undef ARRAY_SIZE
130 #undef ARRAY_PTR
131 } /* }}} void gl_destroy */
133 static int gl_register_host (const char *host) /* {{{ */
134 {
135 char **tmp;
136 size_t i;
138 if (host == NULL)
139 return (EINVAL);
141 for (i = 0; i < host_list_len; i++)
142 if (strcmp (host_list[i], host) == 0)
143 return (0);
145 tmp = realloc (host_list, sizeof (*host_list) * (host_list_len + 1));
146 if (tmp == NULL)
147 return (ENOMEM);
148 host_list = tmp;
150 host_list[host_list_len] = strdup (host);
151 if (host_list[host_list_len] == NULL)
152 return (ENOMEM);
154 host_list_len++;
155 return (0);
156 } /* }}} int gl_register_host */
158 static int gl_clear_hosts (void) /* {{{ */
159 {
160 size_t i;
162 for (i = 0; i < host_list_len; i++)
163 free (host_list[i]);
164 free (host_list);
166 host_list = NULL;
167 host_list_len = 0;
169 return (0);
170 } /* }}} int gl_clear_hosts */
172 static int gl_compare_hosts (const void *v0, const void *v1) /* {{{ */
173 {
174 return (strcmp (*(char * const *) v0, *(char * const *) v1));
175 } /* }}} int gl_compare_hosts */
177 static int gl_register_file (const graph_ident_t *file, /* {{{ */
178 __attribute__((unused)) void *user_data)
179 {
180 graph_config_t *cfg;
181 int num_graphs = 0;
182 size_t i;
184 for (i = 0; i < gl_active_num; i++)
185 {
186 graph_config_t *cfg = gl_active[i];
187 int status;
189 if (!graph_ident_matches (cfg, file))
190 continue;
192 status = graph_add_file (cfg, file);
193 if (status != 0)
194 {
195 /* report error */;
196 }
197 else
198 {
199 num_graphs++;
200 }
201 }
203 if (num_graphs == 0)
204 {
205 cfg = graph_create (file);
206 gl_add_graph_internal (cfg, &gl_dynamic, &gl_dynamic_num);
207 graph_add_file (cfg, file);
208 }
210 gl_register_host (ident_get_host (file));
212 return (0);
213 } /* }}} int gl_register_file */
215 static int gl_register_ident (const graph_ident_t *ident, /* {{{ */
216 __attribute__((unused)) void *user_data)
217 {
218 /* TODO: Check for duplicates if multiple data providers are used. */
220 return (gl_register_file (ident, user_data));
221 } /* }}} int gl_register_ident */
223 static const char *get_part_from_param (const char *prim_key, /* {{{ */
224 const char *sec_key)
225 {
226 const char *val;
228 val = param (prim_key);
229 if (val != NULL)
230 return (val);
232 return (param (sec_key));
233 } /* }}} const char *get_part_from_param */
235 static int gl_clear_instances (void) /* {{{ */
236 {
237 size_t i;
239 for (i = 0; i < gl_active_num; i++)
240 graph_clear_instances (gl_active[i]);
242 return (0);
243 } /* }}} int gl_clear_instances */
245 static void gl_dump_cb (void *ctx, /* {{{ */
246 const char *str, unsigned int len)
247 {
248 int fd = *((int *) ctx);
249 const char *buffer;
250 size_t buffer_size;
251 ssize_t status;
253 buffer = str;
254 buffer_size = (size_t) len;
255 while (buffer_size > 0)
256 {
257 status = write (fd, buffer, buffer_size);
258 if (status < 0)
259 {
260 fprintf (stderr, "write(2) failed with status %i\n", errno);
261 return;
262 }
264 buffer += status;
265 buffer_size -= status;
266 }
267 } /* }}} void gl_dump_cb */
269 static int gl_update_cache (void) /* {{{ */
270 {
271 int fd;
272 yajl_gen handler;
273 yajl_gen_config handler_config = { /* pretty = */ 1, /* indent = */ " " };
274 struct flock lock;
275 struct stat statbuf;
276 int status;
277 size_t i;
279 memset (&statbuf, 0, sizeof (statbuf));
280 status = stat (CACHE_FILE, &statbuf);
281 if (status == 0)
282 {
283 if (statbuf.st_mtime >= gl_last_update)
284 /* Not writing to cache because it's at least as new as our internal data */
285 return (0);
286 }
287 else
288 {
289 status = errno;
290 fprintf (stderr, "gl_update_cache: stat(2) failed with status %i\n",
291 status);
292 /* Continue writing the file if possible. */
293 }
295 fd = open (CACHE_FILE, O_WRONLY | O_TRUNC | O_CREAT,
296 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
297 if (fd < 0)
298 {
299 status = errno;
300 fprintf (stderr, "gl_update_cache: open(2) failed with status %i\n",
301 status);
302 return (status);
303 }
305 memset (&lock, 0, sizeof (lock));
306 lock.l_type = F_WRLCK;
307 lock.l_whence = SEEK_SET;
308 lock.l_start = 0;
309 lock.l_len = 0; /* lock everything */
311 while (42)
312 {
313 status = fcntl (fd, F_SETLKW, &lock);
314 if (status == 0)
315 break;
317 if (errno == EINTR)
318 continue;
320 fprintf (stderr, "gl_update_cache: fcntl(2) failed with status %i\n", errno);
321 close (fd);
322 return (errno);
323 }
325 handler = yajl_gen_alloc2 (gl_dump_cb, &handler_config,
326 /* alloc funcs = */ NULL, /* ctx = */ &fd);
327 if (handler == NULL)
328 {
329 close (fd);
330 return (-1);
331 }
333 fprintf (stderr, "gl_update_cache: Start writing data\n");
334 fflush (stderr);
336 yajl_gen_array_open (handler);
338 for (i = 0; i < gl_active_num; i++)
339 graph_to_json (gl_active[i], handler);
341 for (i = 0; i < gl_dynamic_num; i++)
342 graph_to_json (gl_dynamic[i], handler);
344 yajl_gen_array_close (handler);
346 yajl_gen_free (handler);
347 close (fd);
349 fprintf (stderr, "gl_update_cache: Finished writing data\n");
350 fflush (stderr);
352 return (0);
353 } /* }}} int gl_update_cache */
355 /*
356 * JSON parsing functions
357 */
358 #define CTX_MASK 0xff000000
359 #define CTX_GRAPH 0x01000000
360 #define CTX_GRAPH_SELECT 0x02000000
361 #define CTX_INST 0x03000000
362 #define CTX_INST_SELECT 0x04000000
363 #define CTX_INST_FILE 0x05000000
365 #define CTX_IDENT_MASK 0x00ff0000
366 #define CTX_IDENT_HOST 0x00010000
367 #define CTX_IDENT_PLUGIN 0x00020000
368 #define CTX_IDENT_PLUGIN_INSTANCE 0x00030000
369 #define CTX_IDENT_TYPE 0x00040000
370 #define CTX_IDENT_TYPE_INSTANCE 0x00050000
372 struct gl_json_context_s
373 {
374 uint32_t state;
376 graph_config_t *cfg;
377 graph_instance_t *inst;
378 graph_ident_t *ident;
380 _Bool dynamic_graph;
381 };
382 typedef struct gl_json_context_s gl_json_context_t;
384 static void set_state (gl_json_context_t *ctx, /* {{{ */
385 uint32_t new_state, uint32_t mask)
386 {
387 uint32_t old_state = ctx->state;
388 ctx->state = (old_state & ~mask) | (new_state & mask);
389 } /* }}} void set_state */
391 static int gl_json_string (void *user_data, /* {{{ */
392 const unsigned char *str,
393 unsigned int str_length)
394 {
395 gl_json_context_t *ctx = user_data;
396 char buffer[str_length + 1];
398 memcpy (buffer, str, str_length);
399 buffer[str_length] = 0;
401 if (((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
402 || ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
403 || ((ctx->state & CTX_MASK) == CTX_INST_FILE))
404 {
405 switch (ctx->state & CTX_IDENT_MASK)
406 {
407 case CTX_IDENT_HOST:
408 ident_set_host (ctx->ident, buffer);
409 break;
410 case CTX_IDENT_PLUGIN:
411 ident_set_plugin (ctx->ident, buffer);
412 break;
413 case CTX_IDENT_PLUGIN_INSTANCE:
414 ident_set_plugin_instance (ctx->ident, buffer);
415 break;
416 case CTX_IDENT_TYPE:
417 ident_set_type (ctx->ident, buffer);
418 break;
419 case CTX_IDENT_TYPE_INSTANCE:
420 ident_set_type_instance (ctx->ident, buffer);
421 break;
422 }
423 }
425 return (1);
426 } /* }}} int gl_json_string */
428 static int gl_json_start_map (void *user_data) /* {{{ */
429 {
430 gl_json_context_t *ctx = user_data;
432 if (((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
433 || ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
434 || ((ctx->state & CTX_MASK) == CTX_INST_FILE))
435 {
436 assert (ctx->ident == NULL);
437 ctx->ident = ident_create (ANY_TOKEN,
438 ANY_TOKEN, ANY_TOKEN,
439 ANY_TOKEN, ANY_TOKEN);
440 }
442 return (1);
443 } /* }}} int gl_json_start_map */
445 static int gl_json_end_map (void *user_data) /* {{{ */
446 {
447 gl_json_context_t *ctx = user_data;
449 if ((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
450 {
451 size_t i;
453 /* ctx->ident should now hold the valid selector */
454 assert (ctx->cfg == NULL);
455 assert (ctx->inst == NULL);
456 assert (ctx->ident != NULL);
458 for (i = 0; i < gl_active_num; i++)
459 {
460 if (graph_compare (gl_active[i], ctx->ident) != 0)
461 continue;
463 ctx->cfg = gl_active[i];
464 ctx->dynamic_graph = 0;
465 break;
466 }
468 if (ctx->cfg == NULL)
469 {
470 ctx->cfg = graph_create (ctx->ident);
471 ctx->dynamic_graph = 1;
472 }
474 ident_destroy (ctx->ident);
475 ctx->ident = NULL;
477 set_state (ctx, CTX_GRAPH, CTX_MASK);
478 }
479 else if ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
480 {
481 /* ctx->ident should now hold the valid selector */
482 assert (ctx->cfg != NULL);
483 assert (ctx->inst == NULL);
484 assert (ctx->ident != NULL);
486 ctx->inst = inst_create (ctx->cfg, ctx->ident);
487 ident_destroy (ctx->ident);
488 ctx->ident = NULL;
490 set_state (ctx, CTX_INST, CTX_MASK);
491 }
492 else if ((ctx->state & CTX_MASK) == CTX_INST_FILE)
493 {
494 /* ctx->ident should now hold the valid file */
495 assert (ctx->cfg != NULL);
496 assert (ctx->inst != NULL);
497 assert (ctx->ident != NULL);
499 inst_add_file (ctx->inst, ctx->ident);
500 ident_destroy (ctx->ident);
501 ctx->ident = NULL;
503 /* Don't reset the state here, files are in an array. */
504 }
505 else if ((ctx->state & CTX_MASK) == CTX_INST)
506 {
507 /* ctx->inst should now hold a complete instance */
508 assert (ctx->cfg != NULL);
509 assert (ctx->inst != NULL);
510 assert (ctx->ident == NULL);
512 graph_add_inst (ctx->cfg, ctx->inst);
513 /* don't destroy / free ctx->inst */
514 ctx->inst = NULL;
516 /* Don't reset the state here, instances are in an array. */
517 }
518 else if ((ctx->state & CTX_MASK) == CTX_GRAPH)
519 {
520 /* ctx->cfg should now hold a complete graph */
521 assert (ctx->cfg != NULL);
522 assert (ctx->inst == NULL);
523 assert (ctx->ident == NULL);
525 if (ctx->dynamic_graph)
526 gl_add_graph_internal (ctx->cfg, &gl_dynamic, &gl_dynamic_num);
527 /* else: already contained in gl_active */
528 ctx->cfg = NULL;
530 /* Don't reset the state here, graphs are in an array. */
531 }
533 return (1);
534 } /* }}} int gl_json_end_map */
536 static int gl_json_end_array (void *user_data) /* {{{ */
537 {
538 gl_json_context_t *ctx = user_data;
540 if ((ctx->state & CTX_MASK) == CTX_INST_FILE)
541 set_state (ctx, CTX_INST, CTX_MASK);
542 else if ((ctx->state & CTX_MASK) == CTX_INST)
543 set_state (ctx, CTX_GRAPH, CTX_MASK);
544 else if ((ctx->state & CTX_MASK) == CTX_GRAPH)
545 {
546 /* We're done */
547 }
549 return (1);
550 } /* }}} int gl_json_end_array */
552 static int gl_json_key (void *user_data, /* {{{ */
553 const unsigned char *str,
554 unsigned int str_length)
555 {
556 gl_json_context_t *ctx = user_data;
557 char buffer[str_length + 1];
559 memcpy (buffer, str, str_length);
560 buffer[str_length] = 0;
562 if ((ctx->state & CTX_MASK) == CTX_GRAPH)
563 {
564 if (strcasecmp ("select", buffer) == 0)
565 set_state (ctx, CTX_GRAPH_SELECT, CTX_MASK);
566 else if (strcasecmp ("instances", buffer) == 0)
567 set_state (ctx, CTX_INST, CTX_MASK);
568 }
569 else if ((ctx->state & CTX_MASK) == CTX_INST)
570 {
571 if (strcasecmp ("select", buffer) == 0)
572 set_state (ctx, CTX_INST_SELECT, CTX_MASK);
573 else if (strcasecmp ("files", buffer) == 0)
574 set_state (ctx, CTX_INST_FILE, CTX_MASK);
575 }
576 else if (((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
577 || ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
578 || ((ctx->state & CTX_MASK) == CTX_INST_FILE))
579 {
580 assert (ctx->ident != NULL);
582 if (strcasecmp ("host", buffer) == 0)
583 set_state (ctx, CTX_IDENT_HOST, CTX_IDENT_MASK);
584 else if (strcasecmp ("plugin", buffer) == 0)
585 set_state (ctx, CTX_IDENT_PLUGIN, CTX_IDENT_MASK);
586 else if (strcasecmp ("plugin_instance", buffer) == 0)
587 set_state (ctx, CTX_IDENT_PLUGIN_INSTANCE, CTX_IDENT_MASK);
588 else if (strcasecmp ("type", buffer) == 0)
589 set_state (ctx, CTX_IDENT_TYPE, CTX_IDENT_MASK);
590 else if (strcasecmp ("type_instance", buffer) == 0)
591 set_state (ctx, CTX_IDENT_TYPE_INSTANCE, CTX_IDENT_MASK);
592 }
594 return (1);
595 } /* }}} int gl_json_key */
597 yajl_callbacks gl_json_callbacks =
598 {
599 /* null = */ NULL,
600 /* boolean = */ NULL,
601 /* integer = */ NULL,
602 /* double = */ NULL,
603 /* number = */ NULL,
604 /* string = */ gl_json_string,
605 /* start_map = */ gl_json_start_map,
606 /* map_key = */ gl_json_key,
607 /* end_map = */ gl_json_end_map,
608 /* start_array = */ NULL,
609 /* end_array = */ gl_json_end_array
610 };
612 static int gl_read_cache (_Bool block) /* {{{ */
613 {
614 yajl_handle handle;
615 gl_json_context_t context;
616 yajl_parser_config handle_config = { /* comments = */ 0, /* check UTF-8 */ 0 };
618 int fd;
619 int cmd;
620 struct stat statbuf;
621 struct flock lock;
622 int status;
623 time_t now;
625 fd = open (CACHE_FILE, O_RDONLY);
626 if (fd < 0)
627 {
628 fprintf (stderr, "gl_read_cache: open(2) failed with status %i\n", errno);
629 return (errno);
630 }
632 if (block)
633 cmd = F_SETLKW;
634 else
635 cmd = F_SETLK;
637 memset (&lock, 0, sizeof (lock));
638 lock.l_type = F_RDLCK;
639 lock.l_whence = SEEK_SET;
640 lock.l_start = 0;
641 lock.l_len = 0; /* lock everything */
643 while (42)
644 {
645 status = fcntl (fd, cmd, &lock);
646 if (status == 0)
647 break;
649 if (!block && ((errno == EACCES) || (errno == EAGAIN)))
650 {
651 close (fd);
652 return (0);
653 }
655 if (errno == EINTR)
656 continue;
658 status = errno;
659 fprintf (stderr, "gl_read_cache: fcntl(2) failed with status %i\n",
660 status);
661 close (fd);
662 return (status);
663 }
665 fprintf (stderr, "gl_read_cache: Opening and locking "
666 "cache file successful\n");
668 memset (&statbuf, 0, sizeof (statbuf));
669 status = fstat (fd, &statbuf);
670 if (status != 0)
671 {
672 status = errno;
673 fprintf (stderr, "gl_read_cache: fstat(2) failed with status %i\n",
674 status);
675 close (fd);
676 return (status);
677 }
679 now = time (NULL);
681 if (block)
682 {
683 /* Read the file. No excuses. */
684 }
685 else if (statbuf.st_mtime <= gl_last_update)
686 {
687 /* Our current data is at least as new as the cache. Return. */
688 fprintf (stderr, "gl_read_cache: Not using cache because "
689 "the internal data is newer\n");
690 fflush (stderr);
691 close (fd);
692 return (0);
693 }
694 else if ((statbuf.st_mtime + UPDATE_INTERVAL) < now)
695 {
696 /* We'll scan the directory anyway, so there is no need to parse the cache
697 * here. */
698 fprintf (stderr, "gl_read_cache: Not using cache because it's too old\n");
699 fflush (stderr);
700 close (fd);
701 return (0);
702 }
704 memset (&context, 0, sizeof (context));
705 context.state = CTX_GRAPH;
706 context.cfg = NULL;
707 context.inst = NULL;
708 context.ident = NULL;
710 handle = yajl_alloc (&gl_json_callbacks,
711 &handle_config,
712 /* alloc funcs = */ NULL,
713 &context);
715 fprintf (stderr, "gl_read_cache: Start parsing data\n");
716 fflush (stderr);
718 while (42)
719 {
720 ssize_t rd_status;
721 char buffer[1024*1024];
723 rd_status = read (fd, buffer, sizeof (buffer));
724 if (rd_status < 0)
725 {
726 if ((errno == EINTR) || (errno == EAGAIN))
727 continue;
729 status = errno;
730 fprintf (stderr, "gl_read_cache: read(2) failed with status %i\n",
731 status);
732 close (fd);
733 return (status);
734 }
735 else if (rd_status == 0)
736 {
737 yajl_parse_complete (handle);
738 break;
739 }
740 else
741 {
742 yajl_parse (handle,
743 (unsigned char *) &buffer[0],
744 (unsigned int) rd_status);
745 }
746 }
748 yajl_free (handle);
750 gl_last_update = statbuf.st_mtime;
751 close (fd);
753 fprintf (stderr, "gl_read_cache: Finished parsing data\n");
754 fflush (stderr);
756 return (0);
757 } /* }}} int gl_read_cache */
759 /*
760 * Global functions
761 */
762 int gl_add_graph (graph_config_t *cfg) /* {{{ */
763 {
764 return (gl_add_graph_internal (cfg, &gl_staging, &gl_staging_num));
765 } /* }}} int gl_add_graph */
767 int gl_config_submit (void) /* {{{ */
768 {
769 graph_config_t **old;
770 size_t old_num;
772 old = gl_active;
773 old_num = gl_active_num;
775 gl_active = gl_staging;
776 gl_active_num = gl_staging_num;
778 gl_staging = NULL;
779 gl_staging_num = 0;
781 gl_destroy (&old, &old_num);
783 return (0);
784 } /* }}} int graph_config_submit */
786 int gl_register_data_provider (const char *name, data_provider_t *p) /* {{{ */
787 {
788 fprintf (stderr, "gl_register_data_provider (name = %s, ptr = %p)\n",
789 name, (void *) p);
791 if (data_provider == NULL)
792 data_provider = malloc (sizeof (*data_provider));
793 if (data_provider == NULL)
794 return (ENOMEM);
796 *data_provider = *p;
798 return (0);
799 } /* }}} int gl_register_data_provider */
801 int gl_graph_get_all (_Bool include_dynamic, /* {{{ */
802 graph_callback_t callback, void *user_data)
803 {
804 size_t i;
806 if (callback == NULL)
807 return (EINVAL);
809 gl_update (/* request served = */ 0);
811 for (i = 0; i < gl_active_num; i++)
812 {
813 int status;
815 status = (*callback) (gl_active[i], user_data);
816 if (status != 0)
817 return (status);
818 }
820 if (!include_dynamic)
821 return (0);
823 for (i = 0; i < gl_dynamic_num; i++)
824 {
825 int status;
827 status = (*callback) (gl_dynamic[i], user_data);
828 if (status != 0)
829 return (status);
830 }
832 return (0);
833 } /* }}} int gl_graph_get_all */
835 graph_config_t *gl_graph_get_selected (void) /* {{{ */
836 {
837 const char *host = get_part_from_param ("graph_host", "host");
838 const char *plugin = get_part_from_param ("graph_plugin", "plugin");
839 const char *plugin_instance = get_part_from_param ("graph_plugin_instance", "plugin_instance");
840 const char *type = get_part_from_param ("graph_type", "type");
841 const char *type_instance = get_part_from_param ("graph_type_instance", "type_instance");
842 graph_ident_t *ident;
843 size_t i;
845 if ((host == NULL)
846 || (plugin == NULL) || (plugin_instance == NULL)
847 || (type == NULL) || (type_instance == NULL))
848 return (NULL);
850 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
852 gl_update (/* request served = */ 0);
854 for (i = 0; i < gl_active_num; i++)
855 {
856 if (graph_compare (gl_active[i], ident) != 0)
857 continue;
859 ident_destroy (ident);
860 return (gl_active[i]);
861 }
863 for (i = 0; i < gl_dynamic_num; i++)
864 {
865 if (graph_compare (gl_dynamic[i], ident) != 0)
866 continue;
868 ident_destroy (ident);
869 return (gl_dynamic[i]);
870 }
872 ident_destroy (ident);
873 return (NULL);
874 } /* }}} graph_config_t *gl_graph_get_selected */
876 /* gl_instance_get_all, gl_graph_instance_get_all {{{ */
877 struct gl_inst_callback_data /* {{{ */
878 {
879 graph_config_t *cfg;
880 graph_inst_callback_t callback;
881 void *user_data;
882 }; /* }}} struct gl_inst_callback_data */
884 static int gl_inst_callback_handler (graph_instance_t *inst, /* {{{ */
885 void *user_data)
886 {
887 struct gl_inst_callback_data *data = user_data;
889 return ((*data->callback) (data->cfg, inst, data->user_data));
890 } /* }}} int gl_inst_callback_handler */
892 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
893 graph_inst_callback_t callback, void *user_data)
894 {
895 struct gl_inst_callback_data data =
896 {
897 cfg,
898 callback,
899 user_data
900 };
902 if ((cfg == NULL) || (callback == NULL))
903 return (EINVAL);
905 return (graph_inst_foreach (cfg, gl_inst_callback_handler, &data));
906 } /* }}} int gl_graph_instance_get_all */
908 int gl_instance_get_all (graph_inst_callback_t callback, /* {{{ */
909 void *user_data)
910 {
911 size_t i;
913 gl_update (/* request served = */ 0);
915 for (i = 0; i < gl_active_num; i++)
916 {
917 int status;
919 status = gl_graph_instance_get_all (gl_active[i], callback, user_data);
920 if (status != 0)
921 return (status);
922 }
924 for (i = 0; i < gl_dynamic_num; i++)
925 {
926 int status;
928 status = gl_graph_instance_get_all (gl_dynamic[i], callback, user_data);
929 if (status != 0)
930 return (status);
931 }
933 return (0);
934 } /* }}} int gl_instance_get_all */
935 /* }}} gl_instance_get_all, gl_graph_instance_get_all */
937 int gl_search (search_info_t *si, /* {{{ */
938 graph_inst_callback_t callback, void *user_data)
939 {
940 size_t i;
941 graph_ident_t *ident;
943 if ((si == NULL) || (callback == NULL))
944 return (EINVAL);
946 if (search_has_selector (si))
947 {
948 ident = search_to_ident (si);
949 if (ident == NULL)
950 {
951 fprintf (stderr, "gl_search: search_to_ident failed\n");
952 return (-1);
953 }
954 }
955 else
956 {
957 ident = NULL;
958 }
960 for (i = 0; i < gl_active_num; i++)
961 {
962 int status;
964 if ((ident != NULL) && !graph_ident_intersect (gl_active[i], ident))
965 continue;
967 status = graph_search_inst (gl_active[i], si,
968 /* callback = */ callback,
969 /* user data = */ user_data);
970 if (status != 0)
971 return (status);
972 }
974 for (i = 0; i < gl_dynamic_num; i++)
975 {
976 int status;
978 if ((ident != NULL) && !graph_ident_intersect (gl_dynamic[i], ident))
979 continue;
981 status = graph_search_inst (gl_dynamic[i], si,
982 /* callback = */ callback,
983 /* user data = */ user_data);
984 if (status != 0)
985 return (status);
986 }
988 return (0);
989 } /* }}} int gl_search */
991 int gl_search_string (const char *term, graph_inst_callback_t callback, /* {{{ */
992 void *user_data)
993 {
994 size_t i;
996 for (i = 0; i < gl_active_num; i++)
997 {
998 int status;
1000 status = graph_search_inst_string (gl_active[i], term,
1001 /* callback = */ callback,
1002 /* user data = */ user_data);
1003 if (status != 0)
1004 return (status);
1005 }
1007 for (i = 0; i < gl_dynamic_num; i++)
1008 {
1009 int status;
1011 status = graph_search_inst_string (gl_dynamic[i], term,
1012 /* callback = */ callback,
1013 /* user data = */ user_data);
1014 if (status != 0)
1015 return (status);
1016 }
1018 return (0);
1019 } /* }}} int gl_search_string */
1021 int gl_search_field (graph_ident_field_t field, /* {{{ */
1022 const char *field_value,
1023 graph_inst_callback_t callback, void *user_data)
1024 {
1025 size_t i;
1027 if ((field_value == NULL) || (callback == NULL))
1028 return (EINVAL);
1030 for (i = 0; i < gl_active_num; i++)
1031 {
1032 int status;
1034 status = graph_inst_search_field (gl_active[i],
1035 field, field_value,
1036 /* callback = */ callback,
1037 /* user data = */ user_data);
1038 if (status != 0)
1039 return (status);
1040 }
1042 for (i = 0; i < gl_dynamic_num; i++)
1043 {
1044 int status;
1046 status = graph_inst_search_field (gl_dynamic[i],
1047 field, field_value,
1048 /* callback = */ callback,
1049 /* user data = */ user_data);
1050 if (status != 0)
1051 return (status);
1052 }
1054 return (0);
1055 } /* }}} int gl_search_field */
1057 int gl_foreach_host (int (*callback) (const char *host, void *user_data), /* {{{ */
1058 void *user_data)
1059 {
1060 int status;
1061 size_t i;
1063 for (i = 0; i < host_list_len; i++)
1064 {
1065 status = (*callback) (host_list[i], user_data);
1066 if (status != 0)
1067 return (status);
1068 }
1070 return (0);
1071 } /* }}} int gl_foreach_host */
1073 int gl_update (_Bool request_served) /* {{{ */
1074 {
1075 time_t now;
1076 int status;
1077 size_t i;
1079 if (!request_served && (gl_last_update > 0))
1080 return (0);
1082 now = time (NULL);
1084 if ((gl_last_update + UPDATE_INTERVAL) >= now)
1085 {
1086 /* Write data to cache if appropriate */
1087 if (request_served)
1088 gl_update_cache ();
1089 return (0);
1090 }
1092 /* Clear state */
1093 gl_clear_instances ();
1094 gl_clear_hosts ();
1095 gl_destroy (&gl_dynamic, &gl_dynamic_num);
1097 graph_read_config ();
1099 status = gl_read_cache (/* block = */ 1);
1100 /* We have *something* to work with. Even if it's outdated, just get on with
1101 * handling the request and take care of re-reading data later on. */
1102 if ((status == 0) && !request_served)
1103 return (0);
1105 if ((status != 0)
1106 || ((gl_last_update + UPDATE_INTERVAL) < now))
1107 {
1108 if (data_provider == NULL)
1109 {
1110 fprintf (stderr, "No data provider has been registered\n");
1111 return (ENOENT);
1112 }
1114 /* Clear state */
1115 gl_clear_instances ();
1116 gl_clear_hosts ();
1117 gl_destroy (&gl_dynamic, &gl_dynamic_num);
1119 /* TODO: Iterate over all data providers */
1120 data_provider->get_idents (data_provider->private_data,
1121 gl_register_ident, /* user data = */ NULL);
1123 gl_last_update = now;
1124 }
1126 if (host_list_len > 0)
1127 qsort (host_list, host_list_len, sizeof (*host_list),
1128 gl_compare_hosts);
1130 for (i = 0; i < gl_active_num; i++)
1131 graph_sort_instances (gl_active[i]);
1133 if (request_served)
1134 gl_update_cache ();
1136 return (status);
1137 } /* }}} int gl_update */
1139 /* vim: set sw=2 sts=2 et fdm=marker : */