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 const char *config_keys[] =
41 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
43 static char *datadir = NULL;
44 static char *daemon_address = NULL;
45 static int config_create_files = 1;
46 static int config_collect_stats = 1;
47 static rrdcreate_config_t rrdcreate_config =
54 /* timespans = */ NULL,
55 /* timespans_num = */ 0,
57 /* consolidation_functions = */ NULL,
58 /* consolidation_functions_num = */ 0
61 static int value_list_to_string (char *buffer, int buffer_len,
62 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 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
73 if ((status < 1) || (status >= buffer_len))
77 for (i = 0; i < ds->ds_num; i++)
79 if ((ds->ds[i].type != DS_TYPE_COUNTER)
80 && (ds->ds[i].type != DS_TYPE_GAUGE)
81 && (ds->ds[i].type != DS_TYPE_DERIVE)
82 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
85 if (ds->ds[i].type == DS_TYPE_COUNTER)
87 status = ssnprintf (buffer + offset, buffer_len - offset,
88 ":%llu", vl->values[i].counter);
90 else if (ds->ds[i].type == DS_TYPE_GAUGE)
92 status = ssnprintf (buffer + offset, buffer_len - offset,
93 ":%f", vl->values[i].gauge);
95 else if (ds->ds[i].type == DS_TYPE_DERIVE) {
96 status = ssnprintf (buffer + offset, buffer_len - offset,
97 ":%"PRIi64, vl->values[i].derive);
99 else /* if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */ {
100 status = ssnprintf (buffer + offset, buffer_len - offset,
101 ":%"PRIu64, vl->values[i].absolute);
105 if ((status < 1) || (status >= (buffer_len - offset)))
109 } /* for ds->ds_num */
112 } /* int value_list_to_string */
114 static int value_list_to_filename (char *buffer, int buffer_len,
115 const data_set_t *ds, const value_list_t *vl)
120 assert (0 == strcmp (ds->type, vl->type));
124 status = ssnprintf (buffer + offset, buffer_len - offset,
126 if ((status < 1) || (status >= buffer_len - offset))
131 status = ssnprintf (buffer + offset, buffer_len - offset,
133 if ((status < 1) || (status >= buffer_len - offset))
137 if (strlen (vl->plugin_instance) > 0)
138 status = ssnprintf (buffer + offset, buffer_len - offset,
139 "%s-%s/", vl->plugin, vl->plugin_instance);
141 status = ssnprintf (buffer + offset, buffer_len - offset,
143 if ((status < 1) || (status >= buffer_len - offset))
147 if (strlen (vl->type_instance) > 0)
148 status = ssnprintf (buffer + offset, buffer_len - offset,
149 "%s-%s", vl->type, vl->type_instance);
151 status = ssnprintf (buffer + offset, buffer_len - offset,
153 if ((status < 1) || (status >= buffer_len - offset))
157 strncpy (buffer + offset, ".rrd", buffer_len - offset);
158 buffer[buffer_len - 1] = 0;
161 } /* int value_list_to_filename */
163 static int rc_config (const char *key, const char *value)
165 if (strcasecmp ("DataDir", key) == 0)
169 datadir = strdup (value);
172 int len = strlen (datadir);
173 while ((len > 0) && (datadir[len - 1] == '/'))
185 else if (strcasecmp ("DaemonAddress", key) == 0)
187 sfree (daemon_address);
188 daemon_address = strdup (value);
189 if (daemon_address == NULL)
191 ERROR ("rrdcached plugin: strdup failed.");
195 else if (strcasecmp ("CreateFiles", key) == 0)
197 if ((strcasecmp ("false", value) == 0)
198 || (strcasecmp ("no", value) == 0)
199 || (strcasecmp ("off", value) == 0))
200 config_create_files = 0;
202 config_create_files = 1;
204 else if (strcasecmp ("CollectStatistics", key) == 0)
206 if ((strcasecmp ("false", value) == 0)
207 || (strcasecmp ("no", value) == 0)
208 || (strcasecmp ("off", value) == 0))
209 config_collect_stats = 0;
211 config_collect_stats = 1;
218 } /* int rc_config */
220 static int rc_read (void)
227 value_list_t vl = VALUE_LIST_INIT;
229 if (daemon_address == NULL)
232 if (config_collect_stats == 0)
238 if ((strncmp ("unix:", daemon_address, strlen ("unix:")) == 0)
239 || (daemon_address[0] == '/'))
240 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
242 sstrncpy (vl.host, daemon_address, sizeof (vl.host));
243 sstrncpy (vl.plugin, "rrdcached", sizeof (vl.plugin));
246 status = rrdc_stats_get (&head);
249 ERROR ("rrdcached plugin: rrdc_stats_get failed with status %i.", status);
253 for (ptr = head; ptr != NULL; ptr = ptr->next)
255 if (ptr->type == RRDC_STATS_TYPE_GAUGE)
256 values[0].gauge = (gauge_t) ptr->value.gauge;
257 else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
258 values[0].counter = (counter_t) ptr->value.counter;
262 if (strcasecmp ("QueueLength", ptr->name) == 0)
264 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
265 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
267 else if (strcasecmp ("UpdatesWritten", ptr->name) == 0)
269 sstrncpy (vl.type, "operations", sizeof (vl.type));
270 sstrncpy (vl.type_instance, "write-updates", sizeof (vl.type_instance));
272 else if (strcasecmp ("DataSetsWritten", ptr->name) == 0)
274 sstrncpy (vl.type, "operations", sizeof (vl.type));
275 sstrncpy (vl.type_instance, "write-data_sets",
276 sizeof (vl.type_instance));
278 else if (strcasecmp ("TreeNodesNumber", ptr->name) == 0)
280 sstrncpy (vl.type, "gauge", sizeof (vl.type));
281 sstrncpy (vl.type_instance, "tree_nodes", sizeof (vl.type_instance));
283 else if (strcasecmp ("TreeDepth", ptr->name) == 0)
285 sstrncpy (vl.type, "gauge", sizeof (vl.type));
286 sstrncpy (vl.type_instance, "tree_depth", sizeof (vl.type_instance));
288 else if (strcasecmp ("FlushesReceived", ptr->name) == 0)
290 sstrncpy (vl.type, "operations", sizeof (vl.type));
291 sstrncpy (vl.type_instance, "receive-flush", sizeof (vl.type_instance));
293 else if (strcasecmp ("JournalBytes", ptr->name) == 0)
295 sstrncpy (vl.type, "counter", sizeof (vl.type));
296 sstrncpy (vl.type_instance, "journal-bytes", sizeof (vl.type_instance));
298 else if (strcasecmp ("JournalRotate", ptr->name) == 0)
300 sstrncpy (vl.type, "counter", sizeof (vl.type));
301 sstrncpy (vl.type_instance, "journal-rotates", sizeof (vl.type_instance));
303 else if (strcasecmp ("UpdatesReceived", ptr->name) == 0)
305 sstrncpy (vl.type, "operations", sizeof (vl.type));
306 sstrncpy (vl.type_instance, "receive-update", sizeof (vl.type_instance));
310 DEBUG ("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
314 plugin_dispatch_values (&vl);
315 } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
317 rrdc_stats_free (head);
322 static int rc_init (void)
324 if (config_collect_stats != 0)
325 plugin_register_read ("rrdcached", rc_read);
330 static int rc_write (const data_set_t *ds, const value_list_t *vl,
331 user_data_t __attribute__((unused)) *user_data)
335 char *values_array[2];
338 if (daemon_address == NULL)
340 ERROR ("rrdcached plugin: daemon_address == NULL.");
341 plugin_unregister_write ("rrdcached");
345 if (strcmp (ds->type, vl->type) != 0)
347 ERROR ("rrdcached plugin: DS type does not match value list type");
351 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
353 ERROR ("rrdcached plugin: value_list_to_filename failed.");
357 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
359 ERROR ("rrdcached plugin: value_list_to_string failed.");
363 values_array[0] = values;
364 values_array[1] = NULL;
366 if (config_create_files != 0)
370 status = stat (filename, &statbuf);
376 ERROR ("rrdcached plugin: stat (%s) failed: %s",
377 filename, sstrerror (errno, errbuf, sizeof (errbuf)));
381 status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
384 ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
391 status = rrdc_connect (daemon_address);
394 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
395 daemon_address, status);
399 status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
402 ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
404 filename, values_array[0], status);
411 static int rc_shutdown (void)
415 } /* int rc_shutdown */
417 void module_register (void)
419 plugin_register_config ("rrdcached", rc_config,
420 config_keys, config_keys_num);
421 plugin_register_init ("rrdcached", rc_init);
422 plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
423 plugin_register_shutdown ("rrdcached", rc_shutdown);
424 } /* void module_register */
427 * vim: set sw=2 sts=2 et :