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[] =
38 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
40 static char *datadir = NULL;
41 static char *daemon_address = NULL;
42 static int config_create_files = 1;
43 static rrdcreate_config_t rrdcreate_config =
50 /* timespans = */ NULL,
51 /* timespans_num = */ 0,
53 /* consolidation_functions = */ NULL,
54 /* consolidation_functions_num = */ 0
57 static int value_list_to_string (char *buffer, int buffer_len,
58 const data_set_t *ds, const value_list_t *vl)
64 assert (0 == strcmp (ds->type, vl->type));
66 memset (buffer, '\0', buffer_len);
68 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
69 if ((status < 1) || (status >= buffer_len))
73 for (i = 0; i < ds->ds_num; i++)
75 if ((ds->ds[i].type != DS_TYPE_COUNTER)
76 && (ds->ds[i].type != DS_TYPE_GAUGE))
79 if (ds->ds[i].type == DS_TYPE_COUNTER)
81 status = ssnprintf (buffer + offset, buffer_len - offset,
82 ",%llu", vl->values[i].counter);
84 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
86 status = ssnprintf (buffer + offset, buffer_len - offset,
87 ",%lf", vl->values[i].gauge);
90 if ((status < 1) || (status >= (buffer_len - offset)))
94 } /* for ds->ds_num */
97 } /* int value_list_to_string */
99 static int value_list_to_filename (char *buffer, int buffer_len,
100 const data_set_t *ds, const value_list_t *vl)
105 assert (0 == strcmp (ds->type, vl->type));
109 status = ssnprintf (buffer + offset, buffer_len - offset,
111 if ((status < 1) || (status >= buffer_len - offset))
116 status = ssnprintf (buffer + offset, buffer_len - offset,
118 if ((status < 1) || (status >= buffer_len - offset))
122 if (strlen (vl->plugin_instance) > 0)
123 status = ssnprintf (buffer + offset, buffer_len - offset,
124 "%s-%s/", vl->plugin, vl->plugin_instance);
126 status = ssnprintf (buffer + offset, buffer_len - offset,
128 if ((status < 1) || (status >= buffer_len - offset))
132 if (strlen (vl->type_instance) > 0)
133 status = ssnprintf (buffer + offset, buffer_len - offset,
134 "%s-%s", vl->type, vl->type_instance);
136 status = ssnprintf (buffer + offset, buffer_len - offset,
138 if ((status < 1) || (status >= buffer_len - offset))
142 strncpy (buffer + offset, ".rrd", buffer_len - offset);
143 buffer[buffer_len - 1] = 0;
146 } /* int value_list_to_filename */
148 static int rc_config (const char *key, const char *value)
150 if (strcasecmp ("DataDir", key) == 0)
154 datadir = strdup (value);
157 int len = strlen (datadir);
158 while ((len > 0) && (datadir[len - 1] == '/'))
170 else if (strcasecmp ("DaemonAddress", key) == 0)
172 sfree (daemon_address);
173 daemon_address = strdup (value);
174 if (daemon_address == NULL)
176 ERROR ("rrdcached plugin: strdup failed.");
180 else if (strcasecmp ("CreateFiles", key) == 0)
182 if ((strcasecmp ("true", value) == 0)
183 || (strcasecmp ("yes", value) == 0)
184 || (strcasecmp ("on", value) == 0))
185 config_create_files = 1;
187 config_create_files = 0;
194 } /* int rc_config */
196 static int rc_write (const data_set_t *ds, const value_list_t *vl)
200 char *values_array[2];
203 if (daemon_address == NULL)
205 ERROR ("rrdcached plugin: daemon_address == NULL.");
206 plugin_unregister_write ("rrdcached");
210 if (strcmp (ds->type, vl->type) != 0)
212 ERROR ("rrdcached plugin: DS type does not match value list type");
216 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
218 ERROR ("rrdcached plugin: value_list_to_filename failed.");
222 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
224 ERROR ("rrdcached plugin: value_list_to_string failed.");
228 values_array[0] = values;
229 values_array[1] = NULL;
231 if (config_create_files != 0)
235 status = stat (filename, &statbuf);
241 ERROR ("rrdcached plugin: stat (%s) failed: %s",
242 filename, sstrerror (errno, errbuf, sizeof (errbuf)));
246 status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
249 ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
256 status = rrdc_connect (daemon_address);
259 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
260 daemon_address, status);
264 status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
267 ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
269 filename, values_array[0], status);
276 static int rc_shutdown (void)
280 } /* int rc_shutdown */
282 void module_register (void)
284 plugin_register_config ("rrdcached", rc_config,
285 config_keys, config_keys_num);
286 plugin_register_write ("rrdcached", rc_write);
287 plugin_register_shutdown ("rrdcached", rc_shutdown);
288 } /* void module_register */
291 * vim: set sw=2 sts=2 et :