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
42 int write_riemann_threshold_check(const data_set_t *, const value_list_t *, int *);
46 #define F_CONNECT 0x01
51 _Bool always_append_ds;
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) /* {{{ */
74 sfree (event->service);
76 sfree (event->description);
78 strarray_free (event->tags, event->n_tags);
82 for (i = 0; i < event->n_attributes; i++)
84 sfree (event->attributes[i]->key);
85 sfree (event->attributes[i]->value);
86 sfree (event->attributes[i]);
88 sfree (event->attributes);
89 event->n_attributes = 0;
92 } /* }}} void riemann_event_protobuf_free */
94 static void riemann_msg_protobuf_free (Msg *msg) /* {{{ */
101 for (i = 0; i < msg->n_events; i++)
103 riemann_event_protobuf_free (msg->events[i]);
104 msg->events[i] = NULL;
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) /* {{{ */
117 struct addrinfo *ai, *res, hints;
121 if (host->flags & F_CONNECT)
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;
129 hints.ai_flags |= AI_ADDRCONFIG;
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));
142 for (ai = res; ai != NULL; ai = ai->ai_next) {
143 if ((host->s = socket(ai->ai_family,
145 ai->ai_protocol)) == -1) {
149 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
155 host->flags |= F_CONNECT;
156 DEBUG("write_riemann plugin: got a succesful connection for: %s:%s",
164 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
169 } /* }}} int riemann_connect */
171 /* host->lock must be held when calling this function. */
172 static int riemann_disconnect (struct riemann_host *host) /* {{{ */
174 if ((host->flags & F_CONNECT) == 0)
179 host->flags &= ~F_CONNECT;
182 } /* }}} int riemann_disconnect */
184 static int riemann_send_msg (struct riemann_host *host, const Msg *msg) /* {{{ */
187 u_char *buffer = NULL;
190 status = riemann_connect (host);
194 buffer_len = msg__get_packed_size(msg);
199 buffer = malloc (buffer_len);
200 if (buffer == NULL) {
201 ERROR ("write_riemann plugin: malloc failed.");
204 memset (buffer, 0, buffer_len);
208 uint32_t length = htonl ((uint32_t) (buffer_len - 4));
209 memcpy (buffer, &length, 4);
210 msg__pack(msg, buffer + 4);
214 msg__pack(msg, buffer);
217 status = (int) swrite (host->s, buffer, buffer_len);
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)));
231 } /* }}} int riemann_send_msg */
233 static int riemann_recv_ack(struct riemann_host *host) /* {{{ */
239 status = (int) sread (host->s, &header, 4);
244 size_t size = ntohl(header);
246 // Buffer on the stack since acknowledges are typically small.
248 memset (buffer, 0, size);
250 status = (int) sread (host->s, buffer, size);
255 msg = msg__unpack (NULL, size, buffer);
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,
267 msg__free_unpacked(msg, NULL);
271 msg__free_unpacked (msg, NULL);
273 } /* }}} int riemann_recv_ack */
276 * Function to send messages (Msg) to riemann.
278 * Acquires the host lock, disconnects on errors.
280 static int riemann_send(struct riemann_host *host, Msg const *msg) /* {{{ */
283 pthread_mutex_lock (&host->lock);
285 status = riemann_send_msg(host, msg);
287 riemann_disconnect (host);
288 pthread_mutex_unlock (&host->lock);
293 * For TCP we need to receive message acknowledgemenent.
297 status = riemann_recv_ack(host);
301 riemann_disconnect (host);
302 pthread_mutex_unlock (&host->lock);
307 pthread_mutex_unlock (&host->lock);
309 } /* }}} int riemann_send */
311 static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
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)
319 Attribute **new_attributes;
322 new_attributes = realloc (event->attributes,
323 sizeof (*event->attributes) * (event->n_attributes + 1));
324 if (new_attributes == NULL)
326 ERROR ("write_riemann plugin: realloc failed.");
329 event->attributes = new_attributes;
331 a = malloc (sizeof (*a));
334 ERROR ("write_riemann plugin: malloc failed.");
339 a->key = strdup (key);
341 a->value = strdup (value);
343 event->attributes[event->n_attributes] = a;
344 event->n_attributes++;
347 } /* }}} int riemann_event_add_attribute */
349 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
350 notification_t const *n)
354 char service_buffer[6 * DATA_MAX_NAME_LEN];
355 char const *severity;
356 notification_meta_t *meta;
359 msg = malloc (sizeof (*msg));
362 ERROR ("write_riemann plugin: malloc failed.");
365 memset (msg, 0, sizeof (*msg));
368 msg->events = malloc (sizeof (*msg->events));
369 if (msg->events == NULL)
371 ERROR ("write_riemann plugin: malloc failed.");
376 event = malloc (sizeof (*event));
379 ERROR ("write_riemann plugin: malloc failed.");
384 memset (event, 0, sizeof (*event));
387 msg->events[0] = event;
390 event->host = strdup (n->host);
391 event->time = CDTIME_T_TO_TIME_T (n->time);
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";
401 event->state = strdup (severity);
403 riemann_event_add_tag (event, "notification");
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",
413 riemann_event_add_attribute (event, "type", n->type);
414 if (n->type_instance[0] != 0)
415 riemann_event_add_attribute (event, "type_instance",
418 for (i = 0; i < riemann_attrs_num; i += 2)
419 riemann_event_add_attribute(event,
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 /* 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,
460 char name_buffer[5 * DATA_MAX_NAME_LEN];
461 char service_buffer[6 * DATA_MAX_NAME_LEN];
465 event = malloc (sizeof (*event));
468 ERROR ("write_riemann plugin: malloc failed.");
471 memset (event, 0, sizeof (*event));
474 event->host = strdup (vl->host);
475 event->time = CDTIME_T_TO_TIME_T (vl->time);
480 event->state = strdup("ok");
483 event->state = strdup("critical");
486 event->state = strdup("warning");
489 event->state = strdup("unknown");
493 ttl = CDTIME_T_TO_DOUBLE (vl->interval) * host->ttl_factor;
494 event->ttl = (float) ttl;
497 riemann_event_add_attribute (event, "plugin", vl->plugin);
498 if (vl->plugin_instance[0] != 0)
499 riemann_event_add_attribute (event, "plugin_instance",
500 vl->plugin_instance);
502 riemann_event_add_attribute (event, "type", vl->type);
503 if (vl->type_instance[0] != 0)
504 riemann_event_add_attribute (event, "type_instance",
507 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
509 char ds_type[DATA_MAX_NAME_LEN];
511 ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
512 DS_TYPE_TO_STRING(ds->ds[index].type));
513 riemann_event_add_attribute (event, "ds_type", ds_type);
517 riemann_event_add_attribute (event, "ds_type",
518 DS_TYPE_TO_STRING(ds->ds[index].type));
520 riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
522 char ds_index[DATA_MAX_NAME_LEN];
524 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
525 riemann_event_add_attribute (event, "ds_index", ds_index);
528 for (i = 0; i < riemann_attrs_num; i += 2)
529 riemann_event_add_attribute(event,
531 riemann_attrs[i +1]);
533 for (i = 0; i < riemann_tags_num; i++)
534 riemann_event_add_tag (event, riemann_tags[i]);
536 if (ds->ds[index].type == DS_TYPE_GAUGE)
538 event->has_metric_d = 1;
539 event->metric_d = (double) vl->values[index].gauge;
541 else if (rates != NULL)
543 event->has_metric_d = 1;
544 event->metric_d = (double) rates[index];
548 event->has_metric_sint64 = 1;
549 if (ds->ds[index].type == DS_TYPE_DERIVE)
550 event->metric_sint64 = (int64_t) vl->values[index].derive;
551 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
552 event->metric_sint64 = (int64_t) vl->values[index].absolute;
554 event->metric_sint64 = (int64_t) vl->values[index].counter;
557 format_name (name_buffer, sizeof (name_buffer),
558 /* host = */ "", vl->plugin, vl->plugin_instance,
559 vl->type, vl->type_instance);
560 if (host->always_append_ds || (ds->ds_num > 1))
561 ssnprintf (service_buffer, sizeof (service_buffer),
562 "%s/%s", &name_buffer[1], ds->ds[index].name);
564 sstrncpy (service_buffer, &name_buffer[1],
565 sizeof (service_buffer));
567 event->service = strdup (service_buffer);
569 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
570 "host = \"%s\", service = \"%s\"",
571 event->host, event->service);
573 } /* }}} Event *riemann_value_to_protobuf */
575 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
576 data_set_t const *ds,
577 value_list_t const *vl,
582 gauge_t *rates = NULL;
584 /* Initialize the Msg structure. */
585 msg = malloc (sizeof (*msg));
588 ERROR ("write_riemann plugin: malloc failed.");
591 memset (msg, 0, sizeof (*msg));
594 /* Set up events. First, the list of pointers. */
595 msg->n_events = (size_t) vl->values_len;
596 msg->events = calloc (msg->n_events, sizeof (*msg->events));
597 if (msg->events == NULL)
599 ERROR ("write_riemann plugin: calloc failed.");
600 riemann_msg_protobuf_free (msg);
604 if (host->store_rates)
606 rates = uc_get_rate (ds, vl);
609 ERROR ("write_riemann plugin: uc_get_rate failed.");
610 riemann_msg_protobuf_free (msg);
615 for (i = 0; i < msg->n_events; i++)
617 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
618 (int) i, rates, statuses[i]);
619 if (msg->events[i] == NULL)
621 riemann_msg_protobuf_free (msg);
629 } /* }}} Msg *riemann_value_list_to_protobuf */
631 static int riemann_notification(const notification_t *n, user_data_t *ud) /* {{{ */
634 struct riemann_host *host = ud->data;
637 if (!host->notifications)
640 msg = riemann_notification_to_protobuf (host, n);
644 status = riemann_send (host, msg);
646 ERROR ("write_riemann plugin: riemann_send failed with status %i",
649 riemann_msg_protobuf_free (msg);
651 } /* }}} int riemann_notification */
653 static int riemann_write(const data_set_t *ds, /* {{{ */
654 const value_list_t *vl,
658 int statuses[vl->values_len];
659 struct riemann_host *host = ud->data;
662 write_riemann_threshold_check(ds, vl, statuses);
663 msg = riemann_value_list_to_protobuf (host, ds, vl, statuses);
667 status = riemann_send (host, msg);
669 ERROR ("write_riemann plugin: riemann_send failed with status %i",
672 riemann_msg_protobuf_free (msg);
674 } /* }}} int riemann_write */
676 static void riemann_free(void *p) /* {{{ */
678 struct riemann_host *host = p;
683 pthread_mutex_lock (&host->lock);
685 host->reference_count--;
686 if (host->reference_count > 0)
688 pthread_mutex_unlock (&host->lock);
692 riemann_disconnect (host);
694 sfree(host->service);
695 pthread_mutex_destroy (&host->lock);
697 } /* }}} void riemann_free */
699 static int riemann_config_node(oconfig_item_t *ci) /* {{{ */
701 struct riemann_host *host = NULL;
704 oconfig_item_t *child;
705 char callback_name[DATA_MAX_NAME_LEN];
708 if ((host = calloc(1, sizeof (*host))) == NULL) {
709 ERROR ("write_riemann plugin: calloc failed.");
712 pthread_mutex_init (&host->lock, NULL);
713 host->reference_count = 1;
715 host->service = NULL;
716 host->notifications = 1;
717 host->store_rates = 1;
718 host->always_append_ds = 0;
720 host->ttl_factor = RIEMANN_TTL_FACTOR;
722 status = cf_util_get_string (ci, &host->name);
724 WARNING("write_riemann plugin: Required host name is missing.");
729 for (i = 0; i < ci->children_num; i++) {
731 * The code here could be simplified but makes room
732 * for easy adding of new options later on.
734 child = &ci->children[i];
737 if (strcasecmp ("Host", child->key) == 0) {
738 status = cf_util_get_string (child, &host->node);
741 } else if (strcasecmp ("Notifications", child->key) == 0) {
742 status = cf_util_get_boolean(child, &host->notifications);
745 } else if (strcasecmp ("Port", child->key) == 0) {
746 status = cf_util_get_service (child, &host->service);
748 ERROR ("write_riemann plugin: Invalid argument "
749 "configured for the \"Port\" "
753 } else if (strcasecmp ("Protocol", child->key) == 0) {
755 status = cf_util_get_string_buffer (child,
759 ERROR ("write_riemann plugin: cf_util_get_"
760 "string_buffer failed with "
761 "status %i.", status);
765 if (strcasecmp ("UDP", tmp) == 0)
767 else if (strcasecmp ("TCP", tmp) == 0)
770 WARNING ("write_riemann plugin: The value "
771 "\"%s\" is not valid for the "
772 "\"Protocol\" option. Use "
773 "either \"UDP\" or \"TCP\".",
775 } else if (strcasecmp ("StoreRates", child->key) == 0) {
776 status = cf_util_get_boolean (child, &host->store_rates);
779 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
780 status = cf_util_get_boolean (child,
781 &host->always_append_ds);
784 } else if (strcasecmp ("TTLFactor", child->key) == 0) {
786 status = cf_util_get_double (child, &tmp);
790 host->ttl_factor = tmp;
791 } else if (tmp >= 1.0) {
792 NOTICE ("write_riemann plugin: The configured "
793 "TTLFactor is very small "
794 "(%.1f). A value of 2.0 or "
795 "greater is recommended.",
797 host->ttl_factor = tmp;
798 } else if (tmp > 0.0) {
799 WARNING ("write_riemann plugin: The configured "
800 "TTLFactor is too small to be "
801 "useful (%.1f). I'll use it "
802 "since the user knows best, "
803 "but under protest.",
805 host->ttl_factor = tmp;
806 } else { /* zero, negative and NAN */
807 ERROR ("write_riemann plugin: The configured "
808 "TTLFactor is invalid (%.1f).",
812 WARNING("write_riemann plugin: ignoring unknown config "
813 "option: \"%s\"", child->key);
821 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
824 ud.free_func = riemann_free;
826 pthread_mutex_lock (&host->lock);
828 status = plugin_register_write (callback_name, riemann_write, &ud);
830 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
831 "failed with status %i.",
832 callback_name, status);
834 host->reference_count++;
836 status = plugin_register_notification (callback_name,
837 riemann_notification, &ud);
839 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
840 "failed with status %i.",
841 callback_name, status);
843 host->reference_count++;
845 if (host->reference_count <= 1)
847 /* Both callbacks failed => free memory.
848 * We need to unlock here, because riemann_free() will lock.
849 * This is not a race condition, because we're the only one
850 * holding a reference. */
851 pthread_mutex_unlock (&host->lock);
856 host->reference_count--;
857 pthread_mutex_unlock (&host->lock);
860 } /* }}} int riemann_config_node */
862 static int riemann_config(oconfig_item_t *ci) /* {{{ */
865 oconfig_item_t *child;
868 for (i = 0; i < ci->children_num; i++) {
869 child = &ci->children[i];
871 if (strcasecmp("Node", child->key) == 0) {
872 riemann_config_node (child);
873 } else if (strcasecmp(child->key, "attribute") == 0) {
877 if (child->values_num != 2) {
878 WARNING("riemann attributes need both a key and a value.");
881 if (child->values[0].type != OCONFIG_TYPE_STRING ||
882 child->values[1].type != OCONFIG_TYPE_STRING) {
883 WARNING("riemann attribute needs string arguments.");
886 if ((key = strdup(child->values[0].value.string)) == NULL) {
887 WARNING("cannot allocate memory for attribute key.");
890 if ((val = strdup(child->values[1].value.string)) == NULL) {
891 WARNING("cannot allocate memory for attribute value.");
894 strarray_add(&riemann_attrs, &riemann_attrs_num, key);
895 strarray_add(&riemann_attrs, &riemann_attrs_num, val);
896 DEBUG("write_riemann: got attr: %s => %s", key, val);
899 } else if (strcasecmp(child->key, "tag") == 0) {
901 status = cf_util_get_string(child, &tmp);
905 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
906 DEBUG("write_riemann plugin: Got tag: %s", tmp);
909 WARNING ("write_riemann plugin: Ignoring unknown "
910 "configuration option \"%s\" at top level.",
915 } /* }}} int riemann_config */
917 void module_register(void)
919 plugin_register_complex_config ("write_riemann", riemann_config);
922 /* vim: set sw=8 sts=8 ts=8 noet : */