9 #include "graph_list.h"
11 #include "graph_ident.h"
12 #include "graph_def.h"
13 #include "graph_config.h"
15 #include "filesystem.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 time_t gl_last_update = 0;
40 int gl_add_graph_internal (graph_config_t *cfg, /* {{{ */
41 graph_config_t ***gl_array, size_t *gl_array_num)
45 #define ARRAY_PTR (*gl_array)
46 #define ARRAY_SIZE (*gl_array_num)
51 tmp = realloc (ARRAY_PTR, sizeof (*ARRAY_PTR) * (ARRAY_SIZE + 1));
56 ARRAY_PTR[ARRAY_SIZE] = cfg;
63 } /* }}} int gl_add_graph_internal */
65 static int gl_register_file (const graph_ident_t *file, /* {{{ */
66 __attribute__((unused)) void *user_data)
72 for (i = 0; i < gl_active_num; i++)
74 graph_config_t *cfg = gl_active[i];
77 if (!graph_matches_ident (cfg, file))
80 status = graph_add_file (cfg, file);
93 cfg = graph_create (file);
94 gl_add_graph_internal (cfg, &gl_active, &gl_active_num);
95 graph_add_file (cfg, file);
99 } /* }}} int gl_register_file */
101 static const char *get_part_from_param (const char *prim_key, /* {{{ */
106 val = param (prim_key);
110 return (param (sec_key));
111 } /* }}} const char *get_part_from_param */
113 static int gl_clear_instances (void) /* {{{ */
117 for (i = 0; i < gl_active_num; i++)
118 graph_clear_instances (gl_active[i]);
121 } /* }}} int gl_clear_instances */
127 int gl_add_graph (graph_config_t *cfg) /* {{{ */
129 return (gl_add_graph_internal (cfg, &gl_staging, &gl_staging_num));
130 } /* }}} int gl_add_graph */
132 int gl_config_submit (void) /* {{{ */
134 graph_config_t **old;
139 old_num = gl_active_num;
141 gl_active = gl_staging;
142 gl_active_num = gl_staging_num;
147 for (i = 0; i < old_num; i++)
149 graph_destroy (old[i]);
155 } /* }}} int graph_config_submit */
157 int gl_graph_get_all (graph_callback_t callback, /* {{{ */
162 if (callback == NULL)
167 for (i = 0; i < gl_active_num; i++)
171 status = (*callback) (gl_active[i], user_data);
177 } /* }}} int gl_graph_get_all */
179 graph_config_t *gl_graph_get_selected (void) /* {{{ */
181 const char *host = get_part_from_param ("graph_host", "host");
182 const char *plugin = get_part_from_param ("graph_plugin", "plugin");
183 const char *plugin_instance = get_part_from_param ("graph_plugin_instance", "plugin_instance");
184 const char *type = get_part_from_param ("graph_type", "type");
185 const char *type_instance = get_part_from_param ("graph_type_instance", "type_instance");
186 graph_ident_t *ident;
190 || (plugin == NULL) || (plugin_instance == NULL)
191 || (type == NULL) || (type_instance == NULL))
194 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
198 for (i = 0; i < gl_active_num; i++)
200 if (graph_compare (gl_active[i], ident) != 0)
203 ident_destroy (ident);
204 return (gl_active[i]);
207 ident_destroy (ident);
209 } /* }}} graph_config_t *gl_graph_get_selected */
211 /* gl_instance_get_all, gl_graph_instance_get_all {{{ */
212 struct gl_inst_callback_data /* {{{ */
215 graph_inst_callback_t callback;
217 }; /* }}} struct gl_inst_callback_data */
219 static int gl_inst_callback_handler (graph_instance_t *inst, /* {{{ */
222 struct gl_inst_callback_data *data = user_data;
224 return ((*data->callback) (data->cfg, inst, data->user_data));
225 } /* }}} int gl_inst_callback_handler */
227 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
228 graph_inst_callback_t callback, void *user_data)
230 struct gl_inst_callback_data data =
237 if ((cfg == NULL) || (callback == NULL))
240 return (graph_inst_foreach (cfg, gl_inst_callback_handler, &data));
241 } /* }}} int gl_graph_instance_get_all */
243 int gl_instance_get_all (graph_inst_callback_t callback, /* {{{ */
250 for (i = 0; i < gl_active_num; i++)
254 status = gl_graph_instance_get_all (gl_active[i], callback, user_data);
260 } /* }}} int gl_instance_get_all */
261 /* }}} gl_instance_get_all, gl_graph_instance_get_all */
263 int gl_search (const char *term, graph_inst_callback_t callback, /* {{{ */
268 for (i = 0; i < gl_active_num; i++)
272 status = graph_inst_search (gl_active[i], term,
273 /* callback = */ callback,
274 /* user data = */ user_data);
280 } /* }}} int gl_search */
282 int gl_search_field (graph_ident_field_t field, /* {{{ */
283 const char *field_value,
284 graph_inst_callback_t callback, void *user_data)
288 if ((field_value == NULL) || (callback == NULL))
291 for (i = 0; i < gl_active_num; i++)
295 status = graph_inst_search_field (gl_active[i],
297 /* callback = */ callback,
298 /* user data = */ user_data);
304 } /* }}} int gl_search_field */
306 int gl_update (void) /* {{{ */
312 printf ("Content-Type: text/plain\n\n");
317 if ((gl_last_update + UPDATE_INTERVAL) >= now)
320 graph_read_config ();
322 gl_clear_instances ();
323 status = fs_scan (/* callback = */ gl_register_file, /* user data = */ NULL);
325 gl_last_update = now;
328 } /* }}} int gl_update */
330 /* vim: set sw=2 sts=2 et fdm=marker : */