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)
76 assert (0 == strcmp (ds->type, vl->type));
78 memset (buffer, '\0', buffer_len);
80 t = CDTIME_T_TO_TIME_T (vl->time);
81 status = ssnprintf (buffer, buffer_len, "%lu", (unsigned long) t);
82 if ((status < 1) || (status >= buffer_len))
86 for (i = 0; i < ds->ds_num; i++)
88 if ((ds->ds[i].type != DS_TYPE_COUNTER)
89 && (ds->ds[i].type != DS_TYPE_GAUGE)
90 && (ds->ds[i].type != DS_TYPE_DERIVE)
91 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
94 if (ds->ds[i].type == DS_TYPE_COUNTER)
96 status = ssnprintf (buffer + offset, buffer_len - offset,
97 ":%llu", vl->values[i].counter);
99 else if (ds->ds[i].type == DS_TYPE_GAUGE)
101 status = ssnprintf (buffer + offset, buffer_len - offset,
102 ":%f", vl->values[i].gauge);
104 else if (ds->ds[i].type == DS_TYPE_DERIVE) {
105 status = ssnprintf (buffer + offset, buffer_len - offset,
106 ":%"PRIi64, vl->values[i].derive);
108 else /* if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */ {
109 status = ssnprintf (buffer + offset, buffer_len - offset,
110 ":%"PRIu64, vl->values[i].absolute);
114 if ((status < 1) || (status >= (buffer_len - offset)))
118 } /* for ds->ds_num */
121 } /* int value_list_to_string */
123 static int value_list_to_filename (char *buffer, size_t buffer_size,
124 value_list_t const *vl)
126 char const suffix[] = ".rrd";
132 size_t datadir_len = strlen (datadir) + 1;
134 if (datadir_len >= buffer_size)
137 sstrncpy (buffer, datadir, buffer_size);
138 buffer[datadir_len - 1] = '/';
139 buffer[datadir_len] = 0;
141 buffer += datadir_len;
142 buffer_size -= datadir_len;
145 status = FORMAT_VL (buffer, buffer_size, vl);
149 len = strlen (buffer);
150 assert (len < buffer_size);
154 if (buffer_size <= sizeof (suffix))
157 memcpy (buffer, suffix, sizeof (suffix));
159 } /* int value_list_to_filename */
161 static int rc_config_get_int_positive (oconfig_item_t const *ci, int *ret)
166 status = cf_util_get_int (ci, &tmp);
174 } /* int rc_config_get_int_positive */
176 static int rc_config_get_xff (oconfig_item_t const *ci, double *ret)
180 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
182 ERROR ("rrdcached plugin: The \"%s\" needs exactly one numeric argument "
183 "in the range [0.0, 1.0)", ci->key);
187 value = ci->values[0].value.number;
188 if ((value >= 0.0) && (value < 1.0))
194 ERROR ("rrdcached plugin: The \"%s\" needs exactly one numeric argument "
195 "in the range [0.0, 1.0)", ci->key);
197 } /* int rc_config_get_xff */
199 static int rc_config_add_timespan (int timespan)
206 tmp = realloc (rrdcreate_config.timespans,
207 sizeof (*rrdcreate_config.timespans)
208 * (rrdcreate_config.timespans_num + 1));
211 rrdcreate_config.timespans = tmp;
213 rrdcreate_config.timespans[rrdcreate_config.timespans_num] = timespan;
214 rrdcreate_config.timespans_num++;
217 } /* int rc_config_add_timespan */
219 static int rc_config (oconfig_item_t *ci)
223 for (i = 0; i < ci->children_num; i++)
225 oconfig_item_t const *child = ci->children + i;
226 const char *key = child->key;
229 if (strcasecmp ("DataDir", key) == 0)
231 status = cf_util_get_string (child, &datadir);
234 int len = strlen (datadir);
236 while ((len > 0) && (datadir[len - 1] == '/'))
246 else if (strcasecmp ("DaemonAddress", key) == 0)
247 status = cf_util_get_string (child, &daemon_address);
248 else if (strcasecmp ("CreateFiles", key) == 0)
249 status = cf_util_get_boolean (child, &config_create_files);
250 else if (strcasecmp ("CreateFilesAsync", key) == 0)
251 status = cf_util_get_boolean (child, &rrdcreate_config.async);
252 else if (strcasecmp ("CollectStatistics", key) == 0)
253 status = cf_util_get_boolean (child, &config_collect_stats);
254 else if (strcasecmp ("StepSize", key) == 0)
258 status = rc_config_get_int_positive (child, &tmp);
260 rrdcreate_config.stepsize = (unsigned long) tmp;
262 else if (strcasecmp ("HeartBeat", key) == 0)
263 status = rc_config_get_int_positive (child, &rrdcreate_config.heartbeat);
264 else if (strcasecmp ("RRARows", key) == 0)
265 status = rc_config_get_int_positive (child, &rrdcreate_config.rrarows);
266 else if (strcasecmp ("RRATimespan", key) == 0)
269 status = rc_config_get_int_positive (child, &tmp);
271 status = rc_config_add_timespan (tmp);
273 else if (strcasecmp ("XFF", key) == 0)
274 status = rc_config_get_xff (child, &rrdcreate_config.xff);
277 WARNING ("rrdcached plugin: Ignoring invalid option %s.", key);
282 WARNING ("rrdcached plugin: Handling the \"%s\" option failed.", key);
285 if (daemon_address != NULL)
287 plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
288 plugin_register_flush ("rrdcached", rc_flush, /* user_data = */ NULL);
291 } /* int rc_config */
293 static int rc_read (void)
300 value_list_t vl = VALUE_LIST_INIT;
302 if (daemon_address == NULL)
305 if (!config_collect_stats)
311 if ((strncmp ("unix:", daemon_address, strlen ("unix:")) == 0)
312 || (daemon_address[0] == '/'))
313 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
315 sstrncpy (vl.host, daemon_address, sizeof (vl.host));
316 sstrncpy (vl.plugin, "rrdcached", sizeof (vl.plugin));
318 status = rrdc_connect (daemon_address);
321 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
322 daemon_address, status);
327 status = rrdc_stats_get (&head);
330 ERROR ("rrdcached plugin: rrdc_stats_get failed with status %i.", status);
334 for (ptr = head; ptr != NULL; ptr = ptr->next)
336 if (ptr->type == RRDC_STATS_TYPE_GAUGE)
337 values[0].gauge = (gauge_t) ptr->value.gauge;
338 else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
339 values[0].counter = (counter_t) ptr->value.counter;
343 if (strcasecmp ("QueueLength", ptr->name) == 0)
345 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
346 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
348 else if (strcasecmp ("UpdatesWritten", ptr->name) == 0)
350 sstrncpy (vl.type, "operations", sizeof (vl.type));
351 sstrncpy (vl.type_instance, "write-updates", sizeof (vl.type_instance));
353 else if (strcasecmp ("DataSetsWritten", ptr->name) == 0)
355 sstrncpy (vl.type, "operations", sizeof (vl.type));
356 sstrncpy (vl.type_instance, "write-data_sets",
357 sizeof (vl.type_instance));
359 else if (strcasecmp ("TreeNodesNumber", ptr->name) == 0)
361 sstrncpy (vl.type, "gauge", sizeof (vl.type));
362 sstrncpy (vl.type_instance, "tree_nodes", sizeof (vl.type_instance));
364 else if (strcasecmp ("TreeDepth", ptr->name) == 0)
366 sstrncpy (vl.type, "gauge", sizeof (vl.type));
367 sstrncpy (vl.type_instance, "tree_depth", sizeof (vl.type_instance));
369 else if (strcasecmp ("FlushesReceived", ptr->name) == 0)
371 sstrncpy (vl.type, "operations", sizeof (vl.type));
372 sstrncpy (vl.type_instance, "receive-flush", sizeof (vl.type_instance));
374 else if (strcasecmp ("JournalBytes", ptr->name) == 0)
376 sstrncpy (vl.type, "counter", sizeof (vl.type));
377 sstrncpy (vl.type_instance, "journal-bytes", sizeof (vl.type_instance));
379 else if (strcasecmp ("JournalRotate", ptr->name) == 0)
381 sstrncpy (vl.type, "counter", sizeof (vl.type));
382 sstrncpy (vl.type_instance, "journal-rotates", sizeof (vl.type_instance));
384 else if (strcasecmp ("UpdatesReceived", ptr->name) == 0)
386 sstrncpy (vl.type, "operations", sizeof (vl.type));
387 sstrncpy (vl.type_instance, "receive-update", sizeof (vl.type_instance));
391 DEBUG ("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
395 plugin_dispatch_values (&vl);
396 } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
398 rrdc_stats_free (head);
403 static int rc_init (void)
405 if (config_collect_stats)
406 plugin_register_read ("rrdcached", rc_read);
411 static int rc_write (const data_set_t *ds, const value_list_t *vl,
412 user_data_t __attribute__((unused)) *user_data)
414 char filename[PATH_MAX];
416 char *values_array[2];
419 if (daemon_address == NULL)
421 ERROR ("rrdcached plugin: daemon_address == NULL.");
422 plugin_unregister_write ("rrdcached");
426 if (strcmp (ds->type, vl->type) != 0)
428 ERROR ("rrdcached plugin: DS type does not match value list type");
432 if (value_list_to_filename (filename, sizeof (filename), vl) != 0)
434 ERROR ("rrdcached plugin: value_list_to_filename failed.");
438 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
440 ERROR ("rrdcached plugin: value_list_to_string failed.");
444 values_array[0] = values;
445 values_array[1] = NULL;
447 if (config_create_files)
451 status = stat (filename, &statbuf);
457 ERROR ("rrdcached plugin: stat (%s) failed: %s",
458 filename, sstrerror (errno, errbuf, sizeof (errbuf)));
462 status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
465 ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
469 else if (rrdcreate_config.async)
474 status = rrdc_connect (daemon_address);
477 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
478 daemon_address, status);
482 status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
485 ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
487 filename, values_array[0], status);
494 static int rc_flush (__attribute__((unused)) cdtime_t timeout, /* {{{ */
495 const char *identifier,
496 __attribute__((unused)) user_data_t *ud)
498 char filename[PATH_MAX + 1];
501 if (identifier == NULL)
505 ssnprintf (filename, sizeof (filename), "%s/%s.rrd", datadir, identifier);
507 ssnprintf (filename, sizeof (filename), "%s.rrd", identifier);
509 status = rrdc_connect (daemon_address);
512 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
513 daemon_address, status);
517 status = rrdc_flush (filename);
520 ERROR ("rrdcached plugin: rrdc_flush (%s) failed with status %i.",
524 DEBUG ("rrdcached plugin: rrdc_flush (%s): Success.", filename);
527 } /* }}} int rc_flush */
529 static int rc_shutdown (void)
533 } /* int rc_shutdown */
535 void module_register (void)
537 plugin_register_complex_config ("rrdcached", rc_config);
538 plugin_register_init ("rrdcached", rc_init);
539 plugin_register_shutdown ("rrdcached", rc_shutdown);
540 } /* void module_register */
543 * vim: set sw=2 sts=2 et :