6db3ef58ac88349adfd274cb1d1015e7cf9f4089
[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 *
185 wrr_notification_to_message(struct riemann_host *host, /* {{{ */
186                             notification_t const *n) {
187   riemann_message_t *msg;
188   riemann_event_t *event;
189   char service_buffer[6 * DATA_MAX_NAME_LEN];
190   char const *severity;
191
192   switch (n->severity) {
193   case NOTIF_OKAY:
194     severity = "ok";
195     break;
196   case NOTIF_WARNING:
197     severity = "warning";
198     break;
199   case NOTIF_FAILURE:
200     severity = "critical";
201     break;
202   default:
203     severity = "unknown";
204   }
205
206   format_name(service_buffer, sizeof(service_buffer),
207               /* host = */ "", n->plugin, n->plugin_instance, n->type,
208               n->type_instance);
209
210   event = riemann_event_create(
211       RIEMANN_EVENT_FIELD_HOST, n->host, RIEMANN_EVENT_FIELD_TIME,
212       (int64_t)CDTIME_T_TO_TIME_T(n->time), RIEMANN_EVENT_FIELD_TAGS,
213       "notification", NULL, RIEMANN_EVENT_FIELD_STATE, severity,
214       RIEMANN_EVENT_FIELD_SERVICE, &service_buffer[1],
215       RIEMANN_EVENT_FIELD_NONE);
216
217 #if RCC_VERSION_NUMBER >= 0x010A00
218   riemann_event_set(event, RIEMANN_EVENT_FIELD_TIME_MICROS,
219                     (int64_t)CDTIME_T_TO_US(n->time));
220 #endif
221
222   if (n->host[0] != 0)
223     riemann_event_string_attribute_add(event, "host", n->host);
224   if (n->plugin[0] != 0)
225     riemann_event_string_attribute_add(event, "plugin", n->plugin);
226   if (n->plugin_instance[0] != 0)
227     riemann_event_string_attribute_add(event, "plugin_instance",
228                                        n->plugin_instance);
229
230   if (n->type[0] != 0)
231     riemann_event_string_attribute_add(event, "type", n->type);
232   if (n->type_instance[0] != 0)
233     riemann_event_string_attribute_add(event, "type_instance",
234                                        n->type_instance);
235
236   for (size_t i = 0; i < riemann_attrs_num; i += 2)
237     riemann_event_string_attribute_add(event, riemann_attrs[i],
238                                        riemann_attrs[i + 1]);
239
240   for (size_t i = 0; i < riemann_tags_num; i++)
241     riemann_event_tag_add(event, riemann_tags[i]);
242
243   if (n->message[0] != 0)
244     riemann_event_string_attribute_add(event, "description", n->message);
245
246   /* Pull in values from threshold and add extra attributes */
247   for (notification_meta_t *meta = n->meta; meta != NULL; meta = meta->next) {
248     if (strcasecmp("CurrentValue", meta->name) == 0 &&
249         meta->type == NM_TYPE_DOUBLE) {
250       riemann_event_set(event, RIEMANN_EVENT_FIELD_METRIC_D,
251                         (double)meta->nm_value.nm_double,
252                         RIEMANN_EVENT_FIELD_NONE);
253       continue;
254     }
255
256     if (meta->type == NM_TYPE_STRING) {
257       riemann_event_string_attribute_add(event, meta->name,
258                                          meta->nm_value.nm_string);
259       continue;
260     }
261   }
262
263   msg = riemann_message_create_with_events(event, NULL);
264   if (msg == NULL) {
265     ERROR("write_riemann plugin: riemann_message_create_with_events() failed.");
266     riemann_event_free(event);
267     return NULL;
268   }
269
270   DEBUG("write_riemann plugin: Successfully created message for notification: "
271         "host = \"%s\", service = \"%s\", state = \"%s\"",
272         event->host, event->service, event->state);
273   return msg;
274 } /* }}} riemann_message_t *wrr_notification_to_message */
275
276 static riemann_event_t *
277 wrr_value_to_event(struct riemann_host const *host, /* {{{ */
278                    data_set_t const *ds, value_list_t const *vl, size_t index,
279                    gauge_t const *rates, int status) {
280   riemann_event_t *event;
281   char name_buffer[5 * DATA_MAX_NAME_LEN];
282   char service_buffer[6 * DATA_MAX_NAME_LEN];
283   size_t i;
284
285   event = riemann_event_new();
286   if (event == NULL) {
287     ERROR("write_riemann plugin: riemann_event_new() failed.");
288     return NULL;
289   }
290
291   format_name(name_buffer, sizeof(name_buffer),
292               /* host = */ "", vl->plugin, vl->plugin_instance, vl->type,
293               vl->type_instance);
294   if (host->always_append_ds || (ds->ds_num > 1)) {
295     if (host->event_service_prefix == NULL)
296       snprintf(service_buffer, sizeof(service_buffer), "%s/%s", &name_buffer[1],
297                ds->ds[index].name);
298     else
299       snprintf(service_buffer, sizeof(service_buffer), "%s%s/%s",
300                host->event_service_prefix, &name_buffer[1], ds->ds[index].name);
301   } else {
302     if (host->event_service_prefix == NULL)
303       sstrncpy(service_buffer, &name_buffer[1], sizeof(service_buffer));
304     else
305       snprintf(service_buffer, sizeof(service_buffer), "%s%s",
306                host->event_service_prefix, &name_buffer[1]);
307   }
308
309   riemann_event_set(
310       event, RIEMANN_EVENT_FIELD_HOST, vl->host, RIEMANN_EVENT_FIELD_TIME,
311       (int64_t)CDTIME_T_TO_TIME_T(vl->time), RIEMANN_EVENT_FIELD_TTL,
312       (float)CDTIME_T_TO_DOUBLE(vl->interval) * host->ttl_factor,
313       RIEMANN_EVENT_FIELD_STRING_ATTRIBUTES, "plugin", vl->plugin, "type",
314       vl->type, "ds_name", ds->ds[index].name, NULL,
315       RIEMANN_EVENT_FIELD_SERVICE, service_buffer, RIEMANN_EVENT_FIELD_NONE);
316
317 #if RCC_VERSION_NUMBER >= 0x010A00
318   riemann_event_set(event, RIEMANN_EVENT_FIELD_TIME_MICROS,
319                     (int64_t)CDTIME_T_TO_US(vl->time));
320 #endif
321
322   if (host->check_thresholds) {
323     const char *state = NULL;
324
325     switch (status) {
326     case STATE_OKAY:
327       state = "ok";
328       break;
329     case STATE_ERROR:
330       state = "critical";
331       break;
332     case STATE_WARNING:
333       state = "warning";
334       break;
335     case STATE_MISSING:
336       state = "unknown";
337       break;
338     }
339     if (state)
340       riemann_event_set(event, RIEMANN_EVENT_FIELD_STATE, state,
341                         RIEMANN_EVENT_FIELD_NONE);
342   }
343
344   if (vl->plugin_instance[0] != 0)
345     riemann_event_string_attribute_add(event, "plugin_instance",
346                                        vl->plugin_instance);
347   if (vl->type_instance[0] != 0)
348     riemann_event_string_attribute_add(event, "type_instance",
349                                        vl->type_instance);
350
351   if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL)) {
352     char ds_type[DATA_MAX_NAME_LEN];
353
354     snprintf(ds_type, sizeof(ds_type), "%s:rate",
355              DS_TYPE_TO_STRING(ds->ds[index].type));
356     riemann_event_string_attribute_add(event, "ds_type", ds_type);
357   } else {
358     riemann_event_string_attribute_add(event, "ds_type",
359                                        DS_TYPE_TO_STRING(ds->ds[index].type));
360   }
361
362   {
363     char ds_index[DATA_MAX_NAME_LEN];
364
365     snprintf(ds_index, sizeof(ds_index), "%zu", index);
366     riemann_event_string_attribute_add(event, "ds_index", ds_index);
367   }
368
369   for (i = 0; i < riemann_attrs_num; i += 2)
370     riemann_event_string_attribute_add(event, riemann_attrs[i],
371                                        riemann_attrs[i + 1]);
372
373   for (i = 0; i < riemann_tags_num; i++)
374     riemann_event_tag_add(event, riemann_tags[i]);
375
376   if (ds->ds[index].type == DS_TYPE_GAUGE) {
377     riemann_event_set(event, RIEMANN_EVENT_FIELD_METRIC_D,
378                       (double)vl->values[index].gauge,
379                       RIEMANN_EVENT_FIELD_NONE);
380   } else if (rates != NULL) {
381     riemann_event_set(event, RIEMANN_EVENT_FIELD_METRIC_D, (double)rates[index],
382                       RIEMANN_EVENT_FIELD_NONE);
383   } else {
384     int64_t metric;
385
386     if (ds->ds[index].type == DS_TYPE_DERIVE)
387       metric = (int64_t)vl->values[index].derive;
388     else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
389       metric = (int64_t)vl->values[index].absolute;
390     else
391       metric = (int64_t)vl->values[index].counter;
392
393     riemann_event_set(event, RIEMANN_EVENT_FIELD_METRIC_S64, (int64_t)metric,
394                       RIEMANN_EVENT_FIELD_NONE);
395   }
396
397   DEBUG("write_riemann plugin: Successfully created message for metric: "
398         "host = \"%s\", service = \"%s\"",
399         event->host, event->service);
400   return event;
401 } /* }}} riemann_event_t *wrr_value_to_event */
402
403 static riemann_message_t *
404 wrr_value_list_to_message(struct riemann_host const *host, /* {{{ */
405                           data_set_t const *ds, value_list_t const *vl,
406                           int *statuses) {
407   riemann_message_t *msg;
408   size_t i;
409   gauge_t *rates = NULL;
410
411   /* Initialize the Msg structure. */
412   msg = riemann_message_new();
413   if (msg == NULL) {
414     ERROR("write_riemann plugin: riemann_message_new failed.");
415     return NULL;
416   }
417
418   if (host->store_rates) {
419     rates = uc_get_rate(ds, vl);
420     if (rates == NULL) {
421       ERROR("write_riemann plugin: uc_get_rate failed.");
422       riemann_message_free(msg);
423       return NULL;
424     }
425   }
426
427   for (i = 0; i < vl->values_len; i++) {
428     riemann_event_t *event;
429
430     event = wrr_value_to_event(host, ds, vl, (int)i, rates, statuses[i]);
431     if (event == NULL) {
432       riemann_message_free(msg);
433       sfree(rates);
434       return NULL;
435     }
436     riemann_message_append_events(msg, event, NULL);
437   }
438
439   sfree(rates);
440   return msg;
441 } /* }}} riemann_message_t *wrr_value_list_to_message */
442
443 /*
444  * Always call while holding host->lock !
445  */
446 static int wrr_batch_flush_nolock(cdtime_t timeout, struct riemann_host *host) {
447   cdtime_t now;
448   int status = 0;
449
450   now = cdtime();
451   if (timeout > 0) {
452     if ((host->batch_init + timeout) > now) {
453       return status;
454     }
455   }
456   wrr_send_nolock(host, host->batch_msg);
457   riemann_message_free(host->batch_msg);
458
459   host->batch_init = now;
460   host->batch_msg = NULL;
461   return status;
462 }
463
464 static int wrr_batch_flush(cdtime_t timeout,
465                            const char *identifier __attribute__((unused)),
466                            user_data_t *user_data) {
467   struct riemann_host *host;
468   int status;
469
470   if (user_data == NULL)
471     return -EINVAL;
472
473   host = user_data->data;
474   pthread_mutex_lock(&host->lock);
475   status = wrr_batch_flush_nolock(timeout, host);
476   if (status != 0)
477     c_complain(
478         LOG_ERR, &host->init_complaint,
479         "write_riemann plugin: riemann_client_send failed with status %i",
480         status);
481   else
482     c_release(LOG_DEBUG, &host->init_complaint,
483               "write_riemann plugin: batch sent.");
484
485   pthread_mutex_unlock(&host->lock);
486   return status;
487 }
488
489 static int wrr_batch_add_value_list(struct riemann_host *host, /* {{{ */
490                                     data_set_t const *ds,
491                                     value_list_t const *vl, int *statuses) {
492   riemann_message_t *msg;
493   size_t len;
494   int ret;
495   cdtime_t timeout;
496
497   msg = wrr_value_list_to_message(host, ds, vl, statuses);
498   if (msg == NULL)
499     return -1;
500
501   pthread_mutex_lock(&host->lock);
502
503   if (host->batch_msg == NULL) {
504     host->batch_msg = msg;
505   } else {
506     int status;
507
508     status = riemann_message_append_events_n(host->batch_msg, msg->n_events,
509                                              msg->events);
510     msg->n_events = 0;
511     msg->events = NULL;
512
513     riemann_message_free(msg);
514
515     if (status != 0) {
516       pthread_mutex_unlock(&host->lock);
517       ERROR("write_riemann plugin: out of memory");
518       return -1;
519     }
520   }
521
522   len = riemann_message_get_packed_size(host->batch_msg);
523   ret = 0;
524   if ((host->batch_max < 0) || (((size_t)host->batch_max) <= len)) {
525     ret = wrr_batch_flush_nolock(0, host);
526   } else {
527     if (host->batch_timeout > 0) {
528       timeout = TIME_T_TO_CDTIME_T((time_t)host->batch_timeout);
529       ret = wrr_batch_flush_nolock(timeout, host);
530     }
531   }
532
533   pthread_mutex_unlock(&host->lock);
534   return ret;
535 } /* }}} riemann_message_t *wrr_batch_add_value_list */
536
537 static int wrr_notification(const notification_t *n, user_data_t *ud) /* {{{ */
538 {
539   int status;
540   struct riemann_host *host = ud->data;
541   riemann_message_t *msg;
542
543   if (!host->notifications)
544     return 0;
545
546   /*
547    * Never batch for notifications, send them ASAP
548    */
549   msg = wrr_notification_to_message(host, n);
550   if (msg == NULL)
551     return -1;
552
553   status = wrr_send(host, msg);
554   if (status != 0)
555     c_complain(
556         LOG_ERR, &host->init_complaint,
557         "write_riemann plugin: riemann_client_send failed with status %i",
558         status);
559   else
560     c_release(LOG_DEBUG, &host->init_complaint,
561               "write_riemann plugin: riemann_client_send succeeded");
562
563   riemann_message_free(msg);
564   return status;
565 } /* }}} int wrr_notification */
566
567 static int wrr_write(const data_set_t *ds, /* {{{ */
568                      const value_list_t *vl, user_data_t *ud) {
569   int status = 0;
570   int statuses[vl->values_len];
571   struct riemann_host *host = ud->data;
572   riemann_message_t *msg;
573
574   if (host->check_thresholds) {
575     status = write_riemann_threshold_check(ds, vl, statuses);
576     if (status != 0)
577       return status;
578   } else {
579     memset(statuses, 0, sizeof(statuses));
580   }
581
582   if (host->client_type != RIEMANN_CLIENT_UDP && host->batch_mode) {
583     wrr_batch_add_value_list(host, ds, vl, statuses);
584   } else {
585     msg = wrr_value_list_to_message(host, ds, vl, statuses);
586     if (msg == NULL)
587       return -1;
588
589     status = wrr_send(host, msg);
590
591     riemann_message_free(msg);
592   }
593   return status;
594 } /* }}} int wrr_write */
595
596 static void wrr_free(void *p) /* {{{ */
597 {
598   struct riemann_host *host = p;
599
600   if (host == NULL)
601     return;
602
603   pthread_mutex_lock(&host->lock);
604
605   host->reference_count--;
606   if (host->reference_count > 0) {
607     pthread_mutex_unlock(&host->lock);
608     return;
609   }
610
611   wrr_disconnect(host);
612
613   pthread_mutex_destroy(&host->lock);
614   sfree(host);
615 } /* }}} void wrr_free */
616
617 static int wrr_config_node(oconfig_item_t *ci) /* {{{ */
618 {
619   struct riemann_host *host = NULL;
620   int status = 0;
621   int i;
622   oconfig_item_t *child;
623   char callback_name[DATA_MAX_NAME_LEN];
624
625   if ((host = calloc(1, sizeof(*host))) == NULL) {
626     ERROR("write_riemann plugin: calloc failed.");
627     return ENOMEM;
628   }
629   pthread_mutex_init(&host->lock, NULL);
630   C_COMPLAIN_INIT(&host->init_complaint);
631   host->reference_count = 1;
632   host->node = NULL;
633   host->port = 0;
634   host->notifications = 1;
635   host->check_thresholds = 0;
636   host->store_rates = 1;
637   host->always_append_ds = 0;
638   host->batch_mode = 1;
639   host->batch_max = RIEMANN_BATCH_MAX; /* typical MSS */
640   host->batch_init = cdtime();
641   host->batch_timeout = 0;
642   host->ttl_factor = RIEMANN_TTL_FACTOR;
643   host->client = NULL;
644   host->client_type = RIEMANN_CLIENT_TCP;
645   host->timeout.tv_sec = 0;
646   host->timeout.tv_usec = 0;
647
648   status = cf_util_get_string(ci, &host->name);
649   if (status != 0) {
650     WARNING("write_riemann plugin: Required host name is missing.");
651     wrr_free(host);
652     return -1;
653   }
654
655   for (i = 0; i < ci->children_num; i++) {
656     /*
657      * The code here could be simplified but makes room
658      * for easy adding of new options later on.
659      */
660     child = &ci->children[i];
661     status = 0;
662
663     if (strcasecmp("Host", child->key) == 0) {
664       status = cf_util_get_string(child, &host->node);
665       if (status != 0)
666         break;
667     } else if (strcasecmp("Notifications", child->key) == 0) {
668       status = cf_util_get_boolean(child, &host->notifications);
669       if (status != 0)
670         break;
671     } else if (strcasecmp("EventServicePrefix", child->key) == 0) {
672       status = cf_util_get_string(child, &host->event_service_prefix);
673       if (status != 0)
674         break;
675     } else if (strcasecmp("CheckThresholds", child->key) == 0) {
676       status = cf_util_get_boolean(child, &host->check_thresholds);
677       if (status != 0)
678         break;
679     } else if (strcasecmp("Batch", child->key) == 0) {
680       status = cf_util_get_boolean(child, &host->batch_mode);
681       if (status != 0)
682         break;
683     } else if (strcasecmp("BatchMaxSize", child->key) == 0) {
684       status = cf_util_get_int(child, &host->batch_max);
685       if (status != 0)
686         break;
687     } else if (strcasecmp("BatchFlushTimeout", child->key) == 0) {
688       status = cf_util_get_int(child, &host->batch_timeout);
689       if (status != 0)
690         break;
691     } else if (strcasecmp("Timeout", child->key) == 0) {
692 #if RCC_VERSION_NUMBER >= 0x010800
693       status = cf_util_get_int(child, (int *)&host->timeout.tv_sec);
694       if (status != 0)
695         break;
696 #else
697       WARNING("write_riemann plugin: The Timeout option is not supported. "
698               "Please upgrade the Riemann client to at least 1.8.0.");
699 #endif
700     } else if (strcasecmp("Port", child->key) == 0) {
701       host->port = cf_util_get_port_number(child);
702       if (host->port == -1) {
703         ERROR("write_riemann plugin: Invalid argument "
704               "configured for the \"Port\" "
705               "option.");
706         break;
707       }
708     } else if (strcasecmp("Protocol", child->key) == 0) {
709       char tmp[16];
710       status = cf_util_get_string_buffer(child, tmp, sizeof(tmp));
711       if (status != 0) {
712         ERROR("write_riemann plugin: cf_util_get_"
713               "string_buffer failed with "
714               "status %i.",
715               status);
716         break;
717       }
718
719       if (strcasecmp("UDP", tmp) == 0)
720         host->client_type = RIEMANN_CLIENT_UDP;
721       else if (strcasecmp("TCP", tmp) == 0)
722         host->client_type = RIEMANN_CLIENT_TCP;
723       else if (strcasecmp("TLS", tmp) == 0)
724         host->client_type = RIEMANN_CLIENT_TLS;
725       else
726         WARNING("write_riemann plugin: The value "
727                 "\"%s\" is not valid for the "
728                 "\"Protocol\" option. Use "
729                 "either \"UDP\", \"TCP\" or \"TLS\".",
730                 tmp);
731     } else if (strcasecmp("TLSCAFile", child->key) == 0) {
732       status = cf_util_get_string(child, &host->tls_ca_file);
733       if (status != 0) {
734         ERROR("write_riemann plugin: cf_util_get_"
735               "string_buffer failed with "
736               "status %i.",
737               status);
738         break;
739       }
740     } else if (strcasecmp("TLSCertFile", child->key) == 0) {
741       status = cf_util_get_string(child, &host->tls_cert_file);
742       if (status != 0) {
743         ERROR("write_riemann plugin: cf_util_get_"
744               "string_buffer failed with "
745               "status %i.",
746               status);
747         break;
748       }
749     } else if (strcasecmp("TLSKeyFile", child->key) == 0) {
750       status = cf_util_get_string(child, &host->tls_key_file);
751       if (status != 0) {
752         ERROR("write_riemann plugin: cf_util_get_"
753               "string_buffer failed with "
754               "status %i.",
755               status);
756         break;
757       }
758     } else if (strcasecmp("StoreRates", child->key) == 0) {
759       status = cf_util_get_boolean(child, &host->store_rates);
760       if (status != 0)
761         break;
762     } else if (strcasecmp("AlwaysAppendDS", child->key) == 0) {
763       status = cf_util_get_boolean(child, &host->always_append_ds);
764       if (status != 0)
765         break;
766     } else if (strcasecmp("TTLFactor", child->key) == 0) {
767       double tmp = NAN;
768       status = cf_util_get_double(child, &tmp);
769       if (status != 0)
770         break;
771       if (tmp >= 2.0) {
772         host->ttl_factor = tmp;
773       } else if (tmp >= 1.0) {
774         NOTICE("write_riemann plugin: The configured "
775                "TTLFactor is very small "
776                "(%.1f). A value of 2.0 or "
777                "greater is recommended.",
778                tmp);
779         host->ttl_factor = tmp;
780       } else if (tmp > 0.0) {
781         WARNING("write_riemann plugin: The configured "
782                 "TTLFactor is too small to be "
783                 "useful (%.1f). I'll use it "
784                 "since the user knows best, "
785                 "but under protest.",
786                 tmp);
787         host->ttl_factor = tmp;
788       } else { /* zero, negative and NAN */
789         ERROR("write_riemann plugin: The configured "
790               "TTLFactor is invalid (%.1f).",
791               tmp);
792       }
793     } else {
794       WARNING("write_riemann plugin: ignoring unknown config "
795               "option: \"%s\"",
796               child->key);
797     }
798   }
799   if (status != 0) {
800     wrr_free(host);
801     return status;
802   }
803
804   snprintf(callback_name, sizeof(callback_name), "write_riemann/%s",
805            host->name);
806
807   user_data_t ud = {.data = host, .free_func = wrr_free};
808
809   pthread_mutex_lock(&host->lock);
810
811   status = plugin_register_write(callback_name, wrr_write, &ud);
812
813   if (host->client_type != RIEMANN_CLIENT_UDP && host->batch_mode) {
814     ud.free_func = NULL;
815     plugin_register_flush(callback_name, wrr_batch_flush, &ud);
816   }
817   if (status != 0)
818     WARNING("write_riemann plugin: plugin_register_write (\"%s\") "
819             "failed with status %i.",
820             callback_name, status);
821   else /* success */
822     host->reference_count++;
823
824   status = plugin_register_notification(callback_name, wrr_notification, &ud);
825   if (status != 0)
826     WARNING("write_riemann plugin: plugin_register_notification (\"%s\") "
827             "failed with status %i.",
828             callback_name, status);
829   else /* success */
830     host->reference_count++;
831
832   if (host->reference_count <= 1) {
833     /* Both callbacks failed => free memory.
834      * We need to unlock here, because riemann_free() will lock.
835      * This is not a race condition, because we're the only one
836      * holding a reference. */
837     pthread_mutex_unlock(&host->lock);
838     wrr_free(host);
839     return -1;
840   }
841
842   host->reference_count--;
843   pthread_mutex_unlock(&host->lock);
844
845   return status;
846 } /* }}} int wrr_config_node */
847
848 static int wrr_config(oconfig_item_t *ci) /* {{{ */
849 {
850   int i;
851   oconfig_item_t *child;
852   int status;
853
854   for (i = 0; i < ci->children_num; i++) {
855     child = &ci->children[i];
856
857     if (strcasecmp("Node", child->key) == 0) {
858       wrr_config_node(child);
859     } else if (strcasecmp(child->key, "attribute") == 0) {
860       char *key = NULL;
861       char *val = NULL;
862
863       if (child->values_num != 2) {
864         WARNING("riemann attributes need both a key and a value.");
865         return -1;
866       }
867       if (child->values[0].type != OCONFIG_TYPE_STRING ||
868           child->values[1].type != OCONFIG_TYPE_STRING) {
869         WARNING("riemann attribute needs string arguments.");
870         return -1;
871       }
872       if ((key = strdup(child->values[0].value.string)) == NULL) {
873         WARNING("cannot allocate memory for attribute key.");
874         return -1;
875       }
876       if ((val = strdup(child->values[1].value.string)) == NULL) {
877         WARNING("cannot allocate memory for attribute value.");
878         sfree(key);
879         return -1;
880       }
881       strarray_add(&riemann_attrs, &riemann_attrs_num, key);
882       strarray_add(&riemann_attrs, &riemann_attrs_num, val);
883       DEBUG("write_riemann: got attr: %s => %s", key, val);
884       sfree(key);
885       sfree(val);
886     } else if (strcasecmp(child->key, "tag") == 0) {
887       char *tmp = NULL;
888       status = cf_util_get_string(child, &tmp);
889       if (status != 0)
890         continue;
891
892       strarray_add(&riemann_tags, &riemann_tags_num, tmp);
893       DEBUG("write_riemann plugin: Got tag: %s", tmp);
894       sfree(tmp);
895     } else {
896       WARNING("write_riemann plugin: Ignoring unknown "
897               "configuration option \"%s\" at top level.",
898               child->key);
899     }
900   }
901   return 0;
902 } /* }}} int wrr_config */
903
904 void module_register(void) {
905   plugin_register_complex_config("write_riemann", wrr_config);
906 }