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>
31 #include "utils_rrdcreate.h"
35 #include <rrd_client.h>
40 static char *datadir = NULL;
41 static char *daemon_address = NULL;
42 static _Bool config_create_files = 1;
43 static _Bool config_collect_stats = 1;
44 static rrdcreate_config_t rrdcreate_config = {
50 /* timespans = */ NULL,
51 /* timespans_num = */ 0,
53 /* consolidation_functions = */ NULL,
54 /* consolidation_functions_num = */ 0,
61 static int rc_write(const data_set_t *ds, const value_list_t *vl,
62 user_data_t __attribute__((unused)) * user_data);
63 static int rc_flush(__attribute__((unused)) cdtime_t timeout,
64 const char *identifier,
65 __attribute__((unused)) user_data_t *ud);
67 static int value_list_to_string(char *buffer, int buffer_len,
68 const data_set_t *ds, const value_list_t *vl) {
73 assert(0 == strcmp(ds->type, vl->type));
75 memset(buffer, '\0', buffer_len);
77 t = CDTIME_T_TO_TIME_T(vl->time);
78 status = snprintf(buffer, buffer_len, "%lu", (unsigned long)t);
79 if ((status < 1) || (status >= buffer_len))
83 for (size_t i = 0; i < ds->ds_num; i++) {
84 if ((ds->ds[i].type != DS_TYPE_COUNTER) &&
85 (ds->ds[i].type != DS_TYPE_GAUGE) &&
86 (ds->ds[i].type != DS_TYPE_DERIVE) &&
87 (ds->ds[i].type != DS_TYPE_ABSOLUTE))
90 if (ds->ds[i].type == DS_TYPE_COUNTER) {
91 status = snprintf(buffer + offset, buffer_len - offset, ":%" PRIu64,
92 (uint64_t)vl->values[i].counter);
93 } else if (ds->ds[i].type == DS_TYPE_GAUGE) {
94 status = snprintf(buffer + offset, buffer_len - offset, ":%f",
96 } else if (ds->ds[i].type == DS_TYPE_DERIVE) {
97 status = snprintf(buffer + offset, buffer_len - offset, ":%" PRIi64,
98 vl->values[i].derive);
99 } else /* if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */ {
100 status = snprintf(buffer + offset, buffer_len - offset, ":%" PRIu64,
101 vl->values[i].absolute);
104 if ((status < 1) || (status >= (buffer_len - offset)))
108 } /* for ds->ds_num */
111 } /* int value_list_to_string */
113 static int value_list_to_filename(char *buffer, size_t buffer_size,
114 value_list_t const *vl) {
115 char const suffix[] = ".rrd";
119 if (datadir != NULL) {
120 size_t datadir_len = strlen(datadir) + 1;
122 if (datadir_len >= buffer_size)
125 sstrncpy(buffer, datadir, buffer_size);
126 buffer[datadir_len - 1] = '/';
127 buffer[datadir_len] = 0;
129 buffer += datadir_len;
130 buffer_size -= datadir_len;
133 status = FORMAT_VL(buffer, buffer_size, vl);
137 len = strlen(buffer);
138 assert(len < buffer_size);
142 if (buffer_size <= sizeof(suffix))
145 memcpy(buffer, suffix, sizeof(suffix));
147 } /* int value_list_to_filename */
149 static int rc_config_get_int_positive(oconfig_item_t const *ci, int *ret) {
153 status = cf_util_get_int(ci, &tmp);
161 } /* int rc_config_get_int_positive */
163 static int rc_config_get_xff(oconfig_item_t const *ci, double *ret) {
166 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
167 ERROR("rrdcached plugin: The \"%s\" needs exactly one numeric argument "
168 "in the range [0.0, 1.0)",
173 value = ci->values[0].value.number;
174 if ((value >= 0.0) && (value < 1.0)) {
179 ERROR("rrdcached plugin: The \"%s\" needs exactly one numeric argument "
180 "in the range [0.0, 1.0)",
183 } /* int rc_config_get_xff */
185 static int rc_config_add_timespan(int timespan) {
191 tmp = realloc(rrdcreate_config.timespans,
192 sizeof(*rrdcreate_config.timespans) *
193 (rrdcreate_config.timespans_num + 1));
196 rrdcreate_config.timespans = tmp;
198 rrdcreate_config.timespans[rrdcreate_config.timespans_num] = timespan;
199 rrdcreate_config.timespans_num++;
202 } /* int rc_config_add_timespan */
204 static int rc_config(oconfig_item_t *ci) {
205 for (int i = 0; i < ci->children_num; i++) {
206 oconfig_item_t const *child = ci->children + i;
207 const char *key = child->key;
210 if (strcasecmp("DataDir", key) == 0) {
211 status = cf_util_get_string(child, &datadir);
213 int len = strlen(datadir);
215 while ((len > 0) && (datadir[len - 1] == '/')) {
223 } else if (strcasecmp("DaemonAddress", key) == 0)
224 status = cf_util_get_string(child, &daemon_address);
225 else if (strcasecmp("CreateFiles", key) == 0)
226 status = cf_util_get_boolean(child, &config_create_files);
227 else if (strcasecmp("CreateFilesAsync", key) == 0)
228 status = cf_util_get_boolean(child, &rrdcreate_config.async);
229 else if (strcasecmp("CollectStatistics", key) == 0)
230 status = cf_util_get_boolean(child, &config_collect_stats);
231 else if (strcasecmp("StepSize", key) == 0) {
234 status = rc_config_get_int_positive(child, &tmp);
236 rrdcreate_config.stepsize = (unsigned long)tmp;
237 } else if (strcasecmp("HeartBeat", key) == 0)
238 status = rc_config_get_int_positive(child, &rrdcreate_config.heartbeat);
239 else if (strcasecmp("RRARows", key) == 0)
240 status = rc_config_get_int_positive(child, &rrdcreate_config.rrarows);
241 else if (strcasecmp("RRATimespan", key) == 0) {
243 status = rc_config_get_int_positive(child, &tmp);
245 status = rc_config_add_timespan(tmp);
246 } else if (strcasecmp("XFF", key) == 0)
247 status = rc_config_get_xff(child, &rrdcreate_config.xff);
249 WARNING("rrdcached plugin: Ignoring invalid option %s.", key);
254 WARNING("rrdcached plugin: Handling the \"%s\" option failed.", key);
257 if (daemon_address != NULL) {
258 plugin_register_write("rrdcached", rc_write, /* user_data = */ NULL);
259 plugin_register_flush("rrdcached", rc_flush, /* user_data = */ NULL);
262 } /* int rc_config */
264 static int try_reconnect(void) {
270 status = rrdc_connect(daemon_address);
272 ERROR("rrdcached plugin: Failed to reconnect to RRDCacheD "
273 "at %s: %s (status=%d)",
274 daemon_address, rrd_get_error(), status);
278 INFO("rrdcached plugin: Successfully reconnected to RRDCacheD "
282 } /* int try_reconnect */
284 static int rc_read(void) {
289 value_list_t vl = VALUE_LIST_INIT;
290 vl.values = &(value_t){.gauge = NAN};
293 if (daemon_address == NULL)
296 if (!config_collect_stats)
299 if ((strncmp("unix:", daemon_address, strlen("unix:")) != 0) &&
300 (daemon_address[0] != '/'))
301 sstrncpy(vl.host, daemon_address, sizeof(vl.host));
302 sstrncpy(vl.plugin, "rrdcached", sizeof(vl.plugin));
305 status = rrdc_connect(daemon_address);
307 ERROR("rrdcached plugin: Failed to connect to RRDCacheD "
308 "at %s: %s (status=%d)",
309 daemon_address, rrd_get_error(), status);
314 /* The RRD client lib does not provide any means for checking a
315 * connection, hence we'll have to retry upon failed operations. */
318 status = rrdc_stats_get(&head);
324 if (try_reconnect() == 0)
326 /* else: report the error and fail */
329 ERROR("rrdcached plugin: rrdc_stats_get failed: %s (status=%i).",
330 rrd_get_error(), status);
334 for (rrdc_stats_t *ptr = head; ptr != NULL; ptr = ptr->next) {
335 if (ptr->type == RRDC_STATS_TYPE_GAUGE)
336 vl.values[0].gauge = (gauge_t)ptr->value.gauge;
337 else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
338 vl.values[0].counter = (counter_t)ptr->value.counter;
342 if (strcasecmp("QueueLength", ptr->name) == 0) {
343 sstrncpy(vl.type, "queue_length", sizeof(vl.type));
344 sstrncpy(vl.type_instance, "", sizeof(vl.type_instance));
345 } else if (strcasecmp("UpdatesWritten", ptr->name) == 0) {
346 sstrncpy(vl.type, "operations", sizeof(vl.type));
347 sstrncpy(vl.type_instance, "write-updates", sizeof(vl.type_instance));
348 } else if (strcasecmp("DataSetsWritten", ptr->name) == 0) {
349 sstrncpy(vl.type, "operations", sizeof(vl.type));
350 sstrncpy(vl.type_instance, "write-data_sets", sizeof(vl.type_instance));
351 } else if (strcasecmp("TreeNodesNumber", ptr->name) == 0) {
352 sstrncpy(vl.type, "gauge", sizeof(vl.type));
353 sstrncpy(vl.type_instance, "tree_nodes", sizeof(vl.type_instance));
354 } else if (strcasecmp("TreeDepth", ptr->name) == 0) {
355 sstrncpy(vl.type, "gauge", sizeof(vl.type));
356 sstrncpy(vl.type_instance, "tree_depth", sizeof(vl.type_instance));
357 } else if (strcasecmp("FlushesReceived", ptr->name) == 0) {
358 sstrncpy(vl.type, "operations", sizeof(vl.type));
359 sstrncpy(vl.type_instance, "receive-flush", sizeof(vl.type_instance));
360 } else if (strcasecmp("JournalBytes", ptr->name) == 0) {
361 sstrncpy(vl.type, "counter", sizeof(vl.type));
362 sstrncpy(vl.type_instance, "journal-bytes", sizeof(vl.type_instance));
363 } else if (strcasecmp("JournalRotate", ptr->name) == 0) {
364 sstrncpy(vl.type, "counter", sizeof(vl.type));
365 sstrncpy(vl.type_instance, "journal-rotates", sizeof(vl.type_instance));
366 } else if (strcasecmp("UpdatesReceived", ptr->name) == 0) {
367 sstrncpy(vl.type, "operations", sizeof(vl.type));
368 sstrncpy(vl.type_instance, "receive-update", sizeof(vl.type_instance));
370 DEBUG("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
374 plugin_dispatch_values(&vl);
375 } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
377 rrdc_stats_free(head);
382 static int rc_init(void) {
383 if (config_collect_stats)
384 plugin_register_read("rrdcached", rc_read);
389 static int rc_write(const data_set_t *ds, const value_list_t *vl,
390 user_data_t __attribute__((unused)) * user_data) {
391 char filename[PATH_MAX];
393 char *values_array[2];
397 if (daemon_address == NULL) {
398 ERROR("rrdcached plugin: daemon_address == NULL.");
399 plugin_unregister_write("rrdcached");
403 if (strcmp(ds->type, vl->type) != 0) {
404 ERROR("rrdcached plugin: DS type does not match value list type");
408 if (value_list_to_filename(filename, sizeof(filename), vl) != 0) {
409 ERROR("rrdcached plugin: value_list_to_filename failed.");
413 if (value_list_to_string(values, sizeof(values), ds, vl) != 0) {
414 ERROR("rrdcached plugin: value_list_to_string failed.");
418 values_array[0] = values;
419 values_array[1] = NULL;
421 if (config_create_files) {
424 status = stat(filename, &statbuf);
426 if (errno != ENOENT) {
427 ERROR("rrdcached plugin: stat (%s) failed: %s", filename, STRERRNO);
431 status = cu_rrd_create_file(filename, ds, vl, &rrdcreate_config);
433 ERROR("rrdcached plugin: cu_rrd_create_file (%s) failed.", filename);
435 } else if (rrdcreate_config.async)
441 status = rrdc_connect(daemon_address);
443 ERROR("rrdcached plugin: Failed to connect to RRDCacheD "
444 "at %s: %s (status=%d)",
445 daemon_address, rrd_get_error(), status);
450 /* The RRD client lib does not provide any means for checking a
451 * connection, hence we'll have to retry upon failed operations. */
453 status = rrdc_update(filename, /* values_num = */ 1, (void *)values_array);
459 if (try_reconnect() == 0)
461 /* else: report the error and fail */
464 ERROR("rrdcached plugin: rrdc_update (%s, [%s], 1) failed: %s (status=%i)",
465 filename, values_array[0], rrd_get_error(), status);
472 static int rc_flush(__attribute__((unused)) cdtime_t timeout, /* {{{ */
473 const char *identifier,
474 __attribute__((unused)) user_data_t *ud) {
475 char filename[PATH_MAX + 1];
479 if (identifier == NULL)
483 snprintf(filename, sizeof(filename), "%s/%s.rrd", datadir, identifier);
485 snprintf(filename, sizeof(filename), "%s.rrd", identifier);
488 status = rrdc_connect(daemon_address);
490 ERROR("rrdcached plugin: Failed to connect to RRDCacheD "
491 "at %s: %s (status=%d)",
492 daemon_address, rrd_get_error(), status);
497 /* The RRD client lib does not provide any means for checking a
498 * connection, hence we'll have to retry upon failed operations. */
500 status = rrdc_flush(filename);
506 if (try_reconnect() == 0)
508 /* else: report the error and fail */
511 ERROR("rrdcached plugin: rrdc_flush (%s) failed: %s (status=%i).", filename,
512 rrd_get_error(), status);
515 DEBUG("rrdcached plugin: rrdc_flush (%s): Success.", filename);
518 } /* }}} int rc_flush */
520 static int rc_shutdown(void) {
523 } /* int rc_shutdown */
525 void module_register(void) {
526 plugin_register_complex_config("rrdcached", rc_config);
527 plugin_register_init("rrdcached", rc_init);
528 plugin_register_shutdown("rrdcached", rc_shutdown);
529 } /* void module_register */