src/graph_list.[ch]: Implement "gl_search".
[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
32 #include "graph_list.h"
33 #include "common.h"
34 #include "filesystem.h"
35 #include "graph.h"
36 #include "graph_config.h"
37 #include "graph_def.h"
38 #include "graph_ident.h"
39 #include "utils_cgi.h"
40 #include "utils_search.h"
41
42 #include <fcgiapp.h>
43 #include <fcgi_stdio.h>
44
45 /*
46  * Defines
47  */
48 #define UPDATE_INTERVAL 900
49
50 /*
51  * Global variables
52  */
53 static graph_config_t **gl_active = NULL;
54 static size_t gl_active_num = 0;
55
56 static graph_config_t **gl_staging = NULL;
57 static size_t gl_staging_num = 0;
58
59 /* Graphs created on-the-fly for files which don't match any existing graph
60  * definition. */
61 static graph_config_t **gl_dynamic = NULL;
62 static size_t gl_dynamic_num = 0;
63
64 static char **host_list = NULL;
65 static size_t host_list_len = 0;
66
67 static time_t gl_last_update = 0;
68
69 /*
70  * Private functions
71  */
72 static int gl_add_graph_internal (graph_config_t *cfg, /* {{{ */
73     graph_config_t ***gl_array, size_t *gl_array_num)
74 {
75   graph_config_t **tmp;
76
77 #define ARRAY_PTR  (*gl_array)
78 #define ARRAY_SIZE (*gl_array_num)
79
80   if (cfg == NULL)
81     return (EINVAL);
82
83   tmp = realloc (ARRAY_PTR, sizeof (*ARRAY_PTR) * (ARRAY_SIZE + 1));
84   if (tmp == NULL)
85     return (ENOMEM);
86   ARRAY_PTR = tmp;
87
88   ARRAY_PTR[ARRAY_SIZE] = cfg;
89   ARRAY_SIZE++;
90
91 #undef ARRAY_SIZE
92 #undef ARRAY_PTR
93
94   return (0);
95 } /* }}} int gl_add_graph_internal */
96
97 static void gl_destroy (graph_config_t ***gl_array, /* {{{ */
98     size_t *gl_array_num)
99 {
100   size_t i;
101
102   if ((gl_array == NULL) || (gl_array_num == NULL))
103     return;
104
105 #define ARRAY_PTR  (*gl_array)
106 #define ARRAY_SIZE (*gl_array_num)
107
108   for (i = 0; i < ARRAY_SIZE; i++)
109   {
110     graph_destroy (ARRAY_PTR[i]);
111     ARRAY_PTR[i] = NULL;
112   }
113   free (ARRAY_PTR);
114   ARRAY_PTR = NULL;
115   ARRAY_SIZE = 0;
116
117 #undef ARRAY_SIZE
118 #undef ARRAY_PTR
119 } /* }}} void gl_destroy */
120
121 static int gl_register_host (const char *host) /* {{{ */
122 {
123   char **tmp;
124   size_t i;
125
126   if (host == NULL)
127     return (EINVAL);
128
129   for (i = 0; i < host_list_len; i++)
130     if (strcmp (host_list[i], host) == 0)
131       return (0);
132
133   tmp = realloc (host_list, sizeof (*host_list) * (host_list_len + 1));
134   if (tmp == NULL)
135     return (ENOMEM);
136   host_list = tmp;
137
138   host_list[host_list_len] = strdup (host);
139   if (host_list[host_list_len] == NULL)
140     return (ENOMEM);
141
142   host_list_len++;
143   return (0);
144 } /* }}} int gl_register_host */
145
146 static int gl_clear_hosts (void) /* {{{ */
147 {
148   size_t i;
149
150   for (i = 0; i < host_list_len; i++)
151     free (host_list[i]);
152   free (host_list);
153
154   host_list = NULL;
155   host_list_len = 0;
156
157   return (0);
158 } /* }}} int gl_clear_hosts */
159
160 static int gl_compare_hosts (const void *v0, const void *v1) /* {{{ */
161 {
162   return (strcmp (*(char * const *) v0, *(char * const *) v1));
163 } /* }}} int gl_compare_hosts */
164
165 static int gl_register_file (const graph_ident_t *file, /* {{{ */
166     __attribute__((unused)) void *user_data)
167 {
168   graph_config_t *cfg;
169   int num_graphs = 0;
170   size_t i;
171
172   for (i = 0; i < gl_active_num; i++)
173   {
174     graph_config_t *cfg = gl_active[i];
175     int status;
176
177     if (!graph_ident_matches (cfg, file))
178       continue;
179
180     status = graph_add_file (cfg, file);
181     if (status != 0)
182     {
183       /* report error */;
184     }
185     else
186     {
187       num_graphs++;
188     }
189   }
190
191   if (num_graphs == 0)
192   {
193     cfg = graph_create (file);
194     gl_add_graph_internal (cfg, &gl_dynamic, &gl_dynamic_num);
195     graph_add_file (cfg, file);
196   }
197
198   gl_register_host (ident_get_host (file));
199
200   return (0);
201 } /* }}} int gl_register_file */
202
203 static const char *get_part_from_param (const char *prim_key, /* {{{ */
204     const char *sec_key)
205 {
206   const char *val;
207
208   val = param (prim_key);
209   if (val != NULL)
210     return (val);
211   
212   return (param (sec_key));
213 } /* }}} const char *get_part_from_param */
214
215 static int gl_clear_instances (void) /* {{{ */
216 {
217   size_t i;
218
219   for (i = 0; i < gl_active_num; i++)
220     graph_clear_instances (gl_active[i]);
221
222   return (0);
223 } /* }}} int gl_clear_instances */
224
225 /*
226  * Global functions
227  */
228 int gl_add_graph (graph_config_t *cfg) /* {{{ */
229 {
230   return (gl_add_graph_internal (cfg, &gl_staging, &gl_staging_num));
231 } /* }}} int gl_add_graph */
232
233 int gl_config_submit (void) /* {{{ */
234 {
235   graph_config_t **old;
236   size_t old_num;
237
238   old = gl_active;
239   old_num = gl_active_num;
240
241   gl_active = gl_staging;
242   gl_active_num = gl_staging_num;
243
244   gl_staging = NULL;
245   gl_staging_num = 0;
246
247   gl_destroy (&old, &old_num);
248
249   return (0);
250 } /* }}} int graph_config_submit */
251
252 int gl_graph_get_all (graph_callback_t callback, /* {{{ */
253     void *user_data)
254 {
255   size_t i;
256
257   if (callback == NULL)
258     return (EINVAL);
259
260   gl_update ();
261
262   for (i = 0; i < gl_active_num; i++)
263   {
264     int status;
265
266     status = (*callback) (gl_active[i], user_data);
267     if (status != 0)
268       return (status);
269   }
270
271   for (i = 0; i < gl_dynamic_num; i++)
272   {
273     int status;
274
275     status = (*callback) (gl_dynamic[i], user_data);
276     if (status != 0)
277       return (status);
278   }
279
280   return (0);
281 } /* }}} int gl_graph_get_all */
282
283 graph_config_t *gl_graph_get_selected (void) /* {{{ */
284 {
285   const char *host = get_part_from_param ("graph_host", "host");
286   const char *plugin = get_part_from_param ("graph_plugin", "plugin");
287   const char *plugin_instance = get_part_from_param ("graph_plugin_instance", "plugin_instance");
288   const char *type = get_part_from_param ("graph_type", "type");
289   const char *type_instance = get_part_from_param ("graph_type_instance", "type_instance");
290   graph_ident_t *ident;
291   size_t i;
292
293   if ((host == NULL)
294       || (plugin == NULL) || (plugin_instance == NULL)
295       || (type == NULL) || (type_instance == NULL))
296     return (NULL);
297
298   ident = ident_create (host, plugin, plugin_instance, type, type_instance);
299
300   gl_update ();
301
302   for (i = 0; i < gl_active_num; i++)
303   {
304     if (graph_compare (gl_active[i], ident) != 0)
305       continue;
306
307     ident_destroy (ident);
308     return (gl_active[i]);
309   }
310
311   for (i = 0; i < gl_dynamic_num; i++)
312   {
313     if (graph_compare (gl_dynamic[i], ident) != 0)
314       continue;
315
316     ident_destroy (ident);
317     return (gl_dynamic[i]);
318   }
319
320   ident_destroy (ident);
321   return (NULL);
322 } /* }}} graph_config_t *gl_graph_get_selected */
323
324 /* gl_instance_get_all, gl_graph_instance_get_all {{{ */
325 struct gl_inst_callback_data /* {{{ */
326 {
327   graph_config_t *cfg;
328   graph_inst_callback_t callback;
329   void *user_data;
330 }; /* }}} struct gl_inst_callback_data */
331
332 static int gl_inst_callback_handler (graph_instance_t *inst, /* {{{ */
333     void *user_data)
334 {
335   struct gl_inst_callback_data *data = user_data;
336
337   return ((*data->callback) (data->cfg, inst, data->user_data));
338 } /* }}} int gl_inst_callback_handler */
339
340 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
341     graph_inst_callback_t callback, void *user_data)
342 {
343   struct gl_inst_callback_data data =
344   {
345     cfg,
346     callback,
347     user_data
348   };
349
350   if ((cfg == NULL) || (callback == NULL))
351     return (EINVAL);
352
353   return (graph_inst_foreach (cfg, gl_inst_callback_handler, &data));
354 } /* }}} int gl_graph_instance_get_all */
355
356 int gl_instance_get_all (graph_inst_callback_t callback, /* {{{ */
357     void *user_data)
358 {
359   size_t i;
360
361   gl_update ();
362
363   for (i = 0; i < gl_active_num; i++)
364   {
365     int status;
366
367     status = gl_graph_instance_get_all (gl_active[i], callback, user_data);
368     if (status != 0)
369       return (status);
370   }
371
372   for (i = 0; i < gl_dynamic_num; i++)
373   {
374     int status;
375
376     status = gl_graph_instance_get_all (gl_dynamic[i], callback, user_data);
377     if (status != 0)
378       return (status);
379   }
380
381   return (0);
382 } /* }}} int gl_instance_get_all */
383 /* }}} gl_instance_get_all, gl_graph_instance_get_all */
384
385 int gl_search (search_info_t *si, /* {{{ */
386     graph_inst_callback_t callback, void *user_data)
387 {
388   size_t i;
389   graph_ident_t *ident;
390
391   if ((si == NULL) || (callback == NULL))
392     return (EINVAL);
393
394   ident = search_to_ident (si);
395   if (ident == NULL)
396   {
397     fprintf (stderr, "gl_search: search_to_ident failed\n");
398     return (-1);
399   }
400
401   for (i = 0; i < gl_active_num; i++)
402   {
403     int status;
404
405     if (!graph_matches_ident (gl_active[i], ident))
406       continue;
407
408     status = graph_search_inst (gl_active[i], si,
409         /* callback  = */ callback,
410         /* user data = */ user_data);
411     if (status != 0)
412       return (status);
413   }
414
415   for (i = 0; i < gl_dynamic_num; i++)
416   {
417     int status;
418
419     if (!graph_matches_ident (gl_dynamic[i], ident))
420       continue;
421
422     status = graph_search_inst (gl_dynamic[i], si,
423         /* callback  = */ callback,
424         /* user data = */ user_data);
425     if (status != 0)
426       return (status);
427   }
428
429   return (0);
430 } /* }}} int gl_search */
431
432 int gl_search_string (const char *term, graph_inst_callback_t callback, /* {{{ */
433     void *user_data)
434 {
435   size_t i;
436
437   for (i = 0; i < gl_active_num; i++)
438   {
439     int status;
440
441     status = graph_search_inst_string (gl_active[i], term,
442         /* callback  = */ callback,
443         /* user data = */ user_data);
444     if (status != 0)
445       return (status);
446   }
447
448   for (i = 0; i < gl_dynamic_num; i++)
449   {
450     int status;
451
452     status = graph_search_inst_string (gl_dynamic[i], term,
453         /* callback  = */ callback,
454         /* user data = */ user_data);
455     if (status != 0)
456       return (status);
457   }
458
459   return (0);
460 } /* }}} int gl_search_string */
461
462 int gl_search_field (graph_ident_field_t field, /* {{{ */
463     const char *field_value,
464     graph_inst_callback_t callback, void *user_data)
465 {
466   size_t i;
467
468   if ((field_value == NULL) || (callback == NULL))
469     return (EINVAL);
470
471   for (i = 0; i < gl_active_num; i++)
472   {
473     int status;
474
475     status = graph_inst_search_field (gl_active[i],
476         field, field_value,
477         /* callback  = */ callback,
478         /* user data = */ user_data);
479     if (status != 0)
480       return (status);
481   }
482
483   for (i = 0; i < gl_dynamic_num; i++)
484   {
485     int status;
486
487     status = graph_inst_search_field (gl_dynamic[i],
488         field, field_value,
489         /* callback  = */ callback,
490         /* user data = */ user_data);
491     if (status != 0)
492       return (status);
493   }
494
495   return (0);
496 } /* }}} int gl_search_field */
497
498 int gl_foreach_host (int (*callback) (const char *host, void *user_data), /* {{{ */
499     void *user_data)
500 {
501   int status;
502   size_t i;
503
504   for (i = 0; i < host_list_len; i++)
505   {
506     status = (*callback) (host_list[i], user_data);
507     if (status != 0)
508       return (status);
509   }
510
511   return (0);
512 } /* }}} int gl_foreach_host */
513
514 int gl_update (void) /* {{{ */
515 {
516   time_t now;
517   int status;
518   size_t i;
519
520   /*
521   printf ("Content-Type: text/plain\n\n");
522   */
523
524   now = time (NULL);
525
526   if ((gl_last_update + UPDATE_INTERVAL) >= now)
527     return (0);
528
529   /* Clear state */
530   gl_clear_instances ();
531   gl_clear_hosts ();
532   gl_destroy (&gl_dynamic, &gl_dynamic_num);
533
534   graph_read_config ();
535
536   status = fs_scan (/* callback = */ gl_register_file, /* user data = */ NULL);
537
538   if (host_list_len > 0)
539     qsort (host_list, host_list_len, sizeof (*host_list),
540         gl_compare_hosts);
541
542   gl_last_update = now;
543
544   for (i = 0; i < gl_active_num; i++)
545     graph_sort_instances (gl_active[i]);
546
547   return (status);
548 } /* }}} int gl_update */
549
550 /* vim: set sw=2 sts=2 et fdm=marker : */