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"
43 #define F_CONNECT 0x01
47 _Bool always_append_ds;
56 static char **riemann_tags;
57 static size_t riemann_tags_num;
59 static void riemann_event_protobuf_free (Event *event) /* {{{ */
67 sfree (event->service);
69 sfree (event->description);
71 strarray_free (event->tags, event->n_tags);
75 for (i = 0; i < event->n_attributes; i++)
77 sfree (event->attributes[i]->key);
78 sfree (event->attributes[i]->value);
79 sfree (event->attributes[i]);
81 sfree (event->attributes);
82 event->n_attributes = 0;
85 } /* }}} void riemann_event_protobuf_free */
87 static void riemann_msg_protobuf_free (Msg *msg) /* {{{ */
94 for (i = 0; i < msg->n_events; i++)
96 riemann_event_protobuf_free (msg->events[i]);
97 msg->events[i] = NULL;
104 } /* }}} void riemann_msg_protobuf_free */
106 /* host->lock must be held when calling this function. */
108 riemann_connect(struct riemann_host *host)
111 struct addrinfo *ai, *res, hints;
115 if (host->flags & F_CONNECT)
118 memset(&hints, 0, sizeof(hints));
119 memset(&service, 0, sizeof(service));
120 hints.ai_family = AF_UNSPEC;
121 hints.ai_socktype = host->use_tcp ? SOCK_STREAM : SOCK_DGRAM;
123 hints.ai_flags |= AI_ADDRCONFIG;
126 node = (host->node != NULL) ? host->node : RIEMANN_HOST;
127 service = (host->service != NULL) ? host->service : RIEMANN_PORT;
129 if ((e = getaddrinfo(node, service, &hints, &res)) != 0) {
130 ERROR ("write_riemann plugin: Unable to resolve host \"%s\": %s",
131 node, gai_strerror(e));
136 for (ai = res; ai != NULL; ai = ai->ai_next) {
137 if ((host->s = socket(ai->ai_family,
139 ai->ai_protocol)) == -1) {
143 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
149 host->flags |= F_CONNECT;
150 DEBUG("write_riemann plugin: got a succesful connection for: %s:%s",
158 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
165 /* host->lock must be held when calling this function. */
167 riemann_disconnect (struct riemann_host *host)
169 if ((host->flags & F_CONNECT) == 0)
174 host->flags &= ~F_CONNECT;
180 riemann_send_msg(struct riemann_host *host, const Msg *msg)
183 u_char *buffer = NULL;
186 status = riemann_connect (host);
191 buffer_len = msg__get_packed_size(msg);
196 buffer = malloc (buffer_len);
198 if (buffer == NULL) {
199 ERROR ("write_riemann plugin: malloc failed.");
203 memset (buffer, 0, buffer_len);
207 uint32_t length = htonl ((uint32_t) (buffer_len - 4));
208 memcpy (buffer, &length, 4);
209 msg__pack(msg, buffer + 4);
213 msg__pack(msg, buffer);
216 status = (int) swrite (host->s, buffer, buffer_len);
222 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s",
223 (host->node != NULL) ? host->node : RIEMANN_HOST,
224 (host->service != NULL) ? host->service : RIEMANN_PORT,
225 sstrerror (errno, errbuf, sizeof (errbuf)));
236 riemann_recv_ack(struct riemann_host *host)
242 status = (int) sread (host->s, &header, 4);
247 size_t size = ntohl(header);
249 // Buffer on the stack since acknowledges are typically small.
251 memset (buffer, 0, size);
253 status = (int) sread (host->s, buffer, size);
258 msg = msg__unpack (NULL, size, buffer);
265 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s acknowledgement message reported error: %s",
266 (host->node != NULL) ? host->node : RIEMANN_HOST,
267 (host->service != NULL) ? host->service : RIEMANN_PORT,
270 msg__free_unpacked(msg, NULL);
274 msg__free_unpacked (msg, NULL);
279 * Function to send messages (Msg) to riemann.
281 * Acquires the host lock, disconnects on errors.
284 riemann_send(struct riemann_host *host, Msg const *msg)
287 pthread_mutex_lock (&host->lock);
289 status = riemann_send_msg(host, msg);
292 riemann_disconnect (host);
293 pthread_mutex_unlock (&host->lock);
298 * For TCP we need to receive message acknowledgemenent.
302 status = riemann_recv_ack(host);
306 riemann_disconnect (host);
307 pthread_mutex_unlock (&host->lock);
312 pthread_mutex_unlock (&host->lock);
316 static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
318 return (strarray_add (&event->tags, &event->n_tags, tag));
319 } /* }}} int riemann_event_add_tag */
321 static int riemann_event_add_attribute (Event *event, /* {{{ */
322 char const *key, char const *value)
324 Attribute **new_attributes;
327 new_attributes = realloc (event->attributes,
328 sizeof (*event->attributes) * (event->n_attributes + 1));
329 if (new_attributes == NULL)
331 ERROR ("write_riemann plugin: realloc failed.");
334 event->attributes = new_attributes;
336 a = malloc (sizeof (*a));
339 ERROR ("write_riemann plugin: malloc failed.");
344 a->key = strdup (key);
346 a->value = strdup (value);
348 event->attributes[event->n_attributes] = a;
349 event->n_attributes++;
352 } /* }}} int riemann_event_add_attribute */
354 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
355 notification_t const *n)
359 char service_buffer[6 * DATA_MAX_NAME_LEN];
360 char const *severity;
361 notification_meta_t *meta;
364 msg = malloc (sizeof (*msg));
367 ERROR ("write_riemann plugin: malloc failed.");
370 memset (msg, 0, sizeof (*msg));
373 msg->events = malloc (sizeof (*msg->events));
374 if (msg->events == NULL)
376 ERROR ("write_riemann plugin: malloc failed.");
381 event = malloc (sizeof (*event));
384 ERROR ("write_riemann plugin: malloc failed.");
389 memset (event, 0, sizeof (*event));
392 msg->events[0] = event;
395 event->host = strdup (n->host);
396 event->time = CDTIME_T_TO_TIME_T (n->time);
401 case NOTIF_OKAY: severity = "ok"; break;
402 case NOTIF_WARNING: severity = "warning"; break;
403 case NOTIF_FAILURE: severity = "critical"; break;
404 default: severity = "unknown";
406 event->state = strdup (severity);
408 riemann_event_add_tag (event, "notification");
410 riemann_event_add_attribute (event, "host", n->host);
411 if (n->plugin[0] != 0)
412 riemann_event_add_attribute (event, "plugin", n->plugin);
413 if (n->plugin_instance[0] != 0)
414 riemann_event_add_attribute (event, "plugin_instance",
418 riemann_event_add_attribute (event, "type", n->type);
419 if (n->type_instance[0] != 0)
420 riemann_event_add_attribute (event, "type_instance",
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 */
432 for (meta = n->meta; meta != NULL; meta = meta->next)
434 if (strcasecmp ("CurrentValue", meta->name) != 0)
437 event->metric_d = meta->nm_value.nm_double;
438 event->has_metric_d = 1;
442 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
443 "host = \"%s\", service = \"%s\", state = \"%s\"",
444 event->host, event->service, event->state);
446 } /* }}} Msg *riemann_notification_to_protobuf */
448 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
449 data_set_t const *ds,
450 value_list_t const *vl, size_t index,
451 gauge_t const *rates)
454 char name_buffer[5 * DATA_MAX_NAME_LEN];
455 char service_buffer[6 * DATA_MAX_NAME_LEN];
458 event = malloc (sizeof (*event));
461 ERROR ("write_riemann plugin: malloc failed.");
464 memset (event, 0, sizeof (*event));
467 event->host = strdup (vl->host);
468 event->time = CDTIME_T_TO_TIME_T (vl->time);
470 event->ttl = CDTIME_T_TO_TIME_T (2 * vl->interval);
473 riemann_event_add_attribute (event, "plugin", vl->plugin);
474 if (vl->plugin_instance[0] != 0)
475 riemann_event_add_attribute (event, "plugin_instance",
476 vl->plugin_instance);
478 riemann_event_add_attribute (event, "type", vl->type);
479 if (vl->type_instance[0] != 0)
480 riemann_event_add_attribute (event, "type_instance",
483 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
485 char ds_type[DATA_MAX_NAME_LEN];
487 ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
488 DS_TYPE_TO_STRING(ds->ds[index].type));
489 riemann_event_add_attribute (event, "ds_type", ds_type);
493 riemann_event_add_attribute (event, "ds_type",
494 DS_TYPE_TO_STRING(ds->ds[index].type));
496 riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
498 char ds_index[DATA_MAX_NAME_LEN];
500 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
501 riemann_event_add_attribute (event, "ds_index", ds_index);
504 for (i = 0; i < riemann_tags_num; i++)
505 riemann_event_add_tag (event, riemann_tags[i]);
507 if (ds->ds[index].type == DS_TYPE_GAUGE)
509 event->has_metric_d = 1;
510 event->metric_d = (double) vl->values[index].gauge;
512 else if (rates != NULL)
514 event->has_metric_d = 1;
515 event->metric_d = (double) rates[index];
519 event->has_metric_sint64 = 1;
520 if (ds->ds[index].type == DS_TYPE_DERIVE)
521 event->metric_sint64 = (int64_t) vl->values[index].derive;
522 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
523 event->metric_sint64 = (int64_t) vl->values[index].absolute;
525 event->metric_sint64 = (int64_t) vl->values[index].counter;
528 format_name (name_buffer, sizeof (name_buffer),
529 /* host = */ "", vl->plugin, vl->plugin_instance,
530 vl->type, vl->type_instance);
531 if (host->always_append_ds || (ds->ds_num > 1))
532 ssnprintf (service_buffer, sizeof (service_buffer),
533 "%s/%s", &name_buffer[1], ds->ds[index].name);
535 sstrncpy (service_buffer, &name_buffer[1],
536 sizeof (service_buffer));
538 event->service = strdup (service_buffer);
540 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
541 "host = \"%s\", service = \"%s\"",
542 event->host, event->service);
544 } /* }}} Event *riemann_value_to_protobuf */
546 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
547 data_set_t const *ds,
548 value_list_t const *vl)
552 gauge_t *rates = NULL;
554 /* Initialize the Msg structure. */
555 msg = malloc (sizeof (*msg));
558 ERROR ("write_riemann plugin: malloc failed.");
561 memset (msg, 0, sizeof (*msg));
564 /* Set up events. First, the list of pointers. */
565 msg->n_events = (size_t) vl->values_len;
566 msg->events = calloc (msg->n_events, sizeof (*msg->events));
567 if (msg->events == NULL)
569 ERROR ("write_riemann plugin: calloc failed.");
570 riemann_msg_protobuf_free (msg);
574 if (host->store_rates)
576 rates = uc_get_rate (ds, vl);
579 ERROR ("write_riemann plugin: uc_get_rate failed.");
580 riemann_msg_protobuf_free (msg);
585 for (i = 0; i < msg->n_events; i++)
587 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
589 if (msg->events[i] == NULL)
591 riemann_msg_protobuf_free (msg);
599 } /* }}} Msg *riemann_value_list_to_protobuf */
602 riemann_notification(const notification_t *n, user_data_t *ud)
605 struct riemann_host *host = ud->data;
608 msg = riemann_notification_to_protobuf (host, n);
612 status = riemann_send (host, msg);
614 ERROR ("write_riemann plugin: riemann_send failed with status %i",
617 riemann_msg_protobuf_free (msg);
619 } /* }}} int riemann_notification */
622 riemann_write(const data_set_t *ds,
623 const value_list_t *vl,
627 struct riemann_host *host = ud->data;
630 msg = riemann_value_list_to_protobuf (host, ds, vl);
634 status = riemann_send (host, msg);
636 ERROR ("write_riemann plugin: riemann_send failed with status %i",
639 riemann_msg_protobuf_free (msg);
644 riemann_free(void *p)
646 struct riemann_host *host = p;
651 pthread_mutex_lock (&host->lock);
653 host->reference_count--;
654 if (host->reference_count > 0)
656 pthread_mutex_unlock (&host->lock);
660 riemann_disconnect (host);
662 sfree(host->service);
663 pthread_mutex_destroy (&host->lock);
668 riemann_config_node(oconfig_item_t *ci)
670 struct riemann_host *host = NULL;
673 oconfig_item_t *child;
674 char callback_name[DATA_MAX_NAME_LEN];
677 if ((host = calloc(1, sizeof (*host))) == NULL) {
678 ERROR ("write_riemann plugin: calloc failed.");
681 pthread_mutex_init (&host->lock, NULL);
682 host->reference_count = 1;
684 host->service = NULL;
685 host->store_rates = 1;
686 host->always_append_ds = 0;
689 status = cf_util_get_string (ci, &host->name);
691 WARNING("write_riemann plugin: Required host name is missing.");
696 for (i = 0; i < ci->children_num; i++) {
698 * The code here could be simplified but makes room
699 * for easy adding of new options later on.
701 child = &ci->children[i];
704 if (strcasecmp ("Host", child->key) == 0) {
705 status = cf_util_get_string (child, &host->node);
708 } else if (strcasecmp ("Port", child->key) == 0) {
709 status = cf_util_get_service (child, &host->service);
711 ERROR ("write_riemann plugin: Invalid argument "
712 "configured for the \"Port\" "
716 } else if (strcasecmp ("Protocol", child->key) == 0) {
718 status = cf_util_get_string_buffer (child,
722 ERROR ("write_riemann plugin: cf_util_get_"
723 "string_buffer failed with "
724 "status %i.", status);
728 if (strcasecmp ("UDP", tmp) == 0)
730 else if (strcasecmp ("TCP", tmp) == 0)
733 WARNING ("write_riemann plugin: The value "
734 "\"%s\" is not valid for the "
735 "\"Protocol\" option. Use "
736 "either \"UDP\" or \"TCP\".",
738 } else if (strcasecmp ("StoreRates", child->key) == 0) {
739 status = cf_util_get_boolean (child, &host->store_rates);
742 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
743 status = cf_util_get_boolean (child,
744 &host->always_append_ds);
748 WARNING("write_riemann plugin: ignoring unknown config "
749 "option: \"%s\"", child->key);
757 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
760 ud.free_func = riemann_free;
762 pthread_mutex_lock (&host->lock);
764 status = plugin_register_write (callback_name, riemann_write, &ud);
766 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
767 "failed with status %i.",
768 callback_name, status);
770 host->reference_count++;
772 status = plugin_register_notification (callback_name,
773 riemann_notification, &ud);
775 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
776 "failed with status %i.",
777 callback_name, status);
779 host->reference_count++;
781 if (host->reference_count <= 1)
783 /* Both callbacks failed => free memory.
784 * We need to unlock here, because riemann_free() will lock.
785 * This is not a race condition, because we're the only one
786 * holding a reference. */
787 pthread_mutex_unlock (&host->lock);
792 host->reference_count--;
793 pthread_mutex_unlock (&host->lock);
799 riemann_config(oconfig_item_t *ci)
802 oconfig_item_t *child;
805 for (i = 0; i < ci->children_num; i++) {
806 child = &ci->children[i];
808 if (strcasecmp("Node", child->key) == 0) {
809 riemann_config_node (child);
810 } else if (strcasecmp(child->key, "tag") == 0) {
812 status = cf_util_get_string(child, &tmp);
816 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
817 DEBUG("write_riemann plugin: Got tag: %s", tmp);
820 WARNING ("write_riemann plugin: Ignoring unknown "
821 "configuration option \"%s\" at top level.",
829 module_register(void)
831 plugin_register_complex_config ("write_riemann", riemann_config);
834 /* vim: set sw=8 sts=8 ts=8 noet : */