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 int riemann_send(struct riemann_host *, Msg const *);
60 static int riemann_notification(const notification_t *, user_data_t *);
61 static int riemann_write(const data_set_t *, const value_list_t *, user_data_t *);
62 static int riemann_connect(struct riemann_host *);
63 static int riemann_disconnect (struct riemann_host *host);
64 static void riemann_free(void *);
65 static int riemann_config_node(oconfig_item_t *);
66 static int riemann_config(oconfig_item_t *);
67 void module_register(void);
69 static void riemann_event_protobuf_free (Event *event) /* {{{ */
75 sfree (event->service);
77 sfree (event->description);
79 strarray_free (event->tags, event->n_tags);
84 } /* }}} void riemann_event_protobuf_free */
86 static void riemann_msg_protobuf_free (Msg *msg) /* {{{ */
93 for (i = 0; i < msg->n_events; i++)
95 riemann_event_protobuf_free (msg->events[i]);
96 msg->events[i] = NULL;
103 } /* }}} void riemann_msg_protobuf_free */
106 riemann_send(struct riemann_host *host, Msg const *msg)
112 pthread_mutex_lock (&host->lock);
114 status = riemann_connect (host);
117 pthread_mutex_unlock (&host->lock);
121 buffer_len = msg__get_packed_size(msg);
125 buffer = malloc (buffer_len);
126 if (buffer == NULL) {
127 pthread_mutex_unlock (&host->lock);
128 ERROR ("write_riemann plugin: malloc failed.");
131 memset (buffer, 0, buffer_len);
135 uint32_t length = htonl ((uint32_t) (buffer_len - 4));
136 memcpy (buffer, &length, 4);
137 msg__pack(msg, buffer + 4);
141 msg__pack(msg, buffer);
144 status = (int) swrite (host->s, buffer, buffer_len);
149 riemann_disconnect (host);
150 pthread_mutex_unlock (&host->lock);
152 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s",
153 (host->node != NULL) ? host->node : RIEMANN_HOST,
154 (host->service != NULL) ? host->service : RIEMANN_PORT,
155 sstrerror (errno, errbuf, sizeof (errbuf)));
160 pthread_mutex_unlock (&host->lock);
165 static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
167 return (strarray_add (&event->tags, &event->n_tags, tag));
168 } /* }}} int riemann_event_add_tag */
170 static int riemann_event_add_attribute (Event *event, /* {{{ */
171 char const *key, char const *value)
173 Attribute **new_attributes;
176 new_attributes = realloc (event->attributes,
177 sizeof (*event->attributes) * (event->n_attributes + 1));
178 if (new_attributes == NULL)
180 ERROR ("write_riemann plugin: realloc failed.");
183 event->attributes = new_attributes;
185 a = malloc (sizeof (*a));
188 ERROR ("write_riemann plugin: malloc failed.");
193 a->key = strdup (key);
195 a->value = strdup (value);
197 event->attributes[event->n_attributes] = a;
198 event->n_attributes++;
201 } /* }}} int riemann_event_add_attribute */
203 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
204 notification_t const *n)
208 char service_buffer[6 * DATA_MAX_NAME_LEN];
209 char const *severity;
210 notification_meta_t *meta;
213 msg = malloc (sizeof (*msg));
216 ERROR ("write_riemann plugin: malloc failed.");
219 memset (msg, 0, sizeof (*msg));
222 msg->events = malloc (sizeof (*msg->events));
223 if (msg->events == NULL)
225 ERROR ("write_riemann plugin: malloc failed.");
230 event = malloc (sizeof (*event));
233 ERROR ("write_riemann plugin: malloc failed.");
238 memset (event, 0, sizeof (*event));
241 msg->events[0] = event;
244 event->host = strdup (n->host);
245 event->time = CDTIME_T_TO_TIME_T (n->time);
250 case NOTIF_OKAY: severity = "ok"; break;
251 case NOTIF_WARNING: severity = "warning"; break;
252 case NOTIF_FAILURE: severity = "critical"; break;
253 default: severity = "unknown";
255 event->state = strdup (severity);
257 riemann_event_add_tag (event, "notification");
259 riemann_event_add_attribute (event, "host", n->host);
260 if (n->plugin[0] != 0)
261 riemann_event_add_attribute (event, "plugin", n->plugin);
262 if (n->plugin_instance[0] != 0)
263 riemann_event_add_attribute (event, "plugin_instance",
267 riemann_event_add_attribute (event, "type", n->type);
268 if (n->type_instance[0] != 0)
269 riemann_event_add_attribute (event, "type_instance",
272 for (i = 0; i < riemann_tags_num; i++)
273 riemann_event_add_tag (event, riemann_tags[i]);
275 format_name (service_buffer, sizeof (service_buffer),
276 /* host = */ "", n->plugin, n->plugin_instance,
277 n->type, n->type_instance);
278 event->service = strdup (&service_buffer[1]);
280 /* Pull in values from threshold */
281 for (meta = n->meta; meta != NULL; meta = meta->next)
283 if (strcasecmp ("CurrentValue", meta->name) != 0)
286 event->metric_d = meta->nm_value.nm_double;
287 event->has_metric_d = 1;
291 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
292 "host = \"%s\", service = \"%s\", state = \"%s\"",
293 event->host, event->service, event->state);
295 } /* }}} Msg *riemann_notification_to_protobuf */
297 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
298 data_set_t const *ds,
299 value_list_t const *vl, size_t index,
300 gauge_t const *rates)
303 char name_buffer[5 * DATA_MAX_NAME_LEN];
304 char service_buffer[6 * DATA_MAX_NAME_LEN];
307 event = malloc (sizeof (*event));
310 ERROR ("write_riemann plugin: malloc failed.");
313 memset (event, 0, sizeof (*event));
316 event->host = strdup (vl->host);
317 event->time = CDTIME_T_TO_TIME_T (vl->time);
319 event->ttl = CDTIME_T_TO_TIME_T (2 * vl->interval);
322 riemann_event_add_attribute (event, "plugin", vl->plugin);
323 if (vl->plugin_instance[0] != 0)
324 riemann_event_add_attribute (event, "plugin_instance",
325 vl->plugin_instance);
327 riemann_event_add_attribute (event, "type", vl->type);
328 if (vl->type_instance[0] != 0)
329 riemann_event_add_attribute (event, "type_instance",
332 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
334 char ds_type[DATA_MAX_NAME_LEN];
336 ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
337 DS_TYPE_TO_STRING(ds->ds[index].type));
338 riemann_event_add_attribute (event, "ds_type", ds_type);
342 riemann_event_add_attribute (event, "ds_type",
343 DS_TYPE_TO_STRING(ds->ds[index].type));
345 riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
347 char ds_index[DATA_MAX_NAME_LEN];
349 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
350 riemann_event_add_attribute (event, "ds_index", ds_index);
353 for (i = 0; i < riemann_tags_num; i++)
354 riemann_event_add_tag (event, riemann_tags[i]);
356 if (ds->ds[index].type == DS_TYPE_GAUGE)
358 event->has_metric_d = 1;
359 event->metric_d = (double) vl->values[index].gauge;
361 else if (rates != NULL)
363 event->has_metric_d = 1;
364 event->metric_d = (double) rates[index];
368 event->has_metric_sint64 = 1;
369 if (ds->ds[index].type == DS_TYPE_DERIVE)
370 event->metric_sint64 = (int64_t) vl->values[index].derive;
371 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
372 event->metric_sint64 = (int64_t) vl->values[index].absolute;
374 event->metric_sint64 = (int64_t) vl->values[index].counter;
377 format_name (name_buffer, sizeof (name_buffer),
378 /* host = */ "", vl->plugin, vl->plugin_instance,
379 vl->type, vl->type_instance);
380 if (host->always_append_ds || (ds->ds_num > 1))
381 ssnprintf (service_buffer, sizeof (service_buffer),
382 "%s/%s", &name_buffer[1], ds->ds[index].name);
384 sstrncpy (service_buffer, &name_buffer[1],
385 sizeof (service_buffer));
387 event->service = strdup (service_buffer);
389 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
390 "host = \"%s\", service = \"%s\"",
391 event->host, event->service);
393 } /* }}} Event *riemann_value_to_protobuf */
395 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
396 data_set_t const *ds,
397 value_list_t const *vl)
401 gauge_t *rates = NULL;
403 /* Initialize the Msg structure. */
404 msg = malloc (sizeof (*msg));
407 ERROR ("write_riemann plugin: malloc failed.");
410 memset (msg, 0, sizeof (*msg));
413 /* Set up events. First, the list of pointers. */
414 msg->n_events = (size_t) vl->values_len;
415 msg->events = calloc (msg->n_events, sizeof (*msg->events));
416 if (msg->events == NULL)
418 ERROR ("write_riemann plugin: calloc failed.");
419 riemann_msg_protobuf_free (msg);
423 if (host->store_rates)
425 rates = uc_get_rate (ds, vl);
428 ERROR ("write_riemann plugin: uc_get_rate failed.");
429 riemann_msg_protobuf_free (msg);
434 for (i = 0; i < msg->n_events; i++)
436 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
438 if (msg->events[i] == NULL)
440 riemann_msg_protobuf_free (msg);
448 } /* }}} Msg *riemann_value_list_to_protobuf */
451 riemann_notification(const notification_t *n, user_data_t *ud)
454 struct riemann_host *host = ud->data;
457 msg = riemann_notification_to_protobuf (host, n);
461 status = riemann_send (host, msg);
463 ERROR ("write_riemann plugin: riemann_send failed with status %i",
466 riemann_msg_protobuf_free (msg);
468 } /* }}} int riemann_notification */
471 riemann_write(const data_set_t *ds,
472 const value_list_t *vl,
476 struct riemann_host *host = ud->data;
479 msg = riemann_value_list_to_protobuf (host, ds, vl);
483 status = riemann_send (host, msg);
485 ERROR ("write_riemann plugin: riemann_send failed with status %i",
488 riemann_msg_protobuf_free (msg);
492 /* host->lock must be held when calling this function. */
494 riemann_connect(struct riemann_host *host)
497 struct addrinfo *ai, *res, hints;
501 if (host->flags & F_CONNECT)
504 memset(&hints, 0, sizeof(hints));
505 memset(&service, 0, sizeof(service));
506 hints.ai_family = AF_UNSPEC;
507 hints.ai_socktype = host->use_tcp ? SOCK_STREAM : SOCK_DGRAM;
509 hints.ai_flags |= AI_ADDRCONFIG;
512 node = (host->node != NULL) ? host->node : RIEMANN_HOST;
513 service = (host->service != NULL) ? host->service : RIEMANN_PORT;
515 if ((e = getaddrinfo(node, service, &hints, &res)) != 0) {
516 ERROR ("write_riemann plugin: Unable to resolve host \"%s\": %s",
517 node, gai_strerror(e));
522 for (ai = res; ai != NULL; ai = ai->ai_next) {
523 if ((host->s = socket(ai->ai_family,
525 ai->ai_protocol)) == -1) {
529 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
535 host->flags |= F_CONNECT;
536 DEBUG("write_riemann plugin: got a succesful connection for: %s:%s",
544 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
551 /* host->lock must be held when calling this function. */
553 riemann_disconnect (struct riemann_host *host)
555 if ((host->flags & F_CONNECT) == 0)
560 host->flags &= ~F_CONNECT;
566 riemann_free(void *p)
568 struct riemann_host *host = p;
573 pthread_mutex_lock (&host->lock);
575 host->reference_count--;
576 if (host->reference_count > 0)
578 pthread_mutex_unlock (&host->lock);
582 riemann_disconnect (host);
584 sfree(host->service);
585 pthread_mutex_destroy (&host->lock);
590 riemann_config_node(oconfig_item_t *ci)
592 struct riemann_host *host = NULL;
595 oconfig_item_t *child;
596 char callback_name[DATA_MAX_NAME_LEN];
599 if ((host = calloc(1, sizeof (*host))) == NULL) {
600 ERROR ("write_riemann plugin: calloc failed.");
603 pthread_mutex_init (&host->lock, NULL);
604 host->reference_count = 1;
606 host->service = NULL;
607 host->store_rates = 1;
608 host->always_append_ds = 0;
611 status = cf_util_get_string (ci, &host->name);
613 WARNING("write_riemann plugin: Required host name is missing.");
618 for (i = 0; i < ci->children_num; i++) {
620 * The code here could be simplified but makes room
621 * for easy adding of new options later on.
623 child = &ci->children[i];
626 if (strcasecmp ("Host", child->key) == 0) {
627 status = cf_util_get_string (child, &host->node);
630 } else if (strcasecmp ("Port", child->key) == 0) {
631 status = cf_util_get_service (child, &host->service);
633 ERROR ("write_riemann plugin: Invalid argument "
634 "configured for the \"Port\" "
638 } else if (strcasecmp ("Protocol", child->key) == 0) {
640 status = cf_util_get_string_buffer (child,
644 ERROR ("write_riemann plugin: cf_util_get_"
645 "string_buffer failed with "
646 "status %i.", status);
650 if (strcasecmp ("UDP", tmp) == 0)
652 else if (strcasecmp ("TCP", tmp) == 0)
655 WARNING ("write_riemann plugin: The value "
656 "\"%s\" is not valid for the "
657 "\"Protocol\" option. Use "
658 "either \"UDP\" or \"TCP\".",
660 } else if (strcasecmp ("StoreRates", child->key) == 0) {
661 status = cf_util_get_boolean (child, &host->store_rates);
664 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
665 status = cf_util_get_boolean (child,
666 &host->always_append_ds);
670 WARNING("write_riemann plugin: ignoring unknown config "
671 "option: \"%s\"", child->key);
679 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
682 ud.free_func = riemann_free;
684 pthread_mutex_lock (&host->lock);
686 status = plugin_register_write (callback_name, riemann_write, &ud);
688 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
689 "failed with status %i.",
690 callback_name, status);
692 host->reference_count++;
694 status = plugin_register_notification (callback_name,
695 riemann_notification, &ud);
697 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
698 "failed with status %i.",
699 callback_name, status);
701 host->reference_count++;
703 if (host->reference_count <= 1)
705 /* Both callbacks failed => free memory.
706 * We need to unlock here, because riemann_free() will lock.
707 * This is not a race condition, because we're the only one
708 * holding a reference. */
709 pthread_mutex_unlock (&host->lock);
714 host->reference_count--;
715 pthread_mutex_unlock (&host->lock);
721 riemann_config(oconfig_item_t *ci)
724 oconfig_item_t *child;
727 for (i = 0; i < ci->children_num; i++) {
728 child = &ci->children[i];
730 if (strcasecmp("Node", child->key) == 0) {
731 riemann_config_node (child);
732 } else if (strcasecmp(child->key, "tag") == 0) {
734 status = cf_util_get_string(child, &tmp);
738 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
739 DEBUG("write_riemann plugin: Got tag: %s", tmp);
742 WARNING ("write_riemann plugin: Ignoring unknown "
743 "configuration option \"%s\" at top level.",
751 module_register(void)
753 plugin_register_complex_config ("write_riemann", riemann_config);
756 /* vim: set sw=8 sts=8 ts=8 noet : */