1 /*
2 * SysDB - src/plugins/timeseries/rrdtool.c
3 * Copyright (C) 2014 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 /*
55 * plugin API
56 */
58 static sdb_timeseries_t *
59 sdb_rrd_fetch(const char *id, sdb_timeseries_opts_t *opts,
60 sdb_object_t *user_data)
61 {
62 sdb_timeseries_t *ts;
64 time_t start = (time_t)SDB_TIME_TO_SECS(opts->start);
65 time_t end = (time_t)SDB_TIME_TO_SECS(opts->end);
66 time_t t;
68 unsigned long step = 0;
69 unsigned long ds_cnt = 0;
70 unsigned long val_cnt = 0;
71 char **ds_namv = NULL;
72 rrd_value_t *data = NULL, *data_ptr;
74 if (user_data) {
75 #ifdef HAVE_RRD_CLIENT_H
76 /* -> use RRDCacheD */
77 char *addr = SDB_OBJ_WRAPPER(user_data)->data;
79 rrd_clear_error();
80 if (! rrdc_is_connected(addr)) {
81 if (rrdc_connect(addr)) {
82 sdb_log(SDB_LOG_ERR, "timeseries::rrdtool: Failed to "
83 "connectd to RRDCacheD at %s: %s",
84 addr, rrd_get_error());
85 return NULL;
86 }
87 }
89 if (rrdc_flush(id)) {
90 sdb_log(SDB_LOG_ERR, "timeseries::rrdtool: Failed to flush "
91 "'%s' through RRDCacheD: %s", id, rrd_get_error());
92 return NULL;
93 }
94 #else
95 sdb_log(SDB_LOG_ERR, "timeseries::rrdtool: Callback called with "
96 "RRDCacheD address but your build of SysDB does not support "
97 "that");
98 return NULL;
99 #endif
100 }
102 #define FREE_RRD_DATA() \
103 do { \
104 size_t i; \
105 for (i = 0; i < ds_cnt; ++i) \
106 rrd_freemem(ds_namv[i]); \
107 rrd_freemem(ds_namv); \
108 rrd_freemem(data); \
109 } while (0)
111 /* limit to about 1000 data-points for now
112 * TODO: make this configurable */
113 step = (end - start) / 1000;
115 if (rrd_fetch_r(id, "AVERAGE", &start, &end, &step,
116 &ds_cnt, &ds_namv, &data)) {
117 char errbuf[1024];
118 sdb_strerror(errno, errbuf, sizeof(errbuf));
119 sdb_log(SDB_LOG_ERR, "rrdtool plugin: Failed to fetch data "
120 "from %s: %s", id, errbuf);
121 return NULL;
122 }
124 val_cnt = (unsigned long)(end - start) / step;
126 ts = sdb_timeseries_create(ds_cnt, (const char * const *)ds_namv, val_cnt);
127 if (! ts) {
128 char errbuf[1024];
129 sdb_strerror(errno, errbuf, sizeof(errbuf));
130 sdb_log(SDB_LOG_ERR, "rrdtool plugin: Failed to allocate "
131 "time-series object: %s", errbuf);
132 FREE_RRD_DATA();
133 return NULL;
134 }
136 ts->start = SECS_TO_SDB_TIME(start + (time_t)step);
137 ts->end = SECS_TO_SDB_TIME(end);
139 data_ptr = data;
140 for (t = start + (time_t)step; t <= end; t += (time_t)step) {
141 unsigned long i, j;
143 i = (unsigned long)(t - start) / step - 1;
145 for (j = 0; j < ds_cnt; ++j) {
146 ts->data[j][i].timestamp = SECS_TO_SDB_TIME(t);
147 ts->data[j][i].value = *data_ptr;
148 ++data_ptr;
149 }
150 }
152 FREE_RRD_DATA();
153 return ts;
154 } /* sdb_rrd_fetch */
156 static int
157 sdb_rrdcached_shutdown(sdb_object_t __attribute__((unused)) *user_data)
158 {
159 #ifdef HAVE_RRD_CLIENT_H
160 rrdc_disconnect();
161 #endif
162 return 0;
163 } /* sdb_rrdcached_shutdown */
165 static int
166 sdb_rrd_config_rrdcached(oconfig_item_t *ci)
167 {
168 sdb_object_t *ud;
169 char *addr = NULL;
171 if (rrdcached_in_use) {
172 sdb_log(SDB_LOG_ERR, "timeseries::rrdtool: RRDCacheD does "
173 "not support multiple connections");
174 return -1;
175 }
177 #ifndef HAVE_RRD_CLIENT_H
178 sdb_log(SDB_LOG_ERR, "timeseries::rrdtool: RRDCacheD client "
179 "support not available in your SysDB build");
180 return -1;
181 #else
182 if (oconfig_get_string(ci, &addr)) {
183 sdb_log(SDB_LOG_ERR, "timeseries::unixsock: RRDCacheD requires "
184 "a single string argument\n\tUsage <RRDCacheD ADDR>");
185 return -1;
186 }
187 if ((*addr != '/') && strncmp(addr, "unix:", strlen("unix:"))) {
188 /* XXX: add (optional) support for rrdc_fetch if available */
189 sdb_log(SDB_LOG_ERR, "timeseries::unixsock: RRDCacheD only "
190 "supports local (UNIX socket) addresses");
191 return -1;
192 }
194 addr = strdup(addr);
195 if (! addr) {
196 char errbuf[1024];
197 sdb_log(SDB_LOG_ERR, "timeseries::unixsock: Failed to duplicate "
198 "string: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
199 return -1;
200 }
201 if (ci->children_num)
202 sdb_log(SDB_LOG_WARNING, "timeseries::unixsock: RRDCacheD does "
203 "not support any child config options");
205 ud = sdb_object_create_wrapper("rrdcached-addr", addr, free);
206 if (! ud) {
207 char errbuf[1024];
208 sdb_log(SDB_LOG_ERR, "timeseries::unixsock: Failed to create "
209 "user-data object: %s",
210 sdb_strerror(errno, errbuf, sizeof(errbuf)));
211 free(addr);
212 return -1;
213 }
215 sdb_plugin_register_ts_fetcher("rrdcached", sdb_rrd_fetch, ud);
216 sdb_plugin_register_shutdown("rrdcached", sdb_rrdcached_shutdown, NULL);
217 sdb_object_deref(ud);
218 rrdcached_in_use = 1;
219 return 0;
220 #endif
221 } /* sdb_rrd_config_rrdcached */
223 static int
224 sdb_rrd_config(oconfig_item_t *ci)
225 {
226 int i;
228 if (! ci) { /* reconfigure */
229 rrdcached_in_use = 0;
230 return 0;
231 }
233 for (i = 0; i < ci->children_num; ++i) {
234 oconfig_item_t *child = ci->children + i;
236 if (! strcasecmp(child->key, "RRDCacheD"))
237 sdb_rrd_config_rrdcached(child);
238 else
239 sdb_log(SDB_LOG_WARNING, "timeseries::rrdtool: Ignoring "
240 "unknown config option '%s'.", child->key);
241 }
242 return 0;
243 } /* sdb_rrd_config */
245 int
246 sdb_module_init(sdb_plugin_info_t *info)
247 {
248 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
249 "fetch time-series from RRD files");
250 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
251 "Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>");
252 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
253 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
254 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
256 sdb_plugin_register_ts_fetcher("rrdtool", sdb_rrd_fetch, NULL);
257 sdb_plugin_register_config(sdb_rrd_config);
258 return 0;
259 } /* sdb_module_init */
261 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */