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
43 char name[DATA_MAX_NAME_LEN];
48 static char *riemann_tags[RIEMANN_EXTRA_TAGS];
49 static int riemann_tagcount;
51 static int riemann_send(struct riemann_host *, Msg const *);
52 static int riemann_notification(const notification_t *, user_data_t *);
53 static int riemann_write(const data_set_t *, const value_list_t *, user_data_t *);
54 static int riemann_connect(struct riemann_host *);
55 static int riemann_disconnect (struct riemann_host *host);
56 static void riemann_free(void *);
57 static int riemann_config_host(oconfig_item_t *);
58 static int riemann_config(oconfig_item_t *);
59 void module_register(void);
61 static void riemann_event_protobuf_free (Event *event) /* {{{ */
69 sfree (event->service);
71 sfree (event->description);
73 for (i = 0; i < event->n_tags; i++)
74 sfree (event->tags[i]);
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 buffer_len = msg__get_packed_size(msg);
107 buffer = malloc (buffer_len);
108 if (buffer == NULL) {
109 ERROR ("riemann plugin: malloc failed.");
112 memset (buffer, 0, buffer_len);
114 msg__pack(msg, buffer);
116 status = swrite (host->s, buffer, buffer_len);
120 ERROR ("riemann plugin: Sending to Riemann at %s:%d failed: %s",
121 host->name, host->port,
122 sstrerror (errno, errbuf, sizeof (errbuf)));
123 riemann_disconnect (host);
132 static int riemann_event_add_tag (Event *event, /* {{{ */
133 char const *format, ...)
141 tmp = realloc (event->tags, (event->n_tags + 1) * sizeof (*event->tags));
146 va_start (ap, format);
147 ret = vsnprintf (buffer, sizeof (buffer), format, ap);
148 if (ret >= sizeof (buffer))
149 ret = sizeof (buffer) - 1;
153 event->tags[event->n_tags] = strdup (buffer);
154 if (event->tags[event->n_tags] == NULL)
158 } /* }}} int riemann_event_add_tag */
160 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
161 notification_t const *n)
165 char service_buffer[6 * DATA_MAX_NAME_LEN];
166 char const *severity;
167 notification_meta_t *meta;
170 msg = malloc (sizeof (*msg));
173 ERROR ("riemann plugin: malloc failed.");
176 memset (msg, 0, sizeof (*msg));
179 msg->events = malloc (sizeof (*msg->events));
180 if (msg->events == NULL)
182 ERROR ("riemann plugin: malloc failed.");
187 event = malloc (sizeof (*event));
190 ERROR ("riemann plugin: malloc failed.");
195 memset (event, 0, sizeof (*event));
198 msg->events[0] = event;
201 event->host = strdup (n->host);
202 event->time = CDTIME_T_TO_TIME_T (n->time);
207 case NOTIF_OKAY: severity = "okay"; break;
208 case NOTIF_WARNING: severity = "warning"; break;
209 case NOTIF_FAILURE: severity = "failure"; break;
210 default: severity = "unknown";
212 event->state = strdup (severity);
214 riemann_event_add_tag (event, "notification");
215 if (n->plugin[0] != 0)
216 riemann_event_add_tag (event, "plugin:%s", n->plugin);
217 if (n->plugin_instance[0] != 0)
218 riemann_event_add_tag (event, "plugin_instance:%s",
222 riemann_event_add_tag (event, "type:%s", n->type);
223 if (n->type_instance[0] != 0)
224 riemann_event_add_tag (event, "type_instance:%s",
227 for (i = 0; i < riemann_tagcount; i++)
228 riemann_event_add_tag (event, "%s", riemann_tags[i]);
230 /* TODO: Use FORMAT_VL() here. */
231 ssnprintf (service_buffer, sizeof(service_buffer),
232 "%s-%s-%s-%s", n->plugin, n->plugin_instance,
233 n->type, n->type_instance);
234 event->service = strdup (service_buffer);
236 /* Pull in values from threshold */
237 for (meta = n->meta; meta != NULL; meta = meta->next)
239 if (strcasecmp ("CurrentValue", meta->name) != 0)
242 event->metric_d = meta->nm_value.nm_double;
243 event->has_metric_d = 1;
247 DEBUG ("riemann plugin: Successfully created protobuf for notification: "
248 "host = \"%s\", service = \"%s\", state = \"%s\"",
249 event->host, event->service, event->state);
251 } /* }}} Msg *riemann_notification_to_protobuf */
253 static Event *riemann_value_to_protobuf (struct riemann_host *host, /* {{{ */
254 data_set_t const *ds,
255 value_list_t const *vl, size_t index,
256 gauge_t const *rates)
259 char service_buffer[6 * DATA_MAX_NAME_LEN];
262 event = malloc (sizeof (*event));
265 ERROR ("riemann plugin: malloc failed.");
268 memset (event, 0, sizeof (*event));
271 event->host = strdup (vl->host);
272 event->time = CDTIME_T_TO_TIME_T (vl->time);
274 event->ttl = CDTIME_T_TO_TIME_T (vl->interval) + host->delay;
277 riemann_event_add_tag (event, "plugin:%s", vl->plugin);
278 if (vl->plugin_instance[0] != 0)
279 riemann_event_add_tag (event, "plugin_instance:%s",
280 vl->plugin_instance);
282 riemann_event_add_tag (event, "type:%s", vl->type);
283 if (vl->type_instance[0] != 0)
284 riemann_event_add_tag (event, "type_instance:%s",
287 riemann_event_add_tag (event, "ds_type:%s",
288 DS_TYPE_TO_STRING(ds->ds[index].type));
289 riemann_event_add_tag (event, "ds_name:%s", ds->ds[index].name);
290 riemann_event_add_tag (event, "ds_index:%zu", index);
292 for (i = 0; i < riemann_tagcount; i++)
293 riemann_event_add_tag (event, "%s", riemann_tags[i]);
297 event->has_metric_d = 1;
298 event->metric_d = (double) rates[index];
300 else if (ds->ds[index].type == DS_TYPE_GAUGE)
302 event->has_metric_d = 1;
303 event->metric_d = (double) vl->values[index].gauge;
307 event->has_metric_sint64 = 1;
308 if (ds->ds[index].type == DS_TYPE_DERIVE)
309 event->metric_sint64 = (int64_t) vl->values[index].derive;
310 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
311 event->metric_sint64 = (int64_t) vl->values[index].absolute;
313 event->metric_sint64 = (int64_t) vl->values[index].counter;
316 /* TODO: Use FORMAT_VL() here. */
317 ssnprintf (service_buffer, sizeof(service_buffer),
318 "%s-%s-%s-%s-%s", vl->plugin, vl->plugin_instance,
319 vl->type, vl->type_instance, ds->ds[i].name);
320 event->service = strdup (service_buffer);
322 DEBUG ("riemann plugin: Successfully created protobuf for metric: "
323 "host = \"%s\", service = \"%s\"",
324 event->host, event->service);
326 } /* }}} Event *riemann_value_to_protobuf */
328 static Msg *riemann_value_list_to_protobuf (struct riemann_host *host, /* {{{ */
329 data_set_t const *ds,
330 value_list_t const *vl)
335 /* Initialize the Msg structure. */
336 msg = malloc (sizeof (*msg));
339 ERROR ("riemann plugin: malloc failed.");
342 memset (msg, 0, sizeof (*msg));
345 /* Set up events. First, the list of pointers. */
346 msg->n_events = (size_t) vl->values_len;
347 msg->events = calloc (msg->n_events, sizeof (*msg->events));
348 if (msg->events == NULL)
350 ERROR ("riemann plugin: calloc failed.");
351 riemann_msg_protobuf_free (msg);
355 for (i = 0; i < msg->n_events; i++)
357 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
358 (int) i, /* rates = */ NULL);
361 riemann_msg_protobuf_free (msg);
367 } /* }}} Msg *riemann_value_list_to_protobuf */
370 riemann_notification(const notification_t *n, user_data_t *ud)
373 struct riemann_host *host = ud->data;
376 msg = riemann_notification_to_protobuf (host, n);
380 status = riemann_send (host, msg);
382 ERROR ("riemann plugin: riemann_send failed with status %i",
385 riemann_msg_protobuf_free (msg);
387 } /* }}} int riemann_notification */
390 riemann_write(const data_set_t *ds,
391 const value_list_t *vl,
395 struct riemann_host *host = ud->data;
398 if ((status = riemann_connect(host)) != 0)
401 msg = riemann_value_list_to_protobuf (host, ds, vl);
405 status = riemann_send (host, msg);
407 ERROR ("riemann plugin: riemann_send failed with status %i",
410 riemann_msg_protobuf_free (msg);
415 riemann_connect(struct riemann_host *host)
418 struct addrinfo *ai, *res, hints;
421 if (host->flags & F_CONNECT)
424 memset(&hints, 0, sizeof(hints));
425 memset(&service, 0, sizeof(service));
426 hints.ai_family = PF_UNSPEC;
427 hints.ai_socktype = SOCK_DGRAM;
429 ssnprintf(service, sizeof(service), "%d", host->port);
431 if ((e = getaddrinfo(host->name, service, &hints, &res)) != 0) {
432 WARNING("could not resolve host \"%s\": %s",
433 host->name, gai_strerror(e));
437 for (ai = res; ai != NULL; ai = ai->ai_next) {
438 pthread_mutex_lock(&host->lock);
440 * check if another thread did not already succesfully connect
442 if (host->flags & F_CONNECT) {
447 if ((host->s = socket(ai->ai_family,
449 ai->ai_protocol)) == -1) {
450 pthread_mutex_unlock(&host->lock);
451 WARNING("riemann_connect: could not open socket");
456 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
458 host->flags |= ~F_CONNECT;
459 pthread_mutex_unlock(&host->lock);
463 host->flags |= F_CONNECT;
464 DEBUG("got a succesful connection for: %s", host->name);
465 pthread_mutex_unlock(&host->lock);
471 WARNING("riemann_connect: no suitable hosts found");
479 riemann_disconnect (struct riemann_host *host)
484 if ((host->flags & F_CONNECT) == 0)
489 host->flags &= ~F_CONNECT;
495 riemann_free(void *p)
497 struct riemann_host *host = p;
499 riemann_disconnect (host);
504 riemann_config_host(oconfig_item_t *ci)
506 struct riemann_host *host = NULL;
509 oconfig_item_t *child;
510 char w_cb_name[DATA_MAX_NAME_LEN];
511 char n_cb_name[DATA_MAX_NAME_LEN];
514 if (ci->values_num != 1 ||
515 ci->values[0].type != OCONFIG_TYPE_STRING) {
516 WARNING("riemann hosts need one string argument");
520 if ((host = calloc(1, sizeof (*host))) == NULL) {
521 WARNING("riemann host allocation failed");
525 if (cf_util_get_string_buffer(ci, host->name,
526 sizeof(host->name)) != 0) {
527 WARNING("riemann host name too long");
532 host->port = RIEMANN_PORT;
533 host->delay = RIEMANN_DELAY;
534 for (i = 0; i < ci->children_num; i++) {
536 * The code here could be simplified but makes room
537 * for easy adding of new options later on.
539 child = &ci->children[i];
542 if (strcasecmp(child->key, "port") == 0) {
543 if ((status = cf_util_get_port_number(child)) < 0) {
544 WARNING("invalid port number");
549 } else if (strcasecmp(child->key, "delay") == 0) {
550 if ((status = cf_util_get_int(ci, &host->delay)) != 0)
553 WARNING("riemann plugin: ignoring unknown config "
554 "option: \"%s\"", child->key);
562 pthread_mutex_init(&host->lock, NULL);
563 ssnprintf(w_cb_name, sizeof(w_cb_name), "write-riemann/%s:%d",
564 host->name, host->port);
565 ssnprintf(n_cb_name, sizeof(n_cb_name), "notification-riemann/%s:%d",
566 host->name, host->port);
567 DEBUG("riemann w_cb_name: %s", w_cb_name);
568 DEBUG("riemann n_cb_name: %s", n_cb_name);
570 ud.free_func = riemann_free;
572 if ((status = plugin_register_write(w_cb_name, riemann_write, &ud)) != 0)
575 if ((status = plugin_register_notification(n_cb_name,
576 riemann_notification,
578 plugin_unregister_write(w_cb_name);
585 riemann_config(oconfig_item_t *ci)
589 oconfig_item_t *child;
591 for (i = 0; i < ci->children_num; i++) {
592 child = &ci->children[i];
594 if (strcasecmp(child->key, "host") == 0) {
595 riemann_config_host(child);
596 } else if (strcasecmp(child->key, "tag") == 0) {
597 if (riemann_tagcount >= RIEMANN_EXTRA_TAGS) {
598 WARNING("riemann plugin: too many tags");
602 cf_util_get_string(child, &newtag);
605 riemann_tags[riemann_tagcount++] = newtag;
606 DEBUG("riemann_config: got tag: %s", newtag);
609 WARNING ("riemann plugin: Ignoring unknown "
610 "configuration option \"%s\" at top level.",
618 module_register(void)
620 DEBUG("riemann: module_register");
622 plugin_register_complex_config ("riemann", riemann_config);