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. */
109 static int riemann_connect(struct riemann_host *host) /* {{{ */
112 struct addrinfo *ai, *res, hints;
116 if (host->flags & F_CONNECT)
119 memset(&hints, 0, sizeof(hints));
120 memset(&service, 0, sizeof(service));
121 hints.ai_family = AF_UNSPEC;
122 hints.ai_socktype = host->use_tcp ? SOCK_STREAM : SOCK_DGRAM;
124 hints.ai_flags |= AI_ADDRCONFIG;
127 node = (host->node != NULL) ? host->node : RIEMANN_HOST;
128 service = (host->service != NULL) ? host->service : RIEMANN_PORT;
130 if ((e = getaddrinfo(node, service, &hints, &res)) != 0) {
131 ERROR ("write_riemann plugin: Unable to resolve host \"%s\": %s",
132 node, gai_strerror(e));
137 for (ai = res; ai != NULL; ai = ai->ai_next) {
138 if ((host->s = socket(ai->ai_family,
140 ai->ai_protocol)) == -1) {
144 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
150 host->flags |= F_CONNECT;
151 DEBUG("write_riemann plugin: got a succesful connection for: %s:%s",
159 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
164 } /* }}} int riemann_connect */
166 /* host->lock must be held when calling this function. */
167 static int riemann_disconnect (struct riemann_host *host) /* {{{ */
169 if ((host->flags & F_CONNECT) == 0)
174 host->flags &= ~F_CONNECT;
177 } /* }}} int riemann_disconnect */
179 static int riemann_send_msg (struct riemann_host *host, const Msg *msg) /* {{{ */
182 u_char *buffer = NULL;
185 status = riemann_connect (host);
189 buffer_len = msg__get_packed_size(msg);
194 buffer = malloc (buffer_len);
195 if (buffer == NULL) {
196 ERROR ("write_riemann plugin: malloc failed.");
199 memset (buffer, 0, buffer_len);
203 uint32_t length = htonl ((uint32_t) (buffer_len - 4));
204 memcpy (buffer, &length, 4);
205 msg__pack(msg, buffer + 4);
209 msg__pack(msg, buffer);
212 status = (int) swrite (host->s, buffer, buffer_len);
216 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s",
217 (host->node != NULL) ? host->node : RIEMANN_HOST,
218 (host->service != NULL) ? host->service : RIEMANN_PORT,
219 sstrerror (errno, errbuf, sizeof (errbuf)));
226 } /* }}} int riemann_send_msg */
228 static int riemann_recv_ack(struct riemann_host *host) /* {{{ */
234 status = (int) sread (host->s, &header, 4);
239 size_t size = ntohl(header);
241 // Buffer on the stack since acknowledges are typically small.
243 memset (buffer, 0, size);
245 status = (int) sread (host->s, buffer, size);
250 msg = msg__unpack (NULL, size, buffer);
257 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s acknowledgement message reported error: %s",
258 (host->node != NULL) ? host->node : RIEMANN_HOST,
259 (host->service != NULL) ? host->service : RIEMANN_PORT,
262 msg__free_unpacked(msg, NULL);
266 msg__free_unpacked (msg, NULL);
268 } /* }}} int riemann_recv_ack */
271 * Function to send messages (Msg) to riemann.
273 * Acquires the host lock, disconnects on errors.
275 static int riemann_send(struct riemann_host *host, Msg const *msg) /* {{{ */
278 pthread_mutex_lock (&host->lock);
280 status = riemann_send_msg(host, msg);
282 riemann_disconnect (host);
283 pthread_mutex_unlock (&host->lock);
288 * For TCP we need to receive message acknowledgemenent.
292 status = riemann_recv_ack(host);
296 riemann_disconnect (host);
297 pthread_mutex_unlock (&host->lock);
302 pthread_mutex_unlock (&host->lock);
304 } /* }}} int riemann_send */
306 static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
308 return (strarray_add (&event->tags, &event->n_tags, tag));
309 } /* }}} int riemann_event_add_tag */
311 static int riemann_event_add_attribute(Event *event, /* {{{ */
312 char const *key, char const *value)
314 Attribute **new_attributes;
317 new_attributes = realloc (event->attributes,
318 sizeof (*event->attributes) * (event->n_attributes + 1));
319 if (new_attributes == NULL)
321 ERROR ("write_riemann plugin: realloc failed.");
324 event->attributes = new_attributes;
326 a = malloc (sizeof (*a));
329 ERROR ("write_riemann plugin: malloc failed.");
334 a->key = strdup (key);
336 a->value = strdup (value);
338 event->attributes[event->n_attributes] = a;
339 event->n_attributes++;
342 } /* }}} int riemann_event_add_attribute */
344 static Msg *riemann_notification_to_protobuf(struct riemann_host *host, /* {{{ */
345 notification_t const *n)
349 char service_buffer[6 * DATA_MAX_NAME_LEN];
350 char const *severity;
351 notification_meta_t *meta;
354 msg = malloc (sizeof (*msg));
357 ERROR ("write_riemann plugin: malloc failed.");
360 memset (msg, 0, sizeof (*msg));
363 msg->events = malloc (sizeof (*msg->events));
364 if (msg->events == NULL)
366 ERROR ("write_riemann plugin: malloc failed.");
371 event = malloc (sizeof (*event));
374 ERROR ("write_riemann plugin: malloc failed.");
379 memset (event, 0, sizeof (*event));
382 msg->events[0] = event;
385 event->host = strdup (n->host);
386 event->time = CDTIME_T_TO_TIME_T (n->time);
391 case NOTIF_OKAY: severity = "ok"; break;
392 case NOTIF_WARNING: severity = "warning"; break;
393 case NOTIF_FAILURE: severity = "critical"; break;
394 default: severity = "unknown";
396 event->state = strdup (severity);
398 riemann_event_add_tag (event, "notification");
400 riemann_event_add_attribute (event, "host", n->host);
401 if (n->plugin[0] != 0)
402 riemann_event_add_attribute (event, "plugin", n->plugin);
403 if (n->plugin_instance[0] != 0)
404 riemann_event_add_attribute (event, "plugin_instance",
408 riemann_event_add_attribute (event, "type", n->type);
409 if (n->type_instance[0] != 0)
410 riemann_event_add_attribute (event, "type_instance",
413 for (i = 0; i < riemann_tags_num; i++)
414 riemann_event_add_tag (event, riemann_tags[i]);
416 format_name (service_buffer, sizeof (service_buffer),
417 /* host = */ "", n->plugin, n->plugin_instance,
418 n->type, n->type_instance);
419 event->service = strdup (&service_buffer[1]);
421 /* Pull in values from threshold */
422 for (meta = n->meta; meta != NULL; meta = meta->next)
424 if (strcasecmp ("CurrentValue", meta->name) != 0)
427 event->metric_d = meta->nm_value.nm_double;
428 event->has_metric_d = 1;
432 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
433 "host = \"%s\", service = \"%s\", state = \"%s\"",
434 event->host, event->service, event->state);
436 } /* }}} Msg *riemann_notification_to_protobuf */
438 static Event *riemann_value_to_protobuf(struct riemann_host const *host, /* {{{ */
439 data_set_t const *ds,
440 value_list_t const *vl, size_t index,
441 gauge_t const *rates)
444 char name_buffer[5 * DATA_MAX_NAME_LEN];
445 char service_buffer[6 * DATA_MAX_NAME_LEN];
449 event = malloc (sizeof (*event));
452 ERROR ("write_riemann plugin: malloc failed.");
455 memset (event, 0, sizeof (*event));
458 event->host = strdup (vl->host);
459 event->time = CDTIME_T_TO_TIME_T (vl->time);
462 ttl = CDTIME_T_TO_DOUBLE (vl->interval) * host->ttl_factor;
463 event->ttl = (float) ttl;
466 riemann_event_add_attribute (event, "plugin", vl->plugin);
467 if (vl->plugin_instance[0] != 0)
468 riemann_event_add_attribute (event, "plugin_instance",
469 vl->plugin_instance);
471 riemann_event_add_attribute (event, "type", vl->type);
472 if (vl->type_instance[0] != 0)
473 riemann_event_add_attribute (event, "type_instance",
476 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
478 char ds_type[DATA_MAX_NAME_LEN];
480 ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
481 DS_TYPE_TO_STRING(ds->ds[index].type));
482 riemann_event_add_attribute (event, "ds_type", ds_type);
486 riemann_event_add_attribute (event, "ds_type",
487 DS_TYPE_TO_STRING(ds->ds[index].type));
489 riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
491 char ds_index[DATA_MAX_NAME_LEN];
493 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
494 riemann_event_add_attribute (event, "ds_index", ds_index);
497 for (i = 0; i < riemann_tags_num; i++)
498 riemann_event_add_tag (event, riemann_tags[i]);
500 if (ds->ds[index].type == DS_TYPE_GAUGE)
502 event->has_metric_d = 1;
503 event->metric_d = (double) vl->values[index].gauge;
505 else if (rates != NULL)
507 event->has_metric_d = 1;
508 event->metric_d = (double) rates[index];
512 event->has_metric_sint64 = 1;
513 if (ds->ds[index].type == DS_TYPE_DERIVE)
514 event->metric_sint64 = (int64_t) vl->values[index].derive;
515 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
516 event->metric_sint64 = (int64_t) vl->values[index].absolute;
518 event->metric_sint64 = (int64_t) vl->values[index].counter;
521 format_name (name_buffer, sizeof (name_buffer),
522 /* host = */ "", vl->plugin, vl->plugin_instance,
523 vl->type, vl->type_instance);
524 if (host->always_append_ds || (ds->ds_num > 1))
525 ssnprintf (service_buffer, sizeof (service_buffer),
526 "%s/%s", &name_buffer[1], ds->ds[index].name);
528 sstrncpy (service_buffer, &name_buffer[1],
529 sizeof (service_buffer));
531 event->service = strdup (service_buffer);
533 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
534 "host = \"%s\", service = \"%s\"",
535 event->host, event->service);
537 } /* }}} Event *riemann_value_to_protobuf */
539 static Msg *riemann_value_list_to_protobuf(struct riemann_host const *host, /* {{{ */
540 data_set_t const *ds,
541 value_list_t const *vl)
545 gauge_t *rates = NULL;
547 /* Initialize the Msg structure. */
548 msg = malloc (sizeof (*msg));
551 ERROR ("write_riemann plugin: malloc failed.");
554 memset (msg, 0, sizeof (*msg));
557 /* Set up events. First, the list of pointers. */
558 msg->n_events = (size_t) vl->values_len;
559 msg->events = calloc (msg->n_events, sizeof (*msg->events));
560 if (msg->events == NULL)
562 ERROR ("write_riemann plugin: calloc failed.");
563 riemann_msg_protobuf_free (msg);
567 if (host->store_rates)
569 rates = uc_get_rate (ds, vl);
572 ERROR ("write_riemann plugin: uc_get_rate failed.");
573 riemann_msg_protobuf_free (msg);
578 for (i = 0; i < msg->n_events; i++)
580 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
582 if (msg->events[i] == NULL)
584 riemann_msg_protobuf_free (msg);
592 } /* }}} Msg *riemann_value_list_to_protobuf */
594 static int riemann_notification(const notification_t *n, user_data_t *ud) /* {{{ */
597 struct riemann_host *host = ud->data;
600 msg = riemann_notification_to_protobuf (host, n);
604 status = riemann_send (host, msg);
606 ERROR ("write_riemann plugin: riemann_send failed with status %i",
609 riemann_msg_protobuf_free (msg);
611 } /* }}} int riemann_notification */
613 static int riemann_write(const data_set_t *ds, /* {{{ */
614 const value_list_t *vl,
618 struct riemann_host *host = ud->data;
621 msg = riemann_value_list_to_protobuf (host, ds, vl);
625 status = riemann_send (host, msg);
627 ERROR ("write_riemann plugin: riemann_send failed with status %i",
630 riemann_msg_protobuf_free (msg);
632 } /* }}} int riemann_write */
634 static void riemann_free(void *p) /* {{{ */
636 struct riemann_host *host = p;
641 pthread_mutex_lock (&host->lock);
643 host->reference_count--;
644 if (host->reference_count > 0)
646 pthread_mutex_unlock (&host->lock);
650 riemann_disconnect (host);
652 sfree(host->service);
653 pthread_mutex_destroy (&host->lock);
655 } /* }}} void riemann_free */
657 static int riemann_config_node(oconfig_item_t *ci) /* {{{ */
659 struct riemann_host *host = NULL;
662 oconfig_item_t *child;
663 char callback_name[DATA_MAX_NAME_LEN];
666 if ((host = calloc(1, sizeof (*host))) == NULL) {
667 ERROR ("write_riemann plugin: calloc failed.");
670 pthread_mutex_init (&host->lock, NULL);
671 host->reference_count = 1;
673 host->service = NULL;
674 host->store_rates = 1;
675 host->always_append_ds = 0;
677 host->ttl_factor = RIEMANN_TTL_FACTOR;
679 status = cf_util_get_string (ci, &host->name);
681 WARNING("write_riemann plugin: Required host name is missing.");
686 for (i = 0; i < ci->children_num; i++) {
688 * The code here could be simplified but makes room
689 * for easy adding of new options later on.
691 child = &ci->children[i];
694 if (strcasecmp ("Host", child->key) == 0) {
695 status = cf_util_get_string (child, &host->node);
698 } else if (strcasecmp ("Port", child->key) == 0) {
699 status = cf_util_get_service (child, &host->service);
701 ERROR ("write_riemann plugin: Invalid argument "
702 "configured for the \"Port\" "
706 } else if (strcasecmp ("Protocol", child->key) == 0) {
708 status = cf_util_get_string_buffer (child,
712 ERROR ("write_riemann plugin: cf_util_get_"
713 "string_buffer failed with "
714 "status %i.", status);
718 if (strcasecmp ("UDP", tmp) == 0)
720 else if (strcasecmp ("TCP", tmp) == 0)
723 WARNING ("write_riemann plugin: The value "
724 "\"%s\" is not valid for the "
725 "\"Protocol\" option. Use "
726 "either \"UDP\" or \"TCP\".",
728 } else if (strcasecmp ("StoreRates", child->key) == 0) {
729 status = cf_util_get_boolean (child, &host->store_rates);
732 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
733 status = cf_util_get_boolean (child,
734 &host->always_append_ds);
737 } else if (strcasecmp ("TTLFactor", child->key) == 0) {
739 status = cf_util_get_double (child, &tmp);
743 host->ttl_factor = tmp;
744 } else if (tmp >= 1.0) {
745 NOTICE ("write_riemann plugin: The configured "
746 "TTLFactor is very small "
747 "(%.1f). A value of 2.0 or "
748 "greater is recommended.",
750 host->ttl_factor = tmp;
751 } else if (tmp > 0.0) {
752 WARNING ("write_riemann plugin: The configured "
753 "TTLFactor is too small to be "
754 "useful (%.1f). I'll use it "
755 "since the user knows best, "
756 "but under protest.",
758 host->ttl_factor = tmp;
759 } else { /* zero, negative and NAN */
760 ERROR ("write_riemann plugin: The configured "
761 "TTLFactor is invalid (%.1f).",
765 WARNING("write_riemann plugin: ignoring unknown config "
766 "option: \"%s\"", child->key);
774 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
777 ud.free_func = riemann_free;
779 pthread_mutex_lock (&host->lock);
781 status = plugin_register_write (callback_name, riemann_write, &ud);
783 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
784 "failed with status %i.",
785 callback_name, status);
787 host->reference_count++;
789 status = plugin_register_notification (callback_name,
790 riemann_notification, &ud);
792 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
793 "failed with status %i.",
794 callback_name, status);
796 host->reference_count++;
798 if (host->reference_count <= 1)
800 /* Both callbacks failed => free memory.
801 * We need to unlock here, because riemann_free() will lock.
802 * This is not a race condition, because we're the only one
803 * holding a reference. */
804 pthread_mutex_unlock (&host->lock);
809 host->reference_count--;
810 pthread_mutex_unlock (&host->lock);
813 } /* }}} int riemann_config_node */
815 static int riemann_config(oconfig_item_t *ci) /* {{{ */
818 oconfig_item_t *child;
821 for (i = 0; i < ci->children_num; i++) {
822 child = &ci->children[i];
824 if (strcasecmp("Node", child->key) == 0) {
825 riemann_config_node (child);
826 } else if (strcasecmp(child->key, "tag") == 0) {
828 status = cf_util_get_string(child, &tmp);
832 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
833 DEBUG("write_riemann plugin: Got tag: %s", tmp);
836 WARNING ("write_riemann plugin: Ignoring unknown "
837 "configuration option \"%s\" at top level.",
842 } /* }}} int riemann_config */
844 void module_register(void)
846 plugin_register_complex_config ("write_riemann", riemann_config);
849 /* vim: set sw=8 sts=8 ts=8 noet : */