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 **/
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>
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"
42 #include <fcgiapp.h>
43 #include <fcgi_stdio.h>
45 /*
46 * Defines
47 */
48 #define UPDATE_INTERVAL 900
50 /*
51 * Global variables
52 */
53 static graph_config_t **gl_active = NULL;
54 static size_t gl_active_num = 0;
56 static graph_config_t **gl_staging = NULL;
57 static size_t gl_staging_num = 0;
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;
64 static char **host_list = NULL;
65 static size_t host_list_len = 0;
67 static time_t gl_last_update = 0;
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;
77 #define ARRAY_PTR (*gl_array)
78 #define ARRAY_SIZE (*gl_array_num)
80 if (cfg == NULL)
81 return (EINVAL);
83 tmp = realloc (ARRAY_PTR, sizeof (*ARRAY_PTR) * (ARRAY_SIZE + 1));
84 if (tmp == NULL)
85 return (ENOMEM);
86 ARRAY_PTR = tmp;
88 ARRAY_PTR[ARRAY_SIZE] = cfg;
89 ARRAY_SIZE++;
91 #undef ARRAY_SIZE
92 #undef ARRAY_PTR
94 return (0);
95 } /* }}} int gl_add_graph_internal */
97 static void gl_destroy (graph_config_t ***gl_array, /* {{{ */
98 size_t *gl_array_num)
99 {
100 size_t i;
102 if ((gl_array == NULL) || (gl_array_num == NULL))
103 return;
105 #define ARRAY_PTR (*gl_array)
106 #define ARRAY_SIZE (*gl_array_num)
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;
117 #undef ARRAY_SIZE
118 #undef ARRAY_PTR
119 } /* }}} void gl_destroy */
121 static int gl_register_host (const char *host) /* {{{ */
122 {
123 char **tmp;
124 size_t i;
126 if (host == NULL)
127 return (EINVAL);
129 for (i = 0; i < host_list_len; i++)
130 if (strcmp (host_list[i], host) == 0)
131 return (0);
133 tmp = realloc (host_list, sizeof (*host_list) * (host_list_len + 1));
134 if (tmp == NULL)
135 return (ENOMEM);
136 host_list = tmp;
138 host_list[host_list_len] = strdup (host);
139 if (host_list[host_list_len] == NULL)
140 return (ENOMEM);
142 host_list_len++;
143 return (0);
144 } /* }}} int gl_register_host */
146 static int gl_clear_hosts (void) /* {{{ */
147 {
148 size_t i;
150 for (i = 0; i < host_list_len; i++)
151 free (host_list[i]);
152 free (host_list);
154 host_list = NULL;
155 host_list_len = 0;
157 return (0);
158 } /* }}} int gl_clear_hosts */
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 */
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;
172 for (i = 0; i < gl_active_num; i++)
173 {
174 graph_config_t *cfg = gl_active[i];
175 int status;
177 if (!graph_ident_matches (cfg, file))
178 continue;
180 status = graph_add_file (cfg, file);
181 if (status != 0)
182 {
183 /* report error */;
184 }
185 else
186 {
187 num_graphs++;
188 }
189 }
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 }
198 gl_register_host (ident_get_host (file));
200 return (0);
201 } /* }}} int gl_register_file */
203 static const char *get_part_from_param (const char *prim_key, /* {{{ */
204 const char *sec_key)
205 {
206 const char *val;
208 val = param (prim_key);
209 if (val != NULL)
210 return (val);
212 return (param (sec_key));
213 } /* }}} const char *get_part_from_param */
215 static int gl_clear_instances (void) /* {{{ */
216 {
217 size_t i;
219 for (i = 0; i < gl_active_num; i++)
220 graph_clear_instances (gl_active[i]);
222 return (0);
223 } /* }}} int gl_clear_instances */
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 */
233 int gl_config_submit (void) /* {{{ */
234 {
235 graph_config_t **old;
236 size_t old_num;
238 old = gl_active;
239 old_num = gl_active_num;
241 gl_active = gl_staging;
242 gl_active_num = gl_staging_num;
244 gl_staging = NULL;
245 gl_staging_num = 0;
247 gl_destroy (&old, &old_num);
249 return (0);
250 } /* }}} int graph_config_submit */
252 int gl_graph_get_all (graph_callback_t callback, /* {{{ */
253 void *user_data)
254 {
255 size_t i;
257 if (callback == NULL)
258 return (EINVAL);
260 gl_update ();
262 for (i = 0; i < gl_active_num; i++)
263 {
264 int status;
266 status = (*callback) (gl_active[i], user_data);
267 if (status != 0)
268 return (status);
269 }
271 for (i = 0; i < gl_dynamic_num; i++)
272 {
273 int status;
275 status = (*callback) (gl_dynamic[i], user_data);
276 if (status != 0)
277 return (status);
278 }
280 return (0);
281 } /* }}} int gl_graph_get_all */
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;
293 if ((host == NULL)
294 || (plugin == NULL) || (plugin_instance == NULL)
295 || (type == NULL) || (type_instance == NULL))
296 return (NULL);
298 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
300 gl_update ();
302 for (i = 0; i < gl_active_num; i++)
303 {
304 if (graph_compare (gl_active[i], ident) != 0)
305 continue;
307 ident_destroy (ident);
308 return (gl_active[i]);
309 }
311 for (i = 0; i < gl_dynamic_num; i++)
312 {
313 if (graph_compare (gl_dynamic[i], ident) != 0)
314 continue;
316 ident_destroy (ident);
317 return (gl_dynamic[i]);
318 }
320 ident_destroy (ident);
321 return (NULL);
322 } /* }}} graph_config_t *gl_graph_get_selected */
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 */
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;
337 return ((*data->callback) (data->cfg, inst, data->user_data));
338 } /* }}} int gl_inst_callback_handler */
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 };
350 if ((cfg == NULL) || (callback == NULL))
351 return (EINVAL);
353 return (graph_inst_foreach (cfg, gl_inst_callback_handler, &data));
354 } /* }}} int gl_graph_instance_get_all */
356 int gl_instance_get_all (graph_inst_callback_t callback, /* {{{ */
357 void *user_data)
358 {
359 size_t i;
361 gl_update ();
363 for (i = 0; i < gl_active_num; i++)
364 {
365 int status;
367 status = gl_graph_instance_get_all (gl_active[i], callback, user_data);
368 if (status != 0)
369 return (status);
370 }
372 for (i = 0; i < gl_dynamic_num; i++)
373 {
374 int status;
376 status = gl_graph_instance_get_all (gl_dynamic[i], callback, user_data);
377 if (status != 0)
378 return (status);
379 }
381 return (0);
382 } /* }}} int gl_instance_get_all */
383 /* }}} gl_instance_get_all, gl_graph_instance_get_all */
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;
391 if ((si == NULL) || (callback == NULL))
392 return (EINVAL);
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 }
401 for (i = 0; i < gl_active_num; i++)
402 {
403 int status;
405 if (!graph_matches_ident (gl_active[i], ident))
406 continue;
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 }
415 for (i = 0; i < gl_dynamic_num; i++)
416 {
417 int status;
419 if (!graph_matches_ident (gl_dynamic[i], ident))
420 continue;
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 }
429 return (0);
430 } /* }}} int gl_search */
432 int gl_search_string (const char *term, graph_inst_callback_t callback, /* {{{ */
433 void *user_data)
434 {
435 size_t i;
437 for (i = 0; i < gl_active_num; i++)
438 {
439 int status;
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 }
448 for (i = 0; i < gl_dynamic_num; i++)
449 {
450 int status;
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 }
459 return (0);
460 } /* }}} int gl_search_string */
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;
468 if ((field_value == NULL) || (callback == NULL))
469 return (EINVAL);
471 for (i = 0; i < gl_active_num; i++)
472 {
473 int status;
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 }
483 for (i = 0; i < gl_dynamic_num; i++)
484 {
485 int status;
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 }
495 return (0);
496 } /* }}} int gl_search_field */
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;
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 }
511 return (0);
512 } /* }}} int gl_foreach_host */
514 int gl_update (void) /* {{{ */
515 {
516 time_t now;
517 int status;
518 size_t i;
520 /*
521 printf ("Content-Type: text/plain\n\n");
522 */
524 now = time (NULL);
526 if ((gl_last_update + UPDATE_INTERVAL) >= now)
527 return (0);
529 /* Clear state */
530 gl_clear_instances ();
531 gl_clear_hosts ();
532 gl_destroy (&gl_dynamic, &gl_dynamic_num);
534 graph_read_config ();
536 status = fs_scan (/* callback = */ gl_register_file, /* user data = */ NULL);
538 if (host_list_len > 0)
539 qsort (host_list, host_list_len, sizeof (*host_list),
540 gl_compare_hosts);
542 gl_last_update = now;
544 for (i = 0; i < gl_active_num; i++)
545 graph_sort_instances (gl_active[i]);
547 return (status);
548 } /* }}} int gl_update */
550 /* vim: set sw=2 sts=2 et fdm=marker : */