2 * collectd - src/write_riemann.c
3 * Copyright (C) 2012,2013 Pierre-Yves Ritschard
4 * Copyright (C) 2013 Florian octo Forster
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Pierre-Yves Ritschard <pyr at spootnik.org>
26 * Florian octo Forster <octo at collectd.org>
32 #include "configfile.h"
33 #include "utils_cache.h"
34 #include "riemann.pb-c.h"
36 #include <sys/socket.h>
37 #include <arpa/inet.h>
43 #define RIEMANN_HOST "localhost"
44 #define RIEMANN_PORT "5555"
45 #define RIEMANN_TTL_FACTOR 2.0
47 int write_riemann_threshold_check(const data_set_t *, const value_list_t *, int *);
51 #define F_CONNECT 0x01
55 _Bool check_thresholds;
57 _Bool always_append_ds;
67 static char **riemann_tags;
68 static size_t riemann_tags_num;
69 static char **riemann_attrs;
70 static size_t riemann_attrs_num;
72 static void riemann_event_protobuf_free (Event *event) /* {{{ */
80 sfree (event->service);
82 sfree (event->description);
84 strarray_free (event->tags, event->n_tags);
88 for (i = 0; i < event->n_attributes; i++)
90 sfree (event->attributes[i]->key);
91 sfree (event->attributes[i]->value);
92 sfree (event->attributes[i]);
94 sfree (event->attributes);
95 event->n_attributes = 0;
98 } /* }}} void riemann_event_protobuf_free */
100 static void riemann_msg_protobuf_free (Msg *msg) /* {{{ */
107 for (i = 0; i < msg->n_events; i++)
109 riemann_event_protobuf_free (msg->events[i]);
110 msg->events[i] = NULL;
117 } /* }}} void riemann_msg_protobuf_free */
119 /* host->lock must be held when calling this function. */
120 static int riemann_connect(struct riemann_host *host) /* {{{ */
123 struct addrinfo *ai, *res, hints;
127 if (host->flags & F_CONNECT)
130 memset(&hints, 0, sizeof(hints));
131 memset(&service, 0, sizeof(service));
132 hints.ai_family = AF_UNSPEC;
133 hints.ai_socktype = host->use_tcp ? SOCK_STREAM : SOCK_DGRAM;
135 hints.ai_flags |= AI_ADDRCONFIG;
138 node = (host->node != NULL) ? host->node : RIEMANN_HOST;
139 service = (host->service != NULL) ? host->service : RIEMANN_PORT;
141 if ((e = getaddrinfo(node, service, &hints, &res)) != 0) {
142 ERROR ("write_riemann plugin: Unable to resolve host \"%s\": %s",
143 node, gai_strerror(e));
148 for (ai = res; ai != NULL; ai = ai->ai_next) {
149 if ((host->s = socket(ai->ai_family,
151 ai->ai_protocol)) == -1) {
155 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
161 host->flags |= F_CONNECT;
162 DEBUG("write_riemann plugin: got a successful connection for: %s:%s",
170 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
175 } /* }}} int riemann_connect */
177 /* host->lock must be held when calling this function. */
178 static int riemann_disconnect (struct riemann_host *host) /* {{{ */
180 if ((host->flags & F_CONNECT) == 0)
185 host->flags &= ~F_CONNECT;
188 } /* }}} int riemann_disconnect */
190 static int riemann_send_msg (struct riemann_host *host, const Msg *msg) /* {{{ */
193 u_char *buffer = NULL;
196 status = riemann_connect (host);
200 buffer_len = msg__get_packed_size(msg);
205 buffer = malloc (buffer_len);
206 if (buffer == NULL) {
207 ERROR ("write_riemann plugin: malloc failed.");
210 memset (buffer, 0, buffer_len);
214 uint32_t length = htonl ((uint32_t) (buffer_len - 4));
215 memcpy (buffer, &length, 4);
216 msg__pack(msg, buffer + 4);
220 msg__pack(msg, buffer);
223 status = (int) swrite (host->s, buffer, buffer_len);
227 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s",
228 (host->node != NULL) ? host->node : RIEMANN_HOST,
229 (host->service != NULL) ? host->service : RIEMANN_PORT,
230 sstrerror (errno, errbuf, sizeof (errbuf)));
237 } /* }}} int riemann_send_msg */
239 static int riemann_recv_ack(struct riemann_host *host) /* {{{ */
245 status = (int) sread (host->s, &header, 4);
250 size_t size = ntohl(header);
252 // Buffer on the stack since acknowledges are typically small.
254 memset (buffer, 0, size);
256 status = (int) sread (host->s, buffer, size);
261 msg = msg__unpack (NULL, size, buffer);
268 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s acknowledgement message reported error: %s",
269 (host->node != NULL) ? host->node : RIEMANN_HOST,
270 (host->service != NULL) ? host->service : RIEMANN_PORT,
273 msg__free_unpacked(msg, NULL);
277 msg__free_unpacked (msg, NULL);
279 } /* }}} int riemann_recv_ack */
282 * Function to send messages (Msg) to riemann.
284 * Acquires the host lock, disconnects on errors.
286 static int riemann_send(struct riemann_host *host, Msg const *msg) /* {{{ */
289 pthread_mutex_lock (&host->lock);
291 status = riemann_send_msg(host, msg);
293 riemann_disconnect (host);
294 pthread_mutex_unlock (&host->lock);
299 * For TCP we need to receive message acknowledgemenent.
303 status = riemann_recv_ack(host);
307 riemann_disconnect (host);
308 pthread_mutex_unlock (&host->lock);
313 pthread_mutex_unlock (&host->lock);
315 } /* }}} int riemann_send */
317 static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
319 return (strarray_add (&event->tags, &event->n_tags, tag));
320 } /* }}} int riemann_event_add_tag */
322 static int riemann_event_add_attribute (Event *event, /* {{{ */
323 char const *key, char const *value)
325 Attribute **new_attributes;
328 new_attributes = realloc (event->attributes,
329 sizeof (*event->attributes) * (event->n_attributes + 1));
330 if (new_attributes == NULL)
332 ERROR ("write_riemann plugin: realloc failed.");
335 event->attributes = new_attributes;
337 a = malloc (sizeof (*a));
340 ERROR ("write_riemann plugin: malloc failed.");
345 a->key = strdup (key);
347 a->value = strdup (value);
349 event->attributes[event->n_attributes] = a;
350 event->n_attributes++;
353 } /* }}} int riemann_event_add_attribute */
355 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
356 notification_t const *n)
360 char service_buffer[6 * DATA_MAX_NAME_LEN];
361 char const *severity;
362 notification_meta_t *meta;
365 msg = malloc (sizeof (*msg));
368 ERROR ("write_riemann plugin: malloc failed.");
371 memset (msg, 0, sizeof (*msg));
374 msg->events = malloc (sizeof (*msg->events));
375 if (msg->events == NULL)
377 ERROR ("write_riemann plugin: malloc failed.");
382 event = malloc (sizeof (*event));
385 ERROR ("write_riemann plugin: malloc failed.");
390 memset (event, 0, sizeof (*event));
393 msg->events[0] = event;
396 event->host = strdup (n->host);
397 event->time = CDTIME_T_TO_TIME_T (n->time);
402 case NOTIF_OKAY: severity = "ok"; break;
403 case NOTIF_WARNING: severity = "warning"; break;
404 case NOTIF_FAILURE: severity = "critical"; break;
405 default: severity = "unknown";
407 event->state = strdup (severity);
409 riemann_event_add_tag (event, "notification");
411 riemann_event_add_attribute (event, "host", n->host);
412 if (n->plugin[0] != 0)
413 riemann_event_add_attribute (event, "plugin", n->plugin);
414 if (n->plugin_instance[0] != 0)
415 riemann_event_add_attribute (event, "plugin_instance",
419 riemann_event_add_attribute (event, "type", n->type);
420 if (n->type_instance[0] != 0)
421 riemann_event_add_attribute (event, "type_instance",
424 for (i = 0; i < riemann_attrs_num; i += 2)
425 riemann_event_add_attribute(event,
427 riemann_attrs[i +1]);
429 for (i = 0; i < riemann_tags_num; i++)
430 riemann_event_add_tag (event, riemann_tags[i]);
432 format_name (service_buffer, sizeof (service_buffer),
433 /* host = */ "", n->plugin, n->plugin_instance,
434 n->type, n->type_instance);
435 event->service = strdup (&service_buffer[1]);
437 if (n->message[0] != 0)
438 riemann_event_add_attribute (event, "description", n->message);
440 /* Pull in values from threshold and add extra attributes */
441 for (meta = n->meta; meta != NULL; meta = meta->next)
443 if (strcasecmp ("CurrentValue", meta->name) == 0 && meta->type == NM_TYPE_DOUBLE)
445 event->metric_d = meta->nm_value.nm_double;
446 event->has_metric_d = 1;
450 if (meta->type == NM_TYPE_STRING) {
451 riemann_event_add_attribute (event, meta->name, meta->nm_value.nm_string);
456 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
457 "host = \"%s\", service = \"%s\", state = \"%s\"",
458 event->host, event->service, event->state);
460 } /* }}} Msg *riemann_notification_to_protobuf */
462 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
463 data_set_t const *ds,
464 value_list_t const *vl, size_t index,
465 gauge_t const *rates,
469 char name_buffer[5 * DATA_MAX_NAME_LEN];
470 char service_buffer[6 * DATA_MAX_NAME_LEN];
474 event = malloc (sizeof (*event));
477 ERROR ("write_riemann plugin: malloc failed.");
480 memset (event, 0, sizeof (*event));
483 event->host = strdup (vl->host);
484 event->time = CDTIME_T_TO_TIME_T (vl->time);
487 if (host->check_thresholds) {
490 event->state = strdup("ok");
493 event->state = strdup("critical");
496 event->state = strdup("warning");
499 event->state = strdup("unknown");
504 ttl = CDTIME_T_TO_DOUBLE (vl->interval) * host->ttl_factor;
505 event->ttl = (float) ttl;
508 riemann_event_add_attribute (event, "plugin", vl->plugin);
509 if (vl->plugin_instance[0] != 0)
510 riemann_event_add_attribute (event, "plugin_instance",
511 vl->plugin_instance);
513 riemann_event_add_attribute (event, "type", vl->type);
514 if (vl->type_instance[0] != 0)
515 riemann_event_add_attribute (event, "type_instance",
518 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
520 char ds_type[DATA_MAX_NAME_LEN];
522 ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
523 DS_TYPE_TO_STRING(ds->ds[index].type));
524 riemann_event_add_attribute (event, "ds_type", ds_type);
528 riemann_event_add_attribute (event, "ds_type",
529 DS_TYPE_TO_STRING(ds->ds[index].type));
531 riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
533 char ds_index[DATA_MAX_NAME_LEN];
535 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
536 riemann_event_add_attribute (event, "ds_index", ds_index);
539 for (i = 0; i < riemann_attrs_num; i += 2)
540 riemann_event_add_attribute(event,
542 riemann_attrs[i +1]);
544 for (i = 0; i < riemann_tags_num; i++)
545 riemann_event_add_tag (event, riemann_tags[i]);
547 if (ds->ds[index].type == DS_TYPE_GAUGE)
549 event->has_metric_d = 1;
550 event->metric_d = (double) vl->values[index].gauge;
552 else if (rates != NULL)
554 event->has_metric_d = 1;
555 event->metric_d = (double) rates[index];
559 event->has_metric_sint64 = 1;
560 if (ds->ds[index].type == DS_TYPE_DERIVE)
561 event->metric_sint64 = (int64_t) vl->values[index].derive;
562 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
563 event->metric_sint64 = (int64_t) vl->values[index].absolute;
565 event->metric_sint64 = (int64_t) vl->values[index].counter;
568 format_name (name_buffer, sizeof (name_buffer),
569 /* host = */ "", vl->plugin, vl->plugin_instance,
570 vl->type, vl->type_instance);
571 if (host->always_append_ds || (ds->ds_num > 1))
572 ssnprintf (service_buffer, sizeof (service_buffer),
573 "%s/%s", &name_buffer[1], ds->ds[index].name);
575 sstrncpy (service_buffer, &name_buffer[1],
576 sizeof (service_buffer));
578 event->service = strdup (service_buffer);
580 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
581 "host = \"%s\", service = \"%s\"",
582 event->host, event->service);
584 } /* }}} Event *riemann_value_to_protobuf */
586 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
587 data_set_t const *ds,
588 value_list_t const *vl,
593 gauge_t *rates = NULL;
595 /* Initialize the Msg structure. */
596 msg = malloc (sizeof (*msg));
599 ERROR ("write_riemann plugin: malloc failed.");
602 memset (msg, 0, sizeof (*msg));
605 /* Set up events. First, the list of pointers. */
606 msg->n_events = (size_t) vl->values_len;
607 msg->events = calloc (msg->n_events, sizeof (*msg->events));
608 if (msg->events == NULL)
610 ERROR ("write_riemann plugin: calloc failed.");
611 riemann_msg_protobuf_free (msg);
615 if (host->store_rates)
617 rates = uc_get_rate (ds, vl);
620 ERROR ("write_riemann plugin: uc_get_rate failed.");
621 riemann_msg_protobuf_free (msg);
626 for (i = 0; i < msg->n_events; i++)
628 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
629 (int) i, rates, statuses[i]);
630 if (msg->events[i] == NULL)
632 riemann_msg_protobuf_free (msg);
640 } /* }}} Msg *riemann_value_list_to_protobuf */
642 static int riemann_notification(const notification_t *n, user_data_t *ud) /* {{{ */
645 struct riemann_host *host = ud->data;
648 if (!host->notifications)
651 msg = riemann_notification_to_protobuf (host, n);
655 status = riemann_send (host, msg);
657 ERROR ("write_riemann plugin: riemann_send failed with status %i",
660 riemann_msg_protobuf_free (msg);
662 } /* }}} int riemann_notification */
664 static int riemann_write(const data_set_t *ds, /* {{{ */
665 const value_list_t *vl,
669 int statuses[vl->values_len];
670 struct riemann_host *host = ud->data;
673 if (host->check_thresholds)
674 write_riemann_threshold_check(ds, vl, statuses);
675 msg = riemann_value_list_to_protobuf (host, ds, vl, statuses);
679 status = riemann_send (host, msg);
681 ERROR ("write_riemann plugin: riemann_send failed with status %i",
684 riemann_msg_protobuf_free (msg);
686 } /* }}} int riemann_write */
688 static void riemann_free(void *p) /* {{{ */
690 struct riemann_host *host = p;
695 pthread_mutex_lock (&host->lock);
697 host->reference_count--;
698 if (host->reference_count > 0)
700 pthread_mutex_unlock (&host->lock);
704 riemann_disconnect (host);
706 sfree(host->service);
707 pthread_mutex_destroy (&host->lock);
709 } /* }}} void riemann_free */
711 static int riemann_config_node(oconfig_item_t *ci) /* {{{ */
713 struct riemann_host *host = NULL;
716 oconfig_item_t *child;
717 char callback_name[DATA_MAX_NAME_LEN];
720 if ((host = calloc(1, sizeof (*host))) == NULL) {
721 ERROR ("write_riemann plugin: calloc failed.");
724 pthread_mutex_init (&host->lock, NULL);
725 host->reference_count = 1;
727 host->service = NULL;
728 host->notifications = 1;
729 host->check_thresholds = 0;
730 host->store_rates = 1;
731 host->always_append_ds = 0;
733 host->ttl_factor = RIEMANN_TTL_FACTOR;
735 status = cf_util_get_string (ci, &host->name);
737 WARNING("write_riemann plugin: Required host name is missing.");
742 for (i = 0; i < ci->children_num; i++) {
744 * The code here could be simplified but makes room
745 * for easy adding of new options later on.
747 child = &ci->children[i];
750 if (strcasecmp ("Host", child->key) == 0) {
751 status = cf_util_get_string (child, &host->node);
754 } else if (strcasecmp ("Notifications", child->key) == 0) {
755 status = cf_util_get_boolean(child, &host->notifications);
758 } else if (strcasecmp ("CheckThresholds", child->key) == 0) {
759 status = cf_util_get_boolean(child, &host->check_thresholds);
762 } else if (strcasecmp ("Port", child->key) == 0) {
763 status = cf_util_get_service (child, &host->service);
765 ERROR ("write_riemann plugin: Invalid argument "
766 "configured for the \"Port\" "
770 } else if (strcasecmp ("Protocol", child->key) == 0) {
772 status = cf_util_get_string_buffer (child,
776 ERROR ("write_riemann plugin: cf_util_get_"
777 "string_buffer failed with "
778 "status %i.", status);
782 if (strcasecmp ("UDP", tmp) == 0)
784 else if (strcasecmp ("TCP", tmp) == 0)
787 WARNING ("write_riemann plugin: The value "
788 "\"%s\" is not valid for the "
789 "\"Protocol\" option. Use "
790 "either \"UDP\" or \"TCP\".",
792 } else if (strcasecmp ("StoreRates", child->key) == 0) {
793 status = cf_util_get_boolean (child, &host->store_rates);
796 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
797 status = cf_util_get_boolean (child,
798 &host->always_append_ds);
801 } else if (strcasecmp ("TTLFactor", child->key) == 0) {
803 status = cf_util_get_double (child, &tmp);
807 host->ttl_factor = tmp;
808 } else if (tmp >= 1.0) {
809 NOTICE ("write_riemann plugin: The configured "
810 "TTLFactor is very small "
811 "(%.1f). A value of 2.0 or "
812 "greater is recommended.",
814 host->ttl_factor = tmp;
815 } else if (tmp > 0.0) {
816 WARNING ("write_riemann plugin: The configured "
817 "TTLFactor is too small to be "
818 "useful (%.1f). I'll use it "
819 "since the user knows best, "
820 "but under protest.",
822 host->ttl_factor = tmp;
823 } else { /* zero, negative and NAN */
824 ERROR ("write_riemann plugin: The configured "
825 "TTLFactor is invalid (%.1f).",
829 WARNING("write_riemann plugin: ignoring unknown config "
830 "option: \"%s\"", child->key);
838 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
841 ud.free_func = riemann_free;
843 pthread_mutex_lock (&host->lock);
845 status = plugin_register_write (callback_name, riemann_write, &ud);
847 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
848 "failed with status %i.",
849 callback_name, status);
851 host->reference_count++;
853 status = plugin_register_notification (callback_name,
854 riemann_notification, &ud);
856 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
857 "failed with status %i.",
858 callback_name, status);
860 host->reference_count++;
862 if (host->reference_count <= 1)
864 /* Both callbacks failed => free memory.
865 * We need to unlock here, because riemann_free() will lock.
866 * This is not a race condition, because we're the only one
867 * holding a reference. */
868 pthread_mutex_unlock (&host->lock);
873 host->reference_count--;
874 pthread_mutex_unlock (&host->lock);
877 } /* }}} int riemann_config_node */
879 static int riemann_config(oconfig_item_t *ci) /* {{{ */
882 oconfig_item_t *child;
885 for (i = 0; i < ci->children_num; i++) {
886 child = &ci->children[i];
888 if (strcasecmp("Node", child->key) == 0) {
889 riemann_config_node (child);
890 } else if (strcasecmp(child->key, "attribute") == 0) {
894 if (child->values_num != 2) {
895 WARNING("riemann attributes need both a key and a value.");
898 if (child->values[0].type != OCONFIG_TYPE_STRING ||
899 child->values[1].type != OCONFIG_TYPE_STRING) {
900 WARNING("riemann attribute needs string arguments.");
903 if ((key = strdup(child->values[0].value.string)) == NULL) {
904 WARNING("cannot allocate memory for attribute key.");
907 if ((val = strdup(child->values[1].value.string)) == NULL) {
908 WARNING("cannot allocate memory for attribute value.");
911 strarray_add(&riemann_attrs, &riemann_attrs_num, key);
912 strarray_add(&riemann_attrs, &riemann_attrs_num, val);
913 DEBUG("write_riemann: got attr: %s => %s", key, val);
916 } else if (strcasecmp(child->key, "tag") == 0) {
918 status = cf_util_get_string(child, &tmp);
922 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
923 DEBUG("write_riemann plugin: Got tag: %s", tmp);
926 WARNING ("write_riemann plugin: Ignoring unknown "
927 "configuration option \"%s\" at top level.",
932 } /* }}} int riemann_config */
934 void module_register(void)
936 plugin_register_complex_config ("write_riemann", riemann_config);
939 /* vim: set sw=8 sts=8 ts=8 noet : */