2 * collectd - src/write_riemann.c
4 * Copyright (C) 2012,2013 Pierre-Yves Ritschard
5 * Copyright (C) 2013 Florian octo Forster
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.
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.
20 * Pierre-Yves Ritschard <pyr at spootnik.org>
21 * Florian octo Forster <octo at collectd.org>
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>
38 #define RIEMANN_HOST "localhost"
39 #define RIEMANN_PORT "5555"
40 #define RIEMANN_TTL_FACTOR 2.0
44 #define F_CONNECT 0x01
48 _Bool always_append_ds;
58 static char **riemann_tags;
59 static size_t riemann_tags_num;
61 static void riemann_event_protobuf_free (Event *event) /* {{{ */
69 sfree (event->service);
71 sfree (event->description);
73 strarray_free (event->tags, event->n_tags);
77 for (i = 0; i < event->n_attributes; i++)
79 sfree (event->attributes[i]->key);
80 sfree (event->attributes[i]->value);
81 sfree (event->attributes[i]);
83 sfree (event->attributes);
84 event->n_attributes = 0;
87 } /* }}} void riemann_event_protobuf_free */
89 static void riemann_msg_protobuf_free (Msg *msg) /* {{{ */
96 for (i = 0; i < msg->n_events; i++)
98 riemann_event_protobuf_free (msg->events[i]);
99 msg->events[i] = NULL;
106 } /* }}} void riemann_msg_protobuf_free */
108 /* host->lock must be held when calling this function. */
110 riemann_connect(struct riemann_host *host)
113 struct addrinfo *ai, *res, hints;
117 if (host->flags & F_CONNECT)
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;
125 hints.ai_flags |= AI_ADDRCONFIG;
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));
138 for (ai = res; ai != NULL; ai = ai->ai_next) {
139 if ((host->s = socket(ai->ai_family,
141 ai->ai_protocol)) == -1) {
145 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
151 host->flags |= F_CONNECT;
152 DEBUG("write_riemann plugin: got a succesful connection for: %s:%s",
160 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
167 /* host->lock must be held when calling this function. */
169 riemann_disconnect (struct riemann_host *host)
171 if ((host->flags & F_CONNECT) == 0)
176 host->flags &= ~F_CONNECT;
182 riemann_send_msg(struct riemann_host *host, const Msg *msg)
185 u_char *buffer = NULL;
188 status = riemann_connect (host);
193 buffer_len = msg__get_packed_size(msg);
198 buffer = malloc (buffer_len);
200 if (buffer == NULL) {
201 ERROR ("write_riemann plugin: malloc failed.");
205 memset (buffer, 0, buffer_len);
209 uint32_t length = htonl ((uint32_t) (buffer_len - 4));
210 memcpy (buffer, &length, 4);
211 msg__pack(msg, buffer + 4);
215 msg__pack(msg, buffer);
218 status = (int) swrite (host->s, buffer, buffer_len);
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)));
238 riemann_recv_ack(struct riemann_host *host)
244 status = (int) sread (host->s, &header, 4);
249 size_t size = ntohl(header);
251 // Buffer on the stack since acknowledges are typically small.
253 memset (buffer, 0, size);
255 status = (int) sread (host->s, buffer, size);
260 msg = msg__unpack (NULL, size, buffer);
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,
272 msg__free_unpacked(msg, NULL);
276 msg__free_unpacked (msg, NULL);
281 * Function to send messages (Msg) to riemann.
283 * Acquires the host lock, disconnects on errors.
286 riemann_send(struct riemann_host *host, Msg const *msg)
289 pthread_mutex_lock (&host->lock);
291 status = riemann_send_msg(host, msg);
294 riemann_disconnect (host);
295 pthread_mutex_unlock (&host->lock);
300 * For TCP we need to receive message acknowledgemenent.
304 status = riemann_recv_ack(host);
308 riemann_disconnect (host);
309 pthread_mutex_unlock (&host->lock);
314 pthread_mutex_unlock (&host->lock);
318 static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
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)
326 Attribute **new_attributes;
329 new_attributes = realloc (event->attributes,
330 sizeof (*event->attributes) * (event->n_attributes + 1));
331 if (new_attributes == NULL)
333 ERROR ("write_riemann plugin: realloc failed.");
336 event->attributes = new_attributes;
338 a = malloc (sizeof (*a));
341 ERROR ("write_riemann plugin: malloc failed.");
346 a->key = strdup (key);
348 a->value = strdup (value);
350 event->attributes[event->n_attributes] = a;
351 event->n_attributes++;
354 } /* }}} int riemann_event_add_attribute */
356 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
357 notification_t const *n)
361 char service_buffer[6 * DATA_MAX_NAME_LEN];
362 char const *severity;
363 notification_meta_t *meta;
366 msg = malloc (sizeof (*msg));
369 ERROR ("write_riemann plugin: malloc failed.");
372 memset (msg, 0, sizeof (*msg));
375 msg->events = malloc (sizeof (*msg->events));
376 if (msg->events == NULL)
378 ERROR ("write_riemann plugin: malloc failed.");
383 event = malloc (sizeof (*event));
386 ERROR ("write_riemann plugin: malloc failed.");
391 memset (event, 0, sizeof (*event));
394 msg->events[0] = event;
397 event->host = strdup (n->host);
398 event->time = CDTIME_T_TO_TIME_T (n->time);
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";
408 event->state = strdup (severity);
410 riemann_event_add_tag (event, "notification");
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",
420 riemann_event_add_attribute (event, "type", n->type);
421 if (n->type_instance[0] != 0)
422 riemann_event_add_attribute (event, "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 and add extra attributes */
434 for (meta = n->meta; meta != NULL; meta = meta->next)
436 if (strcasecmp ("CurrentValue", meta->name) == 0 && meta->type == NM_TYPE_DOUBLE)
438 event->metric_d = meta->nm_value.nm_double;
439 event->has_metric_d = 1;
443 if (meta->type == NM_TYPE_STRING) {
444 riemann_event_add_attribute (event, meta->name, meta->nm_value.nm_string);
449 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
450 "host = \"%s\", service = \"%s\", state = \"%s\"",
451 event->host, event->service, event->state);
453 } /* }}} Msg *riemann_notification_to_protobuf */
455 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
456 data_set_t const *ds,
457 value_list_t const *vl, size_t index,
458 gauge_t const *rates)
461 char name_buffer[5 * DATA_MAX_NAME_LEN];
462 char service_buffer[6 * DATA_MAX_NAME_LEN];
466 event = malloc (sizeof (*event));
469 ERROR ("write_riemann plugin: malloc failed.");
472 memset (event, 0, sizeof (*event));
475 event->host = strdup (vl->host);
476 event->time = CDTIME_T_TO_TIME_T (vl->time);
479 ttl = CDTIME_T_TO_DOUBLE (vl->interval) * host->ttl_factor;
480 event->ttl = (float) ttl;
483 riemann_event_add_attribute (event, "plugin", vl->plugin);
484 if (vl->plugin_instance[0] != 0)
485 riemann_event_add_attribute (event, "plugin_instance",
486 vl->plugin_instance);
488 riemann_event_add_attribute (event, "type", vl->type);
489 if (vl->type_instance[0] != 0)
490 riemann_event_add_attribute (event, "type_instance",
493 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
495 char ds_type[DATA_MAX_NAME_LEN];
497 ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
498 DS_TYPE_TO_STRING(ds->ds[index].type));
499 riemann_event_add_attribute (event, "ds_type", ds_type);
503 riemann_event_add_attribute (event, "ds_type",
504 DS_TYPE_TO_STRING(ds->ds[index].type));
506 riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
508 char ds_index[DATA_MAX_NAME_LEN];
510 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
511 riemann_event_add_attribute (event, "ds_index", ds_index);
514 for (i = 0; i < riemann_tags_num; i++)
515 riemann_event_add_tag (event, riemann_tags[i]);
517 if (ds->ds[index].type == DS_TYPE_GAUGE)
519 event->has_metric_d = 1;
520 event->metric_d = (double) vl->values[index].gauge;
522 else if (rates != NULL)
524 event->has_metric_d = 1;
525 event->metric_d = (double) rates[index];
529 event->has_metric_sint64 = 1;
530 if (ds->ds[index].type == DS_TYPE_DERIVE)
531 event->metric_sint64 = (int64_t) vl->values[index].derive;
532 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
533 event->metric_sint64 = (int64_t) vl->values[index].absolute;
535 event->metric_sint64 = (int64_t) vl->values[index].counter;
538 format_name (name_buffer, sizeof (name_buffer),
539 /* host = */ "", vl->plugin, vl->plugin_instance,
540 vl->type, vl->type_instance);
541 if (host->always_append_ds || (ds->ds_num > 1))
542 ssnprintf (service_buffer, sizeof (service_buffer),
543 "%s/%s", &name_buffer[1], ds->ds[index].name);
545 sstrncpy (service_buffer, &name_buffer[1],
546 sizeof (service_buffer));
548 event->service = strdup (service_buffer);
550 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
551 "host = \"%s\", service = \"%s\"",
552 event->host, event->service);
554 } /* }}} Event *riemann_value_to_protobuf */
556 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
557 data_set_t const *ds,
558 value_list_t const *vl)
562 gauge_t *rates = NULL;
564 /* Initialize the Msg structure. */
565 msg = malloc (sizeof (*msg));
568 ERROR ("write_riemann plugin: malloc failed.");
571 memset (msg, 0, sizeof (*msg));
574 /* Set up events. First, the list of pointers. */
575 msg->n_events = (size_t) vl->values_len;
576 msg->events = calloc (msg->n_events, sizeof (*msg->events));
577 if (msg->events == NULL)
579 ERROR ("write_riemann plugin: calloc failed.");
580 riemann_msg_protobuf_free (msg);
584 if (host->store_rates)
586 rates = uc_get_rate (ds, vl);
589 ERROR ("write_riemann plugin: uc_get_rate failed.");
590 riemann_msg_protobuf_free (msg);
595 for (i = 0; i < msg->n_events; i++)
597 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
599 if (msg->events[i] == NULL)
601 riemann_msg_protobuf_free (msg);
609 } /* }}} Msg *riemann_value_list_to_protobuf */
612 riemann_notification(const notification_t *n, user_data_t *ud)
615 struct riemann_host *host = ud->data;
618 msg = riemann_notification_to_protobuf (host, n);
622 status = riemann_send (host, msg);
624 ERROR ("write_riemann plugin: riemann_send failed with status %i",
627 riemann_msg_protobuf_free (msg);
629 } /* }}} int riemann_notification */
632 riemann_write(const data_set_t *ds,
633 const value_list_t *vl,
637 struct riemann_host *host = ud->data;
640 msg = riemann_value_list_to_protobuf (host, ds, vl);
644 status = riemann_send (host, msg);
646 ERROR ("write_riemann plugin: riemann_send failed with status %i",
649 riemann_msg_protobuf_free (msg);
654 riemann_free(void *p)
656 struct riemann_host *host = p;
661 pthread_mutex_lock (&host->lock);
663 host->reference_count--;
664 if (host->reference_count > 0)
666 pthread_mutex_unlock (&host->lock);
670 riemann_disconnect (host);
672 sfree(host->service);
673 pthread_mutex_destroy (&host->lock);
678 riemann_config_node(oconfig_item_t *ci)
680 struct riemann_host *host = NULL;
683 oconfig_item_t *child;
684 char callback_name[DATA_MAX_NAME_LEN];
687 if ((host = calloc(1, sizeof (*host))) == NULL) {
688 ERROR ("write_riemann plugin: calloc failed.");
691 pthread_mutex_init (&host->lock, NULL);
692 host->reference_count = 1;
694 host->service = NULL;
695 host->store_rates = 1;
696 host->always_append_ds = 0;
698 host->ttl_factor = RIEMANN_TTL_FACTOR;
700 status = cf_util_get_string (ci, &host->name);
702 WARNING("write_riemann plugin: Required host name is missing.");
707 for (i = 0; i < ci->children_num; i++) {
709 * The code here could be simplified but makes room
710 * for easy adding of new options later on.
712 child = &ci->children[i];
715 if (strcasecmp ("Host", child->key) == 0) {
716 status = cf_util_get_string (child, &host->node);
719 } else if (strcasecmp ("Port", child->key) == 0) {
720 status = cf_util_get_service (child, &host->service);
722 ERROR ("write_riemann plugin: Invalid argument "
723 "configured for the \"Port\" "
727 } else if (strcasecmp ("Protocol", child->key) == 0) {
729 status = cf_util_get_string_buffer (child,
733 ERROR ("write_riemann plugin: cf_util_get_"
734 "string_buffer failed with "
735 "status %i.", status);
739 if (strcasecmp ("UDP", tmp) == 0)
741 else if (strcasecmp ("TCP", tmp) == 0)
744 WARNING ("write_riemann plugin: The value "
745 "\"%s\" is not valid for the "
746 "\"Protocol\" option. Use "
747 "either \"UDP\" or \"TCP\".",
749 } else if (strcasecmp ("StoreRates", child->key) == 0) {
750 status = cf_util_get_boolean (child, &host->store_rates);
753 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
754 status = cf_util_get_boolean (child,
755 &host->always_append_ds);
758 } else if (strcasecmp ("TTLFactor", child->key) == 0) {
760 status = cf_util_get_double (child, &tmp);
764 host->ttl_factor = tmp;
765 } else if (tmp >= 1.0) {
766 NOTICE ("write_riemann plugin: The configured "
767 "TTLFactor is very small "
768 "(%.1f). A value of 2.0 or "
769 "greater is recommended.",
771 host->ttl_factor = tmp;
772 } else if (tmp > 0.0) {
773 WARNING ("write_riemann plugin: The configured "
774 "TTLFactor is too small to be "
775 "useful (%.1f). I'll use it "
776 "since the user knows best, "
777 "but under protest.",
779 host->ttl_factor = tmp;
780 } else { /* zero, negative and NAN */
781 ERROR ("write_riemann plugin: The configured "
782 "TTLFactor is invalid (%.1f).",
786 WARNING("write_riemann plugin: ignoring unknown config "
787 "option: \"%s\"", child->key);
795 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
798 ud.free_func = riemann_free;
800 pthread_mutex_lock (&host->lock);
802 status = plugin_register_write (callback_name, riemann_write, &ud);
804 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
805 "failed with status %i.",
806 callback_name, status);
808 host->reference_count++;
810 status = plugin_register_notification (callback_name,
811 riemann_notification, &ud);
813 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
814 "failed with status %i.",
815 callback_name, status);
817 host->reference_count++;
819 if (host->reference_count <= 1)
821 /* Both callbacks failed => free memory.
822 * We need to unlock here, because riemann_free() will lock.
823 * This is not a race condition, because we're the only one
824 * holding a reference. */
825 pthread_mutex_unlock (&host->lock);
830 host->reference_count--;
831 pthread_mutex_unlock (&host->lock);
837 riemann_config(oconfig_item_t *ci)
840 oconfig_item_t *child;
843 for (i = 0; i < ci->children_num; i++) {
844 child = &ci->children[i];
846 if (strcasecmp("Node", child->key) == 0) {
847 riemann_config_node (child);
848 } else if (strcasecmp(child->key, "tag") == 0) {
850 status = cf_util_get_string(child, &tmp);
854 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
855 DEBUG("write_riemann plugin: Got tag: %s", tmp);
858 WARNING ("write_riemann plugin: Ignoring unknown "
859 "configuration option \"%s\" at top level.",
867 module_register(void)
869 plugin_register_complex_config ("write_riemann", riemann_config);
872 /* vim: set sw=8 sts=8 ts=8 noet : */