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;
60 static char **riemann_attrs;
61 static size_t riemann_attrs_num;
63 static void riemann_event_protobuf_free (Event *event) /* {{{ */
71 sfree (event->service);
73 sfree (event->description);
75 strarray_free (event->tags, event->n_tags);
79 for (i = 0; i < event->n_attributes; i++)
81 sfree (event->attributes[i]->key);
82 sfree (event->attributes[i]->value);
83 sfree (event->attributes[i]);
85 sfree (event->attributes);
86 event->n_attributes = 0;
89 } /* }}} void riemann_event_protobuf_free */
91 static void riemann_msg_protobuf_free (Msg *msg) /* {{{ */
98 for (i = 0; i < msg->n_events; i++)
100 riemann_event_protobuf_free (msg->events[i]);
101 msg->events[i] = NULL;
108 } /* }}} void riemann_msg_protobuf_free */
110 /* host->lock must be held when calling this function. */
112 riemann_connect(struct riemann_host *host)
115 struct addrinfo *ai, *res, hints;
119 if (host->flags & F_CONNECT)
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;
127 hints.ai_flags |= AI_ADDRCONFIG;
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));
140 for (ai = res; ai != NULL; ai = ai->ai_next) {
141 if ((host->s = socket(ai->ai_family,
143 ai->ai_protocol)) == -1) {
147 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
153 host->flags |= F_CONNECT;
154 DEBUG("write_riemann plugin: got a succesful connection for: %s:%s",
162 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
169 /* host->lock must be held when calling this function. */
171 riemann_disconnect (struct riemann_host *host)
173 if ((host->flags & F_CONNECT) == 0)
178 host->flags &= ~F_CONNECT;
184 riemann_send_msg(struct riemann_host *host, const Msg *msg)
187 u_char *buffer = NULL;
190 status = riemann_connect (host);
195 buffer_len = msg__get_packed_size(msg);
200 buffer = malloc (buffer_len);
202 if (buffer == NULL) {
203 ERROR ("write_riemann plugin: malloc failed.");
207 memset (buffer, 0, buffer_len);
211 uint32_t length = htonl ((uint32_t) (buffer_len - 4));
212 memcpy (buffer, &length, 4);
213 msg__pack(msg, buffer + 4);
217 msg__pack(msg, buffer);
220 status = (int) swrite (host->s, buffer, buffer_len);
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)));
240 riemann_recv_ack(struct riemann_host *host)
246 status = (int) sread (host->s, &header, 4);
251 size_t size = ntohl(header);
253 // Buffer on the stack since acknowledges are typically small.
255 memset (buffer, 0, size);
257 status = (int) sread (host->s, buffer, size);
262 msg = msg__unpack (NULL, size, buffer);
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,
274 msg__free_unpacked(msg, NULL);
278 msg__free_unpacked (msg, NULL);
283 * Function to send messages (Msg) to riemann.
285 * Acquires the host lock, disconnects on errors.
288 riemann_send(struct riemann_host *host, Msg const *msg)
291 pthread_mutex_lock (&host->lock);
293 status = riemann_send_msg(host, msg);
296 riemann_disconnect (host);
297 pthread_mutex_unlock (&host->lock);
302 * For TCP we need to receive message acknowledgemenent.
306 status = riemann_recv_ack(host);
310 riemann_disconnect (host);
311 pthread_mutex_unlock (&host->lock);
316 pthread_mutex_unlock (&host->lock);
320 static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
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)
328 Attribute **new_attributes;
331 new_attributes = realloc (event->attributes,
332 sizeof (*event->attributes) * (event->n_attributes + 1));
333 if (new_attributes == NULL)
335 ERROR ("write_riemann plugin: realloc failed.");
338 event->attributes = new_attributes;
340 a = malloc (sizeof (*a));
343 ERROR ("write_riemann plugin: malloc failed.");
348 a->key = strdup (key);
350 a->value = strdup (value);
352 event->attributes[event->n_attributes] = a;
353 event->n_attributes++;
356 } /* }}} int riemann_event_add_attribute */
358 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
359 notification_t const *n)
363 char service_buffer[6 * DATA_MAX_NAME_LEN];
364 char const *severity;
365 notification_meta_t *meta;
368 msg = malloc (sizeof (*msg));
371 ERROR ("write_riemann plugin: malloc failed.");
374 memset (msg, 0, sizeof (*msg));
377 msg->events = malloc (sizeof (*msg->events));
378 if (msg->events == NULL)
380 ERROR ("write_riemann plugin: malloc failed.");
385 event = malloc (sizeof (*event));
388 ERROR ("write_riemann plugin: malloc failed.");
393 memset (event, 0, sizeof (*event));
396 msg->events[0] = event;
399 event->host = strdup (n->host);
400 event->time = CDTIME_T_TO_TIME_T (n->time);
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";
410 event->state = strdup (severity);
412 riemann_event_add_tag (event, "notification");
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",
422 riemann_event_add_attribute (event, "type", n->type);
423 if (n->type_instance[0] != 0)
424 riemann_event_add_attribute (event, "type_instance",
427 for (i = 0; i < riemann_attrs_num; i += 2)
428 riemann_event_add_attribute(event,
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)
443 if (strcasecmp ("CurrentValue", meta->name) == 0 && meta->type == NM_TYPE_DOUBLE)
445 event->metric_d = meta->nm_value.nm_double;
446 event->has_metric_d = 1;
450 if (meta->type == NM_TYPE_STRING) {
451 riemann_event_add_attribute (event, meta->name, meta->nm_value.nm_string);
456 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
457 "host = \"%s\", service = \"%s\", state = \"%s\"",
458 event->host, event->service, event->state);
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)
468 char name_buffer[5 * DATA_MAX_NAME_LEN];
469 char service_buffer[6 * DATA_MAX_NAME_LEN];
473 event = malloc (sizeof (*event));
476 ERROR ("write_riemann plugin: malloc failed.");
479 memset (event, 0, sizeof (*event));
482 event->host = strdup (vl->host);
483 event->time = CDTIME_T_TO_TIME_T (vl->time);
486 ttl = CDTIME_T_TO_DOUBLE (vl->interval) * host->ttl_factor;
487 event->ttl = (float) ttl;
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",
500 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
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);
510 riemann_event_add_attribute (event, "ds_type",
511 DS_TYPE_TO_STRING(ds->ds[index].type));
513 riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
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);
521 for (i = 0; i < riemann_attrs_num; i += 2)
522 riemann_event_add_attribute(event,
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)
531 event->has_metric_d = 1;
532 event->metric_d = (double) vl->values[index].gauge;
534 else if (rates != NULL)
536 event->has_metric_d = 1;
537 event->metric_d = (double) rates[index];
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;
547 event->metric_sint64 = (int64_t) vl->values[index].counter;
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);
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);
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)
574 gauge_t *rates = NULL;
576 /* Initialize the Msg structure. */
577 msg = malloc (sizeof (*msg));
580 ERROR ("write_riemann plugin: malloc failed.");
583 memset (msg, 0, sizeof (*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)
591 ERROR ("write_riemann plugin: calloc failed.");
592 riemann_msg_protobuf_free (msg);
596 if (host->store_rates)
598 rates = uc_get_rate (ds, vl);
601 ERROR ("write_riemann plugin: uc_get_rate failed.");
602 riemann_msg_protobuf_free (msg);
607 for (i = 0; i < msg->n_events; i++)
609 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
611 if (msg->events[i] == NULL)
613 riemann_msg_protobuf_free (msg);
621 } /* }}} Msg *riemann_value_list_to_protobuf */
624 riemann_notification(const notification_t *n, user_data_t *ud)
627 struct riemann_host *host = ud->data;
630 msg = riemann_notification_to_protobuf (host, n);
634 status = riemann_send (host, msg);
636 ERROR ("write_riemann plugin: riemann_send failed with status %i",
639 riemann_msg_protobuf_free (msg);
641 } /* }}} int riemann_notification */
644 riemann_write(const data_set_t *ds,
645 const value_list_t *vl,
649 struct riemann_host *host = ud->data;
652 msg = riemann_value_list_to_protobuf (host, ds, vl);
656 status = riemann_send (host, msg);
658 ERROR ("write_riemann plugin: riemann_send failed with status %i",
661 riemann_msg_protobuf_free (msg);
666 riemann_free(void *p)
668 struct riemann_host *host = p;
673 pthread_mutex_lock (&host->lock);
675 host->reference_count--;
676 if (host->reference_count > 0)
678 pthread_mutex_unlock (&host->lock);
682 riemann_disconnect (host);
684 sfree(host->service);
685 pthread_mutex_destroy (&host->lock);
690 riemann_config_node(oconfig_item_t *ci)
692 struct riemann_host *host = NULL;
695 oconfig_item_t *child;
696 char callback_name[DATA_MAX_NAME_LEN];
699 if ((host = calloc(1, sizeof (*host))) == NULL) {
700 ERROR ("write_riemann plugin: calloc failed.");
703 pthread_mutex_init (&host->lock, NULL);
704 host->reference_count = 1;
706 host->service = NULL;
707 host->store_rates = 1;
708 host->always_append_ds = 0;
710 host->ttl_factor = RIEMANN_TTL_FACTOR;
712 status = cf_util_get_string (ci, &host->name);
714 WARNING("write_riemann plugin: Required host name is missing.");
719 for (i = 0; i < ci->children_num; i++) {
721 * The code here could be simplified but makes room
722 * for easy adding of new options later on.
724 child = &ci->children[i];
727 if (strcasecmp ("Host", child->key) == 0) {
728 status = cf_util_get_string (child, &host->node);
731 } else if (strcasecmp ("Port", child->key) == 0) {
732 status = cf_util_get_service (child, &host->service);
734 ERROR ("write_riemann plugin: Invalid argument "
735 "configured for the \"Port\" "
739 } else if (strcasecmp ("Protocol", child->key) == 0) {
741 status = cf_util_get_string_buffer (child,
745 ERROR ("write_riemann plugin: cf_util_get_"
746 "string_buffer failed with "
747 "status %i.", status);
751 if (strcasecmp ("UDP", tmp) == 0)
753 else if (strcasecmp ("TCP", tmp) == 0)
756 WARNING ("write_riemann plugin: The value "
757 "\"%s\" is not valid for the "
758 "\"Protocol\" option. Use "
759 "either \"UDP\" or \"TCP\".",
761 } else if (strcasecmp ("StoreRates", child->key) == 0) {
762 status = cf_util_get_boolean (child, &host->store_rates);
765 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
766 status = cf_util_get_boolean (child,
767 &host->always_append_ds);
770 } else if (strcasecmp ("TTLFactor", child->key) == 0) {
772 status = cf_util_get_double (child, &tmp);
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.",
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.",
791 host->ttl_factor = tmp;
792 } else { /* zero, negative and NAN */
793 ERROR ("write_riemann plugin: The configured "
794 "TTLFactor is invalid (%.1f).",
798 WARNING("write_riemann plugin: ignoring unknown config "
799 "option: \"%s\"", child->key);
807 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
810 ud.free_func = riemann_free;
812 pthread_mutex_lock (&host->lock);
814 status = plugin_register_write (callback_name, riemann_write, &ud);
816 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
817 "failed with status %i.",
818 callback_name, status);
820 host->reference_count++;
822 status = plugin_register_notification (callback_name,
823 riemann_notification, &ud);
825 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
826 "failed with status %i.",
827 callback_name, status);
829 host->reference_count++;
831 if (host->reference_count <= 1)
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);
842 host->reference_count--;
843 pthread_mutex_unlock (&host->lock);
849 riemann_config(oconfig_item_t *ci)
852 oconfig_item_t *child;
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) {
864 if (child->values_num != 2) {
865 WARNING("riemann attributes need both a key and a value.");
868 if (child->values[0].type != OCONFIG_TYPE_STRING ||
869 child->values[1].type != OCONFIG_TYPE_STRING) {
870 WARNING("riemann attribute needs string arguments.");
873 if ((key = strdup(child->values[0].value.string)) == NULL) {
874 WARNING("cannot allocate memory for attribute key.");
877 if ((val = strdup(child->values[1].value.string)) == NULL) {
878 WARNING("cannot allocate memory for attribute value.");
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);
886 } else if (strcasecmp(child->key, "tag") == 0) {
888 status = cf_util_get_string(child, &tmp);
892 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
893 DEBUG("write_riemann plugin: Got tag: %s", tmp);
896 WARNING ("write_riemann plugin: Ignoring unknown "
897 "configuration option \"%s\" at top level.",
905 module_register(void)
907 plugin_register_complex_config ("write_riemann", riemann_config);
910 /* vim: set sw=8 sts=8 ts=8 noet : */