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>
26 #include <rrd_client.h>
31 static const char *config_keys[] =
36 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
38 static char *datadir = NULL;
39 static char *daemon_address = NULL;
41 static int value_list_to_string (char *buffer, int buffer_len,
42 const data_set_t *ds, const value_list_t *vl)
48 assert (0 == strcmp (ds->type, vl->type));
50 memset (buffer, '\0', buffer_len);
52 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
53 if ((status < 1) || (status >= buffer_len))
57 for (i = 0; i < ds->ds_num; i++)
59 if ((ds->ds[i].type != DS_TYPE_COUNTER)
60 && (ds->ds[i].type != DS_TYPE_GAUGE))
63 if (ds->ds[i].type == DS_TYPE_COUNTER)
65 status = ssnprintf (buffer + offset, buffer_len - offset,
66 ",%llu", vl->values[i].counter);
68 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
70 status = ssnprintf (buffer + offset, buffer_len - offset,
71 ",%lf", vl->values[i].gauge);
74 if ((status < 1) || (status >= (buffer_len - offset)))
78 } /* for ds->ds_num */
81 } /* int value_list_to_string */
83 static int value_list_to_filename (char *buffer, int buffer_len,
84 const data_set_t *ds, const value_list_t *vl)
89 assert (0 == strcmp (ds->type, vl->type));
93 status = ssnprintf (buffer + offset, buffer_len - offset,
95 if ((status < 1) || (status >= buffer_len - offset))
100 status = ssnprintf (buffer + offset, buffer_len - offset,
102 if ((status < 1) || (status >= buffer_len - offset))
106 if (strlen (vl->plugin_instance) > 0)
107 status = ssnprintf (buffer + offset, buffer_len - offset,
108 "%s-%s/", vl->plugin, vl->plugin_instance);
110 status = ssnprintf (buffer + offset, buffer_len - offset,
112 if ((status < 1) || (status >= buffer_len - offset))
116 if (strlen (vl->type_instance) > 0)
117 status = ssnprintf (buffer + offset, buffer_len - offset,
118 "%s-%s", vl->type, vl->type_instance);
120 status = ssnprintf (buffer + offset, buffer_len - offset,
122 if ((status < 1) || (status >= buffer_len - offset))
130 /* TODO: Find a way to minimize the calls to `localtime_r',
131 * since they are pretty expensive.. */
133 if (localtime_r (&now, &stm) == NULL)
135 ERROR ("rrdcached plugin: localtime_r failed");
139 strftime (buffer + offset, buffer_len - offset,
144 } /* int value_list_to_filename */
146 static int rc_config (const char *key, const char *value)
148 if (strcasecmp ("DataDir", key) == 0)
152 datadir = strdup (value);
155 int len = strlen (datadir);
156 while ((len > 0) && (datadir[len - 1] == '/'))
168 else if (strcasecmp ("DaemonAddress", key) == 0)
170 sfree (daemon_address);
171 daemon_address = strdup (value);
172 if (daemon_address == NULL)
174 ERROR ("rrdcached plugin: strdup failed.");
183 } /* int rc_config */
185 static int rc_write (const data_set_t *ds, const value_list_t *vl)
189 char *values_array[2];
192 if (daemon_address == NULL)
194 ERROR ("rrdcached plugin: daemon_address == NULL.");
195 plugin_unregister_write ("rrdcached");
199 if (strcmp (ds->type, vl->type) != 0)
201 ERROR ("rrdcached plugin: DS type does not match value list type");
205 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
207 ERROR ("rrdcached plugin: value_list_to_filename failed.");
211 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
213 ERROR ("rrdcached plugin: value_list_to_string failed.");
217 values_array[0] = values;
218 values_array[1] = NULL;
220 /* TODO: Check if the file exists. */
222 status = rrdc_connect (daemon_address);
225 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
226 daemon_address, status);
230 status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
233 ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
235 filename, values_array[0], status);
242 static int rc_shutdown (void)
246 } /* int rc_shutdown */
248 void module_register (void)
250 plugin_register_config ("rrdcached", rc_config,
251 config_keys, config_keys_num);
252 plugin_register_write ("rrdcached", rc_write);
253 plugin_register_shutdown ("rrdcached", rc_shutdown);
254 } /* void module_register */
257 * vim: set sw=2 sts=2 et :