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