src/graph_list.[ch]: Use the data provider to read a list of files.
[collection4.git] / src / graph_list.c
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  **/
23
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>
36
37 #include <yajl/yajl_parse.h>
38
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"
49
50 #include <fcgiapp.h>
51 #include <fcgi_stdio.h>
52
53 /*
54  * Defines
55  */
56 #define UPDATE_INTERVAL 900
57 #define CACHE_FILE "/tmp/collection4.json"
58
59 /*
60  * Global variables
61  */
62 static graph_config_t **gl_active = NULL;
63 static size_t gl_active_num = 0;
64
65 static graph_config_t **gl_staging = NULL;
66 static size_t gl_staging_num = 0;
67
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;
72
73 static char **host_list = NULL;
74 static size_t host_list_len = 0;
75
76 static time_t gl_last_update = 0;
77
78 /* TODO: Turn this into an array for multiple data providers. */
79 static data_provider_t *data_provider = NULL;
80
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;
88
89 #define ARRAY_PTR  (*gl_array)
90 #define ARRAY_SIZE (*gl_array_num)
91
92   if (cfg == NULL)
93     return (EINVAL);
94
95   tmp = realloc (ARRAY_PTR, sizeof (*ARRAY_PTR) * (ARRAY_SIZE + 1));
96   if (tmp == NULL)
97     return (ENOMEM);
98   ARRAY_PTR = tmp;
99
100   ARRAY_PTR[ARRAY_SIZE] = cfg;
101   ARRAY_SIZE++;
102
103 #undef ARRAY_SIZE
104 #undef ARRAY_PTR
105
106   return (0);
107 } /* }}} int gl_add_graph_internal */
108
109 static void gl_destroy (graph_config_t ***gl_array, /* {{{ */
110     size_t *gl_array_num)
111 {
112   size_t i;
113
114   if ((gl_array == NULL) || (gl_array_num == NULL))
115     return;
116
117 #define ARRAY_PTR  (*gl_array)
118 #define ARRAY_SIZE (*gl_array_num)
119
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;
128
129 #undef ARRAY_SIZE
130 #undef ARRAY_PTR
131 } /* }}} void gl_destroy */
132
133 static int gl_register_host (const char *host) /* {{{ */
134 {
135   char **tmp;
136   size_t i;
137
138   if (host == NULL)
139     return (EINVAL);
140
141   for (i = 0; i < host_list_len; i++)
142     if (strcmp (host_list[i], host) == 0)
143       return (0);
144
145   tmp = realloc (host_list, sizeof (*host_list) * (host_list_len + 1));
146   if (tmp == NULL)
147     return (ENOMEM);
148   host_list = tmp;
149
150   host_list[host_list_len] = strdup (host);
151   if (host_list[host_list_len] == NULL)
152     return (ENOMEM);
153
154   host_list_len++;
155   return (0);
156 } /* }}} int gl_register_host */
157
158 static int gl_clear_hosts (void) /* {{{ */
159 {
160   size_t i;
161
162   for (i = 0; i < host_list_len; i++)
163     free (host_list[i]);
164   free (host_list);
165
166   host_list = NULL;
167   host_list_len = 0;
168
169   return (0);
170 } /* }}} int gl_clear_hosts */
171
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 */
176
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;
183
184   for (i = 0; i < gl_active_num; i++)
185   {
186     graph_config_t *cfg = gl_active[i];
187     int status;
188
189     if (!graph_ident_matches (cfg, file))
190       continue;
191
192     status = graph_add_file (cfg, file);
193     if (status != 0)
194     {
195       /* report error */;
196     }
197     else
198     {
199       num_graphs++;
200     }
201   }
202
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   }
209
210   gl_register_host (ident_get_host (file));
211
212   return (0);
213 } /* }}} int gl_register_file */
214
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. */
219
220   return (gl_register_file (ident, user_data));
221 } /* }}} int gl_register_ident */
222
223 static const char *get_part_from_param (const char *prim_key, /* {{{ */
224     const char *sec_key)
225 {
226   const char *val;
227
228   val = param (prim_key);
229   if (val != NULL)
230     return (val);
231   
232   return (param (sec_key));
233 } /* }}} const char *get_part_from_param */
234
235 static int gl_clear_instances (void) /* {{{ */
236 {
237   size_t i;
238
239   for (i = 0; i < gl_active_num; i++)
240     graph_clear_instances (gl_active[i]);
241
242   return (0);
243 } /* }}} int gl_clear_instances */
244
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;
252
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     }
263
264     buffer += status;
265     buffer_size -= status;
266   }
267 } /* }}} void gl_dump_cb */
268
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;
278
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     {
285       fprintf (stderr, "gl_update_cache: Not writing to cache because it's "
286           "at least as new as our internal data\n");
287       return (0);
288     }
289   }
290   else
291   {
292     status = errno;
293     fprintf (stderr, "gl_update_cache: stat(2) failed with status %i\n",
294         status);
295     /* Continue writing the file if possible. */
296   }
297
298   fd = open (CACHE_FILE, O_WRONLY | O_TRUNC | O_CREAT,
299       S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
300   if (fd < 0)
301   {
302     status = errno;
303     fprintf (stderr, "gl_update_cache: open(2) failed with status %i\n",
304         status);
305     return (status);
306   }
307
308   memset (&lock, 0, sizeof (lock));
309   lock.l_type = F_WRLCK;
310   lock.l_whence = SEEK_SET;
311   lock.l_start = 0;
312   lock.l_len = 0; /* lock everything */
313
314   while (42)
315   {
316     status = fcntl (fd, F_SETLKW, &lock);
317     if (status == 0)
318       break;
319
320     if (errno == EINTR)
321       continue;
322
323     fprintf (stderr, "gl_update_cache: fcntl(2) failed with status %i\n", errno);
324     close (fd);
325     return (errno);
326   }
327
328   handler = yajl_gen_alloc2 (gl_dump_cb, &handler_config,
329       /* alloc funcs = */ NULL, /* ctx = */ &fd);
330   if (handler == NULL)
331   {
332     close (fd);
333     return (-1);
334   }
335
336   fprintf (stderr, "gl_update_cache: Start writing data\n");
337   fflush (stderr);
338
339   yajl_gen_array_open (handler);
340
341   for (i = 0; i < gl_active_num; i++)
342     graph_to_json (gl_active[i], handler);
343
344   for (i = 0; i < gl_dynamic_num; i++)
345     graph_to_json (gl_dynamic[i], handler);
346
347   yajl_gen_array_close (handler);
348
349   yajl_gen_free (handler);
350   close (fd);
351
352   fprintf (stderr, "gl_update_cache: Finished writing data\n");
353   fflush (stderr);
354
355   return (0);
356 } /* }}} int gl_update_cache */
357
358 /*
359  * JSON parsing functions
360  */
361 #define CTX_MASK                  0xff000000
362 #define CTX_GRAPH                 0x01000000
363 #define CTX_GRAPH_SELECT          0x02000000
364 #define CTX_INST                  0x03000000
365 #define CTX_INST_SELECT           0x04000000
366 #define CTX_INST_FILE             0x05000000
367
368 #define CTX_IDENT_MASK            0x00ff0000
369 #define CTX_IDENT_HOST            0x00010000
370 #define CTX_IDENT_PLUGIN          0x00020000
371 #define CTX_IDENT_PLUGIN_INSTANCE 0x00030000
372 #define CTX_IDENT_TYPE            0x00040000
373 #define CTX_IDENT_TYPE_INSTANCE   0x00050000
374
375 struct gl_json_context_s
376 {
377   uint32_t          state;
378
379   graph_config_t   *cfg;
380   graph_instance_t *inst;
381   graph_ident_t    *ident;
382
383   _Bool             dynamic_graph;
384 };
385 typedef struct gl_json_context_s gl_json_context_t;
386
387 static void set_state (gl_json_context_t *ctx, /* {{{ */
388     uint32_t new_state, uint32_t mask)
389 {
390   uint32_t old_state = ctx->state;
391   ctx->state = (old_state & ~mask) | (new_state & mask);
392 } /* }}} void set_state */
393
394 static int gl_json_string (void *user_data, /* {{{ */
395     const unsigned char *str,
396     unsigned int str_length)
397 {
398   gl_json_context_t *ctx = user_data;
399   char buffer[str_length + 1];
400
401   memcpy (buffer, str, str_length);
402   buffer[str_length] = 0;
403
404   if (((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
405       || ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
406       || ((ctx->state & CTX_MASK) == CTX_INST_FILE))
407   {
408     switch (ctx->state & CTX_IDENT_MASK)
409     {
410       case CTX_IDENT_HOST:
411         ident_set_host (ctx->ident, buffer);
412         break;
413       case CTX_IDENT_PLUGIN:
414         ident_set_plugin (ctx->ident, buffer);
415         break;
416       case CTX_IDENT_PLUGIN_INSTANCE:
417         ident_set_plugin_instance (ctx->ident, buffer);
418         break;
419       case CTX_IDENT_TYPE:
420         ident_set_type (ctx->ident, buffer);
421         break;
422       case CTX_IDENT_TYPE_INSTANCE:
423         ident_set_type_instance (ctx->ident, buffer);
424         break;
425     }
426   }
427
428   return (1);
429 } /* }}} int gl_json_string */
430
431 static int gl_json_start_map (void *user_data) /* {{{ */
432 {
433   gl_json_context_t *ctx = user_data;
434
435   if (((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
436       || ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
437       || ((ctx->state & CTX_MASK) == CTX_INST_FILE))
438   {
439       assert (ctx->ident == NULL);
440       ctx->ident = ident_create (ANY_TOKEN,
441           ANY_TOKEN, ANY_TOKEN,
442           ANY_TOKEN, ANY_TOKEN);
443   }
444
445   return (1);
446 } /* }}} int gl_json_start_map */
447
448 static int gl_json_end_map (void *user_data) /* {{{ */
449 {
450   gl_json_context_t *ctx = user_data;
451
452   if ((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
453   {
454     size_t i;
455
456     /* ctx->ident should now hold the valid selector */
457     assert (ctx->cfg == NULL);
458     assert (ctx->inst  == NULL);
459     assert (ctx->ident != NULL);
460
461     for (i = 0; i < gl_active_num; i++)
462     {
463       if (graph_compare (gl_active[i], ctx->ident) != 0)
464         continue;
465
466       ctx->cfg = gl_active[i];
467       ctx->dynamic_graph = 0;
468       break;
469     }
470
471     if (ctx->cfg == NULL)
472     {
473       ctx->cfg = graph_create (ctx->ident);
474       ctx->dynamic_graph = 1;
475     }
476
477     ident_destroy (ctx->ident);
478     ctx->ident = NULL;
479
480     set_state (ctx, CTX_GRAPH, CTX_MASK);
481   }
482   else if ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
483   {
484     /* ctx->ident should now hold the valid selector */
485     assert (ctx->cfg   != NULL);
486     assert (ctx->inst  == NULL);
487     assert (ctx->ident != NULL);
488
489     ctx->inst = inst_create (ctx->cfg, ctx->ident);
490     ident_destroy (ctx->ident);
491     ctx->ident = NULL;
492
493     set_state (ctx, CTX_INST, CTX_MASK);
494   }
495   else if ((ctx->state & CTX_MASK) == CTX_INST_FILE)
496   {
497     /* ctx->ident should now hold the valid file */
498     assert (ctx->cfg   != NULL);
499     assert (ctx->inst  != NULL);
500     assert (ctx->ident != NULL);
501
502     inst_add_file (ctx->inst, ctx->ident);
503     ident_destroy (ctx->ident);
504     ctx->ident = NULL;
505
506     /* Don't reset the state here, files are in an array. */
507   }
508   else if ((ctx->state & CTX_MASK) == CTX_INST)
509   {
510     /* ctx->inst should now hold a complete instance */
511     assert (ctx->cfg   != NULL);
512     assert (ctx->inst  != NULL);
513     assert (ctx->ident == NULL);
514
515     graph_add_inst (ctx->cfg, ctx->inst);
516     /* don't destroy / free ctx->inst */
517     ctx->inst = NULL;
518
519     /* Don't reset the state here, instances are in an array. */
520   }
521   else if ((ctx->state & CTX_MASK) == CTX_GRAPH)
522   {
523     /* ctx->cfg should now hold a complete graph */
524     assert (ctx->cfg   != NULL);
525     assert (ctx->inst  == NULL);
526     assert (ctx->ident == NULL);
527
528     if (ctx->dynamic_graph)
529       gl_add_graph_internal (ctx->cfg, &gl_dynamic, &gl_dynamic_num);
530     /* else: already contained in gl_active */
531     ctx->cfg = NULL;
532
533     /* Don't reset the state here, graphs are in an array. */
534   }
535
536   return (1);
537 } /* }}} int gl_json_end_map */
538
539 static int gl_json_end_array (void *user_data) /* {{{ */
540 {
541   gl_json_context_t *ctx = user_data;
542
543   if ((ctx->state & CTX_MASK) == CTX_INST_FILE)
544     set_state (ctx, CTX_INST, CTX_MASK);
545   else if ((ctx->state & CTX_MASK) == CTX_INST)
546     set_state (ctx, CTX_GRAPH, CTX_MASK);
547   else if ((ctx->state & CTX_MASK) == CTX_GRAPH)
548   {
549     /* We're done */
550   }
551
552   return (1);
553 } /* }}} int gl_json_end_array */
554
555 static int gl_json_key (void *user_data, /* {{{ */
556     const unsigned char *str,
557     unsigned int str_length)
558 {
559   gl_json_context_t *ctx = user_data;
560   char buffer[str_length + 1];
561
562   memcpy (buffer, str, str_length);
563   buffer[str_length] = 0;
564
565   if ((ctx->state & CTX_MASK) == CTX_GRAPH)
566   {
567     if (strcasecmp ("select", buffer) == 0)
568       set_state (ctx, CTX_GRAPH_SELECT, CTX_MASK);
569     else if (strcasecmp ("instances", buffer) == 0)
570       set_state (ctx, CTX_INST, CTX_MASK);
571   }
572   else if ((ctx->state & CTX_MASK) == CTX_INST)
573   {
574     if (strcasecmp ("select", buffer) == 0)
575       set_state (ctx, CTX_INST_SELECT, CTX_MASK);
576     else if (strcasecmp ("files", buffer) == 0)
577       set_state (ctx, CTX_INST_FILE, CTX_MASK);
578   }
579   else if (((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
580       || ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
581       || ((ctx->state & CTX_MASK) == CTX_INST_FILE))
582   {
583     assert (ctx->ident != NULL);
584
585     if (strcasecmp ("host", buffer) == 0)
586       set_state (ctx, CTX_IDENT_HOST, CTX_IDENT_MASK);
587     else if (strcasecmp ("plugin", buffer) == 0)
588       set_state (ctx, CTX_IDENT_PLUGIN, CTX_IDENT_MASK);
589     else if (strcasecmp ("plugin_instance", buffer) == 0)
590       set_state (ctx, CTX_IDENT_PLUGIN_INSTANCE, CTX_IDENT_MASK);
591     else if (strcasecmp ("type", buffer) == 0)
592       set_state (ctx, CTX_IDENT_TYPE, CTX_IDENT_MASK);
593     else if (strcasecmp ("type_instance", buffer) == 0)
594       set_state (ctx, CTX_IDENT_TYPE_INSTANCE, CTX_IDENT_MASK);
595   }
596
597   return (1);
598 } /* }}} int gl_json_key */
599
600 yajl_callbacks gl_json_callbacks =
601 {
602   /*        null = */ NULL,
603   /*     boolean = */ NULL,
604   /*     integer = */ NULL,
605   /*      double = */ NULL,
606   /*      number = */ NULL,
607   /*      string = */ gl_json_string,
608   /*   start_map = */ gl_json_start_map,
609   /*     map_key = */ gl_json_key,
610   /*     end_map = */ gl_json_end_map,
611   /* start_array = */ NULL,
612   /*   end_array = */ gl_json_end_array
613 };
614
615 static int gl_read_cache (_Bool block) /* {{{ */
616 {
617   yajl_handle handle;
618   gl_json_context_t context;
619   yajl_parser_config handle_config = { /* comments = */ 0, /* check UTF-8 */ 0 };
620
621   int fd;
622   int cmd;
623   struct stat statbuf;
624   struct flock lock;
625   int status;
626   time_t now;
627
628   fd = open (CACHE_FILE, O_RDONLY);
629   if (fd < 0)
630   {
631     fprintf (stderr, "gl_read_cache: open(2) failed with status %i\n", errno);
632     return (errno);
633   }
634
635   if (block)
636     cmd = F_SETLKW;
637   else
638     cmd = F_SETLK;
639
640   memset (&lock, 0, sizeof (lock));
641   lock.l_type = F_RDLCK;
642   lock.l_whence = SEEK_SET;
643   lock.l_start = 0;
644   lock.l_len = 0; /* lock everything */
645
646   while (42)
647   {
648     status = fcntl (fd, cmd, &lock);
649     if (status == 0)
650       break;
651
652     if (!block && ((errno == EACCES) || (errno == EAGAIN)))
653     {
654       close (fd);
655       return (0);
656     }
657
658     if (errno == EINTR)
659       continue;
660
661     status = errno;
662     fprintf (stderr, "gl_read_cache: fcntl(2) failed with status %i\n",
663         status);
664     close (fd);
665     return (status);
666   }
667
668   fprintf (stderr, "gl_read_cache: Opening and locking "
669       "cache file successful\n");
670
671   memset (&statbuf, 0, sizeof (statbuf));
672   status = fstat (fd, &statbuf);
673   if (status != 0)
674   {
675     status = errno;
676     fprintf (stderr, "gl_read_cache: fstat(2) failed with status %i\n",
677         status);
678     close (fd);
679     return (status);
680   }
681
682   now = time (NULL);
683
684   if (block)
685   {
686     /* Read the file. No excuses. */
687   }
688   else if (statbuf.st_mtime <= gl_last_update)
689   {
690     /* Our current data is at least as new as the cache. Return. */
691     fprintf (stderr, "gl_read_cache: Not using cache because "
692         "the internal data is newer\n");
693     fflush (stderr);
694     close (fd);
695     return (0);
696   }
697   else if ((statbuf.st_mtime + UPDATE_INTERVAL) < now)
698   {
699     /* We'll scan the directory anyway, so there is no need to parse the cache
700      * here. */
701     fprintf (stderr, "gl_read_cache: Not using cache because it's too old\n");
702     fflush (stderr);
703     close (fd);
704     return (0);
705   }
706
707   memset (&context, 0, sizeof (context));
708   context.state = CTX_GRAPH;
709   context.cfg = NULL;
710   context.inst = NULL;
711   context.ident = NULL;
712
713   handle = yajl_alloc (&gl_json_callbacks,
714       &handle_config,
715       /* alloc funcs = */ NULL,
716       &context);
717
718   fprintf (stderr, "gl_read_cache: Start parsing data\n");
719   fflush (stderr);
720
721   while (42)
722   {
723     ssize_t rd_status;
724     char buffer[1024*1024];
725
726     rd_status = read (fd, buffer, sizeof (buffer));
727     if (rd_status < 0)
728     {
729       if ((errno == EINTR) || (errno == EAGAIN))
730         continue;
731
732       status = errno;
733       fprintf (stderr, "gl_read_cache: read(2) failed with status %i\n",
734           status);
735       close (fd);
736       return (status);
737     }
738     else if (rd_status == 0)
739     {
740       yajl_parse_complete (handle);
741       break;
742     }
743     else
744     {
745       yajl_parse (handle,
746           (unsigned char *) &buffer[0],
747           (unsigned int) rd_status);
748     }
749   }
750
751   yajl_free (handle);
752
753   gl_last_update = statbuf.st_mtime;
754   close (fd);
755
756   fprintf (stderr, "gl_read_cache: Finished parsing data\n");
757   fflush (stderr);
758
759   return (0);
760 } /* }}} int gl_read_cache */
761
762 /*
763  * Global functions
764  */
765 int gl_add_graph (graph_config_t *cfg) /* {{{ */
766 {
767   return (gl_add_graph_internal (cfg, &gl_staging, &gl_staging_num));
768 } /* }}} int gl_add_graph */
769
770 int gl_config_submit (void) /* {{{ */
771 {
772   graph_config_t **old;
773   size_t old_num;
774
775   old = gl_active;
776   old_num = gl_active_num;
777
778   gl_active = gl_staging;
779   gl_active_num = gl_staging_num;
780
781   gl_staging = NULL;
782   gl_staging_num = 0;
783
784   gl_destroy (&old, &old_num);
785
786   return (0);
787 } /* }}} int graph_config_submit */
788
789 int gl_register_data_provider (const char *name, data_provider_t *p) /* {{{ */
790 {
791   fprintf (stderr, "gl_register_data_provider (name = %s, ptr = %p)\n",
792       name, (void *) p);
793
794   if (data_provider == NULL)
795     data_provider = malloc (sizeof (*data_provider));
796   if (data_provider == NULL)
797     return (ENOMEM);
798
799   *data_provider = *p;
800
801   return (0);
802 } /* }}} int gl_register_data_provider */
803
804 int gl_graph_get_all (_Bool include_dynamic, /* {{{ */
805     graph_callback_t callback, void *user_data)
806 {
807   size_t i;
808
809   if (callback == NULL)
810     return (EINVAL);
811
812   gl_update (/* request served = */ 0);
813
814   for (i = 0; i < gl_active_num; i++)
815   {
816     int status;
817
818     status = (*callback) (gl_active[i], user_data);
819     if (status != 0)
820       return (status);
821   }
822
823   if (!include_dynamic)
824     return (0);
825
826   for (i = 0; i < gl_dynamic_num; i++)
827   {
828     int status;
829
830     status = (*callback) (gl_dynamic[i], user_data);
831     if (status != 0)
832       return (status);
833   }
834
835   return (0);
836 } /* }}} int gl_graph_get_all */
837
838 graph_config_t *gl_graph_get_selected (void) /* {{{ */
839 {
840   const char *host = get_part_from_param ("graph_host", "host");
841   const char *plugin = get_part_from_param ("graph_plugin", "plugin");
842   const char *plugin_instance = get_part_from_param ("graph_plugin_instance", "plugin_instance");
843   const char *type = get_part_from_param ("graph_type", "type");
844   const char *type_instance = get_part_from_param ("graph_type_instance", "type_instance");
845   graph_ident_t *ident;
846   size_t i;
847
848   if ((host == NULL)
849       || (plugin == NULL) || (plugin_instance == NULL)
850       || (type == NULL) || (type_instance == NULL))
851     return (NULL);
852
853   ident = ident_create (host, plugin, plugin_instance, type, type_instance);
854
855   gl_update (/* request served = */ 0);
856
857   for (i = 0; i < gl_active_num; i++)
858   {
859     if (graph_compare (gl_active[i], ident) != 0)
860       continue;
861
862     ident_destroy (ident);
863     return (gl_active[i]);
864   }
865
866   for (i = 0; i < gl_dynamic_num; i++)
867   {
868     if (graph_compare (gl_dynamic[i], ident) != 0)
869       continue;
870
871     ident_destroy (ident);
872     return (gl_dynamic[i]);
873   }
874
875   ident_destroy (ident);
876   return (NULL);
877 } /* }}} graph_config_t *gl_graph_get_selected */
878
879 /* gl_instance_get_all, gl_graph_instance_get_all {{{ */
880 struct gl_inst_callback_data /* {{{ */
881 {
882   graph_config_t *cfg;
883   graph_inst_callback_t callback;
884   void *user_data;
885 }; /* }}} struct gl_inst_callback_data */
886
887 static int gl_inst_callback_handler (graph_instance_t *inst, /* {{{ */
888     void *user_data)
889 {
890   struct gl_inst_callback_data *data = user_data;
891
892   return ((*data->callback) (data->cfg, inst, data->user_data));
893 } /* }}} int gl_inst_callback_handler */
894
895 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
896     graph_inst_callback_t callback, void *user_data)
897 {
898   struct gl_inst_callback_data data =
899   {
900     cfg,
901     callback,
902     user_data
903   };
904
905   if ((cfg == NULL) || (callback == NULL))
906     return (EINVAL);
907
908   return (graph_inst_foreach (cfg, gl_inst_callback_handler, &data));
909 } /* }}} int gl_graph_instance_get_all */
910
911 int gl_instance_get_all (graph_inst_callback_t callback, /* {{{ */
912     void *user_data)
913 {
914   size_t i;
915
916   gl_update (/* request served = */ 0);
917
918   for (i = 0; i < gl_active_num; i++)
919   {
920     int status;
921
922     status = gl_graph_instance_get_all (gl_active[i], callback, user_data);
923     if (status != 0)
924       return (status);
925   }
926
927   for (i = 0; i < gl_dynamic_num; i++)
928   {
929     int status;
930
931     status = gl_graph_instance_get_all (gl_dynamic[i], callback, user_data);
932     if (status != 0)
933       return (status);
934   }
935
936   return (0);
937 } /* }}} int gl_instance_get_all */
938 /* }}} gl_instance_get_all, gl_graph_instance_get_all */
939
940 int gl_search (search_info_t *si, /* {{{ */
941     graph_inst_callback_t callback, void *user_data)
942 {
943   size_t i;
944   graph_ident_t *ident;
945
946   if ((si == NULL) || (callback == NULL))
947     return (EINVAL);
948
949   if (search_has_selector (si))
950   {
951     ident = search_to_ident (si);
952     if (ident == NULL)
953     {
954       fprintf (stderr, "gl_search: search_to_ident failed\n");
955       return (-1);
956     }
957   }
958   else
959   {
960     ident = NULL;
961   }
962
963   for (i = 0; i < gl_active_num; i++)
964   {
965     int status;
966
967     if ((ident != NULL) && !graph_ident_intersect (gl_active[i], ident))
968       continue;
969
970     status = graph_search_inst (gl_active[i], si,
971         /* callback  = */ callback,
972         /* user data = */ user_data);
973     if (status != 0)
974       return (status);
975   }
976
977   for (i = 0; i < gl_dynamic_num; i++)
978   {
979     int status;
980
981     if ((ident != NULL) && !graph_ident_intersect (gl_dynamic[i], ident))
982       continue;
983
984     status = graph_search_inst (gl_dynamic[i], si,
985         /* callback  = */ callback,
986         /* user data = */ user_data);
987     if (status != 0)
988       return (status);
989   }
990
991   return (0);
992 } /* }}} int gl_search */
993
994 int gl_search_string (const char *term, graph_inst_callback_t callback, /* {{{ */
995     void *user_data)
996 {
997   size_t i;
998
999   for (i = 0; i < gl_active_num; i++)
1000   {
1001     int status;
1002
1003     status = graph_search_inst_string (gl_active[i], term,
1004         /* callback  = */ callback,
1005         /* user data = */ user_data);
1006     if (status != 0)
1007       return (status);
1008   }
1009
1010   for (i = 0; i < gl_dynamic_num; i++)
1011   {
1012     int status;
1013
1014     status = graph_search_inst_string (gl_dynamic[i], term,
1015         /* callback  = */ callback,
1016         /* user data = */ user_data);
1017     if (status != 0)
1018       return (status);
1019   }
1020
1021   return (0);
1022 } /* }}} int gl_search_string */
1023
1024 int gl_search_field (graph_ident_field_t field, /* {{{ */
1025     const char *field_value,
1026     graph_inst_callback_t callback, void *user_data)
1027 {
1028   size_t i;
1029
1030   if ((field_value == NULL) || (callback == NULL))
1031     return (EINVAL);
1032
1033   for (i = 0; i < gl_active_num; i++)
1034   {
1035     int status;
1036
1037     status = graph_inst_search_field (gl_active[i],
1038         field, field_value,
1039         /* callback  = */ callback,
1040         /* user data = */ user_data);
1041     if (status != 0)
1042       return (status);
1043   }
1044
1045   for (i = 0; i < gl_dynamic_num; i++)
1046   {
1047     int status;
1048
1049     status = graph_inst_search_field (gl_dynamic[i],
1050         field, field_value,
1051         /* callback  = */ callback,
1052         /* user data = */ user_data);
1053     if (status != 0)
1054       return (status);
1055   }
1056
1057   return (0);
1058 } /* }}} int gl_search_field */
1059
1060 int gl_foreach_host (int (*callback) (const char *host, void *user_data), /* {{{ */
1061     void *user_data)
1062 {
1063   int status;
1064   size_t i;
1065
1066   for (i = 0; i < host_list_len; i++)
1067   {
1068     status = (*callback) (host_list[i], user_data);
1069     if (status != 0)
1070       return (status);
1071   }
1072
1073   return (0);
1074 } /* }}} int gl_foreach_host */
1075
1076 int gl_update (_Bool request_served) /* {{{ */
1077 {
1078   time_t now;
1079   int status;
1080   size_t i;
1081
1082   if (!request_served && (gl_last_update > 0))
1083     return (0);
1084
1085   now = time (NULL);
1086
1087   if ((gl_last_update + UPDATE_INTERVAL) >= now)
1088   {
1089     /* Write data to cache if appropriate */
1090     if (request_served)
1091       gl_update_cache ();
1092     return (0);
1093   }
1094
1095   /* Clear state */
1096   gl_clear_instances ();
1097   gl_clear_hosts ();
1098   gl_destroy (&gl_dynamic, &gl_dynamic_num);
1099
1100   graph_read_config ();
1101
1102   status = gl_read_cache (/* block = */ 1);
1103   /* We have *something* to work with. Even if it's outdated, just get on with
1104    * handling the request and take care of re-reading data later on. */
1105   if ((status == 0) && !request_served)
1106     return (0);
1107
1108   if ((status != 0)
1109       || ((gl_last_update + UPDATE_INTERVAL) < now))
1110   {
1111     if (data_provider == NULL)
1112     {
1113       fprintf (stderr, "No data provider has been registered\n");
1114       return (ENOENT);
1115     }
1116
1117     /* Clear state */
1118     gl_clear_instances ();
1119     gl_clear_hosts ();
1120     gl_destroy (&gl_dynamic, &gl_dynamic_num);
1121
1122     /* TODO: Iterate over all data providers */
1123     data_provider->get_idents (data_provider->private_data,
1124         gl_register_ident, /* user data = */ NULL);
1125
1126     gl_last_update = now;
1127   }
1128
1129   if (host_list_len > 0)
1130     qsort (host_list, host_list_len, sizeof (*host_list),
1131         gl_compare_hosts);
1132
1133   for (i = 0; i < gl_active_num; i++)
1134     graph_sort_instances (gl_active[i]);
1135
1136   if (request_served)
1137     gl_update_cache ();
1138
1139   return (status);
1140 } /* }}} int gl_update */
1141
1142 /* vim: set sw=2 sts=2 et fdm=marker : */