src/plugin.c: Change the write callbacks to receive a user_data_t pointer.
[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 #include <rrd_client.h>
28
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);
40
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,
51
52         /* timespans = */ NULL,
53         /* timespans_num = */ 0,
54
55         /* consolidation_functions = */ NULL,
56         /* consolidation_functions_num = */ 0
57 };
58
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;
65
66   assert (0 == strcmp (ds->type, vl->type));
67
68   memset (buffer, '\0', buffer_len);
69
70   status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
71   if ((status < 1) || (status >= buffer_len))
72     return (-1);
73   offset = status;
74
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);
80
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     }
91
92     if ((status < 1) || (status >= (buffer_len - offset)))
93       return (-1);
94
95     offset += status;
96   } /* for ds->ds_num */
97
98   return (0);
99 } /* int value_list_to_string */
100
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;
106
107   assert (0 == strcmp (ds->type, vl->type));
108
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   }
117
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;
123
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;
133
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;
143
144   strncpy (buffer + offset, ".rrd", buffer_len - offset);
145   buffer[buffer_len - 1] = 0;
146
147   return (0);
148 } /* int value_list_to_filename */
149
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 */
206
207 static int rc_read (void)
208 {
209   int status;
210   rrdc_stats_t *head;
211   rrdc_stats_t *ptr;
212
213   value_t values[1];
214   value_list_t vl = VALUE_LIST_INIT;
215
216   if (daemon_address == NULL)
217     return (-1);
218
219   if (config_collect_stats == 0)
220     return (-1);
221
222   vl.values = values;
223   vl.values_len = 1;
224
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));
231
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   }
239
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;
248
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     }
300
301     plugin_dispatch_values (&vl);
302   } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
303
304   rrdc_stats_free (head);
305
306   return (0);
307 } /* int rc_read */
308
309 static int rc_init (void)
310 {
311   if (config_collect_stats != 0)
312     plugin_register_read ("rrdcached", rc_read);
313
314   return (0);
315 } /* int rc_init */
316
317 static int rc_write (const data_set_t *ds, const value_list_t *vl,
318     user_data_t __attribute__((unused)) *user_data)
319 {
320   char filename[512];
321   char values[512];
322   char *values_array[2];
323   int status;
324
325   if (daemon_address == NULL)
326   {
327     ERROR ("rrdcached plugin: daemon_address == NULL.");
328     plugin_unregister_write ("rrdcached");
329     return (-1);
330   }
331
332   if (strcmp (ds->type, vl->type) != 0)
333   {
334     ERROR ("rrdcached plugin: DS type does not match value list type");
335     return (-1);
336   }
337
338   if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
339   {
340     ERROR ("rrdcached plugin: value_list_to_filename failed.");
341     return (-1);
342   }
343
344   if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
345   {
346     ERROR ("rrdcached plugin: value_list_to_string failed.");
347     return (-1);
348   }
349
350   values_array[0] = values;
351   values_array[1] = NULL;
352
353   if (config_create_files != 0)
354   {
355     struct stat statbuf;
356
357     status = stat (filename, &statbuf);
358     if (status != 0)
359     {
360       if (errno != ENOENT)
361       {
362         char errbuf[1024];
363         ERROR ("rrdcached plugin: stat (%s) failed: %s",
364             filename, sstrerror (errno, errbuf, sizeof (errbuf)));
365         return (-1);
366       }
367
368       status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
369       if (status != 0)
370       {
371         ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
372             filename);
373         return (-1);
374       }
375     }
376   }
377
378   status = rrdc_connect (daemon_address);
379   if (status != 0)
380   {
381     ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
382         daemon_address, status);
383     return (-1);
384   }
385
386   status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
387   if (status != 0)
388   {
389     ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
390         "status %i.",
391         filename, values_array[0], status);
392     return (-1);
393   }
394
395   return (0);
396 } /* int rc_write */
397
398 static int rc_shutdown (void)
399 {
400   rrdc_disconnect ();
401   return (0);
402 } /* int rc_shutdown */
403
404 void module_register (void)
405 {
406   plugin_register_config ("rrdcached", rc_config,
407       config_keys, config_keys_num);
408   plugin_register_init ("rrdcached", rc_init);
409   plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
410   plugin_register_shutdown ("rrdcached", rc_shutdown);
411 } /* void module_register */
412
413 /*
414  * vim: set sw=2 sts=2 et :
415  */