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