10 #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_params.h"
19 #include <fcgi_stdio.h>
24 #define UPDATE_INTERVAL 10
26 #define ANY_TOKEN "/any/"
27 #define ALL_TOKEN "/all/"
32 struct gl_ident_stage_s /* {{{ */
36 char *plugin_instance;
40 typedef struct gl_ident_stage_s gl_ident_stage_t;
45 static graph_config_t **gl_active = NULL;
46 static size_t gl_active_num = 0;
48 static graph_config_t **gl_staging = NULL;
49 static size_t gl_staging_num = 0;
51 static time_t gl_last_update = 0;
57 /* "Safe" version of strcmp(3): Either or both pointers may be NULL. */
58 static int strcmp_s (const char *s1, const char *s2) /* {{{ */
60 if ((s1 == NULL) && (s2 == NULL))
66 assert ((s1 != NULL) && (s2 != NULL));
68 return (strcmp (s1, s2));
69 } /* }}} int strcmp_s */
72 int gl_add_graph_internal (graph_config_t *cfg, /* {{{ */
73 graph_config_t ***gl_array, size_t *gl_array_num)
77 #define ARRAY_PTR (*gl_array)
78 #define ARRAY_SIZE (*gl_array_num)
83 tmp = realloc (ARRAY_PTR, sizeof (*ARRAY_PTR) * (ARRAY_SIZE + 1));
88 ARRAY_PTR[ARRAY_SIZE] = cfg;
95 } /* }}} int gl_add_graph_internal */
97 static int gl_register_file (const graph_ident_t *file, /* {{{ */
98 __attribute__((unused)) void *user_data)
104 for (i = 0; i < gl_active_num; i++)
106 graph_config_t *cfg = gl_active[i];
109 if (!graph_matches (cfg, file))
112 status = graph_add_file (cfg, file);
125 cfg = graph_create (file);
126 gl_add_graph_internal (cfg, &gl_active, &gl_active_num);
127 graph_add_file (cfg, file);
131 } /* }}} int gl_register_file */
133 static const char *get_part_from_param (const char *prim_key, /* {{{ */
138 val = param (prim_key);
142 return (param (sec_key));
143 } /* }}} const char *get_part_from_param */
145 static int gl_clear_instances (void) /* {{{ */
149 for (i = 0; i < gl_active_num; i++)
150 graph_clear_instances (gl_active[i]);
153 } /* }}} int gl_clear_instances */
159 int gl_add_graph (graph_config_t *cfg) /* {{{ */
161 return (gl_add_graph_internal (cfg, &gl_staging, &gl_staging_num));
162 } /* }}} int gl_add_graph */
164 int graph_config_submit (void) /* {{{ */
166 graph_config_t **old;
171 old_num = gl_active_num;
173 gl_active = gl_staging;
174 gl_active_num = gl_staging_num;
179 for (i = 0; i < old_num; i++)
181 graph_destroy (old[i]);
187 } /* }}} int graph_config_submit */
189 int gl_graph_get_all (gl_cfg_callback callback, /* {{{ */
194 if (callback == NULL)
199 for (i = 0; i < gl_active_num; i++)
203 status = (*callback) (gl_active[i], user_data);
209 } /* }}} int gl_graph_get_all */
211 graph_config_t *graph_get_selected (void) /* {{{ */
213 const char *host = get_part_from_param ("graph_host", "host");
214 const char *plugin = get_part_from_param ("graph_plugin", "plugin");
215 const char *plugin_instance = get_part_from_param ("graph_plugin_instance", "plugin_instance");
216 const char *type = get_part_from_param ("graph_type", "type");
217 const char *type_instance = get_part_from_param ("graph_type_instance", "type_instance");
218 graph_ident_t *ident;
222 || (plugin == NULL) || (plugin_instance == NULL)
223 || (type == NULL) || (type_instance == NULL))
226 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
230 for (i = 0; i < gl_active_num; i++)
232 if (graph_compare (gl_active[i], ident) != 0)
235 ident_destroy (ident);
236 return (gl_active[i]);
239 ident_destroy (ident);
241 } /* }}} graph_config_t *graph_get_selected */
243 /* gl_instance_get_all, gl_graph_instance_get_all {{{ */
244 struct gl_inst_callback_data /* {{{ */
247 gl_inst_callback callback;
249 }; /* }}} struct gl_inst_callback_data */
251 static int gl_inst_callback_handler (graph_instance_t *inst, /* {{{ */
254 struct gl_inst_callback_data *data = user_data;
256 return ((*data->callback) (data->cfg, inst, data->user_data));
257 } /* }}} int gl_inst_callback_handler */
259 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
260 gl_inst_callback callback, void *user_data)
262 struct gl_inst_callback_data data =
269 if ((cfg == NULL) || (callback == NULL))
272 return (inst_foreach (gl_graph_get_instances (cfg),
273 gl_inst_callback_handler, &data));
274 } /* }}} int gl_graph_instance_get_all */
276 int gl_instance_get_all (gl_inst_callback callback, /* {{{ */
283 for (i = 0; i < gl_active_num; i++)
287 status = gl_graph_instance_get_all (gl_active[i], callback, user_data);
293 } /* }}} int gl_instance_get_all */
294 /* }}} gl_instance_get_all, gl_graph_instance_get_all */
296 int gl_update (void) /* {{{ */
303 printf ("Content-Type: text/plain\n\n");
308 if ((gl_last_update + UPDATE_INTERVAL) >= now)
311 graph_read_config ();
313 memset (&gl, 0, sizeof (gl));
316 gl.plugin_instance = NULL;
318 gl.type_instance = NULL;
320 gl_clear_instances ();
321 status = fs_scan (/* callback = */ gl_register_file, /* user data = */ NULL);
323 gl_last_update = now;
326 } /* }}} int gl_update */
328 /* vim: set sw=2 sts=2 et fdm=marker : */