2 * collectd - src/rrdcached.c
3 * Copyright (C) 2008-2012 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at collectd.org>
25 #include "utils_rrdcreate.h"
29 #include <rrd_client.h>
34 static char *datadir = NULL;
35 static char *daemon_address = NULL;
36 static _Bool config_create_files = 1;
37 static _Bool config_collect_stats = 1;
38 static rrdcreate_config_t rrdcreate_config =
45 /* timespans = */ NULL,
46 /* timespans_num = */ 0,
48 /* consolidation_functions = */ NULL,
49 /* consolidation_functions_num = */ 0,
57 static int rc_write (const data_set_t *ds, const value_list_t *vl,
58 user_data_t __attribute__((unused)) *user_data);
59 static int rc_flush (__attribute__((unused)) cdtime_t timeout,
60 const char *identifier, __attribute__((unused)) user_data_t *ud);
62 static int value_list_to_string (char *buffer, int buffer_len,
63 const data_set_t *ds, const value_list_t *vl)
70 assert (0 == strcmp (ds->type, vl->type));
72 memset (buffer, '\0', buffer_len);
74 t = CDTIME_T_TO_TIME_T (vl->time);
75 status = ssnprintf (buffer, buffer_len, "%lu", (unsigned long) t);
76 if ((status < 1) || (status >= buffer_len))
80 for (i = 0; i < ds->ds_num; i++)
82 if ((ds->ds[i].type != DS_TYPE_COUNTER)
83 && (ds->ds[i].type != DS_TYPE_GAUGE)
84 && (ds->ds[i].type != DS_TYPE_DERIVE)
85 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
88 if (ds->ds[i].type == DS_TYPE_COUNTER)
90 status = ssnprintf (buffer + offset, buffer_len - offset,
91 ":%llu", vl->values[i].counter);
93 else if (ds->ds[i].type == DS_TYPE_GAUGE)
95 status = ssnprintf (buffer + offset, buffer_len - offset,
96 ":%f", vl->values[i].gauge);
98 else if (ds->ds[i].type == DS_TYPE_DERIVE) {
99 status = ssnprintf (buffer + offset, buffer_len - offset,
100 ":%"PRIi64, vl->values[i].derive);
102 else /* if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */ {
103 status = ssnprintf (buffer + offset, buffer_len - offset,
104 ":%"PRIu64, vl->values[i].absolute);
108 if ((status < 1) || (status >= (buffer_len - offset)))
112 } /* for ds->ds_num */
115 } /* int value_list_to_string */
117 static int value_list_to_filename (char *buffer, int buffer_len,
118 const data_set_t *ds, const value_list_t *vl)
123 assert (0 == strcmp (ds->type, vl->type));
127 status = ssnprintf (buffer + offset, buffer_len - offset,
129 if ((status < 1) || (status >= buffer_len - offset))
134 status = ssnprintf (buffer + offset, buffer_len - offset,
136 if ((status < 1) || (status >= buffer_len - offset))
140 if (strlen (vl->plugin_instance) > 0)
141 status = ssnprintf (buffer + offset, buffer_len - offset,
142 "%s-%s/", vl->plugin, vl->plugin_instance);
144 status = ssnprintf (buffer + offset, buffer_len - offset,
146 if ((status < 1) || (status >= buffer_len - offset))
150 if (strlen (vl->type_instance) > 0)
151 status = ssnprintf (buffer + offset, buffer_len - offset,
152 "%s-%s", vl->type, vl->type_instance);
154 status = ssnprintf (buffer + offset, buffer_len - offset,
156 if ((status < 1) || (status >= buffer_len - offset))
160 strncpy (buffer + offset, ".rrd", buffer_len - offset);
161 buffer[buffer_len - 1] = 0;
164 } /* int value_list_to_filename */
166 static int rc_config_get_int_positive (oconfig_item_t const *ci, int *ret)
171 status = cf_util_get_int (ci, &tmp);
179 } /* int rc_config_get_int_positive */
181 static int rc_config_get_xff (oconfig_item_t const *ci, double *ret)
185 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
187 ERROR ("rrdcached plugin: The \"%s\" needs exactly one numeric argument "
188 "in the range [0.0, 1.0)", ci->key);
192 value = ci->values[0].value.number;
193 if ((value >= 0.0) && (value < 1.0))
199 ERROR ("rrdcached plugin: The \"%s\" needs exactly one numeric argument "
200 "in the range [0.0, 1.0)", ci->key);
202 } /* int rc_config_get_xff */
204 static int rc_config_add_timespan (int timespan)
211 tmp = realloc (rrdcreate_config.timespans,
212 sizeof (*rrdcreate_config.timespans)
213 * (rrdcreate_config.timespans_num + 1));
216 rrdcreate_config.timespans = tmp;
218 rrdcreate_config.timespans[rrdcreate_config.timespans_num] = timespan;
219 rrdcreate_config.timespans_num++;
222 } /* int rc_config_add_timespan */
224 static int rc_config (oconfig_item_t *ci)
228 for (i = 0; i < ci->children_num; i++)
230 oconfig_item_t const *child = ci->children + i;
231 const char *key = child->key;
234 if (strcasecmp ("DataDir", key) == 0)
236 status = cf_util_get_string (child, &datadir);
239 int len = strlen (datadir);
241 while ((len > 0) && (datadir[len - 1] == '/'))
251 else if (strcasecmp ("DaemonAddress", key) == 0)
252 status = cf_util_get_string (child, &daemon_address);
253 else if (strcasecmp ("CreateFiles", key) == 0)
254 status = cf_util_get_boolean (child, &config_create_files);
255 else if (strcasecmp ("CreateFilesAsync", key) == 0)
256 status = cf_util_get_boolean (child, &rrdcreate_config.async);
257 else if (strcasecmp ("CollectStatistics", key) == 0)
258 status = cf_util_get_boolean (child, &config_collect_stats);
259 else if (strcasecmp ("StepSize", key) == 0)
263 status = rc_config_get_int_positive (child, &tmp);
265 rrdcreate_config.stepsize = (unsigned long) tmp;
267 else if (strcasecmp ("HeartBeat", key) == 0)
268 status = rc_config_get_int_positive (child, &rrdcreate_config.heartbeat);
269 else if (strcasecmp ("RRARows", key) == 0)
270 status = rc_config_get_int_positive (child, &rrdcreate_config.rrarows);
271 else if (strcasecmp ("RRATimespan", key) == 0)
274 status = rc_config_get_int_positive (child, &tmp);
276 status = rc_config_add_timespan (tmp);
278 else if (strcasecmp ("XFF", key) == 0)
279 status = rc_config_get_xff (child, &rrdcreate_config.xff);
282 WARNING ("rrdcached plugin: Ignoring invalid option %s.", key);
287 WARNING ("rrdcached plugin: Handling the \"%s\" option failed.", key);
290 if (daemon_address != NULL)
292 plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
293 plugin_register_flush ("rrdcached", rc_flush, /* user_data = */ NULL);
296 } /* int rc_config */
298 static int rc_read (void)
305 value_list_t vl = VALUE_LIST_INIT;
307 if (daemon_address == NULL)
310 if (!config_collect_stats)
316 if ((strncmp ("unix:", daemon_address, strlen ("unix:")) == 0)
317 || (daemon_address[0] == '/'))
318 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
320 sstrncpy (vl.host, daemon_address, sizeof (vl.host));
321 sstrncpy (vl.plugin, "rrdcached", sizeof (vl.plugin));
324 status = rrdc_stats_get (&head);
327 ERROR ("rrdcached plugin: rrdc_stats_get failed with status %i.", status);
331 for (ptr = head; ptr != NULL; ptr = ptr->next)
333 if (ptr->type == RRDC_STATS_TYPE_GAUGE)
334 values[0].gauge = (gauge_t) ptr->value.gauge;
335 else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
336 values[0].counter = (counter_t) ptr->value.counter;
340 if (strcasecmp ("QueueLength", ptr->name) == 0)
342 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
343 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
345 else if (strcasecmp ("UpdatesWritten", ptr->name) == 0)
347 sstrncpy (vl.type, "operations", sizeof (vl.type));
348 sstrncpy (vl.type_instance, "write-updates", sizeof (vl.type_instance));
350 else if (strcasecmp ("DataSetsWritten", ptr->name) == 0)
352 sstrncpy (vl.type, "operations", sizeof (vl.type));
353 sstrncpy (vl.type_instance, "write-data_sets",
354 sizeof (vl.type_instance));
356 else if (strcasecmp ("TreeNodesNumber", ptr->name) == 0)
358 sstrncpy (vl.type, "gauge", sizeof (vl.type));
359 sstrncpy (vl.type_instance, "tree_nodes", sizeof (vl.type_instance));
361 else if (strcasecmp ("TreeDepth", ptr->name) == 0)
363 sstrncpy (vl.type, "gauge", sizeof (vl.type));
364 sstrncpy (vl.type_instance, "tree_depth", sizeof (vl.type_instance));
366 else if (strcasecmp ("FlushesReceived", ptr->name) == 0)
368 sstrncpy (vl.type, "operations", sizeof (vl.type));
369 sstrncpy (vl.type_instance, "receive-flush", sizeof (vl.type_instance));
371 else if (strcasecmp ("JournalBytes", ptr->name) == 0)
373 sstrncpy (vl.type, "counter", sizeof (vl.type));
374 sstrncpy (vl.type_instance, "journal-bytes", sizeof (vl.type_instance));
376 else if (strcasecmp ("JournalRotate", ptr->name) == 0)
378 sstrncpy (vl.type, "counter", sizeof (vl.type));
379 sstrncpy (vl.type_instance, "journal-rotates", sizeof (vl.type_instance));
381 else if (strcasecmp ("UpdatesReceived", ptr->name) == 0)
383 sstrncpy (vl.type, "operations", sizeof (vl.type));
384 sstrncpy (vl.type_instance, "receive-update", sizeof (vl.type_instance));
388 DEBUG ("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
392 plugin_dispatch_values (&vl);
393 } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
395 rrdc_stats_free (head);
400 static int rc_init (void)
402 if (config_collect_stats)
403 plugin_register_read ("rrdcached", rc_read);
408 static int rc_write (const data_set_t *ds, const value_list_t *vl,
409 user_data_t __attribute__((unused)) *user_data)
411 char filename[PATH_MAX];
413 char *values_array[2];
416 if (daemon_address == NULL)
418 ERROR ("rrdcached plugin: daemon_address == NULL.");
419 plugin_unregister_write ("rrdcached");
423 if (strcmp (ds->type, vl->type) != 0)
425 ERROR ("rrdcached plugin: DS type does not match value list type");
429 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
431 ERROR ("rrdcached plugin: value_list_to_filename failed.");
435 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
437 ERROR ("rrdcached plugin: value_list_to_string failed.");
441 values_array[0] = values;
442 values_array[1] = NULL;
444 if (config_create_files)
448 status = stat (filename, &statbuf);
454 ERROR ("rrdcached plugin: stat (%s) failed: %s",
455 filename, sstrerror (errno, errbuf, sizeof (errbuf)));
459 status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
462 ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
469 status = rrdc_connect (daemon_address);
472 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
473 daemon_address, status);
477 status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
480 ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
482 filename, values_array[0], status);
489 static int rc_flush (__attribute__((unused)) cdtime_t timeout, /* {{{ */
490 const char *identifier,
491 __attribute__((unused)) user_data_t *ud)
493 char filename[PATH_MAX + 1];
496 if (identifier == NULL)
500 ssnprintf (filename, sizeof (filename), "%s/%s.rrd", datadir, identifier);
502 ssnprintf (filename, sizeof (filename), "%s.rrd", identifier);
504 status = rrdc_connect (daemon_address);
507 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
508 daemon_address, status);
512 status = rrdc_flush (filename);
515 ERROR ("rrdcached plugin: rrdc_flush (%s) failed with status %i.",
519 DEBUG ("rrdcached plugin: rrdc_flush (%s): Success.", filename);
522 } /* }}} int rc_flush */
524 static int rc_shutdown (void)
528 } /* int rc_shutdown */
530 void module_register (void)
532 plugin_register_complex_config ("rrdcached", rc_config);
533 plugin_register_init ("rrdcached", rc_init);
534 plugin_register_shutdown ("rrdcached", rc_shutdown);
535 } /* void module_register */
538 * vim: set sw=2 sts=2 et :