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