6ef27ca209c633d74387620ae06d36ad6d4c697a
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 int riemann_send(struct riemann_host *, Msg const *);
60 static int riemann_notification(const notification_t *, user_data_t *);
61 static int riemann_write(const data_set_t *, const value_list_t *, user_data_t *);
62 static int riemann_connect(struct riemann_host *);
63 static int riemann_disconnect (struct riemann_host *host);
64 static void riemann_free(void *);
65 static int riemann_config_node(oconfig_item_t *);
66 static int riemann_config(oconfig_item_t *);
67 void module_register(void);
69 static void riemann_event_protobuf_free (Event *event) /* {{{ */
70 {
71 if (event == NULL)
72 return;
74 sfree (event->state);
75 sfree (event->service);
76 sfree (event->host);
77 sfree (event->description);
79 strarray_free (event->tags, event->n_tags);
80 event->tags = NULL;
81 event->n_tags = 0;
83 sfree (event);
84 } /* }}} void riemann_event_protobuf_free */
86 static void riemann_msg_protobuf_free (Msg *msg) /* {{{ */
87 {
88 size_t i;
90 if (msg == NULL)
91 return;
93 for (i = 0; i < msg->n_events; i++)
94 {
95 riemann_event_protobuf_free (msg->events[i]);
96 msg->events[i] = NULL;
97 }
99 sfree (msg->events);
100 msg->n_events = 0;
102 sfree (msg);
103 } /* }}} void riemann_msg_protobuf_free */
105 static int
106 riemann_send(struct riemann_host *host, Msg const *msg)
107 {
108 u_char *buffer;
109 size_t buffer_len;
110 int status;
112 pthread_mutex_lock (&host->lock);
114 status = riemann_connect (host);
115 if (status != 0)
116 {
117 pthread_mutex_unlock (&host->lock);
118 return status;
119 }
121 buffer_len = msg__get_packed_size(msg);
122 if (host->use_tcp)
123 buffer_len += 4;
125 buffer = malloc (buffer_len);
126 if (buffer == NULL) {
127 pthread_mutex_unlock (&host->lock);
128 ERROR ("write_riemann plugin: malloc failed.");
129 return ENOMEM;
130 }
131 memset (buffer, 0, buffer_len);
133 if (host->use_tcp)
134 {
135 uint32_t length = htonl ((uint32_t) (buffer_len - 4));
136 memcpy (buffer, &length, 4);
137 msg__pack(msg, buffer + 4);
138 }
139 else
140 {
141 msg__pack(msg, buffer);
142 }
144 status = (int) swrite (host->s, buffer, buffer_len);
145 if (status != 0)
146 {
147 char errbuf[1024];
149 riemann_disconnect (host);
150 pthread_mutex_unlock (&host->lock);
152 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s",
153 (host->node != NULL) ? host->node : RIEMANN_HOST,
154 (host->service != NULL) ? host->service : RIEMANN_PORT,
155 sstrerror (errno, errbuf, sizeof (errbuf)));
156 sfree (buffer);
157 return -1;
158 }
160 pthread_mutex_unlock (&host->lock);
161 sfree (buffer);
162 return 0;
163 }
165 static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
166 {
167 return (strarray_add (&event->tags, &event->n_tags, tag));
168 } /* }}} int riemann_event_add_tag */
170 static int riemann_event_add_attribute (Event *event, /* {{{ */
171 char const *key, char const *value)
172 {
173 Attribute **new_attributes;
174 Attribute *a;
176 new_attributes = realloc (event->attributes,
177 sizeof (*event->attributes) * (event->n_attributes + 1));
178 if (new_attributes == NULL)
179 {
180 ERROR ("write_riemann plugin: realloc failed.");
181 return (ENOMEM);
182 }
183 event->attributes = new_attributes;
185 a = malloc (sizeof (*a));
186 if (a == NULL)
187 {
188 ERROR ("write_riemann plugin: malloc failed.");
189 return (ENOMEM);
190 }
191 attribute__init (a);
193 a->key = strdup (key);
194 if (value != NULL)
195 a->value = strdup (value);
197 event->attributes[event->n_attributes] = a;
198 event->n_attributes++;
200 return (0);
201 } /* }}} int riemann_event_add_attribute */
203 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
204 notification_t const *n)
205 {
206 Msg *msg;
207 Event *event;
208 char service_buffer[6 * DATA_MAX_NAME_LEN];
209 char const *severity;
210 notification_meta_t *meta;
211 int i;
213 msg = malloc (sizeof (*msg));
214 if (msg == NULL)
215 {
216 ERROR ("write_riemann plugin: malloc failed.");
217 return (NULL);
218 }
219 memset (msg, 0, sizeof (*msg));
220 msg__init (msg);
222 msg->events = malloc (sizeof (*msg->events));
223 if (msg->events == NULL)
224 {
225 ERROR ("write_riemann plugin: malloc failed.");
226 sfree (msg);
227 return (NULL);
228 }
230 event = malloc (sizeof (*event));
231 if (event == NULL)
232 {
233 ERROR ("write_riemann plugin: malloc failed.");
234 sfree (msg->events);
235 sfree (msg);
236 return (NULL);
237 }
238 memset (event, 0, sizeof (*event));
239 event__init (event);
241 msg->events[0] = event;
242 msg->n_events = 1;
244 event->host = strdup (n->host);
245 event->time = CDTIME_T_TO_TIME_T (n->time);
246 event->has_time = 1;
248 switch (n->severity)
249 {
250 case NOTIF_OKAY: severity = "ok"; break;
251 case NOTIF_WARNING: severity = "warning"; break;
252 case NOTIF_FAILURE: severity = "critical"; break;
253 default: severity = "unknown";
254 }
255 event->state = strdup (severity);
257 riemann_event_add_tag (event, "notification");
258 if (n->host[0] != 0)
259 riemann_event_add_attribute (event, "host", n->host);
260 if (n->plugin[0] != 0)
261 riemann_event_add_attribute (event, "plugin", n->plugin);
262 if (n->plugin_instance[0] != 0)
263 riemann_event_add_attribute (event, "plugin_instance",
264 n->plugin_instance);
266 if (n->type[0] != 0)
267 riemann_event_add_attribute (event, "type", n->type);
268 if (n->type_instance[0] != 0)
269 riemann_event_add_attribute (event, "type_instance",
270 n->type_instance);
272 for (i = 0; i < riemann_tags_num; i++)
273 riemann_event_add_tag (event, riemann_tags[i]);
275 format_name (service_buffer, sizeof (service_buffer),
276 /* host = */ "", n->plugin, n->plugin_instance,
277 n->type, n->type_instance);
278 event->service = strdup (&service_buffer[1]);
280 /* Pull in values from threshold */
281 for (meta = n->meta; meta != NULL; meta = meta->next)
282 {
283 if (strcasecmp ("CurrentValue", meta->name) != 0)
284 continue;
286 event->metric_d = meta->nm_value.nm_double;
287 event->has_metric_d = 1;
288 break;
289 }
291 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
292 "host = \"%s\", service = \"%s\", state = \"%s\"",
293 event->host, event->service, event->state);
294 return (msg);
295 } /* }}} Msg *riemann_notification_to_protobuf */
297 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
298 data_set_t const *ds,
299 value_list_t const *vl, size_t index,
300 gauge_t const *rates)
301 {
302 Event *event;
303 char name_buffer[5 * DATA_MAX_NAME_LEN];
304 char service_buffer[6 * DATA_MAX_NAME_LEN];
305 int i;
307 event = malloc (sizeof (*event));
308 if (event == NULL)
309 {
310 ERROR ("write_riemann plugin: malloc failed.");
311 return (NULL);
312 }
313 memset (event, 0, sizeof (*event));
314 event__init (event);
316 event->host = strdup (vl->host);
317 event->time = CDTIME_T_TO_TIME_T (vl->time);
318 event->has_time = 1;
319 event->ttl = CDTIME_T_TO_TIME_T (2 * vl->interval);
320 event->has_ttl = 1;
322 riemann_event_add_attribute (event, "plugin", vl->plugin);
323 if (vl->plugin_instance[0] != 0)
324 riemann_event_add_attribute (event, "plugin_instance",
325 vl->plugin_instance);
327 riemann_event_add_attribute (event, "type", vl->type);
328 if (vl->type_instance[0] != 0)
329 riemann_event_add_attribute (event, "type_instance",
330 vl->type_instance);
332 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
333 {
334 char ds_type[DATA_MAX_NAME_LEN];
336 ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
337 DS_TYPE_TO_STRING(ds->ds[index].type));
338 riemann_event_add_attribute (event, "ds_type", ds_type);
339 }
340 else
341 {
342 riemann_event_add_attribute (event, "ds_type",
343 DS_TYPE_TO_STRING(ds->ds[index].type));
344 }
345 riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
346 {
347 char ds_index[DATA_MAX_NAME_LEN];
349 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
350 riemann_event_add_attribute (event, "ds_index", ds_index);
351 }
353 for (i = 0; i < riemann_tags_num; i++)
354 riemann_event_add_tag (event, riemann_tags[i]);
356 if (ds->ds[index].type == DS_TYPE_GAUGE)
357 {
358 event->has_metric_d = 1;
359 event->metric_d = (double) vl->values[index].gauge;
360 }
361 else if (rates != NULL)
362 {
363 event->has_metric_d = 1;
364 event->metric_d = (double) rates[index];
365 }
366 else
367 {
368 event->has_metric_sint64 = 1;
369 if (ds->ds[index].type == DS_TYPE_DERIVE)
370 event->metric_sint64 = (int64_t) vl->values[index].derive;
371 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
372 event->metric_sint64 = (int64_t) vl->values[index].absolute;
373 else
374 event->metric_sint64 = (int64_t) vl->values[index].counter;
375 }
377 format_name (name_buffer, sizeof (name_buffer),
378 /* host = */ "", vl->plugin, vl->plugin_instance,
379 vl->type, vl->type_instance);
380 if (host->always_append_ds || (ds->ds_num > 1))
381 ssnprintf (service_buffer, sizeof (service_buffer),
382 "%s/%s", &name_buffer[1], ds->ds[index].name);
383 else
384 sstrncpy (service_buffer, &name_buffer[1],
385 sizeof (service_buffer));
387 event->service = strdup (service_buffer);
389 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
390 "host = \"%s\", service = \"%s\"",
391 event->host, event->service);
392 return (event);
393 } /* }}} Event *riemann_value_to_protobuf */
395 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
396 data_set_t const *ds,
397 value_list_t const *vl)
398 {
399 Msg *msg;
400 size_t i;
401 gauge_t *rates = NULL;
403 /* Initialize the Msg structure. */
404 msg = malloc (sizeof (*msg));
405 if (msg == NULL)
406 {
407 ERROR ("write_riemann plugin: malloc failed.");
408 return (NULL);
409 }
410 memset (msg, 0, sizeof (*msg));
411 msg__init (msg);
413 /* Set up events. First, the list of pointers. */
414 msg->n_events = (size_t) vl->values_len;
415 msg->events = calloc (msg->n_events, sizeof (*msg->events));
416 if (msg->events == NULL)
417 {
418 ERROR ("write_riemann plugin: calloc failed.");
419 riemann_msg_protobuf_free (msg);
420 return (NULL);
421 }
423 if (host->store_rates)
424 {
425 rates = uc_get_rate (ds, vl);
426 if (rates == NULL)
427 {
428 ERROR ("write_riemann plugin: uc_get_rate failed.");
429 riemann_msg_protobuf_free (msg);
430 return (NULL);
431 }
432 }
434 for (i = 0; i < msg->n_events; i++)
435 {
436 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
437 (int) i, rates);
438 if (msg->events[i] == NULL)
439 {
440 riemann_msg_protobuf_free (msg);
441 sfree (rates);
442 return (NULL);
443 }
444 }
446 sfree (rates);
447 return (msg);
448 } /* }}} Msg *riemann_value_list_to_protobuf */
450 static int
451 riemann_notification(const notification_t *n, user_data_t *ud)
452 {
453 int status;
454 struct riemann_host *host = ud->data;
455 Msg *msg;
457 msg = riemann_notification_to_protobuf (host, n);
458 if (msg == NULL)
459 return (-1);
461 status = riemann_send (host, msg);
462 if (status != 0)
463 ERROR ("write_riemann plugin: riemann_send failed with status %i",
464 status);
466 riemann_msg_protobuf_free (msg);
467 return (status);
468 } /* }}} int riemann_notification */
470 static int
471 riemann_write(const data_set_t *ds,
472 const value_list_t *vl,
473 user_data_t *ud)
474 {
475 int status;
476 struct riemann_host *host = ud->data;
477 Msg *msg;
479 msg = riemann_value_list_to_protobuf (host, ds, vl);
480 if (msg == NULL)
481 return (-1);
483 status = riemann_send (host, msg);
484 if (status != 0)
485 ERROR ("write_riemann plugin: riemann_send failed with status %i",
486 status);
488 riemann_msg_protobuf_free (msg);
489 return status;
490 }
492 /* host->lock must be held when calling this function. */
493 static int
494 riemann_connect(struct riemann_host *host)
495 {
496 int e;
497 struct addrinfo *ai, *res, hints;
498 char const *node;
499 char const *service;
501 if (host->flags & F_CONNECT)
502 return 0;
504 memset(&hints, 0, sizeof(hints));
505 memset(&service, 0, sizeof(service));
506 hints.ai_family = AF_UNSPEC;
507 hints.ai_socktype = host->use_tcp ? SOCK_STREAM : SOCK_DGRAM;
508 #ifdef AI_ADDRCONFIG
509 hints.ai_flags |= AI_ADDRCONFIG;
510 #endif
512 node = (host->node != NULL) ? host->node : RIEMANN_HOST;
513 service = (host->service != NULL) ? host->service : RIEMANN_PORT;
515 if ((e = getaddrinfo(node, service, &hints, &res)) != 0) {
516 ERROR ("write_riemann plugin: Unable to resolve host \"%s\": %s",
517 node, gai_strerror(e));
518 return -1;
519 }
521 host->s = -1;
522 for (ai = res; ai != NULL; ai = ai->ai_next) {
523 if ((host->s = socket(ai->ai_family,
524 ai->ai_socktype,
525 ai->ai_protocol)) == -1) {
526 continue;
527 }
529 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
530 close(host->s);
531 host->s = -1;
532 continue;
533 }
535 host->flags |= F_CONNECT;
536 DEBUG("write_riemann plugin: got a succesful connection for: %s:%s",
537 node, service);
538 break;
539 }
541 freeaddrinfo(res);
543 if (host->s < 0) {
544 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
545 node, service);
546 return -1;
547 }
548 return 0;
549 }
551 /* host->lock must be held when calling this function. */
552 static int
553 riemann_disconnect (struct riemann_host *host)
554 {
555 if ((host->flags & F_CONNECT) == 0)
556 return (0);
558 close (host->s);
559 host->s = -1;
560 host->flags &= ~F_CONNECT;
562 return (0);
563 }
565 static void
566 riemann_free(void *p)
567 {
568 struct riemann_host *host = p;
570 if (host == NULL)
571 return;
573 pthread_mutex_lock (&host->lock);
575 host->reference_count--;
576 if (host->reference_count > 0)
577 {
578 pthread_mutex_unlock (&host->lock);
579 return;
580 }
582 riemann_disconnect (host);
584 sfree(host->service);
585 pthread_mutex_destroy (&host->lock);
586 sfree(host);
587 }
589 static int
590 riemann_config_node(oconfig_item_t *ci)
591 {
592 struct riemann_host *host = NULL;
593 int status = 0;
594 int i;
595 oconfig_item_t *child;
596 char callback_name[DATA_MAX_NAME_LEN];
597 user_data_t ud;
599 if ((host = calloc(1, sizeof (*host))) == NULL) {
600 ERROR ("write_riemann plugin: calloc failed.");
601 return ENOMEM;
602 }
603 pthread_mutex_init (&host->lock, NULL);
604 host->reference_count = 1;
605 host->node = NULL;
606 host->service = NULL;
607 host->store_rates = 1;
608 host->always_append_ds = 0;
609 host->use_tcp = 0;
611 status = cf_util_get_string (ci, &host->name);
612 if (status != 0) {
613 WARNING("write_riemann plugin: Required host name is missing.");
614 riemann_free (host);
615 return -1;
616 }
618 for (i = 0; i < ci->children_num; i++) {
619 /*
620 * The code here could be simplified but makes room
621 * for easy adding of new options later on.
622 */
623 child = &ci->children[i];
624 status = 0;
626 if (strcasecmp ("Host", child->key) == 0) {
627 status = cf_util_get_string (child, &host->node);
628 if (status != 0)
629 break;
630 } else if (strcasecmp ("Port", child->key) == 0) {
631 status = cf_util_get_service (child, &host->service);
632 if (status != 0) {
633 ERROR ("write_riemann plugin: Invalid argument "
634 "configured for the \"Port\" "
635 "option.");
636 break;
637 }
638 } else if (strcasecmp ("Protocol", child->key) == 0) {
639 char tmp[16];
640 status = cf_util_get_string_buffer (child,
641 tmp, sizeof (tmp));
642 if (status != 0)
643 {
644 ERROR ("write_riemann plugin: cf_util_get_"
645 "string_buffer failed with "
646 "status %i.", status);
647 break;
648 }
650 if (strcasecmp ("UDP", tmp) == 0)
651 host->use_tcp = 0;
652 else if (strcasecmp ("TCP", tmp) == 0)
653 host->use_tcp = 1;
654 else
655 WARNING ("write_riemann plugin: The value "
656 "\"%s\" is not valid for the "
657 "\"Protocol\" option. Use "
658 "either \"UDP\" or \"TCP\".",
659 tmp);
660 } else if (strcasecmp ("StoreRates", child->key) == 0) {
661 status = cf_util_get_boolean (child, &host->store_rates);
662 if (status != 0)
663 break;
664 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
665 status = cf_util_get_boolean (child,
666 &host->always_append_ds);
667 if (status != 0)
668 break;
669 } else {
670 WARNING("write_riemann plugin: ignoring unknown config "
671 "option: \"%s\"", child->key);
672 }
673 }
674 if (status != 0) {
675 riemann_free (host);
676 return status;
677 }
679 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
680 host->name);
681 ud.data = host;
682 ud.free_func = riemann_free;
684 pthread_mutex_lock (&host->lock);
686 status = plugin_register_write (callback_name, riemann_write, &ud);
687 if (status != 0)
688 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
689 "failed with status %i.",
690 callback_name, status);
691 else /* success */
692 host->reference_count++;
694 status = plugin_register_notification (callback_name,
695 riemann_notification, &ud);
696 if (status != 0)
697 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
698 "failed with status %i.",
699 callback_name, status);
700 else /* success */
701 host->reference_count++;
703 if (host->reference_count <= 1)
704 {
705 /* Both callbacks failed => free memory.
706 * We need to unlock here, because riemann_free() will lock.
707 * This is not a race condition, because we're the only one
708 * holding a reference. */
709 pthread_mutex_unlock (&host->lock);
710 riemann_free (host);
711 return (-1);
712 }
714 host->reference_count--;
715 pthread_mutex_unlock (&host->lock);
717 return status;
718 }
720 static int
721 riemann_config(oconfig_item_t *ci)
722 {
723 int i;
724 oconfig_item_t *child;
725 int status;
727 for (i = 0; i < ci->children_num; i++) {
728 child = &ci->children[i];
730 if (strcasecmp("Node", child->key) == 0) {
731 riemann_config_node (child);
732 } else if (strcasecmp(child->key, "tag") == 0) {
733 char *tmp = NULL;
734 status = cf_util_get_string(child, &tmp);
735 if (status != 0)
736 continue;
738 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
739 DEBUG("write_riemann plugin: Got tag: %s", tmp);
740 sfree (tmp);
741 } else {
742 WARNING ("write_riemann plugin: Ignoring unknown "
743 "configuration option \"%s\" at top level.",
744 child->key);
745 }
746 }
747 return (0);
748 }
750 void
751 module_register(void)
752 {
753 plugin_register_complex_config ("write_riemann", riemann_config);
754 }
756 /* vim: set sw=8 sts=8 ts=8 noet : */