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. */
111 static int riemann_connect(struct riemann_host *host) /* {{{ */
114 struct addrinfo *ai, *res, hints;
118 if (host->flags & F_CONNECT)
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;
126 hints.ai_flags |= AI_ADDRCONFIG;
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));
139 for (ai = res; ai != NULL; ai = ai->ai_next) {
140 if ((host->s = socket(ai->ai_family,
142 ai->ai_protocol)) == -1) {
146 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
152 host->flags |= F_CONNECT;
153 DEBUG("write_riemann plugin: got a succesful connection for: %s:%s",
161 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
166 } /* }}} int riemann_connect */
168 /* host->lock must be held when calling this function. */
169 static int riemann_disconnect (struct riemann_host *host) /* {{{ */
171 if ((host->flags & F_CONNECT) == 0)
176 host->flags &= ~F_CONNECT;
179 } /* }}} int riemann_disconnect */
181 static int riemann_send_msg (struct riemann_host *host, const Msg *msg) /* {{{ */
184 u_char *buffer = NULL;
187 status = riemann_connect (host);
191 buffer_len = msg__get_packed_size(msg);
196 buffer = malloc (buffer_len);
197 if (buffer == NULL) {
198 ERROR ("write_riemann plugin: malloc failed.");
201 memset (buffer, 0, buffer_len);
205 uint32_t length = htonl ((uint32_t) (buffer_len - 4));
206 memcpy (buffer, &length, 4);
207 msg__pack(msg, buffer + 4);
211 msg__pack(msg, buffer);
214 status = (int) swrite (host->s, buffer, buffer_len);
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)));
228 } /* }}} int riemann_send_msg */
230 static int riemann_recv_ack(struct riemann_host *host) /* {{{ */
236 status = (int) sread (host->s, &header, 4);
241 size_t size = ntohl(header);
243 // Buffer on the stack since acknowledges are typically small.
245 memset (buffer, 0, size);
247 status = (int) sread (host->s, buffer, size);
252 msg = msg__unpack (NULL, size, buffer);
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,
264 msg__free_unpacked(msg, NULL);
268 msg__free_unpacked (msg, NULL);
270 } /* }}} int riemann_recv_ack */
273 * Function to send messages (Msg) to riemann.
275 * Acquires the host lock, disconnects on errors.
277 static int riemann_send(struct riemann_host *host, Msg const *msg) /* {{{ */
280 pthread_mutex_lock (&host->lock);
282 status = riemann_send_msg(host, msg);
284 riemann_disconnect (host);
285 pthread_mutex_unlock (&host->lock);
290 * For TCP we need to receive message acknowledgemenent.
294 status = riemann_recv_ack(host);
298 riemann_disconnect (host);
299 pthread_mutex_unlock (&host->lock);
304 pthread_mutex_unlock (&host->lock);
306 } /* }}} int riemann_send */
308 static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
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)
316 Attribute **new_attributes;
319 new_attributes = realloc (event->attributes,
320 sizeof (*event->attributes) * (event->n_attributes + 1));
321 if (new_attributes == NULL)
323 ERROR ("write_riemann plugin: realloc failed.");
326 event->attributes = new_attributes;
328 a = malloc (sizeof (*a));
331 ERROR ("write_riemann plugin: malloc failed.");
336 a->key = strdup (key);
338 a->value = strdup (value);
340 event->attributes[event->n_attributes] = a;
341 event->n_attributes++;
344 } /* }}} int riemann_event_add_attribute */
346 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
347 notification_t const *n)
351 char service_buffer[6 * DATA_MAX_NAME_LEN];
352 char const *severity;
353 notification_meta_t *meta;
356 msg = malloc (sizeof (*msg));
359 ERROR ("write_riemann plugin: malloc failed.");
362 memset (msg, 0, sizeof (*msg));
365 msg->events = malloc (sizeof (*msg->events));
366 if (msg->events == NULL)
368 ERROR ("write_riemann plugin: malloc failed.");
373 event = malloc (sizeof (*event));
376 ERROR ("write_riemann plugin: malloc failed.");
381 memset (event, 0, sizeof (*event));
384 msg->events[0] = event;
387 event->host = strdup (n->host);
388 event->time = CDTIME_T_TO_TIME_T (n->time);
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";
398 event->state = strdup (severity);
400 riemann_event_add_tag (event, "notification");
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",
410 riemann_event_add_attribute (event, "type", n->type);
411 if (n->type_instance[0] != 0)
412 riemann_event_add_attribute (event, "type_instance",
415 for (i = 0; i < riemann_attrs_num; i += 2)
416 riemann_event_add_attribute(event,
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)
434 if (strcasecmp ("CurrentValue", meta->name) == 0 && meta->type == NM_TYPE_DOUBLE)
436 event->metric_d = meta->nm_value.nm_double;
437 event->has_metric_d = 1;
441 if (meta->type == NM_TYPE_STRING) {
442 riemann_event_add_attribute (event, meta->name, meta->nm_value.nm_string);
447 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
448 "host = \"%s\", service = \"%s\", state = \"%s\"",
449 event->host, event->service, event->state);
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)
459 char name_buffer[5 * DATA_MAX_NAME_LEN];
460 char service_buffer[6 * DATA_MAX_NAME_LEN];
464 event = malloc (sizeof (*event));
467 ERROR ("write_riemann plugin: malloc failed.");
470 memset (event, 0, sizeof (*event));
473 event->host = strdup (vl->host);
474 event->time = CDTIME_T_TO_TIME_T (vl->time);
477 ttl = CDTIME_T_TO_DOUBLE (vl->interval) * host->ttl_factor;
478 event->ttl = (float) ttl;
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",
491 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
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);
501 riemann_event_add_attribute (event, "ds_type",
502 DS_TYPE_TO_STRING(ds->ds[index].type));
504 riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
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);
512 for (i = 0; i < riemann_attrs_num; i += 2)
513 riemann_event_add_attribute(event,
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)
522 event->has_metric_d = 1;
523 event->metric_d = (double) vl->values[index].gauge;
525 else if (rates != NULL)
527 event->has_metric_d = 1;
528 event->metric_d = (double) rates[index];
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;
538 event->metric_sint64 = (int64_t) vl->values[index].counter;
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);
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);
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)
565 gauge_t *rates = NULL;
567 /* Initialize the Msg structure. */
568 msg = malloc (sizeof (*msg));
571 ERROR ("write_riemann plugin: malloc failed.");
574 memset (msg, 0, sizeof (*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)
582 ERROR ("write_riemann plugin: calloc failed.");
583 riemann_msg_protobuf_free (msg);
587 if (host->store_rates)
589 rates = uc_get_rate (ds, vl);
592 ERROR ("write_riemann plugin: uc_get_rate failed.");
593 riemann_msg_protobuf_free (msg);
598 for (i = 0; i < msg->n_events; i++)
600 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
602 if (msg->events[i] == NULL)
604 riemann_msg_protobuf_free (msg);
612 } /* }}} Msg *riemann_value_list_to_protobuf */
614 static int riemann_notification(const notification_t *n, user_data_t *ud) /* {{{ */
617 struct riemann_host *host = ud->data;
620 msg = riemann_notification_to_protobuf (host, n);
624 status = riemann_send (host, msg);
626 ERROR ("write_riemann plugin: riemann_send failed with status %i",
629 riemann_msg_protobuf_free (msg);
631 } /* }}} int riemann_notification */
633 static int riemann_write(const data_set_t *ds, /* {{{ */
634 const value_list_t *vl,
638 struct riemann_host *host = ud->data;
641 msg = riemann_value_list_to_protobuf (host, ds, vl);
645 status = riemann_send (host, msg);
647 ERROR ("write_riemann plugin: riemann_send failed with status %i",
650 riemann_msg_protobuf_free (msg);
652 } /* }}} int riemann_write */
654 static void 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);
675 } /* }}} void riemann_free */
677 static int riemann_config_node(oconfig_item_t *ci) /* {{{ */
679 struct riemann_host *host = NULL;
682 oconfig_item_t *child;
683 char callback_name[DATA_MAX_NAME_LEN];
686 if ((host = calloc(1, sizeof (*host))) == NULL) {
687 ERROR ("write_riemann plugin: calloc failed.");
690 pthread_mutex_init (&host->lock, NULL);
691 host->reference_count = 1;
693 host->service = NULL;
694 host->store_rates = 1;
695 host->always_append_ds = 0;
697 host->ttl_factor = RIEMANN_TTL_FACTOR;
699 status = cf_util_get_string (ci, &host->name);
701 WARNING("write_riemann plugin: Required host name is missing.");
706 for (i = 0; i < ci->children_num; i++) {
708 * The code here could be simplified but makes room
709 * for easy adding of new options later on.
711 child = &ci->children[i];
714 if (strcasecmp ("Host", child->key) == 0) {
715 status = cf_util_get_string (child, &host->node);
718 } else if (strcasecmp ("Port", child->key) == 0) {
719 status = cf_util_get_service (child, &host->service);
721 ERROR ("write_riemann plugin: Invalid argument "
722 "configured for the \"Port\" "
726 } else if (strcasecmp ("Protocol", child->key) == 0) {
728 status = cf_util_get_string_buffer (child,
732 ERROR ("write_riemann plugin: cf_util_get_"
733 "string_buffer failed with "
734 "status %i.", status);
738 if (strcasecmp ("UDP", tmp) == 0)
740 else if (strcasecmp ("TCP", tmp) == 0)
743 WARNING ("write_riemann plugin: The value "
744 "\"%s\" is not valid for the "
745 "\"Protocol\" option. Use "
746 "either \"UDP\" or \"TCP\".",
748 } else if (strcasecmp ("StoreRates", child->key) == 0) {
749 status = cf_util_get_boolean (child, &host->store_rates);
752 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
753 status = cf_util_get_boolean (child,
754 &host->always_append_ds);
757 } else if (strcasecmp ("TTLFactor", child->key) == 0) {
759 status = cf_util_get_double (child, &tmp);
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.",
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.",
778 host->ttl_factor = tmp;
779 } else { /* zero, negative and NAN */
780 ERROR ("write_riemann plugin: The configured "
781 "TTLFactor is invalid (%.1f).",
785 WARNING("write_riemann plugin: ignoring unknown config "
786 "option: \"%s\"", child->key);
794 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
797 ud.free_func = riemann_free;
799 pthread_mutex_lock (&host->lock);
801 status = plugin_register_write (callback_name, riemann_write, &ud);
803 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
804 "failed with status %i.",
805 callback_name, status);
807 host->reference_count++;
809 status = plugin_register_notification (callback_name,
810 riemann_notification, &ud);
812 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
813 "failed with status %i.",
814 callback_name, status);
816 host->reference_count++;
818 if (host->reference_count <= 1)
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);
829 host->reference_count--;
830 pthread_mutex_unlock (&host->lock);
833 } /* }}} int riemann_config_node */
835 static int riemann_config(oconfig_item_t *ci) /* {{{ */
838 oconfig_item_t *child;
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) {
850 if (child->values_num != 2) {
851 WARNING("riemann attributes need both a key and a value.");
854 if (child->values[0].type != OCONFIG_TYPE_STRING ||
855 child->values[1].type != OCONFIG_TYPE_STRING) {
856 WARNING("riemann attribute needs string arguments.");
859 if ((key = strdup(child->values[0].value.string)) == NULL) {
860 WARNING("cannot allocate memory for attribute key.");
863 if ((val = strdup(child->values[1].value.string)) == NULL) {
864 WARNING("cannot allocate memory for attribute value.");
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);
872 } else if (strcasecmp(child->key, "tag") == 0) {
874 status = cf_util_get_string(child, &tmp);
878 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
879 DEBUG("write_riemann plugin: Got tag: %s", tmp);
882 WARNING ("write_riemann plugin: Ignoring unknown "
883 "configuration option \"%s\" at top level.",
888 } /* }}} int riemann_config */
890 void module_register(void)
892 plugin_register_complex_config ("write_riemann", riemann_config);
895 /* vim: set sw=8 sts=8 ts=8 noet : */