Add threshold checks for riemann output.
[collectd.git] / src / write_riemann.c
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  */
23
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"
30
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>
37
38 #define RIEMANN_HOST            "localhost"
39 #define RIEMANN_PORT            "5555"
40 #define RIEMANN_TTL_FACTOR      2.0
41
42 int write_riemann_threshold_check(const data_set_t *, const value_list_t *, int *);
43 int write_riemann_threshold_config(oconfig_item_t *);
44
45 struct riemann_host {
46         char                    *name;
47 #define F_CONNECT                0x01
48         uint8_t                  flags;
49         pthread_mutex_t          lock;
50         _Bool                    store_rates;
51         _Bool                    always_append_ds;
52         char                    *node;
53         char                    *service;
54         _Bool                    use_tcp;
55         int                      s;
56         double                   ttl_factor;
57
58         int                      reference_count;
59 };
60
61 static char     **riemann_tags;
62 static size_t     riemann_tags_num;
63 static char     **riemann_attrs;
64 static size_t     riemann_attrs_num;
65
66 static void riemann_event_protobuf_free (Event *event) /* {{{ */
67 {
68         size_t i;
69
70         if (event == NULL)
71                 return;
72
73         sfree (event->state);
74         sfree (event->service);
75         sfree (event->host);
76         sfree (event->description);
77
78         strarray_free (event->tags, event->n_tags);
79         event->tags = NULL;
80         event->n_tags = 0;
81
82         for (i = 0; i < event->n_attributes; i++)
83         {
84                 sfree (event->attributes[i]->key);
85                 sfree (event->attributes[i]->value);
86                 sfree (event->attributes[i]);
87         }
88         sfree (event->attributes);
89         event->n_attributes = 0;
90
91         sfree (event);
92 } /* }}} void riemann_event_protobuf_free */
93
94 static void riemann_msg_protobuf_free (Msg *msg) /* {{{ */
95 {
96         size_t i;
97
98         if (msg == NULL)
99                 return;
100
101         for (i = 0; i < msg->n_events; i++)
102         {
103                 riemann_event_protobuf_free (msg->events[i]);
104                 msg->events[i] = NULL;
105         }
106
107         sfree (msg->events);
108         msg->n_events = 0;
109
110         sfree (msg);
111 } /* }}} void riemann_msg_protobuf_free */
112
113 /* host->lock must be held when calling this function. */
114 static int riemann_connect(struct riemann_host *host) /* {{{ */
115 {
116         int                      e;
117         struct addrinfo         *ai, *res, hints;
118         char const              *node;
119         char const              *service;
120
121         if (host->flags & F_CONNECT)
122                 return 0;
123
124         memset(&hints, 0, sizeof(hints));
125         memset(&service, 0, sizeof(service));
126         hints.ai_family = AF_UNSPEC;
127         hints.ai_socktype = host->use_tcp ? SOCK_STREAM : SOCK_DGRAM;
128 #ifdef AI_ADDRCONFIG
129         hints.ai_flags |= AI_ADDRCONFIG;
130 #endif
131
132         node = (host->node != NULL) ? host->node : RIEMANN_HOST;
133         service = (host->service != NULL) ? host->service : RIEMANN_PORT;
134
135         if ((e = getaddrinfo(node, service, &hints, &res)) != 0) {
136                 ERROR ("write_riemann plugin: Unable to resolve host \"%s\": %s",
137                         node, gai_strerror(e));
138                 return -1;
139         }
140
141         host->s = -1;
142         for (ai = res; ai != NULL; ai = ai->ai_next) {
143                 if ((host->s = socket(ai->ai_family,
144                                       ai->ai_socktype,
145                                       ai->ai_protocol)) == -1) {
146                         continue;
147                 }
148
149                 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
150                         close(host->s);
151                         host->s = -1;
152                         continue;
153                 }
154
155                 host->flags |= F_CONNECT;
156                 DEBUG("write_riemann plugin: got a succesful connection for: %s:%s",
157                                 node, service);
158                 break;
159         }
160
161         freeaddrinfo(res);
162
163         if (host->s < 0) {
164                 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
165                                 node, service);
166                 return -1;
167         }
168         return 0;
169 } /* }}} int riemann_connect */
170
171 /* host->lock must be held when calling this function. */
172 static int riemann_disconnect (struct riemann_host *host) /* {{{ */
173 {
174         if ((host->flags & F_CONNECT) == 0)
175                 return (0);
176
177         close (host->s);
178         host->s = -1;
179         host->flags &= ~F_CONNECT;
180
181         return (0);
182 } /* }}} int riemann_disconnect */
183
184 static int riemann_send_msg (struct riemann_host *host, const Msg *msg) /* {{{ */
185 {
186         int status = 0;
187         u_char *buffer = NULL;
188         size_t  buffer_len;
189
190         status = riemann_connect (host);
191         if (status != 0)
192                 return status;
193
194         buffer_len = msg__get_packed_size(msg);
195
196         if (host->use_tcp)
197                 buffer_len += 4;
198
199         buffer = malloc (buffer_len);
200         if (buffer == NULL) {
201                 ERROR ("write_riemann plugin: malloc failed.");
202                 return ENOMEM;
203         }
204         memset (buffer, 0, buffer_len);
205
206         if (host->use_tcp)
207         {
208                 uint32_t length = htonl ((uint32_t) (buffer_len - 4));
209                 memcpy (buffer, &length, 4);
210                 msg__pack(msg, buffer + 4);
211         }
212         else
213         {
214                 msg__pack(msg, buffer);
215         }
216
217         status = (int) swrite (host->s, buffer, buffer_len);
218         if (status != 0)
219         {
220                 char errbuf[1024];
221                 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s",
222                                 (host->node != NULL) ? host->node : RIEMANN_HOST,
223                                 (host->service != NULL) ? host->service : RIEMANN_PORT,
224                                 sstrerror (errno, errbuf, sizeof (errbuf)));
225                 sfree (buffer);
226                 return -1;
227         }
228
229         sfree (buffer);
230         return 0;
231 } /* }}} int riemann_send_msg */
232
233 static int riemann_recv_ack(struct riemann_host *host) /* {{{ */
234 {
235         int status = 0;
236         Msg *msg = NULL;
237         uint32_t header;
238
239         status = (int) sread (host->s, &header, 4);
240
241         if (status != 0)
242                 return -1;
243
244         size_t size = ntohl(header);
245
246         // Buffer on the stack since acknowledges are typically small.
247         u_char buffer[size];
248         memset (buffer, 0, size);
249
250         status = (int) sread (host->s, buffer, size);
251
252         if (status != 0)
253                 return status;
254
255         msg = msg__unpack (NULL, size, buffer);
256
257         if (msg == NULL)
258                 return -1;
259
260         if (!msg->ok)
261         {
262                 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s acknowledgement message reported error: %s",
263                                 (host->node != NULL) ? host->node : RIEMANN_HOST,
264                                 (host->service != NULL) ? host->service : RIEMANN_PORT,
265                                 msg->error);
266
267                 msg__free_unpacked(msg, NULL);
268                 return -1;
269         }
270
271         msg__free_unpacked (msg, NULL);
272         return 0;
273 } /* }}} int riemann_recv_ack */
274
275 /**
276  * Function to send messages (Msg) to riemann.
277  *
278  * Acquires the host lock, disconnects on errors.
279  */
280 static int riemann_send(struct riemann_host *host, Msg const *msg) /* {{{ */
281 {
282         int status = 0;
283         pthread_mutex_lock (&host->lock);
284
285         status = riemann_send_msg(host, msg);
286         if (status != 0) {
287                 riemann_disconnect (host);
288                 pthread_mutex_unlock (&host->lock);
289                 return status;
290         }
291
292         /*
293          * For TCP we need to receive message acknowledgemenent.
294          */
295         if (host->use_tcp)
296         {
297                 status = riemann_recv_ack(host);
298
299                 if (status != 0)
300                 {
301                         riemann_disconnect (host);
302                         pthread_mutex_unlock (&host->lock);
303                         return status;
304                 }
305         }
306
307         pthread_mutex_unlock (&host->lock);
308         return 0;
309 } /* }}} int riemann_send */
310
311 static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
312 {
313         return (strarray_add (&event->tags, &event->n_tags, tag));
314 } /* }}} int riemann_event_add_tag */
315
316 static int riemann_event_add_attribute (Event *event, /* {{{ */
317                 char const *key, char const *value)
318 {
319         Attribute **new_attributes;
320         Attribute *a;
321
322         new_attributes = realloc (event->attributes,
323                         sizeof (*event->attributes) * (event->n_attributes + 1));
324         if (new_attributes == NULL)
325         {
326                 ERROR ("write_riemann plugin: realloc failed.");
327                 return (ENOMEM);
328         }
329         event->attributes = new_attributes;
330
331         a = malloc (sizeof (*a));
332         if (a == NULL)
333         {
334                 ERROR ("write_riemann plugin: malloc failed.");
335                 return (ENOMEM);
336         }
337         attribute__init (a);
338
339         a->key = strdup (key);
340         if (value != NULL)
341                 a->value = strdup (value);
342
343         event->attributes[event->n_attributes] = a;
344         event->n_attributes++;
345
346         return (0);
347 } /* }}} int riemann_event_add_attribute */
348
349 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
350                 notification_t const *n)
351 {
352         Msg *msg;
353         Event *event;
354         char service_buffer[6 * DATA_MAX_NAME_LEN];
355         char const *severity;
356         notification_meta_t *meta;
357         int i;
358
359         msg = malloc (sizeof (*msg));
360         if (msg == NULL)
361         {
362                 ERROR ("write_riemann plugin: malloc failed.");
363                 return (NULL);
364         }
365         memset (msg, 0, sizeof (*msg));
366         msg__init (msg);
367
368         msg->events = malloc (sizeof (*msg->events));
369         if (msg->events == NULL)
370         {
371                 ERROR ("write_riemann plugin: malloc failed.");
372                 sfree (msg);
373                 return (NULL);
374         }
375
376         event = malloc (sizeof (*event));
377         if (event == NULL)
378         {
379                 ERROR ("write_riemann plugin: malloc failed.");
380                 sfree (msg->events);
381                 sfree (msg);
382                 return (NULL);
383         }
384         memset (event, 0, sizeof (*event));
385         event__init (event);
386
387         msg->events[0] = event;
388         msg->n_events = 1;
389
390         event->host = strdup (n->host);
391         event->time = CDTIME_T_TO_TIME_T (n->time);
392         event->has_time = 1;
393
394         switch (n->severity)
395         {
396                 case NOTIF_OKAY:        severity = "ok"; break;
397                 case NOTIF_WARNING:     severity = "warning"; break;
398                 case NOTIF_FAILURE:     severity = "critical"; break;
399                 default:                severity = "unknown";
400         }
401         event->state = strdup (severity);
402
403         riemann_event_add_tag (event, "notification");
404         if (n->host[0] != 0)
405                 riemann_event_add_attribute (event, "host", n->host);
406         if (n->plugin[0] != 0)
407                 riemann_event_add_attribute (event, "plugin", n->plugin);
408         if (n->plugin_instance[0] != 0)
409                 riemann_event_add_attribute (event, "plugin_instance",
410                                 n->plugin_instance);
411
412         if (n->type[0] != 0)
413                 riemann_event_add_attribute (event, "type", n->type);
414         if (n->type_instance[0] != 0)
415                 riemann_event_add_attribute (event, "type_instance",
416                                 n->type_instance);
417
418         for (i = 0; i < riemann_attrs_num; i += 2)
419                 riemann_event_add_attribute(event,
420                                             riemann_attrs[i],
421                                             riemann_attrs[i +1]);
422
423         for (i = 0; i < riemann_tags_num; i++)
424                 riemann_event_add_tag (event, riemann_tags[i]);
425
426         format_name (service_buffer, sizeof (service_buffer),
427                         /* host = */ "", n->plugin, n->plugin_instance,
428                         n->type, n->type_instance);
429         event->service = strdup (&service_buffer[1]);
430
431         /* Pull in values from threshold and add extra attributes */
432         for (meta = n->meta; meta != NULL; meta = meta->next)
433         {
434                 if (strcasecmp ("CurrentValue", meta->name) == 0 && meta->type == NM_TYPE_DOUBLE)
435                 {
436                         event->metric_d = meta->nm_value.nm_double;
437                         event->has_metric_d = 1;
438                         continue;
439                 }
440
441                 if (meta->type == NM_TYPE_STRING) {
442                         riemann_event_add_attribute (event, meta->name, meta->nm_value.nm_string);
443                         continue;
444                 }
445         }
446
447         DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
448                         "host = \"%s\", service = \"%s\", state = \"%s\"",
449                         event->host, event->service, event->state);
450         return (msg);
451 } /* }}} Msg *riemann_notification_to_protobuf */
452
453 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
454                 data_set_t const *ds,
455                 value_list_t const *vl, size_t index,
456                                          gauge_t const *rates,
457                                          int status)
458 {
459         Event *event;
460         char name_buffer[5 * DATA_MAX_NAME_LEN];
461         char service_buffer[6 * DATA_MAX_NAME_LEN];
462         double ttl;
463         int i;
464
465         event = malloc (sizeof (*event));
466         if (event == NULL)
467         {
468                 ERROR ("write_riemann plugin: malloc failed.");
469                 return (NULL);
470         }
471         memset (event, 0, sizeof (*event));
472         event__init (event);
473
474         event->host = strdup (vl->host);
475         event->time = CDTIME_T_TO_TIME_T (vl->time);
476         event->has_time = 1;
477
478         switch (status) {
479         case STATE_OKAY:
480                 event->state = strdup("ok");
481                 break;
482         case STATE_ERROR:
483                 event->state = strdup("critical");
484                 break;
485         case STATE_WARNING:
486                 event->state = strdup("warning");
487                 break;
488         case STATE_MISSING:
489                 event->state = strdup("unknown");
490                 break;
491         }
492
493         ttl = CDTIME_T_TO_DOUBLE (vl->interval) * host->ttl_factor;
494         event->ttl = (float) ttl;
495         event->has_ttl = 1;
496
497         riemann_event_add_attribute (event, "plugin", vl->plugin);
498         if (vl->plugin_instance[0] != 0)
499                 riemann_event_add_attribute (event, "plugin_instance",
500                                 vl->plugin_instance);
501
502         riemann_event_add_attribute (event, "type", vl->type);
503         if (vl->type_instance[0] != 0)
504                 riemann_event_add_attribute (event, "type_instance",
505                                 vl->type_instance);
506
507         if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
508         {
509                 char ds_type[DATA_MAX_NAME_LEN];
510
511                 ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
512                                 DS_TYPE_TO_STRING(ds->ds[index].type));
513                 riemann_event_add_attribute (event, "ds_type", ds_type);
514         }
515         else
516         {
517                 riemann_event_add_attribute (event, "ds_type",
518                                 DS_TYPE_TO_STRING(ds->ds[index].type));
519         }
520         riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
521         {
522                 char ds_index[DATA_MAX_NAME_LEN];
523
524                 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
525                 riemann_event_add_attribute (event, "ds_index", ds_index);
526         }
527
528         for (i = 0; i < riemann_attrs_num; i += 2)
529                 riemann_event_add_attribute(event,
530                                             riemann_attrs[i],
531                                             riemann_attrs[i +1]);
532
533         for (i = 0; i < riemann_tags_num; i++)
534                 riemann_event_add_tag (event, riemann_tags[i]);
535
536         if (ds->ds[index].type == DS_TYPE_GAUGE)
537         {
538                 event->has_metric_d = 1;
539                 event->metric_d = (double) vl->values[index].gauge;
540         }
541         else if (rates != NULL)
542         {
543                 event->has_metric_d = 1;
544                 event->metric_d = (double) rates[index];
545         }
546         else
547         {
548                 event->has_metric_sint64 = 1;
549                 if (ds->ds[index].type == DS_TYPE_DERIVE)
550                         event->metric_sint64 = (int64_t) vl->values[index].derive;
551                 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
552                         event->metric_sint64 = (int64_t) vl->values[index].absolute;
553                 else
554                         event->metric_sint64 = (int64_t) vl->values[index].counter;
555         }
556
557         format_name (name_buffer, sizeof (name_buffer),
558                         /* host = */ "", vl->plugin, vl->plugin_instance,
559                         vl->type, vl->type_instance);
560         if (host->always_append_ds || (ds->ds_num > 1))
561                 ssnprintf (service_buffer, sizeof (service_buffer),
562                                 "%s/%s", &name_buffer[1], ds->ds[index].name);
563         else
564                 sstrncpy (service_buffer, &name_buffer[1],
565                                 sizeof (service_buffer));
566
567         event->service = strdup (service_buffer);
568
569         DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
570                         "host = \"%s\", service = \"%s\"",
571                         event->host, event->service);
572         return (event);
573 } /* }}} Event *riemann_value_to_protobuf */
574
575 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
576                                             data_set_t const *ds,
577                                             value_list_t const *vl,
578                                             int *statuses)
579 {
580         Msg *msg;
581         size_t i;
582         gauge_t *rates = NULL;
583
584         /* Initialize the Msg structure. */
585         msg = malloc (sizeof (*msg));
586         if (msg == NULL)
587         {
588                 ERROR ("write_riemann plugin: malloc failed.");
589                 return (NULL);
590         }
591         memset (msg, 0, sizeof (*msg));
592         msg__init (msg);
593
594         /* Set up events. First, the list of pointers. */
595         msg->n_events = (size_t) vl->values_len;
596         msg->events = calloc (msg->n_events, sizeof (*msg->events));
597         if (msg->events == NULL)
598         {
599                 ERROR ("write_riemann plugin: calloc failed.");
600                 riemann_msg_protobuf_free (msg);
601                 return (NULL);
602         }
603
604         if (host->store_rates)
605         {
606                 rates = uc_get_rate (ds, vl);
607                 if (rates == NULL)
608                 {
609                         ERROR ("write_riemann plugin: uc_get_rate failed.");
610                         riemann_msg_protobuf_free (msg);
611                         return (NULL);
612                 }
613         }
614
615         for (i = 0; i < msg->n_events; i++)
616         {
617                 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
618                                                             (int) i, rates, statuses[i]);
619                 if (msg->events[i] == NULL)
620                 {
621                         riemann_msg_protobuf_free (msg);
622                         sfree (rates);
623                         return (NULL);
624                 }
625         }
626
627         sfree (rates);
628         return (msg);
629 } /* }}} Msg *riemann_value_list_to_protobuf */
630
631 static int riemann_notification(const notification_t *n, user_data_t *ud) /* {{{ */
632 {
633         int                      status;
634         struct riemann_host     *host = ud->data;
635         Msg                     *msg;
636
637         msg = riemann_notification_to_protobuf (host, n);
638         if (msg == NULL)
639                 return (-1);
640
641         status = riemann_send (host, msg);
642         if (status != 0)
643                 ERROR ("write_riemann plugin: riemann_send failed with status %i",
644                                 status);
645
646         riemann_msg_protobuf_free (msg);
647         return (status);
648 } /* }}} int riemann_notification */
649
650 static int riemann_write(const data_set_t *ds, /* {{{ */
651               const value_list_t *vl,
652               user_data_t *ud)
653 {
654         int                      status;
655         int                      statuses[vl->values_len];
656         struct riemann_host     *host = ud->data;
657         Msg                     *msg;
658
659         write_riemann_threshold_check(ds, vl, statuses);
660         msg = riemann_value_list_to_protobuf (host, ds, vl, statuses);
661         if (msg == NULL)
662                 return (-1);
663
664         status = riemann_send (host, msg);
665         if (status != 0)
666                 ERROR ("write_riemann plugin: riemann_send failed with status %i",
667                                 status);
668
669         riemann_msg_protobuf_free (msg);
670         return status;
671 } /* }}} int riemann_write */
672
673 static void riemann_free(void *p) /* {{{ */
674 {
675         struct riemann_host     *host = p;
676
677         if (host == NULL)
678                 return;
679
680         pthread_mutex_lock (&host->lock);
681
682         host->reference_count--;
683         if (host->reference_count > 0)
684         {
685                 pthread_mutex_unlock (&host->lock);
686                 return;
687         }
688
689         riemann_disconnect (host);
690
691         sfree(host->service);
692         pthread_mutex_destroy (&host->lock);
693         sfree(host);
694 } /* }}} void riemann_free */
695
696 static int riemann_config_node(oconfig_item_t *ci) /* {{{ */
697 {
698         struct riemann_host     *host = NULL;
699         int                      status = 0;
700         int                      i;
701         oconfig_item_t          *child;
702         char                     callback_name[DATA_MAX_NAME_LEN];
703         user_data_t              ud;
704
705         if ((host = calloc(1, sizeof (*host))) == NULL) {
706                 ERROR ("write_riemann plugin: calloc failed.");
707                 return ENOMEM;
708         }
709         pthread_mutex_init (&host->lock, NULL);
710         host->reference_count = 1;
711         host->node = NULL;
712         host->service = NULL;
713         host->store_rates = 1;
714         host->always_append_ds = 0;
715         host->use_tcp = 0;
716         host->ttl_factor = RIEMANN_TTL_FACTOR;
717
718         status = cf_util_get_string (ci, &host->name);
719         if (status != 0) {
720                 WARNING("write_riemann plugin: Required host name is missing.");
721                 riemann_free (host);
722                 return -1;
723         }
724
725         for (i = 0; i < ci->children_num; i++) {
726                 /*
727                  * The code here could be simplified but makes room
728                  * for easy adding of new options later on.
729                  */
730                 child = &ci->children[i];
731                 status = 0;
732
733                 if (strcasecmp ("Host", child->key) == 0) {
734                         status = cf_util_get_string (child, &host->node);
735                         if (status != 0)
736                                 break;
737                 } else if (strcasecmp ("Threshold", child->key) == 0) {
738                         status = write_riemann_threshold_config(child);
739                         if (status != 0)
740                                 break;
741                 } else if (strcasecmp ("Port", child->key) == 0) {
742                         status = cf_util_get_service (child, &host->service);
743                         if (status != 0) {
744                                 ERROR ("write_riemann plugin: Invalid argument "
745                                                 "configured for the \"Port\" "
746                                                 "option.");
747                                 break;
748                         }
749                 } else if (strcasecmp ("Protocol", child->key) == 0) {
750                         char tmp[16];
751                         status = cf_util_get_string_buffer (child,
752                                         tmp, sizeof (tmp));
753                         if (status != 0)
754                         {
755                                 ERROR ("write_riemann plugin: cf_util_get_"
756                                                 "string_buffer failed with "
757                                                 "status %i.", status);
758                                 break;
759                         }
760
761                         if (strcasecmp ("UDP", tmp) == 0)
762                                 host->use_tcp = 0;
763                         else if (strcasecmp ("TCP", tmp) == 0)
764                                 host->use_tcp = 1;
765                         else
766                                 WARNING ("write_riemann plugin: The value "
767                                                 "\"%s\" is not valid for the "
768                                                 "\"Protocol\" option. Use "
769                                                 "either \"UDP\" or \"TCP\".",
770                                                 tmp);
771                 } else if (strcasecmp ("StoreRates", child->key) == 0) {
772                         status = cf_util_get_boolean (child, &host->store_rates);
773                         if (status != 0)
774                                 break;
775                 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
776                         status = cf_util_get_boolean (child,
777                                         &host->always_append_ds);
778                         if (status != 0)
779                                 break;
780                 } else if (strcasecmp ("TTLFactor", child->key) == 0) {
781                         double tmp = NAN;
782                         status = cf_util_get_double (child, &tmp);
783                         if (status != 0)
784                                 break;
785                         if (tmp >= 2.0) {
786                                 host->ttl_factor = tmp;
787                         } else if (tmp >= 1.0) {
788                                 NOTICE ("write_riemann plugin: The configured "
789                                                 "TTLFactor is very small "
790                                                 "(%.1f). A value of 2.0 or "
791                                                 "greater is recommended.",
792                                                 tmp);
793                                 host->ttl_factor = tmp;
794                         } else if (tmp > 0.0) {
795                                 WARNING ("write_riemann plugin: The configured "
796                                                 "TTLFactor is too small to be "
797                                                 "useful (%.1f). I'll use it "
798                                                 "since the user knows best, "
799                                                 "but under protest.",
800                                                 tmp);
801                                 host->ttl_factor = tmp;
802                         } else { /* zero, negative and NAN */
803                                 ERROR ("write_riemann plugin: The configured "
804                                                 "TTLFactor is invalid (%.1f).",
805                                                 tmp);
806                         }
807                 } else {
808                         WARNING("write_riemann plugin: ignoring unknown config "
809                                 "option: \"%s\"", child->key);
810                 }
811         }
812         if (status != 0) {
813                 riemann_free (host);
814                 return status;
815         }
816
817         ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
818                         host->name);
819         ud.data = host;
820         ud.free_func = riemann_free;
821
822         pthread_mutex_lock (&host->lock);
823
824         status = plugin_register_write (callback_name, riemann_write, &ud);
825         if (status != 0)
826                 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
827                                 "failed with status %i.",
828                                 callback_name, status);
829         else /* success */
830                 host->reference_count++;
831
832         status = plugin_register_notification (callback_name,
833                         riemann_notification, &ud);
834         if (status != 0)
835                 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
836                                 "failed with status %i.",
837                                 callback_name, status);
838         else /* success */
839                 host->reference_count++;
840
841         if (host->reference_count <= 1)
842         {
843                 /* Both callbacks failed => free memory.
844                  * We need to unlock here, because riemann_free() will lock.
845                  * This is not a race condition, because we're the only one
846                  * holding a reference. */
847                 pthread_mutex_unlock (&host->lock);
848                 riemann_free (host);
849                 return (-1);
850         }
851
852         host->reference_count--;
853         pthread_mutex_unlock (&host->lock);
854
855         return status;
856 } /* }}} int riemann_config_node */
857
858 static int riemann_config(oconfig_item_t *ci) /* {{{ */
859 {
860         int              i;
861         oconfig_item_t  *child;
862         int              status;
863
864         for (i = 0; i < ci->children_num; i++)  {
865                 child = &ci->children[i];
866
867                 if (strcasecmp("Node", child->key) == 0) {
868                         riemann_config_node (child);
869                 } else if (strcasecmp(child->key, "attribute") == 0) {
870                         char *key = NULL;
871                         char *val = NULL;
872
873                         if (child->values_num != 2) {
874                                 WARNING("riemann attributes need both a key and a value.");
875                                 return (-1);
876                         }
877                         if (child->values[0].type != OCONFIG_TYPE_STRING ||
878                             child->values[1].type != OCONFIG_TYPE_STRING) {
879                                 WARNING("riemann attribute needs string arguments.");
880                                 return (-1);
881                         }
882                         if ((key = strdup(child->values[0].value.string)) == NULL) {
883                                 WARNING("cannot allocate memory for attribute key.");
884                                 return (-1);
885                         }
886                         if ((val = strdup(child->values[1].value.string)) == NULL) {
887                                 WARNING("cannot allocate memory for attribute value.");
888                                 return (-1);
889                         }
890                         strarray_add(&riemann_attrs, &riemann_attrs_num, key);
891                         strarray_add(&riemann_attrs, &riemann_attrs_num, val);
892                         DEBUG("write_riemann: got attr: %s => %s", key, val);
893                         sfree(key);
894                         sfree(val);
895                 } else if (strcasecmp(child->key, "tag") == 0) {
896                         char *tmp = NULL;
897                         status = cf_util_get_string(child, &tmp);
898                         if (status != 0)
899                                 continue;
900
901                         strarray_add (&riemann_tags, &riemann_tags_num, tmp);
902                         DEBUG("write_riemann plugin: Got tag: %s", tmp);
903                         sfree (tmp);
904                 } else {
905                         WARNING ("write_riemann plugin: Ignoring unknown "
906                                  "configuration option \"%s\" at top level.",
907                                  child->key);
908                 }
909         }
910         return (0);
911 } /* }}} int riemann_config */
912
913 void module_register(void)
914 {
915         plugin_register_complex_config ("write_riemann", riemann_config);
916 }
917
918 /* vim: set sw=8 sts=8 ts=8 noet : */