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