1 /**
2 * collectd - src/rrdcached.c
3 * Copyright (C) 2008-2013 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at collectd.org>
20 **/
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25 #include "utils_rrdcreate.h"
27 #undef HAVE_CONFIG_H
28 #include <rrd.h>
29 #include <rrd_client.h>
31 /*
32 * Private variables
33 */
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 =
39 {
40 /* stepsize = */ 0,
41 /* heartbeat = */ 0,
42 /* rrarows = */ 1200,
43 /* xff = */ 0.1,
45 /* timespans = */ NULL,
46 /* timespans_num = */ 0,
48 /* consolidation_functions = */ NULL,
49 /* consolidation_functions_num = */ 0
50 };
52 /*
53 * Prototypes.
54 */
55 static int rc_write (const data_set_t *ds, const value_list_t *vl,
56 user_data_t __attribute__((unused)) *user_data);
57 static int rc_flush (__attribute__((unused)) cdtime_t timeout,
58 const char *identifier, __attribute__((unused)) user_data_t *ud);
60 static int value_list_to_string (char *buffer, int buffer_len,
61 const data_set_t *ds, const value_list_t *vl)
62 {
63 int offset;
64 int status;
65 int i;
66 time_t t;
68 assert (0 == strcmp (ds->type, vl->type));
70 memset (buffer, '\0', buffer_len);
72 t = CDTIME_T_TO_TIME_T (vl->time);
73 status = ssnprintf (buffer, buffer_len, "%lu", (unsigned long) t);
74 if ((status < 1) || (status >= buffer_len))
75 return (-1);
76 offset = status;
78 for (i = 0; i < ds->ds_num; i++)
79 {
80 if ((ds->ds[i].type != DS_TYPE_COUNTER)
81 && (ds->ds[i].type != DS_TYPE_GAUGE)
82 && (ds->ds[i].type != DS_TYPE_DERIVE)
83 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
84 return (-1);
86 if (ds->ds[i].type == DS_TYPE_COUNTER)
87 {
88 status = ssnprintf (buffer + offset, buffer_len - offset,
89 ":%llu", vl->values[i].counter);
90 }
91 else if (ds->ds[i].type == DS_TYPE_GAUGE)
92 {
93 status = ssnprintf (buffer + offset, buffer_len - offset,
94 ":%f", vl->values[i].gauge);
95 }
96 else if (ds->ds[i].type == DS_TYPE_DERIVE) {
97 status = ssnprintf (buffer + offset, buffer_len - offset,
98 ":%"PRIi64, vl->values[i].derive);
99 }
100 else /* if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */ {
101 status = ssnprintf (buffer + offset, buffer_len - offset,
102 ":%"PRIu64, vl->values[i].absolute);
104 }
106 if ((status < 1) || (status >= (buffer_len - offset)))
107 return (-1);
109 offset += status;
110 } /* for ds->ds_num */
112 return (0);
113 } /* int value_list_to_string */
115 static int value_list_to_filename (char *buffer, size_t buffer_size,
116 value_list_t const *vl)
117 {
118 char const suffix[] = ".rrd";
119 int status;
120 size_t len;
122 status = FORMAT_VL (buffer, buffer_size, vl);
123 if (status != 0)
124 return (status);
126 len = strlen (buffer);
127 assert (len < buffer_size);
128 buffer += len;
129 buffer_size -= len;
131 if (buffer_size <= sizeof (suffix))
132 return (ENOMEM);
134 memcpy (buffer, suffix, sizeof (suffix));
135 return (0);
136 } /* int value_list_to_filename */
138 static int rc_config_get_int_positive (oconfig_item_t const *ci, int *ret)
139 {
140 int status;
141 int tmp = 0;
143 status = cf_util_get_int (ci, &tmp);
144 if (status != 0)
145 return (status);
146 if (tmp < 0)
147 return (EINVAL);
149 *ret = tmp;
150 return (0);
151 } /* int rc_config_get_int_positive */
153 static int rc_config_get_xff (oconfig_item_t const *ci, double *ret)
154 {
155 double value;
157 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
158 {
159 ERROR ("rrdcached plugin: The \"%s\" needs exactly one numeric argument "
160 "in the range [0.0, 1.0)", ci->key);
161 return (EINVAL);
162 }
164 value = ci->values[0].value.number;
165 if ((value >= 0.0) && (value < 1.0))
166 {
167 *ret = value;
168 return (0);
169 }
171 ERROR ("rrdcached plugin: The \"%s\" needs exactly one numeric argument "
172 "in the range [0.0, 1.0)", ci->key);
173 return (EINVAL);
174 } /* int rc_config_get_xff */
176 static int rc_config_add_timespan (int timespan)
177 {
178 int *tmp;
180 if (timespan <= 0)
181 return (EINVAL);
183 tmp = realloc (rrdcreate_config.timespans,
184 sizeof (*rrdcreate_config.timespans)
185 * (rrdcreate_config.timespans_num + 1));
186 if (tmp == NULL)
187 return (ENOMEM);
188 rrdcreate_config.timespans = tmp;
190 rrdcreate_config.timespans[rrdcreate_config.timespans_num] = timespan;
191 rrdcreate_config.timespans_num++;
193 return (0);
194 } /* int rc_config_add_timespan */
196 static int rc_config (oconfig_item_t *ci)
197 {
198 int i;
200 for (i = 0; i < ci->children_num; i++)
201 {
202 oconfig_item_t const *child = ci->children + i;
203 const char *key = child->key;
204 int status = 0;
206 if (strcasecmp ("DataDir", key) == 0)
207 {
208 status = cf_util_get_string (child, &datadir);
209 if (status == 0)
210 {
211 int len = strlen (datadir);
213 while ((len > 0) && (datadir[len - 1] == '/'))
214 {
215 len--;
216 datadir[len] = 0;
217 }
219 if (len <= 0)
220 sfree (datadir);
221 }
222 }
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 ("CollectStatistics", key) == 0)
228 status = cf_util_get_boolean (child, &config_collect_stats);
229 else if (strcasecmp ("StepSize", key) == 0)
230 {
231 int tmp = -1;
233 status = rc_config_get_int_positive (child, &tmp);
234 if (status == 0)
235 rrdcreate_config.stepsize = (unsigned long) tmp;
236 }
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)
242 {
243 int tmp = -1;
244 status = rc_config_get_int_positive (child, &tmp);
245 if (status == 0)
246 status = rc_config_add_timespan (tmp);
247 }
248 else if (strcasecmp ("XFF", key) == 0)
249 status = rc_config_get_xff (child, &rrdcreate_config.xff);
250 else
251 {
252 WARNING ("rrdcached plugin: Ignoring invalid option %s.", key);
253 continue;
254 }
256 if (status != 0)
257 WARNING ("rrdcached plugin: Handling the \"%s\" option failed.", key);
258 }
260 if (daemon_address != NULL)
261 {
262 plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
263 plugin_register_flush ("rrdcached", rc_flush, /* user_data = */ NULL);
264 }
265 return (0);
266 } /* int rc_config */
268 static int rc_read (void)
269 {
270 int status;
271 rrdc_stats_t *head;
272 rrdc_stats_t *ptr;
274 value_t values[1];
275 value_list_t vl = VALUE_LIST_INIT;
277 if (daemon_address == NULL)
278 return (-1);
280 if (!config_collect_stats)
281 return (-1);
283 vl.values = values;
284 vl.values_len = 1;
286 if ((strncmp ("unix:", daemon_address, strlen ("unix:")) == 0)
287 || (daemon_address[0] == '/'))
288 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
289 else
290 sstrncpy (vl.host, daemon_address, sizeof (vl.host));
291 sstrncpy (vl.plugin, "rrdcached", sizeof (vl.plugin));
293 status = rrdc_connect (daemon_address);
294 if (status != 0)
295 {
296 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
297 daemon_address, status);
298 return (-1);
299 }
301 head = NULL;
302 status = rrdc_stats_get (&head);
303 if (status != 0)
304 {
305 ERROR ("rrdcached plugin: rrdc_stats_get failed with status %i.", status);
306 return (-1);
307 }
309 for (ptr = head; ptr != NULL; ptr = ptr->next)
310 {
311 if (ptr->type == RRDC_STATS_TYPE_GAUGE)
312 values[0].gauge = (gauge_t) ptr->value.gauge;
313 else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
314 values[0].counter = (counter_t) ptr->value.counter;
315 else
316 continue;
318 if (strcasecmp ("QueueLength", ptr->name) == 0)
319 {
320 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
321 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
322 }
323 else if (strcasecmp ("UpdatesWritten", ptr->name) == 0)
324 {
325 sstrncpy (vl.type, "operations", sizeof (vl.type));
326 sstrncpy (vl.type_instance, "write-updates", sizeof (vl.type_instance));
327 }
328 else if (strcasecmp ("DataSetsWritten", ptr->name) == 0)
329 {
330 sstrncpy (vl.type, "operations", sizeof (vl.type));
331 sstrncpy (vl.type_instance, "write-data_sets",
332 sizeof (vl.type_instance));
333 }
334 else if (strcasecmp ("TreeNodesNumber", ptr->name) == 0)
335 {
336 sstrncpy (vl.type, "gauge", sizeof (vl.type));
337 sstrncpy (vl.type_instance, "tree_nodes", sizeof (vl.type_instance));
338 }
339 else if (strcasecmp ("TreeDepth", ptr->name) == 0)
340 {
341 sstrncpy (vl.type, "gauge", sizeof (vl.type));
342 sstrncpy (vl.type_instance, "tree_depth", sizeof (vl.type_instance));
343 }
344 else if (strcasecmp ("FlushesReceived", ptr->name) == 0)
345 {
346 sstrncpy (vl.type, "operations", sizeof (vl.type));
347 sstrncpy (vl.type_instance, "receive-flush", sizeof (vl.type_instance));
348 }
349 else if (strcasecmp ("JournalBytes", ptr->name) == 0)
350 {
351 sstrncpy (vl.type, "counter", sizeof (vl.type));
352 sstrncpy (vl.type_instance, "journal-bytes", sizeof (vl.type_instance));
353 }
354 else if (strcasecmp ("JournalRotate", ptr->name) == 0)
355 {
356 sstrncpy (vl.type, "counter", sizeof (vl.type));
357 sstrncpy (vl.type_instance, "journal-rotates", sizeof (vl.type_instance));
358 }
359 else if (strcasecmp ("UpdatesReceived", ptr->name) == 0)
360 {
361 sstrncpy (vl.type, "operations", sizeof (vl.type));
362 sstrncpy (vl.type_instance, "receive-update", sizeof (vl.type_instance));
363 }
364 else
365 {
366 DEBUG ("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
367 continue;
368 }
370 plugin_dispatch_values (&vl);
371 } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
373 rrdc_stats_free (head);
375 return (0);
376 } /* int rc_read */
378 static int rc_init (void)
379 {
380 if (config_collect_stats)
381 plugin_register_read ("rrdcached", rc_read);
383 return (0);
384 } /* int rc_init */
386 static int rc_write (const data_set_t *ds, const value_list_t *vl,
387 user_data_t __attribute__((unused)) *user_data)
388 {
389 char filename[PATH_MAX];
390 char values[512];
391 char *values_array[2];
392 int status;
394 if (daemon_address == NULL)
395 {
396 ERROR ("rrdcached plugin: daemon_address == NULL.");
397 plugin_unregister_write ("rrdcached");
398 return (-1);
399 }
401 if (strcmp (ds->type, vl->type) != 0)
402 {
403 ERROR ("rrdcached plugin: DS type does not match value list type");
404 return (-1);
405 }
407 if (value_list_to_filename (filename, sizeof (filename), vl) != 0)
408 {
409 ERROR ("rrdcached plugin: value_list_to_filename failed.");
410 return (-1);
411 }
413 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
414 {
415 ERROR ("rrdcached plugin: value_list_to_string failed.");
416 return (-1);
417 }
419 values_array[0] = values;
420 values_array[1] = NULL;
422 if (config_create_files)
423 {
424 struct stat statbuf;
426 status = stat (filename, &statbuf);
427 if (status != 0)
428 {
429 if (errno != ENOENT)
430 {
431 char errbuf[1024];
432 ERROR ("rrdcached plugin: stat (%s) failed: %s",
433 filename, sstrerror (errno, errbuf, sizeof (errbuf)));
434 return (-1);
435 }
437 status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
438 if (status != 0)
439 {
440 ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
441 filename);
442 return (-1);
443 }
444 }
445 }
447 status = rrdc_connect (daemon_address);
448 if (status != 0)
449 {
450 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
451 daemon_address, status);
452 return (-1);
453 }
455 status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
456 if (status != 0)
457 {
458 ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
459 "status %i.",
460 filename, values_array[0], status);
461 return (-1);
462 }
464 return (0);
465 } /* int rc_write */
467 static int rc_flush (__attribute__((unused)) cdtime_t timeout, /* {{{ */
468 const char *identifier,
469 __attribute__((unused)) user_data_t *ud)
470 {
471 char filename[PATH_MAX + 1];
472 int status;
474 if (identifier == NULL)
475 return (EINVAL);
477 if (datadir != NULL)
478 ssnprintf (filename, sizeof (filename), "%s/%s.rrd", datadir, identifier);
479 else
480 ssnprintf (filename, sizeof (filename), "%s.rrd", identifier);
482 status = rrdc_connect (daemon_address);
483 if (status != 0)
484 {
485 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
486 daemon_address, status);
487 return (-1);
488 }
490 status = rrdc_flush (filename);
491 if (status != 0)
492 {
493 ERROR ("rrdcached plugin: rrdc_flush (%s) failed with status %i.",
494 filename, status);
495 return (-1);
496 }
497 DEBUG ("rrdcached plugin: rrdc_flush (%s): Success.", filename);
499 return (0);
500 } /* }}} int rc_flush */
502 static int rc_shutdown (void)
503 {
504 rrdc_disconnect ();
505 return (0);
506 } /* int rc_shutdown */
508 void module_register (void)
509 {
510 plugin_register_complex_config ("rrdcached", rc_config);
511 plugin_register_init ("rrdcached", rc_init);
512 plugin_register_shutdown ("rrdcached", rc_shutdown);
513 } /* void module_register */
515 /*
516 * vim: set sw=2 sts=2 et :
517 */