fef78ce42cad94cc8d640b5df6947d330d84a4fd
1 /**
2 * collectd - src/rrdcached.c
3 * Copyright (C) 2008 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
26 #include <rrd_client.h>
28 /*
29 * Private variables
30 */
31 static const char *config_keys[] =
32 {
33 "DaemonAddress",
34 "DataDir"
35 };
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)
43 {
44 int offset;
45 int status;
46 int i;
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))
54 return (-1);
55 offset = status;
57 for (i = 0; i < ds->ds_num; i++)
58 {
59 if ((ds->ds[i].type != DS_TYPE_COUNTER)
60 && (ds->ds[i].type != DS_TYPE_GAUGE))
61 return (-1);
63 if (ds->ds[i].type == DS_TYPE_COUNTER)
64 {
65 status = ssnprintf (buffer + offset, buffer_len - offset,
66 ",%llu", vl->values[i].counter);
67 }
68 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
69 {
70 status = ssnprintf (buffer + offset, buffer_len - offset,
71 ",%lf", vl->values[i].gauge);
72 }
74 if ((status < 1) || (status >= (buffer_len - offset)))
75 return (-1);
77 offset += status;
78 } /* for ds->ds_num */
80 return (0);
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)
85 {
86 int offset = 0;
87 int status;
89 assert (0 == strcmp (ds->type, vl->type));
91 if (datadir != NULL)
92 {
93 status = ssnprintf (buffer + offset, buffer_len - offset,
94 "%s/", datadir);
95 if ((status < 1) || (status >= buffer_len - offset))
96 return (-1);
97 offset += status;
98 }
100 status = ssnprintf (buffer + offset, buffer_len - offset,
101 "%s/", vl->host);
102 if ((status < 1) || (status >= buffer_len - offset))
103 return (-1);
104 offset += status;
106 if (strlen (vl->plugin_instance) > 0)
107 status = ssnprintf (buffer + offset, buffer_len - offset,
108 "%s-%s/", vl->plugin, vl->plugin_instance);
109 else
110 status = ssnprintf (buffer + offset, buffer_len - offset,
111 "%s/", vl->plugin);
112 if ((status < 1) || (status >= buffer_len - offset))
113 return (-1);
114 offset += status;
116 if (strlen (vl->type_instance) > 0)
117 status = ssnprintf (buffer + offset, buffer_len - offset,
118 "%s-%s", vl->type, vl->type_instance);
119 else
120 status = ssnprintf (buffer + offset, buffer_len - offset,
121 "%s", vl->type);
122 if ((status < 1) || (status >= buffer_len - offset))
123 return (-1);
124 offset += status;
126 strncpy (buffer + offset, ".rrd", buffer_len - offset);
127 buffer[buffer_len - 1] = 0;
129 return (0);
130 } /* int value_list_to_filename */
132 static int rc_config (const char *key, const char *value)
133 {
134 if (strcasecmp ("DataDir", key) == 0)
135 {
136 if (datadir != NULL)
137 free (datadir);
138 datadir = strdup (value);
139 if (datadir != NULL)
140 {
141 int len = strlen (datadir);
142 while ((len > 0) && (datadir[len - 1] == '/'))
143 {
144 len--;
145 datadir[len] = '\0';
146 }
147 if (len <= 0)
148 {
149 free (datadir);
150 datadir = NULL;
151 }
152 }
153 }
154 else if (strcasecmp ("DaemonAddress", key) == 0)
155 {
156 sfree (daemon_address);
157 daemon_address = strdup (value);
158 if (daemon_address == NULL)
159 {
160 ERROR ("rrdcached plugin: strdup failed.");
161 return (1);
162 }
163 }
164 else
165 {
166 return (-1);
167 }
168 return (0);
169 } /* int rc_config */
171 static int rc_write (const data_set_t *ds, const value_list_t *vl)
172 {
173 char filename[512];
174 char values[512];
175 char *values_array[2];
176 int status;
178 if (daemon_address == NULL)
179 {
180 ERROR ("rrdcached plugin: daemon_address == NULL.");
181 plugin_unregister_write ("rrdcached");
182 return (-1);
183 }
185 if (strcmp (ds->type, vl->type) != 0)
186 {
187 ERROR ("rrdcached plugin: DS type does not match value list type");
188 return (-1);
189 }
191 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
192 {
193 ERROR ("rrdcached plugin: value_list_to_filename failed.");
194 return (-1);
195 }
197 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
198 {
199 ERROR ("rrdcached plugin: value_list_to_string failed.");
200 return (-1);
201 }
203 values_array[0] = values;
204 values_array[1] = NULL;
206 /* TODO: Check if the file exists. */
208 status = rrdc_connect (daemon_address);
209 if (status != 0)
210 {
211 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
212 daemon_address, status);
213 return (-1);
214 }
216 status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
217 if (status != 0)
218 {
219 ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
220 "status %i.",
221 filename, values_array[0], status);
222 return (-1);
223 }
225 return (0);
226 } /* int rc_write */
228 static int rc_shutdown (void)
229 {
230 rrdc_disconnect ();
231 return (0);
232 } /* int rc_shutdown */
234 void module_register (void)
235 {
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 */
242 /*
243 * vim: set sw=2 sts=2 et :
244 */