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