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"
27 #include <rrd_client.h>
32 static const char *config_keys[] =
39 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
41 static char *datadir = NULL;
42 static char *daemon_address = NULL;
43 static int config_create_files = 1;
44 static int config_collect_stats = 1;
45 static rrdcreate_config_t rrdcreate_config =
52 /* timespans = */ NULL,
53 /* timespans_num = */ 0,
55 /* consolidation_functions = */ NULL,
56 /* consolidation_functions_num = */ 0
59 static int value_list_to_string (char *buffer, int buffer_len,
60 const data_set_t *ds, const value_list_t *vl)
66 assert (0 == strcmp (ds->type, vl->type));
68 memset (buffer, '\0', buffer_len);
70 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
71 if ((status < 1) || (status >= buffer_len))
75 for (i = 0; i < ds->ds_num; i++)
77 if ((ds->ds[i].type != DS_TYPE_COUNTER)
78 && (ds->ds[i].type != DS_TYPE_GAUGE)
79 && (ds->ds[i].type != DS_TYPE_DERIVE)
80 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
83 if (ds->ds[i].type == DS_TYPE_COUNTER)
85 status = ssnprintf (buffer + offset, buffer_len - offset,
86 ":%llu", vl->values[i].counter);
88 else if (ds->ds[i].type == DS_TYPE_GAUGE)
90 status = ssnprintf (buffer + offset, buffer_len - offset,
91 ":%f", vl->values[i].gauge);
93 else if (ds->ds[i].type == DS_TYPE_DERIVE) {
94 status = ssnprintf (buffer + offset, buffer_len - offset,
95 ":%"PRIi64, vl->values[i].derive);
97 else /* if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */ {
98 status = ssnprintf (buffer + offset, buffer_len - offset,
99 ":%"PRIu64, vl->values[i].absolute);
103 if ((status < 1) || (status >= (buffer_len - offset)))
107 } /* for ds->ds_num */
110 } /* int value_list_to_string */
112 static int value_list_to_filename (char *buffer, int buffer_len,
113 const data_set_t *ds, const value_list_t *vl)
118 assert (0 == strcmp (ds->type, vl->type));
122 status = ssnprintf (buffer + offset, buffer_len - offset,
124 if ((status < 1) || (status >= buffer_len - offset))
129 status = ssnprintf (buffer + offset, buffer_len - offset,
131 if ((status < 1) || (status >= buffer_len - offset))
135 if (strlen (vl->plugin_instance) > 0)
136 status = ssnprintf (buffer + offset, buffer_len - offset,
137 "%s-%s/", vl->plugin, vl->plugin_instance);
139 status = ssnprintf (buffer + offset, buffer_len - offset,
141 if ((status < 1) || (status >= buffer_len - offset))
145 if (strlen (vl->type_instance) > 0)
146 status = ssnprintf (buffer + offset, buffer_len - offset,
147 "%s-%s", vl->type, vl->type_instance);
149 status = ssnprintf (buffer + offset, buffer_len - offset,
151 if ((status < 1) || (status >= buffer_len - offset))
155 strncpy (buffer + offset, ".rrd", buffer_len - offset);
156 buffer[buffer_len - 1] = 0;
159 } /* int value_list_to_filename */
161 static int rc_config (const char *key, const char *value)
163 if (strcasecmp ("DataDir", key) == 0)
167 datadir = strdup (value);
170 int len = strlen (datadir);
171 while ((len > 0) && (datadir[len - 1] == '/'))
183 else if (strcasecmp ("DaemonAddress", key) == 0)
185 sfree (daemon_address);
186 daemon_address = strdup (value);
187 if (daemon_address == NULL)
189 ERROR ("rrdcached plugin: strdup failed.");
193 else if (strcasecmp ("CreateFiles", key) == 0)
195 if ((strcasecmp ("false", value) == 0)
196 || (strcasecmp ("no", value) == 0)
197 || (strcasecmp ("off", value) == 0))
198 config_create_files = 0;
200 config_create_files = 1;
202 else if (strcasecmp ("CollectStatistics", key) == 0)
204 if ((strcasecmp ("false", value) == 0)
205 || (strcasecmp ("no", value) == 0)
206 || (strcasecmp ("off", value) == 0))
207 config_collect_stats = 0;
209 config_collect_stats = 1;
216 } /* int rc_config */
218 static int rc_read (void)
225 value_list_t vl = VALUE_LIST_INIT;
227 if (daemon_address == NULL)
230 if (config_collect_stats == 0)
236 if ((strncmp ("unix:", daemon_address, strlen ("unix:")) == 0)
237 || (daemon_address[0] == '/'))
238 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
240 sstrncpy (vl.host, daemon_address, sizeof (vl.host));
241 sstrncpy (vl.plugin, "rrdcached", sizeof (vl.plugin));
244 status = rrdc_stats_get (&head);
247 ERROR ("rrdcached plugin: rrdc_stats_get failed with status %i.", status);
251 for (ptr = head; ptr != NULL; ptr = ptr->next)
253 if (ptr->type == RRDC_STATS_TYPE_GAUGE)
254 values[0].gauge = (gauge_t) ptr->value.gauge;
255 else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
256 values[0].counter = (counter_t) ptr->value.counter;
260 if (strcasecmp ("QueueLength", ptr->name) == 0)
262 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
263 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
265 else if (strcasecmp ("UpdatesWritten", ptr->name) == 0)
267 sstrncpy (vl.type, "operations", sizeof (vl.type));
268 sstrncpy (vl.type_instance, "write-updates", sizeof (vl.type_instance));
270 else if (strcasecmp ("DataSetsWritten", ptr->name) == 0)
272 sstrncpy (vl.type, "operations", sizeof (vl.type));
273 sstrncpy (vl.type_instance, "write-data_sets",
274 sizeof (vl.type_instance));
276 else if (strcasecmp ("TreeNodesNumber", ptr->name) == 0)
278 sstrncpy (vl.type, "gauge", sizeof (vl.type));
279 sstrncpy (vl.type_instance, "tree_nodes", sizeof (vl.type_instance));
281 else if (strcasecmp ("TreeDepth", ptr->name) == 0)
283 sstrncpy (vl.type, "gauge", sizeof (vl.type));
284 sstrncpy (vl.type_instance, "tree_depth", sizeof (vl.type_instance));
286 else if (strcasecmp ("FlushesReceived", ptr->name) == 0)
288 sstrncpy (vl.type, "operations", sizeof (vl.type));
289 sstrncpy (vl.type_instance, "receive-flush", sizeof (vl.type_instance));
291 else if (strcasecmp ("JournalBytes", ptr->name) == 0)
293 sstrncpy (vl.type, "counter", sizeof (vl.type));
294 sstrncpy (vl.type_instance, "journal-bytes", sizeof (vl.type_instance));
296 else if (strcasecmp ("JournalRotate", ptr->name) == 0)
298 sstrncpy (vl.type, "counter", sizeof (vl.type));
299 sstrncpy (vl.type_instance, "journal-rotates", sizeof (vl.type_instance));
301 else if (strcasecmp ("UpdatesReceived", ptr->name) == 0)
303 sstrncpy (vl.type, "operations", sizeof (vl.type));
304 sstrncpy (vl.type_instance, "receive-update", sizeof (vl.type_instance));
308 DEBUG ("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
312 plugin_dispatch_values (&vl);
313 } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
315 rrdc_stats_free (head);
320 static int rc_init (void)
322 if (config_collect_stats != 0)
323 plugin_register_read ("rrdcached", rc_read);
328 static int rc_write (const data_set_t *ds, const value_list_t *vl,
329 user_data_t __attribute__((unused)) *user_data)
333 char *values_array[2];
336 if (daemon_address == NULL)
338 ERROR ("rrdcached plugin: daemon_address == NULL.");
339 plugin_unregister_write ("rrdcached");
343 if (strcmp (ds->type, vl->type) != 0)
345 ERROR ("rrdcached plugin: DS type does not match value list type");
349 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
351 ERROR ("rrdcached plugin: value_list_to_filename failed.");
355 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
357 ERROR ("rrdcached plugin: value_list_to_string failed.");
361 values_array[0] = values;
362 values_array[1] = NULL;
364 if (config_create_files != 0)
368 status = stat (filename, &statbuf);
374 ERROR ("rrdcached plugin: stat (%s) failed: %s",
375 filename, sstrerror (errno, errbuf, sizeof (errbuf)));
379 status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
382 ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
389 status = rrdc_connect (daemon_address);
392 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
393 daemon_address, status);
397 status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
400 ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
402 filename, values_array[0], status);
409 static int rc_shutdown (void)
413 } /* int rc_shutdown */
415 void module_register (void)
417 plugin_register_config ("rrdcached", rc_config,
418 config_keys, config_keys_num);
419 plugin_register_init ("rrdcached", rc_init);
420 plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
421 plugin_register_shutdown ("rrdcached", rc_shutdown);
422 } /* void module_register */
425 * vim: set sw=2 sts=2 et :