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(struct riemann_host *host, Msg const *msg)
186 pthread_mutex_lock (&host->lock);
188 status = riemann_connect (host);
191 pthread_mutex_unlock (&host->lock);
195 buffer_len = msg__get_packed_size(msg);
199 buffer = malloc (buffer_len);
200 if (buffer == NULL) {
201 pthread_mutex_unlock (&host->lock);
202 ERROR ("write_riemann plugin: malloc failed.");
205 memset (buffer, 0, buffer_len);
209 uint32_t length = htonl ((uint32_t) (buffer_len - 4));
210 memcpy (buffer, &length, 4);
211 msg__pack(msg, buffer + 4);
215 msg__pack(msg, buffer);
218 status = (int) swrite (host->s, buffer, buffer_len);
223 riemann_disconnect (host);
224 pthread_mutex_unlock (&host->lock);
226 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s",
227 (host->node != NULL) ? host->node : RIEMANN_HOST,
228 (host->service != NULL) ? host->service : RIEMANN_PORT,
229 sstrerror (errno, errbuf, sizeof (errbuf)));
234 pthread_mutex_unlock (&host->lock);
239 static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
241 return (strarray_add (&event->tags, &event->n_tags, tag));
242 } /* }}} int riemann_event_add_tag */
244 static int riemann_event_add_attribute (Event *event, /* {{{ */
245 char const *key, char const *value)
247 Attribute **new_attributes;
250 new_attributes = realloc (event->attributes,
251 sizeof (*event->attributes) * (event->n_attributes + 1));
252 if (new_attributes == NULL)
254 ERROR ("write_riemann plugin: realloc failed.");
257 event->attributes = new_attributes;
259 a = malloc (sizeof (*a));
262 ERROR ("write_riemann plugin: malloc failed.");
267 a->key = strdup (key);
269 a->value = strdup (value);
271 event->attributes[event->n_attributes] = a;
272 event->n_attributes++;
275 } /* }}} int riemann_event_add_attribute */
277 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
278 notification_t const *n)
282 char service_buffer[6 * DATA_MAX_NAME_LEN];
283 char const *severity;
284 notification_meta_t *meta;
287 msg = malloc (sizeof (*msg));
290 ERROR ("write_riemann plugin: malloc failed.");
293 memset (msg, 0, sizeof (*msg));
296 msg->events = malloc (sizeof (*msg->events));
297 if (msg->events == NULL)
299 ERROR ("write_riemann plugin: malloc failed.");
304 event = malloc (sizeof (*event));
307 ERROR ("write_riemann plugin: malloc failed.");
312 memset (event, 0, sizeof (*event));
315 msg->events[0] = event;
318 event->host = strdup (n->host);
319 event->time = CDTIME_T_TO_TIME_T (n->time);
324 case NOTIF_OKAY: severity = "ok"; break;
325 case NOTIF_WARNING: severity = "warning"; break;
326 case NOTIF_FAILURE: severity = "critical"; break;
327 default: severity = "unknown";
329 event->state = strdup (severity);
331 riemann_event_add_tag (event, "notification");
333 riemann_event_add_attribute (event, "host", n->host);
334 if (n->plugin[0] != 0)
335 riemann_event_add_attribute (event, "plugin", n->plugin);
336 if (n->plugin_instance[0] != 0)
337 riemann_event_add_attribute (event, "plugin_instance",
341 riemann_event_add_attribute (event, "type", n->type);
342 if (n->type_instance[0] != 0)
343 riemann_event_add_attribute (event, "type_instance",
346 for (i = 0; i < riemann_tags_num; i++)
347 riemann_event_add_tag (event, riemann_tags[i]);
349 format_name (service_buffer, sizeof (service_buffer),
350 /* host = */ "", n->plugin, n->plugin_instance,
351 n->type, n->type_instance);
352 event->service = strdup (&service_buffer[1]);
354 /* Pull in values from threshold */
355 for (meta = n->meta; meta != NULL; meta = meta->next)
357 if (strcasecmp ("CurrentValue", meta->name) != 0)
360 event->metric_d = meta->nm_value.nm_double;
361 event->has_metric_d = 1;
365 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
366 "host = \"%s\", service = \"%s\", state = \"%s\"",
367 event->host, event->service, event->state);
369 } /* }}} Msg *riemann_notification_to_protobuf */
371 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
372 data_set_t const *ds,
373 value_list_t const *vl, size_t index,
374 gauge_t const *rates)
377 char name_buffer[5 * DATA_MAX_NAME_LEN];
378 char service_buffer[6 * DATA_MAX_NAME_LEN];
381 event = malloc (sizeof (*event));
384 ERROR ("write_riemann plugin: malloc failed.");
387 memset (event, 0, sizeof (*event));
390 event->host = strdup (vl->host);
391 event->time = CDTIME_T_TO_TIME_T (vl->time);
393 event->ttl = CDTIME_T_TO_TIME_T (2 * vl->interval);
396 riemann_event_add_attribute (event, "plugin", vl->plugin);
397 if (vl->plugin_instance[0] != 0)
398 riemann_event_add_attribute (event, "plugin_instance",
399 vl->plugin_instance);
401 riemann_event_add_attribute (event, "type", vl->type);
402 if (vl->type_instance[0] != 0)
403 riemann_event_add_attribute (event, "type_instance",
406 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
408 char ds_type[DATA_MAX_NAME_LEN];
410 ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
411 DS_TYPE_TO_STRING(ds->ds[index].type));
412 riemann_event_add_attribute (event, "ds_type", ds_type);
416 riemann_event_add_attribute (event, "ds_type",
417 DS_TYPE_TO_STRING(ds->ds[index].type));
419 riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
421 char ds_index[DATA_MAX_NAME_LEN];
423 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
424 riemann_event_add_attribute (event, "ds_index", ds_index);
427 for (i = 0; i < riemann_tags_num; i++)
428 riemann_event_add_tag (event, riemann_tags[i]);
430 if (ds->ds[index].type == DS_TYPE_GAUGE)
432 event->has_metric_d = 1;
433 event->metric_d = (double) vl->values[index].gauge;
435 else if (rates != NULL)
437 event->has_metric_d = 1;
438 event->metric_d = (double) rates[index];
442 event->has_metric_sint64 = 1;
443 if (ds->ds[index].type == DS_TYPE_DERIVE)
444 event->metric_sint64 = (int64_t) vl->values[index].derive;
445 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
446 event->metric_sint64 = (int64_t) vl->values[index].absolute;
448 event->metric_sint64 = (int64_t) vl->values[index].counter;
451 format_name (name_buffer, sizeof (name_buffer),
452 /* host = */ "", vl->plugin, vl->plugin_instance,
453 vl->type, vl->type_instance);
454 if (host->always_append_ds || (ds->ds_num > 1))
455 ssnprintf (service_buffer, sizeof (service_buffer),
456 "%s/%s", &name_buffer[1], ds->ds[index].name);
458 sstrncpy (service_buffer, &name_buffer[1],
459 sizeof (service_buffer));
461 event->service = strdup (service_buffer);
463 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
464 "host = \"%s\", service = \"%s\"",
465 event->host, event->service);
467 } /* }}} Event *riemann_value_to_protobuf */
469 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
470 data_set_t const *ds,
471 value_list_t const *vl)
475 gauge_t *rates = NULL;
477 /* Initialize the Msg structure. */
478 msg = malloc (sizeof (*msg));
481 ERROR ("write_riemann plugin: malloc failed.");
484 memset (msg, 0, sizeof (*msg));
487 /* Set up events. First, the list of pointers. */
488 msg->n_events = (size_t) vl->values_len;
489 msg->events = calloc (msg->n_events, sizeof (*msg->events));
490 if (msg->events == NULL)
492 ERROR ("write_riemann plugin: calloc failed.");
493 riemann_msg_protobuf_free (msg);
497 if (host->store_rates)
499 rates = uc_get_rate (ds, vl);
502 ERROR ("write_riemann plugin: uc_get_rate failed.");
503 riemann_msg_protobuf_free (msg);
508 for (i = 0; i < msg->n_events; i++)
510 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
512 if (msg->events[i] == NULL)
514 riemann_msg_protobuf_free (msg);
522 } /* }}} Msg *riemann_value_list_to_protobuf */
525 riemann_notification(const notification_t *n, user_data_t *ud)
528 struct riemann_host *host = ud->data;
531 msg = riemann_notification_to_protobuf (host, n);
535 status = riemann_send (host, msg);
537 ERROR ("write_riemann plugin: riemann_send failed with status %i",
540 riemann_msg_protobuf_free (msg);
542 } /* }}} int riemann_notification */
545 riemann_write(const data_set_t *ds,
546 const value_list_t *vl,
550 struct riemann_host *host = ud->data;
553 msg = riemann_value_list_to_protobuf (host, ds, vl);
557 status = riemann_send (host, msg);
559 ERROR ("write_riemann plugin: riemann_send failed with status %i",
562 riemann_msg_protobuf_free (msg);
567 riemann_free(void *p)
569 struct riemann_host *host = p;
574 pthread_mutex_lock (&host->lock);
576 host->reference_count--;
577 if (host->reference_count > 0)
579 pthread_mutex_unlock (&host->lock);
583 riemann_disconnect (host);
585 sfree(host->service);
586 pthread_mutex_destroy (&host->lock);
591 riemann_config_node(oconfig_item_t *ci)
593 struct riemann_host *host = NULL;
596 oconfig_item_t *child;
597 char callback_name[DATA_MAX_NAME_LEN];
600 if ((host = calloc(1, sizeof (*host))) == NULL) {
601 ERROR ("write_riemann plugin: calloc failed.");
604 pthread_mutex_init (&host->lock, NULL);
605 host->reference_count = 1;
607 host->service = NULL;
608 host->store_rates = 1;
609 host->always_append_ds = 0;
612 status = cf_util_get_string (ci, &host->name);
614 WARNING("write_riemann plugin: Required host name is missing.");
619 for (i = 0; i < ci->children_num; i++) {
621 * The code here could be simplified but makes room
622 * for easy adding of new options later on.
624 child = &ci->children[i];
627 if (strcasecmp ("Host", child->key) == 0) {
628 status = cf_util_get_string (child, &host->node);
631 } else if (strcasecmp ("Port", child->key) == 0) {
632 status = cf_util_get_service (child, &host->service);
634 ERROR ("write_riemann plugin: Invalid argument "
635 "configured for the \"Port\" "
639 } else if (strcasecmp ("Protocol", child->key) == 0) {
641 status = cf_util_get_string_buffer (child,
645 ERROR ("write_riemann plugin: cf_util_get_"
646 "string_buffer failed with "
647 "status %i.", status);
651 if (strcasecmp ("UDP", tmp) == 0)
653 else if (strcasecmp ("TCP", tmp) == 0)
656 WARNING ("write_riemann plugin: The value "
657 "\"%s\" is not valid for the "
658 "\"Protocol\" option. Use "
659 "either \"UDP\" or \"TCP\".",
661 } else if (strcasecmp ("StoreRates", child->key) == 0) {
662 status = cf_util_get_boolean (child, &host->store_rates);
665 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
666 status = cf_util_get_boolean (child,
667 &host->always_append_ds);
671 WARNING("write_riemann plugin: ignoring unknown config "
672 "option: \"%s\"", child->key);
680 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
683 ud.free_func = riemann_free;
685 pthread_mutex_lock (&host->lock);
687 status = plugin_register_write (callback_name, riemann_write, &ud);
689 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
690 "failed with status %i.",
691 callback_name, status);
693 host->reference_count++;
695 status = plugin_register_notification (callback_name,
696 riemann_notification, &ud);
698 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
699 "failed with status %i.",
700 callback_name, status);
702 host->reference_count++;
704 if (host->reference_count <= 1)
706 /* Both callbacks failed => free memory.
707 * We need to unlock here, because riemann_free() will lock.
708 * This is not a race condition, because we're the only one
709 * holding a reference. */
710 pthread_mutex_unlock (&host->lock);
715 host->reference_count--;
716 pthread_mutex_unlock (&host->lock);
722 riemann_config(oconfig_item_t *ci)
725 oconfig_item_t *child;
728 for (i = 0; i < ci->children_num; i++) {
729 child = &ci->children[i];
731 if (strcasecmp("Node", child->key) == 0) {
732 riemann_config_node (child);
733 } else if (strcasecmp(child->key, "tag") == 0) {
735 status = cf_util_get_string(child, &tmp);
739 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
740 DEBUG("write_riemann plugin: Got tag: %s", tmp);
743 WARNING ("write_riemann plugin: Ignoring unknown "
744 "configuration option \"%s\" at top level.",
752 module_register(void)
754 plugin_register_complex_config ("write_riemann", riemann_config);
757 /* vim: set sw=8 sts=8 ts=8 noet : */