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