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 *);
43 int write_riemann_threshold_config(oconfig_item_t *);
47 #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 msg = riemann_notification_to_protobuf (host, n);
641 status = riemann_send (host, msg);
643 ERROR ("write_riemann plugin: riemann_send failed with status %i",
646 riemann_msg_protobuf_free (msg);
648 } /* }}} int riemann_notification */
650 static int riemann_write(const data_set_t *ds, /* {{{ */
651 const value_list_t *vl,
655 int statuses[vl->values_len];
656 struct riemann_host *host = ud->data;
659 write_riemann_threshold_check(ds, vl, statuses);
660 msg = riemann_value_list_to_protobuf (host, ds, vl, statuses);
664 status = riemann_send (host, msg);
666 ERROR ("write_riemann plugin: riemann_send failed with status %i",
669 riemann_msg_protobuf_free (msg);
671 } /* }}} int riemann_write */
673 static void riemann_free(void *p) /* {{{ */
675 struct riemann_host *host = p;
680 pthread_mutex_lock (&host->lock);
682 host->reference_count--;
683 if (host->reference_count > 0)
685 pthread_mutex_unlock (&host->lock);
689 riemann_disconnect (host);
691 sfree(host->service);
692 pthread_mutex_destroy (&host->lock);
694 } /* }}} void riemann_free */
696 static int riemann_config_node(oconfig_item_t *ci) /* {{{ */
698 struct riemann_host *host = NULL;
701 oconfig_item_t *child;
702 char callback_name[DATA_MAX_NAME_LEN];
705 if ((host = calloc(1, sizeof (*host))) == NULL) {
706 ERROR ("write_riemann plugin: calloc failed.");
709 pthread_mutex_init (&host->lock, NULL);
710 host->reference_count = 1;
712 host->service = NULL;
713 host->store_rates = 1;
714 host->always_append_ds = 0;
716 host->ttl_factor = RIEMANN_TTL_FACTOR;
718 status = cf_util_get_string (ci, &host->name);
720 WARNING("write_riemann plugin: Required host name is missing.");
725 for (i = 0; i < ci->children_num; i++) {
727 * The code here could be simplified but makes room
728 * for easy adding of new options later on.
730 child = &ci->children[i];
733 if (strcasecmp ("Host", child->key) == 0) {
734 status = cf_util_get_string (child, &host->node);
737 } else if (strcasecmp ("Threshold", child->key) == 0) {
738 status = write_riemann_threshold_config(child);
741 } else if (strcasecmp ("Port", child->key) == 0) {
742 status = cf_util_get_service (child, &host->service);
744 ERROR ("write_riemann plugin: Invalid argument "
745 "configured for the \"Port\" "
749 } else if (strcasecmp ("Protocol", child->key) == 0) {
751 status = cf_util_get_string_buffer (child,
755 ERROR ("write_riemann plugin: cf_util_get_"
756 "string_buffer failed with "
757 "status %i.", status);
761 if (strcasecmp ("UDP", tmp) == 0)
763 else if (strcasecmp ("TCP", tmp) == 0)
766 WARNING ("write_riemann plugin: The value "
767 "\"%s\" is not valid for the "
768 "\"Protocol\" option. Use "
769 "either \"UDP\" or \"TCP\".",
771 } else if (strcasecmp ("StoreRates", child->key) == 0) {
772 status = cf_util_get_boolean (child, &host->store_rates);
775 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
776 status = cf_util_get_boolean (child,
777 &host->always_append_ds);
780 } else if (strcasecmp ("TTLFactor", child->key) == 0) {
782 status = cf_util_get_double (child, &tmp);
786 host->ttl_factor = tmp;
787 } else if (tmp >= 1.0) {
788 NOTICE ("write_riemann plugin: The configured "
789 "TTLFactor is very small "
790 "(%.1f). A value of 2.0 or "
791 "greater is recommended.",
793 host->ttl_factor = tmp;
794 } else if (tmp > 0.0) {
795 WARNING ("write_riemann plugin: The configured "
796 "TTLFactor is too small to be "
797 "useful (%.1f). I'll use it "
798 "since the user knows best, "
799 "but under protest.",
801 host->ttl_factor = tmp;
802 } else { /* zero, negative and NAN */
803 ERROR ("write_riemann plugin: The configured "
804 "TTLFactor is invalid (%.1f).",
808 WARNING("write_riemann plugin: ignoring unknown config "
809 "option: \"%s\"", child->key);
817 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
820 ud.free_func = riemann_free;
822 pthread_mutex_lock (&host->lock);
824 status = plugin_register_write (callback_name, riemann_write, &ud);
826 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
827 "failed with status %i.",
828 callback_name, status);
830 host->reference_count++;
832 status = plugin_register_notification (callback_name,
833 riemann_notification, &ud);
835 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
836 "failed with status %i.",
837 callback_name, status);
839 host->reference_count++;
841 if (host->reference_count <= 1)
843 /* Both callbacks failed => free memory.
844 * We need to unlock here, because riemann_free() will lock.
845 * This is not a race condition, because we're the only one
846 * holding a reference. */
847 pthread_mutex_unlock (&host->lock);
852 host->reference_count--;
853 pthread_mutex_unlock (&host->lock);
856 } /* }}} int riemann_config_node */
858 static int riemann_config(oconfig_item_t *ci) /* {{{ */
861 oconfig_item_t *child;
864 for (i = 0; i < ci->children_num; i++) {
865 child = &ci->children[i];
867 if (strcasecmp("Node", child->key) == 0) {
868 riemann_config_node (child);
869 } else if (strcasecmp(child->key, "attribute") == 0) {
873 if (child->values_num != 2) {
874 WARNING("riemann attributes need both a key and a value.");
877 if (child->values[0].type != OCONFIG_TYPE_STRING ||
878 child->values[1].type != OCONFIG_TYPE_STRING) {
879 WARNING("riemann attribute needs string arguments.");
882 if ((key = strdup(child->values[0].value.string)) == NULL) {
883 WARNING("cannot allocate memory for attribute key.");
886 if ((val = strdup(child->values[1].value.string)) == NULL) {
887 WARNING("cannot allocate memory for attribute value.");
890 strarray_add(&riemann_attrs, &riemann_attrs_num, key);
891 strarray_add(&riemann_attrs, &riemann_attrs_num, val);
892 DEBUG("write_riemann: got attr: %s => %s", key, val);
895 } else if (strcasecmp(child->key, "tag") == 0) {
897 status = cf_util_get_string(child, &tmp);
901 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
902 DEBUG("write_riemann plugin: Got tag: %s", tmp);
905 WARNING ("write_riemann plugin: Ignoring unknown "
906 "configuration option \"%s\" at top level.",
911 } /* }}} int riemann_config */
913 void module_register(void)
915 plugin_register_complex_config ("write_riemann", riemann_config);
918 /* vim: set sw=8 sts=8 ts=8 noet : */