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 /* Pull in values from threshold and add extra attributes */
429 for (meta = n->meta; meta != NULL; meta = meta->next)
431 if (strcasecmp ("CurrentValue", meta->name) == 0 && meta->type == NM_TYPE_DOUBLE)
433 event->metric_d = meta->nm_value.nm_double;
434 event->has_metric_d = 1;
438 if (meta->type == NM_TYPE_STRING) {
439 riemann_event_add_attribute (event, meta->name, meta->nm_value.nm_string);
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_attrs_num; i += 2)
510 riemann_event_add_attribute(event,
512 riemann_attrs[i +1]);
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 */
611 static int riemann_notification(const notification_t *n, user_data_t *ud) /* {{{ */
614 struct riemann_host *host = ud->data;
617 msg = riemann_notification_to_protobuf (host, n);
621 status = riemann_send (host, msg);
623 ERROR ("write_riemann plugin: riemann_send failed with status %i",
626 riemann_msg_protobuf_free (msg);
628 } /* }}} int riemann_notification */
630 static int riemann_write(const data_set_t *ds, /* {{{ */
631 const value_list_t *vl,
635 struct riemann_host *host = ud->data;
638 msg = riemann_value_list_to_protobuf (host, ds, vl);
642 status = riemann_send (host, msg);
644 ERROR ("write_riemann plugin: riemann_send failed with status %i",
647 riemann_msg_protobuf_free (msg);
649 } /* }}} int riemann_write */
651 static void riemann_free(void *p) /* {{{ */
653 struct riemann_host *host = p;
658 pthread_mutex_lock (&host->lock);
660 host->reference_count--;
661 if (host->reference_count > 0)
663 pthread_mutex_unlock (&host->lock);
667 riemann_disconnect (host);
669 sfree(host->service);
670 pthread_mutex_destroy (&host->lock);
672 } /* }}} void riemann_free */
674 static int riemann_config_node(oconfig_item_t *ci) /* {{{ */
676 struct riemann_host *host = NULL;
679 oconfig_item_t *child;
680 char callback_name[DATA_MAX_NAME_LEN];
683 if ((host = calloc(1, sizeof (*host))) == NULL) {
684 ERROR ("write_riemann plugin: calloc failed.");
687 pthread_mutex_init (&host->lock, NULL);
688 host->reference_count = 1;
690 host->service = NULL;
691 host->store_rates = 1;
692 host->always_append_ds = 0;
694 host->ttl_factor = RIEMANN_TTL_FACTOR;
696 status = cf_util_get_string (ci, &host->name);
698 WARNING("write_riemann plugin: Required host name is missing.");
703 for (i = 0; i < ci->children_num; i++) {
705 * The code here could be simplified but makes room
706 * for easy adding of new options later on.
708 child = &ci->children[i];
711 if (strcasecmp ("Host", child->key) == 0) {
712 status = cf_util_get_string (child, &host->node);
715 } else if (strcasecmp ("Port", child->key) == 0) {
716 status = cf_util_get_service (child, &host->service);
718 ERROR ("write_riemann plugin: Invalid argument "
719 "configured for the \"Port\" "
723 } else if (strcasecmp ("Protocol", child->key) == 0) {
725 status = cf_util_get_string_buffer (child,
729 ERROR ("write_riemann plugin: cf_util_get_"
730 "string_buffer failed with "
731 "status %i.", status);
735 if (strcasecmp ("UDP", tmp) == 0)
737 else if (strcasecmp ("TCP", tmp) == 0)
740 WARNING ("write_riemann plugin: The value "
741 "\"%s\" is not valid for the "
742 "\"Protocol\" option. Use "
743 "either \"UDP\" or \"TCP\".",
745 } else if (strcasecmp ("StoreRates", child->key) == 0) {
746 status = cf_util_get_boolean (child, &host->store_rates);
749 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
750 status = cf_util_get_boolean (child,
751 &host->always_append_ds);
754 } else if (strcasecmp ("TTLFactor", child->key) == 0) {
756 status = cf_util_get_double (child, &tmp);
760 host->ttl_factor = tmp;
761 } else if (tmp >= 1.0) {
762 NOTICE ("write_riemann plugin: The configured "
763 "TTLFactor is very small "
764 "(%.1f). A value of 2.0 or "
765 "greater is recommended.",
767 host->ttl_factor = tmp;
768 } else if (tmp > 0.0) {
769 WARNING ("write_riemann plugin: The configured "
770 "TTLFactor is too small to be "
771 "useful (%.1f). I'll use it "
772 "since the user knows best, "
773 "but under protest.",
775 host->ttl_factor = tmp;
776 } else { /* zero, negative and NAN */
777 ERROR ("write_riemann plugin: The configured "
778 "TTLFactor is invalid (%.1f).",
782 WARNING("write_riemann plugin: ignoring unknown config "
783 "option: \"%s\"", child->key);
791 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
794 ud.free_func = riemann_free;
796 pthread_mutex_lock (&host->lock);
798 status = plugin_register_write (callback_name, riemann_write, &ud);
800 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
801 "failed with status %i.",
802 callback_name, status);
804 host->reference_count++;
806 status = plugin_register_notification (callback_name,
807 riemann_notification, &ud);
809 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
810 "failed with status %i.",
811 callback_name, status);
813 host->reference_count++;
815 if (host->reference_count <= 1)
817 /* Both callbacks failed => free memory.
818 * We need to unlock here, because riemann_free() will lock.
819 * This is not a race condition, because we're the only one
820 * holding a reference. */
821 pthread_mutex_unlock (&host->lock);
826 host->reference_count--;
827 pthread_mutex_unlock (&host->lock);
830 } /* }}} int riemann_config_node */
832 static int 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, "attribute") == 0) {
847 if (child->values_num != 2) {
848 WARNING("riemann attributes need both a key and a value.");
851 if (child->values[0].type != OCONFIG_TYPE_STRING ||
852 child->values[1].type != OCONFIG_TYPE_STRING) {
853 WARNING("riemann attribute needs string arguments.");
856 if ((key = strdup(child->values[0].value.string)) == NULL) {
857 WARNING("cannot allocate memory for attribute key.");
860 if ((val = strdup(child->values[1].value.string)) == NULL) {
861 WARNING("cannot allocate memory for attribute value.");
864 strarray_add(&riemann_attrs, &riemann_attrs_num, key);
865 strarray_add(&riemann_attrs, &riemann_attrs_num, val);
866 DEBUG("write_riemann: got attr: %s => %s", key, val);
869 } else if (strcasecmp(child->key, "tag") == 0) {
871 status = cf_util_get_string(child, &tmp);
875 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
876 DEBUG("write_riemann plugin: Got tag: %s", tmp);
879 WARNING ("write_riemann plugin: Ignoring unknown "
880 "configuration option \"%s\" at top level.",
885 } /* }}} int riemann_config */
887 void module_register(void)
889 plugin_register_complex_config ("write_riemann", riemann_config);
892 /* vim: set sw=8 sts=8 ts=8 noet : */