src/graph_list.c: Supply create mode to open(2).
[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_dump (void) /* {{{ */
259 {
260   int fd;
261   yajl_gen handler;
262   yajl_gen_config handler_config = { /* pretty = */ 1, /* indent = */ "  " };
263   struct flock lock;
264   int status;
265   size_t i;
266
267   fd = open (CACHE_FILE, O_WRONLY | O_TRUNC | O_CREAT,
268       S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
269   if (fd < 0)
270   {
271     fprintf (stderr, "gl_dump: open(2) failed with status %i\n", errno);
272     return (errno);
273   }
274
275   memset (&lock, 0, sizeof (lock));
276   lock.l_type = F_WRLCK;
277   lock.l_whence = SEEK_SET;
278   lock.l_start = 0;
279   lock.l_len = 0; /* lock everything */
280
281   while (42)
282   {
283     status = fcntl (fd, F_SETLKW, &lock);
284     if (status == 0)
285       break;
286
287     if (errno == EINTR)
288       continue;
289
290     fprintf (stderr, "gl_dump: fcntl(2) failed with status %i\n", errno);
291     close (fd);
292     return (errno);
293   }
294
295   handler = yajl_gen_alloc2 (gl_dump_cb, &handler_config,
296       /* alloc funcs = */ NULL, /* ctx = */ &fd);
297   if (handler == NULL)
298   {
299     close (fd);
300     return (-1);
301   }
302
303   yajl_gen_array_open (handler);
304
305   for (i = 0; i < gl_active_num; i++)
306     graph_to_json (gl_active[i], handler);
307
308   for (i = 0; i < gl_dynamic_num; i++)
309     graph_to_json (gl_dynamic[i], handler);
310
311   yajl_gen_array_close (handler);
312
313   yajl_gen_free (handler);
314   close (fd);
315
316   return (0);
317 } /* }}} int gl_dump */
318
319 static int gl_scan_directory (void)
320 {
321   return (-1);
322 } /* }}} int gl_scan_directory */
323
324 /*
325  * JSON parsing functions
326  */
327 #define CTX_MASK                  0xff000000
328 #define CTX_GRAPH                 0x01000000
329 #define CTX_GRAPH_SELECT          0x02000000
330 #define CTX_INST                  0x03000000
331 #define CTX_INST_SELECT           0x04000000
332 #define CTX_INST_FILE             0x05000000
333
334 #define CTX_IDENT_MASK            0x00ff0000
335 #define CTX_IDENT_HOST            0x00010000
336 #define CTX_IDENT_PLUGIN          0x00020000
337 #define CTX_IDENT_PLUGIN_INSTANCE 0x00030000
338 #define CTX_IDENT_TYPE            0x00040000
339 #define CTX_IDENT_TYPE_INSTANCE   0x00050000
340
341 struct gl_json_context_s
342 {
343   uint32_t          state;
344
345   graph_config_t   *cfg;
346   graph_instance_t *inst;
347   graph_ident_t    *ident;
348
349   _Bool             dynamic_graph;
350 };
351 typedef struct gl_json_context_s gl_json_context_t;
352
353 static void set_state (gl_json_context_t *ctx, /* {{{ */
354     uint32_t new_state, uint32_t mask)
355 {
356   uint32_t old_state = ctx->state;
357   ctx->state = (old_state & ~mask) | (new_state & mask);
358 } /* }}} void set_state */
359
360 static int gl_json_string (void *user_data, /* {{{ */
361     const unsigned char *str,
362     unsigned int str_length)
363 {
364   gl_json_context_t *ctx = user_data;
365   char buffer[str_length + 1];
366
367   memcpy (buffer, str, str_length);
368   buffer[str_length] = 0;
369
370   if (((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
371       || ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
372       || ((ctx->state & CTX_MASK) == CTX_INST_FILE))
373   {
374     switch (ctx->state & CTX_IDENT_MASK)
375     {
376       case CTX_IDENT_HOST:
377         ident_set_host (ctx->ident, buffer);
378         break;
379       case CTX_IDENT_PLUGIN:
380         ident_set_plugin (ctx->ident, buffer);
381         break;
382       case CTX_IDENT_PLUGIN_INSTANCE:
383         ident_set_plugin_instance (ctx->ident, buffer);
384         break;
385       case CTX_IDENT_TYPE:
386         ident_set_type (ctx->ident, buffer);
387         break;
388       case CTX_IDENT_TYPE_INSTANCE:
389         ident_set_type_instance (ctx->ident, buffer);
390         break;
391     }
392   }
393
394   return (1);
395 } /* }}} int gl_json_string */
396
397 static int gl_json_start_map (void *user_data) /* {{{ */
398 {
399   gl_json_context_t *ctx = user_data;
400
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       assert (ctx->ident == NULL);
406       ctx->ident = ident_create (ANY_TOKEN,
407           ANY_TOKEN, ANY_TOKEN,
408           ANY_TOKEN, ANY_TOKEN);
409   }
410
411   return (1);
412 } /* }}} int gl_json_start_map */
413
414 static int gl_json_end_map (void *user_data) /* {{{ */
415 {
416   gl_json_context_t *ctx = user_data;
417
418   if ((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
419   {
420     size_t i;
421
422     /* ctx->ident should now hold the valid selector */
423     assert (ctx->cfg == NULL);
424     assert (ctx->inst  == NULL);
425     assert (ctx->ident != NULL);
426
427     for (i = 0; i < gl_active_num; i++)
428     {
429       if (graph_compare (gl_active[i], ctx->ident) != 0)
430         continue;
431
432       ctx->cfg = gl_active[i];
433       ctx->dynamic_graph = 0;
434       break;
435     }
436
437     if (ctx->cfg == NULL)
438     {
439       ctx->cfg = graph_create (ctx->ident);
440       ctx->dynamic_graph = 1;
441     }
442
443     ident_destroy (ctx->ident);
444     ctx->ident = NULL;
445
446     set_state (ctx, CTX_GRAPH, CTX_MASK);
447   }
448   else if ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
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     ctx->inst = inst_create (ctx->cfg, ctx->ident);
456     ident_destroy (ctx->ident);
457     ctx->ident = NULL;
458
459     set_state (ctx, CTX_INST, CTX_MASK);
460   }
461   else if ((ctx->state & CTX_MASK) == CTX_INST_FILE)
462   {
463     /* ctx->ident should now hold the valid file */
464     assert (ctx->cfg   != NULL);
465     assert (ctx->inst  != NULL);
466     assert (ctx->ident != NULL);
467
468     inst_add_file (ctx->inst, ctx->ident);
469     ident_destroy (ctx->ident);
470     ctx->ident = NULL;
471
472     /* Don't reset the state here, files are in an array. */
473   }
474   else if ((ctx->state & CTX_MASK) == CTX_INST)
475   {
476     /* ctx->inst should now hold a complete instance */
477     assert (ctx->cfg   != NULL);
478     assert (ctx->inst  != NULL);
479     assert (ctx->ident == NULL);
480
481     graph_add_inst (ctx->cfg, ctx->inst);
482     /* don't destroy / free ctx->inst */
483     ctx->inst = NULL;
484
485     /* Don't reset the state here, instances are in an array. */
486   }
487   else if ((ctx->state & CTX_MASK) == CTX_GRAPH)
488   {
489     /* ctx->cfg should now hold a complete graph */
490     assert (ctx->cfg   != NULL);
491     assert (ctx->inst  == NULL);
492     assert (ctx->ident == NULL);
493
494     if (ctx->dynamic_graph)
495       gl_add_graph_internal (ctx->cfg, &gl_dynamic, &gl_dynamic_num);
496     /* else: already contained in gl_active */
497     ctx->cfg = NULL;
498
499     /* Don't reset the state here, graphs are in an array. */
500   }
501
502   return (1);
503 } /* }}} int gl_json_end_map */
504
505 static int gl_json_end_array (void *user_data) /* {{{ */
506 {
507   gl_json_context_t *ctx = user_data;
508
509   if ((ctx->state & CTX_MASK) == CTX_INST_FILE)
510     set_state (ctx, CTX_INST, CTX_MASK);
511   else if ((ctx->state & CTX_MASK) == CTX_INST)
512     set_state (ctx, CTX_GRAPH, CTX_MASK);
513   else if ((ctx->state & CTX_MASK) == CTX_GRAPH)
514   {
515     /* We're done */
516   }
517
518   return (1);
519 } /* }}} int gl_json_end_array */
520
521 static int gl_json_key (void *user_data, /* {{{ */
522     const unsigned char *str,
523     unsigned int str_length)
524 {
525   gl_json_context_t *ctx = user_data;
526   char buffer[str_length + 1];
527
528   memcpy (buffer, str, str_length);
529   buffer[str_length] = 0;
530
531   if ((ctx->state & CTX_MASK) == CTX_GRAPH)
532   {
533     if (strcasecmp ("select", buffer) == 0)
534       set_state (ctx, CTX_GRAPH_SELECT, CTX_MASK);
535     else if (strcasecmp ("instances", buffer) == 0)
536       set_state (ctx, CTX_INST, CTX_MASK);
537   }
538   else if ((ctx->state & CTX_MASK) == CTX_INST)
539   {
540     if (strcasecmp ("select", buffer) == 0)
541       set_state (ctx, CTX_INST_SELECT, CTX_MASK);
542     else if (strcasecmp ("files", buffer) == 0)
543       set_state (ctx, CTX_INST_FILE, CTX_MASK);
544   }
545   else if (((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
546       || ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
547       || ((ctx->state & CTX_MASK) == CTX_INST_FILE))
548   {
549     assert (ctx->ident != NULL);
550
551     if (strcasecmp ("host", buffer) == 0)
552       set_state (ctx, CTX_IDENT_HOST, CTX_IDENT_MASK);
553     else if (strcasecmp ("plugin", buffer) == 0)
554       set_state (ctx, CTX_IDENT_PLUGIN, CTX_IDENT_MASK);
555     else if (strcasecmp ("plugin_instance", buffer) == 0)
556       set_state (ctx, CTX_IDENT_PLUGIN_INSTANCE, CTX_IDENT_MASK);
557     else if (strcasecmp ("type", buffer) == 0)
558       set_state (ctx, CTX_IDENT_TYPE, CTX_IDENT_MASK);
559     else if (strcasecmp ("type_instance", buffer) == 0)
560       set_state (ctx, CTX_IDENT_TYPE_INSTANCE, CTX_IDENT_MASK);
561   }
562
563   return (1);
564 } /* }}} int gl_json_key */
565
566 yajl_callbacks gl_json_callbacks =
567 {
568   /*        null = */ NULL,
569   /*     boolean = */ NULL,
570   /*     integer = */ NULL,
571   /*      double = */ NULL,
572   /*      number = */ NULL,
573   /*      string = */ gl_json_string,
574   /*   start_map = */ gl_json_start_map,
575   /*     map_key = */ gl_json_key,
576   /*     end_map = */ gl_json_end_map,
577   /* start_array = */ NULL,
578   /*   end_array = */ gl_json_end_array
579 };
580
581 static int gl_read_cache (_Bool block) /* {{{ */
582 {
583   yajl_handle handle;
584   gl_json_context_t context;
585   yajl_parser_config handle_config = { /* comments = */ 0, /* check UTF-8 */ 0 };
586
587   int fd;
588   int cmd;
589   struct stat statbuf;
590   struct flock lock;
591   int status;
592   time_t now;
593
594   fd = open (CACHE_FILE, O_RDONLY);
595   if (fd < 0)
596   {
597     fprintf (stderr, "gl_read_cache: open(2) failed with status %i\n", errno);
598     return (errno);
599   }
600
601   if (block)
602     cmd = F_SETLKW;
603   else
604     cmd = F_SETLK;
605
606   memset (&lock, 0, sizeof (lock));
607   lock.l_type = F_RDLCK;
608   lock.l_whence = SEEK_SET;
609   lock.l_start = 0;
610   lock.l_len = 0; /* lock everything */
611
612   while (42)
613   {
614     status = fcntl (fd, cmd, &lock);
615     if (status == 0)
616       break;
617
618     if (!block && ((errno == EACCES) || (errno == EAGAIN)))
619     {
620       close (fd);
621       return (0);
622     }
623
624     if (errno == EINTR)
625       continue;
626
627     status = errno;
628     fprintf (stderr, "gl_read_cache: fcntl(2) failed with status %i\n",
629         status);
630     close (fd);
631     return (status);
632   }
633
634   fprintf (stderr, "gl_read_cache: Opening and locking "
635       "cache file successful\n");
636
637   memset (&statbuf, 0, sizeof (statbuf));
638   status = fstat (fd, &statbuf);
639   if (status != 0)
640   {
641     status = errno;
642     fprintf (stderr, "gl_read_cache: fstat(2) failed with status %i\n",
643         status);
644     close (fd);
645     return (status);
646   }
647
648   now = time (NULL);
649
650   if (statbuf.st_mtime <= gl_last_update)
651   {
652     /* Our current data is at least as new as the cache. Return. */
653     close (fd);
654     return (0);
655   }
656   else if ((statbuf.st_mtime + UPDATE_INTERVAL) < now)
657   {
658     /* We'll scan the directory anyway, so there is no need to parse the cache here. */
659     close (fd);
660     return (0);
661   }
662
663   memset (&context, 0, sizeof (context));
664   context.state = CTX_GRAPH;
665   context.cfg = NULL;
666   context.inst = NULL;
667   context.ident = NULL;
668
669   handle = yajl_alloc (&gl_json_callbacks,
670       &handle_config,
671       /* alloc funcs = */ NULL,
672       &context);
673
674   while (42)
675   {
676     ssize_t rd_status;
677     char buffer[1024*1024];
678
679     rd_status = read (fd, buffer, sizeof (buffer));
680     if (rd_status < 0)
681     {
682       if ((errno == EINTR) || (errno == EAGAIN))
683         continue;
684
685       fprintf (stderr, "gl_read_cache: read(2) failed with status %i\n",
686           errno);
687       close (fd);
688       return (errno);
689     }
690     else if (rd_status == 0)
691     {
692       yajl_parse_complete (handle);
693       break;
694     }
695     else
696     {
697       yajl_parse (handle,
698           (unsigned char *) &buffer[0],
699           (unsigned int) rd_status);
700     }
701   }
702
703   fprintf (stderr, "gl_read_cache: Closing cache file and returning\n");
704   gl_last_update = statbuf.st_mtime;
705   close (fd);
706
707   return (0);
708 } /* }}} int gl_read_cache */
709
710 /*
711  * Global functions
712  */
713 int gl_add_graph (graph_config_t *cfg) /* {{{ */
714 {
715   return (gl_add_graph_internal (cfg, &gl_staging, &gl_staging_num));
716 } /* }}} int gl_add_graph */
717
718 int gl_config_submit (void) /* {{{ */
719 {
720   graph_config_t **old;
721   size_t old_num;
722
723   old = gl_active;
724   old_num = gl_active_num;
725
726   gl_active = gl_staging;
727   gl_active_num = gl_staging_num;
728
729   gl_staging = NULL;
730   gl_staging_num = 0;
731
732   gl_destroy (&old, &old_num);
733
734   return (0);
735 } /* }}} int graph_config_submit */
736
737 int gl_graph_get_all (graph_callback_t callback, /* {{{ */
738     void *user_data)
739 {
740   size_t i;
741
742   if (callback == NULL)
743     return (EINVAL);
744
745   gl_update (/* request served = */ 0);
746
747   for (i = 0; i < gl_active_num; i++)
748   {
749     int status;
750
751     status = (*callback) (gl_active[i], user_data);
752     if (status != 0)
753       return (status);
754   }
755
756   for (i = 0; i < gl_dynamic_num; i++)
757   {
758     int status;
759
760     status = (*callback) (gl_dynamic[i], user_data);
761     if (status != 0)
762       return (status);
763   }
764
765   return (0);
766 } /* }}} int gl_graph_get_all */
767
768 graph_config_t *gl_graph_get_selected (void) /* {{{ */
769 {
770   const char *host = get_part_from_param ("graph_host", "host");
771   const char *plugin = get_part_from_param ("graph_plugin", "plugin");
772   const char *plugin_instance = get_part_from_param ("graph_plugin_instance", "plugin_instance");
773   const char *type = get_part_from_param ("graph_type", "type");
774   const char *type_instance = get_part_from_param ("graph_type_instance", "type_instance");
775   graph_ident_t *ident;
776   size_t i;
777
778   if ((host == NULL)
779       || (plugin == NULL) || (plugin_instance == NULL)
780       || (type == NULL) || (type_instance == NULL))
781     return (NULL);
782
783   ident = ident_create (host, plugin, plugin_instance, type, type_instance);
784
785   gl_update (/* request served = */ 0);
786
787   for (i = 0; i < gl_active_num; i++)
788   {
789     if (graph_compare (gl_active[i], ident) != 0)
790       continue;
791
792     ident_destroy (ident);
793     return (gl_active[i]);
794   }
795
796   for (i = 0; i < gl_dynamic_num; i++)
797   {
798     if (graph_compare (gl_dynamic[i], ident) != 0)
799       continue;
800
801     ident_destroy (ident);
802     return (gl_dynamic[i]);
803   }
804
805   ident_destroy (ident);
806   return (NULL);
807 } /* }}} graph_config_t *gl_graph_get_selected */
808
809 /* gl_instance_get_all, gl_graph_instance_get_all {{{ */
810 struct gl_inst_callback_data /* {{{ */
811 {
812   graph_config_t *cfg;
813   graph_inst_callback_t callback;
814   void *user_data;
815 }; /* }}} struct gl_inst_callback_data */
816
817 static int gl_inst_callback_handler (graph_instance_t *inst, /* {{{ */
818     void *user_data)
819 {
820   struct gl_inst_callback_data *data = user_data;
821
822   return ((*data->callback) (data->cfg, inst, data->user_data));
823 } /* }}} int gl_inst_callback_handler */
824
825 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
826     graph_inst_callback_t callback, void *user_data)
827 {
828   struct gl_inst_callback_data data =
829   {
830     cfg,
831     callback,
832     user_data
833   };
834
835   if ((cfg == NULL) || (callback == NULL))
836     return (EINVAL);
837
838   return (graph_inst_foreach (cfg, gl_inst_callback_handler, &data));
839 } /* }}} int gl_graph_instance_get_all */
840
841 int gl_instance_get_all (graph_inst_callback_t callback, /* {{{ */
842     void *user_data)
843 {
844   size_t i;
845
846   gl_update (/* request served = */ 0);
847
848   for (i = 0; i < gl_active_num; i++)
849   {
850     int status;
851
852     status = gl_graph_instance_get_all (gl_active[i], callback, user_data);
853     if (status != 0)
854       return (status);
855   }
856
857   for (i = 0; i < gl_dynamic_num; i++)
858   {
859     int status;
860
861     status = gl_graph_instance_get_all (gl_dynamic[i], callback, user_data);
862     if (status != 0)
863       return (status);
864   }
865
866   return (0);
867 } /* }}} int gl_instance_get_all */
868 /* }}} gl_instance_get_all, gl_graph_instance_get_all */
869
870 int gl_search (search_info_t *si, /* {{{ */
871     graph_inst_callback_t callback, void *user_data)
872 {
873   size_t i;
874   graph_ident_t *ident;
875
876   if ((si == NULL) || (callback == NULL))
877     return (EINVAL);
878
879   if (search_has_selector (si))
880   {
881     ident = search_to_ident (si);
882     if (ident == NULL)
883     {
884       fprintf (stderr, "gl_search: search_to_ident failed\n");
885       return (-1);
886     }
887   }
888   else
889   {
890     ident = NULL;
891   }
892
893   for (i = 0; i < gl_active_num; i++)
894   {
895     int status;
896
897     if ((ident != NULL) && !graph_ident_intersect (gl_active[i], ident))
898       continue;
899
900     status = graph_search_inst (gl_active[i], si,
901         /* callback  = */ callback,
902         /* user data = */ user_data);
903     if (status != 0)
904       return (status);
905   }
906
907   for (i = 0; i < gl_dynamic_num; i++)
908   {
909     int status;
910
911     if ((ident != NULL) && !graph_ident_intersect (gl_dynamic[i], ident))
912       continue;
913
914     status = graph_search_inst (gl_dynamic[i], si,
915         /* callback  = */ callback,
916         /* user data = */ user_data);
917     if (status != 0)
918       return (status);
919   }
920
921   return (0);
922 } /* }}} int gl_search */
923
924 int gl_search_string (const char *term, graph_inst_callback_t callback, /* {{{ */
925     void *user_data)
926 {
927   size_t i;
928
929   for (i = 0; i < gl_active_num; i++)
930   {
931     int status;
932
933     status = graph_search_inst_string (gl_active[i], term,
934         /* callback  = */ callback,
935         /* user data = */ user_data);
936     if (status != 0)
937       return (status);
938   }
939
940   for (i = 0; i < gl_dynamic_num; i++)
941   {
942     int status;
943
944     status = graph_search_inst_string (gl_dynamic[i], term,
945         /* callback  = */ callback,
946         /* user data = */ user_data);
947     if (status != 0)
948       return (status);
949   }
950
951   return (0);
952 } /* }}} int gl_search_string */
953
954 int gl_search_field (graph_ident_field_t field, /* {{{ */
955     const char *field_value,
956     graph_inst_callback_t callback, void *user_data)
957 {
958   size_t i;
959
960   if ((field_value == NULL) || (callback == NULL))
961     return (EINVAL);
962
963   for (i = 0; i < gl_active_num; i++)
964   {
965     int status;
966
967     status = graph_inst_search_field (gl_active[i],
968         field, field_value,
969         /* callback  = */ callback,
970         /* user data = */ user_data);
971     if (status != 0)
972       return (status);
973   }
974
975   for (i = 0; i < gl_dynamic_num; i++)
976   {
977     int status;
978
979     status = graph_inst_search_field (gl_dynamic[i],
980         field, field_value,
981         /* callback  = */ callback,
982         /* user data = */ user_data);
983     if (status != 0)
984       return (status);
985   }
986
987   return (0);
988 } /* }}} int gl_search_field */
989
990 int gl_foreach_host (int (*callback) (const char *host, void *user_data), /* {{{ */
991     void *user_data)
992 {
993   int status;
994   size_t i;
995
996   for (i = 0; i < host_list_len; i++)
997   {
998     status = (*callback) (host_list[i], user_data);
999     if (status != 0)
1000       return (status);
1001   }
1002
1003   return (0);
1004 } /* }}} int gl_foreach_host */
1005
1006 int gl_update (_Bool request_served) /* {{{ */
1007 {
1008   time_t now;
1009   int status;
1010   size_t i;
1011
1012   if (!request_served && (gl_last_update > 0))
1013     return (0);
1014
1015   now = time (NULL);
1016
1017   if ((gl_last_update + UPDATE_INTERVAL) >= now)
1018     return (0);
1019
1020   /* Clear state */
1021   gl_clear_instances ();
1022   gl_clear_hosts ();
1023   gl_destroy (&gl_dynamic, &gl_dynamic_num);
1024
1025   graph_read_config ();
1026
1027   status = gl_read_cache (/* block = */ 1);
1028
1029   if ((status != 0)
1030       || ((gl_last_update + UPDATE_INTERVAL) >= now))
1031   {
1032     status = fs_scan (/* callback = */ gl_register_file,
1033         /* user data = */ NULL);
1034   }
1035
1036   if (host_list_len > 0)
1037     qsort (host_list, host_list_len, sizeof (*host_list),
1038         gl_compare_hosts);
1039
1040   gl_last_update = now;
1041
1042   for (i = 0; i < gl_active_num; i++)
1043     graph_sort_instances (gl_active[i]);
1044
1045   gl_dump ();
1046
1047   return (status);
1048 } /* }}} int gl_update */
1049
1050 /* vim: set sw=2 sts=2 et fdm=marker : */