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