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"
28 #include <rrd_client.h>
33 static const char *config_keys[] =
40 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
42 static char *datadir = NULL;
43 static char *daemon_address = NULL;
44 static int config_create_files = 1;
45 static int config_collect_stats = 1;
46 static rrdcreate_config_t rrdcreate_config =
53 /* timespans = */ NULL,
54 /* timespans_num = */ 0,
56 /* consolidation_functions = */ NULL,
57 /* consolidation_functions_num = */ 0
60 static int value_list_to_string (char *buffer, int buffer_len,
61 const data_set_t *ds, const value_list_t *vl)
67 assert (0 == strcmp (ds->type, vl->type));
69 memset (buffer, '\0', buffer_len);
71 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
72 if ((status < 1) || (status >= buffer_len))
76 for (i = 0; i < ds->ds_num; i++)
78 if ((ds->ds[i].type != DS_TYPE_COUNTER)
79 && (ds->ds[i].type != DS_TYPE_GAUGE)
80 && (ds->ds[i].type != DS_TYPE_DERIVE)
81 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
84 if (ds->ds[i].type == DS_TYPE_COUNTER)
86 status = ssnprintf (buffer + offset, buffer_len - offset,
87 ":%llu", vl->values[i].counter);
89 else if (ds->ds[i].type == DS_TYPE_GAUGE)
91 status = ssnprintf (buffer + offset, buffer_len - offset,
92 ":%f", vl->values[i].gauge);
94 else if (ds->ds[i].type == DS_TYPE_DERIVE) {
95 status = ssnprintf (buffer + offset, buffer_len - offset,
96 ":%"PRIi64, vl->values[i].derive);
98 else /* if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */ {
99 status = ssnprintf (buffer + offset, buffer_len - offset,
100 ":%"PRIu64, vl->values[i].absolute);
104 if ((status < 1) || (status >= (buffer_len - offset)))
108 } /* for ds->ds_num */
111 } /* int value_list_to_string */
113 static int value_list_to_filename (char *buffer, int buffer_len,
114 const data_set_t *ds, const value_list_t *vl)
119 assert (0 == strcmp (ds->type, vl->type));
123 status = ssnprintf (buffer + offset, buffer_len - offset,
125 if ((status < 1) || (status >= buffer_len - offset))
130 status = ssnprintf (buffer + offset, buffer_len - offset,
132 if ((status < 1) || (status >= buffer_len - offset))
136 if (strlen (vl->plugin_instance) > 0)
137 status = ssnprintf (buffer + offset, buffer_len - offset,
138 "%s-%s/", vl->plugin, vl->plugin_instance);
140 status = ssnprintf (buffer + offset, buffer_len - offset,
142 if ((status < 1) || (status >= buffer_len - offset))
146 if (strlen (vl->type_instance) > 0)
147 status = ssnprintf (buffer + offset, buffer_len - offset,
148 "%s-%s", vl->type, vl->type_instance);
150 status = ssnprintf (buffer + offset, buffer_len - offset,
152 if ((status < 1) || (status >= buffer_len - offset))
156 strncpy (buffer + offset, ".rrd", buffer_len - offset);
157 buffer[buffer_len - 1] = 0;
160 } /* int value_list_to_filename */
162 static int rc_config (const char *key, const char *value)
164 if (strcasecmp ("DataDir", key) == 0)
168 datadir = strdup (value);
171 int len = strlen (datadir);
172 while ((len > 0) && (datadir[len - 1] == '/'))
184 else if (strcasecmp ("DaemonAddress", key) == 0)
186 sfree (daemon_address);
187 daemon_address = strdup (value);
188 if (daemon_address == NULL)
190 ERROR ("rrdcached plugin: strdup failed.");
194 else if (strcasecmp ("CreateFiles", key) == 0)
196 if (IS_FALSE (value))
197 config_create_files = 0;
199 config_create_files = 1;
201 else if (strcasecmp ("CollectStatistics", key) == 0)
203 if (IS_FALSE (value))
204 config_collect_stats = 0;
206 config_collect_stats = 1;
213 } /* int rc_config */
215 static int rc_read (void)
222 value_list_t vl = VALUE_LIST_INIT;
224 if (daemon_address == NULL)
227 if (config_collect_stats == 0)
233 if ((strncmp ("unix:", daemon_address, strlen ("unix:")) == 0)
234 || (daemon_address[0] == '/'))
235 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
237 sstrncpy (vl.host, daemon_address, sizeof (vl.host));
238 sstrncpy (vl.plugin, "rrdcached", sizeof (vl.plugin));
241 status = rrdc_stats_get (&head);
244 ERROR ("rrdcached plugin: rrdc_stats_get failed with status %i.", status);
248 for (ptr = head; ptr != NULL; ptr = ptr->next)
250 if (ptr->type == RRDC_STATS_TYPE_GAUGE)
251 values[0].gauge = (gauge_t) ptr->value.gauge;
252 else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
253 values[0].counter = (counter_t) ptr->value.counter;
257 if (strcasecmp ("QueueLength", ptr->name) == 0)
259 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
260 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
262 else if (strcasecmp ("UpdatesWritten", ptr->name) == 0)
264 sstrncpy (vl.type, "operations", sizeof (vl.type));
265 sstrncpy (vl.type_instance, "write-updates", sizeof (vl.type_instance));
267 else if (strcasecmp ("DataSetsWritten", ptr->name) == 0)
269 sstrncpy (vl.type, "operations", sizeof (vl.type));
270 sstrncpy (vl.type_instance, "write-data_sets",
271 sizeof (vl.type_instance));
273 else if (strcasecmp ("TreeNodesNumber", ptr->name) == 0)
275 sstrncpy (vl.type, "gauge", sizeof (vl.type));
276 sstrncpy (vl.type_instance, "tree_nodes", sizeof (vl.type_instance));
278 else if (strcasecmp ("TreeDepth", ptr->name) == 0)
280 sstrncpy (vl.type, "gauge", sizeof (vl.type));
281 sstrncpy (vl.type_instance, "tree_depth", sizeof (vl.type_instance));
283 else if (strcasecmp ("FlushesReceived", ptr->name) == 0)
285 sstrncpy (vl.type, "operations", sizeof (vl.type));
286 sstrncpy (vl.type_instance, "receive-flush", sizeof (vl.type_instance));
288 else if (strcasecmp ("JournalBytes", ptr->name) == 0)
290 sstrncpy (vl.type, "counter", sizeof (vl.type));
291 sstrncpy (vl.type_instance, "journal-bytes", sizeof (vl.type_instance));
293 else if (strcasecmp ("JournalRotate", ptr->name) == 0)
295 sstrncpy (vl.type, "counter", sizeof (vl.type));
296 sstrncpy (vl.type_instance, "journal-rotates", sizeof (vl.type_instance));
298 else if (strcasecmp ("UpdatesReceived", ptr->name) == 0)
300 sstrncpy (vl.type, "operations", sizeof (vl.type));
301 sstrncpy (vl.type_instance, "receive-update", sizeof (vl.type_instance));
305 DEBUG ("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
309 plugin_dispatch_values (&vl);
310 } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
312 rrdc_stats_free (head);
317 static int rc_init (void)
319 if (config_collect_stats != 0)
320 plugin_register_read ("rrdcached", rc_read);
325 static int rc_write (const data_set_t *ds, const value_list_t *vl,
326 user_data_t __attribute__((unused)) *user_data)
330 char *values_array[2];
333 if (daemon_address == NULL)
335 ERROR ("rrdcached plugin: daemon_address == NULL.");
336 plugin_unregister_write ("rrdcached");
340 if (strcmp (ds->type, vl->type) != 0)
342 ERROR ("rrdcached plugin: DS type does not match value list type");
346 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
348 ERROR ("rrdcached plugin: value_list_to_filename failed.");
352 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
354 ERROR ("rrdcached plugin: value_list_to_string failed.");
358 values_array[0] = values;
359 values_array[1] = NULL;
361 if (config_create_files != 0)
365 status = stat (filename, &statbuf);
371 ERROR ("rrdcached plugin: stat (%s) failed: %s",
372 filename, sstrerror (errno, errbuf, sizeof (errbuf)));
376 status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
379 ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
386 status = rrdc_connect (daemon_address);
389 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
390 daemon_address, status);
394 status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
397 ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
399 filename, values_array[0], status);
406 static int rc_shutdown (void)
410 } /* int rc_shutdown */
412 void module_register (void)
414 plugin_register_config ("rrdcached", rc_config,
415 config_keys, config_keys_num);
416 plugin_register_init ("rrdcached", rc_init);
417 plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
418 plugin_register_shutdown ("rrdcached", rc_shutdown);
419 } /* void module_register */
422 * vim: set sw=2 sts=2 et :