2 * collectd - src/rrdcached.c
3 * Copyright (C) 2008-2013 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
30 #include "utils/common/common.h"
31 #include "utils/rrdcreate/rrdcreate.h"
35 #include <rrd_client.h>
41 static char *daemon_address;
42 static bool config_create_files = true;
43 static bool config_collect_stats = true;
44 static rrdcreate_config_t rrdcreate_config = {.stepsize = 0,
50 .consolidation_functions = NULL,
51 .consolidation_functions_num = 0,
57 static int rc_write(const data_set_t *ds, const value_list_t *vl,
58 __attribute__((unused)) user_data_t *ud);
59 static int rc_flush(__attribute__((unused)) cdtime_t timeout,
60 const char *identifier,
61 __attribute__((unused)) user_data_t *ud);
63 static int value_list_to_string(char *buffer, int buffer_len,
64 const data_set_t *ds, const value_list_t *vl) {
65 assert(strcmp(ds->type, vl->type) == 0);
67 memset(buffer, '\0', buffer_len);
70 ssnprintf(buffer, buffer_len, "%.6f", CDTIME_T_TO_DOUBLE(vl->time));
71 if ((status < 1) || (status >= buffer_len))
75 for (size_t i = 0; i < ds->ds_num; i++) {
76 if ((ds->ds[i].type != DS_TYPE_COUNTER) &&
77 (ds->ds[i].type != DS_TYPE_GAUGE) &&
78 (ds->ds[i].type != DS_TYPE_DERIVE) &&
79 (ds->ds[i].type != DS_TYPE_ABSOLUTE))
82 if (ds->ds[i].type == DS_TYPE_COUNTER) {
83 status = ssnprintf(buffer + offset, buffer_len - offset, ":%" PRIu64,
84 (uint64_t)vl->values[i].counter);
85 } else if (ds->ds[i].type == DS_TYPE_GAUGE) {
86 status = ssnprintf(buffer + offset, buffer_len - offset, ":%f",
88 } else if (ds->ds[i].type == DS_TYPE_DERIVE) {
89 status = ssnprintf(buffer + offset, buffer_len - offset, ":%" PRIi64,
90 vl->values[i].derive);
91 } else /* if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */ {
92 status = ssnprintf(buffer + offset, buffer_len - offset, ":%" PRIu64,
93 vl->values[i].absolute);
96 if ((status < 1) || (status >= (buffer_len - offset)))
100 } /* for ds->ds_num */
103 } /* int value_list_to_string */
105 static int value_list_to_filename(char *buffer, size_t buffer_size,
106 value_list_t const *vl) {
107 char const suffix[] = ".rrd";
109 if (datadir != NULL) {
110 size_t datadir_len = strlen(datadir) + 1;
112 if (datadir_len >= buffer_size)
115 sstrncpy(buffer, datadir, buffer_size);
116 buffer[datadir_len - 1] = '/';
117 buffer[datadir_len] = '\0';
119 buffer += datadir_len;
120 buffer_size -= datadir_len;
123 int status = FORMAT_VL(buffer, buffer_size, vl);
127 size_t len = strlen(buffer);
128 assert(len < buffer_size);
132 if (buffer_size <= sizeof(suffix))
135 memcpy(buffer, suffix, sizeof(suffix));
137 } /* int value_list_to_filename */
139 static int rc_config_get_int_positive(oconfig_item_t const *ci, int *ret) {
142 int status = cf_util_get_int(ci, &tmp);
150 } /* int rc_config_get_int_positive */
152 static int rc_config_get_xff(oconfig_item_t const *ci, double *ret) {
153 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
154 ERROR("rrdcached plugin: The \"%s\" needs exactly one numeric argument "
155 "in the range [0.0, 1.0)",
160 double value = ci->values[0].value.number;
161 if ((value >= 0.0) && (value < 1.0)) {
166 ERROR("rrdcached plugin: The \"%s\" needs exactly one numeric argument "
167 "in the range [0.0, 1.0)",
170 } /* int rc_config_get_xff */
172 static int rc_config_add_timespan(int timespan) {
176 int *tmp = realloc(rrdcreate_config.timespans,
177 sizeof(*rrdcreate_config.timespans) *
178 (rrdcreate_config.timespans_num + 1));
181 rrdcreate_config.timespans = tmp;
183 rrdcreate_config.timespans[rrdcreate_config.timespans_num] = timespan;
184 rrdcreate_config.timespans_num++;
187 } /* int rc_config_add_timespan */
189 static int rc_config(oconfig_item_t *ci) {
190 for (int i = 0; i < ci->children_num; i++) {
191 oconfig_item_t const *child = ci->children + i;
192 const char *key = child->key;
195 if (strcasecmp("DataDir", key) == 0) {
196 status = cf_util_get_string(child, &datadir);
198 int len = strlen(datadir);
200 while ((len > 0) && (datadir[len - 1] == '/')) {
208 } else if (strcasecmp("DaemonAddress", key) == 0)
209 status = cf_util_get_string(child, &daemon_address);
210 else if (strcasecmp("CreateFiles", key) == 0)
211 status = cf_util_get_boolean(child, &config_create_files);
212 else if (strcasecmp("CreateFilesAsync", key) == 0)
213 status = cf_util_get_boolean(child, &rrdcreate_config.async);
214 else if (strcasecmp("CollectStatistics", key) == 0)
215 status = cf_util_get_boolean(child, &config_collect_stats);
216 else if (strcasecmp("StepSize", key) == 0) {
219 status = rc_config_get_int_positive(child, &tmp);
221 rrdcreate_config.stepsize = (unsigned long)tmp;
222 } else if (strcasecmp("HeartBeat", key) == 0)
223 status = rc_config_get_int_positive(child, &rrdcreate_config.heartbeat);
224 else if (strcasecmp("RRARows", key) == 0)
225 status = rc_config_get_int_positive(child, &rrdcreate_config.rrarows);
226 else if (strcasecmp("RRATimespan", key) == 0) {
228 status = rc_config_get_int_positive(child, &tmp);
230 status = rc_config_add_timespan(tmp);
231 } else if (strcasecmp("XFF", key) == 0)
232 status = rc_config_get_xff(child, &rrdcreate_config.xff);
234 WARNING("rrdcached plugin: Ignoring invalid option %s.", key);
239 WARNING("rrdcached plugin: Handling the \"%s\" option failed.", key);
242 if (daemon_address != NULL) {
243 plugin_register_write("rrdcached", rc_write, /* user_data = */ NULL);
244 plugin_register_flush("rrdcached", rc_flush, /* user_data = */ NULL);
247 } /* int rc_config */
249 static int try_reconnect(void) {
253 int status = rrdc_connect(daemon_address);
255 ERROR("rrdcached plugin: Failed to reconnect to RRDCacheD "
256 "at %s: %s (status=%d)",
257 daemon_address, rrd_get_error(), status);
261 INFO("rrdcached plugin: Successfully reconnected to RRDCacheD "
265 } /* int try_reconnect */
267 static int rc_read(void) {
269 value_list_t vl = VALUE_LIST_INIT;
270 vl.values = &(value_t){.gauge = NAN};
273 if (daemon_address == NULL)
276 if (!config_collect_stats)
279 if ((strncmp("unix:", daemon_address, strlen("unix:")) != 0) &&
280 (daemon_address[0] != '/'))
281 sstrncpy(vl.host, daemon_address, sizeof(vl.host));
282 sstrncpy(vl.plugin, "rrdcached", sizeof(vl.plugin));
285 int status = rrdc_connect(daemon_address);
287 ERROR("rrdcached plugin: Failed to connect to RRDCacheD "
288 "at %s: %s (status=%d)",
289 daemon_address, rrd_get_error(), status);
294 bool retried = false;
297 /* The RRD client lib does not provide any means for checking a
298 * connection, hence we'll have to retry upon failed operations. */
301 status = rrdc_stats_get(&head);
307 if (try_reconnect() == 0)
309 /* else: report the error and fail */
312 ERROR("rrdcached plugin: rrdc_stats_get failed: %s (status=%i).",
313 rrd_get_error(), status);
317 for (rrdc_stats_t *ptr = head; ptr != NULL; ptr = ptr->next) {
318 if (ptr->type == RRDC_STATS_TYPE_GAUGE)
319 vl.values[0].gauge = (gauge_t)ptr->value.gauge;
320 else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
321 vl.values[0].counter = (counter_t)ptr->value.counter;
325 if (strcasecmp("QueueLength", ptr->name) == 0) {
326 sstrncpy(vl.type, "queue_length", sizeof(vl.type));
327 sstrncpy(vl.type_instance, "", sizeof(vl.type_instance));
328 } else if (strcasecmp("UpdatesWritten", ptr->name) == 0) {
329 sstrncpy(vl.type, "operations", sizeof(vl.type));
330 sstrncpy(vl.type_instance, "write-updates", sizeof(vl.type_instance));
331 } else if (strcasecmp("DataSetsWritten", ptr->name) == 0) {
332 sstrncpy(vl.type, "operations", sizeof(vl.type));
333 sstrncpy(vl.type_instance, "write-data_sets", sizeof(vl.type_instance));
334 } else if (strcasecmp("TreeNodesNumber", ptr->name) == 0) {
335 sstrncpy(vl.type, "gauge", sizeof(vl.type));
336 sstrncpy(vl.type_instance, "tree_nodes", sizeof(vl.type_instance));
337 } else if (strcasecmp("TreeDepth", ptr->name) == 0) {
338 sstrncpy(vl.type, "gauge", sizeof(vl.type));
339 sstrncpy(vl.type_instance, "tree_depth", sizeof(vl.type_instance));
340 } else if (strcasecmp("FlushesReceived", ptr->name) == 0) {
341 sstrncpy(vl.type, "operations", sizeof(vl.type));
342 sstrncpy(vl.type_instance, "receive-flush", sizeof(vl.type_instance));
343 } else if (strcasecmp("JournalBytes", ptr->name) == 0) {
344 sstrncpy(vl.type, "counter", sizeof(vl.type));
345 sstrncpy(vl.type_instance, "journal-bytes", sizeof(vl.type_instance));
346 } else if (strcasecmp("JournalRotate", ptr->name) == 0) {
347 sstrncpy(vl.type, "counter", sizeof(vl.type));
348 sstrncpy(vl.type_instance, "journal-rotates", sizeof(vl.type_instance));
349 } else if (strcasecmp("UpdatesReceived", ptr->name) == 0) {
350 sstrncpy(vl.type, "operations", sizeof(vl.type));
351 sstrncpy(vl.type_instance, "receive-update", sizeof(vl.type_instance));
353 DEBUG("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
357 plugin_dispatch_values(&vl);
358 } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
360 rrdc_stats_free(head);
365 static int rc_init(void) {
366 if (config_collect_stats)
367 plugin_register_read("rrdcached", rc_read);
372 static int rc_write(const data_set_t *ds, const value_list_t *vl,
373 user_data_t __attribute__((unused)) * user_data) {
374 char filename[PATH_MAX];
377 bool retried = false;
379 if (daemon_address == NULL) {
380 ERROR("rrdcached plugin: daemon_address == NULL.");
381 plugin_unregister_write("rrdcached");
385 if (strcmp(ds->type, vl->type) != 0) {
386 ERROR("rrdcached plugin: DS type does not match value list type");
390 if (value_list_to_filename(filename, sizeof(filename), vl) != 0) {
391 ERROR("rrdcached plugin: value_list_to_filename failed.");
395 if (value_list_to_string(values, sizeof(values), ds, vl) != 0) {
396 ERROR("rrdcached plugin: value_list_to_string failed.");
400 if (config_create_files) {
403 status = stat(filename, &statbuf);
405 if (errno != ENOENT) {
406 ERROR("rrdcached plugin: stat (%s) failed: %s", filename, STRERRNO);
410 status = cu_rrd_create_file(filename, ds, vl, &rrdcreate_config);
412 ERROR("rrdcached plugin: cu_rrd_create_file (%s) failed.", filename);
414 } else if (rrdcreate_config.async)
420 status = rrdc_connect(daemon_address);
422 ERROR("rrdcached plugin: Failed to connect to RRDCacheD "
423 "at %s: %s (status=%d)",
424 daemon_address, rrd_get_error(), status);
428 char *values_array[2] = {
434 /* The RRD client lib does not provide any means for checking a
435 * connection, hence we'll have to retry upon failed operations. */
437 status = rrdc_update(filename, /* values_num = */ 1, (void *)values_array);
443 if (try_reconnect() == 0)
445 /* else: report the error and fail */
448 ERROR("rrdcached plugin: rrdc_update (%s, [%s], 1) failed: %s (status=%i)",
449 filename, values_array[0], rrd_get_error(), status);
456 static int rc_flush(__attribute__((unused)) cdtime_t timeout, /* {{{ */
457 const char *identifier,
458 __attribute__((unused)) user_data_t *ud) {
459 if (identifier == NULL)
462 char filename[PATH_MAX + 1];
465 ssnprintf(filename, sizeof(filename), "%s/%s.rrd", datadir, identifier);
467 ssnprintf(filename, sizeof(filename), "%s.rrd", identifier);
470 int status = rrdc_connect(daemon_address);
472 ERROR("rrdcached plugin: Failed to connect to RRDCacheD "
473 "at %s: %s (status=%d)",
474 daemon_address, rrd_get_error(), status);
478 bool retried = false;
481 /* The RRD client lib does not provide any means for checking a
482 * connection, hence we'll have to retry upon failed operations. */
484 status = rrdc_flush(filename);
490 if (try_reconnect() == 0)
492 /* else: report the error and fail */
495 ERROR("rrdcached plugin: rrdc_flush (%s) failed: %s (status=%i).", filename,
496 rrd_get_error(), status);
499 DEBUG("rrdcached plugin: rrdc_flush (%s): Success.", filename);
502 } /* }}} int rc_flush */
504 static int rc_shutdown(void) {
507 } /* int rc_shutdown */
509 void module_register(void) {
510 plugin_register_complex_config("rrdcached", rc_config);
511 plugin_register_init("rrdcached", rc_init);
512 plugin_register_shutdown("rrdcached", rc_shutdown);
513 } /* void module_register */