7 #include "graph_list.h"
10 #define UPDATE_INTERVAL 10
12 static graph_list_t *graph_list = NULL;
13 static size_t graph_list_length = 0;
14 static time_t gl_last_update = 0;
16 static void gl_clear_entry (graph_list_t *gl) /* {{{ */
23 free (gl->plugin_instance);
25 free (gl->type_instance);
29 gl->plugin_instance = NULL;
31 gl->type_instance = NULL;
32 } /* }}} void gl_clear_entry */
34 static void gl_clear (void)
38 for (i = 0; i < graph_list_length; i++)
39 gl_clear_entry (graph_list + i);
43 graph_list_length = 0;
45 } /* }}} void gl_clear */
47 static int gl_add_copy (graph_list_t *gl) /* {{{ */
55 ptr = realloc (graph_list, sizeof (*graph_list) * (graph_list_length + 1));
60 ptr = graph_list + graph_list_length;
61 memset (ptr, 0, sizeof (*ptr));
64 ptr->plugin_instance = NULL;
66 ptr->type_instance = NULL;
68 #define DUP_OR_BREAK(member) do { \
70 if (gl->member != NULL) \
72 ptr->member = strdup (gl->member); \
73 if (ptr->member == NULL) \
83 DUP_OR_BREAK(plugin_instance);
85 DUP_OR_BREAK(type_instance);
96 free (ptr->plugin_instance);
98 free (ptr->type_instance);
104 } /* }}} int gl_add_copy */
106 static int callback_type (const char *type, void *user_data) /* {{{ */
111 if ((type == NULL) || (user_data == NULL))
115 if ((gl->type != NULL) || (gl->type_instance != NULL))
118 gl->type = strdup (type);
119 if (gl->type == NULL)
122 gl->type_instance = strchr (gl->type, '-');
123 if (gl->type_instance != NULL)
125 *gl->type_instance = 0;
129 status = gl_add_copy (gl);
133 gl->type_instance = NULL;
136 } /* }}} int callback_type */
138 static int callback_plugin (const char *plugin, void *user_data) /* {{{ */
143 if ((plugin == NULL) || (user_data == NULL))
147 if ((gl->plugin != NULL) || (gl->plugin_instance != NULL))
150 gl->plugin = strdup (plugin);
151 if (gl->plugin == NULL)
154 gl->plugin_instance = strchr (gl->plugin, '-');
155 if (gl->plugin_instance != NULL)
157 *gl->plugin_instance = 0;
158 gl->plugin_instance++;
161 status = foreach_type (gl->host, plugin, callback_type, gl);
165 gl->plugin_instance = NULL;
168 } /* }}} int callback_plugin */
170 static int callback_host (const char *host, void *user_data) /* {{{ */
175 if ((host == NULL) || (user_data == NULL))
179 if (gl->host != NULL)
182 gl->host = strdup (host);
183 if (gl->host == NULL)
186 status = foreach_plugin (host, callback_plugin, gl);
192 } /* }}} int callback_host */
194 int gl_update (void) /* {{{ */
202 if ((gl_last_update + UPDATE_INTERVAL) >= now)
207 memset (&gl, 0, sizeof (gl));
210 gl.plugin_instance = NULL;
212 gl.type_instance = NULL;
214 /* TODO: Free old list */
216 status = foreach_host (callback_host, &gl);
218 } /* }}} int gl_update */
220 int gl_foreach (gl_callback callback, void *user_data) /* {{{ */
224 for (i = 0; i < graph_list_length; i++)
228 status = (*callback) (graph_list + i, user_data);
234 } /* }}} int gl_foreach */
236 /* vim: set sw=2 sts=2 et fdm=marker : */