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 "riemann.pb-c.h"
26 #include <sys/socket.h>
27 #include <arpa/inet.h>
33 #define RIEMANN_DELAY 1
34 #define RIEMANN_PORT "5555"
35 #define RIEMANN_MAX_TAGS 37
36 #define RIEMANN_EXTRA_TAGS 32
39 #define F_CONNECT 0x01
50 static char *riemann_tags[RIEMANN_EXTRA_TAGS];
51 static int riemann_tagcount;
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_host(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) /* {{{ */
71 sfree (event->service);
73 sfree (event->description);
75 for (i = 0; i < event->n_tags; i++)
76 sfree (event->tags[i]);
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 ("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 ("riemann plugin: Sending to Riemann at %s:%s failed: %s",
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, ...)
158 tmp = realloc (event->tags, (event->n_tags + 1) * sizeof (*event->tags));
163 va_start (ap, format);
164 ret = vsnprintf (buffer, sizeof (buffer), format, ap);
165 if (ret >= sizeof (buffer))
166 ret = sizeof (buffer) - 1;
170 event->tags[event->n_tags] = strdup (buffer);
171 if (event->tags[event->n_tags] == NULL)
175 } /* }}} int riemann_event_add_tag */
177 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
178 notification_t const *n)
182 char service_buffer[6 * DATA_MAX_NAME_LEN];
183 char const *severity;
184 notification_meta_t *meta;
187 msg = malloc (sizeof (*msg));
190 ERROR ("riemann plugin: malloc failed.");
193 memset (msg, 0, sizeof (*msg));
196 msg->events = malloc (sizeof (*msg->events));
197 if (msg->events == NULL)
199 ERROR ("riemann plugin: malloc failed.");
204 event = malloc (sizeof (*event));
207 ERROR ("riemann plugin: malloc failed.");
212 memset (event, 0, sizeof (*event));
215 msg->events[0] = event;
218 event->host = strdup (n->host);
219 event->time = CDTIME_T_TO_TIME_T (n->time);
224 case NOTIF_OKAY: severity = "okay"; break;
225 case NOTIF_WARNING: severity = "warning"; break;
226 case NOTIF_FAILURE: severity = "failure"; break;
227 default: severity = "unknown";
229 event->state = strdup (severity);
231 riemann_event_add_tag (event, "notification");
232 if (n->plugin[0] != 0)
233 riemann_event_add_tag (event, "plugin:%s", n->plugin);
234 if (n->plugin_instance[0] != 0)
235 riemann_event_add_tag (event, "plugin_instance:%s",
239 riemann_event_add_tag (event, "type:%s", n->type);
240 if (n->type_instance[0] != 0)
241 riemann_event_add_tag (event, "type_instance:%s",
244 for (i = 0; i < riemann_tagcount; i++)
245 riemann_event_add_tag (event, "%s", riemann_tags[i]);
247 /* TODO: Use FORMAT_VL() here. */
248 ssnprintf (service_buffer, sizeof(service_buffer),
249 "%s-%s-%s-%s", n->plugin, n->plugin_instance,
250 n->type, n->type_instance);
251 event->service = strdup (service_buffer);
253 /* Pull in values from threshold */
254 for (meta = n->meta; meta != NULL; meta = meta->next)
256 if (strcasecmp ("CurrentValue", meta->name) != 0)
259 event->metric_d = meta->nm_value.nm_double;
260 event->has_metric_d = 1;
264 DEBUG ("riemann plugin: Successfully created protobuf for notification: "
265 "host = \"%s\", service = \"%s\", state = \"%s\"",
266 event->host, event->service, event->state);
268 } /* }}} Msg *riemann_notification_to_protobuf */
270 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
271 data_set_t const *ds,
272 value_list_t const *vl, size_t index,
273 gauge_t const *rates)
276 char service_buffer[6 * DATA_MAX_NAME_LEN];
279 event = malloc (sizeof (*event));
282 ERROR ("riemann plugin: malloc failed.");
285 memset (event, 0, sizeof (*event));
288 event->host = strdup (vl->host);
289 event->time = CDTIME_T_TO_TIME_T (vl->time);
291 event->ttl = CDTIME_T_TO_TIME_T (vl->interval) + host->delay;
294 riemann_event_add_tag (event, "plugin:%s", vl->plugin);
295 if (vl->plugin_instance[0] != 0)
296 riemann_event_add_tag (event, "plugin_instance:%s",
297 vl->plugin_instance);
299 riemann_event_add_tag (event, "type:%s", vl->type);
300 if (vl->type_instance[0] != 0)
301 riemann_event_add_tag (event, "type_instance:%s",
304 riemann_event_add_tag (event, "ds_type:%s",
305 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_tagcount; i++)
310 riemann_event_add_tag (event, "%s", riemann_tags[i]);
314 event->has_metric_d = 1;
315 event->metric_d = (double) rates[index];
317 else if (ds->ds[index].type == DS_TYPE_GAUGE)
319 event->has_metric_d = 1;
320 event->metric_d = (double) vl->values[index].gauge;
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 /* TODO: Use FORMAT_VL() here. */
334 ssnprintf (service_buffer, sizeof(service_buffer),
335 "%s-%s-%s-%s-%s", vl->plugin, vl->plugin_instance,
336 vl->type, vl->type_instance, ds->ds[index].name);
337 event->service = strdup (service_buffer);
339 DEBUG ("riemann plugin: Successfully created protobuf for metric: "
340 "host = \"%s\", service = \"%s\"",
341 event->host, event->service);
343 } /* }}} Event *riemann_value_to_protobuf */
345 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
346 data_set_t const *ds,
347 value_list_t const *vl)
352 /* Initialize the Msg structure. */
353 msg = malloc (sizeof (*msg));
356 ERROR ("riemann plugin: malloc failed.");
359 memset (msg, 0, sizeof (*msg));
362 /* Set up events. First, the list of pointers. */
363 msg->n_events = (size_t) vl->values_len;
364 msg->events = calloc (msg->n_events, sizeof (*msg->events));
365 if (msg->events == NULL)
367 ERROR ("riemann plugin: calloc failed.");
368 riemann_msg_protobuf_free (msg);
372 for (i = 0; i < msg->n_events; i++)
374 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
375 (int) i, /* rates = */ NULL);
376 if (msg->events[i] == NULL)
378 riemann_msg_protobuf_free (msg);
384 } /* }}} Msg *riemann_value_list_to_protobuf */
387 riemann_notification(const notification_t *n, user_data_t *ud)
390 struct riemann_host *host = ud->data;
393 msg = riemann_notification_to_protobuf (host, n);
397 status = riemann_send (host, msg);
399 ERROR ("riemann plugin: riemann_send failed with status %i",
402 riemann_msg_protobuf_free (msg);
404 } /* }}} int riemann_notification */
407 riemann_write(const data_set_t *ds,
408 const value_list_t *vl,
412 struct riemann_host *host = ud->data;
415 msg = riemann_value_list_to_protobuf (host, ds, vl);
419 status = riemann_send (host, msg);
421 ERROR ("riemann plugin: riemann_send failed with status %i",
424 riemann_msg_protobuf_free (msg);
428 /* host->lock must be held when calling this function. */
430 riemann_connect(struct riemann_host *host)
433 struct addrinfo *ai, *res, hints;
436 if (host->flags & F_CONNECT)
439 memset(&hints, 0, sizeof(hints));
440 memset(&service, 0, sizeof(service));
441 hints.ai_family = PF_UNSPEC;
442 hints.ai_socktype = SOCK_DGRAM;
444 assert (host->node != NULL);
445 service = (host->service != NULL) ? host->service : RIEMANN_PORT;
447 if ((e = getaddrinfo(host->node, service, &hints, &res)) != 0) {
448 ERROR ("riemann plugin: Unable to resolve host \"%s\": %s",
449 host->node, gai_strerror(e));
453 for (ai = res; ai != NULL; ai = ai->ai_next) {
455 * check if another thread did not already succesfully connect
457 if (host->flags & F_CONNECT) {
462 if ((host->s = socket(ai->ai_family,
464 ai->ai_protocol)) == -1) {
465 WARNING("riemann_connect: could not open socket");
470 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
472 host->flags |= ~F_CONNECT;
476 host->flags |= F_CONNECT;
477 DEBUG("riemann plugin: got a succesful connection for: %s",
484 WARNING("riemann_connect: no suitable hosts found");
491 /* host->lock must be held when calling this function. */
493 riemann_disconnect (struct riemann_host *host)
495 if ((host->flags & F_CONNECT) == 0)
500 host->flags &= ~F_CONNECT;
506 riemann_free(void *p)
508 struct riemann_host *host = p;
513 pthread_mutex_lock (&host->lock);
515 host->reference_count--;
516 if (host->reference_count > 0)
518 pthread_mutex_unlock (&host->lock);
522 riemann_disconnect (host);
524 sfree(host->service);
525 pthread_mutex_destroy (&host->lock);
530 riemann_config_host(oconfig_item_t *ci)
532 struct riemann_host *host = NULL;
535 oconfig_item_t *child;
536 char w_cb_name[DATA_MAX_NAME_LEN];
537 char n_cb_name[DATA_MAX_NAME_LEN];
540 if (ci->values_num != 1 ||
541 ci->values[0].type != OCONFIG_TYPE_STRING) {
542 WARNING("riemann hosts need one string argument");
546 if ((host = calloc(1, sizeof (*host))) == NULL) {
547 WARNING("riemann host allocation failed");
550 pthread_mutex_init (&host->lock, NULL);
551 host->reference_count = 1;
553 host->service = NULL;
554 host->delay = RIEMANN_DELAY;
556 status = cf_util_get_string (ci, &host->node);
558 WARNING("riemann plugin: Required host name is missing.");
563 for (i = 0; i < ci->children_num; i++) {
565 * The code here could be simplified but makes room
566 * for easy adding of new options later on.
568 child = &ci->children[i];
571 if (strcasecmp(child->key, "port") == 0) {
572 status = cf_util_get_service (child, &host->service);
574 ERROR ("riemann plugin: Invalid argument "
575 "configured for the \"Port\" "
579 } else if (strcasecmp(child->key, "delay") == 0) {
580 if ((status = cf_util_get_int(ci, &host->delay)) != 0)
583 WARNING("riemann plugin: ignoring unknown config "
584 "option: \"%s\"", child->key);
592 ssnprintf(w_cb_name, sizeof(w_cb_name), "write-riemann/%s:%s",
594 (host->service != NULL) ? host->service : RIEMANN_PORT);
595 ssnprintf(n_cb_name, sizeof(n_cb_name), "notification-riemann/%s:%s",
597 (host->service != NULL) ? host->service : RIEMANN_PORT);
598 DEBUG("riemann w_cb_name: %s", w_cb_name);
599 DEBUG("riemann n_cb_name: %s", n_cb_name);
601 ud.free_func = riemann_free;
603 pthread_mutex_lock (&host->lock);
605 status = plugin_register_write (w_cb_name, riemann_write, &ud);
607 WARNING ("riemann plugin: plugin_register_write (\"%s\") "
608 "failed with status %i.",
611 host->reference_count++;
613 status = plugin_register_notification (n_cb_name,
614 riemann_notification, &ud);
616 WARNING ("riemann plugin: plugin_register_notification (\"%s\") "
617 "failed with status %i.",
620 host->reference_count++;
622 if (host->reference_count <= 1)
624 /* Both callbacks failed => free memory.
625 * We need to unlock here, because riemann_free() will lock.
626 * This is not a race condition, because we're the only one
627 * holding a reference. */
628 pthread_mutex_unlock (&host->lock);
633 host->reference_count--;
634 pthread_mutex_unlock (&host->lock);
640 riemann_config(oconfig_item_t *ci)
644 oconfig_item_t *child;
646 for (i = 0; i < ci->children_num; i++) {
647 child = &ci->children[i];
649 if (strcasecmp(child->key, "host") == 0) {
650 riemann_config_host(child);
651 } else if (strcasecmp(child->key, "tag") == 0) {
652 if (riemann_tagcount >= RIEMANN_EXTRA_TAGS) {
653 WARNING("riemann plugin: too many tags");
657 cf_util_get_string(child, &newtag);
660 riemann_tags[riemann_tagcount++] = newtag;
661 DEBUG("riemann_config: got tag: %s", newtag);
664 WARNING ("riemann plugin: Ignoring unknown "
665 "configuration option \"%s\" at top level.",
673 module_register(void)
675 DEBUG("riemann: module_register");
677 plugin_register_complex_config ("riemann", riemann_config);
680 /* vim: set sw=8 sts=8 ts=8 noet : */