Merge branch 'collectd-5.3' into collectd-5.4
[collectd.git] / src / write_riemann.c
1 /**
2  * collectd - src/write_riemann.c
3  *
4  * Copyright (C) 2012,2013  Pierre-Yves Ritschard
5  * Copyright (C) 2013       Florian octo Forster
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  *
19  * Authors:
20  *   Pierre-Yves Ritschard <pyr at spootnik.org>
21  *   Florian octo Forster <octo at collectd.org>
22  */
23
24 #include "collectd.h"
25 #include "plugin.h"
26 #include "common.h"
27 #include "configfile.h"
28 #include "utils_cache.h"
29 #include "riemann.pb-c.h"
30
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
33 #include <errno.h>
34 #include <netdb.h>
35 #include <inttypes.h>
36 #include <pthread.h>
37
38 #define RIEMANN_HOST            "localhost"
39 #define RIEMANN_PORT            "5555"
40 #define RIEMANN_TTL_FACTOR      2.0
41
42 struct riemann_host {
43         char                    *name;
44 #define F_CONNECT                0x01
45         uint8_t                  flags;
46         pthread_mutex_t          lock;
47         _Bool                    store_rates;
48         _Bool                    always_append_ds;
49         char                    *node;
50         char                    *service;
51         _Bool                    use_tcp;
52         int                      s;
53         double                   ttl_factor;
54
55         int                      reference_count;
56 };
57
58 static char     **riemann_tags;
59 static size_t     riemann_tags_num;
60
61 static void riemann_event_protobuf_free (Event *event) /* {{{ */
62 {
63         size_t i;
64
65         if (event == NULL)
66                 return;
67
68         sfree (event->state);
69         sfree (event->service);
70         sfree (event->host);
71         sfree (event->description);
72
73         strarray_free (event->tags, event->n_tags);
74         event->tags = NULL;
75         event->n_tags = 0;
76
77         for (i = 0; i < event->n_attributes; i++)
78         {
79                 sfree (event->attributes[i]->key);
80                 sfree (event->attributes[i]->value);
81                 sfree (event->attributes[i]);
82         }
83         sfree (event->attributes);
84         event->n_attributes = 0;
85
86         sfree (event);
87 } /* }}} void riemann_event_protobuf_free */
88
89 static void riemann_msg_protobuf_free(Msg *msg) /* {{{ */
90 {
91         size_t i;
92
93         if (msg == NULL)
94                 return;
95
96         for (i = 0; i < msg->n_events; i++)
97         {
98                 riemann_event_protobuf_free (msg->events[i]);
99                 msg->events[i] = NULL;
100         }
101
102         sfree (msg->events);
103         msg->n_events = 0;
104
105         sfree (msg);
106 } /* }}} void riemann_msg_protobuf_free */
107
108 /* host->lock must be held when calling this function. */
109 static int riemann_connect(struct riemann_host *host) /* {{{ */
110 {
111         int                      e;
112         struct addrinfo         *ai, *res, hints;
113         char const              *node;
114         char const              *service;
115
116         if (host->flags & F_CONNECT)
117                 return 0;
118
119         memset(&hints, 0, sizeof(hints));
120         memset(&service, 0, sizeof(service));
121         hints.ai_family = AF_UNSPEC;
122         hints.ai_socktype = host->use_tcp ? SOCK_STREAM : SOCK_DGRAM;
123 #ifdef AI_ADDRCONFIG
124         hints.ai_flags |= AI_ADDRCONFIG;
125 #endif
126
127         node = (host->node != NULL) ? host->node : RIEMANN_HOST;
128         service = (host->service != NULL) ? host->service : RIEMANN_PORT;
129
130         if ((e = getaddrinfo(node, service, &hints, &res)) != 0) {
131                 ERROR ("write_riemann plugin: Unable to resolve host \"%s\": %s",
132                         node, gai_strerror(e));
133                 return -1;
134         }
135
136         host->s = -1;
137         for (ai = res; ai != NULL; ai = ai->ai_next) {
138                 if ((host->s = socket(ai->ai_family,
139                                       ai->ai_socktype,
140                                       ai->ai_protocol)) == -1) {
141                         continue;
142                 }
143
144                 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
145                         close(host->s);
146                         host->s = -1;
147                         continue;
148                 }
149
150                 host->flags |= F_CONNECT;
151                 DEBUG("write_riemann plugin: got a succesful connection for: %s:%s",
152                                 node, service);
153                 break;
154         }
155
156         freeaddrinfo(res);
157
158         if (host->s < 0) {
159                 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
160                                 node, service);
161                 return -1;
162         }
163         return 0;
164 } /* }}} int riemann_connect */
165
166 /* host->lock must be held when calling this function. */
167 static int riemann_disconnect (struct riemann_host *host) /* {{{ */
168 {
169         if ((host->flags & F_CONNECT) == 0)
170                 return (0);
171
172         close (host->s);
173         host->s = -1;
174         host->flags &= ~F_CONNECT;
175
176         return (0);
177 } /* }}} int riemann_disconnect */
178
179 static int riemann_send_msg (struct riemann_host *host, const Msg *msg) /* {{{ */
180 {
181         int status = 0;
182         u_char *buffer = NULL;
183         size_t  buffer_len;
184
185         status = riemann_connect (host);
186         if (status != 0)
187                 return status;
188
189         buffer_len = msg__get_packed_size(msg);
190
191         if (host->use_tcp)
192                 buffer_len += 4;
193
194         buffer = malloc (buffer_len);
195         if (buffer == NULL) {
196                 ERROR ("write_riemann plugin: malloc failed.");
197                 return ENOMEM;
198         }
199         memset (buffer, 0, buffer_len);
200
201         if (host->use_tcp)
202         {
203                 uint32_t length = htonl ((uint32_t) (buffer_len - 4));
204                 memcpy (buffer, &length, 4);
205                 msg__pack(msg, buffer + 4);
206         }
207         else
208         {
209                 msg__pack(msg, buffer);
210         }
211
212         status = (int) swrite (host->s, buffer, buffer_len);
213         if (status != 0)
214         {
215                 char errbuf[1024];
216                 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s",
217                                 (host->node != NULL) ? host->node : RIEMANN_HOST,
218                                 (host->service != NULL) ? host->service : RIEMANN_PORT,
219                                 sstrerror (errno, errbuf, sizeof (errbuf)));
220                 sfree (buffer);
221                 return -1;
222         }
223
224         sfree (buffer);
225         return 0;
226 } /* }}} int riemann_send_msg */
227
228 static int riemann_recv_ack(struct riemann_host *host) /* {{{ */
229 {
230         int status = 0;
231         Msg *msg = NULL;
232         uint32_t header;
233
234         status = (int) sread (host->s, &header, 4);
235
236         if (status != 0)
237                 return -1;
238
239         size_t size = ntohl(header);
240
241         // Buffer on the stack since acknowledges are typically small.
242         u_char buffer[size];
243         memset (buffer, 0, size);
244
245         status = (int) sread (host->s, buffer, size);
246
247         if (status != 0)
248                 return status;
249
250         msg = msg__unpack (NULL, size, buffer);
251
252         if (msg == NULL)
253                 return -1;
254
255         if (!msg->ok)
256         {
257                 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s acknowledgement message reported error: %s",
258                                 (host->node != NULL) ? host->node : RIEMANN_HOST,
259                                 (host->service != NULL) ? host->service : RIEMANN_PORT,
260                                 msg->error);
261
262                 msg__free_unpacked(msg, NULL);
263                 return -1;
264         }
265
266         msg__free_unpacked (msg, NULL);
267         return 0;
268 } /* }}} int riemann_recv_ack */
269
270 /**
271  * Function to send messages (Msg) to riemann.
272  *
273  * Acquires the host lock, disconnects on errors.
274  */
275 static int riemann_send(struct riemann_host *host, Msg const *msg) /* {{{ */
276 {
277         int status = 0;
278         pthread_mutex_lock (&host->lock);
279
280         status = riemann_send_msg(host, msg);
281         if (status != 0) {
282                 riemann_disconnect (host);
283                 pthread_mutex_unlock (&host->lock);
284                 return status;
285         }
286
287         /*
288          * For TCP we need to receive message acknowledgemenent.
289          */
290         if (host->use_tcp)
291         {
292                 status = riemann_recv_ack(host);
293
294                 if (status != 0)
295                 {
296                         riemann_disconnect (host);
297                         pthread_mutex_unlock (&host->lock);
298                         return status;
299                 }
300         }
301
302         pthread_mutex_unlock (&host->lock);
303         return 0;
304 } /* }}} int riemann_send */
305
306 static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
307 {
308         return (strarray_add (&event->tags, &event->n_tags, tag));
309 } /* }}} int riemann_event_add_tag */
310
311 static int riemann_event_add_attribute(Event *event, /* {{{ */
312                 char const *key, char const *value)
313 {
314         Attribute **new_attributes;
315         Attribute *a;
316
317         new_attributes = realloc (event->attributes,
318                         sizeof (*event->attributes) * (event->n_attributes + 1));
319         if (new_attributes == NULL)
320         {
321                 ERROR ("write_riemann plugin: realloc failed.");
322                 return (ENOMEM);
323         }
324         event->attributes = new_attributes;
325
326         a = malloc (sizeof (*a));
327         if (a == NULL)
328         {
329                 ERROR ("write_riemann plugin: malloc failed.");
330                 return (ENOMEM);
331         }
332         attribute__init (a);
333
334         a->key = strdup (key);
335         if (value != NULL)
336                 a->value = strdup (value);
337
338         event->attributes[event->n_attributes] = a;
339         event->n_attributes++;
340
341         return (0);
342 } /* }}} int riemann_event_add_attribute */
343
344 static Msg *riemann_notification_to_protobuf(struct riemann_host *host, /* {{{ */
345                 notification_t const *n)
346 {
347         Msg *msg;
348         Event *event;
349         char service_buffer[6 * DATA_MAX_NAME_LEN];
350         char const *severity;
351         notification_meta_t *meta;
352         int i;
353
354         msg = malloc (sizeof (*msg));
355         if (msg == NULL)
356         {
357                 ERROR ("write_riemann plugin: malloc failed.");
358                 return (NULL);
359         }
360         memset (msg, 0, sizeof (*msg));
361         msg__init (msg);
362
363         msg->events = malloc (sizeof (*msg->events));
364         if (msg->events == NULL)
365         {
366                 ERROR ("write_riemann plugin: malloc failed.");
367                 sfree (msg);
368                 return (NULL);
369         }
370
371         event = malloc (sizeof (*event));
372         if (event == NULL)
373         {
374                 ERROR ("write_riemann plugin: malloc failed.");
375                 sfree (msg->events);
376                 sfree (msg);
377                 return (NULL);
378         }
379         memset (event, 0, sizeof (*event));
380         event__init (event);
381
382         msg->events[0] = event;
383         msg->n_events = 1;
384
385         event->host = strdup (n->host);
386         event->time = CDTIME_T_TO_TIME_T (n->time);
387         event->has_time = 1;
388
389         switch (n->severity)
390         {
391                 case NOTIF_OKAY:        severity = "ok"; break;
392                 case NOTIF_WARNING:     severity = "warning"; break;
393                 case NOTIF_FAILURE:     severity = "critical"; break;
394                 default:                severity = "unknown";
395         }
396         event->state = strdup (severity);
397
398         riemann_event_add_tag (event, "notification");
399         if (n->host[0] != 0)
400                 riemann_event_add_attribute (event, "host", n->host);
401         if (n->plugin[0] != 0)
402                 riemann_event_add_attribute (event, "plugin", n->plugin);
403         if (n->plugin_instance[0] != 0)
404                 riemann_event_add_attribute (event, "plugin_instance",
405                                 n->plugin_instance);
406
407         if (n->type[0] != 0)
408                 riemann_event_add_attribute (event, "type", n->type);
409         if (n->type_instance[0] != 0)
410                 riemann_event_add_attribute (event, "type_instance",
411                                 n->type_instance);
412
413         for (i = 0; i < riemann_tags_num; i++)
414                 riemann_event_add_tag (event, riemann_tags[i]);
415
416         format_name (service_buffer, sizeof (service_buffer),
417                         /* host = */ "", n->plugin, n->plugin_instance,
418                         n->type, n->type_instance);
419         event->service = strdup (&service_buffer[1]);
420
421         /* Pull in values from threshold */
422         for (meta = n->meta; meta != NULL; meta = meta->next)
423         {
424                 if (strcasecmp ("CurrentValue", meta->name) != 0)
425                         continue;
426
427                 event->metric_d = meta->nm_value.nm_double;
428                 event->has_metric_d = 1;
429                 break;
430         }
431
432         DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
433                         "host = \"%s\", service = \"%s\", state = \"%s\"",
434                         event->host, event->service, event->state);
435         return (msg);
436 } /* }}} Msg *riemann_notification_to_protobuf */
437
438 static Event *riemann_value_to_protobuf(struct riemann_host const *host, /* {{{ */
439                 data_set_t const *ds,
440                 value_list_t const *vl, size_t index,
441                 gauge_t const *rates)
442 {
443         Event *event;
444         char name_buffer[5 * DATA_MAX_NAME_LEN];
445         char service_buffer[6 * DATA_MAX_NAME_LEN];
446         double ttl;
447         int i;
448
449         event = malloc (sizeof (*event));
450         if (event == NULL)
451         {
452                 ERROR ("write_riemann plugin: malloc failed.");
453                 return (NULL);
454         }
455         memset (event, 0, sizeof (*event));
456         event__init (event);
457
458         event->host = strdup (vl->host);
459         event->time = CDTIME_T_TO_TIME_T (vl->time);
460         event->has_time = 1;
461
462         ttl = CDTIME_T_TO_DOUBLE (vl->interval) * host->ttl_factor;
463         event->ttl = (float) ttl;
464         event->has_ttl = 1;
465
466         riemann_event_add_attribute (event, "plugin", vl->plugin);
467         if (vl->plugin_instance[0] != 0)
468                 riemann_event_add_attribute (event, "plugin_instance",
469                                 vl->plugin_instance);
470
471         riemann_event_add_attribute (event, "type", vl->type);
472         if (vl->type_instance[0] != 0)
473                 riemann_event_add_attribute (event, "type_instance",
474                                 vl->type_instance);
475
476         if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
477         {
478                 char ds_type[DATA_MAX_NAME_LEN];
479
480                 ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
481                                 DS_TYPE_TO_STRING(ds->ds[index].type));
482                 riemann_event_add_attribute (event, "ds_type", ds_type);
483         }
484         else
485         {
486                 riemann_event_add_attribute (event, "ds_type",
487                                 DS_TYPE_TO_STRING(ds->ds[index].type));
488         }
489         riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
490         {
491                 char ds_index[DATA_MAX_NAME_LEN];
492
493                 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
494                 riemann_event_add_attribute (event, "ds_index", ds_index);
495         }
496
497         for (i = 0; i < riemann_tags_num; i++)
498                 riemann_event_add_tag (event, riemann_tags[i]);
499
500         if (ds->ds[index].type == DS_TYPE_GAUGE)
501         {
502                 event->has_metric_d = 1;
503                 event->metric_d = (double) vl->values[index].gauge;
504         }
505         else if (rates != NULL)
506         {
507                 event->has_metric_d = 1;
508                 event->metric_d = (double) rates[index];
509         }
510         else
511         {
512                 event->has_metric_sint64 = 1;
513                 if (ds->ds[index].type == DS_TYPE_DERIVE)
514                         event->metric_sint64 = (int64_t) vl->values[index].derive;
515                 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
516                         event->metric_sint64 = (int64_t) vl->values[index].absolute;
517                 else
518                         event->metric_sint64 = (int64_t) vl->values[index].counter;
519         }
520
521         format_name (name_buffer, sizeof (name_buffer),
522                         /* host = */ "", vl->plugin, vl->plugin_instance,
523                         vl->type, vl->type_instance);
524         if (host->always_append_ds || (ds->ds_num > 1))
525                 ssnprintf (service_buffer, sizeof (service_buffer),
526                                 "%s/%s", &name_buffer[1], ds->ds[index].name);
527         else
528                 sstrncpy (service_buffer, &name_buffer[1],
529                                 sizeof (service_buffer));
530
531         event->service = strdup (service_buffer);
532
533         DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
534                         "host = \"%s\", service = \"%s\"",
535                         event->host, event->service);
536         return (event);
537 } /* }}} Event *riemann_value_to_protobuf */
538
539 static Msg *riemann_value_list_to_protobuf(struct riemann_host const *host, /* {{{ */
540                 data_set_t const *ds,
541                 value_list_t const *vl)
542 {
543         Msg *msg;
544         size_t i;
545         gauge_t *rates = NULL;
546
547         /* Initialize the Msg structure. */
548         msg = malloc (sizeof (*msg));
549         if (msg == NULL)
550         {
551                 ERROR ("write_riemann plugin: malloc failed.");
552                 return (NULL);
553         }
554         memset (msg, 0, sizeof (*msg));
555         msg__init (msg);
556
557         /* Set up events. First, the list of pointers. */
558         msg->n_events = (size_t) vl->values_len;
559         msg->events = calloc (msg->n_events, sizeof (*msg->events));
560         if (msg->events == NULL)
561         {
562                 ERROR ("write_riemann plugin: calloc failed.");
563                 riemann_msg_protobuf_free (msg);
564                 return (NULL);
565         }
566
567         if (host->store_rates)
568         {
569                 rates = uc_get_rate (ds, vl);
570                 if (rates == NULL)
571                 {
572                         ERROR ("write_riemann plugin: uc_get_rate failed.");
573                         riemann_msg_protobuf_free (msg);
574                         return (NULL);
575                 }
576         }
577
578         for (i = 0; i < msg->n_events; i++)
579         {
580                 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
581                                 (int) i, rates);
582                 if (msg->events[i] == NULL)
583                 {
584                         riemann_msg_protobuf_free (msg);
585                         sfree (rates);
586                         return (NULL);
587                 }
588         }
589
590         sfree (rates);
591         return (msg);
592 } /* }}} Msg *riemann_value_list_to_protobuf */
593
594 static int riemann_notification(const notification_t *n, user_data_t *ud) /* {{{ */
595 {
596         int                      status;
597         struct riemann_host     *host = ud->data;
598         Msg                     *msg;
599
600         msg = riemann_notification_to_protobuf (host, n);
601         if (msg == NULL)
602                 return (-1);
603
604         status = riemann_send (host, msg);
605         if (status != 0)
606                 ERROR ("write_riemann plugin: riemann_send failed with status %i",
607                                 status);
608
609         riemann_msg_protobuf_free (msg);
610         return (status);
611 } /* }}} int riemann_notification */
612
613 static int riemann_write(const data_set_t *ds, /* {{{ */
614               const value_list_t *vl,
615               user_data_t *ud)
616 {
617         int                      status;
618         struct riemann_host     *host = ud->data;
619         Msg                     *msg;
620
621         msg = riemann_value_list_to_protobuf (host, ds, vl);
622         if (msg == NULL)
623                 return (-1);
624
625         status = riemann_send (host, msg);
626         if (status != 0)
627                 ERROR ("write_riemann plugin: riemann_send failed with status %i",
628                                 status);
629
630         riemann_msg_protobuf_free (msg);
631         return status;
632 } /* }}} int riemann_write */
633
634 static void riemann_free(void *p) /* {{{ */
635 {
636         struct riemann_host     *host = p;
637
638         if (host == NULL)
639                 return;
640
641         pthread_mutex_lock (&host->lock);
642
643         host->reference_count--;
644         if (host->reference_count > 0)
645         {
646                 pthread_mutex_unlock (&host->lock);
647                 return;
648         }
649
650         riemann_disconnect (host);
651
652         sfree(host->service);
653         pthread_mutex_destroy (&host->lock);
654         sfree(host);
655 } /* }}} void riemann_free */
656
657 static int riemann_config_node(oconfig_item_t *ci) /* {{{ */
658 {
659         struct riemann_host     *host = NULL;
660         int                      status = 0;
661         int                      i;
662         oconfig_item_t          *child;
663         char                     callback_name[DATA_MAX_NAME_LEN];
664         user_data_t              ud;
665
666         if ((host = calloc(1, sizeof (*host))) == NULL) {
667                 ERROR ("write_riemann plugin: calloc failed.");
668                 return ENOMEM;
669         }
670         pthread_mutex_init (&host->lock, NULL);
671         host->reference_count = 1;
672         host->node = NULL;
673         host->service = NULL;
674         host->store_rates = 1;
675         host->always_append_ds = 0;
676         host->use_tcp = 0;
677         host->ttl_factor = RIEMANN_TTL_FACTOR;
678
679         status = cf_util_get_string (ci, &host->name);
680         if (status != 0) {
681                 WARNING("write_riemann plugin: Required host name is missing.");
682                 riemann_free (host);
683                 return -1;
684         }
685
686         for (i = 0; i < ci->children_num; i++) {
687                 /*
688                  * The code here could be simplified but makes room
689                  * for easy adding of new options later on.
690                  */
691                 child = &ci->children[i];
692                 status = 0;
693
694                 if (strcasecmp ("Host", child->key) == 0) {
695                         status = cf_util_get_string (child, &host->node);
696                         if (status != 0)
697                                 break;
698                 } else if (strcasecmp ("Port", child->key) == 0) {
699                         status = cf_util_get_service (child, &host->service);
700                         if (status != 0) {
701                                 ERROR ("write_riemann plugin: Invalid argument "
702                                                 "configured for the \"Port\" "
703                                                 "option.");
704                                 break;
705                         }
706                 } else if (strcasecmp ("Protocol", child->key) == 0) {
707                         char tmp[16];
708                         status = cf_util_get_string_buffer (child,
709                                         tmp, sizeof (tmp));
710                         if (status != 0)
711                         {
712                                 ERROR ("write_riemann plugin: cf_util_get_"
713                                                 "string_buffer failed with "
714                                                 "status %i.", status);
715                                 break;
716                         }
717
718                         if (strcasecmp ("UDP", tmp) == 0)
719                                 host->use_tcp = 0;
720                         else if (strcasecmp ("TCP", tmp) == 0)
721                                 host->use_tcp = 1;
722                         else
723                                 WARNING ("write_riemann plugin: The value "
724                                                 "\"%s\" is not valid for the "
725                                                 "\"Protocol\" option. Use "
726                                                 "either \"UDP\" or \"TCP\".",
727                                                 tmp);
728                 } else if (strcasecmp ("StoreRates", child->key) == 0) {
729                         status = cf_util_get_boolean (child, &host->store_rates);
730                         if (status != 0)
731                                 break;
732                 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
733                         status = cf_util_get_boolean (child,
734                                         &host->always_append_ds);
735                         if (status != 0)
736                                 break;
737                 } else if (strcasecmp ("TTLFactor", child->key) == 0) {
738                         double tmp = NAN;
739                         status = cf_util_get_double (child, &tmp);
740                         if (status != 0)
741                                 break;
742                         if (tmp >= 2.0) {
743                                 host->ttl_factor = tmp;
744                         } else if (tmp >= 1.0) {
745                                 NOTICE ("write_riemann plugin: The configured "
746                                                 "TTLFactor is very small "
747                                                 "(%.1f). A value of 2.0 or "
748                                                 "greater is recommended.",
749                                                 tmp);
750                                 host->ttl_factor = tmp;
751                         } else if (tmp > 0.0) {
752                                 WARNING ("write_riemann plugin: The configured "
753                                                 "TTLFactor is too small to be "
754                                                 "useful (%.1f). I'll use it "
755                                                 "since the user knows best, "
756                                                 "but under protest.",
757                                                 tmp);
758                                 host->ttl_factor = tmp;
759                         } else { /* zero, negative and NAN */
760                                 ERROR ("write_riemann plugin: The configured "
761                                                 "TTLFactor is invalid (%.1f).",
762                                                 tmp);
763                         }
764                 } else {
765                         WARNING("write_riemann plugin: ignoring unknown config "
766                                 "option: \"%s\"", child->key);
767                 }
768         }
769         if (status != 0) {
770                 riemann_free (host);
771                 return status;
772         }
773
774         ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
775                         host->name);
776         ud.data = host;
777         ud.free_func = riemann_free;
778
779         pthread_mutex_lock (&host->lock);
780
781         status = plugin_register_write (callback_name, riemann_write, &ud);
782         if (status != 0)
783                 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
784                                 "failed with status %i.",
785                                 callback_name, status);
786         else /* success */
787                 host->reference_count++;
788
789         status = plugin_register_notification (callback_name,
790                         riemann_notification, &ud);
791         if (status != 0)
792                 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
793                                 "failed with status %i.",
794                                 callback_name, status);
795         else /* success */
796                 host->reference_count++;
797
798         if (host->reference_count <= 1)
799         {
800                 /* Both callbacks failed => free memory.
801                  * We need to unlock here, because riemann_free() will lock.
802                  * This is not a race condition, because we're the only one
803                  * holding a reference. */
804                 pthread_mutex_unlock (&host->lock);
805                 riemann_free (host);
806                 return (-1);
807         }
808
809         host->reference_count--;
810         pthread_mutex_unlock (&host->lock);
811
812         return status;
813 } /* }}} int riemann_config_node */
814
815 static int riemann_config(oconfig_item_t *ci) /* {{{ */
816 {
817         int              i;
818         oconfig_item_t  *child;
819         int              status;
820
821         for (i = 0; i < ci->children_num; i++)  {
822                 child = &ci->children[i];
823
824                 if (strcasecmp("Node", child->key) == 0) {
825                         riemann_config_node (child);
826                 } else if (strcasecmp(child->key, "tag") == 0) {
827                         char *tmp = NULL;
828                         status = cf_util_get_string(child, &tmp);
829                         if (status != 0)
830                                 continue;
831
832                         strarray_add (&riemann_tags, &riemann_tags_num, tmp);
833                         DEBUG("write_riemann plugin: Got tag: %s", tmp);
834                         sfree (tmp);
835                 } else {
836                         WARNING ("write_riemann plugin: Ignoring unknown "
837                                  "configuration option \"%s\" at top level.",
838                                  child->key);
839                 }
840         }
841         return (0);
842 } /* }}} int riemann_config */
843
844 void module_register(void)
845 {
846         plugin_register_complex_config ("write_riemann", riemann_config);
847 }
848
849 /* vim: set sw=8 sts=8 ts=8 noet : */