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