9 #include "graph_list.h"
10 #include "graph_ident.h"
11 #include "graph_def.h"
12 #include "graph_config.h"
14 #include "filesystem.h"
15 #include "utils_cgi.h"
18 #include <fcgi_stdio.h>
23 #define UPDATE_INTERVAL 10
28 static graph_config_t **gl_active = NULL;
29 static size_t gl_active_num = 0;
31 static graph_config_t **gl_staging = NULL;
32 static size_t gl_staging_num = 0;
34 static time_t gl_last_update = 0;
39 int gl_add_graph_internal (graph_config_t *cfg, /* {{{ */
40 graph_config_t ***gl_array, size_t *gl_array_num)
44 #define ARRAY_PTR (*gl_array)
45 #define ARRAY_SIZE (*gl_array_num)
50 tmp = realloc (ARRAY_PTR, sizeof (*ARRAY_PTR) * (ARRAY_SIZE + 1));
55 ARRAY_PTR[ARRAY_SIZE] = cfg;
62 } /* }}} int gl_add_graph_internal */
64 static int gl_register_file (const graph_ident_t *file, /* {{{ */
65 __attribute__((unused)) void *user_data)
71 for (i = 0; i < gl_active_num; i++)
73 graph_config_t *cfg = gl_active[i];
76 if (!graph_matches (cfg, file))
79 status = graph_add_file (cfg, file);
92 cfg = graph_create (file);
93 gl_add_graph_internal (cfg, &gl_active, &gl_active_num);
94 graph_add_file (cfg, file);
98 } /* }}} int gl_register_file */
100 static const char *get_part_from_param (const char *prim_key, /* {{{ */
105 val = param (prim_key);
109 return (param (sec_key));
110 } /* }}} const char *get_part_from_param */
112 static int gl_clear_instances (void) /* {{{ */
116 for (i = 0; i < gl_active_num; i++)
117 graph_clear_instances (gl_active[i]);
120 } /* }}} int gl_clear_instances */
126 int gl_add_graph (graph_config_t *cfg) /* {{{ */
128 return (gl_add_graph_internal (cfg, &gl_staging, &gl_staging_num));
129 } /* }}} int gl_add_graph */
131 int gl_config_submit (void) /* {{{ */
133 graph_config_t **old;
138 old_num = gl_active_num;
140 gl_active = gl_staging;
141 gl_active_num = gl_staging_num;
146 for (i = 0; i < old_num; i++)
148 graph_destroy (old[i]);
154 } /* }}} int graph_config_submit */
156 int gl_graph_get_all (graph_callback_t callback, /* {{{ */
161 if (callback == NULL)
166 for (i = 0; i < gl_active_num; i++)
170 status = (*callback) (gl_active[i], user_data);
176 } /* }}} int gl_graph_get_all */
178 graph_config_t *gl_graph_get_selected (void) /* {{{ */
180 const char *host = get_part_from_param ("graph_host", "host");
181 const char *plugin = get_part_from_param ("graph_plugin", "plugin");
182 const char *plugin_instance = get_part_from_param ("graph_plugin_instance", "plugin_instance");
183 const char *type = get_part_from_param ("graph_type", "type");
184 const char *type_instance = get_part_from_param ("graph_type_instance", "type_instance");
185 graph_ident_t *ident;
189 || (plugin == NULL) || (plugin_instance == NULL)
190 || (type == NULL) || (type_instance == NULL))
193 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
197 for (i = 0; i < gl_active_num; i++)
199 if (graph_compare (gl_active[i], ident) != 0)
202 ident_destroy (ident);
203 return (gl_active[i]);
206 ident_destroy (ident);
208 } /* }}} graph_config_t *gl_graph_get_selected */
210 /* gl_instance_get_all, gl_graph_instance_get_all {{{ */
211 struct gl_inst_callback_data /* {{{ */
214 graph_inst_callback_t callback;
216 }; /* }}} struct gl_inst_callback_data */
218 static int gl_inst_callback_handler (graph_instance_t *inst, /* {{{ */
221 struct gl_inst_callback_data *data = user_data;
223 return ((*data->callback) (data->cfg, inst, data->user_data));
224 } /* }}} int gl_inst_callback_handler */
226 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
227 graph_inst_callback_t callback, void *user_data)
229 struct gl_inst_callback_data data =
236 if ((cfg == NULL) || (callback == NULL))
239 return (graph_inst_foreach (cfg, gl_inst_callback_handler, &data));
240 } /* }}} int gl_graph_instance_get_all */
242 int gl_instance_get_all (graph_inst_callback_t callback, /* {{{ */
249 for (i = 0; i < gl_active_num; i++)
253 status = gl_graph_instance_get_all (gl_active[i], callback, user_data);
259 } /* }}} int gl_instance_get_all */
260 /* }}} gl_instance_get_all, gl_graph_instance_get_all */
262 int gl_search (const char *term, graph_inst_callback_t callback, /* {{{ */
267 for (i = 0; i < gl_active_num; i++)
271 status = graph_search (gl_active[i], term,
272 /* callback = */ callback,
273 /* user data = */ user_data);
279 } /* }}} int gl_search */
281 int gl_update (void) /* {{{ */
287 printf ("Content-Type: text/plain\n\n");
292 if ((gl_last_update + UPDATE_INTERVAL) >= now)
295 graph_read_config ();
297 gl_clear_instances ();
298 status = fs_scan (/* callback = */ gl_register_file, /* user data = */ NULL);
300 gl_last_update = now;
303 } /* }}} int gl_update */
305 /* vim: set sw=2 sts=2 et fdm=marker : */