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, /* {{{ */
166 char const *format, ...)
172 va_start (ap, format);
173 ret = vsnprintf (buffer, sizeof (buffer), format, ap);
174 if (ret >= sizeof (buffer))
175 ret = sizeof (buffer) - 1;
179 return (strarray_add (&event->tags, &event->n_tags, buffer));
180 } /* }}} int riemann_event_add_tag */
182 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
183 notification_t const *n)
187 char service_buffer[6 * DATA_MAX_NAME_LEN];
188 char const *severity;
189 notification_meta_t *meta;
192 msg = malloc (sizeof (*msg));
195 ERROR ("write_riemann plugin: malloc failed.");
198 memset (msg, 0, sizeof (*msg));
201 msg->events = malloc (sizeof (*msg->events));
202 if (msg->events == NULL)
204 ERROR ("write_riemann plugin: malloc failed.");
209 event = malloc (sizeof (*event));
212 ERROR ("write_riemann plugin: malloc failed.");
217 memset (event, 0, sizeof (*event));
220 msg->events[0] = event;
223 event->host = strdup (n->host);
224 event->time = CDTIME_T_TO_TIME_T (n->time);
229 case NOTIF_OKAY: severity = "ok"; break;
230 case NOTIF_WARNING: severity = "warning"; break;
231 case NOTIF_FAILURE: severity = "critical"; break;
232 default: severity = "unknown";
234 event->state = strdup (severity);
236 riemann_event_add_tag (event, "notification");
237 if (n->plugin[0] != 0)
238 riemann_event_add_tag (event, "plugin:%s", n->plugin);
239 if (n->plugin_instance[0] != 0)
240 riemann_event_add_tag (event, "plugin_instance:%s",
244 riemann_event_add_tag (event, "type:%s", n->type);
245 if (n->type_instance[0] != 0)
246 riemann_event_add_tag (event, "type_instance:%s",
249 for (i = 0; i < riemann_tags_num; i++)
250 riemann_event_add_tag (event, "%s", riemann_tags[i]);
252 format_name (service_buffer, sizeof (service_buffer),
253 /* host = */ "", n->plugin, n->plugin_instance,
254 n->type, n->type_instance);
255 event->service = strdup (&service_buffer[1]);
257 /* Pull in values from threshold */
258 for (meta = n->meta; meta != NULL; meta = meta->next)
260 if (strcasecmp ("CurrentValue", meta->name) != 0)
263 event->metric_d = meta->nm_value.nm_double;
264 event->has_metric_d = 1;
268 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
269 "host = \"%s\", service = \"%s\", state = \"%s\"",
270 event->host, event->service, event->state);
272 } /* }}} Msg *riemann_notification_to_protobuf */
274 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
275 data_set_t const *ds,
276 value_list_t const *vl, size_t index,
277 gauge_t const *rates)
280 char name_buffer[5 * DATA_MAX_NAME_LEN];
281 char service_buffer[6 * DATA_MAX_NAME_LEN];
284 event = malloc (sizeof (*event));
287 ERROR ("write_riemann plugin: malloc failed.");
290 memset (event, 0, sizeof (*event));
293 event->host = strdup (vl->host);
294 event->time = CDTIME_T_TO_TIME_T (vl->time);
296 event->ttl = CDTIME_T_TO_TIME_T (2 * vl->interval);
299 riemann_event_add_tag (event, "plugin:%s", vl->plugin);
300 if (vl->plugin_instance[0] != 0)
301 riemann_event_add_tag (event, "plugin_instance:%s",
302 vl->plugin_instance);
304 riemann_event_add_tag (event, "type:%s", vl->type);
305 if (vl->type_instance[0] != 0)
306 riemann_event_add_tag (event, "type_instance:%s",
309 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
311 riemann_event_add_tag (event, "ds_type:%s:rate",
312 DS_TYPE_TO_STRING(ds->ds[index].type));
316 riemann_event_add_tag (event, "ds_type:%s",
317 DS_TYPE_TO_STRING(ds->ds[index].type));
319 riemann_event_add_tag (event, "ds_name:%s", ds->ds[index].name);
320 riemann_event_add_tag (event, "ds_index:%zu", index);
322 for (i = 0; i < riemann_tags_num; i++)
323 riemann_event_add_tag (event, "%s", riemann_tags[i]);
325 if (ds->ds[index].type == DS_TYPE_GAUGE)
327 event->has_metric_d = 1;
328 event->metric_d = (double) vl->values[index].gauge;
330 else if (rates != NULL)
332 event->has_metric_d = 1;
333 event->metric_d = (double) rates[index];
337 event->has_metric_sint64 = 1;
338 if (ds->ds[index].type == DS_TYPE_DERIVE)
339 event->metric_sint64 = (int64_t) vl->values[index].derive;
340 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
341 event->metric_sint64 = (int64_t) vl->values[index].absolute;
343 event->metric_sint64 = (int64_t) vl->values[index].counter;
346 format_name (name_buffer, sizeof (name_buffer),
347 /* host = */ "", vl->plugin, vl->plugin_instance,
348 vl->type, vl->type_instance);
349 if (host->always_append_ds || (ds->ds_num > 1))
350 ssnprintf (service_buffer, sizeof (service_buffer),
351 "%s/%s", &name_buffer[1], ds->ds[index].name);
353 sstrncpy (service_buffer, &name_buffer[1],
354 sizeof (service_buffer));
356 event->service = strdup (service_buffer);
358 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
359 "host = \"%s\", service = \"%s\"",
360 event->host, event->service);
362 } /* }}} Event *riemann_value_to_protobuf */
364 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
365 data_set_t const *ds,
366 value_list_t const *vl)
370 gauge_t *rates = NULL;
372 /* Initialize the Msg structure. */
373 msg = malloc (sizeof (*msg));
376 ERROR ("write_riemann plugin: malloc failed.");
379 memset (msg, 0, sizeof (*msg));
382 /* Set up events. First, the list of pointers. */
383 msg->n_events = (size_t) vl->values_len;
384 msg->events = calloc (msg->n_events, sizeof (*msg->events));
385 if (msg->events == NULL)
387 ERROR ("write_riemann plugin: calloc failed.");
388 riemann_msg_protobuf_free (msg);
392 if (host->store_rates)
394 rates = uc_get_rate (ds, vl);
397 ERROR ("write_riemann plugin: uc_get_rate failed.");
398 riemann_msg_protobuf_free (msg);
403 for (i = 0; i < msg->n_events; i++)
405 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
407 if (msg->events[i] == NULL)
409 riemann_msg_protobuf_free (msg);
417 } /* }}} Msg *riemann_value_list_to_protobuf */
420 riemann_notification(const notification_t *n, user_data_t *ud)
423 struct riemann_host *host = ud->data;
426 msg = riemann_notification_to_protobuf (host, n);
430 status = riemann_send (host, msg);
432 ERROR ("write_riemann plugin: riemann_send failed with status %i",
435 riemann_msg_protobuf_free (msg);
437 } /* }}} int riemann_notification */
440 riemann_write(const data_set_t *ds,
441 const value_list_t *vl,
445 struct riemann_host *host = ud->data;
448 msg = riemann_value_list_to_protobuf (host, ds, vl);
452 status = riemann_send (host, msg);
454 ERROR ("write_riemann plugin: riemann_send failed with status %i",
457 riemann_msg_protobuf_free (msg);
461 /* host->lock must be held when calling this function. */
463 riemann_connect(struct riemann_host *host)
466 struct addrinfo *ai, *res, hints;
470 if (host->flags & F_CONNECT)
473 memset(&hints, 0, sizeof(hints));
474 memset(&service, 0, sizeof(service));
475 hints.ai_family = AF_UNSPEC;
476 hints.ai_socktype = host->use_tcp ? SOCK_STREAM : SOCK_DGRAM;
478 hints.ai_flags |= AI_ADDRCONFIG;
481 node = (host->node != NULL) ? host->node : RIEMANN_HOST;
482 service = (host->service != NULL) ? host->service : RIEMANN_PORT;
484 if ((e = getaddrinfo(node, service, &hints, &res)) != 0) {
485 ERROR ("write_riemann plugin: Unable to resolve host \"%s\": %s",
486 node, gai_strerror(e));
491 for (ai = res; ai != NULL; ai = ai->ai_next) {
492 if ((host->s = socket(ai->ai_family,
494 ai->ai_protocol)) == -1) {
498 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
504 host->flags |= F_CONNECT;
505 DEBUG("write_riemann plugin: got a succesful connection for: %s:%s",
513 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
520 /* host->lock must be held when calling this function. */
522 riemann_disconnect (struct riemann_host *host)
524 if ((host->flags & F_CONNECT) == 0)
529 host->flags &= ~F_CONNECT;
535 riemann_free(void *p)
537 struct riemann_host *host = p;
542 pthread_mutex_lock (&host->lock);
544 host->reference_count--;
545 if (host->reference_count > 0)
547 pthread_mutex_unlock (&host->lock);
551 riemann_disconnect (host);
553 sfree(host->service);
554 pthread_mutex_destroy (&host->lock);
559 riemann_config_node(oconfig_item_t *ci)
561 struct riemann_host *host = NULL;
564 oconfig_item_t *child;
565 char callback_name[DATA_MAX_NAME_LEN];
568 if ((host = calloc(1, sizeof (*host))) == NULL) {
569 ERROR ("write_riemann plugin: calloc failed.");
572 pthread_mutex_init (&host->lock, NULL);
573 host->reference_count = 1;
575 host->service = NULL;
576 host->store_rates = 1;
577 host->always_append_ds = 0;
580 status = cf_util_get_string (ci, &host->name);
582 WARNING("write_riemann plugin: Required host name is missing.");
587 for (i = 0; i < ci->children_num; i++) {
589 * The code here could be simplified but makes room
590 * for easy adding of new options later on.
592 child = &ci->children[i];
595 if (strcasecmp ("Host", child->key) == 0) {
596 status = cf_util_get_string (child, &host->node);
599 } else if (strcasecmp ("Port", child->key) == 0) {
600 status = cf_util_get_service (child, &host->service);
602 ERROR ("write_riemann plugin: Invalid argument "
603 "configured for the \"Port\" "
607 } else if (strcasecmp ("Protocol", child->key) == 0) {
609 status = cf_util_get_string_buffer (child,
613 ERROR ("write_riemann plugin: cf_util_get_"
614 "string_buffer failed with "
615 "status %i.", status);
619 if (strcasecmp ("UDP", tmp) == 0)
621 else if (strcasecmp ("TCP", tmp) == 0)
624 WARNING ("write_riemann plugin: The value "
625 "\"%s\" is not valid for the "
626 "\"Protocol\" option. Use "
627 "either \"UDP\" or \"TCP\".",
629 } else if (strcasecmp ("StoreRates", child->key) == 0) {
630 status = cf_util_get_boolean (child, &host->store_rates);
633 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
634 status = cf_util_get_boolean (child,
635 &host->always_append_ds);
639 WARNING("write_riemann plugin: ignoring unknown config "
640 "option: \"%s\"", child->key);
648 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
651 ud.free_func = riemann_free;
653 pthread_mutex_lock (&host->lock);
655 status = plugin_register_write (callback_name, riemann_write, &ud);
657 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
658 "failed with status %i.",
659 callback_name, status);
661 host->reference_count++;
663 status = plugin_register_notification (callback_name,
664 riemann_notification, &ud);
666 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
667 "failed with status %i.",
668 callback_name, status);
670 host->reference_count++;
672 if (host->reference_count <= 1)
674 /* Both callbacks failed => free memory.
675 * We need to unlock here, because riemann_free() will lock.
676 * This is not a race condition, because we're the only one
677 * holding a reference. */
678 pthread_mutex_unlock (&host->lock);
683 host->reference_count--;
684 pthread_mutex_unlock (&host->lock);
690 riemann_config(oconfig_item_t *ci)
693 oconfig_item_t *child;
696 for (i = 0; i < ci->children_num; i++) {
697 child = &ci->children[i];
699 if (strcasecmp("Node", child->key) == 0) {
700 riemann_config_node (child);
701 } else if (strcasecmp(child->key, "tag") == 0) {
703 status = cf_util_get_string(child, &tmp);
707 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
708 DEBUG("write_riemann plugin: Got tag: %s", tmp);
711 WARNING ("write_riemann plugin: Ignoring unknown "
712 "configuration option \"%s\" at top level.",
720 module_register(void)
722 plugin_register_complex_config ("write_riemann", riemann_config);
725 /* vim: set sw=8 sts=8 ts=8 noet : */