Code

timeseries::rrdtool: Added support for RRDCacheD.
[sysdb.git] / src / plugins / timeseries / rrdtool.c
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         if (rrd_fetch_r(id, "AVERAGE", &start, &end, &step,
110                                 &ds_cnt, &ds_namv, &data)) {
111                 char errbuf[1024];
112                 sdb_strerror(errno, errbuf, sizeof(errbuf));
113                 sdb_log(SDB_LOG_ERR, "rrdtool plugin: Failed to fetch data "
114                                 "from %s: %s", id, errbuf);
115                 return NULL;
116         }
118         val_cnt = (unsigned long)(end - start) / step;
120         ts = sdb_timeseries_create(ds_cnt, (const char * const *)ds_namv, val_cnt);
121         if (! ts) {
122                 char errbuf[1024];
123                 sdb_strerror(errno, errbuf, sizeof(errbuf));
124                 sdb_log(SDB_LOG_ERR, "rrdtool plugin: Failed to allocate "
125                                 "time-series object: %s", errbuf);
126                 FREE_RRD_DATA();
127                 return NULL;
128         }
130         ts->start = SECS_TO_SDB_TIME(start + (time_t)step);
131         ts->end = SECS_TO_SDB_TIME(end);
133         data_ptr = data;
134         for (t = start + (time_t)step; t <= end; t += (time_t)step) {
135                 unsigned long i, j;
137                 i = (unsigned long)(t - start) / step - 1;
139                 for (j = 0; j < ds_cnt; ++j) {
140                         ts->data[j][i].timestamp = SECS_TO_SDB_TIME(t);
141                         ts->data[j][i].value = *data_ptr;
142                         ++data_ptr;
143                 }
144         }
146         FREE_RRD_DATA();
147         return ts;
148 } /* sdb_rrd_fetch */
150 static int
151 sdb_rrdcached_shutdown(sdb_object_t __attribute__((unused)) *user_data)
153 #ifdef HAVE_RRD_CLIENT_H
154         rrdc_disconnect();
155 #endif
156         return 0;
157 } /* sdb_rrdcached_shutdown */
159 static int
160 sdb_rrd_config_rrdcached(oconfig_item_t *ci)
162         sdb_object_t *ud;
163         char *addr = NULL;
165         if (rrdcached_in_use) {
166                 sdb_log(SDB_LOG_ERR, "timeseries::rrdtool: RRDCacheD does "
167                                 "not support multiple connections");
168                 return -1;
169         }
171 #ifndef HAVE_RRD_CLIENT_H
172         sdb_log(SDB_LOG_ERR, "timeseries::rrdtool: RRDCacheD client "
173                         "support not available in your SysDB build");
174         return -1;
175 #else
176         if (oconfig_get_string(ci, &addr)) {
177                 sdb_log(SDB_LOG_ERR, "timeseries::unixsock: RRDCacheD requires "
178                                 "a single string argument\n\tUsage <RRDCacheD ADDR>");
179                 return -1;
180         }
181         if ((*addr != '/') && strncmp(addr, "unix:", strlen("unix:"))) {
182                 /* XXX: add (optional) support for rrdc_fetch if available */
183                 sdb_log(SDB_LOG_ERR, "timeseries::unixsock: RRDCacheD only "
184                                 "supports local (UNIX socket) addresses");
185                 return -1;
186         }
188         addr = strdup(addr);
189         if (! addr) {
190                 char errbuf[1024];
191                 sdb_log(SDB_LOG_ERR, "timeseries::unixsock: Failed to duplicate "
192                                 "string: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
193                 return -1;
194         }
195         if (ci->children_num)
196                 sdb_log(SDB_LOG_WARNING, "timeseries::unixsock: RRDCacheD does "
197                                 "not support any child config options");
199         ud = sdb_object_create_wrapper("rrdcached-addr", addr, free);
200         if (! ud) {
201                 char errbuf[1024];
202                 sdb_log(SDB_LOG_ERR, "timeseries::unixsock: Failed to create "
203                                 "user-data object: %s",
204                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
205                 free(addr);
206                 return -1;
207         }
209         sdb_plugin_register_ts_fetcher("rrdcached", sdb_rrd_fetch, ud);
210         sdb_plugin_register_shutdown("rrdcached", sdb_rrdcached_shutdown, NULL);
211         sdb_object_deref(ud);
212         rrdcached_in_use = 1;
213         return 0;
214 #endif
215 } /* sdb_rrd_config_rrdcached */
217 static int
218 sdb_rrd_config(oconfig_item_t *ci)
220         int i;
222         if (! ci) { /* reconfigure */
223                 rrdcached_in_use = 0;
224                 return 0;
225         }
227         for (i = 0; i < ci->children_num; ++i) {
228                 oconfig_item_t *child = ci->children + i;
230                 if (! strcasecmp(child->key, "RRDCacheD"))
231                         sdb_rrd_config_rrdcached(child);
232                 else
233                         sdb_log(SDB_LOG_WARNING, "timeseries::rrdtool: Ignoring "
234                                         "unknown config option '%s'.", child->key);
235         }
236         return 0;
237 } /* sdb_rrd_config */
239 int
240 sdb_module_init(sdb_plugin_info_t *info)
242         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
243                         "fetch time-series from RRD files");
244         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
245                         "Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>");
246         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
247         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
248         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
250         sdb_plugin_register_ts_fetcher("rrdtool", sdb_rrd_fetch, NULL);
251         sdb_plugin_register_config(sdb_rrd_config);
252         return 0;
253 } /* sdb_module_init */
255 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */