2 * collection4 - data_provider.h
3 * Copyright (C) 2010 Florian octo Forster
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
21 * Florian octo Forster <ff at octo.it>
35 #include "graph_types.h"
36 #include "graph_config.h"
37 #include "graph_ident.h"
38 #include "data_provider.h"
39 #include "filesystem.h"
44 #include <fcgi_stdio.h>
50 typedef struct dp_rrdtool_s dp_rrdtool_t;
52 struct dp_get_idents_data_s
55 dp_get_idents_callback callback;
58 typedef struct dp_get_idents_data_s dp_get_idents_data_t;
60 static int scan_type_cb (__attribute__((unused)) const char *base_dir,
61 const char *file, void *ud)
63 dp_get_idents_data_t *data = ud;
69 file_len = strlen (file);
73 /* Ignore files that don't end in ".rrd". */
74 if (strcasecmp (".rrd", file + (file_len - 4)) != 0)
77 strncpy (type_copy, file, sizeof (type_copy));
78 type_copy_len = file_len - 4;
79 if (type_copy_len > (sizeof (type_copy) - 1))
80 type_copy_len = sizeof (type_copy) - 1;
81 type_copy[type_copy_len] = 0;
83 type_inst = strchr (type_copy, '-');
84 if (type_inst != NULL)
94 ident_set_type (data->ident, type_copy);
95 ident_set_type_instance (data->ident, type_inst);
97 return (data->callback (data->ident, data->user_data));
98 } /* }}} int scan_type_cb */
100 static int scan_plugin_cb (const char *base_dir,
101 const char *sub_dir, void *ud)
103 char plugin_copy[1024];
106 dp_get_idents_data_t *data = ud;
107 char abs_dir[PATH_MAX + 1];
109 strncpy (plugin_copy, sub_dir, sizeof (plugin_copy));
110 plugin_copy[sizeof (plugin_copy) - 1] = 0;
112 plugin_inst = strchr (plugin_copy, '-');
113 if (plugin_inst != NULL)
123 ident_set_plugin (data->ident, plugin_copy);
124 ident_set_plugin_instance (data->ident, plugin_inst);
126 snprintf (abs_dir, sizeof (abs_dir), "%s/%s", base_dir, sub_dir);
127 abs_dir[sizeof (abs_dir) - 1] = 0;
129 return (fs_foreach_file (abs_dir, scan_type_cb, data));
130 } /* }}} int scan_host_cb */
132 static int scan_host_cb (const char *base_dir,
133 const char *sub_dir, void *ud)
135 dp_get_idents_data_t *data = ud;
136 char abs_dir[PATH_MAX + 1];
138 ident_set_host (data->ident, sub_dir);
140 snprintf (abs_dir, sizeof (abs_dir), "%s/%s", base_dir, sub_dir);
141 abs_dir[sizeof (abs_dir) - 1] = 0;
143 return (fs_foreach_dir (abs_dir, scan_plugin_cb, data));
144 } /* }}} int scan_host_cb */
146 static int ident_to_rrdfile (const graph_ident_t *ident, /* {{{ */
147 dp_rrdtool_t *config,
148 char *buffer, size_t buffer_size)
150 const char *plugin_instance;
151 const char *type_instance;
153 plugin_instance = ident_get_plugin_instance (ident);
154 if ((plugin_instance != NULL) && (plugin_instance[0] == 0))
155 plugin_instance = NULL;
157 type_instance = ident_get_type_instance (ident);
158 if ((type_instance != NULL) && (type_instance[0] == 0))
159 type_instance = NULL;
163 strlcat (buffer, config->data_dir, buffer_size);
164 strlcat (buffer, "/", buffer_size);
166 strlcat (buffer, ident_get_host (ident), buffer_size);
167 strlcat (buffer, "/", buffer_size);
168 strlcat (buffer, ident_get_plugin (ident), buffer_size);
169 if (plugin_instance != NULL)
171 strlcat (buffer, "-", buffer_size);
172 strlcat (buffer, plugin_instance, buffer_size);
174 strlcat (buffer, "/", buffer_size);
175 strlcat (buffer, ident_get_type (ident), buffer_size);
176 if (type_instance != NULL)
178 strlcat (buffer, "-", buffer_size);
179 strlcat (buffer, type_instance, buffer_size);
182 strlcat (buffer, ".rrd", buffer_size);
185 } /* }}} int ident_to_rrdfile */
190 static int get_idents (void *priv,
191 dp_get_idents_callback cb, void *ud)
193 dp_rrdtool_t *config = priv;
194 dp_get_idents_data_t data;
197 data.ident = ident_create ("", "", "", "", "");
198 if (data.ident == NULL)
203 status = fs_foreach_dir (config->data_dir, scan_host_cb, &data);
205 ident_destroy (data.ident);
207 } /* }}} int get_idents */
209 static int get_ident_ds_names (void *priv, graph_ident_t *ident,
210 dp_list_get_ident_ds_names_callback cb, void *ud)
212 dp_rrdtool_t *config = priv;
213 char file[PATH_MAX + 1];
216 char *rrd_argv[] = { "info", file, NULL };
217 int rrd_argc = (sizeof (rrd_argv) / sizeof (rrd_argv[0])) - 1;
222 memset (file, 0, sizeof (file));
223 status = ident_to_rrdfile (ident, config, file, sizeof (file));
227 info = rrd_info (rrd_argc, rrd_argv);
230 fprintf (stderr, "%s: rrd_info (%s) failed.\n", __func__, file);
235 for (ptr = info; ptr != NULL; ptr = ptr->next)
241 if (ptr->key[0] != 'd')
244 if (strncmp ("ds[", ptr->key, strlen ("ds[")) != 0)
247 keylen = strlen (ptr->key);
248 if (keylen < strlen ("ds[?].type"))
251 dslen = keylen - strlen ("ds[].type");
254 if (strcmp ("].type", ptr->key + (strlen ("ds[") + dslen)) != 0)
257 ds = malloc (dslen + 1);
261 memcpy (ds, ptr->key + strlen ("ds["), dslen);
264 status = (*cb) (ident, ds, ud);
272 rrd_info_free (info);
275 } /* }}} int get_ident_ds_names */
277 static int get_ident_data (void *priv,
278 graph_ident_t *ident, const char *ds_name,
279 dp_time_t begin, dp_time_t end,
280 dp_get_ident_data_callback cb, void *ud)
282 dp_rrdtool_t *config = priv;
284 char filename[PATH_MAX + 1];
285 const char *cf = "AVERAGE"; /* FIXME */
289 unsigned long ds_count;
294 dp_time_t first_value_time;
296 size_t data_points_num;
297 double *data_points = NULL;
299 unsigned long ds_index;
302 status = ident_to_rrdfile (ident, config, filename, sizeof (filename));
306 rrd_start = (time_t) begin.tv_sec;
307 rrd_end = (time_t) end.tv_sec;
313 status = rrd_fetch_r (filename, cf,
314 &rrd_start, &rrd_end,
315 &step, &ds_count, &ds_namv,
320 #define BAIL_OUT(ret_status) do { \
322 for (i = 0; i < ds_count; i++) \
326 free (data_points); \
327 return (ret_status); \
330 for (ds_index = 0; ds_index < ds_count; ds_index++)
331 if (strcmp (ds_name, ds_namv[ds_index]) == 0)
334 if (ds_index >= ds_count)
337 memset (&first_value_time, 0, sizeof (first_value_time));
338 first_value_time.tv_sec = rrd_start;
339 memset (&interval, 0, sizeof (interval));
340 interval.tv_sec = (time_t) step;
341 data_points_num = (size_t) ((rrd_end - rrd_start) / step);
342 data_points = calloc (data_points_num, sizeof (*data_points));
343 if (data_points == NULL)
346 for (i = 0; i < data_points_num; i++)
348 unsigned long index = (ds_count * ((unsigned long) i)) + ds_index;
349 data_points[i] = (double) data[index];
352 status = (*cb) (ident, ds_name, first_value_time, interval,
353 data_points_num, data_points, ud);
359 } /* }}} int get_ident_data */
361 static int print_graph (void *priv,
362 graph_config_t *cfg, graph_instance_t *inst)
369 } /* }}} int print_graph */
371 int dp_rrdtool_config (const oconfig_item_t *ci)
382 /* private_data = */ NULL
385 conf = malloc (sizeof (*conf));
388 memset (conf, 0, sizeof (*conf));
389 conf->data_dir = NULL;
391 for (i = 0; i < ci->children_num; i++)
393 oconfig_item_t *child = ci->children + i;
395 if (strcasecmp ("DataDir", child->key) == 0)
396 graph_config_get_string (child, &conf->data_dir);
399 fprintf (stderr, "dp_rrdtool_config: Ignoring unknown config option "
400 "\"%s\"\n", child->key);
405 if (conf->data_dir == NULL)
406 conf->data_dir = strdup ("/var/lib/collectd/rrd");
407 if (conf->data_dir == NULL)
413 dp.private_data = conf;
415 data_provider_register ("rrdtool", &dp);
418 } /* }}} int dp_rrdtool_config */
420 /* vim: set sw=2 sts=2 et fdm=marker : */