1 /**
2 * collectd - src/write_riemann.c
3 * Copyright (C) 2012,2013 Pierre-Yves Ritschard
4 * Copyright (C) 2013 Florian octo Forster
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Pierre-Yves Ritschard <pyr at spootnik.org>
26 * Florian octo Forster <octo at collectd.org>
27 */
29 #include "collectd.h"
30 #include "plugin.h"
31 #include "common.h"
32 #include "configfile.h"
33 #include "utils_cache.h"
34 #include "riemann.pb-c.h"
36 #include <sys/socket.h>
37 #include <arpa/inet.h>
38 #include <errno.h>
39 #include <netdb.h>
40 #include <inttypes.h>
41 #include <pthread.h>
43 #define RIEMANN_HOST "localhost"
44 #define RIEMANN_PORT "5555"
45 #define RIEMANN_TTL_FACTOR 2.0
46 #define RIEMANN_BATCH_MAX 8192
48 int write_riemann_threshold_check(const data_set_t *, const value_list_t *, int *);
50 struct riemann_host {
51 char *name;
52 char *event_service_prefix;
53 #define F_CONNECT 0x01
54 uint8_t flags;
55 pthread_mutex_t lock;
56 _Bool batch_mode;
57 _Bool notifications;
58 _Bool check_thresholds;
59 _Bool store_rates;
60 _Bool always_append_ds;
61 char *node;
62 char *service;
63 _Bool use_tcp;
64 int s;
65 double ttl_factor;
66 Msg *batch_msg;
67 cdtime_t batch_init;
68 int batch_max;
69 int reference_count;
70 };
72 static char **riemann_tags;
73 static size_t riemann_tags_num;
74 static char **riemann_attrs;
75 static size_t riemann_attrs_num;
77 static void riemann_event_protobuf_free (Event *event) /* {{{ */
78 {
79 size_t i;
81 if (event == NULL)
82 return;
84 sfree (event->state);
85 sfree (event->service);
86 sfree (event->host);
87 sfree (event->description);
89 strarray_free (event->tags, event->n_tags);
90 event->tags = NULL;
91 event->n_tags = 0;
93 for (i = 0; i < event->n_attributes; i++)
94 {
95 sfree (event->attributes[i]->key);
96 sfree (event->attributes[i]->value);
97 sfree (event->attributes[i]);
98 }
99 sfree (event->attributes);
100 event->n_attributes = 0;
102 sfree (event);
103 } /* }}} void riemann_event_protobuf_free */
105 static void riemann_msg_protobuf_free(Msg *msg) /* {{{ */
106 {
107 size_t i;
109 if (msg == NULL)
110 return;
112 for (i = 0; i < msg->n_events; i++)
113 {
114 riemann_event_protobuf_free (msg->events[i]);
115 msg->events[i] = NULL;
116 }
118 sfree (msg->events);
119 msg->n_events = 0;
121 sfree (msg);
122 } /* }}} void riemann_msg_protobuf_free */
124 /* host->lock must be held when calling this function. */
125 static int riemann_connect(struct riemann_host *host) /* {{{ */
126 {
127 int e;
128 struct addrinfo *ai, *res, hints;
129 char const *node;
130 char const *service;
132 if (host->flags & F_CONNECT)
133 return 0;
135 memset(&hints, 0, sizeof(hints));
136 memset(&service, 0, sizeof(service));
137 hints.ai_family = AF_UNSPEC;
138 hints.ai_socktype = host->use_tcp ? SOCK_STREAM : SOCK_DGRAM;
139 #ifdef AI_ADDRCONFIG
140 hints.ai_flags |= AI_ADDRCONFIG;
141 #endif
143 node = (host->node != NULL) ? host->node : RIEMANN_HOST;
144 service = (host->service != NULL) ? host->service : RIEMANN_PORT;
146 if ((e = getaddrinfo(node, service, &hints, &res)) != 0) {
147 ERROR ("write_riemann plugin: Unable to resolve host \"%s\": %s",
148 node, gai_strerror(e));
149 return -1;
150 }
152 host->s = -1;
153 for (ai = res; ai != NULL; ai = ai->ai_next) {
154 if ((host->s = socket(ai->ai_family,
155 ai->ai_socktype,
156 ai->ai_protocol)) == -1) {
157 continue;
158 }
160 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
161 close(host->s);
162 host->s = -1;
163 continue;
164 }
166 host->flags |= F_CONNECT;
167 DEBUG("write_riemann plugin: got a successful connection for: %s:%s",
168 node, service);
169 break;
170 }
172 freeaddrinfo(res);
174 if (host->s < 0) {
175 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
176 node, service);
177 return -1;
178 }
179 return 0;
180 } /* }}} int riemann_connect */
182 /* host->lock must be held when calling this function. */
183 static int riemann_disconnect (struct riemann_host *host) /* {{{ */
184 {
185 if ((host->flags & F_CONNECT) == 0)
186 return (0);
188 close (host->s);
189 host->s = -1;
190 host->flags &= ~F_CONNECT;
192 return (0);
193 } /* }}} int riemann_disconnect */
195 static int riemann_send_msg (struct riemann_host *host, const Msg *msg) /* {{{ */
196 {
197 int status = 0;
198 u_char *buffer = NULL;
199 size_t buffer_len;
201 status = riemann_connect (host);
202 if (status != 0)
203 return status;
205 buffer_len = msg__get_packed_size(msg);
207 if (host->use_tcp)
208 buffer_len += 4;
210 buffer = malloc (buffer_len);
211 if (buffer == NULL) {
212 ERROR ("write_riemann plugin: malloc failed.");
213 return ENOMEM;
214 }
215 memset (buffer, 0, buffer_len);
217 if (host->use_tcp)
218 {
219 uint32_t length = htonl ((uint32_t) (buffer_len - 4));
220 memcpy (buffer, &length, 4);
221 msg__pack(msg, buffer + 4);
222 }
223 else
224 {
225 msg__pack(msg, buffer);
226 }
228 status = (int) swrite (host->s, buffer, buffer_len);
229 if (status != 0)
230 {
231 char errbuf[1024];
232 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s",
233 (host->node != NULL) ? host->node : RIEMANN_HOST,
234 (host->service != NULL) ? host->service : RIEMANN_PORT,
235 sstrerror (errno, errbuf, sizeof (errbuf)));
236 sfree (buffer);
237 return -1;
238 }
240 sfree (buffer);
241 return 0;
242 } /* }}} int riemann_send_msg */
244 static int riemann_recv_ack(struct riemann_host *host) /* {{{ */
245 {
246 int status = 0;
247 Msg *msg = NULL;
248 uint32_t header;
250 status = (int) sread (host->s, &header, 4);
252 if (status != 0)
253 return -1;
255 size_t size = ntohl(header);
257 // Buffer on the stack since acknowledges are typically small.
258 u_char buffer[size];
259 memset (buffer, 0, size);
261 status = (int) sread (host->s, buffer, size);
263 if (status != 0)
264 return status;
266 msg = msg__unpack (NULL, size, buffer);
268 if (msg == NULL)
269 return -1;
271 if (!msg->ok)
272 {
273 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s acknowledgement message reported error: %s",
274 (host->node != NULL) ? host->node : RIEMANN_HOST,
275 (host->service != NULL) ? host->service : RIEMANN_PORT,
276 msg->error);
278 msg__free_unpacked(msg, NULL);
279 return -1;
280 }
282 msg__free_unpacked (msg, NULL);
283 return 0;
284 } /* }}} int riemann_recv_ack */
286 /**
287 * Function to send messages (Msg) to riemann.
288 *
289 * Acquires the host lock, disconnects on errors.
290 */
291 static int riemann_send(struct riemann_host *host, Msg const *msg) /* {{{ */
292 {
293 int status = 0;
294 pthread_mutex_lock (&host->lock);
296 status = riemann_send_msg(host, msg);
297 if (status != 0) {
298 riemann_disconnect (host);
299 pthread_mutex_unlock (&host->lock);
300 return status;
301 }
303 /*
304 * For TCP we need to receive message acknowledgemenent.
305 */
306 if (host->use_tcp)
307 {
308 status = riemann_recv_ack(host);
310 if (status != 0)
311 {
312 riemann_disconnect (host);
313 pthread_mutex_unlock (&host->lock);
314 return status;
315 }
316 }
318 pthread_mutex_unlock (&host->lock);
319 return 0;
320 } /* }}} int riemann_send */
322 static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
323 {
324 return (strarray_add (&event->tags, &event->n_tags, tag));
325 } /* }}} int riemann_event_add_tag */
327 static int riemann_event_add_attribute(Event *event, /* {{{ */
328 char const *key, char const *value)
329 {
330 Attribute **new_attributes;
331 Attribute *a;
333 new_attributes = realloc (event->attributes,
334 sizeof (*event->attributes) * (event->n_attributes + 1));
335 if (new_attributes == NULL)
336 {
337 ERROR ("write_riemann plugin: realloc failed.");
338 return (ENOMEM);
339 }
340 event->attributes = new_attributes;
342 a = malloc (sizeof (*a));
343 if (a == NULL)
344 {
345 ERROR ("write_riemann plugin: malloc failed.");
346 return (ENOMEM);
347 }
348 attribute__init (a);
350 a->key = strdup (key);
351 if (value != NULL)
352 a->value = strdup (value);
354 event->attributes[event->n_attributes] = a;
355 event->n_attributes++;
357 return (0);
358 } /* }}} int riemann_event_add_attribute */
360 static Msg *riemann_notification_to_protobuf(struct riemann_host *host, /* {{{ */
361 notification_t const *n)
362 {
363 Msg *msg;
364 Event *event;
365 char service_buffer[6 * DATA_MAX_NAME_LEN];
366 char const *severity;
367 notification_meta_t *meta;
368 size_t i;
370 msg = malloc (sizeof (*msg));
371 if (msg == NULL)
372 {
373 ERROR ("write_riemann plugin: malloc failed.");
374 return (NULL);
375 }
376 memset (msg, 0, sizeof (*msg));
377 msg__init (msg);
379 msg->events = malloc (sizeof (*msg->events));
380 if (msg->events == NULL)
381 {
382 ERROR ("write_riemann plugin: malloc failed.");
383 sfree (msg);
384 return (NULL);
385 }
387 event = malloc (sizeof (*event));
388 if (event == NULL)
389 {
390 ERROR ("write_riemann plugin: malloc failed.");
391 sfree (msg->events);
392 sfree (msg);
393 return (NULL);
394 }
395 memset (event, 0, sizeof (*event));
396 event__init (event);
398 msg->events[0] = event;
399 msg->n_events = 1;
401 event->host = strdup (n->host);
402 event->time = CDTIME_T_TO_TIME_T (n->time);
403 event->has_time = 1;
405 switch (n->severity)
406 {
407 case NOTIF_OKAY: severity = "ok"; break;
408 case NOTIF_WARNING: severity = "warning"; break;
409 case NOTIF_FAILURE: severity = "critical"; break;
410 default: severity = "unknown";
411 }
412 event->state = strdup (severity);
414 riemann_event_add_tag (event, "notification");
415 if (n->host[0] != 0)
416 riemann_event_add_attribute (event, "host", n->host);
417 if (n->plugin[0] != 0)
418 riemann_event_add_attribute (event, "plugin", n->plugin);
419 if (n->plugin_instance[0] != 0)
420 riemann_event_add_attribute (event, "plugin_instance",
421 n->plugin_instance);
423 if (n->type[0] != 0)
424 riemann_event_add_attribute (event, "type", n->type);
425 if (n->type_instance[0] != 0)
426 riemann_event_add_attribute (event, "type_instance",
427 n->type_instance);
429 for (i = 0; i < riemann_attrs_num; i += 2)
430 riemann_event_add_attribute(event,
431 riemann_attrs[i],
432 riemann_attrs[i +1]);
434 for (i = 0; i < riemann_tags_num; i++)
435 riemann_event_add_tag (event, riemann_tags[i]);
437 format_name (service_buffer, sizeof (service_buffer),
438 /* host = */ "", n->plugin, n->plugin_instance,
439 n->type, n->type_instance);
440 event->service = strdup (&service_buffer[1]);
442 if (n->message[0] != 0)
443 riemann_event_add_attribute (event, "description", n->message);
445 /* Pull in values from threshold and add extra attributes */
446 for (meta = n->meta; meta != NULL; meta = meta->next)
447 {
448 if (strcasecmp ("CurrentValue", meta->name) == 0 && meta->type == NM_TYPE_DOUBLE)
449 {
450 event->metric_d = meta->nm_value.nm_double;
451 event->has_metric_d = 1;
452 continue;
453 }
455 if (meta->type == NM_TYPE_STRING) {
456 riemann_event_add_attribute (event, meta->name, meta->nm_value.nm_string);
457 continue;
458 }
459 }
461 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
462 "host = \"%s\", service = \"%s\", state = \"%s\"",
463 event->host, event->service, event->state);
464 return (msg);
465 } /* }}} Msg *riemann_notification_to_protobuf */
467 static Event *riemann_value_to_protobuf(struct riemann_host const *host, /* {{{ */
468 data_set_t const *ds,
469 value_list_t const *vl, size_t index,
470 gauge_t const *rates,
471 int status)
472 {
473 Event *event;
474 char name_buffer[5 * DATA_MAX_NAME_LEN];
475 char service_buffer[6 * DATA_MAX_NAME_LEN];
476 double ttl;
477 size_t i;
479 event = malloc (sizeof (*event));
480 if (event == NULL)
481 {
482 ERROR ("write_riemann plugin: malloc failed.");
483 return (NULL);
484 }
485 memset (event, 0, sizeof (*event));
486 event__init (event);
488 event->host = strdup (vl->host);
489 event->time = CDTIME_T_TO_TIME_T (vl->time);
490 event->has_time = 1;
492 if (host->check_thresholds) {
493 switch (status) {
494 case STATE_OKAY:
495 event->state = strdup("ok");
496 break;
497 case STATE_ERROR:
498 event->state = strdup("critical");
499 break;
500 case STATE_WARNING:
501 event->state = strdup("warning");
502 break;
503 case STATE_MISSING:
504 event->state = strdup("unknown");
505 break;
506 }
507 }
509 ttl = CDTIME_T_TO_DOUBLE (vl->interval) * host->ttl_factor;
510 event->ttl = (float) ttl;
511 event->has_ttl = 1;
513 riemann_event_add_attribute (event, "plugin", vl->plugin);
514 if (vl->plugin_instance[0] != 0)
515 riemann_event_add_attribute (event, "plugin_instance",
516 vl->plugin_instance);
518 riemann_event_add_attribute (event, "type", vl->type);
519 if (vl->type_instance[0] != 0)
520 riemann_event_add_attribute (event, "type_instance",
521 vl->type_instance);
523 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
524 {
525 char ds_type[DATA_MAX_NAME_LEN];
527 ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
528 DS_TYPE_TO_STRING(ds->ds[index].type));
529 riemann_event_add_attribute (event, "ds_type", ds_type);
530 }
531 else
532 {
533 riemann_event_add_attribute (event, "ds_type",
534 DS_TYPE_TO_STRING(ds->ds[index].type));
535 }
536 riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
537 {
538 char ds_index[DATA_MAX_NAME_LEN];
540 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
541 riemann_event_add_attribute (event, "ds_index", ds_index);
542 }
544 for (i = 0; i < riemann_attrs_num; i += 2)
545 riemann_event_add_attribute(event,
546 riemann_attrs[i],
547 riemann_attrs[i +1]);
549 for (i = 0; i < riemann_tags_num; i++)
550 riemann_event_add_tag (event, riemann_tags[i]);
552 if (ds->ds[index].type == DS_TYPE_GAUGE)
553 {
554 event->has_metric_d = 1;
555 event->metric_d = (double) vl->values[index].gauge;
556 }
557 else if (rates != NULL)
558 {
559 event->has_metric_d = 1;
560 event->metric_d = (double) rates[index];
561 }
562 else
563 {
564 event->has_metric_sint64 = 1;
565 if (ds->ds[index].type == DS_TYPE_DERIVE)
566 event->metric_sint64 = (int64_t) vl->values[index].derive;
567 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
568 event->metric_sint64 = (int64_t) vl->values[index].absolute;
569 else
570 event->metric_sint64 = (int64_t) vl->values[index].counter;
571 }
573 format_name (name_buffer, sizeof (name_buffer),
574 /* host = */ "", vl->plugin, vl->plugin_instance,
575 vl->type, vl->type_instance);
576 if (host->always_append_ds || (ds->ds_num > 1))
577 {
578 if (host->event_service_prefix == NULL)
579 ssnprintf (service_buffer, sizeof (service_buffer), "%s/%s",
580 &name_buffer[1], ds->ds[index].name);
581 else
582 ssnprintf (service_buffer, sizeof (service_buffer), "%s%s/%s",
583 host->event_service_prefix, &name_buffer[1], ds->ds[index].name);
584 }
585 else
586 {
587 if (host->event_service_prefix == NULL)
588 sstrncpy (service_buffer, &name_buffer[1], sizeof (service_buffer));
589 else
590 ssnprintf (service_buffer, sizeof (service_buffer), "%s%s",
591 host->event_service_prefix, &name_buffer[1]);
592 }
594 event->service = strdup (service_buffer);
596 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
597 "host = \"%s\", service = \"%s\"",
598 event->host, event->service);
599 return (event);
600 } /* }}} Event *riemann_value_to_protobuf */
602 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
603 data_set_t const *ds,
604 value_list_t const *vl,
605 int *statuses)
606 {
607 Msg *msg;
608 size_t i;
609 gauge_t *rates = NULL;
611 /* Initialize the Msg structure. */
612 msg = malloc (sizeof (*msg));
613 if (msg == NULL)
614 {
615 ERROR ("write_riemann plugin: malloc failed.");
616 return (NULL);
617 }
618 memset (msg, 0, sizeof (*msg));
619 msg__init (msg);
621 /* Set up events. First, the list of pointers. */
622 msg->n_events = (size_t) vl->values_len;
623 msg->events = calloc (msg->n_events, sizeof (*msg->events));
624 if (msg->events == NULL)
625 {
626 ERROR ("write_riemann plugin: calloc failed.");
627 riemann_msg_protobuf_free (msg);
628 return (NULL);
629 }
631 if (host->store_rates)
632 {
633 rates = uc_get_rate (ds, vl);
634 if (rates == NULL)
635 {
636 ERROR ("write_riemann plugin: uc_get_rate failed.");
637 riemann_msg_protobuf_free (msg);
638 return (NULL);
639 }
640 }
642 for (i = 0; i < msg->n_events; i++)
643 {
644 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
645 (int) i, rates, statuses[i]);
646 if (msg->events[i] == NULL)
647 {
648 riemann_msg_protobuf_free (msg);
649 sfree (rates);
650 return (NULL);
651 }
652 }
654 sfree (rates);
655 return (msg);
656 } /* }}} Msg *riemann_value_list_to_protobuf */
659 /*
660 * Always call while holding host->lock !
661 */
662 static int riemann_batch_flush_nolock (cdtime_t timeout,
663 struct riemann_host *host)
664 {
665 cdtime_t now;
666 int status = 0;
668 if (timeout > 0) {
669 now = cdtime ();
670 if ((host->batch_init + timeout) > now)
671 return status;
672 }
673 riemann_send_msg(host, host->batch_msg);
674 riemann_msg_protobuf_free(host->batch_msg);
676 if (host->use_tcp && ((status = riemann_recv_ack(host)) != 0))
677 riemann_disconnect (host);
679 host->batch_init = cdtime();
680 host->batch_msg = NULL;
681 return status;
682 }
684 static int riemann_batch_flush (cdtime_t timeout,
685 const char *identifier __attribute__((unused)),
686 user_data_t *user_data)
687 {
688 struct riemann_host *host;
689 int status;
691 if (user_data == NULL)
692 return (-EINVAL);
694 host = user_data->data;
695 pthread_mutex_lock (&host->lock);
696 status = riemann_batch_flush_nolock (timeout, host);
697 if (status != 0)
698 ERROR ("write_riemann plugin: riemann_send failed with status %i",
699 status);
701 pthread_mutex_unlock(&host->lock);
702 return status;
703 }
705 static int riemann_batch_add_value_list (struct riemann_host *host, /* {{{ */
706 data_set_t const *ds,
707 value_list_t const *vl,
708 int *statuses)
709 {
710 size_t i;
711 Event **events;
712 Msg *msg;
713 size_t len;
714 int ret;
716 msg = riemann_value_list_to_protobuf (host, ds, vl, statuses);
717 if (msg == NULL)
718 return -1;
720 pthread_mutex_lock(&host->lock);
722 if (host->batch_msg == NULL) {
723 host->batch_msg = msg;
724 } else {
725 len = msg->n_events + host->batch_msg->n_events;
726 events = realloc(host->batch_msg->events,
727 (len * sizeof(*host->batch_msg->events)));
728 if (events == NULL) {
729 pthread_mutex_unlock(&host->lock);
730 ERROR ("write_riemann plugin: out of memory");
731 riemann_msg_protobuf_free (msg);
732 return -1;
733 }
734 host->batch_msg->events = events;
736 for (i = host->batch_msg->n_events; i < len; i++)
737 host->batch_msg->events[i] = msg->events[i - host->batch_msg->n_events];
739 host->batch_msg->n_events = len;
740 sfree (msg->events);
741 msg->n_events = 0;
742 sfree (msg);
743 }
745 len = msg__get_packed_size(host->batch_msg);
746 ret = 0;
747 if ((host->batch_max < 0) || (((size_t) host->batch_max) <= len)) {
748 ret = riemann_batch_flush_nolock(0, host);
749 }
751 pthread_mutex_unlock(&host->lock);
752 return ret;
753 } /* }}} Msg *riemann_batch_add_value_list */
755 static int riemann_notification(const notification_t *n, user_data_t *ud) /* {{{ */
756 {
757 int status;
758 struct riemann_host *host = ud->data;
759 Msg *msg;
761 if (!host->notifications)
762 return 0;
764 /*
765 * Never batch for notifications, send them ASAP
766 */
767 msg = riemann_notification_to_protobuf (host, n);
768 if (msg == NULL)
769 return (-1);
771 status = riemann_send (host, msg);
772 if (status != 0)
773 ERROR ("write_riemann plugin: riemann_send failed with status %i",
774 status);
776 riemann_msg_protobuf_free (msg);
777 return (status);
778 } /* }}} int riemann_notification */
780 static int riemann_write(const data_set_t *ds, /* {{{ */
781 const value_list_t *vl,
782 user_data_t *ud)
783 {
784 int status = 0;
785 int statuses[vl->values_len];
786 struct riemann_host *host = ud->data;
788 if (host->check_thresholds) {
789 status = write_riemann_threshold_check(ds, vl, statuses);
790 if (status != 0)
791 return status;
792 }
794 if (host->use_tcp == 1 && host->batch_mode) {
795 riemann_batch_add_value_list (host, ds, vl, statuses);
796 } else {
797 Msg *msg = riemann_value_list_to_protobuf (host, ds, vl, statuses);
798 if (msg == NULL)
799 return (-1);
801 status = riemann_send (host, msg);
802 if (status != 0)
803 ERROR ("write_riemann plugin: riemann_send failed with status %i", status);
805 riemann_msg_protobuf_free (msg);
806 }
808 return status;
809 } /* }}} int riemann_write */
811 static void riemann_free(void *p) /* {{{ */
812 {
813 struct riemann_host *host = p;
815 if (host == NULL)
816 return;
818 pthread_mutex_lock (&host->lock);
820 host->reference_count--;
821 if (host->reference_count > 0)
822 {
823 pthread_mutex_unlock (&host->lock);
824 return;
825 }
827 riemann_disconnect (host);
829 sfree(host->service);
830 pthread_mutex_destroy (&host->lock);
831 sfree(host);
832 } /* }}} void riemann_free */
834 static int riemann_config_node(oconfig_item_t *ci) /* {{{ */
835 {
836 struct riemann_host *host = NULL;
837 int status = 0;
838 int i;
839 oconfig_item_t *child;
840 char callback_name[DATA_MAX_NAME_LEN];
841 user_data_t ud;
843 if ((host = calloc(1, sizeof (*host))) == NULL) {
844 ERROR ("write_riemann plugin: calloc failed.");
845 return ENOMEM;
846 }
847 pthread_mutex_init (&host->lock, NULL);
848 host->reference_count = 1;
849 host->node = NULL;
850 host->service = NULL;
851 host->notifications = 1;
852 host->check_thresholds = 0;
853 host->store_rates = 1;
854 host->always_append_ds = 0;
855 host->use_tcp = 1;
856 host->batch_mode = 1;
857 host->batch_max = RIEMANN_BATCH_MAX; /* typical MSS */
858 host->batch_init = cdtime();
859 host->ttl_factor = RIEMANN_TTL_FACTOR;
861 status = cf_util_get_string (ci, &host->name);
862 if (status != 0) {
863 WARNING("write_riemann plugin: Required host name is missing.");
864 riemann_free (host);
865 return -1;
866 }
868 for (i = 0; i < ci->children_num; i++) {
869 /*
870 * The code here could be simplified but makes room
871 * for easy adding of new options later on.
872 */
873 child = &ci->children[i];
874 status = 0;
876 if (strcasecmp ("Host", child->key) == 0) {
877 status = cf_util_get_string (child, &host->node);
878 if (status != 0)
879 break;
880 } else if (strcasecmp ("Notifications", child->key) == 0) {
881 status = cf_util_get_boolean(child, &host->notifications);
882 if (status != 0)
883 break;
884 } else if (strcasecmp ("EventServicePrefix", child->key) == 0) {
885 status = cf_util_get_string (child, &host->event_service_prefix);
886 if (status != 0)
887 break;
888 } else if (strcasecmp ("CheckThresholds", child->key) == 0) {
889 status = cf_util_get_boolean(child, &host->check_thresholds);
890 if (status != 0)
891 break;
892 } else if (strcasecmp ("Batch", child->key) == 0) {
893 status = cf_util_get_boolean(child, &host->batch_mode);
894 if (status != 0)
895 break;
896 } else if (strcasecmp("BatchMaxSize", child->key) == 0) {
897 status = cf_util_get_int(child, &host->batch_max);
898 if (status != 0)
899 break;
900 } else if (strcasecmp ("Port", child->key) == 0) {
901 status = cf_util_get_service (child, &host->service);
902 if (status != 0) {
903 ERROR ("write_riemann plugin: Invalid argument "
904 "configured for the \"Port\" "
905 "option.");
906 break;
907 }
908 } else if (strcasecmp ("Protocol", child->key) == 0) {
909 char tmp[16];
910 status = cf_util_get_string_buffer (child,
911 tmp, sizeof (tmp));
912 if (status != 0)
913 {
914 ERROR ("write_riemann plugin: cf_util_get_"
915 "string_buffer failed with "
916 "status %i.", status);
917 break;
918 }
920 if (strcasecmp ("UDP", tmp) == 0)
921 host->use_tcp = 0;
922 else if (strcasecmp ("TCP", tmp) == 0)
923 host->use_tcp = 1;
924 else
925 WARNING ("write_riemann plugin: The value "
926 "\"%s\" is not valid for the "
927 "\"Protocol\" option. Use "
928 "either \"UDP\" or \"TCP\".",
929 tmp);
930 } else if (strcasecmp ("StoreRates", child->key) == 0) {
931 status = cf_util_get_boolean (child, &host->store_rates);
932 if (status != 0)
933 break;
934 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
935 status = cf_util_get_boolean (child,
936 &host->always_append_ds);
937 if (status != 0)
938 break;
939 } else if (strcasecmp ("TTLFactor", child->key) == 0) {
940 double tmp = NAN;
941 status = cf_util_get_double (child, &tmp);
942 if (status != 0)
943 break;
944 if (tmp >= 2.0) {
945 host->ttl_factor = tmp;
946 } else if (tmp >= 1.0) {
947 NOTICE ("write_riemann plugin: The configured "
948 "TTLFactor is very small "
949 "(%.1f). A value of 2.0 or "
950 "greater is recommended.",
951 tmp);
952 host->ttl_factor = tmp;
953 } else if (tmp > 0.0) {
954 WARNING ("write_riemann plugin: The configured "
955 "TTLFactor is too small to be "
956 "useful (%.1f). I'll use it "
957 "since the user knows best, "
958 "but under protest.",
959 tmp);
960 host->ttl_factor = tmp;
961 } else { /* zero, negative and NAN */
962 ERROR ("write_riemann plugin: The configured "
963 "TTLFactor is invalid (%.1f).",
964 tmp);
965 }
966 } else {
967 WARNING("write_riemann plugin: ignoring unknown config "
968 "option: \"%s\"", child->key);
969 }
970 }
971 if (status != 0) {
972 riemann_free (host);
973 return status;
974 }
976 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
977 host->name);
978 ud.data = host;
979 ud.free_func = riemann_free;
981 pthread_mutex_lock (&host->lock);
983 status = plugin_register_write (callback_name, riemann_write, &ud);
985 if (host->use_tcp == 1 && host->batch_mode) {
986 ud.free_func = NULL;
987 plugin_register_flush(callback_name, riemann_batch_flush, &ud);
988 }
989 if (status != 0)
990 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
991 "failed with status %i.",
992 callback_name, status);
993 else /* success */
994 host->reference_count++;
996 status = plugin_register_notification (callback_name,
997 riemann_notification, &ud);
998 if (status != 0)
999 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
1000 "failed with status %i.",
1001 callback_name, status);
1002 else /* success */
1003 host->reference_count++;
1005 if (host->reference_count <= 1)
1006 {
1007 /* Both callbacks failed => free memory.
1008 * We need to unlock here, because riemann_free() will lock.
1009 * This is not a race condition, because we're the only one
1010 * holding a reference. */
1011 pthread_mutex_unlock (&host->lock);
1012 riemann_free (host);
1013 return (-1);
1014 }
1016 host->reference_count--;
1017 pthread_mutex_unlock (&host->lock);
1019 return status;
1020 } /* }}} int riemann_config_node */
1022 static int riemann_config(oconfig_item_t *ci) /* {{{ */
1023 {
1024 int i;
1025 oconfig_item_t *child;
1026 int status;
1028 for (i = 0; i < ci->children_num; i++) {
1029 child = &ci->children[i];
1031 if (strcasecmp("Node", child->key) == 0) {
1032 riemann_config_node (child);
1033 } else if (strcasecmp(child->key, "attribute") == 0) {
1034 char *key = NULL;
1035 char *val = NULL;
1037 if (child->values_num != 2) {
1038 WARNING("riemann attributes need both a key and a value.");
1039 return (-1);
1040 }
1041 if (child->values[0].type != OCONFIG_TYPE_STRING ||
1042 child->values[1].type != OCONFIG_TYPE_STRING) {
1043 WARNING("riemann attribute needs string arguments.");
1044 return (-1);
1045 }
1046 if ((key = strdup(child->values[0].value.string)) == NULL) {
1047 WARNING("cannot allocate memory for attribute key.");
1048 return (-1);
1049 }
1050 if ((val = strdup(child->values[1].value.string)) == NULL) {
1051 WARNING("cannot allocate memory for attribute value.");
1052 return (-1);
1053 }
1054 strarray_add(&riemann_attrs, &riemann_attrs_num, key);
1055 strarray_add(&riemann_attrs, &riemann_attrs_num, val);
1056 DEBUG("write_riemann: got attr: %s => %s", key, val);
1057 sfree(key);
1058 sfree(val);
1059 } else if (strcasecmp(child->key, "tag") == 0) {
1060 char *tmp = NULL;
1061 status = cf_util_get_string(child, &tmp);
1062 if (status != 0)
1063 continue;
1065 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
1066 DEBUG("write_riemann plugin: Got tag: %s", tmp);
1067 sfree (tmp);
1068 } else {
1069 WARNING ("write_riemann plugin: Ignoring unknown "
1070 "configuration option \"%s\" at top level.",
1071 child->key);
1072 }
1073 }
1074 return (0);
1075 } /* }}} int riemann_config */
1077 void module_register(void)
1078 {
1079 plugin_register_complex_config ("write_riemann", riemann_config);
1080 }
1082 /* vim: set sw=8 sts=8 ts=8 noet : */