9 #include "graph_list.h"
11 #include "filesystem.h"
13 #include "graph_config.h"
14 #include "graph_def.h"
15 #include "graph_ident.h"
16 #include "utils_cgi.h"
19 #include <fcgi_stdio.h>
24 #define UPDATE_INTERVAL 10
29 static graph_config_t **gl_active = NULL;
30 static size_t gl_active_num = 0;
32 static graph_config_t **gl_staging = NULL;
33 static size_t gl_staging_num = 0;
35 static char **host_list = NULL;
36 static size_t host_list_len = 0;
38 static time_t gl_last_update = 0;
43 int gl_add_graph_internal (graph_config_t *cfg, /* {{{ */
44 graph_config_t ***gl_array, size_t *gl_array_num)
48 #define ARRAY_PTR (*gl_array)
49 #define ARRAY_SIZE (*gl_array_num)
54 tmp = realloc (ARRAY_PTR, sizeof (*ARRAY_PTR) * (ARRAY_SIZE + 1));
59 ARRAY_PTR[ARRAY_SIZE] = cfg;
66 } /* }}} int gl_add_graph_internal */
68 static int gl_register_host (const char *host) /* {{{ */
76 for (i = 0; i < host_list_len; i++)
77 if (strcmp (host_list[i], host) == 0)
80 tmp = realloc (host_list, sizeof (*host_list) * (host_list_len + 1));
85 host_list[host_list_len] = strdup (host);
86 if (host_list[host_list_len] == NULL)
91 } /* }}} int gl_register_host */
93 static int gl_clear_hosts (void) /* {{{ */
97 for (i = 0; i < host_list_len; i++)
105 } /* }}} int gl_clear_hosts */
107 static int gl_compare_hosts (const void *v0, const void *v1) /* {{{ */
109 return (strcmp (v0, v1));
110 } /* }}} int gl_compare_hosts */
112 static int gl_register_file (const graph_ident_t *file, /* {{{ */
113 __attribute__((unused)) void *user_data)
119 for (i = 0; i < gl_active_num; i++)
121 graph_config_t *cfg = gl_active[i];
124 if (!graph_matches_ident (cfg, file))
127 status = graph_add_file (cfg, file);
140 cfg = graph_create (file);
141 gl_add_graph_internal (cfg, &gl_active, &gl_active_num);
142 graph_add_file (cfg, file);
145 gl_register_host (ident_get_host (file));
148 } /* }}} int gl_register_file */
150 static const char *get_part_from_param (const char *prim_key, /* {{{ */
155 val = param (prim_key);
159 return (param (sec_key));
160 } /* }}} const char *get_part_from_param */
162 static int gl_clear_instances (void) /* {{{ */
166 for (i = 0; i < gl_active_num; i++)
167 graph_clear_instances (gl_active[i]);
170 } /* }}} int gl_clear_instances */
176 int gl_add_graph (graph_config_t *cfg) /* {{{ */
178 return (gl_add_graph_internal (cfg, &gl_staging, &gl_staging_num));
179 } /* }}} int gl_add_graph */
181 int gl_config_submit (void) /* {{{ */
183 graph_config_t **old;
188 old_num = gl_active_num;
190 gl_active = gl_staging;
191 gl_active_num = gl_staging_num;
196 for (i = 0; i < old_num; i++)
198 graph_destroy (old[i]);
204 } /* }}} int graph_config_submit */
206 int gl_graph_get_all (graph_callback_t callback, /* {{{ */
211 if (callback == NULL)
216 for (i = 0; i < gl_active_num; i++)
220 status = (*callback) (gl_active[i], user_data);
226 } /* }}} int gl_graph_get_all */
228 graph_config_t *gl_graph_get_selected (void) /* {{{ */
230 const char *host = get_part_from_param ("graph_host", "host");
231 const char *plugin = get_part_from_param ("graph_plugin", "plugin");
232 const char *plugin_instance = get_part_from_param ("graph_plugin_instance", "plugin_instance");
233 const char *type = get_part_from_param ("graph_type", "type");
234 const char *type_instance = get_part_from_param ("graph_type_instance", "type_instance");
235 graph_ident_t *ident;
239 || (plugin == NULL) || (plugin_instance == NULL)
240 || (type == NULL) || (type_instance == NULL))
243 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
247 for (i = 0; i < gl_active_num; i++)
249 if (graph_compare (gl_active[i], ident) != 0)
252 ident_destroy (ident);
253 return (gl_active[i]);
256 ident_destroy (ident);
258 } /* }}} graph_config_t *gl_graph_get_selected */
260 /* gl_instance_get_all, gl_graph_instance_get_all {{{ */
261 struct gl_inst_callback_data /* {{{ */
264 graph_inst_callback_t callback;
266 }; /* }}} struct gl_inst_callback_data */
268 static int gl_inst_callback_handler (graph_instance_t *inst, /* {{{ */
271 struct gl_inst_callback_data *data = user_data;
273 return ((*data->callback) (data->cfg, inst, data->user_data));
274 } /* }}} int gl_inst_callback_handler */
276 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
277 graph_inst_callback_t callback, void *user_data)
279 struct gl_inst_callback_data data =
286 if ((cfg == NULL) || (callback == NULL))
289 return (graph_inst_foreach (cfg, gl_inst_callback_handler, &data));
290 } /* }}} int gl_graph_instance_get_all */
292 int gl_instance_get_all (graph_inst_callback_t callback, /* {{{ */
299 for (i = 0; i < gl_active_num; i++)
303 status = gl_graph_instance_get_all (gl_active[i], callback, user_data);
309 } /* }}} int gl_instance_get_all */
310 /* }}} gl_instance_get_all, gl_graph_instance_get_all */
312 int gl_search (const char *term, graph_inst_callback_t callback, /* {{{ */
317 for (i = 0; i < gl_active_num; i++)
321 status = graph_inst_search (gl_active[i], term,
322 /* callback = */ callback,
323 /* user data = */ user_data);
329 } /* }}} int gl_search */
331 int gl_search_field (graph_ident_field_t field, /* {{{ */
332 const char *field_value,
333 graph_inst_callback_t callback, void *user_data)
337 if ((field_value == NULL) || (callback == NULL))
340 for (i = 0; i < gl_active_num; i++)
344 status = graph_inst_search_field (gl_active[i],
346 /* callback = */ callback,
347 /* user data = */ user_data);
353 } /* }}} int gl_search_field */
355 int gl_foreach_host (int (*callback) (const char *host, void *user_data), /* {{{ */
361 for (i = 0; i < host_list_len; i++)
363 status = (*callback) (host_list[i], user_data);
369 } /* }}} int gl_foreach_host */
371 int gl_update (void) /* {{{ */
377 printf ("Content-Type: text/plain\n\n");
382 if ((gl_last_update + UPDATE_INTERVAL) >= now)
385 graph_read_config ();
387 gl_clear_instances ();
389 status = fs_scan (/* callback = */ gl_register_file, /* user data = */ NULL);
391 if (host_list_len > 0)
392 qsort (host_list, host_list_len, sizeof (*host_list),
395 gl_last_update = now;
398 } /* }}} int gl_update */
400 /* vim: set sw=2 sts=2 et fdm=marker : */