RRDCacheD plugin: Improve various RRD-related error messages.
[collectd.git] / src / rrdcached.c
1 /**
2  * collectd - src/rrdcached.c
3  * Copyright (C) 2008-2013  Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28 #include "plugin.h"
29 #include "common.h"
30 #include "utils_rrdcreate.h"
31
32 #undef HAVE_CONFIG_H
33 #include <rrd.h>
34 #include <rrd_client.h>
35
36 /*
37  * Private variables
38  */
39 static char *datadir = NULL;
40 static char *daemon_address = NULL;
41 static _Bool config_create_files = 1;
42 static _Bool config_collect_stats = 1;
43 static rrdcreate_config_t rrdcreate_config =
44 {
45         /* stepsize = */ 0,
46         /* heartbeat = */ 0,
47         /* rrarows = */ 1200,
48         /* xff = */ 0.1,
49
50         /* timespans = */ NULL,
51         /* timespans_num = */ 0,
52
53         /* consolidation_functions = */ NULL,
54         /* consolidation_functions_num = */ 0,
55
56         /* async = */ 0
57 };
58
59 /*
60  * Prototypes.
61  */
62 static int rc_write (const data_set_t *ds, const value_list_t *vl,
63     user_data_t __attribute__((unused)) *user_data);
64 static int rc_flush (__attribute__((unused)) cdtime_t timeout,
65     const char *identifier, __attribute__((unused)) user_data_t *ud);
66
67 static int value_list_to_string (char *buffer, int buffer_len,
68     const data_set_t *ds, const value_list_t *vl)
69 {
70   int offset;
71   int status;
72   int i;
73   time_t t;
74
75   assert (0 == strcmp (ds->type, vl->type));
76
77   memset (buffer, '\0', buffer_len);
78
79   t = CDTIME_T_TO_TIME_T (vl->time);
80   status = ssnprintf (buffer, buffer_len, "%lu", (unsigned long) t);
81   if ((status < 1) || (status >= buffer_len))
82     return (-1);
83   offset = status;
84
85   for (i = 0; i < ds->ds_num; i++)
86   {
87     if ((ds->ds[i].type != DS_TYPE_COUNTER)
88         && (ds->ds[i].type != DS_TYPE_GAUGE)
89         && (ds->ds[i].type != DS_TYPE_DERIVE)
90         && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
91       return (-1);
92
93     if (ds->ds[i].type == DS_TYPE_COUNTER)
94     {
95       status = ssnprintf (buffer + offset, buffer_len - offset,
96           ":%llu", vl->values[i].counter);
97     }
98     else if (ds->ds[i].type == DS_TYPE_GAUGE) 
99     {
100       status = ssnprintf (buffer + offset, buffer_len - offset,
101           ":%f", vl->values[i].gauge);
102     }
103     else if (ds->ds[i].type == DS_TYPE_DERIVE) {
104       status = ssnprintf (buffer + offset, buffer_len - offset,
105           ":%"PRIi64, vl->values[i].derive);
106     }
107     else /* if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */ {
108       status = ssnprintf (buffer + offset, buffer_len - offset,
109           ":%"PRIu64, vl->values[i].absolute);
110  
111     }
112
113     if ((status < 1) || (status >= (buffer_len - offset)))
114       return (-1);
115
116     offset += status;
117   } /* for ds->ds_num */
118
119   return (0);
120 } /* int value_list_to_string */
121
122 static int value_list_to_filename (char *buffer, size_t buffer_size,
123     value_list_t const *vl)
124 {
125   char const suffix[] = ".rrd";
126   int status;
127   size_t len;
128
129   if (datadir != NULL)
130   {
131     size_t datadir_len = strlen (datadir) + 1;
132
133     if (datadir_len >= buffer_size)
134       return (ENOMEM);
135
136     sstrncpy (buffer, datadir, buffer_size);
137     buffer[datadir_len - 1] = '/';
138     buffer[datadir_len] = 0;
139
140     buffer += datadir_len;
141     buffer_size -= datadir_len;
142   }
143
144   status = FORMAT_VL (buffer, buffer_size, vl);
145   if (status != 0)
146     return (status);
147
148   len = strlen (buffer);
149   assert (len < buffer_size);
150   buffer += len;
151   buffer_size -= len;
152
153   if (buffer_size <= sizeof (suffix))
154     return (ENOMEM);
155
156   memcpy (buffer, suffix, sizeof (suffix));
157   return (0);
158 } /* int value_list_to_filename */
159
160 static int rc_config_get_int_positive (oconfig_item_t const *ci, int *ret)
161 {
162   int status;
163   int tmp = 0;
164
165   status = cf_util_get_int (ci, &tmp);
166   if (status != 0)
167     return (status);
168   if (tmp < 0)
169     return (EINVAL);
170
171   *ret = tmp;
172   return (0);
173 } /* int rc_config_get_int_positive */
174
175 static int rc_config_get_xff (oconfig_item_t const *ci, double *ret)
176 {
177   double value;
178
179   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
180   {
181     ERROR ("rrdcached plugin: The \"%s\" needs exactly one numeric argument "
182         "in the range [0.0, 1.0)", ci->key);
183     return (EINVAL);
184   }
185
186   value = ci->values[0].value.number;
187   if ((value >= 0.0) && (value < 1.0))
188   {
189     *ret = value;
190     return (0);
191   }
192
193   ERROR ("rrdcached plugin: The \"%s\" needs exactly one numeric argument "
194       "in the range [0.0, 1.0)", ci->key);
195   return (EINVAL);
196 } /* int rc_config_get_xff */
197
198 static int rc_config_add_timespan (int timespan)
199 {
200   int *tmp;
201
202   if (timespan <= 0)
203     return (EINVAL);
204
205   tmp = realloc (rrdcreate_config.timespans,
206       sizeof (*rrdcreate_config.timespans)
207       * (rrdcreate_config.timespans_num + 1));
208   if (tmp == NULL)
209     return (ENOMEM);
210   rrdcreate_config.timespans = tmp;
211
212   rrdcreate_config.timespans[rrdcreate_config.timespans_num] = timespan;
213   rrdcreate_config.timespans_num++;
214
215   return (0);
216 } /* int rc_config_add_timespan */
217
218 static int rc_config (oconfig_item_t *ci)
219 {
220   int i;
221
222   for (i = 0; i < ci->children_num; i++)
223   {
224     oconfig_item_t const *child = ci->children + i;
225     const char *key = child->key;
226     int status = 0;
227
228     if (strcasecmp ("DataDir", key) == 0)
229     {
230       status = cf_util_get_string (child, &datadir);
231       if (status == 0)
232       {
233         int len = strlen (datadir);
234
235         while ((len > 0) && (datadir[len - 1] == '/'))
236         {
237           len--;
238           datadir[len] = 0;
239         }
240
241         if (len <= 0)
242           sfree (datadir);
243       }
244     }
245     else if (strcasecmp ("DaemonAddress", key) == 0)
246       status = cf_util_get_string (child, &daemon_address);
247     else if (strcasecmp ("CreateFiles", key) == 0)
248       status = cf_util_get_boolean (child, &config_create_files);
249     else if (strcasecmp ("CreateFilesAsync", key) == 0)
250       status = cf_util_get_boolean (child, &rrdcreate_config.async);
251     else if (strcasecmp ("CollectStatistics", key) == 0)
252       status = cf_util_get_boolean (child, &config_collect_stats);
253     else if (strcasecmp ("StepSize", key) == 0)
254     {
255       int tmp = -1;
256
257       status = rc_config_get_int_positive (child, &tmp);
258       if (status == 0)
259         rrdcreate_config.stepsize = (unsigned long) tmp;
260     }
261     else if (strcasecmp ("HeartBeat", key) == 0)
262       status = rc_config_get_int_positive (child, &rrdcreate_config.heartbeat);
263     else if (strcasecmp ("RRARows", key) == 0)
264       status = rc_config_get_int_positive (child, &rrdcreate_config.rrarows);
265     else if (strcasecmp ("RRATimespan", key) == 0)
266     {
267       int tmp = -1;
268       status = rc_config_get_int_positive (child, &tmp);
269       if (status == 0)
270         status = rc_config_add_timespan (tmp);
271     }
272     else if (strcasecmp ("XFF", key) == 0)
273       status = rc_config_get_xff (child, &rrdcreate_config.xff);
274     else
275     {
276       WARNING ("rrdcached plugin: Ignoring invalid option %s.", key);
277       continue;
278     }
279
280     if (status != 0)
281       WARNING ("rrdcached plugin: Handling the \"%s\" option failed.", key);
282   }
283
284   if (daemon_address != NULL)
285   {
286     plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
287     plugin_register_flush ("rrdcached", rc_flush, /* user_data = */ NULL);
288   }
289   return (0);
290 } /* int rc_config */
291
292 static int try_reconnect (void)
293 {
294   int status;
295
296   rrdc_disconnect ();
297
298   rrd_clear_error ();
299   status = rrdc_connect (daemon_address);
300   if (status != 0)
301   {
302     ERROR ("rrdcached plugin: Failed to reconnect to RRDCacheD "
303         "at %s: %s (status=%d)", daemon_address, rrd_get_error (), status);
304     return (-1);
305   }
306
307   INFO ("rrdcached plugin: Successfully reconnected to RRDCacheD "
308       "at %s", daemon_address);
309   return (0);
310 } /* int try_reconnect */
311
312 static int rc_read (void)
313 {
314   int status;
315   rrdc_stats_t *head;
316   rrdc_stats_t *ptr;
317   _Bool retried = 0;
318
319   value_t values[1];
320   value_list_t vl = VALUE_LIST_INIT;
321
322   if (daemon_address == NULL)
323     return (-1);
324
325   if (!config_collect_stats)
326     return (-1);
327
328   vl.values = values;
329   vl.values_len = 1;
330
331   if ((strncmp ("unix:", daemon_address, strlen ("unix:")) == 0)
332       || (daemon_address[0] == '/'))
333     sstrncpy (vl.host, hostname_g, sizeof (vl.host));
334   else
335     sstrncpy (vl.host, daemon_address, sizeof (vl.host));
336   sstrncpy (vl.plugin, "rrdcached", sizeof (vl.plugin));
337
338   rrd_clear_error ();
339   status = rrdc_connect (daemon_address);
340   if (status != 0)
341   {
342     ERROR ("rrdcached plugin: Failed to connect to RRDCacheD "
343         "at %s: %s (status=%d)", daemon_address, rrd_get_error (), status);
344     return (-1);
345   }
346
347   while (42)
348   {
349     /* The RRD client lib does not provide any means for checking a
350      * connection, hence we'll have to retry upon failed operations. */
351     head = NULL;
352     rrd_clear_error ();
353     status = rrdc_stats_get (&head);
354     if (status == 0)
355       break;
356
357     if (! retried)
358     {
359       retried = 1;
360       if (try_reconnect () == 0)
361         continue;
362       /* else: report the error and fail */
363     }
364
365     ERROR ("rrdcached plugin: rrdc_stats_get failed: %s (status=%i).",
366         rrd_get_error (), status);
367     return (-1);
368   }
369
370   for (ptr = head; ptr != NULL; ptr = ptr->next)
371   {
372     if (ptr->type == RRDC_STATS_TYPE_GAUGE)
373       values[0].gauge = (gauge_t) ptr->value.gauge;
374     else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
375       values[0].counter = (counter_t) ptr->value.counter;
376     else
377       continue;
378
379     if (strcasecmp ("QueueLength", ptr->name) == 0)
380     {
381       sstrncpy (vl.type, "queue_length", sizeof (vl.type));
382       sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
383     }
384     else if (strcasecmp ("UpdatesWritten", ptr->name) == 0)
385     {
386       sstrncpy (vl.type, "operations", sizeof (vl.type));
387       sstrncpy (vl.type_instance, "write-updates", sizeof (vl.type_instance));
388     }
389     else if (strcasecmp ("DataSetsWritten", ptr->name) == 0)
390     {
391       sstrncpy (vl.type, "operations", sizeof (vl.type));
392       sstrncpy (vl.type_instance, "write-data_sets",
393           sizeof (vl.type_instance));
394     }
395     else if (strcasecmp ("TreeNodesNumber", ptr->name) == 0)
396     {
397       sstrncpy (vl.type, "gauge", sizeof (vl.type));
398       sstrncpy (vl.type_instance, "tree_nodes", sizeof (vl.type_instance));
399     }
400     else if (strcasecmp ("TreeDepth", ptr->name) == 0)
401     {
402       sstrncpy (vl.type, "gauge", sizeof (vl.type));
403       sstrncpy (vl.type_instance, "tree_depth", sizeof (vl.type_instance));
404     }
405     else if (strcasecmp ("FlushesReceived", ptr->name) == 0)
406     {
407       sstrncpy (vl.type, "operations", sizeof (vl.type));
408       sstrncpy (vl.type_instance, "receive-flush", sizeof (vl.type_instance));
409     }
410     else if (strcasecmp ("JournalBytes", ptr->name) == 0)
411     {
412       sstrncpy (vl.type, "counter", sizeof (vl.type));
413       sstrncpy (vl.type_instance, "journal-bytes", sizeof (vl.type_instance));
414     }
415     else if (strcasecmp ("JournalRotate", ptr->name) == 0)
416     {
417       sstrncpy (vl.type, "counter", sizeof (vl.type));
418       sstrncpy (vl.type_instance, "journal-rotates", sizeof (vl.type_instance));
419     }
420     else if (strcasecmp ("UpdatesReceived", ptr->name) == 0)
421     {
422       sstrncpy (vl.type, "operations", sizeof (vl.type));
423       sstrncpy (vl.type_instance, "receive-update", sizeof (vl.type_instance));
424     }
425     else
426     {
427       DEBUG ("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
428       continue;
429     }
430
431     plugin_dispatch_values (&vl);
432   } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
433
434   rrdc_stats_free (head);
435
436   return (0);
437 } /* int rc_read */
438
439 static int rc_init (void)
440 {
441   if (config_collect_stats)
442     plugin_register_read ("rrdcached", rc_read);
443
444   return (0);
445 } /* int rc_init */
446
447 static int rc_write (const data_set_t *ds, const value_list_t *vl,
448     user_data_t __attribute__((unused)) *user_data)
449 {
450   char filename[PATH_MAX];
451   char values[512];
452   char *values_array[2];
453   int status;
454   _Bool retried = 0;
455
456   if (daemon_address == NULL)
457   {
458     ERROR ("rrdcached plugin: daemon_address == NULL.");
459     plugin_unregister_write ("rrdcached");
460     return (-1);
461   }
462
463   if (strcmp (ds->type, vl->type) != 0)
464   {
465     ERROR ("rrdcached plugin: DS type does not match value list type");
466     return (-1);
467   }
468
469   if (value_list_to_filename (filename, sizeof (filename), vl) != 0)
470   {
471     ERROR ("rrdcached plugin: value_list_to_filename failed.");
472     return (-1);
473   }
474
475   if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
476   {
477     ERROR ("rrdcached plugin: value_list_to_string failed.");
478     return (-1);
479   }
480
481   values_array[0] = values;
482   values_array[1] = NULL;
483
484   if (config_create_files)
485   {
486     struct stat statbuf;
487
488     status = stat (filename, &statbuf);
489     if (status != 0)
490     {
491       if (errno != ENOENT)
492       {
493         char errbuf[1024];
494         ERROR ("rrdcached plugin: stat (%s) failed: %s",
495             filename, sstrerror (errno, errbuf, sizeof (errbuf)));
496         return (-1);
497       }
498
499       status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
500       if (status != 0)
501       {
502         ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
503             filename);
504         return (-1);
505       }
506       else if (rrdcreate_config.async)
507         return (0);
508     }
509   }
510
511   rrd_clear_error ();
512   status = rrdc_connect (daemon_address);
513   if (status != 0)
514   {
515     ERROR ("rrdcached plugin: Failed to connect to RRDCacheD "
516         "at %s: %s (status=%d)", daemon_address, rrd_get_error (), status);
517     return (-1);
518   }
519
520   while (42)
521   {
522     /* The RRD client lib does not provide any means for checking a
523      * connection, hence we'll have to retry upon failed operations. */
524     rrd_clear_error ();
525     status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
526     if (status == 0)
527       break;
528
529     if (! retried)
530     {
531       retried = 1;
532       if (try_reconnect () == 0)
533         continue;
534       /* else: report the error and fail */
535     }
536
537     ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed: %s (status=%i)",
538         filename, values_array[0], rrd_get_error (), status);
539     return (-1);
540   }
541
542   return (0);
543 } /* int rc_write */
544
545 static int rc_flush (__attribute__((unused)) cdtime_t timeout, /* {{{ */
546     const char *identifier,
547     __attribute__((unused)) user_data_t *ud)
548 {
549   char filename[PATH_MAX + 1];
550   int status;
551   _Bool retried = 0;
552
553   if (identifier == NULL)
554     return (EINVAL);
555
556   if (datadir != NULL)
557     ssnprintf (filename, sizeof (filename), "%s/%s.rrd", datadir, identifier);
558   else
559     ssnprintf (filename, sizeof (filename), "%s.rrd", identifier);
560
561   rrd_clear_error ();
562   status = rrdc_connect (daemon_address);
563   if (status != 0)
564   {
565     ERROR ("rrdcached plugin: Failed to connect to RRDCacheD "
566         "at %s: %s (status=%d)", daemon_address, rrd_get_error (), status);
567     return (-1);
568   }
569
570   while (42)
571   {
572     /* The RRD client lib does not provide any means for checking a
573      * connection, hence we'll have to retry upon failed operations. */
574     rrd_clear_error ();
575     status = rrdc_flush (filename);
576     if (status == 0)
577       break;
578
579     if (! retried)
580     {
581       retried = 1;
582       if (try_reconnect () == 0)
583         continue;
584       /* else: report the error and fail */
585     }
586
587     ERROR ("rrdcached plugin: rrdc_flush (%s) failed: %s (status=%i).",
588         filename, rrd_get_error (), status);
589     return (-1);
590   }
591   DEBUG ("rrdcached plugin: rrdc_flush (%s): Success.", filename);
592
593   return (0);
594 } /* }}} int rc_flush */
595
596 static int rc_shutdown (void)
597 {
598   rrdc_disconnect ();
599   return (0);
600 } /* int rc_shutdown */
601
602 void module_register (void)
603 {
604   plugin_register_complex_config ("rrdcached", rc_config);
605   plugin_register_init ("rrdcached", rc_init);
606   plugin_register_shutdown ("rrdcached", rc_shutdown);
607 } /* void module_register */
608
609 /*
610  * vim: set sw=2 sts=2 et :
611  */