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