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_HOST "localhost"
35 #define RIEMANN_PORT "5555"
39 #define F_CONNECT 0x01
50 static char **riemann_tags;
51 static size_t riemann_tags_num;
53 static int riemann_send(struct riemann_host *, Msg const *);
54 static int riemann_notification(const notification_t *, user_data_t *);
55 static int riemann_write(const data_set_t *, const value_list_t *, user_data_t *);
56 static int riemann_connect(struct riemann_host *);
57 static int riemann_disconnect (struct riemann_host *host);
58 static void riemann_free(void *);
59 static int riemann_config_node(oconfig_item_t *);
60 static int riemann_config(oconfig_item_t *);
61 void module_register(void);
63 static void riemann_event_protobuf_free (Event *event) /* {{{ */
69 sfree (event->service);
71 sfree (event->description);
73 strarray_free (event->tags, event->n_tags);
78 } /* }}} void riemann_event_protobuf_free */
80 static void riemann_msg_protobuf_free (Msg *msg) /* {{{ */
87 for (i = 0; i < msg->n_events; i++)
89 riemann_event_protobuf_free (msg->events[i]);
90 msg->events[i] = NULL;
97 } /* }}} void riemann_msg_protobuf_free */
100 riemann_send(struct riemann_host *host, Msg const *msg)
106 pthread_mutex_lock (&host->lock);
108 status = riemann_connect (host);
111 pthread_mutex_unlock (&host->lock);
115 buffer_len = msg__get_packed_size(msg);
116 buffer = malloc (buffer_len);
117 if (buffer == NULL) {
118 pthread_mutex_unlock (&host->lock);
119 ERROR ("write_riemann plugin: malloc failed.");
122 memset (buffer, 0, buffer_len);
124 msg__pack(msg, buffer);
126 status = (int) swrite (host->s, buffer, buffer_len);
131 riemann_disconnect (host);
132 pthread_mutex_unlock (&host->lock);
134 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s",
135 (host->node != NULL) ? host->node : RIEMANN_HOST,
136 (host->service != NULL) ? host->service : RIEMANN_PORT,
137 sstrerror (errno, errbuf, sizeof (errbuf)));
142 pthread_mutex_unlock (&host->lock);
147 static int riemann_event_add_tag (Event *event, /* {{{ */
148 char const *format, ...)
154 va_start (ap, format);
155 ret = vsnprintf (buffer, sizeof (buffer), format, ap);
156 if (ret >= sizeof (buffer))
157 ret = sizeof (buffer) - 1;
161 return (strarray_add (&event->tags, &event->n_tags, buffer));
162 } /* }}} int riemann_event_add_tag */
164 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
165 notification_t const *n)
169 char service_buffer[6 * DATA_MAX_NAME_LEN];
170 char const *severity;
171 notification_meta_t *meta;
174 msg = malloc (sizeof (*msg));
177 ERROR ("write_riemann plugin: malloc failed.");
180 memset (msg, 0, sizeof (*msg));
183 msg->events = malloc (sizeof (*msg->events));
184 if (msg->events == NULL)
186 ERROR ("write_riemann plugin: malloc failed.");
191 event = malloc (sizeof (*event));
194 ERROR ("write_riemann plugin: malloc failed.");
199 memset (event, 0, sizeof (*event));
202 msg->events[0] = event;
205 event->host = strdup (n->host);
206 event->time = CDTIME_T_TO_TIME_T (n->time);
211 case NOTIF_OKAY: severity = "okay"; break;
212 case NOTIF_WARNING: severity = "warning"; break;
213 case NOTIF_FAILURE: severity = "failure"; break;
214 default: severity = "unknown";
216 event->state = strdup (severity);
218 riemann_event_add_tag (event, "notification");
219 if (n->plugin[0] != 0)
220 riemann_event_add_tag (event, "plugin:%s", n->plugin);
221 if (n->plugin_instance[0] != 0)
222 riemann_event_add_tag (event, "plugin_instance:%s",
226 riemann_event_add_tag (event, "type:%s", n->type);
227 if (n->type_instance[0] != 0)
228 riemann_event_add_tag (event, "type_instance:%s",
231 for (i = 0; i < riemann_tags_num; i++)
232 riemann_event_add_tag (event, "%s", riemann_tags[i]);
234 /* TODO: Use FORMAT_VL() here. */
235 ssnprintf (service_buffer, sizeof(service_buffer),
236 "%s-%s-%s-%s", n->plugin, n->plugin_instance,
237 n->type, n->type_instance);
238 event->service = strdup (service_buffer);
240 /* Pull in values from threshold */
241 for (meta = n->meta; meta != NULL; meta = meta->next)
243 if (strcasecmp ("CurrentValue", meta->name) != 0)
246 event->metric_d = meta->nm_value.nm_double;
247 event->has_metric_d = 1;
251 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
252 "host = \"%s\", service = \"%s\", state = \"%s\"",
253 event->host, event->service, event->state);
255 } /* }}} Msg *riemann_notification_to_protobuf */
257 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
258 data_set_t const *ds,
259 value_list_t const *vl, size_t index,
260 gauge_t const *rates)
263 char service_buffer[6 * DATA_MAX_NAME_LEN];
266 event = malloc (sizeof (*event));
269 ERROR ("write_riemann plugin: malloc failed.");
272 memset (event, 0, sizeof (*event));
275 event->host = strdup (vl->host);
276 event->time = CDTIME_T_TO_TIME_T (vl->time);
278 event->ttl = CDTIME_T_TO_TIME_T (2 * vl->interval);
281 riemann_event_add_tag (event, "plugin:%s", vl->plugin);
282 if (vl->plugin_instance[0] != 0)
283 riemann_event_add_tag (event, "plugin_instance:%s",
284 vl->plugin_instance);
286 riemann_event_add_tag (event, "type:%s", vl->type);
287 if (vl->type_instance[0] != 0)
288 riemann_event_add_tag (event, "type_instance:%s",
291 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
293 riemann_event_add_tag (event, "ds_type:%s:rate",
294 DS_TYPE_TO_STRING(ds->ds[index].type));
298 riemann_event_add_tag (event, "ds_type:%s",
299 DS_TYPE_TO_STRING(ds->ds[index].type));
301 riemann_event_add_tag (event, "ds_name:%s", ds->ds[index].name);
302 riemann_event_add_tag (event, "ds_index:%zu", index);
304 for (i = 0; i < riemann_tags_num; i++)
305 riemann_event_add_tag (event, "%s", riemann_tags[i]);
307 if (ds->ds[index].type == DS_TYPE_GAUGE)
309 event->has_metric_d = 1;
310 event->metric_d = (double) vl->values[index].gauge;
312 else if (rates != NULL)
314 event->has_metric_d = 1;
315 event->metric_d = (double) rates[index];
319 event->has_metric_sint64 = 1;
320 if (ds->ds[index].type == DS_TYPE_DERIVE)
321 event->metric_sint64 = (int64_t) vl->values[index].derive;
322 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
323 event->metric_sint64 = (int64_t) vl->values[index].absolute;
325 event->metric_sint64 = (int64_t) vl->values[index].counter;
328 /* TODO: Use FORMAT_VL() here. */
329 ssnprintf (service_buffer, sizeof(service_buffer),
330 "%s-%s-%s-%s-%s", vl->plugin, vl->plugin_instance,
331 vl->type, vl->type_instance, ds->ds[index].name);
332 event->service = strdup (service_buffer);
334 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
335 "host = \"%s\", service = \"%s\"",
336 event->host, event->service);
338 } /* }}} Event *riemann_value_to_protobuf */
340 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
341 data_set_t const *ds,
342 value_list_t const *vl)
346 gauge_t *rates = NULL;
348 /* Initialize the Msg structure. */
349 msg = malloc (sizeof (*msg));
352 ERROR ("write_riemann plugin: malloc failed.");
355 memset (msg, 0, sizeof (*msg));
358 /* Set up events. First, the list of pointers. */
359 msg->n_events = (size_t) vl->values_len;
360 msg->events = calloc (msg->n_events, sizeof (*msg->events));
361 if (msg->events == NULL)
363 ERROR ("write_riemann plugin: calloc failed.");
364 riemann_msg_protobuf_free (msg);
368 if (host->store_rates)
370 rates = uc_get_rate (ds, vl);
373 ERROR ("write_riemann plugin: uc_get_rate failed.");
374 riemann_msg_protobuf_free (msg);
379 for (i = 0; i < msg->n_events; i++)
381 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
383 if (msg->events[i] == NULL)
385 riemann_msg_protobuf_free (msg);
393 } /* }}} Msg *riemann_value_list_to_protobuf */
396 riemann_notification(const notification_t *n, user_data_t *ud)
399 struct riemann_host *host = ud->data;
402 msg = riemann_notification_to_protobuf (host, n);
406 status = riemann_send (host, msg);
408 ERROR ("write_riemann plugin: riemann_send failed with status %i",
411 riemann_msg_protobuf_free (msg);
413 } /* }}} int riemann_notification */
416 riemann_write(const data_set_t *ds,
417 const value_list_t *vl,
421 struct riemann_host *host = ud->data;
424 msg = riemann_value_list_to_protobuf (host, ds, vl);
428 status = riemann_send (host, msg);
430 ERROR ("write_riemann plugin: riemann_send failed with status %i",
433 riemann_msg_protobuf_free (msg);
437 /* host->lock must be held when calling this function. */
439 riemann_connect(struct riemann_host *host)
442 struct addrinfo *ai, *res, hints;
446 if (host->flags & F_CONNECT)
449 memset(&hints, 0, sizeof(hints));
450 memset(&service, 0, sizeof(service));
451 hints.ai_family = PF_UNSPEC;
452 hints.ai_socktype = SOCK_DGRAM;
454 hints.ai_flags |= AI_ADDRCONFIG;
457 node = (host->node != NULL) ? host->node : RIEMANN_HOST;
458 service = (host->service != NULL) ? host->service : RIEMANN_PORT;
460 if ((e = getaddrinfo(node, service, &hints, &res)) != 0) {
461 ERROR ("write_riemann plugin: Unable to resolve host \"%s\": %s",
462 node, gai_strerror(e));
467 for (ai = res; ai != NULL; ai = ai->ai_next) {
468 if ((host->s = socket(ai->ai_family,
470 ai->ai_protocol)) == -1) {
474 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
480 host->flags |= F_CONNECT;
481 DEBUG("write_riemann plugin: got a succesful connection for: %s:%s",
489 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
496 /* host->lock must be held when calling this function. */
498 riemann_disconnect (struct riemann_host *host)
500 if ((host->flags & F_CONNECT) == 0)
505 host->flags &= ~F_CONNECT;
511 riemann_free(void *p)
513 struct riemann_host *host = p;
518 pthread_mutex_lock (&host->lock);
520 host->reference_count--;
521 if (host->reference_count > 0)
523 pthread_mutex_unlock (&host->lock);
527 riemann_disconnect (host);
529 sfree(host->service);
530 pthread_mutex_destroy (&host->lock);
535 riemann_config_node(oconfig_item_t *ci)
537 struct riemann_host *host = NULL;
540 oconfig_item_t *child;
541 char callback_name[DATA_MAX_NAME_LEN];
544 if ((host = calloc(1, sizeof (*host))) == NULL) {
545 ERROR ("write_riemann plugin: calloc failed.");
548 pthread_mutex_init (&host->lock, NULL);
549 host->reference_count = 1;
551 host->service = NULL;
552 host->store_rates = 1;
554 status = cf_util_get_string (ci, &host->name);
556 WARNING("write_riemann plugin: Required host name is missing.");
561 for (i = 0; i < ci->children_num; i++) {
563 * The code here could be simplified but makes room
564 * for easy adding of new options later on.
566 child = &ci->children[i];
569 if (strcasecmp ("Host", child->key) == 0) {
570 status = cf_util_get_string (child, &host->node);
573 } else if (strcasecmp ("Port", child->key) == 0) {
574 status = cf_util_get_service (child, &host->service);
576 ERROR ("write_riemann plugin: Invalid argument "
577 "configured for the \"Port\" "
581 } else if (strcasecmp ("StoreRates", child->key) == 0) {
582 status = cf_util_get_boolean (ci, &host->store_rates);
586 WARNING("write_riemann plugin: ignoring unknown config "
587 "option: \"%s\"", child->key);
595 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
598 ud.free_func = riemann_free;
600 pthread_mutex_lock (&host->lock);
602 status = plugin_register_write (callback_name, riemann_write, &ud);
604 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
605 "failed with status %i.",
606 callback_name, status);
608 host->reference_count++;
610 status = plugin_register_notification (callback_name,
611 riemann_notification, &ud);
613 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
614 "failed with status %i.",
615 callback_name, status);
617 host->reference_count++;
619 if (host->reference_count <= 1)
621 /* Both callbacks failed => free memory.
622 * We need to unlock here, because riemann_free() will lock.
623 * This is not a race condition, because we're the only one
624 * holding a reference. */
625 pthread_mutex_unlock (&host->lock);
630 host->reference_count--;
631 pthread_mutex_unlock (&host->lock);
637 riemann_config(oconfig_item_t *ci)
640 oconfig_item_t *child;
643 for (i = 0; i < ci->children_num; i++) {
644 child = &ci->children[i];
646 if (strcasecmp("Node", child->key) == 0) {
647 riemann_config_node (child);
648 } else if (strcasecmp(child->key, "tag") == 0) {
650 status = cf_util_get_string(child, &tmp);
654 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
655 DEBUG("write_riemann plugin: Got tag: %s", tmp);
658 WARNING ("write_riemann plugin: Ignoring unknown "
659 "configuration option \"%s\" at top level.",
667 module_register(void)
669 plugin_register_complex_config ("write_riemann", riemann_config);
672 /* vim: set sw=8 sts=8 ts=8 noet : */