rrdcached plugin: Work around a bug in RRDtool 1.4rc2.
[collectd.git] / src / rrdcached.c
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  **/
21
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25 #include "utils_rrdcreate.h"
26
27 #undef HAVE_CONFIG_H
28 #include <rrd_client.h>
29
30 /*
31  * Private variables
32  */
33 static const char *config_keys[] =
34 {
35   "DaemonAddress",
36   "DataDir",
37   "CreateFiles",
38   "CollectStatistics"
39 };
40 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
41
42 static char *datadir = NULL;
43 static char *daemon_address = NULL;
44 static int config_create_files = 1;
45 static int config_collect_stats = 1;
46 static rrdcreate_config_t rrdcreate_config =
47 {
48         /* stepsize = */ 0,
49         /* heartbeat = */ 0,
50         /* rrarows = */ 1200,
51         /* xff = */ 0.1,
52
53         /* timespans = */ NULL,
54         /* timespans_num = */ 0,
55
56         /* consolidation_functions = */ NULL,
57         /* consolidation_functions_num = */ 0
58 };
59
60 static int value_list_to_string (char *buffer, int buffer_len,
61     const data_set_t *ds, const value_list_t *vl)
62 {
63   int offset;
64   int status;
65   int i;
66
67   assert (0 == strcmp (ds->type, vl->type));
68
69   memset (buffer, '\0', buffer_len);
70
71   status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
72   if ((status < 1) || (status >= buffer_len))
73     return (-1);
74   offset = status;
75
76   for (i = 0; i < ds->ds_num; i++)
77   {
78     if ((ds->ds[i].type != DS_TYPE_COUNTER)
79         && (ds->ds[i].type != DS_TYPE_GAUGE))
80       return (-1);
81
82     if (ds->ds[i].type == DS_TYPE_COUNTER)
83     {
84       status = ssnprintf (buffer + offset, buffer_len - offset,
85           ":%llu", vl->values[i].counter);
86     }
87     else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
88     {
89       status = ssnprintf (buffer + offset, buffer_len - offset,
90           ":%lf", vl->values[i].gauge);
91     }
92
93     if ((status < 1) || (status >= (buffer_len - offset)))
94       return (-1);
95
96     offset += status;
97   } /* for ds->ds_num */
98
99   return (0);
100 } /* int value_list_to_string */
101
102 static int value_list_to_filename (char *buffer, int buffer_len,
103     const data_set_t *ds, const value_list_t *vl)
104 {
105   int offset = 0;
106   int status;
107
108   assert (0 == strcmp (ds->type, vl->type));
109
110   if (datadir != NULL)
111   {
112     status = ssnprintf (buffer + offset, buffer_len - offset,
113         "%s/", datadir);
114     if ((status < 1) || (status >= buffer_len - offset))
115       return (-1);
116     offset += status;
117   }
118
119   status = ssnprintf (buffer + offset, buffer_len - offset,
120       "%s/", vl->host);
121   if ((status < 1) || (status >= buffer_len - offset))
122     return (-1);
123   offset += status;
124
125   if (strlen (vl->plugin_instance) > 0)
126     status = ssnprintf (buffer + offset, buffer_len - offset,
127         "%s-%s/", vl->plugin, vl->plugin_instance);
128   else
129     status = ssnprintf (buffer + offset, buffer_len - offset,
130         "%s/", vl->plugin);
131   if ((status < 1) || (status >= buffer_len - offset))
132     return (-1);
133   offset += status;
134
135   if (strlen (vl->type_instance) > 0)
136     status = ssnprintf (buffer + offset, buffer_len - offset,
137         "%s-%s", vl->type, vl->type_instance);
138   else
139     status = ssnprintf (buffer + offset, buffer_len - offset,
140         "%s", vl->type);
141   if ((status < 1) || (status >= buffer_len - offset))
142     return (-1);
143   offset += status;
144
145   strncpy (buffer + offset, ".rrd", buffer_len - offset);
146   buffer[buffer_len - 1] = 0;
147
148   return (0);
149 } /* int value_list_to_filename */
150
151 static int rc_config (const char *key, const char *value)
152 {
153   if (strcasecmp ("DataDir", key) == 0)
154   {
155     if (datadir != NULL)
156       free (datadir);
157     datadir = strdup (value);
158     if (datadir != NULL)
159     {
160       int len = strlen (datadir);
161       while ((len > 0) && (datadir[len - 1] == '/'))
162       {
163         len--;
164         datadir[len] = '\0';
165       }
166       if (len <= 0)
167       {
168         free (datadir);
169         datadir = NULL;
170       }
171     }
172   }
173   else if (strcasecmp ("DaemonAddress", key) == 0)
174   {
175     sfree (daemon_address);
176     daemon_address = strdup (value);
177     if (daemon_address == NULL)
178     {
179       ERROR ("rrdcached plugin: strdup failed.");
180       return (1);
181     }
182   }
183   else if (strcasecmp ("CreateFiles", key) == 0)
184   {
185     if ((strcasecmp ("false", value) == 0)
186         || (strcasecmp ("no", value) == 0)
187         || (strcasecmp ("off", value) == 0))
188       config_create_files = 0;
189     else
190       config_create_files = 1;
191   }
192   else if (strcasecmp ("CollectStatistics", key) == 0)
193   {
194     if ((strcasecmp ("false", value) == 0)
195         || (strcasecmp ("no", value) == 0)
196         || (strcasecmp ("off", value) == 0))
197       config_collect_stats = 0;
198     else
199       config_collect_stats = 1;
200   }
201   else
202   {
203     return (-1);
204   }
205   return (0);
206 } /* int rc_config */
207
208 static int rc_read (void)
209 {
210   int status;
211   rrdc_stats_t *head;
212   rrdc_stats_t *ptr;
213
214   value_t values[1];
215   value_list_t vl = VALUE_LIST_INIT;
216
217   if (daemon_address == NULL)
218     return (-1);
219
220   if (config_collect_stats == 0)
221     return (-1);
222
223   vl.values = values;
224   vl.values_len = 1;
225
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));
232
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   }
240
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;
249
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     }
301
302     plugin_dispatch_values (&vl);
303   } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
304
305   rrdc_stats_free (head);
306
307   return (0);
308 } /* int rc_read */
309
310 static int rc_init (void)
311 {
312   if (config_collect_stats != 0)
313     plugin_register_read ("rrdcached", rc_read);
314
315   return (0);
316 } /* int rc_init */
317
318 static int rc_write (const data_set_t *ds, const value_list_t *vl,
319     user_data_t __attribute__((unused)) *user_data)
320 {
321   char filename[512];
322   char values[512];
323   char *values_array[2];
324   int status;
325
326   if (daemon_address == NULL)
327   {
328     ERROR ("rrdcached plugin: daemon_address == NULL.");
329     plugin_unregister_write ("rrdcached");
330     return (-1);
331   }
332
333   if (strcmp (ds->type, vl->type) != 0)
334   {
335     ERROR ("rrdcached plugin: DS type does not match value list type");
336     return (-1);
337   }
338
339   if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
340   {
341     ERROR ("rrdcached plugin: value_list_to_filename failed.");
342     return (-1);
343   }
344
345   if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
346   {
347     ERROR ("rrdcached plugin: value_list_to_string failed.");
348     return (-1);
349   }
350
351   values_array[0] = values;
352   values_array[1] = NULL;
353
354   if (config_create_files != 0)
355   {
356     struct stat statbuf;
357
358     status = stat (filename, &statbuf);
359     if (status != 0)
360     {
361       if (errno != ENOENT)
362       {
363         char errbuf[1024];
364         ERROR ("rrdcached plugin: stat (%s) failed: %s",
365             filename, sstrerror (errno, errbuf, sizeof (errbuf)));
366         return (-1);
367       }
368
369       status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
370       if (status != 0)
371       {
372         ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
373             filename);
374         return (-1);
375       }
376     }
377   }
378
379   status = rrdc_connect (daemon_address);
380   if (status != 0)
381   {
382     ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
383         daemon_address, status);
384     return (-1);
385   }
386
387   status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
388   if (status != 0)
389   {
390     ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
391         "status %i.",
392         filename, values_array[0], status);
393     return (-1);
394   }
395
396   return (0);
397 } /* int rc_write */
398
399 static int rc_shutdown (void)
400 {
401   rrdc_disconnect ();
402   return (0);
403 } /* int rc_shutdown */
404
405 void module_register (void)
406 {
407   plugin_register_config ("rrdcached", rc_config,
408       config_keys, config_keys_num);
409   plugin_register_init ("rrdcached", rc_init);
410   plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
411   plugin_register_shutdown ("rrdcached", rc_shutdown);
412 } /* void module_register */
413
414 /*
415  * vim: set sw=2 sts=2 et :
416  */