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 =
51 /* timespans = */ NULL,
52 /* timespans_num = */ 0,
54 /* consolidation_functions = */ NULL,
55 /* consolidation_functions_num = */ 0,
63 static int rc_write (const data_set_t *ds, const value_list_t *vl,
64 user_data_t __attribute__((unused)) *user_data);
65 static int rc_flush (__attribute__((unused)) cdtime_t timeout,
66 const char *identifier, __attribute__((unused)) user_data_t *ud);
68 static int value_list_to_string (char *buffer, int buffer_len,
69 const data_set_t *ds, const value_list_t *vl)
75 assert (0 == strcmp (ds->type, vl->type));
77 memset (buffer, '\0', buffer_len);
79 t = CDTIME_T_TO_TIME_T (vl->time);
80 status = ssnprintf (buffer, buffer_len, "%lu", (unsigned long) t);
81 if ((status < 1) || (status >= buffer_len))
85 for (size_t i = 0; i < ds->ds_num; i++)
87 if ((ds->ds[i].type != DS_TYPE_COUNTER)
88 && (ds->ds[i].type != DS_TYPE_GAUGE)
89 && (ds->ds[i].type != DS_TYPE_DERIVE)
90 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
93 if (ds->ds[i].type == DS_TYPE_COUNTER)
95 status = ssnprintf (buffer + offset, buffer_len - offset,
96 ":%llu", vl->values[i].counter);
98 else if (ds->ds[i].type == DS_TYPE_GAUGE)
100 status = ssnprintf (buffer + offset, buffer_len - offset,
101 ":%f", vl->values[i].gauge);
103 else if (ds->ds[i].type == DS_TYPE_DERIVE) {
104 status = ssnprintf (buffer + offset, buffer_len - offset,
105 ":%"PRIi64, vl->values[i].derive);
107 else /* if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */ {
108 status = ssnprintf (buffer + offset, buffer_len - offset,
109 ":%"PRIu64, vl->values[i].absolute);
113 if ((status < 1) || (status >= (buffer_len - offset)))
117 } /* for ds->ds_num */
120 } /* int value_list_to_string */
122 static int value_list_to_filename (char *buffer, size_t buffer_size,
123 value_list_t const *vl)
125 char const suffix[] = ".rrd";
131 size_t datadir_len = strlen (datadir) + 1;
133 if (datadir_len >= buffer_size)
136 sstrncpy (buffer, datadir, buffer_size);
137 buffer[datadir_len - 1] = '/';
138 buffer[datadir_len] = 0;
140 buffer += datadir_len;
141 buffer_size -= datadir_len;
144 status = FORMAT_VL (buffer, buffer_size, vl);
148 len = strlen (buffer);
149 assert (len < buffer_size);
153 if (buffer_size <= sizeof (suffix))
156 memcpy (buffer, suffix, sizeof (suffix));
158 } /* int value_list_to_filename */
160 static int rc_config_get_int_positive (oconfig_item_t const *ci, int *ret)
165 status = cf_util_get_int (ci, &tmp);
173 } /* int rc_config_get_int_positive */
175 static int rc_config_get_xff (oconfig_item_t const *ci, double *ret)
179 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
181 ERROR ("rrdcached plugin: The \"%s\" needs exactly one numeric argument "
182 "in the range [0.0, 1.0)", ci->key);
186 value = ci->values[0].value.number;
187 if ((value >= 0.0) && (value < 1.0))
193 ERROR ("rrdcached plugin: The \"%s\" needs exactly one numeric argument "
194 "in the range [0.0, 1.0)", ci->key);
196 } /* int rc_config_get_xff */
198 static int rc_config_add_timespan (int timespan)
205 tmp = realloc (rrdcreate_config.timespans,
206 sizeof (*rrdcreate_config.timespans)
207 * (rrdcreate_config.timespans_num + 1));
210 rrdcreate_config.timespans = tmp;
212 rrdcreate_config.timespans[rrdcreate_config.timespans_num] = timespan;
213 rrdcreate_config.timespans_num++;
216 } /* int rc_config_add_timespan */
218 static int rc_config (oconfig_item_t *ci)
220 for (int i = 0; i < ci->children_num; i++)
222 oconfig_item_t const *child = ci->children + i;
223 const char *key = child->key;
226 if (strcasecmp ("DataDir", key) == 0)
228 status = cf_util_get_string (child, &datadir);
231 int len = strlen (datadir);
233 while ((len > 0) && (datadir[len - 1] == '/'))
243 else if (strcasecmp ("DaemonAddress", key) == 0)
244 status = cf_util_get_string (child, &daemon_address);
245 else if (strcasecmp ("CreateFiles", key) == 0)
246 status = cf_util_get_boolean (child, &config_create_files);
247 else if (strcasecmp ("CreateFilesAsync", key) == 0)
248 status = cf_util_get_boolean (child, &rrdcreate_config.async);
249 else if (strcasecmp ("CollectStatistics", key) == 0)
250 status = cf_util_get_boolean (child, &config_collect_stats);
251 else if (strcasecmp ("StepSize", key) == 0)
255 status = rc_config_get_int_positive (child, &tmp);
257 rrdcreate_config.stepsize = (unsigned long) tmp;
259 else if (strcasecmp ("HeartBeat", key) == 0)
260 status = rc_config_get_int_positive (child, &rrdcreate_config.heartbeat);
261 else if (strcasecmp ("RRARows", key) == 0)
262 status = rc_config_get_int_positive (child, &rrdcreate_config.rrarows);
263 else if (strcasecmp ("RRATimespan", key) == 0)
266 status = rc_config_get_int_positive (child, &tmp);
268 status = rc_config_add_timespan (tmp);
270 else if (strcasecmp ("XFF", key) == 0)
271 status = rc_config_get_xff (child, &rrdcreate_config.xff);
274 WARNING ("rrdcached plugin: Ignoring invalid option %s.", key);
279 WARNING ("rrdcached plugin: Handling the \"%s\" option failed.", key);
282 if (daemon_address != NULL)
284 plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
285 plugin_register_flush ("rrdcached", rc_flush, /* user_data = */ NULL);
288 } /* int rc_config */
290 static int try_reconnect (void)
297 status = rrdc_connect (daemon_address);
300 ERROR ("rrdcached plugin: Failed to reconnect to RRDCacheD "
301 "at %s: %s (status=%d)", daemon_address, rrd_get_error (), status);
305 INFO ("rrdcached plugin: Successfully reconnected to RRDCacheD "
306 "at %s", daemon_address);
308 } /* int try_reconnect */
310 static int rc_read (void)
317 value_list_t vl = VALUE_LIST_INIT;
319 if (daemon_address == NULL)
322 if (!config_collect_stats)
328 if ((strncmp ("unix:", daemon_address, strlen ("unix:")) == 0)
329 || (daemon_address[0] == '/'))
330 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
332 sstrncpy (vl.host, daemon_address, sizeof (vl.host));
333 sstrncpy (vl.plugin, "rrdcached", sizeof (vl.plugin));
336 status = rrdc_connect (daemon_address);
339 ERROR ("rrdcached plugin: Failed to connect to RRDCacheD "
340 "at %s: %s (status=%d)", daemon_address, rrd_get_error (), status);
346 /* The RRD client lib does not provide any means for checking a
347 * connection, hence we'll have to retry upon failed operations. */
350 status = rrdc_stats_get (&head);
357 if (try_reconnect () == 0)
359 /* else: report the error and fail */
362 ERROR ("rrdcached plugin: rrdc_stats_get failed: %s (status=%i).",
363 rrd_get_error (), status);
367 for (rrdc_stats_t *ptr = head; ptr != NULL; ptr = ptr->next)
369 if (ptr->type == RRDC_STATS_TYPE_GAUGE)
370 values[0].gauge = (gauge_t) ptr->value.gauge;
371 else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
372 values[0].counter = (counter_t) ptr->value.counter;
376 if (strcasecmp ("QueueLength", ptr->name) == 0)
378 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
379 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
381 else if (strcasecmp ("UpdatesWritten", ptr->name) == 0)
383 sstrncpy (vl.type, "operations", sizeof (vl.type));
384 sstrncpy (vl.type_instance, "write-updates", sizeof (vl.type_instance));
386 else if (strcasecmp ("DataSetsWritten", ptr->name) == 0)
388 sstrncpy (vl.type, "operations", sizeof (vl.type));
389 sstrncpy (vl.type_instance, "write-data_sets",
390 sizeof (vl.type_instance));
392 else if (strcasecmp ("TreeNodesNumber", ptr->name) == 0)
394 sstrncpy (vl.type, "gauge", sizeof (vl.type));
395 sstrncpy (vl.type_instance, "tree_nodes", sizeof (vl.type_instance));
397 else if (strcasecmp ("TreeDepth", ptr->name) == 0)
399 sstrncpy (vl.type, "gauge", sizeof (vl.type));
400 sstrncpy (vl.type_instance, "tree_depth", sizeof (vl.type_instance));
402 else if (strcasecmp ("FlushesReceived", ptr->name) == 0)
404 sstrncpy (vl.type, "operations", sizeof (vl.type));
405 sstrncpy (vl.type_instance, "receive-flush", sizeof (vl.type_instance));
407 else if (strcasecmp ("JournalBytes", ptr->name) == 0)
409 sstrncpy (vl.type, "counter", sizeof (vl.type));
410 sstrncpy (vl.type_instance, "journal-bytes", sizeof (vl.type_instance));
412 else if (strcasecmp ("JournalRotate", ptr->name) == 0)
414 sstrncpy (vl.type, "counter", sizeof (vl.type));
415 sstrncpy (vl.type_instance, "journal-rotates", sizeof (vl.type_instance));
417 else if (strcasecmp ("UpdatesReceived", ptr->name) == 0)
419 sstrncpy (vl.type, "operations", sizeof (vl.type));
420 sstrncpy (vl.type_instance, "receive-update", sizeof (vl.type_instance));
424 DEBUG ("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
428 plugin_dispatch_values (&vl);
429 } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
431 rrdc_stats_free (head);
436 static int rc_init (void)
438 if (config_collect_stats)
439 plugin_register_read ("rrdcached", rc_read);
444 static int rc_write (const data_set_t *ds, const value_list_t *vl,
445 user_data_t __attribute__((unused)) *user_data)
447 char filename[PATH_MAX];
449 char *values_array[2];
453 if (daemon_address == NULL)
455 ERROR ("rrdcached plugin: daemon_address == NULL.");
456 plugin_unregister_write ("rrdcached");
460 if (strcmp (ds->type, vl->type) != 0)
462 ERROR ("rrdcached plugin: DS type does not match value list type");
466 if (value_list_to_filename (filename, sizeof (filename), vl) != 0)
468 ERROR ("rrdcached plugin: value_list_to_filename failed.");
472 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
474 ERROR ("rrdcached plugin: value_list_to_string failed.");
478 values_array[0] = values;
479 values_array[1] = NULL;
481 if (config_create_files)
485 status = stat (filename, &statbuf);
491 ERROR ("rrdcached plugin: stat (%s) failed: %s",
492 filename, sstrerror (errno, errbuf, sizeof (errbuf)));
496 status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
499 ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
503 else if (rrdcreate_config.async)
509 status = rrdc_connect (daemon_address);
512 ERROR ("rrdcached plugin: Failed to connect to RRDCacheD "
513 "at %s: %s (status=%d)", daemon_address, rrd_get_error (), status);
519 /* The RRD client lib does not provide any means for checking a
520 * connection, hence we'll have to retry upon failed operations. */
522 status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
529 if (try_reconnect () == 0)
531 /* else: report the error and fail */
534 ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed: %s (status=%i)",
535 filename, values_array[0], rrd_get_error (), status);
542 static int rc_flush (__attribute__((unused)) cdtime_t timeout, /* {{{ */
543 const char *identifier,
544 __attribute__((unused)) user_data_t *ud)
546 char filename[PATH_MAX + 1];
550 if (identifier == NULL)
554 ssnprintf (filename, sizeof (filename), "%s/%s.rrd", datadir, identifier);
556 ssnprintf (filename, sizeof (filename), "%s.rrd", identifier);
559 status = rrdc_connect (daemon_address);
562 ERROR ("rrdcached plugin: Failed to connect to RRDCacheD "
563 "at %s: %s (status=%d)", daemon_address, rrd_get_error (), status);
569 /* The RRD client lib does not provide any means for checking a
570 * connection, hence we'll have to retry upon failed operations. */
572 status = rrdc_flush (filename);
579 if (try_reconnect () == 0)
581 /* else: report the error and fail */
584 ERROR ("rrdcached plugin: rrdc_flush (%s) failed: %s (status=%i).",
585 filename, rrd_get_error (), status);
588 DEBUG ("rrdcached plugin: rrdc_flush (%s): Success.", filename);
591 } /* }}} int rc_flush */
593 static int rc_shutdown (void)
597 } /* int rc_shutdown */
599 void module_register (void)
601 plugin_register_complex_config ("rrdcached", rc_config);
602 plugin_register_init ("rrdcached", rc_init);
603 plugin_register_shutdown ("rrdcached", rc_shutdown);
604 } /* void module_register */
607 * vim: set sw=2 sts=2 et :