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(struct riemann_host *host, Msg const *msg)
188 pthread_mutex_lock (&host->lock);
190 status = riemann_connect (host);
193 pthread_mutex_unlock (&host->lock);
197 buffer_len = msg__get_packed_size(msg);
201 buffer = malloc (buffer_len);
202 if (buffer == NULL) {
203 pthread_mutex_unlock (&host->lock);
204 ERROR ("write_riemann plugin: malloc failed.");
207 memset (buffer, 0, buffer_len);
211 uint32_t length = htonl ((uint32_t) (buffer_len - 4));
212 memcpy (buffer, &length, 4);
213 msg__pack(msg, buffer + 4);
217 msg__pack(msg, buffer);
220 status = (int) swrite (host->s, buffer, buffer_len);
225 riemann_disconnect (host);
226 pthread_mutex_unlock (&host->lock);
228 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s",
229 (host->node != NULL) ? host->node : RIEMANN_HOST,
230 (host->service != NULL) ? host->service : RIEMANN_PORT,
231 sstrerror (errno, errbuf, sizeof (errbuf)));
236 pthread_mutex_unlock (&host->lock);
241 static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
243 return (strarray_add (&event->tags, &event->n_tags, tag));
244 } /* }}} int riemann_event_add_tag */
246 static int riemann_event_add_attribute (Event *event, /* {{{ */
247 char const *key, char const *value)
249 Attribute **new_attributes;
252 new_attributes = realloc (event->attributes,
253 sizeof (*event->attributes) * (event->n_attributes + 1));
254 if (new_attributes == NULL)
256 ERROR ("write_riemann plugin: realloc failed.");
259 event->attributes = new_attributes;
261 a = malloc (sizeof (*a));
264 ERROR ("write_riemann plugin: malloc failed.");
269 a->key = strdup (key);
271 a->value = strdup (value);
273 event->attributes[event->n_attributes] = a;
274 event->n_attributes++;
277 } /* }}} int riemann_event_add_attribute */
279 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
280 notification_t const *n)
284 char service_buffer[6 * DATA_MAX_NAME_LEN];
285 char const *severity;
286 notification_meta_t *meta;
289 msg = malloc (sizeof (*msg));
292 ERROR ("write_riemann plugin: malloc failed.");
295 memset (msg, 0, sizeof (*msg));
298 msg->events = malloc (sizeof (*msg->events));
299 if (msg->events == NULL)
301 ERROR ("write_riemann plugin: malloc failed.");
306 event = malloc (sizeof (*event));
309 ERROR ("write_riemann plugin: malloc failed.");
314 memset (event, 0, sizeof (*event));
317 msg->events[0] = event;
320 event->host = strdup (n->host);
321 event->time = CDTIME_T_TO_TIME_T (n->time);
326 case NOTIF_OKAY: severity = "ok"; break;
327 case NOTIF_WARNING: severity = "warning"; break;
328 case NOTIF_FAILURE: severity = "critical"; break;
329 default: severity = "unknown";
331 event->state = strdup (severity);
333 riemann_event_add_tag (event, "notification");
335 riemann_event_add_attribute (event, "host", n->host);
336 if (n->plugin[0] != 0)
337 riemann_event_add_attribute (event, "plugin", n->plugin);
338 if (n->plugin_instance[0] != 0)
339 riemann_event_add_attribute (event, "plugin_instance",
343 riemann_event_add_attribute (event, "type", n->type);
344 if (n->type_instance[0] != 0)
345 riemann_event_add_attribute (event, "type_instance",
348 for (i = 0; i < riemann_tags_num; i++)
349 riemann_event_add_tag (event, riemann_tags[i]);
351 format_name (service_buffer, sizeof (service_buffer),
352 /* host = */ "", n->plugin, n->plugin_instance,
353 n->type, n->type_instance);
354 event->service = strdup (&service_buffer[1]);
356 /* Pull in values from threshold */
357 for (meta = n->meta; meta != NULL; meta = meta->next)
359 if (strcasecmp ("CurrentValue", meta->name) != 0)
362 event->metric_d = meta->nm_value.nm_double;
363 event->has_metric_d = 1;
367 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
368 "host = \"%s\", service = \"%s\", state = \"%s\"",
369 event->host, event->service, event->state);
371 } /* }}} Msg *riemann_notification_to_protobuf */
373 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
374 data_set_t const *ds,
375 value_list_t const *vl, size_t index,
376 gauge_t const *rates)
379 char name_buffer[5 * DATA_MAX_NAME_LEN];
380 char service_buffer[6 * DATA_MAX_NAME_LEN];
384 event = malloc (sizeof (*event));
387 ERROR ("write_riemann plugin: malloc failed.");
390 memset (event, 0, sizeof (*event));
393 event->host = strdup (vl->host);
394 event->time = CDTIME_T_TO_TIME_T (vl->time);
397 ttl = CDTIME_T_TO_DOUBLE (vl->interval) * host->ttl_factor;
398 event->ttl = (float) ttl;
401 riemann_event_add_attribute (event, "plugin", vl->plugin);
402 if (vl->plugin_instance[0] != 0)
403 riemann_event_add_attribute (event, "plugin_instance",
404 vl->plugin_instance);
406 riemann_event_add_attribute (event, "type", vl->type);
407 if (vl->type_instance[0] != 0)
408 riemann_event_add_attribute (event, "type_instance",
411 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
413 char ds_type[DATA_MAX_NAME_LEN];
415 ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
416 DS_TYPE_TO_STRING(ds->ds[index].type));
417 riemann_event_add_attribute (event, "ds_type", ds_type);
421 riemann_event_add_attribute (event, "ds_type",
422 DS_TYPE_TO_STRING(ds->ds[index].type));
424 riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
426 char ds_index[DATA_MAX_NAME_LEN];
428 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
429 riemann_event_add_attribute (event, "ds_index", ds_index);
432 for (i = 0; i < riemann_tags_num; i++)
433 riemann_event_add_tag (event, riemann_tags[i]);
435 if (ds->ds[index].type == DS_TYPE_GAUGE)
437 event->has_metric_d = 1;
438 event->metric_d = (double) vl->values[index].gauge;
440 else if (rates != NULL)
442 event->has_metric_d = 1;
443 event->metric_d = (double) rates[index];
447 event->has_metric_sint64 = 1;
448 if (ds->ds[index].type == DS_TYPE_DERIVE)
449 event->metric_sint64 = (int64_t) vl->values[index].derive;
450 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
451 event->metric_sint64 = (int64_t) vl->values[index].absolute;
453 event->metric_sint64 = (int64_t) vl->values[index].counter;
456 format_name (name_buffer, sizeof (name_buffer),
457 /* host = */ "", vl->plugin, vl->plugin_instance,
458 vl->type, vl->type_instance);
459 if (host->always_append_ds || (ds->ds_num > 1))
460 ssnprintf (service_buffer, sizeof (service_buffer),
461 "%s/%s", &name_buffer[1], ds->ds[index].name);
463 sstrncpy (service_buffer, &name_buffer[1],
464 sizeof (service_buffer));
466 event->service = strdup (service_buffer);
468 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
469 "host = \"%s\", service = \"%s\"",
470 event->host, event->service);
472 } /* }}} Event *riemann_value_to_protobuf */
474 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
475 data_set_t const *ds,
476 value_list_t const *vl)
480 gauge_t *rates = NULL;
482 /* Initialize the Msg structure. */
483 msg = malloc (sizeof (*msg));
486 ERROR ("write_riemann plugin: malloc failed.");
489 memset (msg, 0, sizeof (*msg));
492 /* Set up events. First, the list of pointers. */
493 msg->n_events = (size_t) vl->values_len;
494 msg->events = calloc (msg->n_events, sizeof (*msg->events));
495 if (msg->events == NULL)
497 ERROR ("write_riemann plugin: calloc failed.");
498 riemann_msg_protobuf_free (msg);
502 if (host->store_rates)
504 rates = uc_get_rate (ds, vl);
507 ERROR ("write_riemann plugin: uc_get_rate failed.");
508 riemann_msg_protobuf_free (msg);
513 for (i = 0; i < msg->n_events; i++)
515 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
517 if (msg->events[i] == NULL)
519 riemann_msg_protobuf_free (msg);
527 } /* }}} Msg *riemann_value_list_to_protobuf */
530 riemann_notification(const notification_t *n, user_data_t *ud)
533 struct riemann_host *host = ud->data;
536 msg = riemann_notification_to_protobuf (host, n);
540 status = riemann_send (host, msg);
542 ERROR ("write_riemann plugin: riemann_send failed with status %i",
545 riemann_msg_protobuf_free (msg);
547 } /* }}} int riemann_notification */
550 riemann_write(const data_set_t *ds,
551 const value_list_t *vl,
555 struct riemann_host *host = ud->data;
558 msg = riemann_value_list_to_protobuf (host, ds, vl);
562 status = riemann_send (host, msg);
564 ERROR ("write_riemann plugin: riemann_send failed with status %i",
567 riemann_msg_protobuf_free (msg);
572 riemann_free(void *p)
574 struct riemann_host *host = p;
579 pthread_mutex_lock (&host->lock);
581 host->reference_count--;
582 if (host->reference_count > 0)
584 pthread_mutex_unlock (&host->lock);
588 riemann_disconnect (host);
590 sfree(host->service);
591 pthread_mutex_destroy (&host->lock);
596 riemann_config_node(oconfig_item_t *ci)
598 struct riemann_host *host = NULL;
601 oconfig_item_t *child;
602 char callback_name[DATA_MAX_NAME_LEN];
605 if ((host = calloc(1, sizeof (*host))) == NULL) {
606 ERROR ("write_riemann plugin: calloc failed.");
609 pthread_mutex_init (&host->lock, NULL);
610 host->reference_count = 1;
612 host->service = NULL;
613 host->store_rates = 1;
614 host->always_append_ds = 0;
616 host->ttl_factor = RIEMANN_TTL_FACTOR;
618 status = cf_util_get_string (ci, &host->name);
620 WARNING("write_riemann plugin: Required host name is missing.");
625 for (i = 0; i < ci->children_num; i++) {
627 * The code here could be simplified but makes room
628 * for easy adding of new options later on.
630 child = &ci->children[i];
633 if (strcasecmp ("Host", child->key) == 0) {
634 status = cf_util_get_string (child, &host->node);
637 } else if (strcasecmp ("Port", child->key) == 0) {
638 status = cf_util_get_service (child, &host->service);
640 ERROR ("write_riemann plugin: Invalid argument "
641 "configured for the \"Port\" "
645 } else if (strcasecmp ("Protocol", child->key) == 0) {
647 status = cf_util_get_string_buffer (child,
651 ERROR ("write_riemann plugin: cf_util_get_"
652 "string_buffer failed with "
653 "status %i.", status);
657 if (strcasecmp ("UDP", tmp) == 0)
659 else if (strcasecmp ("TCP", tmp) == 0)
662 WARNING ("write_riemann plugin: The value "
663 "\"%s\" is not valid for the "
664 "\"Protocol\" option. Use "
665 "either \"UDP\" or \"TCP\".",
667 } else if (strcasecmp ("StoreRates", child->key) == 0) {
668 status = cf_util_get_boolean (child, &host->store_rates);
671 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
672 status = cf_util_get_boolean (child,
673 &host->always_append_ds);
676 } else if (strcasecmp ("TTLFactor", child->key) == 0) {
678 status = cf_util_get_double (child, &tmp);
682 host->ttl_factor = tmp;
683 } else if (tmp >= 1.0) {
684 NOTICE ("write_riemann plugin: The configured "
685 "TTLFactor is very small "
686 "(%.1f). A value of 2.0 or "
687 "greater is recommended.",
689 host->ttl_factor = tmp;
690 } else if (tmp > 0.0) {
691 WARNING ("write_riemann plugin: The configured "
692 "TTLFactor is too small to be "
693 "useful (%.1f). I'll use it "
694 "since the user knows best, "
695 "but under protest.",
697 host->ttl_factor = tmp;
698 } else { /* zero, negative and NAN */
699 ERROR ("write_riemann plugin: The configured "
700 "TTLFactor is invalid (%.1f).",
704 WARNING("write_riemann plugin: ignoring unknown config "
705 "option: \"%s\"", child->key);
713 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
716 ud.free_func = riemann_free;
718 pthread_mutex_lock (&host->lock);
720 status = plugin_register_write (callback_name, riemann_write, &ud);
722 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
723 "failed with status %i.",
724 callback_name, status);
726 host->reference_count++;
728 status = plugin_register_notification (callback_name,
729 riemann_notification, &ud);
731 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
732 "failed with status %i.",
733 callback_name, status);
735 host->reference_count++;
737 if (host->reference_count <= 1)
739 /* Both callbacks failed => free memory.
740 * We need to unlock here, because riemann_free() will lock.
741 * This is not a race condition, because we're the only one
742 * holding a reference. */
743 pthread_mutex_unlock (&host->lock);
748 host->reference_count--;
749 pthread_mutex_unlock (&host->lock);
755 riemann_config(oconfig_item_t *ci)
758 oconfig_item_t *child;
761 for (i = 0; i < ci->children_num; i++) {
762 child = &ci->children[i];
764 if (strcasecmp("Node", child->key) == 0) {
765 riemann_config_node (child);
766 } else if (strcasecmp(child->key, "tag") == 0) {
768 status = cf_util_get_string(child, &tmp);
772 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
773 DEBUG("write_riemann plugin: Got tag: %s", tmp);
776 WARNING ("write_riemann plugin: Ignoring unknown "
777 "configuration option \"%s\" at top level.",
785 module_register(void)
787 plugin_register_complex_config ("write_riemann", riemann_config);
790 /* vim: set sw=8 sts=8 ts=8 noet : */