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
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:%s failed: %s",
122 (host->service != NULL) ? host->service : RIEMANN_PORT,
123 sstrerror (errno, errbuf, sizeof (errbuf)));
124 riemann_disconnect (host);
133 static int riemann_event_add_tag (Event *event, /* {{{ */
134 char const *format, ...)
142 tmp = realloc (event->tags, (event->n_tags + 1) * sizeof (*event->tags));
147 va_start (ap, format);
148 ret = vsnprintf (buffer, sizeof (buffer), format, ap);
149 if (ret >= sizeof (buffer))
150 ret = sizeof (buffer) - 1;
154 event->tags[event->n_tags] = strdup (buffer);
155 if (event->tags[event->n_tags] == NULL)
159 } /* }}} int riemann_event_add_tag */
161 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
162 notification_t const *n)
166 char service_buffer[6 * DATA_MAX_NAME_LEN];
167 char const *severity;
168 notification_meta_t *meta;
171 msg = malloc (sizeof (*msg));
174 ERROR ("riemann plugin: malloc failed.");
177 memset (msg, 0, sizeof (*msg));
180 msg->events = malloc (sizeof (*msg->events));
181 if (msg->events == NULL)
183 ERROR ("riemann plugin: malloc failed.");
188 event = malloc (sizeof (*event));
191 ERROR ("riemann plugin: malloc failed.");
196 memset (event, 0, sizeof (*event));
199 msg->events[0] = event;
202 event->host = strdup (n->host);
203 event->time = CDTIME_T_TO_TIME_T (n->time);
208 case NOTIF_OKAY: severity = "okay"; break;
209 case NOTIF_WARNING: severity = "warning"; break;
210 case NOTIF_FAILURE: severity = "failure"; break;
211 default: severity = "unknown";
213 event->state = strdup (severity);
215 riemann_event_add_tag (event, "notification");
216 if (n->plugin[0] != 0)
217 riemann_event_add_tag (event, "plugin:%s", n->plugin);
218 if (n->plugin_instance[0] != 0)
219 riemann_event_add_tag (event, "plugin_instance:%s",
223 riemann_event_add_tag (event, "type:%s", n->type);
224 if (n->type_instance[0] != 0)
225 riemann_event_add_tag (event, "type_instance:%s",
228 for (i = 0; i < riemann_tagcount; i++)
229 riemann_event_add_tag (event, "%s", riemann_tags[i]);
231 /* TODO: Use FORMAT_VL() here. */
232 ssnprintf (service_buffer, sizeof(service_buffer),
233 "%s-%s-%s-%s", n->plugin, n->plugin_instance,
234 n->type, n->type_instance);
235 event->service = strdup (service_buffer);
237 /* Pull in values from threshold */
238 for (meta = n->meta; meta != NULL; meta = meta->next)
240 if (strcasecmp ("CurrentValue", meta->name) != 0)
243 event->metric_d = meta->nm_value.nm_double;
244 event->has_metric_d = 1;
248 DEBUG ("riemann plugin: Successfully created protobuf for notification: "
249 "host = \"%s\", service = \"%s\", state = \"%s\"",
250 event->host, event->service, event->state);
252 } /* }}} Msg *riemann_notification_to_protobuf */
254 static Event *riemann_value_to_protobuf (struct riemann_host *host, /* {{{ */
255 data_set_t const *ds,
256 value_list_t const *vl, size_t index,
257 gauge_t const *rates)
260 char service_buffer[6 * DATA_MAX_NAME_LEN];
263 event = malloc (sizeof (*event));
266 ERROR ("riemann plugin: malloc failed.");
269 memset (event, 0, sizeof (*event));
272 event->host = strdup (vl->host);
273 event->time = CDTIME_T_TO_TIME_T (vl->time);
275 event->ttl = CDTIME_T_TO_TIME_T (vl->interval) + host->delay;
278 riemann_event_add_tag (event, "plugin:%s", vl->plugin);
279 if (vl->plugin_instance[0] != 0)
280 riemann_event_add_tag (event, "plugin_instance:%s",
281 vl->plugin_instance);
283 riemann_event_add_tag (event, "type:%s", vl->type);
284 if (vl->type_instance[0] != 0)
285 riemann_event_add_tag (event, "type_instance:%s",
288 riemann_event_add_tag (event, "ds_type:%s",
289 DS_TYPE_TO_STRING(ds->ds[index].type));
290 riemann_event_add_tag (event, "ds_name:%s", ds->ds[index].name);
291 riemann_event_add_tag (event, "ds_index:%zu", index);
293 for (i = 0; i < riemann_tagcount; i++)
294 riemann_event_add_tag (event, "%s", riemann_tags[i]);
298 event->has_metric_d = 1;
299 event->metric_d = (double) rates[index];
301 else if (ds->ds[index].type == DS_TYPE_GAUGE)
303 event->has_metric_d = 1;
304 event->metric_d = (double) vl->values[index].gauge;
308 event->has_metric_sint64 = 1;
309 if (ds->ds[index].type == DS_TYPE_DERIVE)
310 event->metric_sint64 = (int64_t) vl->values[index].derive;
311 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
312 event->metric_sint64 = (int64_t) vl->values[index].absolute;
314 event->metric_sint64 = (int64_t) vl->values[index].counter;
317 /* TODO: Use FORMAT_VL() here. */
318 ssnprintf (service_buffer, sizeof(service_buffer),
319 "%s-%s-%s-%s-%s", vl->plugin, vl->plugin_instance,
320 vl->type, vl->type_instance, ds->ds[i].name);
321 event->service = strdup (service_buffer);
323 DEBUG ("riemann plugin: Successfully created protobuf for metric: "
324 "host = \"%s\", service = \"%s\"",
325 event->host, event->service);
327 } /* }}} Event *riemann_value_to_protobuf */
329 static Msg *riemann_value_list_to_protobuf (struct riemann_host *host, /* {{{ */
330 data_set_t const *ds,
331 value_list_t const *vl)
336 /* Initialize the Msg structure. */
337 msg = malloc (sizeof (*msg));
340 ERROR ("riemann plugin: malloc failed.");
343 memset (msg, 0, sizeof (*msg));
346 /* Set up events. First, the list of pointers. */
347 msg->n_events = (size_t) vl->values_len;
348 msg->events = calloc (msg->n_events, sizeof (*msg->events));
349 if (msg->events == NULL)
351 ERROR ("riemann plugin: calloc failed.");
352 riemann_msg_protobuf_free (msg);
356 for (i = 0; i < msg->n_events; i++)
358 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
359 (int) i, /* rates = */ NULL);
362 riemann_msg_protobuf_free (msg);
368 } /* }}} Msg *riemann_value_list_to_protobuf */
371 riemann_notification(const notification_t *n, user_data_t *ud)
374 struct riemann_host *host = ud->data;
377 msg = riemann_notification_to_protobuf (host, n);
381 status = riemann_send (host, msg);
383 ERROR ("riemann plugin: riemann_send failed with status %i",
386 riemann_msg_protobuf_free (msg);
388 } /* }}} int riemann_notification */
391 riemann_write(const data_set_t *ds,
392 const value_list_t *vl,
396 struct riemann_host *host = ud->data;
399 if ((status = riemann_connect(host)) != 0)
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);
416 riemann_connect(struct riemann_host *host)
419 struct addrinfo *ai, *res, hints;
422 if (host->flags & F_CONNECT)
425 memset(&hints, 0, sizeof(hints));
426 memset(&service, 0, sizeof(service));
427 hints.ai_family = PF_UNSPEC;
428 hints.ai_socktype = SOCK_DGRAM;
430 assert (host->node != NULL);
431 service = (host->service != NULL) ? host->service : RIEMANN_PORT;
433 if ((e = getaddrinfo(host->node, service, &hints, &res)) != 0) {
434 ERROR ("riemann plugin: Unable to resolve host \"%s\": %s",
435 host->node, gai_strerror(e));
439 for (ai = res; ai != NULL; ai = ai->ai_next) {
440 pthread_mutex_lock(&host->lock);
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 pthread_mutex_unlock(&host->lock);
453 WARNING("riemann_connect: could not open socket");
458 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
460 host->flags |= ~F_CONNECT;
461 pthread_mutex_unlock(&host->lock);
465 host->flags |= F_CONNECT;
466 DEBUG("riemann plugin: got a succesful connection for: %s",
468 pthread_mutex_unlock(&host->lock);
474 WARNING("riemann_connect: no suitable hosts found");
482 riemann_disconnect (struct riemann_host *host)
487 if ((host->flags & F_CONNECT) == 0)
492 host->flags &= ~F_CONNECT;
498 riemann_free(void *p)
500 struct riemann_host *host = p;
505 riemann_disconnect (host);
507 sfree(host->service);
512 riemann_config_host(oconfig_item_t *ci)
514 struct riemann_host *host = NULL;
517 oconfig_item_t *child;
518 char w_cb_name[DATA_MAX_NAME_LEN];
519 char n_cb_name[DATA_MAX_NAME_LEN];
522 if (ci->values_num != 1 ||
523 ci->values[0].type != OCONFIG_TYPE_STRING) {
524 WARNING("riemann hosts need one string argument");
528 if ((host = calloc(1, sizeof (*host))) == NULL) {
529 WARNING("riemann host allocation failed");
532 pthread_mutex_init(&host->lock, NULL);
534 host->service = NULL;
535 host->delay = RIEMANN_DELAY;
537 status = cf_util_get_string (ci, &host->node);
539 WARNING("riemann plugin: Required host name is missing.");
544 for (i = 0; i < ci->children_num; i++) {
546 * The code here could be simplified but makes room
547 * for easy adding of new options later on.
549 child = &ci->children[i];
552 if (strcasecmp(child->key, "port") == 0) {
553 status = cf_util_get_service (child, &host->service);
555 ERROR ("riemann plugin: Invalid argument "
556 "configured for the \"Port\" "
560 } else if (strcasecmp(child->key, "delay") == 0) {
561 if ((status = cf_util_get_int(ci, &host->delay)) != 0)
564 WARNING("riemann plugin: ignoring unknown config "
565 "option: \"%s\"", child->key);
573 ssnprintf(w_cb_name, sizeof(w_cb_name), "write-riemann/%s:%s",
575 (host->service != NULL) ? host->service : RIEMANN_PORT);
576 ssnprintf(n_cb_name, sizeof(n_cb_name), "notification-riemann/%s:%s",
578 (host->service != NULL) ? host->service : RIEMANN_PORT);
579 DEBUG("riemann w_cb_name: %s", w_cb_name);
580 DEBUG("riemann n_cb_name: %s", n_cb_name);
582 ud.free_func = riemann_free;
584 if ((status = plugin_register_write(w_cb_name, riemann_write, &ud)) != 0)
587 if ((status = plugin_register_notification(n_cb_name,
588 riemann_notification,
590 plugin_unregister_write(w_cb_name);
597 riemann_config(oconfig_item_t *ci)
601 oconfig_item_t *child;
603 for (i = 0; i < ci->children_num; i++) {
604 child = &ci->children[i];
606 if (strcasecmp(child->key, "host") == 0) {
607 riemann_config_host(child);
608 } else if (strcasecmp(child->key, "tag") == 0) {
609 if (riemann_tagcount >= RIEMANN_EXTRA_TAGS) {
610 WARNING("riemann plugin: too many tags");
614 cf_util_get_string(child, &newtag);
617 riemann_tags[riemann_tagcount++] = newtag;
618 DEBUG("riemann_config: got tag: %s", newtag);
621 WARNING ("riemann plugin: Ignoring unknown "
622 "configuration option \"%s\" at top level.",
630 module_register(void)
632 DEBUG("riemann: module_register");
634 plugin_register_complex_config ("riemann", riemann_config);
637 /* vim: set sw=8 sts=8 ts=8 noet : */