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) /* {{{ */
65 sfree (event->service);
67 sfree (event->description);
69 strarray_free (event->tags, event->n_tags);
74 } /* }}} void riemann_event_protobuf_free */
76 static void riemann_msg_protobuf_free (Msg *msg) /* {{{ */
83 for (i = 0; i < msg->n_events; i++)
85 riemann_event_protobuf_free (msg->events[i]);
86 msg->events[i] = NULL;
93 } /* }}} void riemann_msg_protobuf_free */
95 /* host->lock must be held when calling this function. */
97 riemann_connect(struct riemann_host *host)
100 struct addrinfo *ai, *res, hints;
104 if (host->flags & F_CONNECT)
107 memset(&hints, 0, sizeof(hints));
108 memset(&service, 0, sizeof(service));
109 hints.ai_family = AF_UNSPEC;
110 hints.ai_socktype = host->use_tcp ? SOCK_STREAM : SOCK_DGRAM;
112 hints.ai_flags |= AI_ADDRCONFIG;
115 node = (host->node != NULL) ? host->node : RIEMANN_HOST;
116 service = (host->service != NULL) ? host->service : RIEMANN_PORT;
118 if ((e = getaddrinfo(node, service, &hints, &res)) != 0) {
119 ERROR ("write_riemann plugin: Unable to resolve host \"%s\": %s",
120 node, gai_strerror(e));
125 for (ai = res; ai != NULL; ai = ai->ai_next) {
126 if ((host->s = socket(ai->ai_family,
128 ai->ai_protocol)) == -1) {
132 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
138 host->flags |= F_CONNECT;
139 DEBUG("write_riemann plugin: got a succesful connection for: %s:%s",
147 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
154 /* host->lock must be held when calling this function. */
156 riemann_disconnect (struct riemann_host *host)
158 if ((host->flags & F_CONNECT) == 0)
163 host->flags &= ~F_CONNECT;
169 riemann_send(struct riemann_host *host, Msg const *msg)
175 pthread_mutex_lock (&host->lock);
177 status = riemann_connect (host);
180 pthread_mutex_unlock (&host->lock);
184 buffer_len = msg__get_packed_size(msg);
188 buffer = malloc (buffer_len);
189 if (buffer == NULL) {
190 pthread_mutex_unlock (&host->lock);
191 ERROR ("write_riemann plugin: malloc failed.");
194 memset (buffer, 0, buffer_len);
198 uint32_t length = htonl ((uint32_t) (buffer_len - 4));
199 memcpy (buffer, &length, 4);
200 msg__pack(msg, buffer + 4);
204 msg__pack(msg, buffer);
207 status = (int) swrite (host->s, buffer, buffer_len);
212 riemann_disconnect (host);
213 pthread_mutex_unlock (&host->lock);
215 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s",
216 (host->node != NULL) ? host->node : RIEMANN_HOST,
217 (host->service != NULL) ? host->service : RIEMANN_PORT,
218 sstrerror (errno, errbuf, sizeof (errbuf)));
223 pthread_mutex_unlock (&host->lock);
228 static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
230 return (strarray_add (&event->tags, &event->n_tags, tag));
231 } /* }}} int riemann_event_add_tag */
233 static int riemann_event_add_attribute (Event *event, /* {{{ */
234 char const *key, char const *value)
236 Attribute **new_attributes;
239 new_attributes = realloc (event->attributes,
240 sizeof (*event->attributes) * (event->n_attributes + 1));
241 if (new_attributes == NULL)
243 ERROR ("write_riemann plugin: realloc failed.");
246 event->attributes = new_attributes;
248 a = malloc (sizeof (*a));
251 ERROR ("write_riemann plugin: malloc failed.");
256 a->key = strdup (key);
258 a->value = strdup (value);
260 event->attributes[event->n_attributes] = a;
261 event->n_attributes++;
264 } /* }}} int riemann_event_add_attribute */
266 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
267 notification_t const *n)
271 char service_buffer[6 * DATA_MAX_NAME_LEN];
272 char const *severity;
273 notification_meta_t *meta;
276 msg = malloc (sizeof (*msg));
279 ERROR ("write_riemann plugin: malloc failed.");
282 memset (msg, 0, sizeof (*msg));
285 msg->events = malloc (sizeof (*msg->events));
286 if (msg->events == NULL)
288 ERROR ("write_riemann plugin: malloc failed.");
293 event = malloc (sizeof (*event));
296 ERROR ("write_riemann plugin: malloc failed.");
301 memset (event, 0, sizeof (*event));
304 msg->events[0] = event;
307 event->host = strdup (n->host);
308 event->time = CDTIME_T_TO_TIME_T (n->time);
313 case NOTIF_OKAY: severity = "ok"; break;
314 case NOTIF_WARNING: severity = "warning"; break;
315 case NOTIF_FAILURE: severity = "critical"; break;
316 default: severity = "unknown";
318 event->state = strdup (severity);
320 riemann_event_add_tag (event, "notification");
322 riemann_event_add_attribute (event, "host", n->host);
323 if (n->plugin[0] != 0)
324 riemann_event_add_attribute (event, "plugin", n->plugin);
325 if (n->plugin_instance[0] != 0)
326 riemann_event_add_attribute (event, "plugin_instance",
330 riemann_event_add_attribute (event, "type", n->type);
331 if (n->type_instance[0] != 0)
332 riemann_event_add_attribute (event, "type_instance",
335 for (i = 0; i < riemann_tags_num; i++)
336 riemann_event_add_tag (event, riemann_tags[i]);
338 format_name (service_buffer, sizeof (service_buffer),
339 /* host = */ "", n->plugin, n->plugin_instance,
340 n->type, n->type_instance);
341 event->service = strdup (&service_buffer[1]);
343 /* Pull in values from threshold */
344 for (meta = n->meta; meta != NULL; meta = meta->next)
346 if (strcasecmp ("CurrentValue", meta->name) != 0)
349 event->metric_d = meta->nm_value.nm_double;
350 event->has_metric_d = 1;
354 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
355 "host = \"%s\", service = \"%s\", state = \"%s\"",
356 event->host, event->service, event->state);
358 } /* }}} Msg *riemann_notification_to_protobuf */
360 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
361 data_set_t const *ds,
362 value_list_t const *vl, size_t index,
363 gauge_t const *rates)
366 char name_buffer[5 * DATA_MAX_NAME_LEN];
367 char service_buffer[6 * DATA_MAX_NAME_LEN];
370 event = malloc (sizeof (*event));
373 ERROR ("write_riemann plugin: malloc failed.");
376 memset (event, 0, sizeof (*event));
379 event->host = strdup (vl->host);
380 event->time = CDTIME_T_TO_TIME_T (vl->time);
382 event->ttl = CDTIME_T_TO_TIME_T (2 * vl->interval);
385 riemann_event_add_attribute (event, "plugin", vl->plugin);
386 if (vl->plugin_instance[0] != 0)
387 riemann_event_add_attribute (event, "plugin_instance",
388 vl->plugin_instance);
390 riemann_event_add_attribute (event, "type", vl->type);
391 if (vl->type_instance[0] != 0)
392 riemann_event_add_attribute (event, "type_instance",
395 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
397 char ds_type[DATA_MAX_NAME_LEN];
399 ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
400 DS_TYPE_TO_STRING(ds->ds[index].type));
401 riemann_event_add_attribute (event, "ds_type", ds_type);
405 riemann_event_add_attribute (event, "ds_type",
406 DS_TYPE_TO_STRING(ds->ds[index].type));
408 riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
410 char ds_index[DATA_MAX_NAME_LEN];
412 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
413 riemann_event_add_attribute (event, "ds_index", ds_index);
416 for (i = 0; i < riemann_tags_num; i++)
417 riemann_event_add_tag (event, riemann_tags[i]);
419 if (ds->ds[index].type == DS_TYPE_GAUGE)
421 event->has_metric_d = 1;
422 event->metric_d = (double) vl->values[index].gauge;
424 else if (rates != NULL)
426 event->has_metric_d = 1;
427 event->metric_d = (double) rates[index];
431 event->has_metric_sint64 = 1;
432 if (ds->ds[index].type == DS_TYPE_DERIVE)
433 event->metric_sint64 = (int64_t) vl->values[index].derive;
434 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
435 event->metric_sint64 = (int64_t) vl->values[index].absolute;
437 event->metric_sint64 = (int64_t) vl->values[index].counter;
440 format_name (name_buffer, sizeof (name_buffer),
441 /* host = */ "", vl->plugin, vl->plugin_instance,
442 vl->type, vl->type_instance);
443 if (host->always_append_ds || (ds->ds_num > 1))
444 ssnprintf (service_buffer, sizeof (service_buffer),
445 "%s/%s", &name_buffer[1], ds->ds[index].name);
447 sstrncpy (service_buffer, &name_buffer[1],
448 sizeof (service_buffer));
450 event->service = strdup (service_buffer);
452 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
453 "host = \"%s\", service = \"%s\"",
454 event->host, event->service);
456 } /* }}} Event *riemann_value_to_protobuf */
458 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
459 data_set_t const *ds,
460 value_list_t const *vl)
464 gauge_t *rates = NULL;
466 /* Initialize the Msg structure. */
467 msg = malloc (sizeof (*msg));
470 ERROR ("write_riemann plugin: malloc failed.");
473 memset (msg, 0, sizeof (*msg));
476 /* Set up events. First, the list of pointers. */
477 msg->n_events = (size_t) vl->values_len;
478 msg->events = calloc (msg->n_events, sizeof (*msg->events));
479 if (msg->events == NULL)
481 ERROR ("write_riemann plugin: calloc failed.");
482 riemann_msg_protobuf_free (msg);
486 if (host->store_rates)
488 rates = uc_get_rate (ds, vl);
491 ERROR ("write_riemann plugin: uc_get_rate failed.");
492 riemann_msg_protobuf_free (msg);
497 for (i = 0; i < msg->n_events; i++)
499 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
501 if (msg->events[i] == NULL)
503 riemann_msg_protobuf_free (msg);
511 } /* }}} Msg *riemann_value_list_to_protobuf */
514 riemann_notification(const notification_t *n, user_data_t *ud)
517 struct riemann_host *host = ud->data;
520 msg = riemann_notification_to_protobuf (host, n);
524 status = riemann_send (host, msg);
526 ERROR ("write_riemann plugin: riemann_send failed with status %i",
529 riemann_msg_protobuf_free (msg);
531 } /* }}} int riemann_notification */
534 riemann_write(const data_set_t *ds,
535 const value_list_t *vl,
539 struct riemann_host *host = ud->data;
542 msg = riemann_value_list_to_protobuf (host, ds, vl);
546 status = riemann_send (host, msg);
548 ERROR ("write_riemann plugin: riemann_send failed with status %i",
551 riemann_msg_protobuf_free (msg);
556 riemann_free(void *p)
558 struct riemann_host *host = p;
563 pthread_mutex_lock (&host->lock);
565 host->reference_count--;
566 if (host->reference_count > 0)
568 pthread_mutex_unlock (&host->lock);
572 riemann_disconnect (host);
574 sfree(host->service);
575 pthread_mutex_destroy (&host->lock);
580 riemann_config_node(oconfig_item_t *ci)
582 struct riemann_host *host = NULL;
585 oconfig_item_t *child;
586 char callback_name[DATA_MAX_NAME_LEN];
589 if ((host = calloc(1, sizeof (*host))) == NULL) {
590 ERROR ("write_riemann plugin: calloc failed.");
593 pthread_mutex_init (&host->lock, NULL);
594 host->reference_count = 1;
596 host->service = NULL;
597 host->store_rates = 1;
598 host->always_append_ds = 0;
601 status = cf_util_get_string (ci, &host->name);
603 WARNING("write_riemann plugin: Required host name is missing.");
608 for (i = 0; i < ci->children_num; i++) {
610 * The code here could be simplified but makes room
611 * for easy adding of new options later on.
613 child = &ci->children[i];
616 if (strcasecmp ("Host", child->key) == 0) {
617 status = cf_util_get_string (child, &host->node);
620 } else if (strcasecmp ("Port", child->key) == 0) {
621 status = cf_util_get_service (child, &host->service);
623 ERROR ("write_riemann plugin: Invalid argument "
624 "configured for the \"Port\" "
628 } else if (strcasecmp ("Protocol", child->key) == 0) {
630 status = cf_util_get_string_buffer (child,
634 ERROR ("write_riemann plugin: cf_util_get_"
635 "string_buffer failed with "
636 "status %i.", status);
640 if (strcasecmp ("UDP", tmp) == 0)
642 else if (strcasecmp ("TCP", tmp) == 0)
645 WARNING ("write_riemann plugin: The value "
646 "\"%s\" is not valid for the "
647 "\"Protocol\" option. Use "
648 "either \"UDP\" or \"TCP\".",
650 } else if (strcasecmp ("StoreRates", child->key) == 0) {
651 status = cf_util_get_boolean (child, &host->store_rates);
654 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
655 status = cf_util_get_boolean (child,
656 &host->always_append_ds);
660 WARNING("write_riemann plugin: ignoring unknown config "
661 "option: \"%s\"", child->key);
669 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
672 ud.free_func = riemann_free;
674 pthread_mutex_lock (&host->lock);
676 status = plugin_register_write (callback_name, riemann_write, &ud);
678 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
679 "failed with status %i.",
680 callback_name, status);
682 host->reference_count++;
684 status = plugin_register_notification (callback_name,
685 riemann_notification, &ud);
687 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
688 "failed with status %i.",
689 callback_name, status);
691 host->reference_count++;
693 if (host->reference_count <= 1)
695 /* Both callbacks failed => free memory.
696 * We need to unlock here, because riemann_free() will lock.
697 * This is not a race condition, because we're the only one
698 * holding a reference. */
699 pthread_mutex_unlock (&host->lock);
704 host->reference_count--;
705 pthread_mutex_unlock (&host->lock);
711 riemann_config(oconfig_item_t *ci)
714 oconfig_item_t *child;
717 for (i = 0; i < ci->children_num; i++) {
718 child = &ci->children[i];
720 if (strcasecmp("Node", child->key) == 0) {
721 riemann_config_node (child);
722 } else if (strcasecmp(child->key, "tag") == 0) {
724 status = cf_util_get_string(child, &tmp);
728 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
729 DEBUG("write_riemann plugin: Got tag: %s", tmp);
732 WARNING ("write_riemann plugin: Ignoring unknown "
733 "configuration option \"%s\" at top level.",
741 module_register(void)
743 plugin_register_complex_config ("write_riemann", riemann_config);
746 /* vim: set sw=8 sts=8 ts=8 noet : */