1 /**
2 * collectd - src/rrdcached.c
3 * Copyright (C) 2008-2013 Florian octo Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
29 #include "plugin.h"
30 #include "common.h"
31 #include "utils_rrdcreate.h"
33 #undef HAVE_CONFIG_H
34 #include <rrd.h>
35 #include <rrd_client.h>
37 /*
38 * Private variables
39 */
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 =
45 {
46 /* stepsize = */ 0,
47 /* heartbeat = */ 0,
48 /* rrarows = */ 1200,
49 /* xff = */ 0.1,
51 /* timespans = */ NULL,
52 /* timespans_num = */ 0,
54 /* consolidation_functions = */ NULL,
55 /* consolidation_functions_num = */ 0,
57 /* async = */ 0
58 };
60 /*
61 * Prototypes.
62 */
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)
70 {
71 int offset;
72 int status;
73 time_t t;
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))
82 return (-1);
83 offset = status;
85 for (size_t i = 0; i < ds->ds_num; i++)
86 {
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))
91 return (-1);
93 if (ds->ds[i].type == DS_TYPE_COUNTER)
94 {
95 status = ssnprintf (buffer + offset, buffer_len - offset,
96 ":%llu", vl->values[i].counter);
97 }
98 else if (ds->ds[i].type == DS_TYPE_GAUGE)
99 {
100 status = ssnprintf (buffer + offset, buffer_len - offset,
101 ":%f", vl->values[i].gauge);
102 }
103 else if (ds->ds[i].type == DS_TYPE_DERIVE) {
104 status = ssnprintf (buffer + offset, buffer_len - offset,
105 ":%"PRIi64, vl->values[i].derive);
106 }
107 else /* if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */ {
108 status = ssnprintf (buffer + offset, buffer_len - offset,
109 ":%"PRIu64, vl->values[i].absolute);
111 }
113 if ((status < 1) || (status >= (buffer_len - offset)))
114 return (-1);
116 offset += status;
117 } /* for ds->ds_num */
119 return (0);
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)
124 {
125 char const suffix[] = ".rrd";
126 int status;
127 size_t len;
129 if (datadir != NULL)
130 {
131 size_t datadir_len = strlen (datadir) + 1;
133 if (datadir_len >= buffer_size)
134 return (ENOMEM);
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;
142 }
144 status = FORMAT_VL (buffer, buffer_size, vl);
145 if (status != 0)
146 return (status);
148 len = strlen (buffer);
149 assert (len < buffer_size);
150 buffer += len;
151 buffer_size -= len;
153 if (buffer_size <= sizeof (suffix))
154 return (ENOMEM);
156 memcpy (buffer, suffix, sizeof (suffix));
157 return (0);
158 } /* int value_list_to_filename */
160 static int rc_config_get_int_positive (oconfig_item_t const *ci, int *ret)
161 {
162 int status;
163 int tmp = 0;
165 status = cf_util_get_int (ci, &tmp);
166 if (status != 0)
167 return (status);
168 if (tmp < 0)
169 return (EINVAL);
171 *ret = tmp;
172 return (0);
173 } /* int rc_config_get_int_positive */
175 static int rc_config_get_xff (oconfig_item_t const *ci, double *ret)
176 {
177 double value;
179 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
180 {
181 ERROR ("rrdcached plugin: The \"%s\" needs exactly one numeric argument "
182 "in the range [0.0, 1.0)", ci->key);
183 return (EINVAL);
184 }
186 value = ci->values[0].value.number;
187 if ((value >= 0.0) && (value < 1.0))
188 {
189 *ret = value;
190 return (0);
191 }
193 ERROR ("rrdcached plugin: The \"%s\" needs exactly one numeric argument "
194 "in the range [0.0, 1.0)", ci->key);
195 return (EINVAL);
196 } /* int rc_config_get_xff */
198 static int rc_config_add_timespan (int timespan)
199 {
200 int *tmp;
202 if (timespan <= 0)
203 return (EINVAL);
205 tmp = realloc (rrdcreate_config.timespans,
206 sizeof (*rrdcreate_config.timespans)
207 * (rrdcreate_config.timespans_num + 1));
208 if (tmp == NULL)
209 return (ENOMEM);
210 rrdcreate_config.timespans = tmp;
212 rrdcreate_config.timespans[rrdcreate_config.timespans_num] = timespan;
213 rrdcreate_config.timespans_num++;
215 return (0);
216 } /* int rc_config_add_timespan */
218 static int rc_config (oconfig_item_t *ci)
219 {
220 for (int i = 0; i < ci->children_num; i++)
221 {
222 oconfig_item_t const *child = ci->children + i;
223 const char *key = child->key;
224 int status = 0;
226 if (strcasecmp ("DataDir", key) == 0)
227 {
228 status = cf_util_get_string (child, &datadir);
229 if (status == 0)
230 {
231 int len = strlen (datadir);
233 while ((len > 0) && (datadir[len - 1] == '/'))
234 {
235 len--;
236 datadir[len] = 0;
237 }
239 if (len <= 0)
240 sfree (datadir);
241 }
242 }
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)
252 {
253 int tmp = -1;
255 status = rc_config_get_int_positive (child, &tmp);
256 if (status == 0)
257 rrdcreate_config.stepsize = (unsigned long) tmp;
258 }
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)
264 {
265 int tmp = -1;
266 status = rc_config_get_int_positive (child, &tmp);
267 if (status == 0)
268 status = rc_config_add_timespan (tmp);
269 }
270 else if (strcasecmp ("XFF", key) == 0)
271 status = rc_config_get_xff (child, &rrdcreate_config.xff);
272 else
273 {
274 WARNING ("rrdcached plugin: Ignoring invalid option %s.", key);
275 continue;
276 }
278 if (status != 0)
279 WARNING ("rrdcached plugin: Handling the \"%s\" option failed.", key);
280 }
282 if (daemon_address != NULL)
283 {
284 plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
285 plugin_register_flush ("rrdcached", rc_flush, /* user_data = */ NULL);
286 }
287 return (0);
288 } /* int rc_config */
290 static int try_reconnect (void)
291 {
292 int status;
294 rrdc_disconnect ();
296 rrd_clear_error ();
297 status = rrdc_connect (daemon_address);
298 if (status != 0)
299 {
300 ERROR ("rrdcached plugin: Failed to reconnect to RRDCacheD "
301 "at %s: %s (status=%d)", daemon_address, rrd_get_error (), status);
302 return (-1);
303 }
305 INFO ("rrdcached plugin: Successfully reconnected to RRDCacheD "
306 "at %s", daemon_address);
307 return (0);
308 } /* int try_reconnect */
310 static int rc_read (void)
311 {
312 int status;
313 rrdc_stats_t *head;
314 _Bool retried = 0;
316 value_list_t vl = VALUE_LIST_INIT;
317 vl.values = &(value_t) { .gauge = NAN };
318 vl.values_len = 1;
320 if (daemon_address == NULL)
321 return (-1);
323 if (!config_collect_stats)
324 return (-1);
326 if ((strncmp ("unix:", daemon_address, strlen ("unix:")) != 0)
327 && (daemon_address[0] != '/'))
328 sstrncpy (vl.host, daemon_address, sizeof (vl.host));
329 sstrncpy (vl.plugin, "rrdcached", sizeof (vl.plugin));
331 rrd_clear_error ();
332 status = rrdc_connect (daemon_address);
333 if (status != 0)
334 {
335 ERROR ("rrdcached plugin: Failed to connect to RRDCacheD "
336 "at %s: %s (status=%d)", daemon_address, rrd_get_error (), status);
337 return (-1);
338 }
340 while (42)
341 {
342 /* The RRD client lib does not provide any means for checking a
343 * connection, hence we'll have to retry upon failed operations. */
344 head = NULL;
345 rrd_clear_error ();
346 status = rrdc_stats_get (&head);
347 if (status == 0)
348 break;
350 if (!retried)
351 {
352 retried = 1;
353 if (try_reconnect () == 0)
354 continue;
355 /* else: report the error and fail */
356 }
358 ERROR ("rrdcached plugin: rrdc_stats_get failed: %s (status=%i).",
359 rrd_get_error (), status);
360 return (-1);
361 }
363 for (rrdc_stats_t *ptr = head; ptr != NULL; ptr = ptr->next)
364 {
365 if (ptr->type == RRDC_STATS_TYPE_GAUGE)
366 vl.values[0].gauge = (gauge_t) ptr->value.gauge;
367 else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
368 vl.values[0].counter = (counter_t) ptr->value.counter;
369 else
370 continue;
372 if (strcasecmp ("QueueLength", ptr->name) == 0)
373 {
374 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
375 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
376 }
377 else if (strcasecmp ("UpdatesWritten", ptr->name) == 0)
378 {
379 sstrncpy (vl.type, "operations", sizeof (vl.type));
380 sstrncpy (vl.type_instance, "write-updates", sizeof (vl.type_instance));
381 }
382 else if (strcasecmp ("DataSetsWritten", ptr->name) == 0)
383 {
384 sstrncpy (vl.type, "operations", sizeof (vl.type));
385 sstrncpy (vl.type_instance, "write-data_sets",
386 sizeof (vl.type_instance));
387 }
388 else if (strcasecmp ("TreeNodesNumber", ptr->name) == 0)
389 {
390 sstrncpy (vl.type, "gauge", sizeof (vl.type));
391 sstrncpy (vl.type_instance, "tree_nodes", sizeof (vl.type_instance));
392 }
393 else if (strcasecmp ("TreeDepth", ptr->name) == 0)
394 {
395 sstrncpy (vl.type, "gauge", sizeof (vl.type));
396 sstrncpy (vl.type_instance, "tree_depth", sizeof (vl.type_instance));
397 }
398 else if (strcasecmp ("FlushesReceived", ptr->name) == 0)
399 {
400 sstrncpy (vl.type, "operations", sizeof (vl.type));
401 sstrncpy (vl.type_instance, "receive-flush", sizeof (vl.type_instance));
402 }
403 else if (strcasecmp ("JournalBytes", ptr->name) == 0)
404 {
405 sstrncpy (vl.type, "counter", sizeof (vl.type));
406 sstrncpy (vl.type_instance, "journal-bytes", sizeof (vl.type_instance));
407 }
408 else if (strcasecmp ("JournalRotate", ptr->name) == 0)
409 {
410 sstrncpy (vl.type, "counter", sizeof (vl.type));
411 sstrncpy (vl.type_instance, "journal-rotates", sizeof (vl.type_instance));
412 }
413 else if (strcasecmp ("UpdatesReceived", ptr->name) == 0)
414 {
415 sstrncpy (vl.type, "operations", sizeof (vl.type));
416 sstrncpy (vl.type_instance, "receive-update", sizeof (vl.type_instance));
417 }
418 else
419 {
420 DEBUG ("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
421 continue;
422 }
424 plugin_dispatch_values (&vl);
425 } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
427 rrdc_stats_free (head);
429 return (0);
430 } /* int rc_read */
432 static int rc_init (void)
433 {
434 if (config_collect_stats)
435 plugin_register_read ("rrdcached", rc_read);
437 return (0);
438 } /* int rc_init */
440 static int rc_write (const data_set_t *ds, const value_list_t *vl,
441 user_data_t __attribute__((unused)) *user_data)
442 {
443 char filename[PATH_MAX];
444 char values[512];
445 char *values_array[2];
446 int status;
447 _Bool retried = 0;
449 if (daemon_address == NULL)
450 {
451 ERROR ("rrdcached plugin: daemon_address == NULL.");
452 plugin_unregister_write ("rrdcached");
453 return (-1);
454 }
456 if (strcmp (ds->type, vl->type) != 0)
457 {
458 ERROR ("rrdcached plugin: DS type does not match value list type");
459 return (-1);
460 }
462 if (value_list_to_filename (filename, sizeof (filename), vl) != 0)
463 {
464 ERROR ("rrdcached plugin: value_list_to_filename failed.");
465 return (-1);
466 }
468 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
469 {
470 ERROR ("rrdcached plugin: value_list_to_string failed.");
471 return (-1);
472 }
474 values_array[0] = values;
475 values_array[1] = NULL;
477 if (config_create_files)
478 {
479 struct stat statbuf;
481 status = stat (filename, &statbuf);
482 if (status != 0)
483 {
484 if (errno != ENOENT)
485 {
486 char errbuf[1024];
487 ERROR ("rrdcached plugin: stat (%s) failed: %s",
488 filename, sstrerror (errno, errbuf, sizeof (errbuf)));
489 return (-1);
490 }
492 status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
493 if (status != 0)
494 {
495 ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
496 filename);
497 return (-1);
498 }
499 else if (rrdcreate_config.async)
500 return (0);
501 }
502 }
504 rrd_clear_error ();
505 status = rrdc_connect (daemon_address);
506 if (status != 0)
507 {
508 ERROR ("rrdcached plugin: Failed to connect to RRDCacheD "
509 "at %s: %s (status=%d)", daemon_address, rrd_get_error (), status);
510 return (-1);
511 }
513 while (42)
514 {
515 /* The RRD client lib does not provide any means for checking a
516 * connection, hence we'll have to retry upon failed operations. */
517 rrd_clear_error ();
518 status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
519 if (status == 0)
520 break;
522 if (!retried)
523 {
524 retried = 1;
525 if (try_reconnect () == 0)
526 continue;
527 /* else: report the error and fail */
528 }
530 ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed: %s (status=%i)",
531 filename, values_array[0], rrd_get_error (), status);
532 return (-1);
533 }
535 return (0);
536 } /* int rc_write */
538 static int rc_flush (__attribute__((unused)) cdtime_t timeout, /* {{{ */
539 const char *identifier,
540 __attribute__((unused)) user_data_t *ud)
541 {
542 char filename[PATH_MAX + 1];
543 int status;
544 _Bool retried = 0;
546 if (identifier == NULL)
547 return (EINVAL);
549 if (datadir != NULL)
550 ssnprintf (filename, sizeof (filename), "%s/%s.rrd", datadir, identifier);
551 else
552 ssnprintf (filename, sizeof (filename), "%s.rrd", identifier);
554 rrd_clear_error ();
555 status = rrdc_connect (daemon_address);
556 if (status != 0)
557 {
558 ERROR ("rrdcached plugin: Failed to connect to RRDCacheD "
559 "at %s: %s (status=%d)", daemon_address, rrd_get_error (), status);
560 return (-1);
561 }
563 while (42)
564 {
565 /* The RRD client lib does not provide any means for checking a
566 * connection, hence we'll have to retry upon failed operations. */
567 rrd_clear_error ();
568 status = rrdc_flush (filename);
569 if (status == 0)
570 break;
572 if (!retried)
573 {
574 retried = 1;
575 if (try_reconnect () == 0)
576 continue;
577 /* else: report the error and fail */
578 }
580 ERROR ("rrdcached plugin: rrdc_flush (%s) failed: %s (status=%i).",
581 filename, rrd_get_error (), status);
582 return (-1);
583 }
584 DEBUG ("rrdcached plugin: rrdc_flush (%s): Success.", filename);
586 return (0);
587 } /* }}} int rc_flush */
589 static int rc_shutdown (void)
590 {
591 rrdc_disconnect ();
592 return (0);
593 } /* int rc_shutdown */
595 void module_register (void)
596 {
597 plugin_register_complex_config ("rrdcached", rc_config);
598 plugin_register_init ("rrdcached", rc_init);
599 plugin_register_shutdown ("rrdcached", rc_shutdown);
600 } /* void module_register */
602 /*
603 * vim: set sw=2 sts=2 et :
604 */