2 * collection4 - graph_list.c
3 * Copyright (C) 2010 Florian octo Forster
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.
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.
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
21 * Florian octo Forster <ff at octo.it>
32 #include "graph_list.h"
34 #include "filesystem.h"
36 #include "graph_config.h"
37 #include "graph_def.h"
38 #include "graph_ident.h"
39 #include "utils_cgi.h"
42 #include <fcgi_stdio.h>
47 #define UPDATE_INTERVAL 10
52 static graph_config_t **gl_active = NULL;
53 static size_t gl_active_num = 0;
55 static graph_config_t **gl_staging = NULL;
56 static size_t gl_staging_num = 0;
58 static char **host_list = NULL;
59 static size_t host_list_len = 0;
61 static time_t gl_last_update = 0;
66 static int gl_add_graph_internal (graph_config_t *cfg, /* {{{ */
67 graph_config_t ***gl_array, size_t *gl_array_num)
71 #define ARRAY_PTR (*gl_array)
72 #define ARRAY_SIZE (*gl_array_num)
77 tmp = realloc (ARRAY_PTR, sizeof (*ARRAY_PTR) * (ARRAY_SIZE + 1));
82 ARRAY_PTR[ARRAY_SIZE] = cfg;
89 } /* }}} int gl_add_graph_internal */
91 static int gl_register_host (const char *host) /* {{{ */
99 for (i = 0; i < host_list_len; i++)
100 if (strcmp (host_list[i], host) == 0)
103 tmp = realloc (host_list, sizeof (*host_list) * (host_list_len + 1));
108 host_list[host_list_len] = strdup (host);
109 if (host_list[host_list_len] == NULL)
114 } /* }}} int gl_register_host */
116 static int gl_clear_hosts (void) /* {{{ */
120 for (i = 0; i < host_list_len; i++)
128 } /* }}} int gl_clear_hosts */
130 static int gl_compare_hosts (const void *v0, const void *v1) /* {{{ */
132 return (strcmp (v0, v1));
133 } /* }}} int gl_compare_hosts */
135 static int gl_register_file (const graph_ident_t *file, /* {{{ */
136 __attribute__((unused)) void *user_data)
142 for (i = 0; i < gl_active_num; i++)
144 graph_config_t *cfg = gl_active[i];
147 if (!graph_matches_ident (cfg, file))
150 status = graph_add_file (cfg, file);
163 cfg = graph_create (file);
164 gl_add_graph_internal (cfg, &gl_active, &gl_active_num);
165 graph_add_file (cfg, file);
168 gl_register_host (ident_get_host (file));
171 } /* }}} int gl_register_file */
173 static const char *get_part_from_param (const char *prim_key, /* {{{ */
178 val = param (prim_key);
182 return (param (sec_key));
183 } /* }}} const char *get_part_from_param */
185 static int gl_clear_instances (void) /* {{{ */
189 for (i = 0; i < gl_active_num; i++)
190 graph_clear_instances (gl_active[i]);
193 } /* }}} int gl_clear_instances */
199 int gl_add_graph (graph_config_t *cfg) /* {{{ */
201 return (gl_add_graph_internal (cfg, &gl_staging, &gl_staging_num));
202 } /* }}} int gl_add_graph */
204 int gl_config_submit (void) /* {{{ */
206 graph_config_t **old;
211 old_num = gl_active_num;
213 gl_active = gl_staging;
214 gl_active_num = gl_staging_num;
219 for (i = 0; i < old_num; i++)
221 graph_destroy (old[i]);
227 } /* }}} int graph_config_submit */
229 int gl_graph_get_all (graph_callback_t callback, /* {{{ */
234 if (callback == NULL)
239 for (i = 0; i < gl_active_num; i++)
243 status = (*callback) (gl_active[i], user_data);
249 } /* }}} int gl_graph_get_all */
251 graph_config_t *gl_graph_get_selected (void) /* {{{ */
253 const char *host = get_part_from_param ("graph_host", "host");
254 const char *plugin = get_part_from_param ("graph_plugin", "plugin");
255 const char *plugin_instance = get_part_from_param ("graph_plugin_instance", "plugin_instance");
256 const char *type = get_part_from_param ("graph_type", "type");
257 const char *type_instance = get_part_from_param ("graph_type_instance", "type_instance");
258 graph_ident_t *ident;
262 || (plugin == NULL) || (plugin_instance == NULL)
263 || (type == NULL) || (type_instance == NULL))
266 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
270 for (i = 0; i < gl_active_num; i++)
272 if (graph_compare (gl_active[i], ident) != 0)
275 ident_destroy (ident);
276 return (gl_active[i]);
279 ident_destroy (ident);
281 } /* }}} graph_config_t *gl_graph_get_selected */
283 /* gl_instance_get_all, gl_graph_instance_get_all {{{ */
284 struct gl_inst_callback_data /* {{{ */
287 graph_inst_callback_t callback;
289 }; /* }}} struct gl_inst_callback_data */
291 static int gl_inst_callback_handler (graph_instance_t *inst, /* {{{ */
294 struct gl_inst_callback_data *data = user_data;
296 return ((*data->callback) (data->cfg, inst, data->user_data));
297 } /* }}} int gl_inst_callback_handler */
299 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
300 graph_inst_callback_t callback, void *user_data)
302 struct gl_inst_callback_data data =
309 if ((cfg == NULL) || (callback == NULL))
312 return (graph_inst_foreach (cfg, gl_inst_callback_handler, &data));
313 } /* }}} int gl_graph_instance_get_all */
315 int gl_instance_get_all (graph_inst_callback_t callback, /* {{{ */
322 for (i = 0; i < gl_active_num; i++)
326 status = gl_graph_instance_get_all (gl_active[i], callback, user_data);
332 } /* }}} int gl_instance_get_all */
333 /* }}} gl_instance_get_all, gl_graph_instance_get_all */
335 int gl_search (const char *term, graph_inst_callback_t callback, /* {{{ */
340 for (i = 0; i < gl_active_num; i++)
344 status = graph_inst_search (gl_active[i], term,
345 /* callback = */ callback,
346 /* user data = */ user_data);
352 } /* }}} int gl_search */
354 int gl_search_field (graph_ident_field_t field, /* {{{ */
355 const char *field_value,
356 graph_inst_callback_t callback, void *user_data)
360 if ((field_value == NULL) || (callback == NULL))
363 for (i = 0; i < gl_active_num; i++)
367 status = graph_inst_search_field (gl_active[i],
369 /* callback = */ callback,
370 /* user data = */ user_data);
376 } /* }}} int gl_search_field */
378 int gl_foreach_host (int (*callback) (const char *host, void *user_data), /* {{{ */
384 for (i = 0; i < host_list_len; i++)
386 status = (*callback) (host_list[i], user_data);
392 } /* }}} int gl_foreach_host */
394 int gl_update (void) /* {{{ */
400 printf ("Content-Type: text/plain\n\n");
405 if ((gl_last_update + UPDATE_INTERVAL) >= now)
408 graph_read_config ();
410 gl_clear_instances ();
412 status = fs_scan (/* callback = */ gl_register_file, /* user data = */ NULL);
414 if (host_list_len > 0)
415 qsort (host_list, host_list_len, sizeof (*host_list),
418 gl_last_update = now;
421 } /* }}} int gl_update */
423 /* vim: set sw=2 sts=2 et fdm=marker : */