1 /*
2 * SysDB - src/plugins/timeseries/rrdtool.c
3 * Copyright (C) 2014-2016 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
33 #include "core/plugin.h"
34 #include "utils/error.h"
36 #include "liboconfig/utils.h"
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <strings.h>
42 #include <rrd.h>
43 #ifdef HAVE_RRD_CLIENT_H
44 # include <rrd_client.h>
45 #endif
47 SDB_PLUGIN_MAGIC;
49 /* Current versions of RRDtool do not support multiple RRDCacheD client
50 * connections. Use this to guard against multiple configured RRDCacheD
51 * instances. */
52 static bool rrdcached_in_use = 0;
54 static bool
55 rrdcached_connect(char *addr)
56 {
57 #ifdef HAVE_RRD_CLIENT_H
58 rrd_clear_error();
59 if (! rrdc_is_connected(addr)) {
60 if (rrdc_connect(addr)) {
61 sdb_log(SDB_LOG_ERR, "Failed to connectd to RRDCacheD at %s: %s",
62 addr, rrd_get_error());
63 return 0;
64 }
65 }
66 #else
67 sdb_log(SDB_LOG_ERR, "Callback called with RRDCacheD address "
68 "but your build of SysDB does not support that");
69 return 0;
70 #endif
71 return 1;
72 } /* rrdcached_connect */
74 /*
75 * plugin API
76 */
78 static sdb_timeseries_info_t *
79 sdb_rrd_describe(const char *id, sdb_object_t *user_data)
80 {
81 rrd_info_t *info, *iter;
82 char filename[strlen(id) + 1];
83 sdb_timeseries_info_t *ts_info;
85 strncpy(filename, id, sizeof(filename));
87 if (user_data) {
88 /* -> use RRDCacheD */
89 char *addr = SDB_OBJ_WRAPPER(user_data)->data;
91 if (! rrdcached_connect(addr))
92 return NULL;
94 #ifdef HAVE_RRD_CLIENT_H
95 /* TODO: detect and use rrdc_info if possible */
96 sdb_log(SDB_LOG_ERR, "DESCRIBE not yet supported via RRDCacheD");
97 return NULL;
98 #endif
99 }
100 else {
101 rrd_clear_error();
102 info = rrd_info_r(filename);
103 }
104 if (! info) {
105 sdb_log(SDB_LOG_ERR, "Failed to extract header information from '%s': %s",
106 filename, rrd_get_error());
107 return NULL;
108 }
110 ts_info = calloc(1, sizeof(*ts_info));
111 if (! ts_info) {
112 sdb_log(SDB_LOG_ERR, "Failed to allocate memory");
113 rrd_info_free(info);
114 return NULL;
115 }
117 for (iter = info; iter != NULL; iter = iter->next) {
118 size_t len, n, m;
119 char *ds_name;
120 char **tmp;
122 /* Parse the DS name. The raw value is not exposed via the rrd_info
123 * interface. */
124 n = strlen("ds[");
125 if (strncmp(iter->key, "ds[", n))
126 continue;
128 len = strlen(iter->key);
129 m = strlen("].index");
130 if ((len < m) || strcmp(iter->key + len - m, "].index"))
131 continue;
133 ds_name = iter->key + n;
134 len -= n;
135 ds_name[len - m] = '\0';
137 /* Append the new datum. */
138 tmp = realloc(ts_info->data_names,
139 (ts_info->data_names_len + 1) * sizeof(*ts_info->data_names));
140 if (! tmp) {
141 sdb_log(SDB_LOG_ERR, "Failed to allocate memory");
142 sdb_timeseries_info_destroy(ts_info);
143 rrd_info_free(info);
144 return NULL;
145 }
147 ts_info->data_names = tmp;
148 ts_info->data_names[ts_info->data_names_len] = strdup(ds_name);
149 if (! ts_info->data_names[ts_info->data_names_len]) {
150 sdb_log(SDB_LOG_ERR, "Failed to allocate memory");
151 sdb_timeseries_info_destroy(ts_info);
152 rrd_info_free(info);
153 return NULL;
154 }
155 ts_info->data_names_len++;
156 }
157 rrd_info_free(info);
159 return ts_info;
160 } /* sdb_rrd_describe */
162 static sdb_timeseries_t *
163 sdb_rrd_fetch(const char *id, sdb_timeseries_opts_t *opts,
164 sdb_object_t *user_data)
165 {
166 sdb_timeseries_t *ts;
168 time_t start = (time_t)SDB_TIME_TO_SECS(opts->start);
169 time_t end = (time_t)SDB_TIME_TO_SECS(opts->end);
170 time_t t;
172 unsigned long step = 0;
173 unsigned long ds_cnt = 0;
174 unsigned long val_cnt = 0;
175 char **ds_namv = NULL;
176 rrd_value_t *data = NULL, *data_ptr;
178 if (user_data) {
179 /* -> use RRDCacheD */
180 char *addr = SDB_OBJ_WRAPPER(user_data)->data;
182 if (! rrdcached_connect(addr))
183 return NULL;
185 #ifdef HAVE_RRD_CLIENT_H
186 if (rrdc_flush(id)) {
187 sdb_log(SDB_LOG_ERR, "Failed to flush '%s' through RRDCacheD: %s",
188 id, rrd_get_error());
189 return NULL;
190 }
191 #endif
192 }
194 #define FREE_RRD_DATA() \
195 do { \
196 size_t i; \
197 for (i = 0; i < ds_cnt; ++i) \
198 rrd_freemem(ds_namv[i]); \
199 rrd_freemem(ds_namv); \
200 rrd_freemem(data); \
201 } while (0)
203 /* limit to about 1000 data-points for now
204 * TODO: make this configurable */
205 step = (end - start) / 1000;
207 if (rrd_fetch_r(id, "AVERAGE", &start, &end, &step,
208 &ds_cnt, &ds_namv, &data)) {
209 char errbuf[1024];
210 sdb_strerror(errno, errbuf, sizeof(errbuf));
211 sdb_log(SDB_LOG_ERR, "Failed to fetch data from %s: %s", id, errbuf);
212 return NULL;
213 }
215 val_cnt = (unsigned long)(end - start) / step;
217 ts = sdb_timeseries_create(ds_cnt, (const char * const *)ds_namv, val_cnt);
218 if (! ts) {
219 char errbuf[1024];
220 sdb_strerror(errno, errbuf, sizeof(errbuf));
221 sdb_log(SDB_LOG_ERR, "Failed to allocate time-series object: %s", errbuf);
222 FREE_RRD_DATA();
223 return NULL;
224 }
226 ts->start = SECS_TO_SDB_TIME(start + (time_t)step);
227 ts->end = SECS_TO_SDB_TIME(end);
229 data_ptr = data;
230 for (t = start + (time_t)step; t <= end; t += (time_t)step) {
231 unsigned long i, j;
233 i = (unsigned long)(t - start) / step - 1;
235 for (j = 0; j < ds_cnt; ++j) {
236 ts->data[j][i].timestamp = SECS_TO_SDB_TIME(t);
237 ts->data[j][i].value = *data_ptr;
238 ++data_ptr;
239 }
240 }
242 FREE_RRD_DATA();
243 return ts;
244 } /* sdb_rrd_fetch */
246 static sdb_timeseries_fetcher_t fetcher_impl = {
247 sdb_rrd_describe, sdb_rrd_fetch,
248 };
250 static int
251 sdb_rrdcached_shutdown(sdb_object_t __attribute__((unused)) *user_data)
252 {
253 #ifdef HAVE_RRD_CLIENT_H
254 rrdc_disconnect();
255 #endif
256 return 0;
257 } /* sdb_rrdcached_shutdown */
259 static int
260 sdb_rrd_config_rrdcached(oconfig_item_t *ci)
261 {
262 sdb_object_t *ud;
263 char *addr = NULL;
265 if (rrdcached_in_use) {
266 sdb_log(SDB_LOG_ERR, "RRDCacheD does not support multiple connections");
267 return -1;
268 }
270 #ifndef HAVE_RRD_CLIENT_H
271 sdb_log(SDB_LOG_ERR, "RRDCacheD client support not available in your SysDB build");
272 return -1;
273 #else
274 if (oconfig_get_string(ci, &addr)) {
275 sdb_log(SDB_LOG_ERR, "RRDCacheD requires a single string argument\n"
276 "\tUsage <RRDCacheD ADDR>");
277 return -1;
278 }
279 if ((*addr != '/') && strncmp(addr, "unix:", strlen("unix:"))) {
280 /* XXX: add (optional) support for rrdc_fetch if available */
281 sdb_log(SDB_LOG_ERR, "RRDCacheD only supports local (UNIX socket) addresses");
282 return -1;
283 }
285 addr = strdup(addr);
286 if (! addr) {
287 char errbuf[1024];
288 sdb_log(SDB_LOG_ERR, "Failed to duplicate string: %s",
289 sdb_strerror(errno, errbuf, sizeof(errbuf)));
290 return -1;
291 }
292 if (ci->children_num)
293 sdb_log(SDB_LOG_WARNING, "RRDCacheD does not support any child config options");
295 ud = sdb_object_create_wrapper("rrdcached-addr", addr, free);
296 if (! ud) {
297 char errbuf[1024];
298 sdb_log(SDB_LOG_ERR, "Failed to create user-data object: %s",
299 sdb_strerror(errno, errbuf, sizeof(errbuf)));
300 free(addr);
301 return -1;
302 }
304 sdb_plugin_register_timeseries_fetcher("rrdcached", &fetcher_impl, ud);
305 sdb_plugin_register_shutdown("rrdcached", sdb_rrdcached_shutdown, NULL);
306 sdb_object_deref(ud);
307 rrdcached_in_use = 1;
308 return 0;
309 #endif
310 } /* sdb_rrd_config_rrdcached */
312 static int
313 sdb_rrd_config(oconfig_item_t *ci)
314 {
315 int i;
317 if (! ci) { /* reconfigure */
318 rrdcached_in_use = 0;
319 return 0;
320 }
322 for (i = 0; i < ci->children_num; ++i) {
323 oconfig_item_t *child = ci->children + i;
325 if (! strcasecmp(child->key, "RRDCacheD"))
326 sdb_rrd_config_rrdcached(child);
327 else
328 sdb_log(SDB_LOG_WARNING, "Ignoring unknown config option '%s'.", child->key);
329 }
330 return 0;
331 } /* sdb_rrd_config */
333 int
334 sdb_module_init(sdb_plugin_info_t *info)
335 {
336 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
337 "fetch time-series from RRD files");
338 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
339 "Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>");
340 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
341 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
342 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
344 sdb_plugin_register_timeseries_fetcher("rrdtool", &fetcher_impl, NULL);
345 sdb_plugin_register_config(sdb_rrd_config);
346 return 0;
347 } /* sdb_module_init */
349 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */