1 /**
2 * collectd - src/write_riemann.c
3 *
4 * Copyright (C) 2012,2013 Pierre-Yves Ritschard
5 * Copyright (C) 2013 Florian octo Forster
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 *
19 * Authors:
20 * Pierre-Yves Ritschard <pyr at spootnik.org>
21 * Florian octo Forster <octo at collectd.org>
22 */
24 #include "collectd.h"
25 #include "plugin.h"
26 #include "common.h"
27 #include "configfile.h"
28 #include "utils_cache.h"
29 #include "riemann.pb-c.h"
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
33 #include <errno.h>
34 #include <netdb.h>
35 #include <inttypes.h>
36 #include <pthread.h>
38 #define RIEMANN_HOST "localhost"
39 #define RIEMANN_PORT "5555"
41 struct riemann_host {
42 char *name;
43 #define F_CONNECT 0x01
44 uint8_t flags;
45 pthread_mutex_t lock;
46 _Bool store_rates;
47 _Bool always_append_ds;
48 char *node;
49 char *service;
50 _Bool use_tcp;
51 int s;
53 int reference_count;
54 };
56 static char **riemann_tags;
57 static size_t riemann_tags_num;
59 static void riemann_event_protobuf_free (Event *event) /* {{{ */
60 {
61 size_t i;
63 if (event == NULL)
64 return;
66 sfree (event->state);
67 sfree (event->service);
68 sfree (event->host);
69 sfree (event->description);
71 strarray_free (event->tags, event->n_tags);
72 event->tags = NULL;
73 event->n_tags = 0;
75 for (i = 0; i < event->n_attributes; i++)
76 {
77 sfree (event->attributes[i]->key);
78 sfree (event->attributes[i]->value);
79 sfree (event->attributes[i]);
80 }
81 sfree (event->attributes);
82 event->n_attributes = 0;
84 sfree (event);
85 } /* }}} void riemann_event_protobuf_free */
87 static void riemann_msg_protobuf_free (Msg *msg) /* {{{ */
88 {
89 size_t i;
91 if (msg == NULL)
92 return;
94 for (i = 0; i < msg->n_events; i++)
95 {
96 riemann_event_protobuf_free (msg->events[i]);
97 msg->events[i] = NULL;
98 }
100 sfree (msg->events);
101 msg->n_events = 0;
103 sfree (msg);
104 } /* }}} void riemann_msg_protobuf_free */
106 /* host->lock must be held when calling this function. */
107 static int
108 riemann_connect(struct riemann_host *host)
109 {
110 int e;
111 struct addrinfo *ai, *res, hints;
112 char const *node;
113 char const *service;
115 if (host->flags & F_CONNECT)
116 return 0;
118 memset(&hints, 0, sizeof(hints));
119 memset(&service, 0, sizeof(service));
120 hints.ai_family = AF_UNSPEC;
121 hints.ai_socktype = host->use_tcp ? SOCK_STREAM : SOCK_DGRAM;
122 #ifdef AI_ADDRCONFIG
123 hints.ai_flags |= AI_ADDRCONFIG;
124 #endif
126 node = (host->node != NULL) ? host->node : RIEMANN_HOST;
127 service = (host->service != NULL) ? host->service : RIEMANN_PORT;
129 if ((e = getaddrinfo(node, service, &hints, &res)) != 0) {
130 ERROR ("write_riemann plugin: Unable to resolve host \"%s\": %s",
131 node, gai_strerror(e));
132 return -1;
133 }
135 host->s = -1;
136 for (ai = res; ai != NULL; ai = ai->ai_next) {
137 if ((host->s = socket(ai->ai_family,
138 ai->ai_socktype,
139 ai->ai_protocol)) == -1) {
140 continue;
141 }
143 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
144 close(host->s);
145 host->s = -1;
146 continue;
147 }
149 host->flags |= F_CONNECT;
150 DEBUG("write_riemann plugin: got a succesful connection for: %s:%s",
151 node, service);
152 break;
153 }
155 freeaddrinfo(res);
157 if (host->s < 0) {
158 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
159 node, service);
160 return -1;
161 }
162 return 0;
163 }
165 /* host->lock must be held when calling this function. */
166 static int
167 riemann_disconnect (struct riemann_host *host)
168 {
169 if ((host->flags & F_CONNECT) == 0)
170 return (0);
172 close (host->s);
173 host->s = -1;
174 host->flags &= ~F_CONNECT;
176 return (0);
177 }
179 static int
180 riemann_send(struct riemann_host *host, Msg const *msg)
181 {
182 u_char *buffer;
183 size_t buffer_len;
184 int status;
186 pthread_mutex_lock (&host->lock);
188 status = riemann_connect (host);
189 if (status != 0)
190 {
191 pthread_mutex_unlock (&host->lock);
192 return status;
193 }
195 buffer_len = msg__get_packed_size(msg);
196 if (host->use_tcp)
197 buffer_len += 4;
199 buffer = malloc (buffer_len);
200 if (buffer == NULL) {
201 pthread_mutex_unlock (&host->lock);
202 ERROR ("write_riemann plugin: malloc failed.");
203 return ENOMEM;
204 }
205 memset (buffer, 0, buffer_len);
207 if (host->use_tcp)
208 {
209 uint32_t length = htonl ((uint32_t) (buffer_len - 4));
210 memcpy (buffer, &length, 4);
211 msg__pack(msg, buffer + 4);
212 }
213 else
214 {
215 msg__pack(msg, buffer);
216 }
218 status = (int) swrite (host->s, buffer, buffer_len);
219 if (status != 0)
220 {
221 char errbuf[1024];
223 riemann_disconnect (host);
224 pthread_mutex_unlock (&host->lock);
226 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s",
227 (host->node != NULL) ? host->node : RIEMANN_HOST,
228 (host->service != NULL) ? host->service : RIEMANN_PORT,
229 sstrerror (errno, errbuf, sizeof (errbuf)));
230 sfree (buffer);
231 return -1;
232 }
234 pthread_mutex_unlock (&host->lock);
235 sfree (buffer);
236 return 0;
237 }
239 static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
240 {
241 return (strarray_add (&event->tags, &event->n_tags, tag));
242 } /* }}} int riemann_event_add_tag */
244 static int riemann_event_add_attribute (Event *event, /* {{{ */
245 char const *key, char const *value)
246 {
247 Attribute **new_attributes;
248 Attribute *a;
250 new_attributes = realloc (event->attributes,
251 sizeof (*event->attributes) * (event->n_attributes + 1));
252 if (new_attributes == NULL)
253 {
254 ERROR ("write_riemann plugin: realloc failed.");
255 return (ENOMEM);
256 }
257 event->attributes = new_attributes;
259 a = malloc (sizeof (*a));
260 if (a == NULL)
261 {
262 ERROR ("write_riemann plugin: malloc failed.");
263 return (ENOMEM);
264 }
265 attribute__init (a);
267 a->key = strdup (key);
268 if (value != NULL)
269 a->value = strdup (value);
271 event->attributes[event->n_attributes] = a;
272 event->n_attributes++;
274 return (0);
275 } /* }}} int riemann_event_add_attribute */
277 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
278 notification_t const *n)
279 {
280 Msg *msg;
281 Event *event;
282 char service_buffer[6 * DATA_MAX_NAME_LEN];
283 char const *severity;
284 notification_meta_t *meta;
285 int i;
287 msg = malloc (sizeof (*msg));
288 if (msg == NULL)
289 {
290 ERROR ("write_riemann plugin: malloc failed.");
291 return (NULL);
292 }
293 memset (msg, 0, sizeof (*msg));
294 msg__init (msg);
296 msg->events = malloc (sizeof (*msg->events));
297 if (msg->events == NULL)
298 {
299 ERROR ("write_riemann plugin: malloc failed.");
300 sfree (msg);
301 return (NULL);
302 }
304 event = malloc (sizeof (*event));
305 if (event == NULL)
306 {
307 ERROR ("write_riemann plugin: malloc failed.");
308 sfree (msg->events);
309 sfree (msg);
310 return (NULL);
311 }
312 memset (event, 0, sizeof (*event));
313 event__init (event);
315 msg->events[0] = event;
316 msg->n_events = 1;
318 event->host = strdup (n->host);
319 event->time = CDTIME_T_TO_TIME_T (n->time);
320 event->has_time = 1;
322 switch (n->severity)
323 {
324 case NOTIF_OKAY: severity = "ok"; break;
325 case NOTIF_WARNING: severity = "warning"; break;
326 case NOTIF_FAILURE: severity = "critical"; break;
327 default: severity = "unknown";
328 }
329 event->state = strdup (severity);
331 riemann_event_add_tag (event, "notification");
332 if (n->host[0] != 0)
333 riemann_event_add_attribute (event, "host", n->host);
334 if (n->plugin[0] != 0)
335 riemann_event_add_attribute (event, "plugin", n->plugin);
336 if (n->plugin_instance[0] != 0)
337 riemann_event_add_attribute (event, "plugin_instance",
338 n->plugin_instance);
340 if (n->type[0] != 0)
341 riemann_event_add_attribute (event, "type", n->type);
342 if (n->type_instance[0] != 0)
343 riemann_event_add_attribute (event, "type_instance",
344 n->type_instance);
346 for (i = 0; i < riemann_tags_num; i++)
347 riemann_event_add_tag (event, riemann_tags[i]);
349 format_name (service_buffer, sizeof (service_buffer),
350 /* host = */ "", n->plugin, n->plugin_instance,
351 n->type, n->type_instance);
352 event->service = strdup (&service_buffer[1]);
354 /* Pull in values from threshold */
355 for (meta = n->meta; meta != NULL; meta = meta->next)
356 {
357 if (strcasecmp ("CurrentValue", meta->name) != 0)
358 continue;
360 event->metric_d = meta->nm_value.nm_double;
361 event->has_metric_d = 1;
362 break;
363 }
365 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
366 "host = \"%s\", service = \"%s\", state = \"%s\"",
367 event->host, event->service, event->state);
368 return (msg);
369 } /* }}} Msg *riemann_notification_to_protobuf */
371 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
372 data_set_t const *ds,
373 value_list_t const *vl, size_t index,
374 gauge_t const *rates)
375 {
376 Event *event;
377 char name_buffer[5 * DATA_MAX_NAME_LEN];
378 char service_buffer[6 * DATA_MAX_NAME_LEN];
379 int i;
381 event = malloc (sizeof (*event));
382 if (event == NULL)
383 {
384 ERROR ("write_riemann plugin: malloc failed.");
385 return (NULL);
386 }
387 memset (event, 0, sizeof (*event));
388 event__init (event);
390 event->host = strdup (vl->host);
391 event->time = CDTIME_T_TO_TIME_T (vl->time);
392 event->has_time = 1;
393 event->ttl = CDTIME_T_TO_TIME_T (2 * vl->interval);
394 event->has_ttl = 1;
396 riemann_event_add_attribute (event, "plugin", vl->plugin);
397 if (vl->plugin_instance[0] != 0)
398 riemann_event_add_attribute (event, "plugin_instance",
399 vl->plugin_instance);
401 riemann_event_add_attribute (event, "type", vl->type);
402 if (vl->type_instance[0] != 0)
403 riemann_event_add_attribute (event, "type_instance",
404 vl->type_instance);
406 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
407 {
408 char ds_type[DATA_MAX_NAME_LEN];
410 ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
411 DS_TYPE_TO_STRING(ds->ds[index].type));
412 riemann_event_add_attribute (event, "ds_type", ds_type);
413 }
414 else
415 {
416 riemann_event_add_attribute (event, "ds_type",
417 DS_TYPE_TO_STRING(ds->ds[index].type));
418 }
419 riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
420 {
421 char ds_index[DATA_MAX_NAME_LEN];
423 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
424 riemann_event_add_attribute (event, "ds_index", ds_index);
425 }
427 for (i = 0; i < riemann_tags_num; i++)
428 riemann_event_add_tag (event, riemann_tags[i]);
430 if (ds->ds[index].type == DS_TYPE_GAUGE)
431 {
432 event->has_metric_d = 1;
433 event->metric_d = (double) vl->values[index].gauge;
434 }
435 else if (rates != NULL)
436 {
437 event->has_metric_d = 1;
438 event->metric_d = (double) rates[index];
439 }
440 else
441 {
442 event->has_metric_sint64 = 1;
443 if (ds->ds[index].type == DS_TYPE_DERIVE)
444 event->metric_sint64 = (int64_t) vl->values[index].derive;
445 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
446 event->metric_sint64 = (int64_t) vl->values[index].absolute;
447 else
448 event->metric_sint64 = (int64_t) vl->values[index].counter;
449 }
451 format_name (name_buffer, sizeof (name_buffer),
452 /* host = */ "", vl->plugin, vl->plugin_instance,
453 vl->type, vl->type_instance);
454 if (host->always_append_ds || (ds->ds_num > 1))
455 ssnprintf (service_buffer, sizeof (service_buffer),
456 "%s/%s", &name_buffer[1], ds->ds[index].name);
457 else
458 sstrncpy (service_buffer, &name_buffer[1],
459 sizeof (service_buffer));
461 event->service = strdup (service_buffer);
463 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
464 "host = \"%s\", service = \"%s\"",
465 event->host, event->service);
466 return (event);
467 } /* }}} Event *riemann_value_to_protobuf */
469 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
470 data_set_t const *ds,
471 value_list_t const *vl)
472 {
473 Msg *msg;
474 size_t i;
475 gauge_t *rates = NULL;
477 /* Initialize the Msg structure. */
478 msg = malloc (sizeof (*msg));
479 if (msg == NULL)
480 {
481 ERROR ("write_riemann plugin: malloc failed.");
482 return (NULL);
483 }
484 memset (msg, 0, sizeof (*msg));
485 msg__init (msg);
487 /* Set up events. First, the list of pointers. */
488 msg->n_events = (size_t) vl->values_len;
489 msg->events = calloc (msg->n_events, sizeof (*msg->events));
490 if (msg->events == NULL)
491 {
492 ERROR ("write_riemann plugin: calloc failed.");
493 riemann_msg_protobuf_free (msg);
494 return (NULL);
495 }
497 if (host->store_rates)
498 {
499 rates = uc_get_rate (ds, vl);
500 if (rates == NULL)
501 {
502 ERROR ("write_riemann plugin: uc_get_rate failed.");
503 riemann_msg_protobuf_free (msg);
504 return (NULL);
505 }
506 }
508 for (i = 0; i < msg->n_events; i++)
509 {
510 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
511 (int) i, rates);
512 if (msg->events[i] == NULL)
513 {
514 riemann_msg_protobuf_free (msg);
515 sfree (rates);
516 return (NULL);
517 }
518 }
520 sfree (rates);
521 return (msg);
522 } /* }}} Msg *riemann_value_list_to_protobuf */
524 static int
525 riemann_notification(const notification_t *n, user_data_t *ud)
526 {
527 int status;
528 struct riemann_host *host = ud->data;
529 Msg *msg;
531 msg = riemann_notification_to_protobuf (host, n);
532 if (msg == NULL)
533 return (-1);
535 status = riemann_send (host, msg);
536 if (status != 0)
537 ERROR ("write_riemann plugin: riemann_send failed with status %i",
538 status);
540 riemann_msg_protobuf_free (msg);
541 return (status);
542 } /* }}} int riemann_notification */
544 static int
545 riemann_write(const data_set_t *ds,
546 const value_list_t *vl,
547 user_data_t *ud)
548 {
549 int status;
550 struct riemann_host *host = ud->data;
551 Msg *msg;
553 msg = riemann_value_list_to_protobuf (host, ds, vl);
554 if (msg == NULL)
555 return (-1);
557 status = riemann_send (host, msg);
558 if (status != 0)
559 ERROR ("write_riemann plugin: riemann_send failed with status %i",
560 status);
562 riemann_msg_protobuf_free (msg);
563 return status;
564 }
566 static void
567 riemann_free(void *p)
568 {
569 struct riemann_host *host = p;
571 if (host == NULL)
572 return;
574 pthread_mutex_lock (&host->lock);
576 host->reference_count--;
577 if (host->reference_count > 0)
578 {
579 pthread_mutex_unlock (&host->lock);
580 return;
581 }
583 riemann_disconnect (host);
585 sfree(host->service);
586 pthread_mutex_destroy (&host->lock);
587 sfree(host);
588 }
590 static int
591 riemann_config_node(oconfig_item_t *ci)
592 {
593 struct riemann_host *host = NULL;
594 int status = 0;
595 int i;
596 oconfig_item_t *child;
597 char callback_name[DATA_MAX_NAME_LEN];
598 user_data_t ud;
600 if ((host = calloc(1, sizeof (*host))) == NULL) {
601 ERROR ("write_riemann plugin: calloc failed.");
602 return ENOMEM;
603 }
604 pthread_mutex_init (&host->lock, NULL);
605 host->reference_count = 1;
606 host->node = NULL;
607 host->service = NULL;
608 host->store_rates = 1;
609 host->always_append_ds = 0;
610 host->use_tcp = 0;
612 status = cf_util_get_string (ci, &host->name);
613 if (status != 0) {
614 WARNING("write_riemann plugin: Required host name is missing.");
615 riemann_free (host);
616 return -1;
617 }
619 for (i = 0; i < ci->children_num; i++) {
620 /*
621 * The code here could be simplified but makes room
622 * for easy adding of new options later on.
623 */
624 child = &ci->children[i];
625 status = 0;
627 if (strcasecmp ("Host", child->key) == 0) {
628 status = cf_util_get_string (child, &host->node);
629 if (status != 0)
630 break;
631 } else if (strcasecmp ("Port", child->key) == 0) {
632 status = cf_util_get_service (child, &host->service);
633 if (status != 0) {
634 ERROR ("write_riemann plugin: Invalid argument "
635 "configured for the \"Port\" "
636 "option.");
637 break;
638 }
639 } else if (strcasecmp ("Protocol", child->key) == 0) {
640 char tmp[16];
641 status = cf_util_get_string_buffer (child,
642 tmp, sizeof (tmp));
643 if (status != 0)
644 {
645 ERROR ("write_riemann plugin: cf_util_get_"
646 "string_buffer failed with "
647 "status %i.", status);
648 break;
649 }
651 if (strcasecmp ("UDP", tmp) == 0)
652 host->use_tcp = 0;
653 else if (strcasecmp ("TCP", tmp) == 0)
654 host->use_tcp = 1;
655 else
656 WARNING ("write_riemann plugin: The value "
657 "\"%s\" is not valid for the "
658 "\"Protocol\" option. Use "
659 "either \"UDP\" or \"TCP\".",
660 tmp);
661 } else if (strcasecmp ("StoreRates", child->key) == 0) {
662 status = cf_util_get_boolean (child, &host->store_rates);
663 if (status != 0)
664 break;
665 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
666 status = cf_util_get_boolean (child,
667 &host->always_append_ds);
668 if (status != 0)
669 break;
670 } else {
671 WARNING("write_riemann plugin: ignoring unknown config "
672 "option: \"%s\"", child->key);
673 }
674 }
675 if (status != 0) {
676 riemann_free (host);
677 return status;
678 }
680 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
681 host->name);
682 ud.data = host;
683 ud.free_func = riemann_free;
685 pthread_mutex_lock (&host->lock);
687 status = plugin_register_write (callback_name, riemann_write, &ud);
688 if (status != 0)
689 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
690 "failed with status %i.",
691 callback_name, status);
692 else /* success */
693 host->reference_count++;
695 status = plugin_register_notification (callback_name,
696 riemann_notification, &ud);
697 if (status != 0)
698 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
699 "failed with status %i.",
700 callback_name, status);
701 else /* success */
702 host->reference_count++;
704 if (host->reference_count <= 1)
705 {
706 /* Both callbacks failed => free memory.
707 * We need to unlock here, because riemann_free() will lock.
708 * This is not a race condition, because we're the only one
709 * holding a reference. */
710 pthread_mutex_unlock (&host->lock);
711 riemann_free (host);
712 return (-1);
713 }
715 host->reference_count--;
716 pthread_mutex_unlock (&host->lock);
718 return status;
719 }
721 static int
722 riemann_config(oconfig_item_t *ci)
723 {
724 int i;
725 oconfig_item_t *child;
726 int status;
728 for (i = 0; i < ci->children_num; i++) {
729 child = &ci->children[i];
731 if (strcasecmp("Node", child->key) == 0) {
732 riemann_config_node (child);
733 } else if (strcasecmp(child->key, "tag") == 0) {
734 char *tmp = NULL;
735 status = cf_util_get_string(child, &tmp);
736 if (status != 0)
737 continue;
739 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
740 DEBUG("write_riemann plugin: Got tag: %s", tmp);
741 sfree (tmp);
742 } else {
743 WARNING ("write_riemann plugin: Ignoring unknown "
744 "configuration option \"%s\" at top level.",
745 child->key);
746 }
747 }
748 return (0);
749 }
751 void
752 module_register(void)
753 {
754 plugin_register_complex_config ("write_riemann", riemann_config);
755 }
757 /* vim: set sw=8 sts=8 ts=8 noet : */