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