11 #define DATA_DIR "/var/lib/collectd/rrd"
13 #include "filesystem.h"
15 struct fs_scan_dir_data_s /* {{{ */
17 fs_ident_cb_t callback;
22 char *plugin_instance;
26 typedef struct fs_scan_dir_data_s fs_scan_dir_data_t;
28 typedef int (*callback_type_t) (const char *type, void *user_data);
29 typedef int (*callback_plugin_t) (const char *plugin, void *user_data);
30 typedef int (*callback_host_t) (const char *host, void *user_data);
33 * Directory and file walking functions
35 static int foreach_rrd_file (const char *dir, /* {{{ */
36 int (*callback) (const char *, void *),
50 while ((entry = readdir (dh)) != NULL)
53 char abspath[PATH_MAX + 1];
56 if (entry->d_name[0] == '.')
59 d_name_len = strlen (entry->d_name);
63 if (strcasecmp (".rrd", entry->d_name + (d_name_len - 4)) != 0)
66 snprintf (abspath, sizeof (abspath), "%s/%s", dir, entry->d_name);
67 abspath[sizeof (abspath) - 1] = 0;
69 memset (&statbuf, 0, sizeof (statbuf));
71 status = stat (abspath, &statbuf);
75 if (!S_ISREG (statbuf.st_mode))
78 entry->d_name[d_name_len - 4] = 0;
80 status = (*callback) (entry->d_name, user_data);
83 } /* while (readdir) */
87 } /* }}} int foreach_rrd_file */
89 static int foreach_dir (const char *dir, /* {{{ */
90 int (*callback) (const char *, void *),
104 while ((entry = readdir (dh)) != NULL)
107 char abspath[PATH_MAX + 1];
109 if (entry->d_name[0] == '.')
112 snprintf (abspath, sizeof (abspath), "%s/%s", dir, entry->d_name);
113 abspath[sizeof (abspath) - 1] = 0;
115 memset (&statbuf, 0, sizeof (statbuf));
117 status = stat (abspath, &statbuf);
121 if (!S_ISDIR (statbuf.st_mode))
124 status = (*callback) (entry->d_name, user_data);
127 } /* while (readdir) */
131 } /* }}} int foreach_dir */
133 static int foreach_type (const char *host, const char *plugin, /* {{{ */
134 callback_type_t callback, void *user_data)
136 char abspath[PATH_MAX + 1];
138 if ((host == NULL) || (plugin == NULL))
141 snprintf (abspath, sizeof (abspath), "%s/%s/%s", DATA_DIR, host, plugin);
142 abspath[sizeof (abspath) - 1] = 0;
144 return (foreach_rrd_file (abspath, callback, user_data));
145 } /* }}} int foreach_type */
147 static int foreach_plugin (const char *host, /* {{{ */
148 callback_plugin_t callback,
151 char abspath[PATH_MAX + 1];
156 snprintf (abspath, sizeof (abspath), "%s/%s", DATA_DIR, host);
157 abspath[sizeof (abspath) - 1] = 0;
159 return (foreach_dir (abspath, callback, user_data));
160 } /* }}} int foreach_plugin */
162 static int foreach_host (callback_host_t callback, /* {{{ */
165 return (foreach_dir (DATA_DIR, callback, user_data));
166 } /* }}} int foreach_host */
169 * Functions building "fs_scan_dir_data_t" and calling the user-supplied
170 * callback eventually.
172 static int scan_type (const char *type, void *user_data) /* {{{ */
174 fs_scan_dir_data_t *data = user_data;
175 graph_ident_t *ident;
178 if ((type == NULL) || (data == NULL))
181 if ((data->type != NULL) || (data->type_instance != NULL))
184 data->type = strdup (type);
185 if (data->type == NULL)
188 data->type_instance = strchr (data->type, '-');
189 if (data->type_instance != NULL)
191 *data->type_instance = 0;
192 data->type_instance++;
196 data->type_instance = data->type + strlen (data->type);
199 ident = ident_create (data->host,
200 data->plugin, data->plugin_instance,
201 data->type, data->type_instance);
208 status = (*data->callback) (ident, data->user_data);
209 ident_destroy (ident);
214 data->type_instance = NULL;
217 } /* }}} int scan_type */
219 static int scan_plugin (const char *plugin, void *user_data) /* {{{ */
221 fs_scan_dir_data_t *data = user_data;
224 if ((plugin == NULL) || (data == NULL))
227 if ((data->plugin != NULL) || (data->plugin_instance != NULL))
230 data->plugin = strdup (plugin);
231 if (data->plugin == NULL)
234 data->plugin_instance = strchr (data->plugin, '-');
235 if (data->plugin_instance != NULL)
237 *data->plugin_instance = 0;
238 data->plugin_instance++;
242 data->plugin_instance = data->plugin + strlen (data->plugin);
245 status = foreach_type (data->host, plugin, scan_type, data);
249 data->plugin_instance = NULL;
252 } /* }}} int scan_plugin */
254 static int scan_host (const char *host, void *user_data) /* {{{ */
256 fs_scan_dir_data_t *data = user_data;
259 if ((host == NULL) || (data == NULL))
262 if (data->host != NULL)
265 data->host = strdup (host);
266 if (data->host == NULL)
269 status = foreach_plugin (host, scan_plugin, data);
275 } /* }}} int scan_host */
280 int fs_scan (fs_ident_cb_t callback, void *user_data) /* {{{ */
282 fs_scan_dir_data_t data;
284 memset (&data, 0, sizeof (data));
285 data.callback = callback;
286 data.user_data = user_data;
290 data.plugin_instance = NULL;
292 data.type_instance = NULL;
294 foreach_host (scan_host, &data);
297 } /* }}} int fs_scan */
299 /* vim: set sw=2 sts=2 et fdm=marker : */