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;
55 static char **riemann_tags;
56 static size_t riemann_tags_num;
58 static int riemann_send(struct riemann_host *, Msg const *);
59 static int riemann_notification(const notification_t *, user_data_t *);
60 static int riemann_write(const data_set_t *, const value_list_t *, user_data_t *);
61 static int riemann_connect(struct riemann_host *);
62 static int riemann_disconnect (struct riemann_host *host);
63 static void riemann_free(void *);
64 static int riemann_config_node(oconfig_item_t *);
65 static int riemann_config(oconfig_item_t *);
66 void module_register(void);
68 static void riemann_event_protobuf_free (Event *event) /* {{{ */
74 sfree (event->service);
76 sfree (event->description);
78 strarray_free (event->tags, event->n_tags);
83 } /* }}} void riemann_event_protobuf_free */
85 static void riemann_msg_protobuf_free (Msg *msg) /* {{{ */
92 for (i = 0; i < msg->n_events; i++)
94 riemann_event_protobuf_free (msg->events[i]);
95 msg->events[i] = NULL;
102 } /* }}} void riemann_msg_protobuf_free */
105 riemann_send(struct riemann_host *host, Msg const *msg)
111 pthread_mutex_lock (&host->lock);
113 status = riemann_connect (host);
116 pthread_mutex_unlock (&host->lock);
120 buffer_len = msg__get_packed_size(msg);
121 buffer = malloc (buffer_len);
122 if (buffer == NULL) {
123 pthread_mutex_unlock (&host->lock);
124 ERROR ("write_riemann plugin: malloc failed.");
127 memset (buffer, 0, buffer_len);
129 msg__pack(msg, buffer);
131 status = (int) swrite (host->s, buffer, buffer_len);
136 riemann_disconnect (host);
137 pthread_mutex_unlock (&host->lock);
139 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s",
140 (host->node != NULL) ? host->node : RIEMANN_HOST,
141 (host->service != NULL) ? host->service : RIEMANN_PORT,
142 sstrerror (errno, errbuf, sizeof (errbuf)));
147 pthread_mutex_unlock (&host->lock);
152 static int riemann_event_add_tag (Event *event, /* {{{ */
153 char const *format, ...)
159 va_start (ap, format);
160 ret = vsnprintf (buffer, sizeof (buffer), format, ap);
161 if (ret >= sizeof (buffer))
162 ret = sizeof (buffer) - 1;
166 return (strarray_add (&event->tags, &event->n_tags, buffer));
167 } /* }}} int riemann_event_add_tag */
169 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
170 notification_t const *n)
174 char service_buffer[6 * DATA_MAX_NAME_LEN];
175 char const *severity;
176 notification_meta_t *meta;
179 msg = malloc (sizeof (*msg));
182 ERROR ("write_riemann plugin: malloc failed.");
185 memset (msg, 0, sizeof (*msg));
188 msg->events = malloc (sizeof (*msg->events));
189 if (msg->events == NULL)
191 ERROR ("write_riemann plugin: malloc failed.");
196 event = malloc (sizeof (*event));
199 ERROR ("write_riemann plugin: malloc failed.");
204 memset (event, 0, sizeof (*event));
207 msg->events[0] = event;
210 event->host = strdup (n->host);
211 event->time = CDTIME_T_TO_TIME_T (n->time);
216 case NOTIF_OKAY: severity = "okay"; break;
217 case NOTIF_WARNING: severity = "warning"; break;
218 case NOTIF_FAILURE: severity = "failure"; break;
219 default: severity = "unknown";
221 event->state = strdup (severity);
223 riemann_event_add_tag (event, "notification");
224 if (n->plugin[0] != 0)
225 riemann_event_add_tag (event, "plugin:%s", n->plugin);
226 if (n->plugin_instance[0] != 0)
227 riemann_event_add_tag (event, "plugin_instance:%s",
231 riemann_event_add_tag (event, "type:%s", n->type);
232 if (n->type_instance[0] != 0)
233 riemann_event_add_tag (event, "type_instance:%s",
236 for (i = 0; i < riemann_tags_num; i++)
237 riemann_event_add_tag (event, "%s", riemann_tags[i]);
239 format_name (service_buffer, sizeof (service_buffer),
240 /* host = */ "", n->plugin, n->plugin_instance,
241 n->type, n->type_instance);
242 event->service = strdup (&service_buffer[1]);
244 /* Pull in values from threshold */
245 for (meta = n->meta; meta != NULL; meta = meta->next)
247 if (strcasecmp ("CurrentValue", meta->name) != 0)
250 event->metric_d = meta->nm_value.nm_double;
251 event->has_metric_d = 1;
255 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
256 "host = \"%s\", service = \"%s\", state = \"%s\"",
257 event->host, event->service, event->state);
259 } /* }}} Msg *riemann_notification_to_protobuf */
261 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
262 data_set_t const *ds,
263 value_list_t const *vl, size_t index,
264 gauge_t const *rates)
267 char name_buffer[5 * DATA_MAX_NAME_LEN];
268 char service_buffer[6 * DATA_MAX_NAME_LEN];
271 event = malloc (sizeof (*event));
274 ERROR ("write_riemann plugin: malloc failed.");
277 memset (event, 0, sizeof (*event));
280 event->host = strdup (vl->host);
281 event->time = CDTIME_T_TO_TIME_T (vl->time);
283 event->ttl = CDTIME_T_TO_TIME_T (2 * vl->interval);
286 riemann_event_add_tag (event, "plugin:%s", vl->plugin);
287 if (vl->plugin_instance[0] != 0)
288 riemann_event_add_tag (event, "plugin_instance:%s",
289 vl->plugin_instance);
291 riemann_event_add_tag (event, "type:%s", vl->type);
292 if (vl->type_instance[0] != 0)
293 riemann_event_add_tag (event, "type_instance:%s",
296 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
298 riemann_event_add_tag (event, "ds_type:%s:rate",
299 DS_TYPE_TO_STRING(ds->ds[index].type));
303 riemann_event_add_tag (event, "ds_type:%s",
304 DS_TYPE_TO_STRING(ds->ds[index].type));
306 riemann_event_add_tag (event, "ds_name:%s", ds->ds[index].name);
307 riemann_event_add_tag (event, "ds_index:%zu", index);
309 for (i = 0; i < riemann_tags_num; i++)
310 riemann_event_add_tag (event, "%s", riemann_tags[i]);
312 if (ds->ds[index].type == DS_TYPE_GAUGE)
314 event->has_metric_d = 1;
315 event->metric_d = (double) vl->values[index].gauge;
317 else if (rates != NULL)
319 event->has_metric_d = 1;
320 event->metric_d = (double) rates[index];
324 event->has_metric_sint64 = 1;
325 if (ds->ds[index].type == DS_TYPE_DERIVE)
326 event->metric_sint64 = (int64_t) vl->values[index].derive;
327 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
328 event->metric_sint64 = (int64_t) vl->values[index].absolute;
330 event->metric_sint64 = (int64_t) vl->values[index].counter;
333 format_name (name_buffer, sizeof (name_buffer),
334 /* host = */ "", vl->plugin, vl->plugin_instance,
335 vl->type, vl->type_instance);
336 if (host->always_append_ds || (ds->ds_num > 1))
337 ssnprintf (service_buffer, sizeof (service_buffer),
338 "%s/%s", &name_buffer[1], ds->ds[index].name);
340 sstrncpy (service_buffer, &name_buffer[1],
341 sizeof (service_buffer));
343 event->service = strdup (service_buffer);
345 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
346 "host = \"%s\", service = \"%s\"",
347 event->host, event->service);
349 } /* }}} Event *riemann_value_to_protobuf */
351 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
352 data_set_t const *ds,
353 value_list_t const *vl)
357 gauge_t *rates = NULL;
359 /* Initialize the Msg structure. */
360 msg = malloc (sizeof (*msg));
363 ERROR ("write_riemann plugin: malloc failed.");
366 memset (msg, 0, sizeof (*msg));
369 /* Set up events. First, the list of pointers. */
370 msg->n_events = (size_t) vl->values_len;
371 msg->events = calloc (msg->n_events, sizeof (*msg->events));
372 if (msg->events == NULL)
374 ERROR ("write_riemann plugin: calloc failed.");
375 riemann_msg_protobuf_free (msg);
379 if (host->store_rates)
381 rates = uc_get_rate (ds, vl);
384 ERROR ("write_riemann plugin: uc_get_rate failed.");
385 riemann_msg_protobuf_free (msg);
390 for (i = 0; i < msg->n_events; i++)
392 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
394 if (msg->events[i] == NULL)
396 riemann_msg_protobuf_free (msg);
404 } /* }}} Msg *riemann_value_list_to_protobuf */
407 riemann_notification(const notification_t *n, user_data_t *ud)
410 struct riemann_host *host = ud->data;
413 msg = riemann_notification_to_protobuf (host, n);
417 status = riemann_send (host, msg);
419 ERROR ("write_riemann plugin: riemann_send failed with status %i",
422 riemann_msg_protobuf_free (msg);
424 } /* }}} int riemann_notification */
427 riemann_write(const data_set_t *ds,
428 const value_list_t *vl,
432 struct riemann_host *host = ud->data;
435 msg = riemann_value_list_to_protobuf (host, ds, vl);
439 status = riemann_send (host, msg);
441 ERROR ("write_riemann plugin: riemann_send failed with status %i",
444 riemann_msg_protobuf_free (msg);
448 /* host->lock must be held when calling this function. */
450 riemann_connect(struct riemann_host *host)
453 struct addrinfo *ai, *res, hints;
457 if (host->flags & F_CONNECT)
460 memset(&hints, 0, sizeof(hints));
461 memset(&service, 0, sizeof(service));
462 hints.ai_family = AF_UNSPEC;
463 hints.ai_socktype = SOCK_DGRAM;
465 hints.ai_flags |= AI_ADDRCONFIG;
468 node = (host->node != NULL) ? host->node : RIEMANN_HOST;
469 service = (host->service != NULL) ? host->service : RIEMANN_PORT;
471 if ((e = getaddrinfo(node, service, &hints, &res)) != 0) {
472 ERROR ("write_riemann plugin: Unable to resolve host \"%s\": %s",
473 node, gai_strerror(e));
478 for (ai = res; ai != NULL; ai = ai->ai_next) {
479 if ((host->s = socket(ai->ai_family,
481 ai->ai_protocol)) == -1) {
485 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
491 host->flags |= F_CONNECT;
492 DEBUG("write_riemann plugin: got a succesful connection for: %s:%s",
500 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
507 /* host->lock must be held when calling this function. */
509 riemann_disconnect (struct riemann_host *host)
511 if ((host->flags & F_CONNECT) == 0)
516 host->flags &= ~F_CONNECT;
522 riemann_free(void *p)
524 struct riemann_host *host = p;
529 pthread_mutex_lock (&host->lock);
531 host->reference_count--;
532 if (host->reference_count > 0)
534 pthread_mutex_unlock (&host->lock);
538 riemann_disconnect (host);
540 sfree(host->service);
541 pthread_mutex_destroy (&host->lock);
546 riemann_config_node(oconfig_item_t *ci)
548 struct riemann_host *host = NULL;
551 oconfig_item_t *child;
552 char callback_name[DATA_MAX_NAME_LEN];
555 if ((host = calloc(1, sizeof (*host))) == NULL) {
556 ERROR ("write_riemann plugin: calloc failed.");
559 pthread_mutex_init (&host->lock, NULL);
560 host->reference_count = 1;
562 host->service = NULL;
563 host->store_rates = 1;
564 host->always_append_ds = 0;
566 status = cf_util_get_string (ci, &host->name);
568 WARNING("write_riemann plugin: Required host name is missing.");
573 for (i = 0; i < ci->children_num; i++) {
575 * The code here could be simplified but makes room
576 * for easy adding of new options later on.
578 child = &ci->children[i];
581 if (strcasecmp ("Host", child->key) == 0) {
582 status = cf_util_get_string (child, &host->node);
585 } else if (strcasecmp ("Port", child->key) == 0) {
586 status = cf_util_get_service (child, &host->service);
588 ERROR ("write_riemann plugin: Invalid argument "
589 "configured for the \"Port\" "
593 } else if (strcasecmp ("StoreRates", child->key) == 0) {
594 status = cf_util_get_boolean (child, &host->store_rates);
597 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
598 status = cf_util_get_boolean (child,
599 &host->always_append_ds);
603 WARNING("write_riemann plugin: ignoring unknown config "
604 "option: \"%s\"", child->key);
612 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
615 ud.free_func = riemann_free;
617 pthread_mutex_lock (&host->lock);
619 status = plugin_register_write (callback_name, riemann_write, &ud);
621 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
622 "failed with status %i.",
623 callback_name, status);
625 host->reference_count++;
627 status = plugin_register_notification (callback_name,
628 riemann_notification, &ud);
630 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
631 "failed with status %i.",
632 callback_name, status);
634 host->reference_count++;
636 if (host->reference_count <= 1)
638 /* Both callbacks failed => free memory.
639 * We need to unlock here, because riemann_free() will lock.
640 * This is not a race condition, because we're the only one
641 * holding a reference. */
642 pthread_mutex_unlock (&host->lock);
647 host->reference_count--;
648 pthread_mutex_unlock (&host->lock);
654 riemann_config(oconfig_item_t *ci)
657 oconfig_item_t *child;
660 for (i = 0; i < ci->children_num; i++) {
661 child = &ci->children[i];
663 if (strcasecmp("Node", child->key) == 0) {
664 riemann_config_node (child);
665 } else if (strcasecmp(child->key, "tag") == 0) {
667 status = cf_util_get_string(child, &tmp);
671 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
672 DEBUG("write_riemann plugin: Got tag: %s", tmp);
675 WARNING ("write_riemann plugin: Ignoring unknown "
676 "configuration option \"%s\" at top level.",
684 module_register(void)
686 plugin_register_complex_config ("write_riemann", riemann_config);
689 /* vim: set sw=8 sts=8 ts=8 noet : */