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