2 * collectd - src/rrdcached.c
3 * Copyright (C) 2008 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 verplant.org>
25 #include "utils_rrdcreate.h"
29 #include <rrd_client.h>
34 static char *datadir = NULL;
35 static char *daemon_address = NULL;
36 static int config_create_files = 1;
37 static int 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
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)
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))
78 for (i = 0; i < ds->ds_num; i++)
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))
86 if (ds->ds[i].type == DS_TYPE_COUNTER)
88 status = ssnprintf (buffer + offset, buffer_len - offset,
89 ":%llu", vl->values[i].counter);
91 else if (ds->ds[i].type == DS_TYPE_GAUGE)
93 status = ssnprintf (buffer + offset, buffer_len - offset,
94 ":%f", vl->values[i].gauge);
96 else if (ds->ds[i].type == DS_TYPE_DERIVE) {
97 status = ssnprintf (buffer + offset, buffer_len - offset,
98 ":%"PRIi64, vl->values[i].derive);
100 else /* if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */ {
101 status = ssnprintf (buffer + offset, buffer_len - offset,
102 ":%"PRIu64, vl->values[i].absolute);
106 if ((status < 1) || (status >= (buffer_len - offset)))
110 } /* for ds->ds_num */
113 } /* int value_list_to_string */
115 static int value_list_to_filename (char *buffer, int buffer_len,
116 const data_set_t *ds, const value_list_t *vl)
121 assert (0 == strcmp (ds->type, vl->type));
125 status = ssnprintf (buffer + offset, buffer_len - offset,
127 if ((status < 1) || (status >= buffer_len - offset))
132 status = ssnprintf (buffer + offset, buffer_len - offset,
134 if ((status < 1) || (status >= buffer_len - offset))
138 if (strlen (vl->plugin_instance) > 0)
139 status = ssnprintf (buffer + offset, buffer_len - offset,
140 "%s-%s/", vl->plugin, vl->plugin_instance);
142 status = ssnprintf (buffer + offset, buffer_len - offset,
144 if ((status < 1) || (status >= buffer_len - offset))
148 if (strlen (vl->type_instance) > 0)
149 status = ssnprintf (buffer + offset, buffer_len - offset,
150 "%s-%s", vl->type, vl->type_instance);
152 status = ssnprintf (buffer + offset, buffer_len - offset,
154 if ((status < 1) || (status >= buffer_len - offset))
158 strncpy (buffer + offset, ".rrd", buffer_len - offset);
159 buffer[buffer_len - 1] = 0;
162 } /* int value_list_to_filename */
164 static const char *config_get_string (oconfig_item_t *ci)
166 if ((ci->children_num != 0) || (ci->values_num != 1)
167 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
168 && (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)))
170 ERROR ("rrdcached plugin: %s expects a single string argument.",
175 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN) {
176 if (ci->values[0].value.boolean)
181 return (ci->values[0].value.string);
182 } /* const char *config_get_string */
184 static int rc_config (oconfig_item_t *ci)
188 for (i = 0; i < ci->children_num; ++i) {
189 const char *key = ci->children[i].key;
190 const char *value = config_get_string (ci->children + i);
192 if (value == NULL) /* config_get_strings prints error message */
195 if (strcasecmp ("DataDir", key) == 0)
199 datadir = strdup (value);
202 int len = strlen (datadir);
203 while ((len > 0) && (datadir[len - 1] == '/'))
215 else if (strcasecmp ("DaemonAddress", key) == 0)
217 sfree (daemon_address);
218 daemon_address = strdup (value);
219 if (daemon_address == NULL)
221 ERROR ("rrdcached plugin: strdup failed.");
225 else if (strcasecmp ("CreateFiles", key) == 0)
227 if (IS_FALSE (value))
228 config_create_files = 0;
230 config_create_files = 1;
232 else if (strcasecmp ("CollectStatistics", key) == 0)
234 if (IS_FALSE (value))
235 config_collect_stats = 0;
237 config_collect_stats = 1;
241 WARNING ("rrdcached plugin: Ignoring invalid option %s.", key);
246 if (daemon_address != NULL) {
247 plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
248 plugin_register_flush ("rrdcached", rc_flush, /* user_data = */ NULL);
251 } /* int rc_config */
253 static int rc_read (void)
260 value_list_t vl = VALUE_LIST_INIT;
262 if (daemon_address == NULL)
265 if (config_collect_stats == 0)
271 if ((strncmp ("unix:", daemon_address, strlen ("unix:")) == 0)
272 || (daemon_address[0] == '/'))
273 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
275 sstrncpy (vl.host, daemon_address, sizeof (vl.host));
276 sstrncpy (vl.plugin, "rrdcached", sizeof (vl.plugin));
279 status = rrdc_stats_get (&head);
282 ERROR ("rrdcached plugin: rrdc_stats_get failed with status %i.", status);
286 for (ptr = head; ptr != NULL; ptr = ptr->next)
288 if (ptr->type == RRDC_STATS_TYPE_GAUGE)
289 values[0].gauge = (gauge_t) ptr->value.gauge;
290 else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
291 values[0].counter = (counter_t) ptr->value.counter;
295 if (strcasecmp ("QueueLength", ptr->name) == 0)
297 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
298 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
300 else if (strcasecmp ("UpdatesWritten", ptr->name) == 0)
302 sstrncpy (vl.type, "operations", sizeof (vl.type));
303 sstrncpy (vl.type_instance, "write-updates", sizeof (vl.type_instance));
305 else if (strcasecmp ("DataSetsWritten", ptr->name) == 0)
307 sstrncpy (vl.type, "operations", sizeof (vl.type));
308 sstrncpy (vl.type_instance, "write-data_sets",
309 sizeof (vl.type_instance));
311 else if (strcasecmp ("TreeNodesNumber", ptr->name) == 0)
313 sstrncpy (vl.type, "gauge", sizeof (vl.type));
314 sstrncpy (vl.type_instance, "tree_nodes", sizeof (vl.type_instance));
316 else if (strcasecmp ("TreeDepth", ptr->name) == 0)
318 sstrncpy (vl.type, "gauge", sizeof (vl.type));
319 sstrncpy (vl.type_instance, "tree_depth", sizeof (vl.type_instance));
321 else if (strcasecmp ("FlushesReceived", ptr->name) == 0)
323 sstrncpy (vl.type, "operations", sizeof (vl.type));
324 sstrncpy (vl.type_instance, "receive-flush", sizeof (vl.type_instance));
326 else if (strcasecmp ("JournalBytes", ptr->name) == 0)
328 sstrncpy (vl.type, "counter", sizeof (vl.type));
329 sstrncpy (vl.type_instance, "journal-bytes", sizeof (vl.type_instance));
331 else if (strcasecmp ("JournalRotate", ptr->name) == 0)
333 sstrncpy (vl.type, "counter", sizeof (vl.type));
334 sstrncpy (vl.type_instance, "journal-rotates", sizeof (vl.type_instance));
336 else if (strcasecmp ("UpdatesReceived", ptr->name) == 0)
338 sstrncpy (vl.type, "operations", sizeof (vl.type));
339 sstrncpy (vl.type_instance, "receive-update", sizeof (vl.type_instance));
343 DEBUG ("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
347 plugin_dispatch_values (&vl);
348 } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
350 rrdc_stats_free (head);
355 static int rc_init (void)
357 if (config_collect_stats != 0)
358 plugin_register_read ("rrdcached", rc_read);
363 static int rc_write (const data_set_t *ds, const value_list_t *vl,
364 user_data_t __attribute__((unused)) *user_data)
366 char filename[PATH_MAX];
368 char *values_array[2];
371 if (daemon_address == NULL)
373 ERROR ("rrdcached plugin: daemon_address == NULL.");
374 plugin_unregister_write ("rrdcached");
378 if (strcmp (ds->type, vl->type) != 0)
380 ERROR ("rrdcached plugin: DS type does not match value list type");
384 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
386 ERROR ("rrdcached plugin: value_list_to_filename failed.");
390 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
392 ERROR ("rrdcached plugin: value_list_to_string failed.");
396 values_array[0] = values;
397 values_array[1] = NULL;
399 if (config_create_files != 0)
403 status = stat (filename, &statbuf);
409 ERROR ("rrdcached plugin: stat (%s) failed: %s",
410 filename, sstrerror (errno, errbuf, sizeof (errbuf)));
414 status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
417 ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
424 status = rrdc_connect (daemon_address);
427 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
428 daemon_address, status);
432 status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
435 ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
437 filename, values_array[0], status);
444 static int rc_flush (__attribute__((unused)) cdtime_t timeout, /* {{{ */
445 const char *identifier,
446 __attribute__((unused)) user_data_t *ud)
448 char filename[PATH_MAX + 1];
451 if (identifier == NULL)
455 ssnprintf (filename, sizeof (filename), "%s/%s.rrd", datadir, identifier);
457 ssnprintf (filename, sizeof (filename), "%s.rrd", identifier);
459 status = rrdc_connect (daemon_address);
462 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
463 daemon_address, status);
467 status = rrdc_flush (filename);
470 ERROR ("rrdcached plugin: rrdc_flush (%s) failed with status %i.",
474 DEBUG ("rrdcached plugin: rrdc_flush (%s): Success.", filename);
477 } /* }}} int rc_flush */
479 static int rc_shutdown (void)
483 } /* int rc_shutdown */
485 void module_register (void)
487 plugin_register_complex_config ("rrdcached", rc_config);
488 plugin_register_init ("rrdcached", rc_init);
489 plugin_register_shutdown ("rrdcached", rc_shutdown);
490 } /* void module_register */
493 * vim: set sw=2 sts=2 et :