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