Merge branch 'master' into ff/rrdcached
[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   vl.time = time (NULL);
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.");
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
277     {
278       continue;
279     }
280
281     plugin_dispatch_values (&vl);
282   } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
283
284   rrdc_stats_free (head);
285
286   return (0);
287 } /* int rc_read */
288
289 static int rc_init (void)
290 {
291   if (config_collect_stats != 0)
292     plugin_register_read ("rrdcached", rc_read);
293
294   return (0);
295 } /* int rc_init */
296
297 static int rc_write (const data_set_t *ds, const value_list_t *vl)
298 {
299   char filename[512];
300   char values[512];
301   char *values_array[2];
302   int status;
303
304   if (daemon_address == NULL)
305   {
306     ERROR ("rrdcached plugin: daemon_address == NULL.");
307     plugin_unregister_write ("rrdcached");
308     return (-1);
309   }
310
311   if (strcmp (ds->type, vl->type) != 0)
312   {
313     ERROR ("rrdcached plugin: DS type does not match value list type");
314     return (-1);
315   }
316
317   if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
318   {
319     ERROR ("rrdcached plugin: value_list_to_filename failed.");
320     return (-1);
321   }
322
323   if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
324   {
325     ERROR ("rrdcached plugin: value_list_to_string failed.");
326     return (-1);
327   }
328
329   values_array[0] = values;
330   values_array[1] = NULL;
331
332   if (config_create_files != 0)
333   {
334     struct stat statbuf;
335
336     status = stat (filename, &statbuf);
337     if (status != 0)
338     {
339       if (errno != ENOENT)
340       {
341         char errbuf[1024];
342         ERROR ("rrdcached plugin: stat (%s) failed: %s",
343             filename, sstrerror (errno, errbuf, sizeof (errbuf)));
344         return (-1);
345       }
346
347       status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
348       if (status != 0)
349       {
350         ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
351             filename);
352         return (-1);
353       }
354     }
355   }
356
357   status = rrdc_connect (daemon_address);
358   if (status != 0)
359   {
360     ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
361         daemon_address, status);
362     return (-1);
363   }
364
365   status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
366   if (status != 0)
367   {
368     ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
369         "status %i.",
370         filename, values_array[0], status);
371     return (-1);
372   }
373
374   return (0);
375 } /* int rc_write */
376
377 static int rc_shutdown (void)
378 {
379   rrdc_disconnect ();
380   return (0);
381 } /* int rc_shutdown */
382
383 void module_register (void)
384 {
385   plugin_register_config ("rrdcached", rc_config,
386       config_keys, config_keys_num);
387   plugin_register_init ("rrdcached", rc_init);
388   plugin_register_write ("rrdcached", rc_write);
389   plugin_register_shutdown ("rrdcached", rc_shutdown);
390 } /* void module_register */
391
392 /*
393  * vim: set sw=2 sts=2 et :
394  */