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))
126 strncpy (buffer + offset, ".rrd", buffer_len - offset);
127 buffer[buffer_len - 1] = 0;
130 } /* int value_list_to_filename */
132 static int rc_config (const char *key, const char *value)
134 if (strcasecmp ("DataDir", key) == 0)
138 datadir = strdup (value);
141 int len = strlen (datadir);
142 while ((len > 0) && (datadir[len - 1] == '/'))
154 else if (strcasecmp ("DaemonAddress", key) == 0)
156 sfree (daemon_address);
157 daemon_address = strdup (value);
158 if (daemon_address == NULL)
160 ERROR ("rrdcached plugin: strdup failed.");
169 } /* int rc_config */
171 static int rc_write (const data_set_t *ds, const value_list_t *vl)
175 char *values_array[2];
178 if (daemon_address == NULL)
180 ERROR ("rrdcached plugin: daemon_address == NULL.");
181 plugin_unregister_write ("rrdcached");
185 if (strcmp (ds->type, vl->type) != 0)
187 ERROR ("rrdcached plugin: DS type does not match value list type");
191 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
193 ERROR ("rrdcached plugin: value_list_to_filename failed.");
197 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
199 ERROR ("rrdcached plugin: value_list_to_string failed.");
203 values_array[0] = values;
204 values_array[1] = NULL;
206 /* TODO: Check if the file exists. */
208 status = rrdc_connect (daemon_address);
211 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
212 daemon_address, status);
216 status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
219 ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
221 filename, values_array[0], status);
228 static int rc_shutdown (void)
232 } /* int rc_shutdown */
234 void module_register (void)
236 plugin_register_config ("rrdcached", rc_config,
237 config_keys, config_keys_num);
238 plugin_register_write ("rrdcached", rc_write);
239 plugin_register_shutdown ("rrdcached", rc_shutdown);
240 } /* void module_register */
243 * vim: set sw=2 sts=2 et :