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