2 * collectd - src/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_PORT "5555"
36 #define RIEMANN_MAX_TAGS 37
37 #define RIEMANN_EXTRA_TAGS 32
40 #define F_CONNECT 0x01
52 static char *riemann_tags[RIEMANN_EXTRA_TAGS];
53 static int riemann_tagcount;
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_host(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) /* {{{ */
73 sfree (event->service);
75 sfree (event->description);
77 for (i = 0; i < event->n_tags; i++)
78 sfree (event->tags[i]);
82 } /* }}} void riemann_event_protobuf_free */
84 static void riemann_msg_protobuf_free (Msg *msg) /* {{{ */
91 for (i = 0; i < msg->n_events; i++)
93 riemann_event_protobuf_free (msg->events[i]);
94 msg->events[i] = NULL;
101 } /* }}} void riemann_msg_protobuf_free */
104 riemann_send(struct riemann_host *host, Msg const *msg)
110 pthread_mutex_lock (&host->lock);
112 status = riemann_connect (host);
115 pthread_mutex_unlock (&host->lock);
119 buffer_len = msg__get_packed_size(msg);
120 buffer = malloc (buffer_len);
121 if (buffer == NULL) {
122 pthread_mutex_unlock (&host->lock);
123 ERROR ("riemann plugin: malloc failed.");
126 memset (buffer, 0, buffer_len);
128 msg__pack(msg, buffer);
130 status = (int) swrite (host->s, buffer, buffer_len);
135 riemann_disconnect (host);
136 pthread_mutex_unlock (&host->lock);
138 ERROR ("riemann plugin: Sending to Riemann at %s:%s failed: %s",
140 (host->service != NULL) ? host->service : RIEMANN_PORT,
141 sstrerror (errno, errbuf, sizeof (errbuf)));
146 pthread_mutex_unlock (&host->lock);
151 static int riemann_event_add_tag (Event *event, /* {{{ */
152 char const *format, ...)
160 tmp = realloc (event->tags, (event->n_tags + 1) * sizeof (*event->tags));
165 va_start (ap, format);
166 ret = vsnprintf (buffer, sizeof (buffer), format, ap);
167 if (ret >= sizeof (buffer))
168 ret = sizeof (buffer) - 1;
172 event->tags[event->n_tags] = strdup (buffer);
173 if (event->tags[event->n_tags] == NULL)
177 } /* }}} int riemann_event_add_tag */
179 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
180 notification_t const *n)
184 char service_buffer[6 * DATA_MAX_NAME_LEN];
185 char const *severity;
186 notification_meta_t *meta;
189 msg = malloc (sizeof (*msg));
192 ERROR ("riemann plugin: malloc failed.");
195 memset (msg, 0, sizeof (*msg));
198 msg->events = malloc (sizeof (*msg->events));
199 if (msg->events == NULL)
201 ERROR ("riemann plugin: malloc failed.");
206 event = malloc (sizeof (*event));
209 ERROR ("riemann plugin: malloc failed.");
214 memset (event, 0, sizeof (*event));
217 msg->events[0] = event;
220 event->host = strdup (n->host);
221 event->time = CDTIME_T_TO_TIME_T (n->time);
226 case NOTIF_OKAY: severity = "okay"; break;
227 case NOTIF_WARNING: severity = "warning"; break;
228 case NOTIF_FAILURE: severity = "failure"; break;
229 default: severity = "unknown";
231 event->state = strdup (severity);
233 riemann_event_add_tag (event, "notification");
234 if (n->plugin[0] != 0)
235 riemann_event_add_tag (event, "plugin:%s", n->plugin);
236 if (n->plugin_instance[0] != 0)
237 riemann_event_add_tag (event, "plugin_instance:%s",
241 riemann_event_add_tag (event, "type:%s", n->type);
242 if (n->type_instance[0] != 0)
243 riemann_event_add_tag (event, "type_instance:%s",
246 for (i = 0; i < riemann_tagcount; i++)
247 riemann_event_add_tag (event, "%s", riemann_tags[i]);
249 /* TODO: Use FORMAT_VL() here. */
250 ssnprintf (service_buffer, sizeof(service_buffer),
251 "%s-%s-%s-%s", n->plugin, n->plugin_instance,
252 n->type, n->type_instance);
253 event->service = strdup (service_buffer);
255 /* Pull in values from threshold */
256 for (meta = n->meta; meta != NULL; meta = meta->next)
258 if (strcasecmp ("CurrentValue", meta->name) != 0)
261 event->metric_d = meta->nm_value.nm_double;
262 event->has_metric_d = 1;
266 DEBUG ("riemann plugin: Successfully created protobuf for notification: "
267 "host = \"%s\", service = \"%s\", state = \"%s\"",
268 event->host, event->service, event->state);
270 } /* }}} Msg *riemann_notification_to_protobuf */
272 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
273 data_set_t const *ds,
274 value_list_t const *vl, size_t index,
275 gauge_t const *rates)
278 char service_buffer[6 * DATA_MAX_NAME_LEN];
281 event = malloc (sizeof (*event));
284 ERROR ("riemann plugin: malloc failed.");
287 memset (event, 0, sizeof (*event));
290 event->host = strdup (vl->host);
291 event->time = CDTIME_T_TO_TIME_T (vl->time);
293 event->ttl = CDTIME_T_TO_TIME_T (vl->interval) + host->delay;
296 riemann_event_add_tag (event, "plugin:%s", vl->plugin);
297 if (vl->plugin_instance[0] != 0)
298 riemann_event_add_tag (event, "plugin_instance:%s",
299 vl->plugin_instance);
301 riemann_event_add_tag (event, "type:%s", vl->type);
302 if (vl->type_instance[0] != 0)
303 riemann_event_add_tag (event, "type_instance:%s",
306 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
308 riemann_event_add_tag (event, "ds_type:%s:rate",
309 DS_TYPE_TO_STRING(ds->ds[index].type));
313 riemann_event_add_tag (event, "ds_type:%s",
314 DS_TYPE_TO_STRING(ds->ds[index].type));
316 riemann_event_add_tag (event, "ds_name:%s", ds->ds[index].name);
317 riemann_event_add_tag (event, "ds_index:%zu", index);
319 for (i = 0; i < riemann_tagcount; i++)
320 riemann_event_add_tag (event, "%s", riemann_tags[i]);
322 if (ds->ds[index].type == DS_TYPE_GAUGE)
324 event->has_metric_d = 1;
325 event->metric_d = (double) vl->values[index].gauge;
327 else if (rates != NULL)
329 event->has_metric_d = 1;
330 event->metric_d = (double) rates[index];
334 event->has_metric_sint64 = 1;
335 if (ds->ds[index].type == DS_TYPE_DERIVE)
336 event->metric_sint64 = (int64_t) vl->values[index].derive;
337 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
338 event->metric_sint64 = (int64_t) vl->values[index].absolute;
340 event->metric_sint64 = (int64_t) vl->values[index].counter;
343 /* TODO: Use FORMAT_VL() here. */
344 ssnprintf (service_buffer, sizeof(service_buffer),
345 "%s-%s-%s-%s-%s", vl->plugin, vl->plugin_instance,
346 vl->type, vl->type_instance, ds->ds[index].name);
347 event->service = strdup (service_buffer);
349 DEBUG ("riemann plugin: Successfully created protobuf for metric: "
350 "host = \"%s\", service = \"%s\"",
351 event->host, event->service);
353 } /* }}} Event *riemann_value_to_protobuf */
355 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
356 data_set_t const *ds,
357 value_list_t const *vl)
361 gauge_t *rates = NULL;
363 /* Initialize the Msg structure. */
364 msg = malloc (sizeof (*msg));
367 ERROR ("riemann plugin: malloc failed.");
370 memset (msg, 0, sizeof (*msg));
373 /* Set up events. First, the list of pointers. */
374 msg->n_events = (size_t) vl->values_len;
375 msg->events = calloc (msg->n_events, sizeof (*msg->events));
376 if (msg->events == NULL)
378 ERROR ("riemann plugin: calloc failed.");
379 riemann_msg_protobuf_free (msg);
383 if (host->store_rates)
385 rates = uc_get_rate (ds, vl);
388 ERROR ("riemann plugin: uc_get_rate failed.");
389 riemann_msg_protobuf_free (msg);
394 for (i = 0; i < msg->n_events; i++)
396 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
398 if (msg->events[i] == NULL)
400 riemann_msg_protobuf_free (msg);
408 } /* }}} Msg *riemann_value_list_to_protobuf */
411 riemann_notification(const notification_t *n, user_data_t *ud)
414 struct riemann_host *host = ud->data;
417 msg = riemann_notification_to_protobuf (host, n);
421 status = riemann_send (host, msg);
423 ERROR ("riemann plugin: riemann_send failed with status %i",
426 riemann_msg_protobuf_free (msg);
428 } /* }}} int riemann_notification */
431 riemann_write(const data_set_t *ds,
432 const value_list_t *vl,
436 struct riemann_host *host = ud->data;
439 msg = riemann_value_list_to_protobuf (host, ds, vl);
443 status = riemann_send (host, msg);
445 ERROR ("riemann plugin: riemann_send failed with status %i",
448 riemann_msg_protobuf_free (msg);
452 /* host->lock must be held when calling this function. */
454 riemann_connect(struct riemann_host *host)
457 struct addrinfo *ai, *res, hints;
460 if (host->flags & F_CONNECT)
463 memset(&hints, 0, sizeof(hints));
464 memset(&service, 0, sizeof(service));
465 hints.ai_family = PF_UNSPEC;
466 hints.ai_socktype = SOCK_DGRAM;
468 assert (host->node != NULL);
469 service = (host->service != NULL) ? host->service : RIEMANN_PORT;
471 if ((e = getaddrinfo(host->node, service, &hints, &res)) != 0) {
472 ERROR ("riemann plugin: Unable to resolve host \"%s\": %s",
473 host->node, gai_strerror(e));
477 for (ai = res; ai != NULL; ai = ai->ai_next) {
479 * check if another thread did not already succesfully connect
481 if (host->flags & F_CONNECT) {
486 if ((host->s = socket(ai->ai_family,
488 ai->ai_protocol)) == -1) {
489 WARNING("riemann_connect: could not open socket");
494 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
496 host->flags |= ~F_CONNECT;
500 host->flags |= F_CONNECT;
501 DEBUG("riemann plugin: got a succesful connection for: %s",
508 WARNING("riemann_connect: no suitable hosts found");
515 /* host->lock must be held when calling this function. */
517 riemann_disconnect (struct riemann_host *host)
519 if ((host->flags & F_CONNECT) == 0)
524 host->flags &= ~F_CONNECT;
530 riemann_free(void *p)
532 struct riemann_host *host = p;
537 pthread_mutex_lock (&host->lock);
539 host->reference_count--;
540 if (host->reference_count > 0)
542 pthread_mutex_unlock (&host->lock);
546 riemann_disconnect (host);
548 sfree(host->service);
549 pthread_mutex_destroy (&host->lock);
554 riemann_config_host(oconfig_item_t *ci)
556 struct riemann_host *host = NULL;
559 oconfig_item_t *child;
560 char w_cb_name[DATA_MAX_NAME_LEN];
561 char n_cb_name[DATA_MAX_NAME_LEN];
564 if (ci->values_num != 1 ||
565 ci->values[0].type != OCONFIG_TYPE_STRING) {
566 WARNING("riemann hosts need one string argument");
570 if ((host = calloc(1, sizeof (*host))) == NULL) {
571 WARNING("riemann host allocation failed");
574 pthread_mutex_init (&host->lock, NULL);
575 host->reference_count = 1;
577 host->service = NULL;
578 host->delay = RIEMANN_DELAY;
580 status = cf_util_get_string (ci, &host->node);
582 WARNING("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(child->key, "port") == 0) {
596 status = cf_util_get_service (child, &host->service);
598 ERROR ("riemann plugin: Invalid argument "
599 "configured for the \"Port\" "
603 } else if (strcasecmp(child->key, "delay") == 0) {
604 if ((status = cf_util_get_int(ci, &host->delay)) != 0)
606 } else if (strcasecmp ("StoreRates", child->key) == 0) {
607 status = cf_util_get_boolean (ci, &host->store_rates);
611 WARNING("riemann plugin: ignoring unknown config "
612 "option: \"%s\"", child->key);
620 ssnprintf(w_cb_name, sizeof(w_cb_name), "write-riemann/%s:%s",
622 (host->service != NULL) ? host->service : RIEMANN_PORT);
623 ssnprintf(n_cb_name, sizeof(n_cb_name), "notification-riemann/%s:%s",
625 (host->service != NULL) ? host->service : RIEMANN_PORT);
626 DEBUG("riemann w_cb_name: %s", w_cb_name);
627 DEBUG("riemann n_cb_name: %s", n_cb_name);
629 ud.free_func = riemann_free;
631 pthread_mutex_lock (&host->lock);
633 status = plugin_register_write (w_cb_name, riemann_write, &ud);
635 WARNING ("riemann plugin: plugin_register_write (\"%s\") "
636 "failed with status %i.",
639 host->reference_count++;
641 status = plugin_register_notification (n_cb_name,
642 riemann_notification, &ud);
644 WARNING ("riemann plugin: plugin_register_notification (\"%s\") "
645 "failed with status %i.",
648 host->reference_count++;
650 if (host->reference_count <= 1)
652 /* Both callbacks failed => free memory.
653 * We need to unlock here, because riemann_free() will lock.
654 * This is not a race condition, because we're the only one
655 * holding a reference. */
656 pthread_mutex_unlock (&host->lock);
661 host->reference_count--;
662 pthread_mutex_unlock (&host->lock);
668 riemann_config(oconfig_item_t *ci)
672 oconfig_item_t *child;
674 for (i = 0; i < ci->children_num; i++) {
675 child = &ci->children[i];
677 if (strcasecmp(child->key, "host") == 0) {
678 riemann_config_host(child);
679 } else if (strcasecmp(child->key, "tag") == 0) {
680 if (riemann_tagcount >= RIEMANN_EXTRA_TAGS) {
681 WARNING("riemann plugin: too many tags");
685 cf_util_get_string(child, &newtag);
688 riemann_tags[riemann_tagcount++] = newtag;
689 DEBUG("riemann_config: got tag: %s", newtag);
692 WARNING ("riemann plugin: Ignoring unknown "
693 "configuration option \"%s\" at top level.",
701 module_register(void)
703 DEBUG("riemann: module_register");
705 plugin_register_complex_config ("riemann", riemann_config);
708 /* vim: set sw=8 sts=8 ts=8 noet : */