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"
25 #include "utils_rrdcreate.h"
27 #undef HAVE_CONFIG_H
28 #include <rrd.h>
29 #include <rrd_client.h>
31 /*
32 * Private variables
33 */
34 static const char *config_keys[] =
35 {
36 "DaemonAddress",
37 "DataDir",
38 "CreateFiles",
39 "CollectStatistics"
40 };
41 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
43 static char *datadir = NULL;
44 static char *daemon_address = NULL;
45 static int config_create_files = 1;
46 static int config_collect_stats = 1;
47 static rrdcreate_config_t rrdcreate_config =
48 {
49 /* stepsize = */ 0,
50 /* heartbeat = */ 0,
51 /* rrarows = */ 1200,
52 /* xff = */ 0.1,
54 /* timespans = */ NULL,
55 /* timespans_num = */ 0,
57 /* consolidation_functions = */ NULL,
58 /* consolidation_functions_num = */ 0
59 };
61 static int value_list_to_string (char *buffer, int buffer_len,
62 const data_set_t *ds, const value_list_t *vl)
63 {
64 int offset;
65 int status;
66 int i;
68 assert (0 == strcmp (ds->type, vl->type));
70 memset (buffer, '\0', buffer_len);
72 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
73 if ((status < 1) || (status >= buffer_len))
74 return (-1);
75 offset = status;
77 for (i = 0; i < ds->ds_num; i++)
78 {
79 if ((ds->ds[i].type != DS_TYPE_COUNTER)
80 && (ds->ds[i].type != DS_TYPE_GAUGE)
81 && (ds->ds[i].type != DS_TYPE_DERIVE)
82 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
83 return (-1);
85 if (ds->ds[i].type == DS_TYPE_COUNTER)
86 {
87 status = ssnprintf (buffer + offset, buffer_len - offset,
88 ":%llu", vl->values[i].counter);
89 }
90 else if (ds->ds[i].type == DS_TYPE_GAUGE)
91 {
92 status = ssnprintf (buffer + offset, buffer_len - offset,
93 ":%f", vl->values[i].gauge);
94 }
95 else if (ds->ds[i].type == DS_TYPE_DERIVE) {
96 status = ssnprintf (buffer + offset, buffer_len - offset,
97 ":%"PRIi64, vl->values[i].derive);
98 }
99 else /* if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */ {
100 status = ssnprintf (buffer + offset, buffer_len - offset,
101 ":%"PRIu64, vl->values[i].absolute);
103 }
105 if ((status < 1) || (status >= (buffer_len - offset)))
106 return (-1);
108 offset += status;
109 } /* for ds->ds_num */
111 return (0);
112 } /* int value_list_to_string */
114 static int value_list_to_filename (char *buffer, int buffer_len,
115 const data_set_t *ds, const value_list_t *vl)
116 {
117 int offset = 0;
118 int status;
120 assert (0 == strcmp (ds->type, vl->type));
122 if (datadir != NULL)
123 {
124 status = ssnprintf (buffer + offset, buffer_len - offset,
125 "%s/", datadir);
126 if ((status < 1) || (status >= buffer_len - offset))
127 return (-1);
128 offset += status;
129 }
131 status = ssnprintf (buffer + offset, buffer_len - offset,
132 "%s/", vl->host);
133 if ((status < 1) || (status >= buffer_len - offset))
134 return (-1);
135 offset += status;
137 if (strlen (vl->plugin_instance) > 0)
138 status = ssnprintf (buffer + offset, buffer_len - offset,
139 "%s-%s/", vl->plugin, vl->plugin_instance);
140 else
141 status = ssnprintf (buffer + offset, buffer_len - offset,
142 "%s/", vl->plugin);
143 if ((status < 1) || (status >= buffer_len - offset))
144 return (-1);
145 offset += status;
147 if (strlen (vl->type_instance) > 0)
148 status = ssnprintf (buffer + offset, buffer_len - offset,
149 "%s-%s", vl->type, vl->type_instance);
150 else
151 status = ssnprintf (buffer + offset, buffer_len - offset,
152 "%s", vl->type);
153 if ((status < 1) || (status >= buffer_len - offset))
154 return (-1);
155 offset += status;
157 strncpy (buffer + offset, ".rrd", buffer_len - offset);
158 buffer[buffer_len - 1] = 0;
160 return (0);
161 } /* int value_list_to_filename */
163 static int rc_config (const char *key, const char *value)
164 {
165 if (strcasecmp ("DataDir", key) == 0)
166 {
167 if (datadir != NULL)
168 free (datadir);
169 datadir = strdup (value);
170 if (datadir != NULL)
171 {
172 int len = strlen (datadir);
173 while ((len > 0) && (datadir[len - 1] == '/'))
174 {
175 len--;
176 datadir[len] = '\0';
177 }
178 if (len <= 0)
179 {
180 free (datadir);
181 datadir = NULL;
182 }
183 }
184 }
185 else if (strcasecmp ("DaemonAddress", key) == 0)
186 {
187 sfree (daemon_address);
188 daemon_address = strdup (value);
189 if (daemon_address == NULL)
190 {
191 ERROR ("rrdcached plugin: strdup failed.");
192 return (1);
193 }
194 }
195 else if (strcasecmp ("CreateFiles", key) == 0)
196 {
197 if (IS_FALSE (value))
198 config_create_files = 0;
199 else
200 config_create_files = 1;
201 }
202 else if (strcasecmp ("CollectStatistics", key) == 0)
203 {
204 if (IS_FALSE (value))
205 config_collect_stats = 0;
206 else
207 config_collect_stats = 1;
208 }
209 else
210 {
211 return (-1);
212 }
213 return (0);
214 } /* int rc_config */
216 static int rc_read (void)
217 {
218 int status;
219 rrdc_stats_t *head;
220 rrdc_stats_t *ptr;
222 value_t values[1];
223 value_list_t vl = VALUE_LIST_INIT;
225 if (daemon_address == NULL)
226 return (-1);
228 if (config_collect_stats == 0)
229 return (-1);
231 vl.values = values;
232 vl.values_len = 1;
234 if ((strncmp ("unix:", daemon_address, strlen ("unix:")) == 0)
235 || (daemon_address[0] == '/'))
236 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
237 else
238 sstrncpy (vl.host, daemon_address, sizeof (vl.host));
239 sstrncpy (vl.plugin, "rrdcached", sizeof (vl.plugin));
241 head = NULL;
242 status = rrdc_stats_get (&head);
243 if (status != 0)
244 {
245 ERROR ("rrdcached plugin: rrdc_stats_get failed with status %i.", status);
246 return (-1);
247 }
249 for (ptr = head; ptr != NULL; ptr = ptr->next)
250 {
251 if (ptr->type == RRDC_STATS_TYPE_GAUGE)
252 values[0].gauge = (gauge_t) ptr->value.gauge;
253 else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
254 values[0].counter = (counter_t) ptr->value.counter;
255 else
256 continue;
258 if (strcasecmp ("QueueLength", ptr->name) == 0)
259 {
260 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
261 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
262 }
263 else if (strcasecmp ("UpdatesWritten", ptr->name) == 0)
264 {
265 sstrncpy (vl.type, "operations", sizeof (vl.type));
266 sstrncpy (vl.type_instance, "write-updates", sizeof (vl.type_instance));
267 }
268 else if (strcasecmp ("DataSetsWritten", ptr->name) == 0)
269 {
270 sstrncpy (vl.type, "operations", sizeof (vl.type));
271 sstrncpy (vl.type_instance, "write-data_sets",
272 sizeof (vl.type_instance));
273 }
274 else if (strcasecmp ("TreeNodesNumber", ptr->name) == 0)
275 {
276 sstrncpy (vl.type, "gauge", sizeof (vl.type));
277 sstrncpy (vl.type_instance, "tree_nodes", sizeof (vl.type_instance));
278 }
279 else if (strcasecmp ("TreeDepth", ptr->name) == 0)
280 {
281 sstrncpy (vl.type, "gauge", sizeof (vl.type));
282 sstrncpy (vl.type_instance, "tree_depth", sizeof (vl.type_instance));
283 }
284 else if (strcasecmp ("FlushesReceived", ptr->name) == 0)
285 {
286 sstrncpy (vl.type, "operations", sizeof (vl.type));
287 sstrncpy (vl.type_instance, "receive-flush", sizeof (vl.type_instance));
288 }
289 else if (strcasecmp ("JournalBytes", ptr->name) == 0)
290 {
291 sstrncpy (vl.type, "counter", sizeof (vl.type));
292 sstrncpy (vl.type_instance, "journal-bytes", sizeof (vl.type_instance));
293 }
294 else if (strcasecmp ("JournalRotate", ptr->name) == 0)
295 {
296 sstrncpy (vl.type, "counter", sizeof (vl.type));
297 sstrncpy (vl.type_instance, "journal-rotates", sizeof (vl.type_instance));
298 }
299 else if (strcasecmp ("UpdatesReceived", ptr->name) == 0)
300 {
301 sstrncpy (vl.type, "operations", sizeof (vl.type));
302 sstrncpy (vl.type_instance, "receive-update", sizeof (vl.type_instance));
303 }
304 else
305 {
306 DEBUG ("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
307 continue;
308 }
310 plugin_dispatch_values (&vl);
311 } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
313 rrdc_stats_free (head);
315 return (0);
316 } /* int rc_read */
318 static int rc_init (void)
319 {
320 if (config_collect_stats != 0)
321 plugin_register_read ("rrdcached", rc_read);
323 return (0);
324 } /* int rc_init */
326 static int rc_write (const data_set_t *ds, const value_list_t *vl,
327 user_data_t __attribute__((unused)) *user_data)
328 {
329 char filename[512];
330 char values[512];
331 char *values_array[2];
332 int status;
334 if (daemon_address == NULL)
335 {
336 ERROR ("rrdcached plugin: daemon_address == NULL.");
337 plugin_unregister_write ("rrdcached");
338 return (-1);
339 }
341 if (strcmp (ds->type, vl->type) != 0)
342 {
343 ERROR ("rrdcached plugin: DS type does not match value list type");
344 return (-1);
345 }
347 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
348 {
349 ERROR ("rrdcached plugin: value_list_to_filename failed.");
350 return (-1);
351 }
353 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
354 {
355 ERROR ("rrdcached plugin: value_list_to_string failed.");
356 return (-1);
357 }
359 values_array[0] = values;
360 values_array[1] = NULL;
362 if (config_create_files != 0)
363 {
364 struct stat statbuf;
366 status = stat (filename, &statbuf);
367 if (status != 0)
368 {
369 if (errno != ENOENT)
370 {
371 char errbuf[1024];
372 ERROR ("rrdcached plugin: stat (%s) failed: %s",
373 filename, sstrerror (errno, errbuf, sizeof (errbuf)));
374 return (-1);
375 }
377 status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
378 if (status != 0)
379 {
380 ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
381 filename);
382 return (-1);
383 }
384 }
385 }
387 status = rrdc_connect (daemon_address);
388 if (status != 0)
389 {
390 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
391 daemon_address, status);
392 return (-1);
393 }
395 status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
396 if (status != 0)
397 {
398 ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
399 "status %i.",
400 filename, values_array[0], status);
401 return (-1);
402 }
404 return (0);
405 } /* int rc_write */
407 static int rc_shutdown (void)
408 {
409 rrdc_disconnect ();
410 return (0);
411 } /* int rc_shutdown */
413 void module_register (void)
414 {
415 plugin_register_config ("rrdcached", rc_config,
416 config_keys, config_keys_num);
417 plugin_register_init ("rrdcached", rc_init);
418 plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
419 plugin_register_shutdown ("rrdcached", rc_shutdown);
420 } /* void module_register */
422 /*
423 * vim: set sw=2 sts=2 et :
424 */