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