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))
81 if (ds->ds[i].type == DS_TYPE_COUNTER)
83 status = ssnprintf (buffer + offset, buffer_len - offset,
84 ":%llu", vl->values[i].counter);
86 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
88 status = ssnprintf (buffer + offset, buffer_len - offset,
89 ":%lf", vl->values[i].gauge);
92 if ((status < 1) || (status >= (buffer_len - offset)))
96 } /* for ds->ds_num */
99 } /* int value_list_to_string */
101 static int value_list_to_filename (char *buffer, int buffer_len,
102 const data_set_t *ds, const value_list_t *vl)
107 assert (0 == strcmp (ds->type, vl->type));
111 status = ssnprintf (buffer + offset, buffer_len - offset,
113 if ((status < 1) || (status >= buffer_len - offset))
118 status = ssnprintf (buffer + offset, buffer_len - offset,
120 if ((status < 1) || (status >= buffer_len - offset))
124 if (strlen (vl->plugin_instance) > 0)
125 status = ssnprintf (buffer + offset, buffer_len - offset,
126 "%s-%s/", vl->plugin, vl->plugin_instance);
128 status = ssnprintf (buffer + offset, buffer_len - offset,
130 if ((status < 1) || (status >= buffer_len - offset))
134 if (strlen (vl->type_instance) > 0)
135 status = ssnprintf (buffer + offset, buffer_len - offset,
136 "%s-%s", vl->type, vl->type_instance);
138 status = ssnprintf (buffer + offset, buffer_len - offset,
140 if ((status < 1) || (status >= buffer_len - offset))
144 strncpy (buffer + offset, ".rrd", buffer_len - offset);
145 buffer[buffer_len - 1] = 0;
148 } /* int value_list_to_filename */
150 static int rc_config (const char *key, const char *value)
152 if (strcasecmp ("DataDir", key) == 0)
156 datadir = strdup (value);
159 int len = strlen (datadir);
160 while ((len > 0) && (datadir[len - 1] == '/'))
172 else if (strcasecmp ("DaemonAddress", key) == 0)
174 sfree (daemon_address);
175 daemon_address = strdup (value);
176 if (daemon_address == NULL)
178 ERROR ("rrdcached plugin: strdup failed.");
182 else if (strcasecmp ("CreateFiles", key) == 0)
184 if ((strcasecmp ("false", value) == 0)
185 || (strcasecmp ("no", value) == 0)
186 || (strcasecmp ("off", value) == 0))
187 config_create_files = 0;
189 config_create_files = 1;
191 else if (strcasecmp ("CollectStatistics", key) == 0)
193 if ((strcasecmp ("false", value) == 0)
194 || (strcasecmp ("no", value) == 0)
195 || (strcasecmp ("off", value) == 0))
196 config_collect_stats = 0;
198 config_collect_stats = 1;
205 } /* int rc_config */
207 static int rc_read (void)
214 value_list_t vl = VALUE_LIST_INIT;
216 if (daemon_address == NULL)
219 if (config_collect_stats == 0)
225 if ((strncmp ("unix:", daemon_address, strlen ("unix:")) == 0)
226 || (daemon_address[0] == '/'))
227 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
229 sstrncpy (vl.host, daemon_address, sizeof (vl.host));
230 sstrncpy (vl.plugin, "rrdcached", sizeof (vl.plugin));
233 status = rrdc_stats_get (&head);
236 ERROR ("rrdcached plugin: rrdc_stats_get failed with status %i.", status);
240 for (ptr = head; ptr != NULL; ptr = ptr->next)
242 if (ptr->type == RRDC_STATS_TYPE_GAUGE)
243 values[0].gauge = (gauge_t) ptr->value.gauge;
244 else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
245 values[0].counter = (counter_t) ptr->value.counter;
249 if (strcasecmp ("QueueLength", ptr->name) == 0)
251 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
252 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
254 else if (strcasecmp ("UpdatesWritten", ptr->name) == 0)
256 sstrncpy (vl.type, "operations", sizeof (vl.type));
257 sstrncpy (vl.type_instance, "write-updates", sizeof (vl.type_instance));
259 else if (strcasecmp ("DataSetsWritten", ptr->name) == 0)
261 sstrncpy (vl.type, "operations", sizeof (vl.type));
262 sstrncpy (vl.type_instance, "write-data_sets",
263 sizeof (vl.type_instance));
265 else if (strcasecmp ("TreeNodesNumber", ptr->name) == 0)
267 sstrncpy (vl.type, "gauge", sizeof (vl.type));
268 sstrncpy (vl.type_instance, "tree_nodes", sizeof (vl.type_instance));
270 else if (strcasecmp ("TreeDepth", ptr->name) == 0)
272 sstrncpy (vl.type, "gauge", sizeof (vl.type));
273 sstrncpy (vl.type_instance, "tree_depth", sizeof (vl.type_instance));
275 else if (strcasecmp ("FlushesReceived", ptr->name) == 0)
277 sstrncpy (vl.type, "operations", sizeof (vl.type));
278 sstrncpy (vl.type_instance, "receive-flush", sizeof (vl.type_instance));
280 else if (strcasecmp ("JournalBytes", ptr->name) == 0)
282 sstrncpy (vl.type, "counter", sizeof (vl.type));
283 sstrncpy (vl.type_instance, "journal-bytes", sizeof (vl.type_instance));
285 else if (strcasecmp ("JournalRotate", ptr->name) == 0)
287 sstrncpy (vl.type, "counter", sizeof (vl.type));
288 sstrncpy (vl.type_instance, "journal-rotates", sizeof (vl.type_instance));
290 else if (strcasecmp ("UpdatesReceived", ptr->name) == 0)
292 sstrncpy (vl.type, "operations", sizeof (vl.type));
293 sstrncpy (vl.type_instance, "receive-update", sizeof (vl.type_instance));
297 DEBUG ("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
301 plugin_dispatch_values (&vl);
302 } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
304 rrdc_stats_free (head);
309 static int rc_init (void)
311 if (config_collect_stats != 0)
312 plugin_register_read ("rrdcached", rc_read);
317 static int rc_write (const data_set_t *ds, const value_list_t *vl,
318 user_data_t __attribute__((unused)) *user_data)
322 char *values_array[2];
325 if (daemon_address == NULL)
327 ERROR ("rrdcached plugin: daemon_address == NULL.");
328 plugin_unregister_write ("rrdcached");
332 if (strcmp (ds->type, vl->type) != 0)
334 ERROR ("rrdcached plugin: DS type does not match value list type");
338 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
340 ERROR ("rrdcached plugin: value_list_to_filename failed.");
344 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
346 ERROR ("rrdcached plugin: value_list_to_string failed.");
350 values_array[0] = values;
351 values_array[1] = NULL;
353 if (config_create_files != 0)
357 status = stat (filename, &statbuf);
363 ERROR ("rrdcached plugin: stat (%s) failed: %s",
364 filename, sstrerror (errno, errbuf, sizeof (errbuf)));
368 status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
371 ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
378 status = rrdc_connect (daemon_address);
381 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
382 daemon_address, status);
386 status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
389 ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
391 filename, values_array[0], status);
398 static int rc_shutdown (void)
402 } /* int rc_shutdown */
404 void module_register (void)
406 plugin_register_config ("rrdcached", rc_config,
407 config_keys, config_keys_num);
408 plugin_register_init ("rrdcached", rc_init);
409 plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
410 plugin_register_shutdown ("rrdcached", rc_shutdown);
411 } /* void module_register */
414 * vim: set sw=2 sts=2 et :