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. */
107 static int riemann_connect(struct riemann_host *host) /* {{{ */
110 struct addrinfo *ai, *res, hints;
114 if (host->flags & F_CONNECT)
117 memset(&hints, 0, sizeof(hints));
118 memset(&service, 0, sizeof(service));
119 hints.ai_family = AF_UNSPEC;
120 hints.ai_socktype = host->use_tcp ? SOCK_STREAM : SOCK_DGRAM;
122 hints.ai_flags |= AI_ADDRCONFIG;
125 node = (host->node != NULL) ? host->node : RIEMANN_HOST;
126 service = (host->service != NULL) ? host->service : RIEMANN_PORT;
128 if ((e = getaddrinfo(node, service, &hints, &res)) != 0) {
129 ERROR ("write_riemann plugin: Unable to resolve host \"%s\": %s",
130 node, gai_strerror(e));
135 for (ai = res; ai != NULL; ai = ai->ai_next) {
136 if ((host->s = socket(ai->ai_family,
138 ai->ai_protocol)) == -1) {
142 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
148 host->flags |= F_CONNECT;
149 DEBUG("write_riemann plugin: got a succesful connection for: %s:%s",
157 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
162 } /* }}} int riemann_connect */
164 /* host->lock must be held when calling this function. */
165 static int riemann_disconnect (struct riemann_host *host) /* {{{ */
167 if ((host->flags & F_CONNECT) == 0)
172 host->flags &= ~F_CONNECT;
175 } /* }}} int riemann_disconnect */
177 static int riemann_send_msg (struct riemann_host *host, const Msg *msg) /* {{{ */
180 u_char *buffer = NULL;
183 status = riemann_connect (host);
187 buffer_len = msg__get_packed_size(msg);
192 buffer = malloc (buffer_len);
193 if (buffer == NULL) {
194 ERROR ("write_riemann plugin: malloc failed.");
197 memset (buffer, 0, buffer_len);
201 uint32_t length = htonl ((uint32_t) (buffer_len - 4));
202 memcpy (buffer, &length, 4);
203 msg__pack(msg, buffer + 4);
207 msg__pack(msg, buffer);
210 status = (int) swrite (host->s, buffer, buffer_len);
214 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s",
215 (host->node != NULL) ? host->node : RIEMANN_HOST,
216 (host->service != NULL) ? host->service : RIEMANN_PORT,
217 sstrerror (errno, errbuf, sizeof (errbuf)));
224 } /* }}} int riemann_send_msg */
226 static int riemann_recv_ack(struct riemann_host *host) /* {{{ */
232 status = (int) sread (host->s, &header, 4);
237 size_t size = ntohl(header);
239 // Buffer on the stack since acknowledges are typically small.
241 memset (buffer, 0, size);
243 status = (int) sread (host->s, buffer, size);
248 msg = msg__unpack (NULL, size, buffer);
255 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s acknowledgement message reported error: %s",
256 (host->node != NULL) ? host->node : RIEMANN_HOST,
257 (host->service != NULL) ? host->service : RIEMANN_PORT,
260 msg__free_unpacked(msg, NULL);
264 msg__free_unpacked (msg, NULL);
266 } /* }}} int riemann_recv_ack */
269 * Function to send messages (Msg) to riemann.
271 * Acquires the host lock, disconnects on errors.
273 static int riemann_send(struct riemann_host *host, Msg const *msg) /* {{{ */
276 pthread_mutex_lock (&host->lock);
278 status = riemann_send_msg(host, msg);
280 riemann_disconnect (host);
281 pthread_mutex_unlock (&host->lock);
286 * For TCP we need to receive message acknowledgemenent.
290 status = riemann_recv_ack(host);
294 riemann_disconnect (host);
295 pthread_mutex_unlock (&host->lock);
300 pthread_mutex_unlock (&host->lock);
302 } /* }}} int riemann_send */
304 static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
306 return (strarray_add (&event->tags, &event->n_tags, tag));
307 } /* }}} int riemann_event_add_tag */
309 static int riemann_event_add_attribute(Event *event, /* {{{ */
310 char const *key, char const *value)
312 Attribute **new_attributes;
315 new_attributes = realloc (event->attributes,
316 sizeof (*event->attributes) * (event->n_attributes + 1));
317 if (new_attributes == NULL)
319 ERROR ("write_riemann plugin: realloc failed.");
322 event->attributes = new_attributes;
324 a = malloc (sizeof (*a));
327 ERROR ("write_riemann plugin: malloc failed.");
332 a->key = strdup (key);
334 a->value = strdup (value);
336 event->attributes[event->n_attributes] = a;
337 event->n_attributes++;
340 } /* }}} int riemann_event_add_attribute */
342 static Msg *riemann_notification_to_protobuf(struct riemann_host *host, /* {{{ */
343 notification_t const *n)
347 char service_buffer[6 * DATA_MAX_NAME_LEN];
348 char const *severity;
349 notification_meta_t *meta;
352 msg = malloc (sizeof (*msg));
355 ERROR ("write_riemann plugin: malloc failed.");
358 memset (msg, 0, sizeof (*msg));
361 msg->events = malloc (sizeof (*msg->events));
362 if (msg->events == NULL)
364 ERROR ("write_riemann plugin: malloc failed.");
369 event = malloc (sizeof (*event));
372 ERROR ("write_riemann plugin: malloc failed.");
377 memset (event, 0, sizeof (*event));
380 msg->events[0] = event;
383 event->host = strdup (n->host);
384 event->time = CDTIME_T_TO_TIME_T (n->time);
389 case NOTIF_OKAY: severity = "ok"; break;
390 case NOTIF_WARNING: severity = "warning"; break;
391 case NOTIF_FAILURE: severity = "critical"; break;
392 default: severity = "unknown";
394 event->state = strdup (severity);
396 riemann_event_add_tag (event, "notification");
398 riemann_event_add_attribute (event, "host", n->host);
399 if (n->plugin[0] != 0)
400 riemann_event_add_attribute (event, "plugin", n->plugin);
401 if (n->plugin_instance[0] != 0)
402 riemann_event_add_attribute (event, "plugin_instance",
406 riemann_event_add_attribute (event, "type", n->type);
407 if (n->type_instance[0] != 0)
408 riemann_event_add_attribute (event, "type_instance",
411 for (i = 0; i < riemann_tags_num; i++)
412 riemann_event_add_tag (event, riemann_tags[i]);
414 format_name (service_buffer, sizeof (service_buffer),
415 /* host = */ "", n->plugin, n->plugin_instance,
416 n->type, n->type_instance);
417 event->service = strdup (&service_buffer[1]);
419 /* Pull in values from threshold */
420 for (meta = n->meta; meta != NULL; meta = meta->next)
422 if (strcasecmp ("CurrentValue", meta->name) != 0)
425 event->metric_d = meta->nm_value.nm_double;
426 event->has_metric_d = 1;
430 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
431 "host = \"%s\", service = \"%s\", state = \"%s\"",
432 event->host, event->service, event->state);
434 } /* }}} Msg *riemann_notification_to_protobuf */
436 static Event *riemann_value_to_protobuf(struct riemann_host const *host, /* {{{ */
437 data_set_t const *ds,
438 value_list_t const *vl, size_t index,
439 gauge_t const *rates)
442 char name_buffer[5 * DATA_MAX_NAME_LEN];
443 char service_buffer[6 * DATA_MAX_NAME_LEN];
446 event = malloc (sizeof (*event));
449 ERROR ("write_riemann plugin: malloc failed.");
452 memset (event, 0, sizeof (*event));
455 event->host = strdup (vl->host);
456 event->time = CDTIME_T_TO_TIME_T (vl->time);
458 event->ttl = CDTIME_T_TO_TIME_T (2 * vl->interval);
461 riemann_event_add_attribute (event, "plugin", vl->plugin);
462 if (vl->plugin_instance[0] != 0)
463 riemann_event_add_attribute (event, "plugin_instance",
464 vl->plugin_instance);
466 riemann_event_add_attribute (event, "type", vl->type);
467 if (vl->type_instance[0] != 0)
468 riemann_event_add_attribute (event, "type_instance",
471 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
473 char ds_type[DATA_MAX_NAME_LEN];
475 ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
476 DS_TYPE_TO_STRING(ds->ds[index].type));
477 riemann_event_add_attribute (event, "ds_type", ds_type);
481 riemann_event_add_attribute (event, "ds_type",
482 DS_TYPE_TO_STRING(ds->ds[index].type));
484 riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
486 char ds_index[DATA_MAX_NAME_LEN];
488 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
489 riemann_event_add_attribute (event, "ds_index", ds_index);
492 for (i = 0; i < riemann_tags_num; i++)
493 riemann_event_add_tag (event, riemann_tags[i]);
495 if (ds->ds[index].type == DS_TYPE_GAUGE)
497 event->has_metric_d = 1;
498 event->metric_d = (double) vl->values[index].gauge;
500 else if (rates != NULL)
502 event->has_metric_d = 1;
503 event->metric_d = (double) rates[index];
507 event->has_metric_sint64 = 1;
508 if (ds->ds[index].type == DS_TYPE_DERIVE)
509 event->metric_sint64 = (int64_t) vl->values[index].derive;
510 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
511 event->metric_sint64 = (int64_t) vl->values[index].absolute;
513 event->metric_sint64 = (int64_t) vl->values[index].counter;
516 format_name (name_buffer, sizeof (name_buffer),
517 /* host = */ "", vl->plugin, vl->plugin_instance,
518 vl->type, vl->type_instance);
519 if (host->always_append_ds || (ds->ds_num > 1))
520 ssnprintf (service_buffer, sizeof (service_buffer),
521 "%s/%s", &name_buffer[1], ds->ds[index].name);
523 sstrncpy (service_buffer, &name_buffer[1],
524 sizeof (service_buffer));
526 event->service = strdup (service_buffer);
528 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
529 "host = \"%s\", service = \"%s\"",
530 event->host, event->service);
532 } /* }}} Event *riemann_value_to_protobuf */
534 static Msg *riemann_value_list_to_protobuf(struct riemann_host const *host, /* {{{ */
535 data_set_t const *ds,
536 value_list_t const *vl)
540 gauge_t *rates = NULL;
542 /* Initialize the Msg structure. */
543 msg = malloc (sizeof (*msg));
546 ERROR ("write_riemann plugin: malloc failed.");
549 memset (msg, 0, sizeof (*msg));
552 /* Set up events. First, the list of pointers. */
553 msg->n_events = (size_t) vl->values_len;
554 msg->events = calloc (msg->n_events, sizeof (*msg->events));
555 if (msg->events == NULL)
557 ERROR ("write_riemann plugin: calloc failed.");
558 riemann_msg_protobuf_free (msg);
562 if (host->store_rates)
564 rates = uc_get_rate (ds, vl);
567 ERROR ("write_riemann plugin: uc_get_rate failed.");
568 riemann_msg_protobuf_free (msg);
573 for (i = 0; i < msg->n_events; i++)
575 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
577 if (msg->events[i] == NULL)
579 riemann_msg_protobuf_free (msg);
587 } /* }}} Msg *riemann_value_list_to_protobuf */
589 static int riemann_notification(const notification_t *n, user_data_t *ud) /* {{{ */
592 struct riemann_host *host = ud->data;
595 msg = riemann_notification_to_protobuf (host, n);
599 status = riemann_send (host, msg);
601 ERROR ("write_riemann plugin: riemann_send failed with status %i",
604 riemann_msg_protobuf_free (msg);
606 } /* }}} int riemann_notification */
608 static int riemann_write(const data_set_t *ds, /* {{{ */
609 const value_list_t *vl,
613 struct riemann_host *host = ud->data;
616 msg = riemann_value_list_to_protobuf (host, ds, vl);
620 status = riemann_send (host, msg);
622 ERROR ("write_riemann plugin: riemann_send failed with status %i",
625 riemann_msg_protobuf_free (msg);
627 } /* }}} int riemann_write */
629 static void riemann_free(void *p) /* {{{ */
631 struct riemann_host *host = p;
636 pthread_mutex_lock (&host->lock);
638 host->reference_count--;
639 if (host->reference_count > 0)
641 pthread_mutex_unlock (&host->lock);
645 riemann_disconnect (host);
647 sfree(host->service);
648 pthread_mutex_destroy (&host->lock);
650 } /* }}} void riemann_free */
652 static int riemann_config_node(oconfig_item_t *ci) /* {{{ */
654 struct riemann_host *host = NULL;
657 oconfig_item_t *child;
658 char callback_name[DATA_MAX_NAME_LEN];
661 if ((host = calloc(1, sizeof (*host))) == NULL) {
662 ERROR ("write_riemann plugin: calloc failed.");
665 pthread_mutex_init (&host->lock, NULL);
666 host->reference_count = 1;
668 host->service = NULL;
669 host->store_rates = 1;
670 host->always_append_ds = 0;
673 status = cf_util_get_string (ci, &host->name);
675 WARNING("write_riemann plugin: Required host name is missing.");
680 for (i = 0; i < ci->children_num; i++) {
682 * The code here could be simplified but makes room
683 * for easy adding of new options later on.
685 child = &ci->children[i];
688 if (strcasecmp ("Host", child->key) == 0) {
689 status = cf_util_get_string (child, &host->node);
692 } else if (strcasecmp ("Port", child->key) == 0) {
693 status = cf_util_get_service (child, &host->service);
695 ERROR ("write_riemann plugin: Invalid argument "
696 "configured for the \"Port\" "
700 } else if (strcasecmp ("Protocol", child->key) == 0) {
702 status = cf_util_get_string_buffer (child,
706 ERROR ("write_riemann plugin: cf_util_get_"
707 "string_buffer failed with "
708 "status %i.", status);
712 if (strcasecmp ("UDP", tmp) == 0)
714 else if (strcasecmp ("TCP", tmp) == 0)
717 WARNING ("write_riemann plugin: The value "
718 "\"%s\" is not valid for the "
719 "\"Protocol\" option. Use "
720 "either \"UDP\" or \"TCP\".",
722 } else if (strcasecmp ("StoreRates", child->key) == 0) {
723 status = cf_util_get_boolean (child, &host->store_rates);
726 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
727 status = cf_util_get_boolean (child,
728 &host->always_append_ds);
732 WARNING("write_riemann plugin: ignoring unknown config "
733 "option: \"%s\"", child->key);
741 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
744 ud.free_func = riemann_free;
746 pthread_mutex_lock (&host->lock);
748 status = plugin_register_write (callback_name, riemann_write, &ud);
750 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
751 "failed with status %i.",
752 callback_name, status);
754 host->reference_count++;
756 status = plugin_register_notification (callback_name,
757 riemann_notification, &ud);
759 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
760 "failed with status %i.",
761 callback_name, status);
763 host->reference_count++;
765 if (host->reference_count <= 1)
767 /* Both callbacks failed => free memory.
768 * We need to unlock here, because riemann_free() will lock.
769 * This is not a race condition, because we're the only one
770 * holding a reference. */
771 pthread_mutex_unlock (&host->lock);
776 host->reference_count--;
777 pthread_mutex_unlock (&host->lock);
780 } /* }}} int riemann_config_node */
782 static int riemann_config(oconfig_item_t *ci) /* {{{ */
785 oconfig_item_t *child;
788 for (i = 0; i < ci->children_num; i++) {
789 child = &ci->children[i];
791 if (strcasecmp("Node", child->key) == 0) {
792 riemann_config_node (child);
793 } else if (strcasecmp(child->key, "tag") == 0) {
795 status = cf_util_get_string(child, &tmp);
799 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
800 DEBUG("write_riemann plugin: Got tag: %s", tmp);
803 WARNING ("write_riemann plugin: Ignoring unknown "
804 "configuration option \"%s\" at top level.",
809 } /* }}} int riemann_config */
811 void module_register(void)
813 plugin_register_complex_config ("write_riemann", riemann_config);
816 /* vim: set sw=8 sts=8 ts=8 noet : */