Merge pull request #2618 from ajssmith/amqp1_dev1_branch
[collectd.git] / src / write_riemann.c
1 /**
2  * collectd - src/write_riemann.c
3  * Copyright (C) 2012,2013  Pierre-Yves Ritschard
4  * Copyright (C) 2013       Florian octo Forster
5  * Copyright (C) 2015,2016  Gergely Nagy
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *   Pierre-Yves Ritschard <pyr at spootnik.org>
27  *   Florian octo Forster <octo at collectd.org>
28  *   Gergely Nagy <algernon at madhouse-project.org>
29  */
30
31 #include "collectd.h"
32
33 #include "common.h"
34 #include "plugin.h"
35 #include "utils_cache.h"
36 #include "utils_complain.h"
37 #include "write_riemann_threshold.h"
38
39 #include <riemann/riemann-client.h>
40
41 #define RIEMANN_HOST "localhost"
42 #define RIEMANN_PORT 5555
43 #define RIEMANN_TTL_FACTOR 2.0
44 #define RIEMANN_BATCH_MAX 8192
45
46 struct riemann_host {
47   c_complain_t init_complaint;
48   char *name;
49   char *event_service_prefix;
50   pthread_mutex_t lock;
51   bool batch_mode;
52   bool notifications;
53   bool check_thresholds;
54   bool store_rates;
55   bool always_append_ds;
56   char *node;
57   int port;
58   riemann_client_type_t client_type;
59   riemann_client_t *client;
60   double ttl_factor;
61   cdtime_t batch_init;
62   int batch_max;
63   int batch_timeout;
64   int reference_count;
65   riemann_message_t *batch_msg;
66   char *tls_ca_file;
67   char *tls_cert_file;
68   char *tls_key_file;
69   struct timeval timeout;
70 };
71
72 static char **riemann_tags;
73 static size_t riemann_tags_num;
74 static char **riemann_attrs;
75 static size_t riemann_attrs_num;
76
77 /* host->lock must be held when calling this function. */
78 static int wrr_connect(struct riemann_host *host) /* {{{ */
79 {
80   char const *node;
81   int port;
82
83   if (host->client)
84     return 0;
85
86   node = (host->node != NULL) ? host->node : RIEMANN_HOST;
87   port = (host->port) ? host->port : RIEMANN_PORT;
88
89   host->client = NULL;
90
91   host->client = riemann_client_create(
92       host->client_type, node, port, RIEMANN_CLIENT_OPTION_TLS_CA_FILE,
93       host->tls_ca_file, RIEMANN_CLIENT_OPTION_TLS_CERT_FILE,
94       host->tls_cert_file, RIEMANN_CLIENT_OPTION_TLS_KEY_FILE,
95       host->tls_key_file, RIEMANN_CLIENT_OPTION_NONE);
96   if (host->client == NULL) {
97     c_complain(LOG_ERR, &host->init_complaint,
98                "write_riemann plugin: Unable to connect to Riemann at %s:%d",
99                node, port);
100     return -1;
101   }
102 #if RCC_VERSION_NUMBER >= 0x010800
103   if (host->timeout.tv_sec != 0) {
104     if (riemann_client_set_timeout(host->client, &host->timeout) != 0) {
105       riemann_client_free(host->client);
106       host->client = NULL;
107       c_complain(LOG_ERR, &host->init_complaint,
108                  "write_riemann plugin: Unable to connect to Riemann at %s:%d",
109                  node, port);
110       return -1;
111     }
112   }
113 #endif
114
115   set_sock_opts(riemann_client_get_fd(host->client));
116
117   c_release(LOG_INFO, &host->init_complaint,
118             "write_riemann plugin: Successfully connected to %s:%d", node,
119             port);
120
121   return 0;
122 } /* }}} int wrr_connect */
123
124 /* host->lock must be held when calling this function. */
125 static int wrr_disconnect(struct riemann_host *host) /* {{{ */
126 {
127   if (!host->client)
128     return 0;
129
130   riemann_client_free(host->client);
131   host->client = NULL;
132
133   return 0;
134 } /* }}} int wrr_disconnect */
135
136 /**
137  * Function to send messages to riemann.
138  *
139  * Acquires the host lock, disconnects on errors.
140  */
141 static int wrr_send_nolock(struct riemann_host *host,
142                            riemann_message_t *msg) /* {{{ */
143 {
144   int status = 0;
145
146   status = wrr_connect(host);
147   if (status != 0) {
148     return status;
149   }
150
151   status = riemann_client_send_message(host->client, msg);
152   if (status != 0) {
153     wrr_disconnect(host);
154     return status;
155   }
156
157   /*
158    * For TCP we need to receive message acknowledgemenent.
159    */
160   if (host->client_type != RIEMANN_CLIENT_UDP) {
161     riemann_message_t *response;
162
163     response = riemann_client_recv_message(host->client);
164
165     if (response == NULL) {
166       wrr_disconnect(host);
167       return errno;
168     }
169     riemann_message_free(response);
170   }
171
172   return 0;
173 } /* }}} int wrr_send */
174
175 static int wrr_send(struct riemann_host *host, riemann_message_t *msg) {
176   int status = 0;
177
178   pthread_mutex_lock(&host->lock);
179   status = wrr_send_nolock(host, msg);
180   pthread_mutex_unlock(&host->lock);
181   return status;
182 }
183
184 static riemann_message_t *wrr_notification_to_message(notification_t const *n) {
185   riemann_message_t *msg;
186   riemann_event_t *event;
187   char service_buffer[6 * DATA_MAX_NAME_LEN];
188   char const *severity;
189
190   switch (n->severity) {
191   case NOTIF_OKAY:
192     severity = "ok";
193     break;
194   case NOTIF_WARNING:
195     severity = "warning";
196     break;
197   case NOTIF_FAILURE:
198     severity = "critical";
199     break;
200   default:
201     severity = "unknown";
202   }
203
204   format_name(service_buffer, sizeof(service_buffer),
205               /* host = */ "", n->plugin, n->plugin_instance, n->type,
206               n->type_instance);
207
208   event = riemann_event_create(
209       RIEMANN_EVENT_FIELD_HOST, n->host, RIEMANN_EVENT_FIELD_TIME,
210       (int64_t)CDTIME_T_TO_TIME_T(n->time), RIEMANN_EVENT_FIELD_TAGS,
211       "notification", NULL, RIEMANN_EVENT_FIELD_STATE, severity,
212       RIEMANN_EVENT_FIELD_SERVICE, &service_buffer[1],
213       RIEMANN_EVENT_FIELD_NONE);
214
215 #if RCC_VERSION_NUMBER >= 0x010A00
216   riemann_event_set(event, RIEMANN_EVENT_FIELD_TIME_MICROS,
217                     (int64_t)CDTIME_T_TO_US(n->time));
218 #endif
219
220   if (n->host[0] != 0)
221     riemann_event_string_attribute_add(event, "host", n->host);
222   if (n->plugin[0] != 0)
223     riemann_event_string_attribute_add(event, "plugin", n->plugin);
224   if (n->plugin_instance[0] != 0)
225     riemann_event_string_attribute_add(event, "plugin_instance",
226                                        n->plugin_instance);
227
228   if (n->type[0] != 0)
229     riemann_event_string_attribute_add(event, "type", n->type);
230   if (n->type_instance[0] != 0)
231     riemann_event_string_attribute_add(event, "type_instance",
232                                        n->type_instance);
233
234   for (size_t i = 0; i < riemann_attrs_num; i += 2)
235     riemann_event_string_attribute_add(event, riemann_attrs[i],
236                                        riemann_attrs[i + 1]);
237
238   for (size_t i = 0; i < riemann_tags_num; i++)
239     riemann_event_tag_add(event, riemann_tags[i]);
240
241   if (n->message[0] != 0)
242     riemann_event_string_attribute_add(event, "description", n->message);
243
244   /* Pull in values from threshold and add extra attributes */
245   for (notification_meta_t *meta = n->meta; meta != NULL; meta = meta->next) {
246     if (strcasecmp("CurrentValue", meta->name) == 0 &&
247         meta->type == NM_TYPE_DOUBLE) {
248       riemann_event_set(event, RIEMANN_EVENT_FIELD_METRIC_D,
249                         (double)meta->nm_value.nm_double,
250                         RIEMANN_EVENT_FIELD_NONE);
251       continue;
252     }
253
254     if (meta->type == NM_TYPE_STRING) {
255       riemann_event_string_attribute_add(event, meta->name,
256                                          meta->nm_value.nm_string);
257       continue;
258     }
259   }
260
261   msg = riemann_message_create_with_events(event, NULL);
262   if (msg == NULL) {
263     ERROR("write_riemann plugin: riemann_message_create_with_events() failed.");
264     riemann_event_free(event);
265     return NULL;
266   }
267
268   DEBUG("write_riemann plugin: Successfully created message for notification: "
269         "host = \"%s\", service = \"%s\", state = \"%s\"",
270         event->host, event->service, event->state);
271   return msg;
272 }
273
274 static riemann_event_t *
275 wrr_value_to_event(struct riemann_host const *host, /* {{{ */
276                    data_set_t const *ds, value_list_t const *vl, size_t index,
277                    gauge_t const *rates, int status) {
278   riemann_event_t *event;
279   char name_buffer[5 * DATA_MAX_NAME_LEN];
280   char service_buffer[6 * DATA_MAX_NAME_LEN];
281   size_t i;
282
283   event = riemann_event_new();
284   if (event == NULL) {
285     ERROR("write_riemann plugin: riemann_event_new() failed.");
286     return NULL;
287   }
288
289   format_name(name_buffer, sizeof(name_buffer),
290               /* host = */ "", vl->plugin, vl->plugin_instance, vl->type,
291               vl->type_instance);
292   if (host->always_append_ds || (ds->ds_num > 1)) {
293     if (host->event_service_prefix == NULL)
294       snprintf(service_buffer, sizeof(service_buffer), "%s/%s", &name_buffer[1],
295                ds->ds[index].name);
296     else
297       snprintf(service_buffer, sizeof(service_buffer), "%s%s/%s",
298                host->event_service_prefix, &name_buffer[1], ds->ds[index].name);
299   } else {
300     if (host->event_service_prefix == NULL)
301       sstrncpy(service_buffer, &name_buffer[1], sizeof(service_buffer));
302     else
303       snprintf(service_buffer, sizeof(service_buffer), "%s%s",
304                host->event_service_prefix, &name_buffer[1]);
305   }
306
307   riemann_event_set(
308       event, RIEMANN_EVENT_FIELD_HOST, vl->host, RIEMANN_EVENT_FIELD_TIME,
309       (int64_t)CDTIME_T_TO_TIME_T(vl->time), RIEMANN_EVENT_FIELD_TTL,
310       (float)CDTIME_T_TO_DOUBLE(vl->interval) * host->ttl_factor,
311       RIEMANN_EVENT_FIELD_STRING_ATTRIBUTES, "plugin", vl->plugin, "type",
312       vl->type, "ds_name", ds->ds[index].name, NULL,
313       RIEMANN_EVENT_FIELD_SERVICE, service_buffer, RIEMANN_EVENT_FIELD_NONE);
314
315 #if RCC_VERSION_NUMBER >= 0x010A00
316   riemann_event_set(event, RIEMANN_EVENT_FIELD_TIME_MICROS,
317                     (int64_t)CDTIME_T_TO_US(vl->time));
318 #endif
319
320   if (host->check_thresholds) {
321     const char *state = NULL;
322
323     switch (status) {
324     case STATE_OKAY:
325       state = "ok";
326       break;
327     case STATE_ERROR:
328       state = "critical";
329       break;
330     case STATE_WARNING:
331       state = "warning";
332       break;
333     case STATE_MISSING:
334       state = "unknown";
335       break;
336     }
337     if (state)
338       riemann_event_set(event, RIEMANN_EVENT_FIELD_STATE, state,
339                         RIEMANN_EVENT_FIELD_NONE);
340   }
341
342   if (vl->plugin_instance[0] != 0)
343     riemann_event_string_attribute_add(event, "plugin_instance",
344                                        vl->plugin_instance);
345   if (vl->type_instance[0] != 0)
346     riemann_event_string_attribute_add(event, "type_instance",
347                                        vl->type_instance);
348
349   if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL)) {
350     char ds_type[DATA_MAX_NAME_LEN];
351
352     snprintf(ds_type, sizeof(ds_type), "%s:rate",
353              DS_TYPE_TO_STRING(ds->ds[index].type));
354     riemann_event_string_attribute_add(event, "ds_type", ds_type);
355   } else {
356     riemann_event_string_attribute_add(event, "ds_type",
357                                        DS_TYPE_TO_STRING(ds->ds[index].type));
358   }
359
360   {
361     char ds_index[DATA_MAX_NAME_LEN];
362
363     snprintf(ds_index, sizeof(ds_index), "%" PRIsz, index);
364     riemann_event_string_attribute_add(event, "ds_index", ds_index);
365   }
366
367   for (i = 0; i < riemann_attrs_num; i += 2)
368     riemann_event_string_attribute_add(event, riemann_attrs[i],
369                                        riemann_attrs[i + 1]);
370
371   for (i = 0; i < riemann_tags_num; i++)
372     riemann_event_tag_add(event, riemann_tags[i]);
373
374   if (ds->ds[index].type == DS_TYPE_GAUGE) {
375     riemann_event_set(event, RIEMANN_EVENT_FIELD_METRIC_D,
376                       (double)vl->values[index].gauge,
377                       RIEMANN_EVENT_FIELD_NONE);
378   } else if (rates != NULL) {
379     riemann_event_set(event, RIEMANN_EVENT_FIELD_METRIC_D, (double)rates[index],
380                       RIEMANN_EVENT_FIELD_NONE);
381   } else {
382     int64_t metric;
383
384     if (ds->ds[index].type == DS_TYPE_DERIVE)
385       metric = (int64_t)vl->values[index].derive;
386     else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
387       metric = (int64_t)vl->values[index].absolute;
388     else
389       metric = (int64_t)vl->values[index].counter;
390
391     riemann_event_set(event, RIEMANN_EVENT_FIELD_METRIC_S64, (int64_t)metric,
392                       RIEMANN_EVENT_FIELD_NONE);
393   }
394
395   DEBUG("write_riemann plugin: Successfully created message for metric: "
396         "host = \"%s\", service = \"%s\"",
397         event->host, event->service);
398   return event;
399 } /* }}} riemann_event_t *wrr_value_to_event */
400
401 static riemann_message_t *
402 wrr_value_list_to_message(struct riemann_host const *host, /* {{{ */
403                           data_set_t const *ds, value_list_t const *vl,
404                           int *statuses) {
405   riemann_message_t *msg;
406   size_t i;
407   gauge_t *rates = NULL;
408
409   /* Initialize the Msg structure. */
410   msg = riemann_message_new();
411   if (msg == NULL) {
412     ERROR("write_riemann plugin: riemann_message_new failed.");
413     return NULL;
414   }
415
416   if (host->store_rates) {
417     rates = uc_get_rate(ds, vl);
418     if (rates == NULL) {
419       ERROR("write_riemann plugin: uc_get_rate failed.");
420       riemann_message_free(msg);
421       return NULL;
422     }
423   }
424
425   for (i = 0; i < vl->values_len; i++) {
426     riemann_event_t *event;
427
428     event = wrr_value_to_event(host, ds, vl, (int)i, rates, statuses[i]);
429     if (event == NULL) {
430       riemann_message_free(msg);
431       sfree(rates);
432       return NULL;
433     }
434     riemann_message_append_events(msg, event, NULL);
435   }
436
437   sfree(rates);
438   return msg;
439 } /* }}} riemann_message_t *wrr_value_list_to_message */
440
441 /*
442  * Always call while holding host->lock !
443  */
444 static int wrr_batch_flush_nolock(cdtime_t timeout, struct riemann_host *host) {
445   cdtime_t now;
446   int status = 0;
447
448   now = cdtime();
449   if (timeout > 0) {
450     if ((host->batch_init + timeout) > now) {
451       return status;
452     }
453   }
454   wrr_send_nolock(host, host->batch_msg);
455   riemann_message_free(host->batch_msg);
456
457   host->batch_init = now;
458   host->batch_msg = NULL;
459   return status;
460 }
461
462 static int wrr_batch_flush(cdtime_t timeout,
463                            const char *identifier __attribute__((unused)),
464                            user_data_t *user_data) {
465   struct riemann_host *host;
466   int status;
467
468   if (user_data == NULL)
469     return -EINVAL;
470
471   host = user_data->data;
472   pthread_mutex_lock(&host->lock);
473   status = wrr_batch_flush_nolock(timeout, host);
474   if (status != 0)
475     c_complain(
476         LOG_ERR, &host->init_complaint,
477         "write_riemann plugin: riemann_client_send failed with status %i",
478         status);
479   else
480     c_release(LOG_DEBUG, &host->init_complaint,
481               "write_riemann plugin: batch sent.");
482
483   pthread_mutex_unlock(&host->lock);
484   return status;
485 }
486
487 static int wrr_batch_add_value_list(struct riemann_host *host, /* {{{ */
488                                     data_set_t const *ds,
489                                     value_list_t const *vl, int *statuses) {
490   riemann_message_t *msg;
491   size_t len;
492   int ret;
493   cdtime_t timeout;
494
495   msg = wrr_value_list_to_message(host, ds, vl, statuses);
496   if (msg == NULL)
497     return -1;
498
499   pthread_mutex_lock(&host->lock);
500
501   if (host->batch_msg == NULL) {
502     host->batch_msg = msg;
503   } else {
504     int status;
505
506     status = riemann_message_append_events_n(host->batch_msg, msg->n_events,
507                                              msg->events);
508     msg->n_events = 0;
509     msg->events = NULL;
510
511     riemann_message_free(msg);
512
513     if (status != 0) {
514       pthread_mutex_unlock(&host->lock);
515       ERROR("write_riemann plugin: out of memory");
516       return -1;
517     }
518   }
519
520   len = riemann_message_get_packed_size(host->batch_msg);
521   ret = 0;
522   if ((host->batch_max < 0) || (((size_t)host->batch_max) <= len)) {
523     ret = wrr_batch_flush_nolock(0, host);
524   } else {
525     if (host->batch_timeout > 0) {
526       timeout = TIME_T_TO_CDTIME_T((time_t)host->batch_timeout);
527       ret = wrr_batch_flush_nolock(timeout, host);
528     }
529   }
530
531   pthread_mutex_unlock(&host->lock);
532   return ret;
533 } /* }}} riemann_message_t *wrr_batch_add_value_list */
534
535 static int wrr_notification(const notification_t *n, user_data_t *ud) /* {{{ */
536 {
537   int status;
538   struct riemann_host *host = ud->data;
539   riemann_message_t *msg;
540
541   if (!host->notifications)
542     return 0;
543
544   /*
545    * Never batch for notifications, send them ASAP
546    */
547   msg = wrr_notification_to_message(n);
548   if (msg == NULL)
549     return -1;
550
551   status = wrr_send(host, msg);
552   if (status != 0)
553     c_complain(
554         LOG_ERR, &host->init_complaint,
555         "write_riemann plugin: riemann_client_send failed with status %i",
556         status);
557   else
558     c_release(LOG_DEBUG, &host->init_complaint,
559               "write_riemann plugin: riemann_client_send succeeded");
560
561   riemann_message_free(msg);
562   return status;
563 } /* }}} int wrr_notification */
564
565 static int wrr_write(const data_set_t *ds, /* {{{ */
566                      const value_list_t *vl, user_data_t *ud) {
567   int status = 0;
568   int statuses[vl->values_len];
569   struct riemann_host *host = ud->data;
570   riemann_message_t *msg;
571
572   if (host->check_thresholds) {
573     status = write_riemann_threshold_check(ds, vl, statuses);
574     if (status != 0)
575       return status;
576   } else {
577     memset(statuses, 0, sizeof(statuses));
578   }
579
580   if (host->client_type != RIEMANN_CLIENT_UDP && host->batch_mode) {
581     wrr_batch_add_value_list(host, ds, vl, statuses);
582   } else {
583     msg = wrr_value_list_to_message(host, ds, vl, statuses);
584     if (msg == NULL)
585       return -1;
586
587     status = wrr_send(host, msg);
588
589     riemann_message_free(msg);
590   }
591   return status;
592 } /* }}} int wrr_write */
593
594 static void wrr_free(void *p) /* {{{ */
595 {
596   struct riemann_host *host = p;
597
598   if (host == NULL)
599     return;
600
601   pthread_mutex_lock(&host->lock);
602
603   host->reference_count--;
604   if (host->reference_count > 0) {
605     pthread_mutex_unlock(&host->lock);
606     return;
607   }
608
609   wrr_disconnect(host);
610
611   pthread_mutex_lock(&host->lock);
612   pthread_mutex_destroy(&host->lock);
613   sfree(host);
614 } /* }}} void wrr_free */
615
616 static int wrr_config_node(oconfig_item_t *ci) /* {{{ */
617 {
618   struct riemann_host *host = NULL;
619   int status = 0;
620   int i;
621   oconfig_item_t *child;
622   char callback_name[DATA_MAX_NAME_LEN];
623
624   if ((host = calloc(1, sizeof(*host))) == NULL) {
625     ERROR("write_riemann plugin: calloc failed.");
626     return ENOMEM;
627   }
628   pthread_mutex_init(&host->lock, NULL);
629   C_COMPLAIN_INIT(&host->init_complaint);
630   host->reference_count = 1;
631   host->node = NULL;
632   host->port = 0;
633   host->notifications = true;
634   host->check_thresholds = false;
635   host->store_rates = true;
636   host->always_append_ds = false;
637   host->batch_mode = true;
638   host->batch_max = RIEMANN_BATCH_MAX; /* typical MSS */
639   host->batch_init = cdtime();
640   host->batch_timeout = 0;
641   host->ttl_factor = RIEMANN_TTL_FACTOR;
642   host->client = NULL;
643   host->client_type = RIEMANN_CLIENT_TCP;
644   host->timeout.tv_sec = 0;
645   host->timeout.tv_usec = 0;
646
647   status = cf_util_get_string(ci, &host->name);
648   if (status != 0) {
649     WARNING("write_riemann plugin: Required host name is missing.");
650     wrr_free(host);
651     return -1;
652   }
653
654   for (i = 0; i < ci->children_num; i++) {
655     /*
656      * The code here could be simplified but makes room
657      * for easy adding of new options later on.
658      */
659     child = &ci->children[i];
660     status = 0;
661
662     if (strcasecmp("Host", child->key) == 0) {
663       status = cf_util_get_string(child, &host->node);
664       if (status != 0)
665         break;
666     } else if (strcasecmp("Notifications", child->key) == 0) {
667       status = cf_util_get_boolean(child, &host->notifications);
668       if (status != 0)
669         break;
670     } else if (strcasecmp("EventServicePrefix", child->key) == 0) {
671       status = cf_util_get_string(child, &host->event_service_prefix);
672       if (status != 0)
673         break;
674     } else if (strcasecmp("CheckThresholds", child->key) == 0) {
675       status = cf_util_get_boolean(child, &host->check_thresholds);
676       if (status != 0)
677         break;
678     } else if (strcasecmp("Batch", child->key) == 0) {
679       status = cf_util_get_boolean(child, &host->batch_mode);
680       if (status != 0)
681         break;
682     } else if (strcasecmp("BatchMaxSize", child->key) == 0) {
683       status = cf_util_get_int(child, &host->batch_max);
684       if (status != 0)
685         break;
686     } else if (strcasecmp("BatchFlushTimeout", child->key) == 0) {
687       status = cf_util_get_int(child, &host->batch_timeout);
688       if (status != 0)
689         break;
690     } else if (strcasecmp("Timeout", child->key) == 0) {
691 #if RCC_VERSION_NUMBER >= 0x010800
692       status = cf_util_get_int(child, (int *)&host->timeout.tv_sec);
693       if (status != 0)
694         break;
695 #else
696       WARNING("write_riemann plugin: The Timeout option is not supported. "
697               "Please upgrade the Riemann client to at least 1.8.0.");
698 #endif
699     } else if (strcasecmp("Port", child->key) == 0) {
700       host->port = cf_util_get_port_number(child);
701       if (host->port == -1) {
702         ERROR("write_riemann plugin: Invalid argument "
703               "configured for the \"Port\" "
704               "option.");
705         break;
706       }
707     } else if (strcasecmp("Protocol", child->key) == 0) {
708       char tmp[16];
709       status = cf_util_get_string_buffer(child, tmp, sizeof(tmp));
710       if (status != 0) {
711         ERROR("write_riemann plugin: cf_util_get_"
712               "string_buffer failed with "
713               "status %i.",
714               status);
715         break;
716       }
717
718       if (strcasecmp("UDP", tmp) == 0)
719         host->client_type = RIEMANN_CLIENT_UDP;
720       else if (strcasecmp("TCP", tmp) == 0)
721         host->client_type = RIEMANN_CLIENT_TCP;
722       else if (strcasecmp("TLS", tmp) == 0)
723         host->client_type = RIEMANN_CLIENT_TLS;
724       else
725         WARNING("write_riemann plugin: The value "
726                 "\"%s\" is not valid for the "
727                 "\"Protocol\" option. Use "
728                 "either \"UDP\", \"TCP\" or \"TLS\".",
729                 tmp);
730     } else if (strcasecmp("TLSCAFile", child->key) == 0) {
731       status = cf_util_get_string(child, &host->tls_ca_file);
732       if (status != 0) {
733         ERROR("write_riemann plugin: cf_util_get_"
734               "string_buffer failed with "
735               "status %i.",
736               status);
737         break;
738       }
739     } else if (strcasecmp("TLSCertFile", child->key) == 0) {
740       status = cf_util_get_string(child, &host->tls_cert_file);
741       if (status != 0) {
742         ERROR("write_riemann plugin: cf_util_get_"
743               "string_buffer failed with "
744               "status %i.",
745               status);
746         break;
747       }
748     } else if (strcasecmp("TLSKeyFile", child->key) == 0) {
749       status = cf_util_get_string(child, &host->tls_key_file);
750       if (status != 0) {
751         ERROR("write_riemann plugin: cf_util_get_"
752               "string_buffer failed with "
753               "status %i.",
754               status);
755         break;
756       }
757     } else if (strcasecmp("StoreRates", child->key) == 0) {
758       status = cf_util_get_boolean(child, &host->store_rates);
759       if (status != 0)
760         break;
761     } else if (strcasecmp("AlwaysAppendDS", child->key) == 0) {
762       status = cf_util_get_boolean(child, &host->always_append_ds);
763       if (status != 0)
764         break;
765     } else if (strcasecmp("TTLFactor", child->key) == 0) {
766       double tmp = NAN;
767       status = cf_util_get_double(child, &tmp);
768       if (status != 0)
769         break;
770       if (tmp >= 2.0) {
771         host->ttl_factor = tmp;
772       } else if (tmp >= 1.0) {
773         NOTICE("write_riemann plugin: The configured "
774                "TTLFactor is very small "
775                "(%.1f). A value of 2.0 or "
776                "greater is recommended.",
777                tmp);
778         host->ttl_factor = tmp;
779       } else if (tmp > 0.0) {
780         WARNING("write_riemann plugin: The configured "
781                 "TTLFactor is too small to be "
782                 "useful (%.1f). I'll use it "
783                 "since the user knows best, "
784                 "but under protest.",
785                 tmp);
786         host->ttl_factor = tmp;
787       } else { /* zero, negative and NAN */
788         ERROR("write_riemann plugin: The configured "
789               "TTLFactor is invalid (%.1f).",
790               tmp);
791       }
792     } else {
793       WARNING("write_riemann plugin: ignoring unknown config "
794               "option: \"%s\"",
795               child->key);
796     }
797   }
798   if (status != 0) {
799     wrr_free(host);
800     return status;
801   }
802
803   snprintf(callback_name, sizeof(callback_name), "write_riemann/%s",
804            host->name);
805
806   user_data_t ud = {.data = host, .free_func = wrr_free};
807
808   pthread_mutex_lock(&host->lock);
809
810   status = plugin_register_write(callback_name, wrr_write, &ud);
811
812   if (host->client_type != RIEMANN_CLIENT_UDP && host->batch_mode) {
813     ud.free_func = NULL;
814     plugin_register_flush(callback_name, wrr_batch_flush, &ud);
815   }
816   if (status != 0)
817     WARNING("write_riemann plugin: plugin_register_write (\"%s\") "
818             "failed with status %i.",
819             callback_name, status);
820   else /* success */
821     host->reference_count++;
822
823   status = plugin_register_notification(callback_name, wrr_notification, &ud);
824   if (status != 0)
825     WARNING("write_riemann plugin: plugin_register_notification (\"%s\") "
826             "failed with status %i.",
827             callback_name, status);
828   else /* success */
829     host->reference_count++;
830
831   if (host->reference_count <= 1) {
832     /* Both callbacks failed => free memory.
833      * We need to unlock here, because riemann_free() will lock.
834      * This is not a race condition, because we're the only one
835      * holding a reference. */
836     pthread_mutex_unlock(&host->lock);
837     wrr_free(host);
838     return -1;
839   }
840
841   host->reference_count--;
842   pthread_mutex_unlock(&host->lock);
843
844   return status;
845 } /* }}} int wrr_config_node */
846
847 static int wrr_config(oconfig_item_t *ci) /* {{{ */
848 {
849   int i;
850   oconfig_item_t *child;
851   int status;
852
853   for (i = 0; i < ci->children_num; i++) {
854     child = &ci->children[i];
855
856     if (strcasecmp("Node", child->key) == 0) {
857       wrr_config_node(child);
858     } else if (strcasecmp(child->key, "attribute") == 0) {
859       char *key = NULL;
860       char *val = NULL;
861
862       if (child->values_num != 2) {
863         WARNING("riemann attributes need both a key and a value.");
864         return -1;
865       }
866       if (child->values[0].type != OCONFIG_TYPE_STRING ||
867           child->values[1].type != OCONFIG_TYPE_STRING) {
868         WARNING("riemann attribute needs string arguments.");
869         return -1;
870       }
871       if ((key = strdup(child->values[0].value.string)) == NULL) {
872         WARNING("cannot allocate memory for attribute key.");
873         return -1;
874       }
875       if ((val = strdup(child->values[1].value.string)) == NULL) {
876         WARNING("cannot allocate memory for attribute value.");
877         sfree(key);
878         return -1;
879       }
880       strarray_add(&riemann_attrs, &riemann_attrs_num, key);
881       strarray_add(&riemann_attrs, &riemann_attrs_num, val);
882       DEBUG("write_riemann: got attr: %s => %s", key, val);
883       sfree(key);
884       sfree(val);
885     } else if (strcasecmp(child->key, "tag") == 0) {
886       char *tmp = NULL;
887       status = cf_util_get_string(child, &tmp);
888       if (status != 0)
889         continue;
890
891       strarray_add(&riemann_tags, &riemann_tags_num, tmp);
892       DEBUG("write_riemann plugin: Got tag: %s", tmp);
893       sfree(tmp);
894     } else {
895       WARNING("write_riemann plugin: Ignoring unknown "
896               "configuration option \"%s\" at top level.",
897               child->key);
898     }
899   }
900   return 0;
901 } /* }}} int wrr_config */
902
903 void module_register(void) {
904   plugin_register_complex_config("write_riemann", wrr_config);
905 }