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