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 and add extra attributes */
357 for (meta = n->meta; meta != NULL; meta = meta->next)
359 if (strcasecmp ("CurrentValue", meta->name) == 0 && meta->type == NM_TYPE_DOUBLE)
361 event->metric_d = meta->nm_value.nm_double;
362 event->has_metric_d = 1;
366 if (meta->type == NM_TYPE_STRING) {
367 riemann_event_add_attribute (event, meta->name, meta->nm_value.nm_string);
372 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
373 "host = \"%s\", service = \"%s\", state = \"%s\"",
374 event->host, event->service, event->state);
376 } /* }}} Msg *riemann_notification_to_protobuf */
378 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
379 data_set_t const *ds,
380 value_list_t const *vl, size_t index,
381 gauge_t const *rates)
384 char name_buffer[5 * DATA_MAX_NAME_LEN];
385 char service_buffer[6 * DATA_MAX_NAME_LEN];
389 event = malloc (sizeof (*event));
392 ERROR ("write_riemann plugin: malloc failed.");
395 memset (event, 0, sizeof (*event));
398 event->host = strdup (vl->host);
399 event->time = CDTIME_T_TO_TIME_T (vl->time);
402 ttl = CDTIME_T_TO_DOUBLE (vl->interval) * host->ttl_factor;
403 event->ttl = (float) ttl;
406 riemann_event_add_attribute (event, "plugin", vl->plugin);
407 if (vl->plugin_instance[0] != 0)
408 riemann_event_add_attribute (event, "plugin_instance",
409 vl->plugin_instance);
411 riemann_event_add_attribute (event, "type", vl->type);
412 if (vl->type_instance[0] != 0)
413 riemann_event_add_attribute (event, "type_instance",
416 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
418 char ds_type[DATA_MAX_NAME_LEN];
420 ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
421 DS_TYPE_TO_STRING(ds->ds[index].type));
422 riemann_event_add_attribute (event, "ds_type", ds_type);
426 riemann_event_add_attribute (event, "ds_type",
427 DS_TYPE_TO_STRING(ds->ds[index].type));
429 riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
431 char ds_index[DATA_MAX_NAME_LEN];
433 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
434 riemann_event_add_attribute (event, "ds_index", ds_index);
437 for (i = 0; i < riemann_tags_num; i++)
438 riemann_event_add_tag (event, riemann_tags[i]);
440 if (ds->ds[index].type == DS_TYPE_GAUGE)
442 event->has_metric_d = 1;
443 event->metric_d = (double) vl->values[index].gauge;
445 else if (rates != NULL)
447 event->has_metric_d = 1;
448 event->metric_d = (double) rates[index];
452 event->has_metric_sint64 = 1;
453 if (ds->ds[index].type == DS_TYPE_DERIVE)
454 event->metric_sint64 = (int64_t) vl->values[index].derive;
455 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
456 event->metric_sint64 = (int64_t) vl->values[index].absolute;
458 event->metric_sint64 = (int64_t) vl->values[index].counter;
461 format_name (name_buffer, sizeof (name_buffer),
462 /* host = */ "", vl->plugin, vl->plugin_instance,
463 vl->type, vl->type_instance);
464 if (host->always_append_ds || (ds->ds_num > 1))
465 ssnprintf (service_buffer, sizeof (service_buffer),
466 "%s/%s", &name_buffer[1], ds->ds[index].name);
468 sstrncpy (service_buffer, &name_buffer[1],
469 sizeof (service_buffer));
471 event->service = strdup (service_buffer);
473 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
474 "host = \"%s\", service = \"%s\"",
475 event->host, event->service);
477 } /* }}} Event *riemann_value_to_protobuf */
479 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
480 data_set_t const *ds,
481 value_list_t const *vl)
485 gauge_t *rates = NULL;
487 /* Initialize the Msg structure. */
488 msg = malloc (sizeof (*msg));
491 ERROR ("write_riemann plugin: malloc failed.");
494 memset (msg, 0, sizeof (*msg));
497 /* Set up events. First, the list of pointers. */
498 msg->n_events = (size_t) vl->values_len;
499 msg->events = calloc (msg->n_events, sizeof (*msg->events));
500 if (msg->events == NULL)
502 ERROR ("write_riemann plugin: calloc failed.");
503 riemann_msg_protobuf_free (msg);
507 if (host->store_rates)
509 rates = uc_get_rate (ds, vl);
512 ERROR ("write_riemann plugin: uc_get_rate failed.");
513 riemann_msg_protobuf_free (msg);
518 for (i = 0; i < msg->n_events; i++)
520 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
522 if (msg->events[i] == NULL)
524 riemann_msg_protobuf_free (msg);
532 } /* }}} Msg *riemann_value_list_to_protobuf */
535 riemann_notification(const notification_t *n, user_data_t *ud)
538 struct riemann_host *host = ud->data;
541 msg = riemann_notification_to_protobuf (host, n);
545 status = riemann_send (host, msg);
547 ERROR ("write_riemann plugin: riemann_send failed with status %i",
550 riemann_msg_protobuf_free (msg);
552 } /* }}} int riemann_notification */
555 riemann_write(const data_set_t *ds,
556 const value_list_t *vl,
560 struct riemann_host *host = ud->data;
563 msg = riemann_value_list_to_protobuf (host, ds, vl);
567 status = riemann_send (host, msg);
569 ERROR ("write_riemann plugin: riemann_send failed with status %i",
572 riemann_msg_protobuf_free (msg);
577 riemann_free(void *p)
579 struct riemann_host *host = p;
584 pthread_mutex_lock (&host->lock);
586 host->reference_count--;
587 if (host->reference_count > 0)
589 pthread_mutex_unlock (&host->lock);
593 riemann_disconnect (host);
595 sfree(host->service);
596 pthread_mutex_destroy (&host->lock);
601 riemann_config_node(oconfig_item_t *ci)
603 struct riemann_host *host = NULL;
606 oconfig_item_t *child;
607 char callback_name[DATA_MAX_NAME_LEN];
610 if ((host = calloc(1, sizeof (*host))) == NULL) {
611 ERROR ("write_riemann plugin: calloc failed.");
614 pthread_mutex_init (&host->lock, NULL);
615 host->reference_count = 1;
617 host->service = NULL;
618 host->store_rates = 1;
619 host->always_append_ds = 0;
621 host->ttl_factor = RIEMANN_TTL_FACTOR;
623 status = cf_util_get_string (ci, &host->name);
625 WARNING("write_riemann plugin: Required host name is missing.");
630 for (i = 0; i < ci->children_num; i++) {
632 * The code here could be simplified but makes room
633 * for easy adding of new options later on.
635 child = &ci->children[i];
638 if (strcasecmp ("Host", child->key) == 0) {
639 status = cf_util_get_string (child, &host->node);
642 } else if (strcasecmp ("Port", child->key) == 0) {
643 status = cf_util_get_service (child, &host->service);
645 ERROR ("write_riemann plugin: Invalid argument "
646 "configured for the \"Port\" "
650 } else if (strcasecmp ("Protocol", child->key) == 0) {
652 status = cf_util_get_string_buffer (child,
656 ERROR ("write_riemann plugin: cf_util_get_"
657 "string_buffer failed with "
658 "status %i.", status);
662 if (strcasecmp ("UDP", tmp) == 0)
664 else if (strcasecmp ("TCP", tmp) == 0)
667 WARNING ("write_riemann plugin: The value "
668 "\"%s\" is not valid for the "
669 "\"Protocol\" option. Use "
670 "either \"UDP\" or \"TCP\".",
672 } else if (strcasecmp ("StoreRates", child->key) == 0) {
673 status = cf_util_get_boolean (child, &host->store_rates);
676 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
677 status = cf_util_get_boolean (child,
678 &host->always_append_ds);
681 } else if (strcasecmp ("TTLFactor", child->key) == 0) {
683 status = cf_util_get_double (child, &tmp);
687 host->ttl_factor = tmp;
688 } else if (tmp >= 1.0) {
689 NOTICE ("write_riemann plugin: The configured "
690 "TTLFactor is very small "
691 "(%.1f). A value of 2.0 or "
692 "greater is recommended.",
694 host->ttl_factor = tmp;
695 } else if (tmp > 0.0) {
696 WARNING ("write_riemann plugin: The configured "
697 "TTLFactor is too small to be "
698 "useful (%.1f). I'll use it "
699 "since the user knows best, "
700 "but under protest.",
702 host->ttl_factor = tmp;
703 } else { /* zero, negative and NAN */
704 ERROR ("write_riemann plugin: The configured "
705 "TTLFactor is invalid (%.1f).",
709 WARNING("write_riemann plugin: ignoring unknown config "
710 "option: \"%s\"", child->key);
718 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
721 ud.free_func = riemann_free;
723 pthread_mutex_lock (&host->lock);
725 status = plugin_register_write (callback_name, riemann_write, &ud);
727 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
728 "failed with status %i.",
729 callback_name, status);
731 host->reference_count++;
733 status = plugin_register_notification (callback_name,
734 riemann_notification, &ud);
736 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
737 "failed with status %i.",
738 callback_name, status);
740 host->reference_count++;
742 if (host->reference_count <= 1)
744 /* Both callbacks failed => free memory.
745 * We need to unlock here, because riemann_free() will lock.
746 * This is not a race condition, because we're the only one
747 * holding a reference. */
748 pthread_mutex_unlock (&host->lock);
753 host->reference_count--;
754 pthread_mutex_unlock (&host->lock);
760 riemann_config(oconfig_item_t *ci)
763 oconfig_item_t *child;
766 for (i = 0; i < ci->children_num; i++) {
767 child = &ci->children[i];
769 if (strcasecmp("Node", child->key) == 0) {
770 riemann_config_node (child);
771 } else if (strcasecmp(child->key, "tag") == 0) {
773 status = cf_util_get_string(child, &tmp);
777 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
778 DEBUG("write_riemann plugin: Got tag: %s", tmp);
781 WARNING ("write_riemann plugin: Ignoring unknown "
782 "configuration option \"%s\" at top level.",
790 module_register(void)
792 plugin_register_complex_config ("write_riemann", riemann_config);
795 /* vim: set sw=8 sts=8 ts=8 noet : */