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