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