Code

a392715616ff7fb501cc8294db0461a9412bb62a
[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  **/
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 char *datadir = NULL;
35 static char *daemon_address = NULL;
36 static int config_create_files = 1;
37 static int config_collect_stats = 1;
38 static rrdcreate_config_t rrdcreate_config =
39 {
40         /* stepsize = */ 0,
41         /* heartbeat = */ 0,
42         /* rrarows = */ 1200,
43         /* xff = */ 0.1,
45         /* timespans = */ NULL,
46         /* timespans_num = */ 0,
48         /* consolidation_functions = */ NULL,
49         /* consolidation_functions_num = */ 0
50 };
52 static int value_list_to_string (char *buffer, int buffer_len,
53     const data_set_t *ds, const value_list_t *vl)
54 {
55   int offset;
56   int status;
57   int i;
58   time_t t;
60   assert (0 == strcmp (ds->type, vl->type));
62   memset (buffer, '\0', buffer_len);
64   t = CDTIME_T_TO_TIME_T (vl->time);
65   status = ssnprintf (buffer, buffer_len, "%lu", (unsigned long) t);
66   if ((status < 1) || (status >= buffer_len))
67     return (-1);
68   offset = status;
70   for (i = 0; i < ds->ds_num; i++)
71   {
72     if ((ds->ds[i].type != DS_TYPE_COUNTER)
73         && (ds->ds[i].type != DS_TYPE_GAUGE)
74         && (ds->ds[i].type != DS_TYPE_DERIVE)
75         && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
76       return (-1);
78     if (ds->ds[i].type == DS_TYPE_COUNTER)
79     {
80       status = ssnprintf (buffer + offset, buffer_len - offset,
81           ":%llu", vl->values[i].counter);
82     }
83     else if (ds->ds[i].type == DS_TYPE_GAUGE) 
84     {
85       status = ssnprintf (buffer + offset, buffer_len - offset,
86           ":%f", vl->values[i].gauge);
87     }
88     else if (ds->ds[i].type == DS_TYPE_DERIVE) {
89       status = ssnprintf (buffer + offset, buffer_len - offset,
90           ":%"PRIi64, vl->values[i].derive);
91     }
92     else /* if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */ {
93       status = ssnprintf (buffer + offset, buffer_len - offset,
94           ":%"PRIu64, vl->values[i].absolute);
95  
96     }
98     if ((status < 1) || (status >= (buffer_len - offset)))
99       return (-1);
101     offset += status;
102   } /* for ds->ds_num */
104   return (0);
105 } /* int value_list_to_string */
107 static int value_list_to_filename (char *buffer, int buffer_len,
108     const data_set_t *ds, const value_list_t *vl)
110   int offset = 0;
111   int status;
113   assert (0 == strcmp (ds->type, vl->type));
115   if (datadir != NULL)
116   {
117     status = ssnprintf (buffer + offset, buffer_len - offset,
118         "%s/", datadir);
119     if ((status < 1) || (status >= buffer_len - offset))
120       return (-1);
121     offset += status;
122   }
124   status = ssnprintf (buffer + offset, buffer_len - offset,
125       "%s/", vl->host);
126   if ((status < 1) || (status >= buffer_len - offset))
127     return (-1);
128   offset += status;
130   if (strlen (vl->plugin_instance) > 0)
131     status = ssnprintf (buffer + offset, buffer_len - offset,
132         "%s-%s/", vl->plugin, vl->plugin_instance);
133   else
134     status = ssnprintf (buffer + offset, buffer_len - offset,
135         "%s/", vl->plugin);
136   if ((status < 1) || (status >= buffer_len - offset))
137     return (-1);
138   offset += status;
140   if (strlen (vl->type_instance) > 0)
141     status = ssnprintf (buffer + offset, buffer_len - offset,
142         "%s-%s", vl->type, vl->type_instance);
143   else
144     status = ssnprintf (buffer + offset, buffer_len - offset,
145         "%s", vl->type);
146   if ((status < 1) || (status >= buffer_len - offset))
147     return (-1);
148   offset += status;
150   strncpy (buffer + offset, ".rrd", buffer_len - offset);
151   buffer[buffer_len - 1] = 0;
153   return (0);
154 } /* int value_list_to_filename */
156 static const char *config_get_string (oconfig_item_t *ci)
158   if ((ci->children_num != 0) || (ci->values_num != 1)
159       || (ci->values[0].type != OCONFIG_TYPE_STRING))
160   {
161     ERROR ("rrdcached plugin: %s expects a single string argument.",
162         ci->key);
163     return (NULL);
164   }
166   return (ci->values[0].value.string);
167 } /* const char *config_get_string */
169 static int rc_config (oconfig_item_t *ci)
171   int i;
173   for (i = 0; i < ci->children_num; ++i) {
174     const char *key = ci->children[i].key;
175     const char *value = config_get_string (ci->children + i);
177     if (value == NULL) /* config_get_strings prints error message */
178       continue;
180     if (strcasecmp ("DataDir", key) == 0)
181     {
182       if (datadir != NULL)
183         free (datadir);
184       datadir = strdup (value);
185       if (datadir != NULL)
186       {
187         int len = strlen (datadir);
188         while ((len > 0) && (datadir[len - 1] == '/'))
189         {
190           len--;
191           datadir[len] = '\0';
192         }
193         if (len <= 0)
194         {
195           free (datadir);
196           datadir = NULL;
197         }
198       }
199     }
200     else if (strcasecmp ("DaemonAddress", key) == 0)
201     {
202       sfree (daemon_address);
203       daemon_address = strdup (value);
204       if (daemon_address == NULL)
205       {
206         ERROR ("rrdcached plugin: strdup failed.");
207         continue;
208       }
209     }
210     else if (strcasecmp ("CreateFiles", key) == 0)
211     {
212       if (IS_FALSE (value))
213         config_create_files = 0;
214       else
215         config_create_files = 1;
216     }
217     else if (strcasecmp ("CollectStatistics", key) == 0)
218     {
219       if (IS_FALSE (value))
220         config_collect_stats = 0;
221       else
222         config_collect_stats = 1;
223     }
224     else
225     {
226       WARNING ("rrdcached plugin: Ignoring invalid option %s.", key);
227       continue;
228     }
229   }
230   return (0);
231 } /* int rc_config */
233 static int rc_read (void)
235   int status;
236   rrdc_stats_t *head;
237   rrdc_stats_t *ptr;
239   value_t values[1];
240   value_list_t vl = VALUE_LIST_INIT;
242   if (daemon_address == NULL)
243     return (-1);
245   if (config_collect_stats == 0)
246     return (-1);
248   vl.values = values;
249   vl.values_len = 1;
251   if ((strncmp ("unix:", daemon_address, strlen ("unix:")) == 0)
252       || (daemon_address[0] == '/'))
253     sstrncpy (vl.host, hostname_g, sizeof (vl.host));
254   else
255     sstrncpy (vl.host, daemon_address, sizeof (vl.host));
256   sstrncpy (vl.plugin, "rrdcached", sizeof (vl.plugin));
258   head = NULL;
259   status = rrdc_stats_get (&head);
260   if (status != 0)
261   {
262     ERROR ("rrdcached plugin: rrdc_stats_get failed with status %i.", status);
263     return (-1);
264   }
266   for (ptr = head; ptr != NULL; ptr = ptr->next)
267   {
268     if (ptr->type == RRDC_STATS_TYPE_GAUGE)
269       values[0].gauge = (gauge_t) ptr->value.gauge;
270     else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
271       values[0].counter = (counter_t) ptr->value.counter;
272     else
273       continue;
275     if (strcasecmp ("QueueLength", ptr->name) == 0)
276     {
277       sstrncpy (vl.type, "queue_length", sizeof (vl.type));
278       sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
279     }
280     else if (strcasecmp ("UpdatesWritten", ptr->name) == 0)
281     {
282       sstrncpy (vl.type, "operations", sizeof (vl.type));
283       sstrncpy (vl.type_instance, "write-updates", sizeof (vl.type_instance));
284     }
285     else if (strcasecmp ("DataSetsWritten", ptr->name) == 0)
286     {
287       sstrncpy (vl.type, "operations", sizeof (vl.type));
288       sstrncpy (vl.type_instance, "write-data_sets",
289           sizeof (vl.type_instance));
290     }
291     else if (strcasecmp ("TreeNodesNumber", ptr->name) == 0)
292     {
293       sstrncpy (vl.type, "gauge", sizeof (vl.type));
294       sstrncpy (vl.type_instance, "tree_nodes", sizeof (vl.type_instance));
295     }
296     else if (strcasecmp ("TreeDepth", ptr->name) == 0)
297     {
298       sstrncpy (vl.type, "gauge", sizeof (vl.type));
299       sstrncpy (vl.type_instance, "tree_depth", sizeof (vl.type_instance));
300     }
301     else if (strcasecmp ("FlushesReceived", ptr->name) == 0)
302     {
303       sstrncpy (vl.type, "operations", sizeof (vl.type));
304       sstrncpy (vl.type_instance, "receive-flush", sizeof (vl.type_instance));
305     }
306     else if (strcasecmp ("JournalBytes", ptr->name) == 0)
307     {
308       sstrncpy (vl.type, "counter", sizeof (vl.type));
309       sstrncpy (vl.type_instance, "journal-bytes", sizeof (vl.type_instance));
310     }
311     else if (strcasecmp ("JournalRotate", ptr->name) == 0)
312     {
313       sstrncpy (vl.type, "counter", sizeof (vl.type));
314       sstrncpy (vl.type_instance, "journal-rotates", sizeof (vl.type_instance));
315     }
316     else if (strcasecmp ("UpdatesReceived", ptr->name) == 0)
317     {
318       sstrncpy (vl.type, "operations", sizeof (vl.type));
319       sstrncpy (vl.type_instance, "receive-update", sizeof (vl.type_instance));
320     }
321     else
322     {
323       DEBUG ("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
324       continue;
325     }
327     plugin_dispatch_values (&vl);
328   } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
330   rrdc_stats_free (head);
332   return (0);
333 } /* int rc_read */
335 static int rc_init (void)
337   if (config_collect_stats != 0)
338     plugin_register_read ("rrdcached", rc_read);
340   return (0);
341 } /* int rc_init */
343 static int rc_write (const data_set_t *ds, const value_list_t *vl,
344     user_data_t __attribute__((unused)) *user_data)
346   char filename[PATH_MAX];
347   char values[512];
348   char *values_array[2];
349   int status;
351   if (daemon_address == NULL)
352   {
353     ERROR ("rrdcached plugin: daemon_address == NULL.");
354     plugin_unregister_write ("rrdcached");
355     return (-1);
356   }
358   if (strcmp (ds->type, vl->type) != 0)
359   {
360     ERROR ("rrdcached plugin: DS type does not match value list type");
361     return (-1);
362   }
364   if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
365   {
366     ERROR ("rrdcached plugin: value_list_to_filename failed.");
367     return (-1);
368   }
370   if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
371   {
372     ERROR ("rrdcached plugin: value_list_to_string failed.");
373     return (-1);
374   }
376   values_array[0] = values;
377   values_array[1] = NULL;
379   if (config_create_files != 0)
380   {
381     struct stat statbuf;
383     status = stat (filename, &statbuf);
384     if (status != 0)
385     {
386       if (errno != ENOENT)
387       {
388         char errbuf[1024];
389         ERROR ("rrdcached plugin: stat (%s) failed: %s",
390             filename, sstrerror (errno, errbuf, sizeof (errbuf)));
391         return (-1);
392       }
394       status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
395       if (status != 0)
396       {
397         ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
398             filename);
399         return (-1);
400       }
401     }
402   }
404   status = rrdc_connect (daemon_address);
405   if (status != 0)
406   {
407     ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
408         daemon_address, status);
409     return (-1);
410   }
412   status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
413   if (status != 0)
414   {
415     ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
416         "status %i.",
417         filename, values_array[0], status);
418     return (-1);
419   }
421   return (0);
422 } /* int rc_write */
424 static int rc_flush (__attribute__((unused)) cdtime_t timeout, /* {{{ */
425     const char *identifier,
426     __attribute__((unused)) user_data_t *ud)
428   char filename[PATH_MAX + 1];
429   int status;
431   if (identifier == NULL)
432     return (EINVAL);
434   if (datadir != NULL)
435     ssnprintf (filename, sizeof (filename), "%s/%s.rrd", datadir, identifier);
436   else
437     ssnprintf (filename, sizeof (filename), "%s.rrd", identifier);
439   status = rrdc_connect (daemon_address);
440   if (status != 0)
441   {
442     ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
443         daemon_address, status);
444     return (-1);
445   }
447   status = rrdc_flush (filename);
448   if (status != 0)
449   {
450     ERROR ("rrdcached plugin: rrdc_flush (%s) failed with status %i.",
451         filename, status);
452     return (-1);
453   }
454   DEBUG ("rrdcached plugin: rrdc_flush (%s): Success.", filename);
456   return (0);
457 } /* }}} int rc_flush */
459 static int rc_shutdown (void)
461   rrdc_disconnect ();
462   return (0);
463 } /* int rc_shutdown */
465 void module_register (void)
467   plugin_register_complex_config ("rrdcached", rc_config);
468   plugin_register_init ("rrdcached", rc_init);
469   plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
470   plugin_register_flush ("rrdcached", rc_flush, /* user_data = */ NULL);
471   plugin_register_shutdown ("rrdcached", rc_shutdown);
472 } /* void module_register */
474 /*
475  * vim: set sw=2 sts=2 et :
476  */