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 */
434 for (meta = n->meta; meta != NULL; meta = meta->next)
436 if (strcasecmp ("CurrentValue", meta->name) != 0)
439 event->metric_d = meta->nm_value.nm_double;
440 event->has_metric_d = 1;
444 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
445 "host = \"%s\", service = \"%s\", state = \"%s\"",
446 event->host, event->service, event->state);
448 } /* }}} Msg *riemann_notification_to_protobuf */
450 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
451 data_set_t const *ds,
452 value_list_t const *vl, size_t index,
453 gauge_t const *rates)
456 char name_buffer[5 * DATA_MAX_NAME_LEN];
457 char service_buffer[6 * DATA_MAX_NAME_LEN];
461 event = malloc (sizeof (*event));
464 ERROR ("write_riemann plugin: malloc failed.");
467 memset (event, 0, sizeof (*event));
470 event->host = strdup (vl->host);
471 event->time = CDTIME_T_TO_TIME_T (vl->time);
474 ttl = CDTIME_T_TO_DOUBLE (vl->interval) * host->ttl_factor;
475 event->ttl = (float) ttl;
478 riemann_event_add_attribute (event, "plugin", vl->plugin);
479 if (vl->plugin_instance[0] != 0)
480 riemann_event_add_attribute (event, "plugin_instance",
481 vl->plugin_instance);
483 riemann_event_add_attribute (event, "type", vl->type);
484 if (vl->type_instance[0] != 0)
485 riemann_event_add_attribute (event, "type_instance",
488 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
490 char ds_type[DATA_MAX_NAME_LEN];
492 ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
493 DS_TYPE_TO_STRING(ds->ds[index].type));
494 riemann_event_add_attribute (event, "ds_type", ds_type);
498 riemann_event_add_attribute (event, "ds_type",
499 DS_TYPE_TO_STRING(ds->ds[index].type));
501 riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
503 char ds_index[DATA_MAX_NAME_LEN];
505 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
506 riemann_event_add_attribute (event, "ds_index", ds_index);
509 for (i = 0; i < riemann_tags_num; i++)
510 riemann_event_add_tag (event, riemann_tags[i]);
512 if (ds->ds[index].type == DS_TYPE_GAUGE)
514 event->has_metric_d = 1;
515 event->metric_d = (double) vl->values[index].gauge;
517 else if (rates != NULL)
519 event->has_metric_d = 1;
520 event->metric_d = (double) rates[index];
524 event->has_metric_sint64 = 1;
525 if (ds->ds[index].type == DS_TYPE_DERIVE)
526 event->metric_sint64 = (int64_t) vl->values[index].derive;
527 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
528 event->metric_sint64 = (int64_t) vl->values[index].absolute;
530 event->metric_sint64 = (int64_t) vl->values[index].counter;
533 format_name (name_buffer, sizeof (name_buffer),
534 /* host = */ "", vl->plugin, vl->plugin_instance,
535 vl->type, vl->type_instance);
536 if (host->always_append_ds || (ds->ds_num > 1))
537 ssnprintf (service_buffer, sizeof (service_buffer),
538 "%s/%s", &name_buffer[1], ds->ds[index].name);
540 sstrncpy (service_buffer, &name_buffer[1],
541 sizeof (service_buffer));
543 event->service = strdup (service_buffer);
545 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
546 "host = \"%s\", service = \"%s\"",
547 event->host, event->service);
549 } /* }}} Event *riemann_value_to_protobuf */
551 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
552 data_set_t const *ds,
553 value_list_t const *vl)
557 gauge_t *rates = NULL;
559 /* Initialize the Msg structure. */
560 msg = malloc (sizeof (*msg));
563 ERROR ("write_riemann plugin: malloc failed.");
566 memset (msg, 0, sizeof (*msg));
569 /* Set up events. First, the list of pointers. */
570 msg->n_events = (size_t) vl->values_len;
571 msg->events = calloc (msg->n_events, sizeof (*msg->events));
572 if (msg->events == NULL)
574 ERROR ("write_riemann plugin: calloc failed.");
575 riemann_msg_protobuf_free (msg);
579 if (host->store_rates)
581 rates = uc_get_rate (ds, vl);
584 ERROR ("write_riemann plugin: uc_get_rate failed.");
585 riemann_msg_protobuf_free (msg);
590 for (i = 0; i < msg->n_events; i++)
592 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
594 if (msg->events[i] == NULL)
596 riemann_msg_protobuf_free (msg);
604 } /* }}} Msg *riemann_value_list_to_protobuf */
607 riemann_notification(const notification_t *n, user_data_t *ud)
610 struct riemann_host *host = ud->data;
613 msg = riemann_notification_to_protobuf (host, n);
617 status = riemann_send (host, msg);
619 ERROR ("write_riemann plugin: riemann_send failed with status %i",
622 riemann_msg_protobuf_free (msg);
624 } /* }}} int riemann_notification */
627 riemann_write(const data_set_t *ds,
628 const value_list_t *vl,
632 struct riemann_host *host = ud->data;
635 msg = riemann_value_list_to_protobuf (host, ds, vl);
639 status = riemann_send (host, msg);
641 ERROR ("write_riemann plugin: riemann_send failed with status %i",
644 riemann_msg_protobuf_free (msg);
649 riemann_free(void *p)
651 struct riemann_host *host = p;
656 pthread_mutex_lock (&host->lock);
658 host->reference_count--;
659 if (host->reference_count > 0)
661 pthread_mutex_unlock (&host->lock);
665 riemann_disconnect (host);
667 sfree(host->service);
668 pthread_mutex_destroy (&host->lock);
673 riemann_config_node(oconfig_item_t *ci)
675 struct riemann_host *host = NULL;
678 oconfig_item_t *child;
679 char callback_name[DATA_MAX_NAME_LEN];
682 if ((host = calloc(1, sizeof (*host))) == NULL) {
683 ERROR ("write_riemann plugin: calloc failed.");
686 pthread_mutex_init (&host->lock, NULL);
687 host->reference_count = 1;
689 host->service = NULL;
690 host->store_rates = 1;
691 host->always_append_ds = 0;
693 host->ttl_factor = RIEMANN_TTL_FACTOR;
695 status = cf_util_get_string (ci, &host->name);
697 WARNING("write_riemann plugin: Required host name is missing.");
702 for (i = 0; i < ci->children_num; i++) {
704 * The code here could be simplified but makes room
705 * for easy adding of new options later on.
707 child = &ci->children[i];
710 if (strcasecmp ("Host", child->key) == 0) {
711 status = cf_util_get_string (child, &host->node);
714 } else if (strcasecmp ("Port", child->key) == 0) {
715 status = cf_util_get_service (child, &host->service);
717 ERROR ("write_riemann plugin: Invalid argument "
718 "configured for the \"Port\" "
722 } else if (strcasecmp ("Protocol", child->key) == 0) {
724 status = cf_util_get_string_buffer (child,
728 ERROR ("write_riemann plugin: cf_util_get_"
729 "string_buffer failed with "
730 "status %i.", status);
734 if (strcasecmp ("UDP", tmp) == 0)
736 else if (strcasecmp ("TCP", tmp) == 0)
739 WARNING ("write_riemann plugin: The value "
740 "\"%s\" is not valid for the "
741 "\"Protocol\" option. Use "
742 "either \"UDP\" or \"TCP\".",
744 } else if (strcasecmp ("StoreRates", child->key) == 0) {
745 status = cf_util_get_boolean (child, &host->store_rates);
748 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
749 status = cf_util_get_boolean (child,
750 &host->always_append_ds);
753 } else if (strcasecmp ("TTLFactor", child->key) == 0) {
755 status = cf_util_get_double (child, &tmp);
759 host->ttl_factor = tmp;
760 } else if (tmp >= 1.0) {
761 NOTICE ("write_riemann plugin: The configured "
762 "TTLFactor is very small "
763 "(%.1f). A value of 2.0 or "
764 "greater is recommended.",
766 host->ttl_factor = tmp;
767 } else if (tmp > 0.0) {
768 WARNING ("write_riemann plugin: The configured "
769 "TTLFactor is too small to be "
770 "useful (%.1f). I'll use it "
771 "since the user knows best, "
772 "but under protest.",
774 host->ttl_factor = tmp;
775 } else { /* zero, negative and NAN */
776 ERROR ("write_riemann plugin: The configured "
777 "TTLFactor is invalid (%.1f).",
781 WARNING("write_riemann plugin: ignoring unknown config "
782 "option: \"%s\"", child->key);
790 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
793 ud.free_func = riemann_free;
795 pthread_mutex_lock (&host->lock);
797 status = plugin_register_write (callback_name, riemann_write, &ud);
799 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
800 "failed with status %i.",
801 callback_name, status);
803 host->reference_count++;
805 status = plugin_register_notification (callback_name,
806 riemann_notification, &ud);
808 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
809 "failed with status %i.",
810 callback_name, status);
812 host->reference_count++;
814 if (host->reference_count <= 1)
816 /* Both callbacks failed => free memory.
817 * We need to unlock here, because riemann_free() will lock.
818 * This is not a race condition, because we're the only one
819 * holding a reference. */
820 pthread_mutex_unlock (&host->lock);
825 host->reference_count--;
826 pthread_mutex_unlock (&host->lock);
832 riemann_config(oconfig_item_t *ci)
835 oconfig_item_t *child;
838 for (i = 0; i < ci->children_num; i++) {
839 child = &ci->children[i];
841 if (strcasecmp("Node", child->key) == 0) {
842 riemann_config_node (child);
843 } else if (strcasecmp(child->key, "tag") == 0) {
845 status = cf_util_get_string(child, &tmp);
849 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
850 DEBUG("write_riemann plugin: Got tag: %s", tmp);
853 WARNING ("write_riemann plugin: Ignoring unknown "
854 "configuration option \"%s\" at top level.",
862 module_register(void)
864 plugin_register_complex_config ("write_riemann", riemann_config);
867 /* vim: set sw=8 sts=8 ts=8 noet : */