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