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;
225 if ((strncmp ("unix:", daemon_address, strlen ("unix:")) == 0)
226 || (daemon_address[0] == '/'))
227 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
228 else
229 sstrncpy (vl.host, daemon_address, sizeof (vl.host));
230 sstrncpy (vl.plugin, "rrdcached", sizeof (vl.plugin));
232 head = NULL;
233 status = rrdc_stats_get (&head);
234 if (status != 0)
235 {
236 ERROR ("rrdcached plugin: rrdc_stats_get failed with status %i.", status);
237 return (-1);
238 }
240 for (ptr = head; ptr != NULL; ptr = ptr->next)
241 {
242 if (ptr->type == RRDC_STATS_TYPE_GAUGE)
243 values[0].gauge = (gauge_t) ptr->value.gauge;
244 else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
245 values[0].counter = (counter_t) ptr->value.counter;
246 else
247 continue;
249 if (strcasecmp ("QueueLength", ptr->name) == 0)
250 {
251 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
252 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
253 }
254 else if (strcasecmp ("UpdatesWritten", ptr->name) == 0)
255 {
256 sstrncpy (vl.type, "operations", sizeof (vl.type));
257 sstrncpy (vl.type_instance, "write-updates", sizeof (vl.type_instance));
258 }
259 else if (strcasecmp ("DataSetsWritten", ptr->name) == 0)
260 {
261 sstrncpy (vl.type, "operations", sizeof (vl.type));
262 sstrncpy (vl.type_instance, "write-data_sets",
263 sizeof (vl.type_instance));
264 }
265 else if (strcasecmp ("TreeNodesNumber", ptr->name) == 0)
266 {
267 sstrncpy (vl.type, "gauge", sizeof (vl.type));
268 sstrncpy (vl.type_instance, "tree_nodes", sizeof (vl.type_instance));
269 }
270 else if (strcasecmp ("TreeDepth", ptr->name) == 0)
271 {
272 sstrncpy (vl.type, "gauge", sizeof (vl.type));
273 sstrncpy (vl.type_instance, "tree_depth", sizeof (vl.type_instance));
274 }
275 else if (strcasecmp ("FlushesReceived", ptr->name) == 0)
276 {
277 sstrncpy (vl.type, "operations", sizeof (vl.type));
278 sstrncpy (vl.type_instance, "receive-flush", sizeof (vl.type_instance));
279 }
280 else if (strcasecmp ("JournalBytes", ptr->name) == 0)
281 {
282 sstrncpy (vl.type, "counter", sizeof (vl.type));
283 sstrncpy (vl.type_instance, "journal-bytes", sizeof (vl.type_instance));
284 }
285 else if (strcasecmp ("JournalRotate", ptr->name) == 0)
286 {
287 sstrncpy (vl.type, "counter", sizeof (vl.type));
288 sstrncpy (vl.type_instance, "journal-rotates", sizeof (vl.type_instance));
289 }
290 else if (strcasecmp ("UpdatesReceived", ptr->name) == 0)
291 {
292 sstrncpy (vl.type, "operations", sizeof (vl.type));
293 sstrncpy (vl.type_instance, "receive-update", sizeof (vl.type_instance));
294 }
295 else
296 {
297 DEBUG ("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
298 continue;
299 }
301 plugin_dispatch_values (&vl);
302 } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
304 rrdc_stats_free (head);
306 return (0);
307 } /* int rc_read */
309 static int rc_init (void)
310 {
311 if (config_collect_stats != 0)
312 plugin_register_read ("rrdcached", rc_read);
314 return (0);
315 } /* int rc_init */
317 static int rc_write (const data_set_t *ds, const value_list_t *vl)
318 {
319 char filename[512];
320 char values[512];
321 char *values_array[2];
322 int status;
324 if (daemon_address == NULL)
325 {
326 ERROR ("rrdcached plugin: daemon_address == NULL.");
327 plugin_unregister_write ("rrdcached");
328 return (-1);
329 }
331 if (strcmp (ds->type, vl->type) != 0)
332 {
333 ERROR ("rrdcached plugin: DS type does not match value list type");
334 return (-1);
335 }
337 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
338 {
339 ERROR ("rrdcached plugin: value_list_to_filename failed.");
340 return (-1);
341 }
343 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
344 {
345 ERROR ("rrdcached plugin: value_list_to_string failed.");
346 return (-1);
347 }
349 values_array[0] = values;
350 values_array[1] = NULL;
352 if (config_create_files != 0)
353 {
354 struct stat statbuf;
356 status = stat (filename, &statbuf);
357 if (status != 0)
358 {
359 if (errno != ENOENT)
360 {
361 char errbuf[1024];
362 ERROR ("rrdcached plugin: stat (%s) failed: %s",
363 filename, sstrerror (errno, errbuf, sizeof (errbuf)));
364 return (-1);
365 }
367 status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
368 if (status != 0)
369 {
370 ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
371 filename);
372 return (-1);
373 }
374 }
375 }
377 status = rrdc_connect (daemon_address);
378 if (status != 0)
379 {
380 ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
381 daemon_address, status);
382 return (-1);
383 }
385 status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
386 if (status != 0)
387 {
388 ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
389 "status %i.",
390 filename, values_array[0], status);
391 return (-1);
392 }
394 return (0);
395 } /* int rc_write */
397 static int rc_shutdown (void)
398 {
399 rrdc_disconnect ();
400 return (0);
401 } /* int rc_shutdown */
403 void module_register (void)
404 {
405 plugin_register_config ("rrdcached", rc_config,
406 config_keys, config_keys_num);
407 plugin_register_init ("rrdcached", rc_init);
408 plugin_register_write ("rrdcached", rc_write);
409 plugin_register_shutdown ("rrdcached", rc_shutdown);
410 } /* void module_register */
412 /*
413 * vim: set sw=2 sts=2 et :
414 */