2 * collectd - src/write_riemann.c
4 * Copyright (C) 2012 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #include "configfile.h"
24 #include "utils_cache.h"
25 #include "riemann.pb-c.h"
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
34 #define RIEMANN_DELAY 1
35 #define RIEMANN_HOST "localhost"
36 #define RIEMANN_PORT "5555"
40 #define F_CONNECT 0x01
52 static char **riemann_tags;
53 static size_t riemann_tags_num;
55 static int riemann_send(struct riemann_host *, Msg const *);
56 static int riemann_notification(const notification_t *, user_data_t *);
57 static int riemann_write(const data_set_t *, const value_list_t *, user_data_t *);
58 static int riemann_connect(struct riemann_host *);
59 static int riemann_disconnect (struct riemann_host *host);
60 static void riemann_free(void *);
61 static int riemann_config_node(oconfig_item_t *);
62 static int riemann_config(oconfig_item_t *);
63 void module_register(void);
65 static void riemann_event_protobuf_free (Event *event) /* {{{ */
71 sfree (event->service);
73 sfree (event->description);
75 strarray_free (event->tags, event->n_tags);
80 } /* }}} void riemann_event_protobuf_free */
82 static void riemann_msg_protobuf_free (Msg *msg) /* {{{ */
89 for (i = 0; i < msg->n_events; i++)
91 riemann_event_protobuf_free (msg->events[i]);
92 msg->events[i] = NULL;
99 } /* }}} void riemann_msg_protobuf_free */
102 riemann_send(struct riemann_host *host, Msg const *msg)
108 pthread_mutex_lock (&host->lock);
110 status = riemann_connect (host);
113 pthread_mutex_unlock (&host->lock);
117 buffer_len = msg__get_packed_size(msg);
118 buffer = malloc (buffer_len);
119 if (buffer == NULL) {
120 pthread_mutex_unlock (&host->lock);
121 ERROR ("write_riemann plugin: malloc failed.");
124 memset (buffer, 0, buffer_len);
126 msg__pack(msg, buffer);
128 status = (int) swrite (host->s, buffer, buffer_len);
133 riemann_disconnect (host);
134 pthread_mutex_unlock (&host->lock);
136 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s",
137 (host->node != NULL) ? host->node : RIEMANN_HOST,
138 (host->service != NULL) ? host->service : RIEMANN_PORT,
139 sstrerror (errno, errbuf, sizeof (errbuf)));
144 pthread_mutex_unlock (&host->lock);
149 static int riemann_event_add_tag (Event *event, /* {{{ */
150 char const *format, ...)
156 va_start (ap, format);
157 ret = vsnprintf (buffer, sizeof (buffer), format, ap);
158 if (ret >= sizeof (buffer))
159 ret = sizeof (buffer) - 1;
163 return (strarray_add (&event->tags, &event->n_tags, buffer));
164 } /* }}} int riemann_event_add_tag */
166 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
167 notification_t const *n)
171 char service_buffer[6 * DATA_MAX_NAME_LEN];
172 char const *severity;
173 notification_meta_t *meta;
176 msg = malloc (sizeof (*msg));
179 ERROR ("write_riemann plugin: malloc failed.");
182 memset (msg, 0, sizeof (*msg));
185 msg->events = malloc (sizeof (*msg->events));
186 if (msg->events == NULL)
188 ERROR ("write_riemann plugin: malloc failed.");
193 event = malloc (sizeof (*event));
196 ERROR ("write_riemann plugin: malloc failed.");
201 memset (event, 0, sizeof (*event));
204 msg->events[0] = event;
207 event->host = strdup (n->host);
208 event->time = CDTIME_T_TO_TIME_T (n->time);
213 case NOTIF_OKAY: severity = "okay"; break;
214 case NOTIF_WARNING: severity = "warning"; break;
215 case NOTIF_FAILURE: severity = "failure"; break;
216 default: severity = "unknown";
218 event->state = strdup (severity);
220 riemann_event_add_tag (event, "notification");
221 if (n->plugin[0] != 0)
222 riemann_event_add_tag (event, "plugin:%s", n->plugin);
223 if (n->plugin_instance[0] != 0)
224 riemann_event_add_tag (event, "plugin_instance:%s",
228 riemann_event_add_tag (event, "type:%s", n->type);
229 if (n->type_instance[0] != 0)
230 riemann_event_add_tag (event, "type_instance:%s",
233 for (i = 0; i < riemann_tags_num; i++)
234 riemann_event_add_tag (event, "%s", riemann_tags[i]);
236 /* TODO: Use FORMAT_VL() here. */
237 ssnprintf (service_buffer, sizeof(service_buffer),
238 "%s-%s-%s-%s", n->plugin, n->plugin_instance,
239 n->type, n->type_instance);
240 event->service = strdup (service_buffer);
242 /* Pull in values from threshold */
243 for (meta = n->meta; meta != NULL; meta = meta->next)
245 if (strcasecmp ("CurrentValue", meta->name) != 0)
248 event->metric_d = meta->nm_value.nm_double;
249 event->has_metric_d = 1;
253 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
254 "host = \"%s\", service = \"%s\", state = \"%s\"",
255 event->host, event->service, event->state);
257 } /* }}} Msg *riemann_notification_to_protobuf */
259 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
260 data_set_t const *ds,
261 value_list_t const *vl, size_t index,
262 gauge_t const *rates)
265 char service_buffer[6 * DATA_MAX_NAME_LEN];
268 event = malloc (sizeof (*event));
271 ERROR ("write_riemann plugin: malloc failed.");
274 memset (event, 0, sizeof (*event));
277 event->host = strdup (vl->host);
278 event->time = CDTIME_T_TO_TIME_T (vl->time);
280 event->ttl = CDTIME_T_TO_TIME_T (vl->interval) + host->delay;
283 riemann_event_add_tag (event, "plugin:%s", vl->plugin);
284 if (vl->plugin_instance[0] != 0)
285 riemann_event_add_tag (event, "plugin_instance:%s",
286 vl->plugin_instance);
288 riemann_event_add_tag (event, "type:%s", vl->type);
289 if (vl->type_instance[0] != 0)
290 riemann_event_add_tag (event, "type_instance:%s",
293 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
295 riemann_event_add_tag (event, "ds_type:%s:rate",
296 DS_TYPE_TO_STRING(ds->ds[index].type));
300 riemann_event_add_tag (event, "ds_type:%s",
301 DS_TYPE_TO_STRING(ds->ds[index].type));
303 riemann_event_add_tag (event, "ds_name:%s", ds->ds[index].name);
304 riemann_event_add_tag (event, "ds_index:%zu", index);
306 for (i = 0; i < riemann_tags_num; i++)
307 riemann_event_add_tag (event, "%s", riemann_tags[i]);
309 if (ds->ds[index].type == DS_TYPE_GAUGE)
311 event->has_metric_d = 1;
312 event->metric_d = (double) vl->values[index].gauge;
314 else if (rates != NULL)
316 event->has_metric_d = 1;
317 event->metric_d = (double) rates[index];
321 event->has_metric_sint64 = 1;
322 if (ds->ds[index].type == DS_TYPE_DERIVE)
323 event->metric_sint64 = (int64_t) vl->values[index].derive;
324 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
325 event->metric_sint64 = (int64_t) vl->values[index].absolute;
327 event->metric_sint64 = (int64_t) vl->values[index].counter;
330 /* TODO: Use FORMAT_VL() here. */
331 ssnprintf (service_buffer, sizeof(service_buffer),
332 "%s-%s-%s-%s-%s", vl->plugin, vl->plugin_instance,
333 vl->type, vl->type_instance, ds->ds[index].name);
334 event->service = strdup (service_buffer);
336 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
337 "host = \"%s\", service = \"%s\"",
338 event->host, event->service);
340 } /* }}} Event *riemann_value_to_protobuf */
342 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
343 data_set_t const *ds,
344 value_list_t const *vl)
348 gauge_t *rates = NULL;
350 /* Initialize the Msg structure. */
351 msg = malloc (sizeof (*msg));
354 ERROR ("write_riemann plugin: malloc failed.");
357 memset (msg, 0, sizeof (*msg));
360 /* Set up events. First, the list of pointers. */
361 msg->n_events = (size_t) vl->values_len;
362 msg->events = calloc (msg->n_events, sizeof (*msg->events));
363 if (msg->events == NULL)
365 ERROR ("write_riemann plugin: calloc failed.");
366 riemann_msg_protobuf_free (msg);
370 if (host->store_rates)
372 rates = uc_get_rate (ds, vl);
375 ERROR ("write_riemann plugin: uc_get_rate failed.");
376 riemann_msg_protobuf_free (msg);
381 for (i = 0; i < msg->n_events; i++)
383 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
385 if (msg->events[i] == NULL)
387 riemann_msg_protobuf_free (msg);
395 } /* }}} Msg *riemann_value_list_to_protobuf */
398 riemann_notification(const notification_t *n, user_data_t *ud)
401 struct riemann_host *host = ud->data;
404 msg = riemann_notification_to_protobuf (host, n);
408 status = riemann_send (host, msg);
410 ERROR ("write_riemann plugin: riemann_send failed with status %i",
413 riemann_msg_protobuf_free (msg);
415 } /* }}} int riemann_notification */
418 riemann_write(const data_set_t *ds,
419 const value_list_t *vl,
423 struct riemann_host *host = ud->data;
426 msg = riemann_value_list_to_protobuf (host, ds, vl);
430 status = riemann_send (host, msg);
432 ERROR ("write_riemann plugin: riemann_send failed with status %i",
435 riemann_msg_protobuf_free (msg);
439 /* host->lock must be held when calling this function. */
441 riemann_connect(struct riemann_host *host)
444 struct addrinfo *ai, *res, hints;
448 if (host->flags & F_CONNECT)
451 memset(&hints, 0, sizeof(hints));
452 memset(&service, 0, sizeof(service));
453 hints.ai_family = PF_UNSPEC;
454 hints.ai_socktype = SOCK_DGRAM;
456 hints.ai_flags |= AI_ADDRCONFIG;
459 node = (host->node != NULL) ? host->node : RIEMANN_HOST;
460 service = (host->service != NULL) ? host->service : RIEMANN_PORT;
462 if ((e = getaddrinfo(node, service, &hints, &res)) != 0) {
463 ERROR ("write_riemann plugin: Unable to resolve host \"%s\": %s",
464 node, gai_strerror(e));
469 for (ai = res; ai != NULL; ai = ai->ai_next) {
470 if ((host->s = socket(ai->ai_family,
472 ai->ai_protocol)) == -1) {
476 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
482 host->flags |= F_CONNECT;
483 DEBUG("write_riemann plugin: got a succesful connection for: %s:%s",
491 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
498 /* host->lock must be held when calling this function. */
500 riemann_disconnect (struct riemann_host *host)
502 if ((host->flags & F_CONNECT) == 0)
507 host->flags &= ~F_CONNECT;
513 riemann_free(void *p)
515 struct riemann_host *host = p;
520 pthread_mutex_lock (&host->lock);
522 host->reference_count--;
523 if (host->reference_count > 0)
525 pthread_mutex_unlock (&host->lock);
529 riemann_disconnect (host);
531 sfree(host->service);
532 pthread_mutex_destroy (&host->lock);
537 riemann_config_node(oconfig_item_t *ci)
539 struct riemann_host *host = NULL;
542 oconfig_item_t *child;
543 char callback_name[DATA_MAX_NAME_LEN];
546 if ((host = calloc(1, sizeof (*host))) == NULL) {
547 ERROR ("write_riemann plugin: calloc failed.");
550 pthread_mutex_init (&host->lock, NULL);
551 host->reference_count = 1;
553 host->service = NULL;
554 host->delay = RIEMANN_DELAY;
555 host->store_rates = 1;
557 status = cf_util_get_string (ci, &host->name);
559 WARNING("write_riemann plugin: Required host name is missing.");
564 for (i = 0; i < ci->children_num; i++) {
566 * The code here could be simplified but makes room
567 * for easy adding of new options later on.
569 child = &ci->children[i];
572 if (strcasecmp ("Host", child->key) == 0) {
573 status = cf_util_get_string (child, &host->node);
576 } else if (strcasecmp ("Port", child->key) == 0) {
577 status = cf_util_get_service (child, &host->service);
579 ERROR ("write_riemann plugin: Invalid argument "
580 "configured for the \"Port\" "
584 } else if (strcasecmp(child->key, "delay") == 0) {
585 if ((status = cf_util_get_int(ci, &host->delay)) != 0)
587 } else if (strcasecmp ("StoreRates", child->key) == 0) {
588 status = cf_util_get_boolean (ci, &host->store_rates);
592 WARNING("write_riemann plugin: ignoring unknown config "
593 "option: \"%s\"", child->key);
601 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
604 ud.free_func = riemann_free;
606 pthread_mutex_lock (&host->lock);
608 status = plugin_register_write (callback_name, riemann_write, &ud);
610 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
611 "failed with status %i.",
612 callback_name, status);
614 host->reference_count++;
616 status = plugin_register_notification (callback_name,
617 riemann_notification, &ud);
619 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
620 "failed with status %i.",
621 callback_name, status);
623 host->reference_count++;
625 if (host->reference_count <= 1)
627 /* Both callbacks failed => free memory.
628 * We need to unlock here, because riemann_free() will lock.
629 * This is not a race condition, because we're the only one
630 * holding a reference. */
631 pthread_mutex_unlock (&host->lock);
636 host->reference_count--;
637 pthread_mutex_unlock (&host->lock);
643 riemann_config(oconfig_item_t *ci)
646 oconfig_item_t *child;
649 for (i = 0; i < ci->children_num; i++) {
650 child = &ci->children[i];
652 if (strcasecmp("Node", child->key) == 0) {
653 riemann_config_node (child);
654 } else if (strcasecmp(child->key, "tag") == 0) {
656 status = cf_util_get_string(child, &tmp);
660 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
661 DEBUG("write_riemann plugin: Got tag: %s", tmp);
664 WARNING ("write_riemann plugin: Ignoring unknown "
665 "configuration option \"%s\" at top level.",
673 module_register(void)
675 plugin_register_complex_config ("write_riemann", riemann_config);
678 /* vim: set sw=8 sts=8 ts=8 noet : */