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;
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_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) /* {{{ */
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 ("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 ("riemann plugin: Sending to Riemann at %s:%s failed: %s",
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 ("riemann plugin: malloc failed.");
180 memset (msg, 0, sizeof (*msg));
183 msg->events = malloc (sizeof (*msg->events));
184 if (msg->events == NULL)
186 ERROR ("riemann plugin: malloc failed.");
191 event = malloc (sizeof (*event));
194 ERROR ("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 ("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 ("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 (vl->interval) + host->delay;
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 riemann_event_add_tag (event, "ds_type:%s",
292 DS_TYPE_TO_STRING(ds->ds[index].type));
293 riemann_event_add_tag (event, "ds_name:%s", ds->ds[index].name);
294 riemann_event_add_tag (event, "ds_index:%zu", index);
296 for (i = 0; i < riemann_tags_num; i++)
297 riemann_event_add_tag (event, "%s", riemann_tags[i]);
301 event->has_metric_d = 1;
302 event->metric_d = (double) rates[index];
304 else if (ds->ds[index].type == DS_TYPE_GAUGE)
306 event->has_metric_d = 1;
307 event->metric_d = (double) vl->values[index].gauge;
311 event->has_metric_sint64 = 1;
312 if (ds->ds[index].type == DS_TYPE_DERIVE)
313 event->metric_sint64 = (int64_t) vl->values[index].derive;
314 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
315 event->metric_sint64 = (int64_t) vl->values[index].absolute;
317 event->metric_sint64 = (int64_t) vl->values[index].counter;
320 /* TODO: Use FORMAT_VL() here. */
321 ssnprintf (service_buffer, sizeof(service_buffer),
322 "%s-%s-%s-%s-%s", vl->plugin, vl->plugin_instance,
323 vl->type, vl->type_instance, ds->ds[index].name);
324 event->service = strdup (service_buffer);
326 DEBUG ("riemann plugin: Successfully created protobuf for metric: "
327 "host = \"%s\", service = \"%s\"",
328 event->host, event->service);
330 } /* }}} Event *riemann_value_to_protobuf */
332 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
333 data_set_t const *ds,
334 value_list_t const *vl)
339 /* Initialize the Msg structure. */
340 msg = malloc (sizeof (*msg));
343 ERROR ("riemann plugin: malloc failed.");
346 memset (msg, 0, sizeof (*msg));
349 /* Set up events. First, the list of pointers. */
350 msg->n_events = (size_t) vl->values_len;
351 msg->events = calloc (msg->n_events, sizeof (*msg->events));
352 if (msg->events == NULL)
354 ERROR ("riemann plugin: calloc failed.");
355 riemann_msg_protobuf_free (msg);
359 for (i = 0; i < msg->n_events; i++)
361 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
362 (int) i, /* rates = */ NULL);
363 if (msg->events[i] == NULL)
365 riemann_msg_protobuf_free (msg);
371 } /* }}} Msg *riemann_value_list_to_protobuf */
374 riemann_notification(const notification_t *n, user_data_t *ud)
377 struct riemann_host *host = ud->data;
380 msg = riemann_notification_to_protobuf (host, n);
384 status = riemann_send (host, msg);
386 ERROR ("riemann plugin: riemann_send failed with status %i",
389 riemann_msg_protobuf_free (msg);
391 } /* }}} int riemann_notification */
394 riemann_write(const data_set_t *ds,
395 const value_list_t *vl,
399 struct riemann_host *host = ud->data;
402 msg = riemann_value_list_to_protobuf (host, ds, vl);
406 status = riemann_send (host, msg);
408 ERROR ("riemann plugin: riemann_send failed with status %i",
411 riemann_msg_protobuf_free (msg);
415 /* host->lock must be held when calling this function. */
417 riemann_connect(struct riemann_host *host)
420 struct addrinfo *ai, *res, hints;
423 if (host->flags & F_CONNECT)
426 memset(&hints, 0, sizeof(hints));
427 memset(&service, 0, sizeof(service));
428 hints.ai_family = PF_UNSPEC;
429 hints.ai_socktype = SOCK_DGRAM;
431 assert (host->node != NULL);
432 service = (host->service != NULL) ? host->service : RIEMANN_PORT;
434 if ((e = getaddrinfo(host->node, service, &hints, &res)) != 0) {
435 ERROR ("riemann plugin: Unable to resolve host \"%s\": %s",
436 host->node, gai_strerror(e));
440 for (ai = res; ai != NULL; ai = ai->ai_next) {
442 * check if another thread did not already succesfully connect
444 if (host->flags & F_CONNECT) {
449 if ((host->s = socket(ai->ai_family,
451 ai->ai_protocol)) == -1) {
452 WARNING("riemann_connect: could not open socket");
457 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
459 host->flags |= ~F_CONNECT;
463 host->flags |= F_CONNECT;
464 DEBUG("riemann plugin: got a succesful connection for: %s",
471 WARNING("riemann_connect: no suitable hosts found");
478 /* host->lock must be held when calling this function. */
480 riemann_disconnect (struct riemann_host *host)
482 if ((host->flags & F_CONNECT) == 0)
487 host->flags &= ~F_CONNECT;
493 riemann_free(void *p)
495 struct riemann_host *host = p;
500 pthread_mutex_lock (&host->lock);
502 host->reference_count--;
503 if (host->reference_count > 0)
505 pthread_mutex_unlock (&host->lock);
509 riemann_disconnect (host);
511 sfree(host->service);
512 pthread_mutex_destroy (&host->lock);
517 riemann_config_host(oconfig_item_t *ci)
519 struct riemann_host *host = NULL;
522 oconfig_item_t *child;
523 char w_cb_name[DATA_MAX_NAME_LEN];
524 char n_cb_name[DATA_MAX_NAME_LEN];
527 if (ci->values_num != 1 ||
528 ci->values[0].type != OCONFIG_TYPE_STRING) {
529 WARNING("riemann hosts need one string argument");
533 if ((host = calloc(1, sizeof (*host))) == NULL) {
534 WARNING("riemann host allocation failed");
537 pthread_mutex_init (&host->lock, NULL);
538 host->reference_count = 1;
540 host->service = NULL;
541 host->delay = RIEMANN_DELAY;
543 status = cf_util_get_string (ci, &host->node);
545 WARNING("riemann plugin: Required host name is missing.");
550 for (i = 0; i < ci->children_num; i++) {
552 * The code here could be simplified but makes room
553 * for easy adding of new options later on.
555 child = &ci->children[i];
558 if (strcasecmp(child->key, "port") == 0) {
559 status = cf_util_get_service (child, &host->service);
561 ERROR ("riemann plugin: Invalid argument "
562 "configured for the \"Port\" "
566 } else if (strcasecmp(child->key, "delay") == 0) {
567 if ((status = cf_util_get_int(ci, &host->delay)) != 0)
570 WARNING("riemann plugin: ignoring unknown config "
571 "option: \"%s\"", child->key);
579 ssnprintf(w_cb_name, sizeof(w_cb_name), "write-riemann/%s:%s",
581 (host->service != NULL) ? host->service : RIEMANN_PORT);
582 ssnprintf(n_cb_name, sizeof(n_cb_name), "notification-riemann/%s:%s",
584 (host->service != NULL) ? host->service : RIEMANN_PORT);
585 DEBUG("riemann w_cb_name: %s", w_cb_name);
586 DEBUG("riemann n_cb_name: %s", n_cb_name);
588 ud.free_func = riemann_free;
590 pthread_mutex_lock (&host->lock);
592 status = plugin_register_write (w_cb_name, riemann_write, &ud);
594 WARNING ("riemann plugin: plugin_register_write (\"%s\") "
595 "failed with status %i.",
598 host->reference_count++;
600 status = plugin_register_notification (n_cb_name,
601 riemann_notification, &ud);
603 WARNING ("riemann plugin: plugin_register_notification (\"%s\") "
604 "failed with status %i.",
607 host->reference_count++;
609 if (host->reference_count <= 1)
611 /* Both callbacks failed => free memory.
612 * We need to unlock here, because riemann_free() will lock.
613 * This is not a race condition, because we're the only one
614 * holding a reference. */
615 pthread_mutex_unlock (&host->lock);
620 host->reference_count--;
621 pthread_mutex_unlock (&host->lock);
627 riemann_config(oconfig_item_t *ci)
630 oconfig_item_t *child;
633 for (i = 0; i < ci->children_num; i++) {
634 child = &ci->children[i];
636 if (strcasecmp(child->key, "host") == 0) {
637 riemann_config_host(child);
638 } else if (strcasecmp(child->key, "tag") == 0) {
640 status = cf_util_get_string(child, &tmp);
644 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
645 DEBUG("riemann plugin: Got tag: %s", tmp);
648 WARNING ("riemann plugin: Ignoring unknown "
649 "configuration option \"%s\" at top level.",
657 module_register(void)
659 DEBUG("riemann: module_register");
661 plugin_register_complex_config ("riemann", riemann_config);
664 /* vim: set sw=8 sts=8 ts=8 noet : */